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