ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
data_struct.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#if defined(P3M) or defined(DP3M)
27
28#include "common.hpp"
29
30#include <array>
31#include <cassert>
32#include <memory>
33#include <tuple>
34#include <utility>
35#include <vector>
36
37template <typename FloatType> class FFTBackend;
38template <typename FloatType> class FFTBuffers;
39
40/**
41 * @brief Base class for the electrostatics and magnetostatics P3M algorithms.
42 * Contains a handle to the FFT backend, information about the local mesh,
43 * the differential operator, and various buffers.
44 */
45template <typename FloatType> struct p3m_data_struct {
46 using value_type = FloatType;
47
48 explicit p3m_data_struct(P3MParameters &&parameters)
49 : params{std::move(parameters)} {}
50
51 /** @brief P3M base parameters. */
53 /** @brief Local mesh properties. */
55 /** @brief Local mesh FFT buffers. */
57
58 /**
59 * @brief Spatial differential operator in k-space.
60 * We use an i*k differentiation.
61 */
62 std::array<std::vector<int>, 3> d_op;
63
64 /** Calculate the Fourier transformed differential operator.
65 * Remark: This is done on the level of n-vectors and not k-vectors,
66 * i.e. the prefactor @f$ 2i\pi/L @f$ is missing!
67 */
69 d_op = detail::calc_meshift(params.mesh, true);
70 }
71
72 /** @brief Force optimised influence function (k-space) */
73 std::vector<FloatType> g_force;
74 /** @brief Energy optimised influence function (k-space) */
75 std::vector<FloatType> g_energy;
76 /** @brief FFT algorithm. */
77 std::unique_ptr<FFTBackend<FloatType>> fft;
78 /** @brief FFT buffers. */
79 std::unique_ptr<FFTBuffers<FloatType>> fft_buffers;
80
81 void init();
82
84 auto const mesh_size_ptr = fft->get_mesh_size();
85 auto const mesh_start_ptr = fft->get_mesh_start();
86 for (auto i = 0u; i < 3u; ++i) {
87 mesh.size[i] = mesh_size_ptr[i];
88 mesh.start[i] = mesh_start_ptr[i];
89 }
90 mesh.stop = mesh.start + mesh.size;
91 fft_buffers->update_mesh_views(mesh);
92 }
93
94 template <typename T, class... Args> void make_fft_instance(Args... args) {
95 assert(fft == nullptr);
96 fft = std::make_unique<T>(std::as_const(local_mesh), args...);
97 }
98
99 template <typename T, class... Args> void make_mesh_instance(Args... args) {
100 assert(fft_buffers == nullptr);
101 fft_buffers = std::make_unique<T>(std::as_const(local_mesh), args...);
102 }
103};
104
105/**
106 * @brief API for the FFT backend of the P3M algorithm.
107 */
108template <typename FloatType> class FFTBackend {
109protected:
111
112public:
116 virtual ~FFTBackend() = default;
117 virtual void init(P3MParameters const &params) = 0;
118 virtual int get_ca_mesh_size() const noexcept = 0;
119 virtual int get_ks_pnum() const noexcept = 0;
120 /** @brief Carry out the forward FFT of the scalar mesh. */
121 virtual void forward_fft(FloatType *rs_mesh) = 0;
122 /** @brief Carry out the backward FFT of the scalar mesh. */
123 virtual void backward_fft(FloatType *rs_mesh) = 0;
124 /** @brief Get indices of the k-space data layout. */
125 virtual std::tuple<int, int, int> get_permutations() const = 0;
126 virtual std::array<int, 3u> const &get_mesh_size() const = 0;
127 virtual std::array<int, 3u> const &get_mesh_start() const = 0;
128};
129
130/**
131 * @brief API for the FFT mesh buffers.
132 */
133template <typename FloatType> class FFTBuffers {
134protected:
136
137public:
141 virtual ~FFTBuffers() = default;
142 /** @brief Initialize the meshes. */
143 virtual void init_meshes(int ca_mesh_size) = 0;
144 /** @brief Initialize the halo buffers. */
145 virtual void init_halo() = 0;
146 /** @brief Update scalar mesh halo with data from neighbors (accumulation). */
147 virtual void perform_scalar_halo_gather() = 0;
148 /** @brief Update vector mesh halo with data from neighbors (accumulation). */
149 virtual void perform_vector_halo_gather() = 0;
150 /** @brief Update scalar mesh halo of all neighbors. */
151 virtual void perform_scalar_halo_spread() = 0;
152 /** @brief Update vector mesh halo of all neighbors. */
153 virtual void perform_vector_halo_spread() = 0;
154 /**
155 * @brief Get pointer to scalar mesh begin.
156 * Should only be used by @ref FFTBackend.
157 */
158 virtual FloatType *get_scalar_mesh() = 0;
159 /**
160 * @brief Get pointer to vector mesh begin.
161 * Should only be used by @ref FFTBackend.
162 */
163 virtual std::array<FloatType *, 3u> get_vector_mesh() = 0;
164 /**
165 * @brief Update the scalar and vector mesh views in @ref P3MFFTMesh
166 * to point to the new underlying data structures.
167 */
169};
170
171#endif // defined(P3M) or defined(DP3M)
API for the FFT backend of the P3M algorithm.
virtual void init(P3MParameters const &params)=0
virtual int get_ca_mesh_size() const noexcept=0
virtual ~FFTBackend()=default
virtual void backward_fft(FloatType *rs_mesh)=0
Carry out the backward FFT of the scalar mesh.
bool check_complex_residuals
virtual std::array< int, 3u > const & get_mesh_size() const =0
P3MLocalMesh const & local_mesh
virtual int get_ks_pnum() const noexcept=0
virtual void forward_fft(FloatType *rs_mesh)=0
Carry out the forward FFT of the scalar mesh.
virtual std::array< int, 3u > const & get_mesh_start() const =0
FFTBackend(P3MLocalMesh const &local_mesh)
virtual std::tuple< int, int, int > get_permutations() const =0
Get indices of the k-space data layout.
API for the FFT mesh buffers.
P3MLocalMesh const & local_mesh
virtual void init_meshes(int ca_mesh_size)=0
Initialize the meshes.
virtual void perform_scalar_halo_gather()=0
Update scalar mesh halo with data from neighbors (accumulation).
virtual void perform_vector_halo_gather()=0
Update vector mesh halo with data from neighbors (accumulation).
virtual std::array< FloatType *, 3u > get_vector_mesh()=0
Get pointer to vector mesh begin.
virtual void perform_vector_halo_spread()=0
Update vector mesh halo of all neighbors.
virtual void init_halo()=0
Initialize the halo buffers.
virtual void update_mesh_views(P3MFFTMesh< FloatType > &out)=0
Update the scalar and vector mesh views in P3MFFTMesh to point to the new underlying data structures.
FFTBuffers(P3MLocalMesh const &local_mesh)
virtual ~FFTBuffers()=default
virtual void perform_scalar_halo_spread()=0
Update scalar mesh halo of all neighbors.
virtual FloatType * get_scalar_mesh()=0
Get pointer to scalar mesh begin.
This file contains the defaults for ESPResSo.
Definition fft.cpp:74
Common functions for dipolar and charge P3M.
static SteepestDescentParameters params
Currently active steepest descent instance.
Local mesh FFT buffers.
Properties of the local mesh.
Structure to hold P3M parameters and some dependent variables.
Utils::Vector3i mesh
number of mesh points per coordinate direction (>0).
Base class for the electrostatics and magnetostatics P3M algorithms.
std::unique_ptr< FFTBuffers< FloatType > > fft_buffers
FFT buffers.
void make_fft_instance(Args... args)
P3MLocalMesh local_mesh
Local mesh properties.
P3MParameters params
P3M base parameters.
void update_mesh_views()
std::array< std::vector< int >, 3 > d_op
Spatial differential operator in k-space.
std::unique_ptr< FFTBackend< FloatType > > fft
FFT algorithm.
std::vector< FloatType > g_energy
Energy optimised influence function (k-space)
P3MFFTMesh< FloatType > mesh
Local mesh FFT buffers.
FloatType value_type
p3m_data_struct(P3MParameters &&parameters)
void make_mesh_instance(Args... args)
void calc_differential_operator()
Calculate the Fourier transformed differential operator.
std::vector< FloatType > g_force
Force optimised influence function (k-space)