ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKFFT.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <config/config.hpp>
23
24#ifdef ESPRESSO_WALBERLA_FFT
25
26#include "EKPoissonSolver.hpp"
27
28#include "LatticeWalberla.hpp"
29
30#include "core/MpiCallbacks.hpp"
32
35
39
41
43
44#include <memory>
45#include <string>
46#include <vector>
47
49
50class EKFFT : public EKPoissonSolver {
51protected:
52 std::unique_ptr<ResourceManager> m_resources_lock;
53 std::shared_ptr<LatticeWalberla> m_lattice;
55 bool m_gpu;
57
58protected:
59 void make_instance(VariantMap const &args) override {
60 // unit conversions
61 auto const agrid = get_value<double>(m_lattice->get_parameter("agrid"));
62 m_conv_permittivity = Utils::int_pow<2>(agrid);
63 auto const permittivity =
65 auto *make_new_instance = &::walberla::new_ek_poisson_fft;
66 if (m_gpu) {
67 std::vector<std::string> required_features;
68 required_features.emplace_back("CUDA");
70#ifdef ESPRESSO_CUDA
71 make_new_instance = &::walberla::new_ek_poisson_fft_cuda;
72#endif
73 }
74 m_instance = make_new_instance(m_lattice->lattice(), permittivity,
76 {
77#ifdef ESPRESSO_FPE
78 // cuFFT builds device kernels using CUDA-JIT
79 // (https://docs.nvidia.com/cuda/archive/13.1.1/cufft/#plan-initialization-time)
80 // please note this operation is not guaranteed to succeed for all
81 // mesh sizes, and in rare cases, it can send the SIGFPE signal
83#endif
84 auto const use_gpu_aware =
85 m_gpu and ::communication_environment->is_mpi_gpu_aware();
86 m_instance->setup_fft(use_gpu_aware);
87 }
88 }
89
90public:
91 void do_construct(VariantMap const &args) override {
92 m_gpu = get_value_or<bool>(args, "gpu", false);
93 m_single_precision = get_value_or<bool>(args, "single_precision", m_gpu);
97
98 make_instance(args), m_resources_lock = std::make_unique<ResourceManager>();
99 // MPI communicator is needed to destroy the FFT plans
100 m_resources_lock->acquire_lock(::communication_environment->get_mpi_env());
101 for (auto &vtk : m_vtk_writers) {
102 vtk->attach_to_lattice(m_instance, get_lattice_to_md_units_conversion());
103 }
104 }
105
108 {"permittivity",
109 [this](Variant const &v) {
110 m_instance->set_permittivity(get_value<double>(v) *
112 },
113 [this]() {
114 return m_instance->get_permittivity() / m_conv_permittivity;
115 }},
116 {"single_precision", AutoParameter::read_only,
117 [this]() { return m_single_precision; }},
118 {"gpu", AutoParameter::read_only, [this]() { return m_gpu; }},
119 {"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
120 {"shape", AutoParameter::read_only,
121 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
122 });
123 }
124
125 ~EKFFT() override {
126 m_lattice.reset();
127 m_instance.reset();
128 m_resources_lock.reset();
129 }
130
131 [[nodiscard]] std::shared_ptr<::walberla::PoissonSolver>
133 return m_instance;
134 }
135};
136
137} // namespace ScriptInterface::walberla
138
139#endif // ESPRESSO_WALBERLA_FFT
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
void add_parameters(std::vector< AutoParameter > &&params)
std::unique_ptr< ResourceManager > m_resources_lock
Definition EKFFT.hpp:52
void do_construct(VariantMap const &args) override
Definition EKFFT.hpp:91
std::shared_ptr< LatticeWalberla > m_lattice
Definition EKFFT.hpp:53
void make_instance(VariantMap const &args) override
Definition EKFFT.hpp:59
std::shared_ptr<::walberla::PoissonSolver > get_instance() const noexcept override
Definition EKFFT.hpp:132
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
static std::shared_ptr< scoped_pause > make_shared_pause_scoped()
Generate a shared handle to temporarily disable any currently active exception trap for the lifetime ...
Definition fe_trap.cpp:144
std::unique_ptr< CommunicationEnvironment > communication_environment
This file contains the asynchronous MPI communication.
void check_features(std::vector< std::string > const &features)
Definition CodeInfo.cpp:74
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
std::shared_ptr< walberla::PoissonSolver > new_ek_poisson_fft(std::shared_ptr< LatticeWalberla > const &lattice, double permittivity, bool single_precision)
std::shared_ptr< walberla::PoissonSolver > new_ek_poisson_fft_cuda(std::shared_ptr< LatticeWalberla > const &lattice, double permittivity, bool single_precision)
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84