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-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/** \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 <Kokkos_Core.hpp>
54
55#include <cstddef>
56#include <exception>
57#include <optional>
58#include <span>
59#include <stdexcept>
60#include <string>
61#include <utility>
62
63/** @brief P3M kernel architecture. */
64enum class Arch { CPU, CUDA };
65
66/** @brief Structure to hold P3M parameters and some dependent variables. */
68 /** tuning or production? */
69 bool tuning;
70 /** Ewald splitting parameter (0<alpha<1), rescaled to
71 * @p alpha_L = @p alpha * @p box_l. */
72 double alpha_L;
73 /** cutoff radius for real space electrostatics (>0), rescaled to
74 * @p r_cut_iL = @p r_cut * @p box_l_i. */
75 double r_cut_iL;
76 /** number of mesh points per coordinate direction (>0), in real space. */
78 /** offset of the first mesh point (lower left corner) from the
79 * coordinate origin ([0,1[). */
81 /** charge assignment order ([0,7]). */
82 int cao;
83 /** accuracy of the actual parameter set. */
84 double accuracy;
85
86 /** epsilon of the "surrounding dielectric". */
87 double epsilon;
88 /** cutoff for charge assignment. */
90 /** mesh constant. */
92 /** inverse mesh constant. */
94 /** unscaled @ref P3MParameters::alpha_L "alpha_L" for use with fast
95 * inline functions only */
96 double alpha;
97 /** unscaled @ref P3MParameters::r_cut_iL "r_cut_iL" for use with fast
98 * inline functions only */
99 double r_cut;
100 /** number of points unto which a single charge is interpolated, i.e.
101 * @ref P3MParameters::cao "cao" cubed */
102 int cao3;
103
104 P3MParameters(bool tuning, double epsilon, double r_cut,
106 int cao, double alpha, double accuracy)
107 : tuning{tuning}, alpha_L{0.}, r_cut_iL{0.}, mesh{mesh},
109 cao_cut{}, a{}, ai{}, alpha{alpha}, r_cut{r_cut}, cao3{-1} {
110
111 auto constexpr value_to_tune = -1.;
112
113 if (epsilon < 0.) {
114 throw std::domain_error("Parameter 'epsilon' must be >= 0");
115 }
116
117 if (accuracy <= 0.) {
118 throw std::domain_error("Parameter 'accuracy' must be > 0");
119 }
120
121 if (r_cut <= 0.) {
122 if (tuning and r_cut == value_to_tune) {
123 this->r_cut = 0.;
124 } else {
125 throw std::domain_error("Parameter 'r_cut' must be > 0");
126 }
127 }
128
129 if (alpha <= 0.) {
130 if (tuning and alpha == value_to_tune) {
131 this->alpha = 0.;
132 } else {
133 throw std::domain_error("Parameter 'alpha' must be > 0");
134 }
135 }
136
137 if (not(mesh >= Utils::Vector3i::broadcast(1) or
138 ((mesh[0] >= 1) and (mesh == Utils::Vector3i{{mesh[0], -1, -1}})) or
139 (tuning and mesh == Utils::Vector3i::broadcast(-1)))) {
140 throw std::domain_error("Parameter 'mesh' must be > 0");
141 }
142
143 if (not(mesh_off >= Utils::Vector3d::broadcast(0.) and
146 this->mesh_off = Utils::Vector3d::broadcast(0.5);
147 } else {
148 throw std::domain_error("Parameter 'mesh_off' must be >= 0 and <= 1");
149 }
150 }
151
152 if ((cao < 1 or cao > 7) and (not tuning or cao != -1)) {
153 throw std::domain_error("Parameter 'cao' must be >= 1 and <= 7");
154 }
155
156 if (not tuning and (Utils::Vector3i::broadcast(cao) > mesh)) {
157 throw std::domain_error("Parameter 'cao' cannot be larger than 'mesh'");
158 }
159 }
160
161 /**
162 * @brief Recalculate quantities derived from the mesh and box length:
163 * @ref P3MParameters::a "a",
164 * @ref P3MParameters::ai "ai" and
165 * @ref P3MParameters::cao_cut "cao_cut".
166 */
170 cao_cut = (static_cast<double>(cao) / 2.) * a;
171 }
172
173 /**
174 * @brief Convert spatial position to grid position.
175 * To get the grid index, round the result to the nearest integer.
176 */
177 auto calc_grid_pos(Utils::Vector3d const &pos) const {
178 return Utils::hadamard_product(pos, ai) - mesh_off;
179 }
180};
181
182/**
183 * @brief Properties of the local mesh and halo region.
184 * The thickness of the halo region depends on the following parameters:
185 * - The half-width of the charge-assignment stencil (cao_cut = cao/2*a).
186 * A charge at position (0,0,0) is spread onto a cube of width cao, hence
187 * the half-width must be padded on both sides of the local domain.
188 * - The Verlet list skin must be added extra, since particles are not resorted
189 * at every time step, but rather when they moved more than one skin.
190 * - The ELC space layer is also needed when dielectric contrast is active,
191 * since real charges within `space_layer` of the bottom wall induce image
192 * charges outside the simulation box, hence the halo region is thicker
193 * in the non-periodic direction.
194 */
196 /** dimension (size) of local mesh including halo layers. */
199 /** number of local mesh points including halo layers. */
200 std::size_t size;
201 /** index of lower left corner of the
202 local mesh in the global mesh. */
204 /** position of the first local mesh point. */
208 /** dimension of mesh inside node domain. */
210 /** inner left down grid point */
212 /** inner up right grid point + (1,1,1) */
214 /** number of margin mesh points. */
215 int margin[6]; // !! legacy
218 /** number of margin mesh points from neighbour nodes */
219 int r_margin[6];
220 /** offset between mesh lines of the last dimension */
222 /** offset between mesh lines of the two last dimensions */
224
225 /**
226 * @brief Recalculate quantities derived from the mesh and box length:
227 * @ref P3MLocalMesh::ld_pos "ld_pos" (position of the left down mesh).
228 */
229 void recalc_ld_pos(P3MParameters const &params) {
230 // spatial position of left down mesh point
231 for (auto i = 0u; i < 3u; i++) {
232 ld_pos[i] = (ld_ind[i] + params.mesh_off[i]) * params.a[i];
233 }
234 }
235
236 /**
237 * @brief Calculate properties of the local FFT mesh
238 * for the charge assignment process.
239 */
240 void calc_local_ca_mesh(P3MParameters const &params,
241 LocalBox const &local_geo, double skin,
242 double space_layer);
243};
244
245/** @brief Local mesh FFT buffers. */
246template <typename FloatType> struct P3MFFTMesh {
247 /** @brief real-space scalar mesh for charge assignment and FFT. */
248 std::span<FloatType> rs_scalar;
249 /** @brief real-space scalar charge density. */
250 std::span<FloatType> rs_charge_density;
251
252 /** @brief real-space vector meshes for the electric or dipolar field. */
253 std::array<std::span<FloatType>, 3> rs_fields;
254
255 /** @brief Indices of the lower left corner of the local mesh grid. */
257 /** @brief Indices of the upper right corner of the local mesh grid. */
259 /** @brief Extents of the local mesh grid. */
261
262 /** @brief number of permutations in k_space */
263 int ks_pnum = 0;
264};
265
268 std::pair<std::optional<int>, std::optional<int>> limits;
270};
271
272/**
273 * @brief Adapt an influence function grid for real-to-complex FFTs.
274 * @param[in] global_size size of the global mesh grid
275 * @param[in] local_size size of the local mesh grid
276 * @param[in] local_origin offset of the local mesh grid
277 * @param[in,out] g_function influence function grid to modify in-place
278 * @tparam r2c_dir direction of the reduced dimension
279 */
280template <unsigned int r2c_dir>
281void influence_function_r2c(auto &g_function, auto const &global_size,
282 auto const &local_size, auto const &local_origin) {
283 auto const cutoff_right = global_size[r2c_dir] / 2 - local_origin[r2c_dir];
284 std::remove_cvref_t<decltype(g_function)> g_function_r2c;
285 g_function_r2c.reserve(g_function.size() / 2ul);
286 auto local_index = Utils::Vector3i::broadcast(0);
287 auto &short_dim = local_index[r2c_dir];
288 auto &nx = local_index[0u];
289 auto &ny = local_index[1u];
290 auto &nz = local_index[2u];
291 std::size_t index = 0u;
292 for (nx = 0; nx < local_size[0u]; ++nx) {
293 for (ny = 0; ny < local_size[1u]; ++ny) {
294 for (nz = 0; nz < local_size[2u]; ++nz) {
295 if (short_dim <= cutoff_right) {
296 g_function_r2c.emplace_back(g_function[index]);
297 }
298 ++index;
299 }
300 }
301 }
302 std::swap(g_function, g_function_r2c);
303}
304
305#endif // defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
306
307/** @brief Calculate indices that shift @ref P3MParameters::mesh by `mesh/2`.
308 * For each mesh size @f$ n @f$ in @c mesh_size, create a sequence of integer
309 * values @f$ \left( 0, \ldots, \lfloor n/2 \rfloor, -\lfloor n/2 \rfloor,
310 * \ldots, -1\right) @f$ if @c zero_out_midpoint is false, otherwise
311 * @f$ \left( 0, \ldots, \lfloor n/2 - 1 \rfloor, 0, -\lfloor n/2 \rfloor,
312 * \ldots, -1\right) @f$.
313 */
314std::array<std::vector<int>, 3> inline calc_p3m_mesh_shift(
315 Utils::Vector3i const &mesh_size, bool zero_out_midpoint = false) {
316 std::array<std::vector<int>, 3> ret{};
317
318 for (auto i = 0u; i < 3u; ++i) {
319 ret[i] = std::vector<int>(static_cast<std::size_t>(mesh_size[i]));
320
321 for (int j = 1; j <= mesh_size[i] / 2; j++) {
322 ret[i][j] = j;
323 ret[i][mesh_size[i] - j] = -j;
324 }
325 if (zero_out_midpoint)
326 ret[i][mesh_size[i] / 2] = 0;
327 }
328
329 return ret;
330}
331
332#if defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
335 bool UseR2C = false, unsigned int R2CDir = 2u>
337 /** @brief Data layout of the input real-space 3D matrix. */
339 std::conditional_t<RSpaceOrder == Utils::MemoryOrder::ROW_MAJOR,
340 Kokkos::LayoutRight, Kokkos::LayoutLeft>;
341 /** @brief Data layout of the output k-space 3D matrix. */
343 std::conditional_t<KSpaceOrder == Utils::MemoryOrder::ROW_MAJOR,
344 Kokkos::LayoutRight, Kokkos::LayoutLeft>;
345 /** @brief Data layout of the input real-space 3D matrix. */
346 static auto constexpr r_space_order = RSpaceOrder;
347 /** @brief Data layout of the output k-space 3D matrix. */
348 static auto constexpr k_space_order = KSpaceOrder;
349 /** @brief Use real-to-complex implementation. */
350 static auto constexpr use_r2c = UseR2C;
351 /** @brief Direction of the reduced dimension (if @c use_r2c is true). */
352 static auto constexpr r2c_dir = R2CDir;
353};
354
355class p3m_tuning_exception : public std::exception {
356 std::string m_msg;
357
358public:
359 p3m_tuning_exception(std::string msg) : m_msg{std::move(msg)} {}
360 char const *what() const noexcept override { return m_msg.c_str(); }
361};
362
363#endif // defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
Vector implementation and trait types for boost qvm interoperability.
static DEVICE_QUALIFIER constexpr Vector< int, N > broadcast(typename Base::value_type const &value) noexcept
Definition Vector.hpp:144
p3m_tuning_exception(std::string msg)
char const * what() const noexcept override
VectorXd< 3 > Vector3d
Definition Vector.hpp:207
VectorXi< 3 > Vector3i
Definition Vector.hpp:216
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
Definition Vector.hpp:401
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
Definition Vector.hpp:384
MemoryOrder
Definition index.hpp:33
STL namespace.
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.
std::conditional_t< RSpaceOrder==Utils::MemoryOrder::ROW_MAJOR, Kokkos::LayoutRight, Kokkos::LayoutLeft > r_space_layout
Data layout of the input real-space 3D matrix.
std::conditional_t< KSpaceOrder==Utils::MemoryOrder::ROW_MAJOR, Kokkos::LayoutRight, Kokkos::LayoutLeft > k_space_layout
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 and halo region.
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