ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
p3m/common.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2024 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 * Common functions for dipolar and charge P3M.
24 *
25 * We use here a P3M (Particle-Particle Particle-Mesh) method based
26 * on the Ewald summation. Details of the used method can be found in
27 * @cite hockney88a and @cite deserno98a @cite deserno98b. The file p3m
28 * contains only the Particle-Mesh part.
29 *
30 * Further reading: @cite ewald21a, @cite hockney88a, @cite deserno98a,
31 * @cite deserno98b, @cite deserno00e, @cite deserno00b, @cite cerda08d
32 *
33 */
34
35#pragma once
36
37#include <config/config.hpp>
38
39#include <utils/Vector.hpp>
40#include <utils/index.hpp>
41
42#include <algorithm>
43#include <array>
44#include <vector>
45
46/** This value indicates metallic boundary conditions. */
47inline auto constexpr P3M_EPSILON_METALLIC = 0.0;
48
49#if defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
50
51#include "LocalBox.hpp"
52
53#include <cstddef>
54#include <optional>
55#include <span>
56#include <stdexcept>
57
58/** @brief P3M kernel architecture. */
59enum class Arch { CPU, CUDA };
60
61/** @brief Structure to hold P3M parameters and some dependent variables. */
63 /** tuning or production? */
64 bool tuning;
65 /** Ewald splitting parameter (0<alpha<1), rescaled to
66 * @p alpha_L = @p alpha * @p box_l. */
67 double alpha_L;
68 /** cutoff radius for real space electrostatics (>0), rescaled to
69 * @p r_cut_iL = @p r_cut * @p box_l_i. */
70 double r_cut_iL;
71 /** number of mesh points per coordinate direction (>0), in real space. */
73 /** offset of the first mesh point (lower left corner) from the
74 * coordinate origin ([0,1[). */
76 /** charge assignment order ([0,7]). */
77 int cao;
78 /** accuracy of the actual parameter set. */
79 double accuracy;
80
81 /** epsilon of the "surrounding dielectric". */
82 double epsilon;
83 /** cutoff for charge assignment. */
85 /** mesh constant. */
87 /** inverse mesh constant. */
89 /** unscaled @ref P3MParameters::alpha_L "alpha_L" for use with fast
90 * inline functions only */
91 double alpha;
92 /** unscaled @ref P3MParameters::r_cut_iL "r_cut_iL" for use with fast
93 * inline functions only */
94 double r_cut;
95 /** number of points unto which a single charge is interpolated, i.e.
96 * @ref P3MParameters::cao "cao" cubed */
97 int cao3;
98
99 P3MParameters(bool tuning, double epsilon, double r_cut,
101 int cao, double alpha, double accuracy)
102 : tuning{tuning}, alpha_L{0.}, r_cut_iL{0.}, mesh{mesh},
104 cao_cut{}, a{}, ai{}, alpha{alpha}, r_cut{r_cut}, cao3{-1} {
105
106 auto constexpr value_to_tune = -1.;
107
108 if (epsilon < 0.) {
109 throw std::domain_error("Parameter 'epsilon' must be >= 0");
110 }
111
112 if (accuracy <= 0.) {
113 throw std::domain_error("Parameter 'accuracy' must be > 0");
114 }
115
116 if (r_cut <= 0.) {
117 if (tuning and r_cut == value_to_tune) {
118 this->r_cut = 0.;
119 } else {
120 throw std::domain_error("Parameter 'r_cut' must be > 0");
121 }
122 }
123
124 if (alpha <= 0.) {
125 if (tuning and alpha == value_to_tune) {
126 this->alpha = 0.;
127 } else {
128 throw std::domain_error("Parameter 'alpha' must be > 0");
129 }
130 }
131
132 if (not(mesh >= Utils::Vector3i::broadcast(1) or
133 ((mesh[0] >= 1) and (mesh == Utils::Vector3i{{mesh[0], -1, -1}})) or
134 (tuning and mesh == Utils::Vector3i::broadcast(-1)))) {
135 throw std::domain_error("Parameter 'mesh' must be > 0");
136 }
137
138 if (not(mesh_off >= Utils::Vector3d::broadcast(0.) and
141 this->mesh_off = Utils::Vector3d::broadcast(0.5);
142 } else {
143 throw std::domain_error("Parameter 'mesh_off' must be >= 0 and <= 1");
144 }
145 }
146
147 if ((cao < 1 or cao > 7) and (not tuning or cao != -1)) {
148 throw std::domain_error("Parameter 'cao' must be >= 1 and <= 7");
149 }
150
151 if (not tuning and (Utils::Vector3i::broadcast(cao) > mesh)) {
152 throw std::domain_error("Parameter 'cao' cannot be larger than 'mesh'");
153 }
154 }
155
156 /**
157 * @brief Recalculate quantities derived from the mesh and box length:
158 * @ref P3MParameters::a "a",
159 * @ref P3MParameters::ai "ai" and
160 * @ref P3MParameters::cao_cut "cao_cut".
161 */
165 cao_cut = (static_cast<double>(cao) / 2.) * a;
166 }
167
168 /**
169 * @brief Convert spatial position to grid position.
170 * To get the grid index, round the result to the nearest integer.
171 */
172 auto calc_grid_pos(Utils::Vector3d const &pos) const {
173 return Utils::hadamard_product(pos, ai) - mesh_off;
174 }
175};
176
177/** @brief Properties of the local mesh. */
179 /** dimension (size) of local mesh including halo layers. */
182 /** number of local mesh points including halo layers. */
183 std::size_t size;
184 /** index of lower left corner of the
185 local mesh in the global mesh. */
187 /** position of the first local mesh point. */
191 /** dimension of mesh inside node domain. */
193 /** inner left down grid point */
195 /** inner up right grid point + (1,1,1) */
197 /** number of margin mesh points. */
198 int margin[6]; // !! legacy
201 /** number of margin mesh points from neighbour nodes */
202 int r_margin[6];
203 /** offset between mesh lines of the last dimension */
205 /** offset between mesh lines of the two last dimensions */
207
208 /**
209 * @brief Recalculate quantities derived from the mesh and box length:
210 * @ref P3MLocalMesh::ld_pos "ld_pos" (position of the left down mesh).
211 */
213 // spatial position of left down mesh point
214 for (auto i = 0u; i < 3u; i++) {
215 ld_pos[i] = (ld_ind[i] + params.mesh_off[i]) * params.a[i];
216 }
217 }
218
219 /**
220 * @brief Calculate properties of the local FFT mesh
221 * for the charge assignment process.
222 */
224 LocalBox const &local_geo, double skin,
225 double space_layer);
226};
227
228/** @brief Local mesh FFT buffers. */
229template <typename FloatType> struct P3MFFTMesh {
230 /** @brief real-space scalar mesh for charge assignment and FFT. */
231 std::span<FloatType> rs_scalar;
232 /** @brief real-space scalar charge density. */
233 std::span<FloatType> rs_charge_density;
234
235 /** @brief real-space vector meshes for the electric or dipolar field. */
236 std::array<std::span<FloatType>, 3> rs_fields;
237
238 /** @brief Indices of the lower left corner of the local mesh grid. */
240 /** @brief Indices of the upper right corner of the local mesh grid. */
242 /** @brief Extents of the local mesh grid. */
244
245 /** @brief number of permutations in k_space */
246 int ks_pnum = 0;
247};
248
251 std::pair<std::optional<int>, std::optional<int>> limits;
253};
254
255/**
256 * @brief Adapt an influence function grid for real-to-complex FFTs.
257 * @param[in] global_size size of the global mesh grid
258 * @param[in] local_size size of the local mesh grid
259 * @param[in] local_origin offset of the local mesh grid
260 * @param[in,out] g_function influence function grid to modify in-place
261 * @tparam r2c_dir direction of the reduced dimension
262 */
263template <unsigned int r2c_dir>
264void influence_function_r2c(auto &g_function, auto const &global_size,
265 auto const &local_size, auto const &local_origin) {
266 auto const cutoff_right = global_size[r2c_dir] / 2 - local_origin[r2c_dir];
267 std::remove_cvref_t<decltype(g_function)> g_function_r2c;
268 g_function_r2c.reserve(g_function.size() / 2ul);
269 auto local_index = Utils::Vector3i::broadcast(0);
270 auto &short_dim = local_index[r2c_dir];
271 auto &nx = local_index[0u];
272 auto &ny = local_index[1u];
273 auto &nz = local_index[2u];
274 std::size_t index = 0u;
275 for (nx = 0; nx < local_size[0u]; ++nx) {
276 for (ny = 0; ny < local_size[1u]; ++ny) {
277 for (nz = 0; nz < local_size[2u]; ++nz) {
278 if (short_dim <= cutoff_right) {
279 g_function_r2c.emplace_back(g_function[index]);
280 }
281 ++index;
282 }
283 }
284 }
285 std::swap(g_function, g_function_r2c);
286}
287
288#endif // defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
289
290/** @brief Calculate indices that shift @ref P3MParameters::mesh by `mesh/2`.
291 * For each mesh size @f$ n @f$ in @c mesh_size, create a sequence of integer
292 * values @f$ \left( 0, \ldots, \lfloor n/2 \rfloor, -\lfloor n/2 \rfloor,
293 * \ldots, -1\right) @f$ if @c zero_out_midpoint is false, otherwise
294 * @f$ \left( 0, \ldots, \lfloor n/2 - 1 \rfloor, 0, -\lfloor n/2 \rfloor,
295 * \ldots, -1\right) @f$.
296 */
297std::array<std::vector<int>, 3> inline calc_p3m_mesh_shift(
298 Utils::Vector3i const &mesh_size, bool zero_out_midpoint = false) {
299 std::array<std::vector<int>, 3> ret{};
300
301 for (auto i = 0u; i < 3u; ++i) {
302 ret[i] = std::vector<int>(static_cast<std::size_t>(mesh_size[i]));
303
304 for (int j = 1; j <= mesh_size[i] / 2; j++) {
305 ret[i][j] = j;
306 ret[i][mesh_size[i] - j] = -j;
307 }
308 if (zero_out_midpoint)
309 ret[i][mesh_size[i] / 2] = 0;
310 }
311
312 return ret;
313}
314
317 bool UseR2C = false, unsigned int R2CDir = 2u>
319 /** @brief Data layout of the input real-space 3D matrix. */
320 static auto constexpr r_space_order = RSpaceOrder;
321 /** @brief Data layout of the output k-space 3D matrix. */
322 static auto constexpr k_space_order = KSpaceOrder;
323 /** @brief Use real-to-complex implementation. */
324 static auto constexpr use_r2c = UseR2C;
325 /** @brief Direction of the reduced dimension (if @c use_r2c is true). */
326 static auto constexpr r2c_dir = R2CDir;
327};
Vector implementation and trait types for boost qvm interoperability.
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
Definition Vector.hpp:132
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
Definition Vector.hpp:395
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
Definition Vector.hpp:378
MemoryOrder
Definition index.hpp:32
Arch
P3M kernel architecture.
std::array< std::vector< int >, 3 > calc_p3m_mesh_shift(Utils::Vector3i const &mesh_size, bool zero_out_midpoint=false)
Calculate indices that shift P3MParameters::mesh by mesh/2.
void influence_function_r2c(auto &g_function, auto const &global_size, auto const &local_size, auto const &local_origin)
Adapt an influence function grid for real-to-complex FFTs.
auto constexpr P3M_EPSILON_METALLIC
This value indicates metallic boundary conditions.
static SteepestDescentParameters params
Currently active steepest descent instance.
static auto constexpr r2c_dir
Direction of the reduced dimension (if use_r2c is true).
static auto constexpr use_r2c
Use real-to-complex implementation.
static auto constexpr r_space_order
Data layout of the input real-space 3D matrix.
static auto constexpr k_space_order
Data layout of the output k-space 3D matrix.
Local mesh FFT buffers.
std::array< std::span< FloatType >, 3 > rs_fields
real-space vector meshes for the electric or dipolar field.
int ks_pnum
number of permutations in k_space
Utils::Vector3i start
Indices of the lower left corner of the local mesh grid.
std::span< FloatType > rs_scalar
real-space scalar mesh for charge assignment and FFT.
std::span< FloatType > rs_charge_density
real-space scalar charge density.
Utils::Vector3i stop
Indices of the upper right corner of the local mesh grid.
Utils::Vector3i size
Extents of the local mesh grid.
Properties of the local mesh.
Utils::Vector3i ur_no_halo
Utils::Vector3i ld_ind
index of lower left corner of the local mesh in the global mesh.
std::size_t size
number of local mesh points including halo layers.
Utils::Vector3i dim
dimension (size) of local mesh including halo layers.
void recalc_ld_pos(P3MParameters const &params)
Recalculate quantities derived from the mesh and box length: ld_pos (position of the left down mesh).
void calc_local_ca_mesh(P3MParameters const &params, LocalBox const &local_geo, double skin, double space_layer)
Calculate properties of the local FFT mesh for the charge assignment process.
Definition common.cpp:34
Utils::Vector3i dim_no_halo
Utils::Vector3i n_halo_ld
Utils::Vector3i inner
dimension of mesh inside node domain.
Utils::Vector3i in_ld
inner left down grid point
int r_margin[6]
number of margin mesh points from neighbour nodes
int margin[6]
number of margin mesh points.
int q_2_off
offset between mesh lines of the last dimension
Utils::Vector3i ld_no_halo
Utils::Vector3i in_ur
inner up right grid point + (1,1,1)
Utils::Vector3d ld_pos
position of the first local mesh point.
Utils::Vector3i n_halo_ur
int q_21_off
offset between mesh lines of the two last dimensions
Structure to hold P3M parameters and some dependent variables.
auto calc_grid_pos(Utils::Vector3d const &pos) const
Convert spatial position to grid position.
Utils::Vector3d cao_cut
cutoff for charge assignment.
double alpha
unscaled alpha_L for use with fast inline functions only
P3MParameters(bool tuning, double epsilon, double r_cut, Utils::Vector3i const &mesh, Utils::Vector3d const &mesh_off, int cao, double alpha, double accuracy)
double r_cut_iL
cutoff radius for real space electrostatics (>0), rescaled to r_cut_iL = r_cut * box_l_i.
int cao
charge assignment order ([0,7]).
double accuracy
accuracy of the actual parameter set.
double alpha_L
Ewald splitting parameter (0.
int cao3
number of points unto which a single charge is interpolated, i.e.
Utils::Vector3d mesh_off
offset of the first mesh point (lower left corner) from the coordinate origin ([0,...
Utils::Vector3d ai
inverse mesh constant.
double r_cut
unscaled r_cut_iL for use with fast inline functions only
void recalc_a_ai_cao_cut(Utils::Vector3d const &box_l)
Recalculate quantities derived from the mesh and box length: a, ai and cao_cut.
bool tuning
tuning or production?
Utils::Vector3i mesh
number of mesh points per coordinate direction (>0), in real space.
double epsilon
epsilon of the "surrounding dielectric".
Utils::Vector3d a
mesh constant.
std::pair< std::optional< int >, std::optional< int > > limits