ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
p3m.hpp
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/** @file
23 * P3M algorithm for long-range Coulomb interaction.
24 *
25 * We use a P3M (Particle-Particle Particle-Mesh) method based on the
26 * Ewald summation. Details of the used method can be found in
27 * @cite hockney88a and @cite deserno98a @cite deserno98b.
28 *
29 * Further reading: @cite ewald21a, @cite hockney88a, @cite deserno98a,
30 * @cite deserno98b, @cite deserno00e, @cite deserno00b, @cite cerda08d.
31 *
32 * Implementation in p3m.cpp.
33 */
34
35#pragma once
36
37#include "config/config.hpp"
38
39#ifdef P3M
40
42
43#include "p3m/common.hpp"
44#include "p3m/data_struct.hpp"
45#include "p3m/fft.hpp"
46#include "p3m/interpolation.hpp"
47#include "p3m/send_mesh.hpp"
48
49#include "ParticleRange.hpp"
50
51#include <utils/Vector.hpp>
52#include <utils/constants.hpp>
54
55#include <array>
56#include <cmath>
57
59 explicit p3m_data_struct(P3MParameters &&parameters)
60 : p3m_data_struct_base{std::move(parameters)} {}
61
62 /** local mesh. */
64 /** real space mesh (local) for CA/FFT. */
66 /** mesh (local) for the electric field. */
67 std::array<fft_vector<double>, 3> E_mesh;
68
69 /** number of charged particles (only on head node). */
70 int sum_qpart = 0;
71 /** Sum of square of charges (only on head node). */
72 double sum_q2 = 0.;
73 /** square of sum of charges (only on head node). */
74 double square_sum_q = 0.;
75
77
78 /** send/recv mesh sizes */
80
82};
83
84/** @brief P3M solver. */
85struct CoulombP3M : public Coulomb::Actor<CoulombP3M> {
86 /** P3M parameters. */
88
92
93private:
94 bool m_is_tuned;
95
96public:
97 CoulombP3M(P3MParameters &&parameters, double prefactor, int tune_timings,
99
100 bool is_tuned() const { return m_is_tuned; }
101
102 /** @brief Recalculate all derived parameters. */
103 void init();
106 tune();
107 }
108 /** @brief Recalculate all box-length-dependent parameters. */
109 void on_boxl_change() { scaleby_box_l(); }
110 void on_node_grid_change() const { sanity_checks_node_grid(); }
111 void on_periodicity_change() const { sanity_checks_periodicity(); }
113 sanity_checks_cell_structure();
114 init();
115 }
116 void sanity_checks() const {
117 sanity_checks_boxl();
118 sanity_checks_node_grid();
119 sanity_checks_periodicity();
120 sanity_checks_cell_structure();
122 }
123
124 /**
125 * Count the number of charged particles and calculate
126 * the sum of the squared charges.
127 */
129
130 /**
131 * @brief Tune P3M parameters to desired accuracy.
132 *
133 * The parameters
134 * @ref P3MParameters::mesh "mesh",
135 * @ref P3MParameters::cao "cao",
136 * @ref P3MParameters::r_cut_iL "r_cut_iL" and
137 * @ref P3MParameters::alpha_L "alpha_L" are tuned to obtain the target
138 * @ref P3MParameters::accuracy "accuracy" in optimal time.
139 * These parameters are stored in the @ref p3m object.
140 *
141 * The function utilizes the analytic expression of the error estimate
142 * for the P3M method in @cite hockney88a (eq. (8.23)) in
143 * order to obtain the rms error in the force for a system of N randomly
144 * distributed particles in a cubic box.
145 * For the real space error the estimate of Kolafa/Perram is used.
146 *
147 * Parameter ranges if not given explicitly in the constructor:
148 * - @p mesh explores the range 0-512 (biased toward values less than 128)
149 * - @p cao explores all possible values
150 * - @p alpha_L is tuned for each tuple (@p r_cut_iL, @p mesh, @p cao) and
151 * calculated assuming that the error contributions of real and reciprocal
152 * space should be equal
153 *
154 * After checking if the total error lies below the target accuracy,
155 * the time needed for one force calculation is measured. Parameters
156 * that minimize the runtime are kept.
157 *
158 * The function is based on routines of the program HE_Q.cpp written by M.
159 * Deserno.
160 */
161 void tune();
162
163 /** Assign the physical charges using the tabulated charge assignment
164 * function.
165 */
166 void charge_assign(ParticleRange const &particles);
167
168 /**
169 * @brief Assign a single charge into the current charge grid.
170 *
171 * @param[in] q Particle charge
172 * @param[in] real_pos Particle position in real space
173 * @param[out] inter_weights Cached interpolation weights to be used.
174 */
175 void assign_charge(double q, Utils::Vector3d const &real_pos,
176 p3m_interpolation_cache &inter_weights);
177 /** @overload */
178 void assign_charge(double q, Utils::Vector3d const &real_pos);
179
180 /** Calculate real-space contribution of p3m Coulomb pair forces. */
182 double dist) const {
183 if ((q1q2 == 0.) || dist >= p3m.params.r_cut || dist <= 0.) {
184 return {};
185 }
186 auto const adist = p3m.params.alpha * dist;
187 auto const exp_adist_sq = exp(-adist * adist);
188 auto const dist_sq = dist * dist;
189 auto const two_a_sqrt_pi_i = 2.0 * p3m.params.alpha * Utils::sqrt_pi_i();
190#if USE_ERFC_APPROXIMATION
191 auto const erfc_part_ri = Utils::AS_erfc_part(adist) / dist;
192 auto const fac = exp_adist_sq * (erfc_part_ri + two_a_sqrt_pi_i) / dist_sq;
193#else
194 auto const erfc_part_ri = erfc(adist) / dist;
195 auto const fac = (erfc_part_ri + two_a_sqrt_pi_i * exp_adist_sq) / dist_sq;
196#endif
197 return (fac * prefactor * q1q2) * d;
198 }
199
200 /** Calculate real-space contribution of Coulomb pair energy. */
201 // Eq. (3.6) @cite deserno00b
202 double pair_energy(double q1q2, double dist) const {
203 if ((q1q2 == 0.) || dist >= p3m.params.r_cut || dist <= 0.) {
204 return {};
205 }
206 auto const adist = p3m.params.alpha * dist;
207#if USE_ERFC_APPROXIMATION
208 auto const erfc_part_ri = Utils::AS_erfc_part(adist) / dist;
209 return prefactor * q1q2 * erfc_part_ri * exp(-adist * adist);
210#else
211 auto const erfc_part_ri = erfc(adist) / dist;
212 return prefactor * q1q2 * erfc_part_ri;
213#endif
214 }
215
216 /** Compute the k-space part of the pressure tensor */
218
219 /** Compute the k-space part of energies. */
220 double long_range_energy(ParticleRange const &particles) {
221 return long_range_kernel(false, true, particles);
222 }
223
224 /** Compute the k-space part of forces. */
225 void add_long_range_forces(ParticleRange const &particles) {
226 long_range_kernel(true, false, particles);
227 }
228
229 /** Compute the k-space part of forces and energies. */
230 double long_range_kernel(bool force_flag, bool energy_flag,
231 ParticleRange const &particles);
232
233private:
234 void calc_influence_function_force();
235 void calc_influence_function_energy();
236
237 /** Checks for correctness of the k-space cutoff. */
238 void sanity_checks_boxl() const;
239 void sanity_checks_node_grid() const;
240 void sanity_checks_periodicity() const;
241 void sanity_checks_cell_structure() const;
242
243 void scaleby_box_l();
244};
245
246#endif // P3M
Vector implementation and trait types for boost qvm interoperability.
double prefactor
Electrostatics prefactor.
A range of particles.
Cache for interpolation weights.
Structure for send/recv meshes.
Definition send_mesh.hpp:38
Common functions for dipolar and charge P3M.
This file contains the defaults for ESPResSo.
Routines, row decomposition, data structures and communication for the 3D-FFT.
std::vector< T, fft_allocator< T > > fft_vector
Definition fft.hpp:87
constexpr T AS_erfc_part(T d)
Approximate by applying a formula from chapter 7.
DEVICE_QUALIFIER constexpr T sqrt_pi_i()
One over square root of pi.
Definition constants.hpp:43
P3M solver.
Definition p3m.hpp:85
void charge_assign(ParticleRange const &particles)
Assign the physical charges using the tabulated charge assignment function.
Definition p3m.cpp:339
void add_long_range_forces(ParticleRange const &particles)
Compute the k-space part of forces.
Definition p3m.hpp:225
void on_cell_structure_change()
Definition p3m.hpp:112
bool check_complex_residuals
Definition p3m.hpp:91
void assign_charge(double q, Utils::Vector3d const &real_pos, p3m_interpolation_cache &inter_weights)
Assign a single charge into the current charge grid.
Definition p3m.cpp:353
double long_range_kernel(bool force_flag, bool energy_flag, ParticleRange const &particles)
Compute the k-space part of forces and energies.
Definition p3m.cpp:475
void on_activation()
Definition p3m.hpp:104
Utils::Vector3d pair_force(double q1q2, Utils::Vector3d const &d, double dist) const
Calculate real-space contribution of p3m Coulomb pair forces.
Definition p3m.hpp:181
bool tune_verbose
Definition p3m.hpp:90
void on_boxl_change()
Recalculate all box-length-dependent parameters.
Definition p3m.hpp:109
double pair_energy(double q1q2, double dist) const
Calculate real-space contribution of Coulomb pair energy.
Definition p3m.hpp:202
bool is_tuned() const
Definition p3m.hpp:100
Utils::Vector9d long_range_pressure(ParticleRange const &particles)
Compute the k-space part of the pressure tensor.
Definition p3m.cpp:416
void init()
Recalculate all derived parameters.
Definition p3m.cpp:246
void sanity_checks() const
Definition p3m.hpp:116
void count_charged_particles()
Count the number of charged particles and calculate the sum of the squared charges.
Definition p3m.cpp:84
void on_periodicity_change() const
Definition p3m.hpp:111
p3m_data_struct p3m
P3M parameters.
Definition p3m.hpp:87
void on_node_grid_change() const
Definition p3m.hpp:110
double long_range_energy(ParticleRange const &particles)
Compute the k-space part of energies.
Definition p3m.hpp:220
int tune_timings
Definition p3m.hpp:89
void tune()
Tune P3M parameters to desired accuracy.
Definition p3m.cpp:776
Structure for local mesh parameters.
Definition common.hpp:183
Structure to hold P3M parameters and some dependent variables.
Definition common.hpp:67
double alpha
unscaled alpha_L for use with fast inline functions only
Definition common.hpp:96
double r_cut
unscaled r_cut_iL for use with fast inline functions only
Definition common.hpp:99
Information about the three one dimensional FFTs and how the nodes have to communicate inbetween.
Definition fft.hpp:152
P3MParameters params
P3MLocalMesh local_mesh
local mesh.
Definition p3m.hpp:63
p3m_data_struct(P3MParameters &&parameters)
Definition p3m.hpp:59
p3m_send_mesh sm
send/recv mesh sizes
Definition p3m.hpp:79
double sum_q2
Sum of square of charges (only on head node).
Definition p3m.hpp:72
fft_vector< double > rs_mesh
real space mesh (local) for CA/FFT.
Definition p3m.hpp:65
double square_sum_q
square of sum of charges (only on head node).
Definition p3m.hpp:74
int sum_qpart
number of charged particles (only on head node).
Definition p3m.hpp:70
fft_data_struct fft
Definition p3m.hpp:81
p3m_interpolation_cache inter_weights
Definition p3m.hpp:76
std::array< fft_vector< double >, 3 > E_mesh
mesh (local) for the electric field.
Definition p3m.hpp:67