ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
elc.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <config/config.hpp>
23
24#ifdef ESPRESSO_P3M
25
27
30
31#include "BoxGeometry.hpp"
32#include "Particle.hpp"
34#include "ParticleRange.hpp"
35#include "aosoa_pack.hpp"
37#include "communication.hpp"
38#include "errorhandling.hpp"
39#include "system/System.hpp"
40
41#include <utils/math/sqr.hpp>
42
43#include <Kokkos_Core.hpp>
44
45#include <boost/mpi/collectives/all_reduce.hpp>
46
47#include <algorithm>
48#include <cassert>
49#include <cmath>
50#include <cstddef>
51#include <functional>
52#include <numbers>
53#include <stdexcept>
54#include <utility>
55#include <variant>
56#include <vector>
57
58/** \name Product decomposition data organization
59 * For the cell blocks it is assumed that the lower blocks part is in the
60 * lower half. This has to have positive sign, so that has to be first.
61 */
62/**@{*/
63#define POQESP 0
64#define POQECP 1
65#define POQESM 2
66#define POQECM 3
67
68#define PQESSP 0
69#define PQESCP 1
70#define PQECSP 2
71#define PQECCP 3
72#define PQESSM 4
73#define PQESCM 5
74#define PQECSM 6
75#define PQECCM 7
76/**@}*/
77
78/** ELC axes (x and y directions)*/
79enum class PoQ : int { P, Q };
80/** ELC charge sum/assign protocol: real charges, image charges, or both. */
81enum class ChargeProtocol : int { REAL, IMAGE, BOTH };
82
83/** temporary buffers for product decomposition */
84static std::vector<double> partblk;
85/** collected data from the other cells */
86static double gblcblk[8];
87
88/** structure for caching sin and cos values */
89struct SCCache {
90 double s, c;
91};
92
93/** Cached sin/cos values along the x-axis and y-axis */
94/**@{*/
95static std::vector<SCCache> scxcache;
96static std::vector<SCCache> scycache;
97/**@}*/
98
99/**
100 * @brief Calculate cached sin/cos values for one direction.
101 *
102 * @tparam dir Index of the dimension to consider (e.g. 0 for x ...).
103 *
104 * @param particles Particle to calculate values for
105 * @param n_freq Number of frequencies to calculate per particle
106 * @param u Inverse box length
107 * @return Calculated values.
108 */
109template <std::size_t dir>
110static std::vector<SCCache> calc_sc_cache(ParticleRange const &particles,
111 std::size_t n_freq, double u) {
112 auto constexpr c_2pi = 2. * std::numbers::pi;
113 auto const n_part = particles.size();
114 std::vector<SCCache> ret(n_freq * n_part);
115
116 for (std::size_t freq = 1; freq <= n_freq; freq++) {
117 auto const pref = c_2pi * u * static_cast<double>(freq);
118
119 std::size_t o = (freq - 1) * n_part;
120 for (auto const &p : particles) {
121 auto const arg = pref * p.pos()[dir];
122 ret[o++] = {sin(arg), cos(arg)};
123 }
124 }
125
126 return ret;
127}
128
129static std::pair<std::size_t, std::size_t>
130prepare_sc_cache(ParticleRange const &particles, BoxGeometry const &box_geo,
131 double far_cut) {
132 assert(far_cut >= 0.);
133 auto const n_freq_x =
134 static_cast<std::size_t>(std::ceil(far_cut * box_geo.length()[0]) + 1.);
135 auto const n_freq_y =
136 static_cast<std::size_t>(std::ceil(far_cut * box_geo.length()[1]) + 1.);
137 auto const u_x = box_geo.length_inv()[0];
138 auto const u_y = box_geo.length_inv()[1];
139 scxcache = calc_sc_cache<0>(particles, n_freq_x, u_x);
140 scycache = calc_sc_cache<1>(particles, n_freq_y, u_y);
141 return {n_freq_x, n_freq_y};
142}
143
144/*****************************************************************/
145/* data distribution */
146/*****************************************************************/
147
148static void clear_vec(double *pdc, std::size_t size) {
149 for (std::size_t i = 0; i < size; i++)
150 pdc[i] = 0.;
151}
152
153static void copy_vec(double *pdc_d, double const *pdc_s, std::size_t size) {
154 for (std::size_t i = 0; i < size; i++)
155 pdc_d[i] = pdc_s[i];
156}
157
158static void add_vec(double *pdc_d, double const *pdc_s1, double const *pdc_s2,
159 std::size_t size) {
160 for (std::size_t i = 0; i < size; i++)
161 pdc_d[i] = pdc_s1[i] + pdc_s2[i];
162}
163
164static void addscale_vec(double *pdc_d, double scale, double const *pdc_s1,
165 double const *pdc_s2, std::size_t size) {
166 for (std::size_t i = 0; i < size; i++)
167 pdc_d[i] = scale * pdc_s1[i] + pdc_s2[i];
168}
169
170static void scale_vec(double scale, double *pdc, std::size_t size) {
171 for (std::size_t i = 0; i < size; i++)
172 pdc[i] *= scale;
173}
174
175static double *block(double *p, std::size_t index, std::size_t size) {
176 return &p[index * size];
177}
178
179static void distribute(std::size_t size) {
180 assert(size <= 8);
181 double send_buf[8];
182 copy_vec(send_buf, gblcblk, size);
183 boost::mpi::all_reduce(comm_cart, send_buf, static_cast<int>(size), gblcblk,
184 std::plus<>());
185}
186
187void ElectrostaticLayerCorrection::check_gap(Particle const &p) const {
188 if (p.q() != 0.) {
189 auto const z = p.pos()[2];
190 if (z < 0. or z > elc.box_h) {
191 runtimeErrorMsg() << "Particle " << p.id() << " entered ELC gap "
192 << "region by " << ((z < 0.) ? z : z - elc.box_h);
193 }
194 }
195}
196
197/*****************************************************************/
198/* dipole terms */
199/*****************************************************************/
200
201/** Calculate the dipole force.
202 * See @cite yeh99a.
203 */
204void ElectrostaticLayerCorrection::add_dipole_force() const {
205 constexpr std::size_t size = 3;
206 auto const &system = get_system();
207 auto const &box_geo = *system.box_geo;
208 auto const particles = system.cell_structure->local_particles();
209 auto const pref = prefactor * 4. * std::numbers::pi / box_geo.volume();
210
211 /* for non-neutral systems, this shift gives the background contribution
212 * (rsp. for this shift, the DM of the background is zero) */
213 auto const shift = box_geo.length_half()[2];
214
215 // collect moments
216
217 gblcblk[0] = 0.; // sum q_i (z_i - L/2)
218 gblcblk[1] = 0.; // sum q_i (z_i - box_h/2)
219 gblcblk[2] = 0.; // sum q_i
220
221 auto const mid = 0.5 * elc.box_h;
222
223 for (auto const &p : particles) {
224 check_gap(p);
225 auto const q = p.q();
226 auto const z = p.pos()[2];
227
228 gblcblk[0] += q * (z - shift);
229 gblcblk[1] += q * (z - mid);
230 gblcblk[2] += q;
231
233 if (z < elc.space_layer) {
234 gblcblk[0] += elc.delta_mid_bot * q * (-z - shift);
235 gblcblk[2] += elc.delta_mid_bot * q;
236 }
237 if (z > (elc.box_h - elc.space_layer)) {
238 gblcblk[0] += elc.delta_mid_top * q * (2. * elc.box_h - z - shift);
239 gblcblk[2] += elc.delta_mid_top * q;
240 }
241 }
242 }
243
244 gblcblk[0] *= pref;
245 gblcblk[1] *= pref / elc.box_h * box_geo.length()[2];
246 gblcblk[2] *= pref;
247
248 distribute(size);
249
250 // Yeh + Berkowitz dipole term @cite yeh99a
251 auto field_tot = gblcblk[0];
252
253 // Constant potential contribution
254 if (elc.const_pot) {
255 auto const field_induced = gblcblk[1];
256 auto const field_applied = elc.pot_diff / elc.box_h;
258 }
259
260 for (auto &p : particles) {
261 p.force()[2] -= field_tot * p.q();
262
263 if (!elc.neutralize) {
264 // SUBTRACT the forces of the P3M homogeneous neutralizing background
265 p.force()[2] += gblcblk[2] * p.q() * (p.pos()[2] - shift);
266 }
267 }
268}
269
270/** Calculate the dipole energy.
271 * See @cite yeh99a.
272 */
273double ElectrostaticLayerCorrection::dipole_energy() const {
274 constexpr std::size_t size = 7;
275 auto const &system = get_system();
276 auto const &box_geo = *system.box_geo;
277 auto const particles = system.cell_structure->local_particles();
278 auto const pref = prefactor * 2. * std::numbers::pi / box_geo.volume();
279 auto const lz = box_geo.length()[2];
280 /* for nonneutral systems, this shift gives the background contribution
281 (rsp. for this shift, the DM of the background is zero) */
282 auto const shift = box_geo.length_half()[2];
283
284 // collect moments
285
286 gblcblk[0] = 0.; // sum q_i primary box
287 gblcblk[1] = 0.; // sum q_i boundary layers
288 gblcblk[2] = 0.; // sum q_i (z_i - L/2) primary box
289 gblcblk[3] = 0.; // sum q_i (z_i - L/2) boundary layers
290 gblcblk[4] = 0.; // sum q_i (z_i - L/2)^2 primary box
291 gblcblk[5] = 0.; // sum q_i (z_i - L/2)^2 boundary layers
292 gblcblk[6] = 0.; // sum q_i z_i primary box
293
294 for (auto const &p : particles) {
295 check_gap(p);
296 auto const q = p.q();
297 auto const z = p.pos()[2];
298
299 gblcblk[0] += q;
300 gblcblk[2] += q * (z - shift);
301 gblcblk[4] += q * (Utils::sqr(z - shift));
302 gblcblk[6] += q * z;
303
305 if (z < elc.space_layer) {
306 gblcblk[1] += elc.delta_mid_bot * q;
307 gblcblk[3] += elc.delta_mid_bot * q * (-z - shift);
308 gblcblk[5] += elc.delta_mid_bot * q * (Utils::sqr(-z - shift));
309 }
310 if (z > (elc.box_h - elc.space_layer)) {
311 gblcblk[1] += elc.delta_mid_top * q;
312 gblcblk[3] += elc.delta_mid_top * q * (2. * elc.box_h - z - shift);
313 gblcblk[5] +=
314 elc.delta_mid_top * q * (Utils::sqr(2. * elc.box_h - z - shift));
315 }
316 }
317 }
318
319 distribute(size);
320
321 // Yeh + Berkowitz term @cite yeh99a
322 auto energy = 2. * pref * (Utils::sqr(gblcblk[2]) + gblcblk[2] * gblcblk[3]);
323
324 if (!elc.neutralize) {
325 // SUBTRACT the energy of the P3M homogeneous neutralizing background
326 energy += 2. * pref *
327 (-gblcblk[0] * gblcblk[4] -
328 (.25 - .5 / 3.) * Utils::sqr(gblcblk[0] * lz));
329 }
330
332 if (elc.const_pot) {
333 // zero potential difference contribution
334 energy -= 2. * pref / elc.box_h * lz * gblcblk[6] *
335 (gblcblk[6] - elc.box_h * gblcblk[0]);
336 // external potential shift contribution
337 energy -= 2. * elc.pot_diff / elc.box_h * gblcblk[6];
338 }
339
340 /* counter the P3M homogeneous background contribution to the
341 boundaries. We never need that, since a homogeneous background
342 spanning the artificial boundary layers is aphysical. */
343 energy +=
344 pref * (-(gblcblk[1] * gblcblk[4] + gblcblk[0] * gblcblk[5]) -
345 (.5 - 1. / 3.) * gblcblk[0] * gblcblk[1] * Utils::sqr(lz));
346 }
347
348 return this_node == 0 ? energy : 0.;
349}
350
351/*****************************************************************/
352
353struct ImageSum {
354 double delta;
355 double shift;
356 double h; // plate separation
357 double dci; // delta complement inverse
358
359 ImageSum(double delta, double shift, double h)
360 : delta{delta}, shift{shift}, h{h}, dci{1. / (1. - delta)} {}
361
362 /** @brief Image sum from the bottom layer. */
363 double b(double q, double z) const {
364 return q * dci * (z - 2. * delta * h * dci) - q * dci * shift;
365 }
366
367 /** @brief Image sum from the top layer. */
368 double t(double q, double z) const {
369 return q * dci * (z + 2. * delta * h * dci) - q * dci * shift;
370 }
371};
372
373double ElectrostaticLayerCorrection::z_energy() const {
374 constexpr std::size_t size = 4;
375 auto const &system = get_system();
376 auto const &box_geo = *system.box_geo;
377 auto const particles = system.cell_structure->local_particles();
378 auto const xy_area_inv = box_geo.length_inv()[0] * box_geo.length_inv()[1];
379 auto const pref = prefactor * 2. * std::numbers::pi * xy_area_inv;
380
381 /* for non-neutral systems, this shift gives the background contribution
382 * (rsp. for this shift, the DM of the background is zero) */
383 auto const shift = box_geo.length_half()[2];
384
386 if (elc.const_pot) {
387 // metallic boundaries
388 clear_vec(gblcblk, size);
389 for (auto const &p : particles) {
390 auto const z = p.pos()[2];
391 auto const q = p.q();
392 gblcblk[0] += q;
393 gblcblk[1] += q * (z - shift);
394 if (z < elc.space_layer) {
395 gblcblk[2] -= elc.delta_mid_bot * q;
396 gblcblk[3] -= elc.delta_mid_bot * q * (-z - shift);
397 }
398 if (z > (elc.box_h - elc.space_layer)) {
399 gblcblk[2] += elc.delta_mid_top * q;
400 gblcblk[3] += elc.delta_mid_top * q * (2. * elc.box_h - z - shift);
401 }
402 }
403 } else {
404 // dielectric boundaries
405 auto const delta = elc.delta_mid_top * elc.delta_mid_bot;
406 auto const fac_delta_mid_bot = elc.delta_mid_bot / (1. - delta);
407 auto const fac_delta_mid_top = elc.delta_mid_top / (1. - delta);
408 auto const fac_delta = delta / (1. - delta);
409 clear_vec(gblcblk, size);
410 auto const h = elc.box_h;
411 ImageSum const image_sum{delta, shift, h};
412 for (auto const &p : particles) {
413 auto const z = p.pos()[2];
414 auto const q = p.q();
415 gblcblk[0] += q;
416 gblcblk[1] += q * (z - shift);
418 if (z < elc.space_layer) {
419 gblcblk[2] += fac_delta * (elc.delta_mid_bot + 1.) * q;
420 gblcblk[3] +=
421 q * (image_sum.b(elc.delta_mid_bot * delta, -(2. * h + z)) +
422 image_sum.b(delta, -(2. * h - z)));
423 } else {
424 gblcblk[2] += fac_delta_mid_bot * (1. + elc.delta_mid_top) * q;
425 gblcblk[3] += q * (image_sum.b(elc.delta_mid_bot, -z) +
426 image_sum.b(delta, -(2. * h - z)));
427 }
428 if (z > (h - elc.space_layer)) {
429 // note the minus sign here which is required due to |z_i-z_j|
430 gblcblk[2] -= fac_delta * (elc.delta_mid_top + 1.) * q;
431 gblcblk[3] -=
432 q * (image_sum.t(elc.delta_mid_top * delta, 4. * h - z) +
433 image_sum.t(delta, 2. * h + z));
434 } else {
435 // note the minus sign here which is required due to |z_i-z_j|
436 gblcblk[2] -= fac_delta_mid_top * (1. + elc.delta_mid_bot) * q;
437 gblcblk[3] -= q * (image_sum.t(elc.delta_mid_top, 2. * h - z) +
438 image_sum.t(delta, 2. * h + z));
439 }
440 }
441 }
442 }
443 }
444 distribute(size);
445
446 auto const energy = gblcblk[1] * gblcblk[2] - gblcblk[0] * gblcblk[3];
447 return (this_node == 0) ? -pref * energy : 0.;
448}
449
450void ElectrostaticLayerCorrection::add_z_force() const {
451 constexpr std::size_t size = 1;
452 auto const &system = get_system();
453 auto const &box_geo = *system.box_geo;
454 auto const particles = system.cell_structure->local_particles();
455 auto const xy_area_inv = box_geo.length_inv()[0] * box_geo.length_inv()[1];
456 auto const pref = prefactor * 2. * std::numbers::pi * xy_area_inv;
457
459 if (elc.const_pot) {
460 // metallic boundaries
461 clear_vec(gblcblk, size);
462 /* just counter the 2 pi |z| contribution stemming from P3M */
463 for (auto const &p : particles) {
464 auto const z = p.pos()[2];
465 auto const q = p.q();
466 if (z < elc.space_layer)
467 gblcblk[0] -= elc.delta_mid_bot * q;
468 if (z > (elc.box_h - elc.space_layer))
469 gblcblk[0] += elc.delta_mid_top * q;
470 }
471 } else {
472 // dielectric boundaries
473 auto const delta = elc.delta_mid_top * elc.delta_mid_bot;
474 auto const fac_delta_mid_bot = elc.delta_mid_bot / (1. - delta);
475 auto const fac_delta_mid_top = elc.delta_mid_top / (1. - delta);
476 auto const fac_delta = delta / (1. - delta);
477 clear_vec(gblcblk, size);
478 for (auto const &p : particles) {
479 auto const z = p.pos()[2];
480 auto const q = p.q();
481 if (z < elc.space_layer) {
482 gblcblk[0] += fac_delta * (elc.delta_mid_bot + 1.) * q;
483 } else {
484 gblcblk[0] += fac_delta_mid_bot * (elc.delta_mid_top + 1.) * q;
485 }
486 if (z > (elc.box_h - elc.space_layer)) {
487 // note the minus sign here which is required due to |z_i-z_j|
488 gblcblk[0] -= fac_delta * (elc.delta_mid_top + 1.) * q;
489 } else {
490 // note the minus sign here which is required due to |z_i-z_j|
491 gblcblk[0] -= fac_delta_mid_top * (elc.delta_mid_bot + 1.) * q;
492 }
493 }
494 }
495
496 gblcblk[0] *= pref;
497
498 distribute(size);
499
500 for (auto &p : particles) {
501 p.force()[2] += gblcblk[0] * p.q();
502 }
503 }
504}
505
506/*****************************************************************/
507/* PoQ exp sum */
508/*****************************************************************/
509
510/** \name q=0 or p=0 per frequency code */
511/**@{*/
512template <PoQ axis>
513void setup_PoQ(elc_data const &elc, double prefactor, std::size_t index,
514 double omega, ParticleRange const &particles,
515 BoxGeometry const &box_geo) {
516 assert(index >= 1);
517 constexpr std::size_t size = 4;
518 auto const xy_area_inv = box_geo.length_inv()[0] * box_geo.length_inv()[1];
519 auto const pref_di = prefactor * 4. * std::numbers::pi * xy_area_inv;
520 auto const pref = -pref_di / expm1(omega * box_geo.length()[2]);
521 double lclimgebot[4], lclimgetop[4], lclimge[4];
522 double fac_delta_mid_bot = 1., fac_delta_mid_top = 1., fac_delta = 1.;
523
524 if (elc.dielectric_contrast_on) {
525 auto const delta = elc.delta_mid_top * elc.delta_mid_bot;
526 auto const fac_elc = 1. / (1. - delta * exp(-omega * 2. * elc.box_h));
530 }
531
532 clear_vec(lclimge, size);
533 clear_vec(gblcblk, size);
534 auto const &sc_cache = (axis == PoQ::P) ? scxcache : scycache;
535
536 std::size_t ic = 0;
537 auto const o = (index - 1) * particles.size();
538 for (auto const &p : particles) {
539 auto const z = p.pos()[2];
540 auto const q = p.q();
541 auto e = exp(omega * z);
542
543 partblk[size * ic + POQESM] = q * sc_cache[o + ic].s / e;
544 partblk[size * ic + POQESP] = q * sc_cache[o + ic].s * e;
545 partblk[size * ic + POQECM] = q * sc_cache[o + ic].c / e;
546 partblk[size * ic + POQECP] = q * sc_cache[o + ic].c * e;
547
548 add_vec(gblcblk, gblcblk, block(partblk.data(), ic, size), size);
549
550 if (elc.dielectric_contrast_on) {
551 if (z < elc.space_layer) { // handle the lower case first
552 // negative sign is okay here as the image is located at -z
553
554 e = exp(-omega * z);
555
556 auto const scale = q * elc.delta_mid_bot;
557
558 lclimgebot[POQESM] = sc_cache[o + ic].s / e;
559 lclimgebot[POQESP] = sc_cache[o + ic].s * e;
560 lclimgebot[POQECM] = sc_cache[o + ic].c / e;
561 lclimgebot[POQECP] = sc_cache[o + ic].c * e;
562
563 addscale_vec(gblcblk, scale, lclimgebot, gblcblk, size);
564
565 e = (exp(omega * (-z - 2. * elc.box_h)) * elc.delta_mid_bot +
566 exp(omega * (+z - 2. * elc.box_h))) *
567 fac_delta;
568 } else {
569 e = (exp(-omega * z) +
570 exp(omega * (z - 2. * elc.box_h)) * elc.delta_mid_top) *
572 }
573
574 lclimge[POQESP] += q * sc_cache[o + ic].s * e;
575 lclimge[POQECP] += q * sc_cache[o + ic].c * e;
576
577 if (z > (elc.box_h - elc.space_layer)) { // handle the upper case now
578 e = exp(omega * (2. * elc.box_h - z));
579
580 auto const scale = q * elc.delta_mid_top;
581
582 lclimgetop[POQESM] = sc_cache[o + ic].s / e;
583 lclimgetop[POQESP] = sc_cache[o + ic].s * e;
584 lclimgetop[POQECM] = sc_cache[o + ic].c / e;
585 lclimgetop[POQECP] = sc_cache[o + ic].c * e;
586
587 addscale_vec(gblcblk, scale, lclimgetop, gblcblk, size);
588
589 e = (exp(omega * (+z - 4. * elc.box_h)) * elc.delta_mid_top +
590 exp(omega * (-z - 2. * elc.box_h))) *
591 fac_delta;
592 } else {
593 e = (exp(omega * (+z - 2. * elc.box_h)) +
594 exp(omega * (-z - 2. * elc.box_h)) * elc.delta_mid_bot) *
596 }
597
598 lclimge[POQESM] += q * sc_cache[o + ic].s * e;
599 lclimge[POQECM] += q * sc_cache[o + ic].c * e;
600 }
601
602 ++ic;
603 }
604
605 scale_vec(pref, gblcblk, size);
606
607 if (elc.dielectric_contrast_on) {
608 scale_vec(pref_di, lclimge, size);
610 }
611}
612
613template <PoQ axis> void add_PoQ_force(ParticleRange const &particles) {
614 constexpr auto i = static_cast<int>(axis);
615 constexpr std::size_t size = 4;
616
617 std::size_t ic = 0;
618 for (auto &p : particles) {
619 auto &force = p.force();
620 force[i] += partblk[size * ic + POQESM] * gblcblk[POQECP] -
621 partblk[size * ic + POQECM] * gblcblk[POQESP] +
622 partblk[size * ic + POQESP] * gblcblk[POQECM] -
623 partblk[size * ic + POQECP] * gblcblk[POQESM];
624 force[2] += partblk[size * ic + POQECM] * gblcblk[POQECP] +
625 partblk[size * ic + POQESM] * gblcblk[POQESP] -
626 partblk[size * ic + POQECP] * gblcblk[POQECM] -
627 partblk[size * ic + POQESP] * gblcblk[POQESM];
628 ++ic;
629 }
630}
631
632static double PoQ_energy(double omega, std::size_t n_part) {
633 constexpr std::size_t size = 4;
634
635 auto energy = 0.;
636 for (std::size_t ic = 0; ic < n_part; ic++) {
637 energy += partblk[size * ic + POQECM] * gblcblk[POQECP] +
638 partblk[size * ic + POQESM] * gblcblk[POQESP] +
639 partblk[size * ic + POQECP] * gblcblk[POQECM] +
640 partblk[size * ic + POQESP] * gblcblk[POQESM];
641 }
642
643 return energy / omega;
644}
645/**@}*/
646
647/*****************************************************************/
648/* PQ particle blocks */
649/*****************************************************************/
650
651/** \name p,q <> 0 per frequency code */
652/**@{*/
653static void setup_PQ(elc_data const &elc, double prefactor, std::size_t index_p,
654 std::size_t index_q, double omega,
655 ParticleRange const &particles,
656 BoxGeometry const &box_geo) {
657 assert(index_p >= 1);
658 assert(index_q >= 1);
659 constexpr std::size_t size = 8;
660 auto const xy_area_inv = box_geo.length_inv()[0] * box_geo.length_inv()[1];
661 auto const pref_di = prefactor * 8. * std::numbers::pi * xy_area_inv;
662 auto const pref = -pref_di / expm1(omega * box_geo.length()[2]);
663 double lclimgebot[8], lclimgetop[8], lclimge[8];
664 double fac_delta_mid_bot = 1., fac_delta_mid_top = 1., fac_delta = 1.;
665 if (elc.dielectric_contrast_on) {
666 auto const delta = elc.delta_mid_top * elc.delta_mid_bot;
667 auto const fac_elc = 1. / (1. - delta * exp(-omega * 2. * elc.box_h));
671 }
672
673 clear_vec(lclimge, size);
674 clear_vec(gblcblk, size);
675
676 std::size_t ic = 0;
677 auto const ox = (index_p - 1) * particles.size();
678 auto const oy = (index_q - 1) * particles.size();
679 for (auto const &p : particles) {
680 auto const z = p.pos()[2];
681 auto const q = p.q();
682 auto e = exp(omega * z);
683
684 partblk[size * ic + PQESSM] =
685 scxcache[ox + ic].s * scycache[oy + ic].s * q / e;
686 partblk[size * ic + PQESCM] =
687 scxcache[ox + ic].s * scycache[oy + ic].c * q / e;
688 partblk[size * ic + PQECSM] =
689 scxcache[ox + ic].c * scycache[oy + ic].s * q / e;
690 partblk[size * ic + PQECCM] =
691 scxcache[ox + ic].c * scycache[oy + ic].c * q / e;
692
693 partblk[size * ic + PQESSP] =
694 scxcache[ox + ic].s * scycache[oy + ic].s * q * e;
695 partblk[size * ic + PQESCP] =
696 scxcache[ox + ic].s * scycache[oy + ic].c * q * e;
697 partblk[size * ic + PQECSP] =
698 scxcache[ox + ic].c * scycache[oy + ic].s * q * e;
699 partblk[size * ic + PQECCP] =
700 scxcache[ox + ic].c * scycache[oy + ic].c * q * e;
701
702 add_vec(gblcblk, gblcblk, block(partblk.data(), ic, size), size);
703
704 if (elc.dielectric_contrast_on) {
705 if (z < elc.space_layer) { // handle the lower case first
706 // change e to take into account the z position of the images
707
708 e = exp(-omega * z);
709 auto const scale = q * elc.delta_mid_bot;
710
711 lclimgebot[PQESSM] = scxcache[ox + ic].s * scycache[oy + ic].s / e;
712 lclimgebot[PQESCM] = scxcache[ox + ic].s * scycache[oy + ic].c / e;
713 lclimgebot[PQECSM] = scxcache[ox + ic].c * scycache[oy + ic].s / e;
714 lclimgebot[PQECCM] = scxcache[ox + ic].c * scycache[oy + ic].c / e;
715
716 lclimgebot[PQESSP] = scxcache[ox + ic].s * scycache[oy + ic].s * e;
717 lclimgebot[PQESCP] = scxcache[ox + ic].s * scycache[oy + ic].c * e;
718 lclimgebot[PQECSP] = scxcache[ox + ic].c * scycache[oy + ic].s * e;
719 lclimgebot[PQECCP] = scxcache[ox + ic].c * scycache[oy + ic].c * e;
720
721 addscale_vec(gblcblk, scale, lclimgebot, gblcblk, size);
722
723 e = (exp(omega * (-z - 2. * elc.box_h)) * elc.delta_mid_bot +
724 exp(omega * (+z - 2. * elc.box_h))) *
725 fac_delta * q;
726
727 } else {
728
729 e = (exp(-omega * z) +
730 exp(omega * (z - 2. * elc.box_h)) * elc.delta_mid_top) *
732 }
733
734 lclimge[PQESSP] += scxcache[ox + ic].s * scycache[oy + ic].s * e;
735 lclimge[PQESCP] += scxcache[ox + ic].s * scycache[oy + ic].c * e;
736 lclimge[PQECSP] += scxcache[ox + ic].c * scycache[oy + ic].s * e;
737 lclimge[PQECCP] += scxcache[ox + ic].c * scycache[oy + ic].c * e;
738
739 if (z > (elc.box_h - elc.space_layer)) { // handle the upper case now
740
741 e = exp(omega * (2. * elc.box_h - z));
742 auto const scale = q * elc.delta_mid_top;
743
744 lclimgetop[PQESSM] = scxcache[ox + ic].s * scycache[oy + ic].s / e;
745 lclimgetop[PQESCM] = scxcache[ox + ic].s * scycache[oy + ic].c / e;
746 lclimgetop[PQECSM] = scxcache[ox + ic].c * scycache[oy + ic].s / e;
747 lclimgetop[PQECCM] = scxcache[ox + ic].c * scycache[oy + ic].c / e;
748
749 lclimgetop[PQESSP] = scxcache[ox + ic].s * scycache[oy + ic].s * e;
750 lclimgetop[PQESCP] = scxcache[ox + ic].s * scycache[oy + ic].c * e;
751 lclimgetop[PQECSP] = scxcache[ox + ic].c * scycache[oy + ic].s * e;
752 lclimgetop[PQECCP] = scxcache[ox + ic].c * scycache[oy + ic].c * e;
753
754 addscale_vec(gblcblk, scale, lclimgetop, gblcblk, size);
755
756 e = (exp(omega * (+z - 4. * elc.box_h)) * elc.delta_mid_top +
757 exp(omega * (-z - 2. * elc.box_h))) *
758 fac_delta * q;
759
760 } else {
761
762 e = (exp(omega * (+z - 2. * elc.box_h)) +
763 exp(omega * (-z - 2. * elc.box_h)) * elc.delta_mid_bot) *
765 }
766
767 lclimge[PQESSM] += scxcache[ox + ic].s * scycache[oy + ic].s * e;
768 lclimge[PQESCM] += scxcache[ox + ic].s * scycache[oy + ic].c * e;
769 lclimge[PQECSM] += scxcache[ox + ic].c * scycache[oy + ic].s * e;
770 lclimge[PQECCM] += scxcache[ox + ic].c * scycache[oy + ic].c * e;
771 }
772
773 ic++;
774 }
775
776 scale_vec(pref, gblcblk, size);
777 if (elc.dielectric_contrast_on) {
778 scale_vec(pref_di, lclimge, size);
780 }
781}
782
783static void add_PQ_force(std::size_t index_p, std::size_t index_q, double omega,
784 ParticleRange const &particles,
785 BoxGeometry const &box_geo) {
786 auto constexpr c_2pi = 2. * std::numbers::pi;
787 auto const pref_x =
788 c_2pi * box_geo.length_inv()[0] * static_cast<double>(index_p) / omega;
789 auto const pref_y =
790 c_2pi * box_geo.length_inv()[1] * static_cast<double>(index_q) / omega;
791 constexpr std::size_t size = 8;
792
793 std::size_t ic = 0;
794 for (auto &p : particles) {
795 auto &force = p.force();
796 force[0] += pref_x * (partblk[size * ic + PQESCM] * gblcblk[PQECCP] +
797 partblk[size * ic + PQESSM] * gblcblk[PQECSP] -
798 partblk[size * ic + PQECCM] * gblcblk[PQESCP] -
799 partblk[size * ic + PQECSM] * gblcblk[PQESSP] +
800 partblk[size * ic + PQESCP] * gblcblk[PQECCM] +
801 partblk[size * ic + PQESSP] * gblcblk[PQECSM] -
802 partblk[size * ic + PQECCP] * gblcblk[PQESCM] -
803 partblk[size * ic + PQECSP] * gblcblk[PQESSM]);
804 force[1] += pref_y * (partblk[size * ic + PQECSM] * gblcblk[PQECCP] +
805 partblk[size * ic + PQESSM] * gblcblk[PQESCP] -
806 partblk[size * ic + PQECCM] * gblcblk[PQECSP] -
807 partblk[size * ic + PQESCM] * gblcblk[PQESSP] +
808 partblk[size * ic + PQECSP] * gblcblk[PQECCM] +
809 partblk[size * ic + PQESSP] * gblcblk[PQESCM] -
810 partblk[size * ic + PQECCP] * gblcblk[PQECSM] -
811 partblk[size * ic + PQESCP] * gblcblk[PQESSM]);
812 force[2] += (partblk[size * ic + PQECCM] * gblcblk[PQECCP] +
813 partblk[size * ic + PQECSM] * gblcblk[PQECSP] +
814 partblk[size * ic + PQESCM] * gblcblk[PQESCP] +
815 partblk[size * ic + PQESSM] * gblcblk[PQESSP] -
816 partblk[size * ic + PQECCP] * gblcblk[PQECCM] -
817 partblk[size * ic + PQECSP] * gblcblk[PQECSM] -
818 partblk[size * ic + PQESCP] * gblcblk[PQESCM] -
819 partblk[size * ic + PQESSP] * gblcblk[PQESSM]);
820 ic++;
821 }
822}
823
824static double PQ_energy(double omega, std::size_t n_part) {
825 constexpr std::size_t size = 8;
826
827 auto energy = 0.;
828 for (std::size_t ic = 0; ic < n_part; ic++) {
829 energy += partblk[size * ic + PQECCM] * gblcblk[PQECCP] +
830 partblk[size * ic + PQECSM] * gblcblk[PQECSP] +
831 partblk[size * ic + PQESCM] * gblcblk[PQESCP] +
832 partblk[size * ic + PQESSM] * gblcblk[PQESSP] +
833 partblk[size * ic + PQECCP] * gblcblk[PQECCM] +
834 partblk[size * ic + PQECSP] * gblcblk[PQECSM] +
835 partblk[size * ic + PQESCP] * gblcblk[PQESCM] +
836 partblk[size * ic + PQESSP] * gblcblk[PQESSM];
837 }
838 return energy / omega;
839}
840/**@}*/
841
842void ElectrostaticLayerCorrection::add_force() const {
843 auto constexpr c_2pi = 2. * std::numbers::pi;
844 auto const &system = get_system();
845 auto const &box_geo = *system.box_geo;
846 auto const particles = system.cell_structure->local_particles();
847 auto const n_freqs = prepare_sc_cache(particles, box_geo, elc.far_cut);
848 auto const n_scxcache = std::get<0>(n_freqs);
849 auto const n_scycache = std::get<1>(n_freqs);
850 partblk.resize(particles.size() * 8);
851
852 add_dipole_force();
853 add_z_force();
854
855 /* the second condition is just for the case of numerical accident */
856 for (std::size_t p = 1;
857 box_geo.length_inv()[0] * static_cast<double>(p - 1) < elc.far_cut &&
858 p <= n_scxcache;
859 p++) {
860 auto const omega = c_2pi * box_geo.length_inv()[0] * static_cast<double>(p);
861 setup_PoQ<PoQ::P>(elc, prefactor, p, omega, particles, box_geo);
862 distribute(4);
863 add_PoQ_force<PoQ::P>(particles);
864 }
865
866 for (std::size_t q = 1;
867 box_geo.length_inv()[1] * static_cast<double>(q - 1) < elc.far_cut &&
868 q <= n_scycache;
869 q++) {
870 auto const omega = c_2pi * box_geo.length_inv()[1] * static_cast<double>(q);
871 setup_PoQ<PoQ::Q>(elc, prefactor, q, omega, particles, box_geo);
872 distribute(4);
873 add_PoQ_force<PoQ::Q>(particles);
874 }
875
876 for (std::size_t p = 1;
877 box_geo.length_inv()[0] * static_cast<double>(p - 1) < elc.far_cut &&
878 p <= n_scxcache;
879 p++) {
880 for (std::size_t q = 1;
881 Utils::sqr(box_geo.length_inv()[0] * static_cast<double>(p - 1)) +
882 Utils::sqr(box_geo.length_inv()[1] *
883 static_cast<double>(q - 1)) <
884 elc.far_cut2 &&
885 q <= n_scycache;
886 q++) {
887 auto const omega =
888 c_2pi *
889 sqrt(Utils::sqr(box_geo.length_inv()[0] * static_cast<double>(p)) +
890 Utils::sqr(box_geo.length_inv()[1] * static_cast<double>(q)));
891 setup_PQ(elc, prefactor, p, q, omega, particles, box_geo);
892 distribute(8);
893 add_PQ_force(p, q, omega, particles, box_geo);
894 }
895 }
896}
897
898double ElectrostaticLayerCorrection::calc_energy() const {
899 auto constexpr c_2pi = 2. * std::numbers::pi;
900 auto const &system = get_system();
901 auto const &box_geo = *system.box_geo;
902 auto const particles = system.cell_structure->local_particles();
903 auto energy = dipole_energy() + z_energy();
904 auto const n_freqs = prepare_sc_cache(particles, box_geo, elc.far_cut);
905 auto const n_scxcache = std::get<0>(n_freqs);
906 auto const n_scycache = std::get<1>(n_freqs);
907
908 auto const n_localpart = particles.size();
909 partblk.resize(n_localpart * 8);
910
911 /* the second condition is just for the case of numerical accident */
912 for (std::size_t p = 1;
913 box_geo.length_inv()[0] * static_cast<double>(p - 1) < elc.far_cut &&
914 p <= n_scxcache;
915 p++) {
916 auto const omega = c_2pi * box_geo.length_inv()[0] * static_cast<double>(p);
917 setup_PoQ<PoQ::P>(elc, prefactor, p, omega, particles, box_geo);
918 distribute(4);
919 energy += PoQ_energy(omega, n_localpart);
920 }
921
922 for (std::size_t q = 1;
923 box_geo.length_inv()[1] * static_cast<double>(q - 1) < elc.far_cut &&
924 q <= n_scycache;
925 q++) {
926 auto const omega = c_2pi * box_geo.length_inv()[1] * static_cast<double>(q);
927 setup_PoQ<PoQ::Q>(elc, prefactor, q, omega, particles, box_geo);
928 distribute(4);
929 energy += PoQ_energy(omega, n_localpart);
930 }
931
932 for (std::size_t p = 1;
933 box_geo.length_inv()[0] * static_cast<double>(p - 1) < elc.far_cut &&
934 p <= n_scxcache;
935 p++) {
936 for (std::size_t q = 1;
937 Utils::sqr(box_geo.length_inv()[0] * static_cast<double>(p - 1)) +
938 Utils::sqr(box_geo.length_inv()[1] *
939 static_cast<double>(q - 1)) <
940 elc.far_cut2 &&
941 q <= n_scycache;
942 q++) {
943 auto const omega =
944 c_2pi *
945 sqrt(Utils::sqr(box_geo.length_inv()[0] * static_cast<double>(p)) +
946 Utils::sqr(box_geo.length_inv()[1] * static_cast<double>(q)));
947 setup_PQ(elc, prefactor, p, q, omega, particles, box_geo);
948 distribute(8);
949 energy += PQ_energy(omega, n_localpart);
950 }
951 }
952 /* we count both i<->j and j<->i, so return just half of it */
953 return 0.5 * energy;
954}
955
956double ElectrostaticLayerCorrection::tune_far_cut() const {
957 // Largest reasonable cutoff for far formula
958 auto constexpr maximal_far_cut = 50.;
959 auto const &box_geo = *get_system().box_geo;
960 auto const box_l_x_inv = box_geo.length_inv()[0];
961 auto const box_l_y_inv = box_geo.length_inv()[1];
962 auto const min_inv_boxl = std::min(box_l_x_inv, box_l_y_inv);
963 auto const box_l_z = box_geo.length()[2];
964 auto const h = elc.box_h;
965 // adjust lz according to dielectric layer method
966 auto const lz = (elc.dielectric_contrast_on) ? h + elc.space_layer : box_l_z;
967
969 double err;
970 do {
971 // following equation 18 in arnold02d
972 auto const pref = 2. * std::numbers::pi * tuned_far_cut;
973 auto const sum = pref + 2. * (box_l_x_inv + box_l_y_inv);
974 auto const den = expm1(pref * lz);
975 auto const num1 = exp(pref * h);
976 auto const num2 = 1. / num1; // exp(-pref * h);
977
978 err = 0.5 / den *
979 (num1 / (lz - h) * (sum + 1. / (lz - h)) +
980 num2 / (lz + h) * (sum + 1. / (lz + h)));
981
985 throw std::runtime_error("ELC tuning failed: maxPWerror too small");
986 }
988}
989
990void ElectrostaticLayerCorrection::sanity_checks_periodicity() const {
991 auto const &box_geo = *get_system().box_geo;
992 if (!box_geo.periodic(0) || !box_geo.periodic(1) || !box_geo.periodic(2)) {
993 throw std::runtime_error("ELC: requires periodicity (True, True, True)");
994 }
995}
996
997void ElectrostaticLayerCorrection::adapt_solver() {
998 std::visit(
999 [this](auto &solver) {
1000 set_prefactor(solver->prefactor);
1001 solver->adapt_epsilon_elc();
1002 assert(solver->p3m_params.epsilon == P3M_EPSILON_METALLIC);
1003 },
1004 base_solver);
1005}
1006
1007void ElectrostaticLayerCorrection::recalc_box_h() {
1008 m_box_geo = get_system().box_geo.get();
1009 auto const box_z = m_box_geo->length()[2];
1010 auto const new_box_h = box_z - elc.gap_size;
1011 if (new_box_h < 0.) {
1012 throw std::runtime_error("ELC gap size (" + std::to_string(elc.gap_size) +
1013 ") larger than box length in z-direction (" +
1014 std::to_string(box_z) + ")");
1015 }
1017}
1018
1019void ElectrostaticLayerCorrection::recalc_space_layer() {
1021 auto const p3m_r_cut = std::visit(
1022 [](auto &solver) { return solver->p3m_params.r_cut; }, base_solver);
1023 // recalculate the space layer size:
1024 // 1. set the space_layer to be 1/3 of the gap size, so that box = layer
1025 elc.space_layer = (1. / 3.) * elc.gap_size;
1026 // 2. but make sure we don't overlap with the near-field formula
1027 auto const free_space = elc.gap_size - p3m_r_cut;
1028 // 3. and make sure the space layer is not bigger than half the actual
1029 // simulation box, to avoid overlaps
1030 auto const half_box_h = elc.box_h / 2.;
1031 auto const max_space_layer = std::min(free_space, half_box_h);
1033 if (max_space_layer <= 0.) {
1034 throw std::runtime_error("P3M real-space cutoff too large for ELC w/ "
1035 "dielectric contrast");
1036 }
1038 }
1040 }
1041}
1042
1043elc_data::elc_data(double maxPWerror, double gap_size, double far_cut,
1044 bool neutralize, double delta_top, double delta_bot,
1045 bool with_const_pot, double potential_diff)
1046 : maxPWerror{maxPWerror}, gap_size{gap_size}, box_h{-1.}, far_cut{far_cut},
1047 far_cut2{-1.}, far_calculated{far_cut == -1.},
1048 dielectric_contrast_on{delta_top != 0. or delta_bot != 0.},
1049 const_pot{with_const_pot and dielectric_contrast_on},
1050 neutralize{neutralize and !dielectric_contrast_on},
1051 delta_mid_top{std::clamp(delta_top, -1., +1.)},
1052 delta_mid_bot{std::clamp(delta_bot, -1., +1.)},
1053 pot_diff{(with_const_pot) ? potential_diff : 0.},
1054 // initial setup of parameters, may change later when P3M is finally tuned
1055 // set the space_layer to be 1/3 of the gap size, so that box = layer
1056 space_layer{(dielectric_contrast_on) ? gap_size / 3. : 0.},
1057 space_box{gap_size - ((dielectric_contrast_on) ? 2. * space_layer : 0.)} {
1058
1059 auto const delta_range = 1. + std::sqrt(round_error_prec);
1060 if (far_cut <= 0. and not far_calculated) {
1061 throw std::domain_error("Parameter 'far_cut' must be > 0");
1062 }
1063 if (maxPWerror <= 0.) {
1064 throw std::domain_error("Parameter 'maxPWerror' must be > 0");
1065 }
1066 if (gap_size <= 0.) {
1067 throw std::domain_error("Parameter 'gap_size' must be > 0");
1068 }
1070 throw std::invalid_argument(
1071 "Parameter 'const_pot' must be True when 'pot_diff' is non-zero");
1072 }
1074 throw std::invalid_argument(
1075 "Parameter 'const_pot' requires a dielectric contrast; set "
1076 "'delta_mid_top' and 'delta_mid_bot' (use -1 for metallic walls)");
1077 }
1079 throw std::domain_error(
1080 "Parameter 'delta_mid_top' must be >= -1 and <= +1");
1081 }
1083 throw std::domain_error(
1084 "Parameter 'delta_mid_bot' must be >= -1 and <= +1");
1085 }
1086 /* Dielectric contrasts: the deltas should be either both -1 or both +1 when
1087 * no constant potential difference is applied. The case of two non-metallic
1088 * parallel boundaries can only be treated with a constant potential. */
1090 (std::fabs(1. - delta_mid_top * delta_mid_bot) < round_error_prec)) {
1091 throw std::domain_error("ELC with two parallel metallic boundaries "
1092 "requires the const_pot option");
1093 }
1094}
1095
1097 elc_data &&parameters, BaseSolver &&solver)
1098 : elc{parameters}, base_solver{solver} {
1099 // The P3M-GPU ignores images charges, disabled for now
1101 auto const on_gpu =
1102 std::visit([](auto const &solver_ptr) { return solver_ptr->is_gpu(); },
1103 base_solver);
1104 if (on_gpu) {
1105 throw std::runtime_error(
1106 "ELC with a dielectric contrast is not supported by the GPU "
1107 "variant of P3M");
1108 }
1109 }
1110 adapt_solver();
1111}
1112
1113template <ChargeProtocol protocol>
1114void charge_assign(elc_data const &elc, CoulombP3M &solver, auto const &cs) {
1115
1116 solver.prepare_fft_mesh(protocol == ChargeProtocol::BOTH or
1117 protocol == ChargeProtocol::IMAGE);
1118
1119 // multi-threading -> cache sizes must be equal to the number of particles
1120 auto constexpr include_neutral_particles = true;
1121 auto const &aosoa = cs.get_aosoa();
1122 auto const n_part = cs.count_local_particles();
1123
1124 for (std::size_t p_index = 0; p_index < n_part; ++p_index) {
1125 auto const p_q = aosoa.charge(p_index);
1126 auto const p_pos = aosoa.get_span_at(aosoa.position, p_index);
1127 if (include_neutral_particles or p_q != 0.) {
1128 // assign real charges
1129 if (protocol == ChargeProtocol::BOTH or
1130 protocol == ChargeProtocol::REAL) {
1131 solver.assign_charge(p_q, {p_pos[0], p_pos[1], p_pos[2]}, false);
1132 }
1133 // assign image charges
1134 if (protocol == ChargeProtocol::BOTH or
1135 protocol == ChargeProtocol::IMAGE) {
1136 if (p_pos[2] < elc.space_layer) {
1137 auto const q_eff = elc.delta_mid_bot * p_q;
1138 solver.assign_charge(q_eff, {p_pos[0], p_pos[1], -p_pos[2]}, true);
1139 }
1140 if (p_pos[2] > (elc.box_h - elc.space_layer)) {
1141 auto const q_eff = elc.delta_mid_top * p_q;
1142 solver.assign_charge(
1143 q_eff, {p_pos[0], p_pos[1], 2. * elc.box_h - p_pos[2]}, true);
1144 }
1145 }
1146 }
1147 }
1148}
1149
1150template <ChargeProtocol protocol>
1151void modify_p3m_sums(elc_data const &elc, CoulombP3M &solver, auto const &cs) {
1152
1153 auto const &aosoa = cs.get_aosoa();
1154 auto const n_part = cs.count_local_particles();
1155 auto local_n = std::size_t{0u};
1156 auto local_q2 = 0.0;
1157 auto local_q = 0.0;
1158 for (std::size_t p_index = 0; p_index < n_part; ++p_index) {
1159 auto const p_q = aosoa.charge(p_index);
1160 if (p_q != 0.) {
1161 auto const p_z = aosoa.position(p_index, 2ul);
1162
1163 if (protocol == ChargeProtocol::BOTH or
1164 protocol == ChargeProtocol::REAL) {
1165 local_n++;
1167 local_q += p_q;
1168 }
1169
1170 if (protocol == ChargeProtocol::BOTH or
1171 protocol == ChargeProtocol::IMAGE) {
1172 if (p_z < elc.space_layer) {
1173 local_n++;
1175 local_q += elc.delta_mid_bot * p_q;
1176 }
1177
1178 if (p_z > (elc.box_h - elc.space_layer)) {
1179 local_n++;
1181 local_q += elc.delta_mid_top * p_q;
1182 }
1183 }
1184 }
1185 }
1186
1187 auto global_n = std::size_t{0u};
1188 auto global_q2 = 0.;
1189 auto global_q = 0.;
1190 boost::mpi::all_reduce(comm_cart, local_n, global_n, std::plus<>());
1191 boost::mpi::all_reduce(comm_cart, local_q2, global_q2, std::plus<>());
1192 boost::mpi::all_reduce(comm_cart, local_q, global_q, std::plus<>());
1194}
1195
1197 auto const &system = get_system();
1198 auto const energy = std::visit(
1199 [this, &system](auto const &solver_ptr) {
1200 auto &solver = *solver_ptr;
1201 auto const &cs = *system.cell_structure;
1202 auto const &box_geo = *system.box_geo;
1203 auto const particles = cs.local_particles();
1204
1205 // assign the original charges (they may not have been assigned yet)
1206 solver.charge_assign();
1207
1209 return solver.long_range_energy();
1210 }
1211
1212 auto energy = 0.;
1213 energy += 0.5 * solver.long_range_energy();
1214 energy +=
1215 0.5 * elc.dielectric_layers_self_energy(solver, box_geo, particles);
1216
1217 // assign both original and image charges
1220 energy += 0.5 * solver.long_range_energy();
1221
1222 // assign only the image charges now
1225 energy -= 0.5 * solver.long_range_energy();
1226
1227 // restore modified sums
1229
1230 return energy;
1231 },
1232 base_solver);
1233 return energy + calc_energy();
1234}
1235
1237 auto const &system = get_system();
1238 std::visit(
1239 [this, &system](auto const &solver_ptr) {
1240 auto const &cs = *system.cell_structure;
1241 auto &solver = *solver_ptr;
1243 auto const &box_geo = *system.box_geo;
1244 auto const particles = cs.local_particles();
1245 modify_p3m_sums<ChargeProtocol::BOTH>(elc, solver, cs);
1246 charge_assign<ChargeProtocol::BOTH>(elc, solver, cs);
1247 elc.dielectric_layers_self_forces(solver, box_geo, particles);
1248 } else {
1249 solver.charge_assign();
1250 }
1251 solver.add_long_range_forces();
1254 }
1255 },
1256 base_solver);
1257 add_force();
1258}
1259
1260#endif // ESPRESSO_P3M
Utils::Vector3d const & length() const
Box length.
Utils::Vector3d const & length_inv() const
Inverse box length.
A range of particles.
base_type::size_type size() const
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
constexpr auto round_error_prec
Precision below which a double-precision float is assumed to be zero.
Definition config.hpp:47
static void addscale_vec(double *pdc_d, double scale, double const *pdc_s1, double const *pdc_s2, std::size_t size)
Definition elc.cpp:164
#define PQESSM
Definition elc.cpp:72
#define POQESP
Definition elc.cpp:63
static std::pair< std::size_t, std::size_t > prepare_sc_cache(ParticleRange const &particles, BoxGeometry const &box_geo, double far_cut)
Definition elc.cpp:130
#define PQESCP
Definition elc.cpp:69
static void add_PQ_force(std::size_t index_p, std::size_t index_q, double omega, ParticleRange const &particles, BoxGeometry const &box_geo)
Definition elc.cpp:783
static std::vector< double > partblk
temporary buffers for product decomposition
Definition elc.cpp:84
#define PQESCM
Definition elc.cpp:73
static void clear_vec(double *pdc, std::size_t size)
Definition elc.cpp:148
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:175
void setup_PoQ(elc_data const &elc, double prefactor, std::size_t index, double omega, ParticleRange const &particles, BoxGeometry const &box_geo)
Definition elc.cpp:513
static void distribute(std::size_t size)
Definition elc.cpp:179
static double PoQ_energy(double omega, std::size_t n_part)
Definition elc.cpp:632
static std::vector< SCCache > scxcache
Cached sin/cos values along the x-axis and y-axis.
Definition elc.cpp:95
#define PQECCP
Definition elc.cpp:71
static double PQ_energy(double omega, std::size_t n_part)
Definition elc.cpp:824
#define POQECP
Definition elc.cpp:64
static std::vector< SCCache > scycache
Definition elc.cpp:96
static void setup_PQ(elc_data const &elc, double prefactor, std::size_t index_p, std::size_t index_q, double omega, ParticleRange const &particles, BoxGeometry const &box_geo)
Definition elc.cpp:653
static void copy_vec(double *pdc_d, double const *pdc_s, std::size_t size)
Definition elc.cpp:153
#define PQECCM
Definition elc.cpp:75
#define POQECM
Definition elc.cpp:66
static void add_vec(double *pdc_d, double const *pdc_s1, double const *pdc_s2, std::size_t size)
Definition elc.cpp:158
#define POQESM
Definition elc.cpp:65
static void scale_vec(double scale, double *pdc, std::size_t size)
Definition elc.cpp:170
void add_PoQ_force(ParticleRange const &particles)
Definition elc.cpp:613
ChargeProtocol
ELC charge sum/assign protocol: real charges, image charges, or both.
Definition elc.cpp:81
void modify_p3m_sums(elc_data const &elc, CoulombP3M &solver, auto const &cs)
Definition elc.cpp:1151
static std::vector< SCCache > calc_sc_cache(ParticleRange const &particles, std::size_t n_freq, double u)
Calculate cached sin/cos values for one direction.
Definition elc.cpp:110
#define PQESSP
Definition elc.cpp:68
#define PQECSP
Definition elc.cpp:70
static double gblcblk[8]
collected data from the other cells
Definition elc.cpp:86
void charge_assign(elc_data const &elc, CoulombP3M &solver, auto const &cs)
Definition elc.cpp:1114
PoQ
ELC axes (x and y directions)
Definition elc.cpp:79
#define PQECSM
Definition elc.cpp:74
ELC algorithm for long-range Coulomb interactions.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
ParticleRange particles(std::span< Cell *const > cells)
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28
auto sqrt(Vector< T, N > const &a)
Definition Vector.hpp:368
STL namespace.
auto constexpr P3M_EPSILON_METALLIC
This value indicates metallic boundary conditions.
P3M algorithm for long-range Coulomb interaction.
P3M solver.
Definition p3m.hpp:55
virtual void prepare_fft_mesh(bool reset_weights)=0
virtual void count_charged_particles_elc(std::size_t, double, double)=0
virtual void assign_charge(double q, Utils::Vector3d const &real_pos, bool skip_cache)=0
Assign a single charge into the current charge grid.
void add_long_range_forces() const
Accumulate long-range electrostatic forces with corrections.
Definition elc.cpp:1236
std::variant< std::shared_ptr< CoulombP3M > > BaseSolver
Definition elc.hpp:193
BaseSolver base_solver
Electrostatics solver that is adapted.
Definition elc.hpp:199
ElectrostaticLayerCorrection(elc_data &&parameters, BaseSolver &&solver)
Definition elc.cpp:1096
double long_range_energy() const
Calculate long-range electrostatic energy with corrections.
Definition elc.cpp:1196
BoxGeometry * m_box_geo
Definition elc.hpp:196
double b(double q, double z) const
Image sum from the bottom layer.
Definition elc.cpp:363
double dci
Definition elc.cpp:357
ImageSum(double delta, double shift, double h)
Definition elc.cpp:359
double shift
Definition elc.cpp:355
double t(double q, double z) const
Image sum from the top layer.
Definition elc.cpp:368
double delta
Definition elc.cpp:354
double h
Definition elc.cpp:356
Struct holding all information for one particle.
Definition Particle.hpp:436
constexpr auto const & pos() const
Definition Particle.hpp:476
constexpr auto const & force() const
Definition Particle.hpp:480
constexpr auto const & id() const
Definition Particle.hpp:455
constexpr auto const & q() const
Definition Particle.hpp:597
structure for caching sin and cos values
Definition elc.cpp:89
double c
Definition elc.cpp:90
double s
Definition elc.cpp:90
Parameters for the ELC method.
Definition elc.hpp:65
double dielectric_layers_self_energy(CoulombP3M const &p3m, BoxGeometry const &box_geo, ParticleRange const &particles) const
self energies of top and bottom layers with their virtual images
Definition elc.hpp:163
double maxPWerror
Maximal allowed pairwise error for the potential and force.
Definition elc.hpp:74
double pot_diff
Constant potential difference.
Definition elc.hpp:113
double box_h
Up to where particles can be found.
Definition elc.hpp:80
bool dielectric_contrast_on
Flag whether there is any dielectric contrast in the system.
Definition elc.hpp:96
elc_data(double maxPWerror, double gap_size, double far_cut, bool neutralize, double delta_top, double delta_bot, bool const_pot, double pot_diff)
Definition elc.cpp:1043
double space_box
The space that is finally left.
Definition elc.hpp:118
bool neutralize
Flag whether the box is neutralized by a homogeneous background.
Definition elc.hpp:106
double far_cut
Cutoff of the exponential sum.
Definition elc.hpp:86
double space_layer
Layer around the dielectric contrast in which we trick around.
Definition elc.hpp:116
bool far_calculated
Flag whether far_cut was set by the user, or calculated by ESPResSo.
Definition elc.hpp:93
double gap_size
Size of the empty gap.
Definition elc.hpp:78
double delta_mid_bot
dielectric contrast in the lower part of the simulation cell.
Definition elc.hpp:111
bool const_pot
Flag whether a constant potential difference is applied.
Definition elc.hpp:98
double far_cut2
Squared value of far_cut.
Definition elc.hpp:88
double delta_mid_top
dielectric contrast in the upper part of the simulation cell.
Definition elc.hpp:109