ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
p3m.impl.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#pragma once
23
24#include <config/config.hpp>
25
26#ifdef ESPRESSO_P3M
27
29
30#include "communication.hpp"
31#include "fft/vector.hpp"
32#include "kokkos_helpers.hpp"
33#include "p3m/P3MFFTBackend.hpp"
34#include "p3m/common.hpp"
35#include "p3m/data_struct.hpp"
36#include "p3m/interpolation.hpp"
37#include "p3m/send_mesh.hpp"
38
39#include <utils/Vector.hpp>
40#include <utils/index.hpp>
41
42#include <Kokkos_Core.hpp>
43#include <omp.h>
44
45#include <algorithm>
46#include <array>
47#include <cassert>
48#include <complex>
49#include <cstddef>
50#include <memory>
51#include <stdexcept>
52#include <type_traits>
53#include <utility>
54#include <vector>
55
56template <typename FloatType, Arch Architecture, class FFTConfig> class P3MFFT;
57
58/**
59 * @brief Base class for the electrostatics P3M algorithm.
60 * Contains a handle to the FFT backend, information about the local
61 * mesh, the differential operator, and various buffers.
62 */
63template <typename FloatType, Arch Architecture, class FFTConfig>
64struct CoulombP3MState : public P3MStateCommon<FloatType, Architecture> {
65 using P3MStateCommon<FloatType, Architecture>::P3MStateCommon;
66 using value_type = FloatType;
67 using ComplexType = std::complex<value_type>;
68 using memory_space = Kokkos::HostSpace;
69 using execution_space = Kokkos::DefaultHostExecutionSpace;
70 using r_space_layout = FFTConfig::r_space_layout;
71 using k_space_layout = FFTConfig::k_space_layout;
72
73 /** number of charged particles. */
74 std::size_t sum_qpart = 0;
75 /** Sum of square of charges. */
76 double sum_q2 = 0.;
77 /** square of sum of charges. */
78 double square_sum_q = 0.;
79
81
82 /** charge density in real-space with halo */
84 /** charge density in k-space without halo */
86 /** electric fields in real-space with halo */
87 std::array<fft::vector<FloatType>, 3> rs_E_fields;
88 /** electric fields in k-space without halo */
89 std::array<std::span<ComplexType>, 3> ks_E_fields;
91 /** electric fields in real-space without halo */
92 std::array<std::span<FloatType>, 3> rs_E_fields_no_halo;
95 // FFT reciprocal-space transform, hidden behind the backend interface so the
96 // solver is agnostic to heFFTe vs kokkos-fft (see init_cpu_kernels).
97 std::shared_ptr<P3MFFTBackend<FloatType, FFTConfig>> fft;
98 Kokkos::View<FloatType **, r_space_layout, Kokkos::HostSpace>
100
101 void init_labels() {
102 assert(rs_charge_density.empty());
104 "CoulombP3MState::rs_charge_density_kokkos", 0, 0);
105 }
106};
107
108#ifdef ESPRESSO_CUDA
109struct P3MGpuParams;
110#endif
111
112template <typename FloatType, Arch Architecture, class FFTConfig>
113struct CoulombP3MImpl : public CoulombP3M {
114 ~CoulombP3MImpl() override = default;
115
118 /** @brief Coulomb P3M parameters. */
120
121private:
122 // kokkos handle must outlive kokkos data structures from other class members
123 std::shared_ptr<KokkosHandle> m_kokkos_handle;
124 std::unique_ptr<CoulombP3MStateClass> p3m_state_ptr;
125 TuningParameters tuning;
126 bool m_is_tuned;
127
128 constexpr const Utils::Vector3i get_memory_layout() const {
129 auto constexpr memory_order = CoulombP3MStateClass::memory_order;
130 if constexpr (memory_order == Utils::MemoryOrder::COLUMN_MAJOR) {
131 return {2, 1, 0};
132 }
133 return {0, 1, 2};
134 }
135
136public:
137 CoulombP3MImpl(std::unique_ptr<CoulombP3MStateClass> &&p3m_state,
138 TuningParameters tuning_params, double prefactor)
139 : CoulombP3M(p3m_state->params), p3m{*p3m_state},
140 m_kokkos_handle{::kokkos_handle}, p3m_state_ptr{std::move(p3m_state)},
141 tuning{std::move(tuning_params)} {
142
143 if (tuning.timings <= 0) {
144 throw std::domain_error("Parameter 'timings' must be > 0");
145 }
146 m_is_tuned = not p3m.params.tuning;
147 p3m.params.tuning = false;
149 p3m.init_labels();
150 }
151
152 void init() override {
153 if constexpr (Architecture == Arch::CPU) {
155 }
156#ifdef ESPRESSO_CUDA
157 if constexpr (Architecture == Arch::CUDA) {
159 }
160#endif
161 }
162 void tune() override;
163 void count_charged_particles() override;
164 void count_charged_particles_elc(std::size_t n, double sum_q2,
165 double square_sum_q) override {
166 p3m.sum_qpart = n;
167 p3m.sum_q2 = sum_q2;
168 p3m.square_sum_q = square_sum_q;
169 }
170 void adapt_epsilon_elc() override {
171 p3m.params.epsilon = P3M_EPSILON_METALLIC;
172 }
173
174 [[nodiscard]] bool is_tuned() const noexcept override { return m_is_tuned; }
175 [[nodiscard]] bool is_gpu() const noexcept override {
176 return Architecture != Arch::CPU;
177 }
178 [[nodiscard]] bool is_double_precision() const noexcept override {
179 return std::is_same_v<FloatType, double>;
180 }
181
182 void on_activation() override {
183#ifdef ESPRESSO_CUDA
184 if constexpr (Architecture == Arch::CUDA) {
185 request_gpu();
186 }
187#endif
189 tune();
190#ifdef ESPRESSO_CUDA
191 if constexpr (Architecture == Arch::CUDA) {
192 if (is_tuned()) {
194 }
195 }
196#endif
197 }
198
199 double long_range_energy() override { return long_range_kernel(false, true); }
200
201 void add_long_range_forces() override {
202 if constexpr (Architecture == Arch::CPU) {
203 long_range_kernel(true, false);
204 }
205#ifdef ESPRESSO_CUDA
206 if constexpr (Architecture == Arch::CUDA) {
208 }
209#endif
210 }
211
213
214 void charge_assign() override;
215 void assign_charge(double q, Utils::Vector3d const &real_pos,
216 bool skip_cache) override;
217 void prepare_fft_mesh(bool reset_weights, bool zero_rs) override {
218 if (reset_weights) {
219 p3m.inter_weights.reset(p3m.params.cao);
220 }
221 p3m.rs_charge_density.resize(p3m.local_mesh.size);
222 using execution_space = Kokkos::DefaultHostExecutionSpace;
223 auto const num_threads = execution_space().concurrency();
224 Kokkos::realloc(Kokkos::WithoutInitializing, p3m.rs_charge_density_kokkos,
225 num_threads, p3m.local_mesh.size);
226 kokkos_deep_copy(execution_space{}, p3m.rs_charge_density_kokkos,
227 FloatType{0});
228 if (num_threads > 1 or zero_rs) {
229 std::ranges::fill(p3m.rs_charge_density, FloatType{0});
230 }
231 }
232
233protected:
234 /** Compute the k-space part of forces and energies. */
235 double long_range_kernel(bool force_flag, bool energy_flag);
236 void calc_influence_function_force() override;
237 void calc_influence_function_energy() override;
238 void scaleby_box_l() override;
239 void init_cpu_kernels();
240#ifdef ESPRESSO_CUDA
241 void init_gpu_kernels();
243 std::shared_ptr<P3MGpuParams> m_gpu_data = nullptr;
244 void request_gpu() const;
245#endif
246private:
247 void kernel_ks_charge_density();
248 void kernel_rs_electric_field();
249};
250
251#endif // ESPRESSO_P3M
Vector implementation and trait types for boost qvm interoperability.
void set_prefactor(double new_prefactor)
FFT manager.
Definition P3MFFT.hpp:53
Cache for interpolation weights.
P3M halo communicator.
Definition send_mesh.hpp:38
std::shared_ptr< KokkosHandle > kokkos_handle
ESPRESSO_ATTR_ALWAYS_INLINE void kokkos_deep_copy(auto const &exec_space, auto const &view, auto const &value)
Wrapper for Kokkos::deep_copy that skips fork/join when the number of threads is 1.
VectorXd< 9 > Vector9d
Definition Vector.hpp:210
VectorXd< 3 > Vector3d
Definition Vector.hpp:207
VectorXi< 3 > Vector3i
Definition Vector.hpp:216
Definition fft.cpp:77
std::vector< T, allocator< T > > vector
Definition vector.hpp:54
STL namespace.
Common functions for dipolar and charge P3M.
auto constexpr P3M_EPSILON_METALLIC
This value indicates metallic boundary conditions.
P3M algorithm for long-range Coulomb interaction.
~CoulombP3MImpl() override=default
void count_charged_particles() override
bool is_tuned() const noexcept override
Definition p3m.impl.hpp:174
bool is_gpu() const noexcept override
Definition p3m.impl.hpp:175
void add_long_range_forces() override
Definition p3m.impl.hpp:201
CoulombP3MStateClass & p3m
Coulomb P3M parameters.
Definition p3m.impl.hpp:119
void init() override
Definition p3m.impl.hpp:152
CoulombP3MImpl(std::unique_ptr< CoulombP3MStateClass > &&p3m_state, TuningParameters tuning_params, double prefactor)
Definition p3m.impl.hpp:137
void charge_assign() override
double long_range_energy() override
Definition p3m.impl.hpp:199
double long_range_kernel(bool force_flag, bool energy_flag)
Compute the k-space part of forces and energies.
Utils::Vector9d long_range_pressure() override
void scaleby_box_l() override
void calc_influence_function_force() override
Calculate the optimal influence function of hockney88a.
void count_charged_particles_elc(std::size_t n, double sum_q2, double square_sum_q) override
Definition p3m.impl.hpp:164
void assign_charge(double q, Utils::Vector3d const &real_pos, bool skip_cache) override
std::shared_ptr< P3MGpuParams > m_gpu_data
Definition p3m.impl.hpp:243
bool is_double_precision() const noexcept override
Definition p3m.impl.hpp:178
CoulombP3MState< FloatType, Architecture, FFTConfig > CoulombP3MStateClass
Definition p3m.impl.hpp:116
void prepare_fft_mesh(bool reset_weights, bool zero_rs) override
Definition p3m.impl.hpp:217
void adapt_epsilon_elc() override
Definition p3m.impl.hpp:170
void calc_influence_function_energy() override
Calculate the influence function optimized for the energy and the self energy correction.
void on_activation() override
Definition p3m.impl.hpp:182
Base class for the electrostatics P3M algorithm.
Definition p3m.impl.hpp:64
Kokkos::DefaultHostExecutionSpace execution_space
Definition p3m.impl.hpp:69
Kokkos::View< FloatType **, r_space_layout, Kokkos::HostSpace > rs_charge_density_kokkos
Definition p3m.impl.hpp:99
fft::vector< ComplexType > ks_charge_density
charge density in k-space without halo
Definition p3m.impl.hpp:85
fft::vector< FloatType > rs_charge_density
charge density in real-space with halo
Definition p3m.impl.hpp:83
FFTConfig::r_space_layout r_space_layout
Definition p3m.impl.hpp:70
Kokkos::HostSpace memory_space
Definition p3m.impl.hpp:68
std::array< std::span< ComplexType >, 3 > ks_E_fields
electric fields in k-space without halo
Definition p3m.impl.hpp:89
fft::vector< ComplexType > ks_E_fields_batched
Definition p3m.impl.hpp:90
fft::vector< FloatType > rs_E_fields_no_halo_batched
Definition p3m.impl.hpp:93
FFTConfig::k_space_layout k_space_layout
Definition p3m.impl.hpp:71
p3m_interpolation_cache inter_weights
Definition p3m.impl.hpp:80
FloatType value_type
Definition p3m.impl.hpp:66
double square_sum_q
square of sum of charges.
Definition p3m.impl.hpp:78
std::array< std::span< FloatType >, 3 > rs_E_fields_no_halo
electric fields in real-space without halo
Definition p3m.impl.hpp:92
std::array< fft::vector< FloatType >, 3 > rs_E_fields
electric fields in real-space with halo
Definition p3m.impl.hpp:87
std::complex< value_type > ComplexType
Definition p3m.impl.hpp:67
std::size_t sum_qpart
number of charged particles.
Definition p3m.impl.hpp:74
p3m_send_mesh< FloatType > halo_comm
Definition p3m.impl.hpp:94
double sum_q2
Sum of square of charges.
Definition p3m.impl.hpp:76
CoulombP3M(P3MParameters const &p3m_params)
Definition p3m.hpp:59
void sanity_checks() const
Definition p3m.hpp:79
P3MStateCommon(P3MParameters &&parameters)