Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
dp3m.impl.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#pragma once
23
24#include "config/config.hpp"
25
26#ifdef DP3M
27
29
30#include "p3m/common.hpp"
31#include "p3m/data_struct.hpp"
32#include "p3m/interpolation.hpp"
33
34#include "ParticleRange.hpp"
35
36#include <utils/Vector.hpp>
37
38#include <cstddef>
39#include <memory>
40#include <optional>
41#include <stdexcept>
42#include <type_traits>
43#include <utility>
44#include <vector>
45
46template <typename FloatType>
47struct p3m_data_struct_dipoles : public p3m_data_struct<FloatType> {
48 using p3m_data_struct<FloatType>::p3m_data_struct;
49
50 /** number of dipolar particles. */
51 std::size_t sum_dip_part = 0;
52 /** Sum of square of magnetic dipoles. */
53 double sum_mu2 = 0.;
54
55 /** position shift for calculation of first assignment mesh point. */
56 double pos_shift = 0.;
57
58 /** cached k-space self-energy correction */
59 double energy_correction = 0.;
60 /** k-space scalar mesh for k-space calculations. */
61 std::vector<FloatType> ks_scalar;
62
64};
65
66template <typename FloatType, Arch Architecture>
67struct DipolarP3MImpl : public DipolarP3M {
68 ~DipolarP3MImpl() override = default;
69
70 /** @brief Dipolar P3M parameters. */
72
73private:
74 /** @brief Coulomb P3M meshes and FFT algorithm. */
75 std::unique_ptr<p3m_data_struct_dipoles<FloatType>> dp3m_impl;
76 int tune_timings;
77 std::pair<std::optional<int>, std::optional<int>> tune_limits;
78 bool tune_verbose;
79 bool m_is_tuned;
80
81public:
83 std::unique_ptr<p3m_data_struct_dipoles<FloatType>> &&dp3m_handle,
84 double prefactor, int tune_timings, bool tune_verbose,
85 decltype(tune_limits) tune_limits)
86 : DipolarP3M(dp3m_handle->params), dp3m{*dp3m_handle},
87 dp3m_impl{std::move(dp3m_handle)}, tune_timings{tune_timings},
88 tune_limits{std::move(tune_limits)}, tune_verbose{tune_verbose} {
89
90 if (tune_timings <= 0) {
91 throw std::domain_error("Parameter 'timings' must be > 0");
92 }
93 if (dp3m.params.mesh != Utils::Vector3i::broadcast(dp3m.params.mesh[0])) {
94 throw std::domain_error("DipolarP3M requires a cubic mesh");
95 }
96 m_is_tuned = not dp3m.params.tuning;
97 dp3m.params.tuning = false;
99 }
100
101 void init() override {
102 if constexpr (Architecture == Arch::CPU) {
104 }
105 }
106 void tune() override;
107 void count_magnetic_particles() override;
108
109 [[nodiscard]] bool is_tuned() const noexcept override { return m_is_tuned; }
110 [[nodiscard]] bool is_gpu() const noexcept override {
111 return Architecture == Arch::GPU;
112 }
113 [[nodiscard]] bool is_double_precision() const noexcept override {
114 return std::is_same_v<FloatType, double>;
115 }
116
117 void on_activation() override {
119 tune();
120 }
121
122 double long_range_energy(ParticleRange const &particles) override {
123 return long_range_kernel(false, true, particles);
124 }
125
126 void add_long_range_forces(ParticleRange const &particles) override {
127 if constexpr (Architecture == Arch::CPU) {
128 long_range_kernel(true, false, particles);
129 }
130 }
131
132 void dipole_assign(ParticleRange const &particles) override;
133
134protected:
135 /** Compute the k-space part of forces and energies. */
136 double long_range_kernel(bool force_flag, bool energy_flag,
137 ParticleRange const &particles);
138 double calc_average_self_energy_k_space() const override;
139 void calc_energy_correction() override;
140 void calc_influence_function_force() override;
141 void calc_influence_function_energy() override;
142 double calc_surface_term(bool force_flag, bool energy_flag,
143 ParticleRange const &particles) override;
144 void init_cpu_kernels();
145 void scaleby_box_l() override;
146#ifdef NPT
147 void npt_add_virial_contribution(double energy) const override;
148#endif
149};
150
151template <typename FloatType, Arch Architecture,
152 template <typename> class FFTBackendImpl,
153 template <typename> class P3MFFTMeshImpl, class... Args>
154std::shared_ptr<DipolarP3M> new_dp3m_handle(P3MParameters &&p3m,
155 Args &&...args) {
156 auto obj = std::make_shared<DipolarP3MImpl<FloatType, Architecture>>(
157 std::make_unique<p3m_data_struct_dipoles<FloatType>>(std::move(p3m)),
158 std::forward<Args>(args)...);
159 obj->dp3m.template make_mesh_instance<P3MFFTMeshImpl<FloatType>>();
160 obj->dp3m.template make_fft_instance<FFTBackendImpl<FloatType>>();
161 return obj;
162}
163
164#endif // DP3M
Vector implementation and trait types for boost qvm interoperability.
void set_prefactor(double new_prefactor)
double prefactor
Magnetostatics prefactor.
A range of particles.
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:111
Cache for interpolation weights.
This file contains the defaults for ESPResSo.
P3M algorithm for long-range magnetic dipole-dipole interaction.
std::shared_ptr< DipolarP3M > new_dp3m_handle(P3MParameters &&p3m, Args &&...args)
Common functions for dipolar and charge P3M.
Arch
P3M kernel architecture.
static SteepestDescentParameters params
Currently active steepest descent instance.
void calc_energy_correction() override
Definition dp3m.cpp:912
double long_range_kernel(bool force_flag, bool energy_flag, ParticleRange const &particles)
Compute the k-space part of forces and energies.
Definition dp3m.cpp:250
double long_range_energy(ParticleRange const &particles) override
p3m_data_struct_dipoles< FloatType > & dp3m
Dipolar P3M parameters.
Definition dp3m.impl.hpp:71
double calc_surface_term(bool force_flag, bool energy_flag, ParticleRange const &particles) override
Definition dp3m.cpp:460
void dipole_assign(ParticleRange const &particles) override
Definition dp3m.cpp:180
bool is_double_precision() const noexcept override
void init_cpu_kernels()
Definition dp3m.cpp:130
void calc_influence_function_energy() override
Definition dp3m.cpp:535
bool is_gpu() const noexcept override
void add_long_range_forces(ParticleRange const &particles) override
void count_magnetic_particles() override
Definition dp3m.cpp:85
~DipolarP3MImpl() override=default
void npt_add_virial_contribution(double energy) const override
Definition dp3m.cpp:923
void calc_influence_function_force() override
Definition dp3m.cpp:528
bool is_tuned() const noexcept override
DipolarP3MImpl(std::unique_ptr< p3m_data_struct_dipoles< FloatType > > &&dp3m_handle, double prefactor, int tune_timings, bool tune_verbose, decltype(tune_limits) tune_limits)
Definition dp3m.impl.hpp:82
void init() override
void on_activation() override
void tune() override
Definition dp3m.cpp:665
void scaleby_box_l() override
Definition dp3m.cpp:899
double calc_average_self_energy_k_space() const override
Definition dp3m.cpp:116
Dipolar P3M solver.
Definition dp3m.hpp:54
void sanity_checks() const
Definition dp3m.hpp:78
Structure to hold P3M parameters and some dependent variables.
double energy_correction
cached k-space self-energy correction
Definition dp3m.impl.hpp:59
double pos_shift
position shift for calculation of first assignment mesh point.
Definition dp3m.impl.hpp:56
p3m_interpolation_cache inter_weights
Definition dp3m.impl.hpp:63
double sum_mu2
Sum of square of magnetic dipoles.
Definition dp3m.impl.hpp:53
std::size_t sum_dip_part
number of dipolar particles.
Definition dp3m.impl.hpp:51
std::vector< FloatType > ks_scalar
k-space scalar mesh for k-space calculations.
Definition dp3m.impl.hpp:61
LEGACY Base class for the electrostatics and magnetostatics P3M algorithms.