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 auto const tau = get_value<double>(args, "tau");
63 m_tau = tau;
64 m_conv_permittivity = Utils::int_pow<3>(agrid) / Utils::int_pow<2>(tau);
66 auto const permittivity =
68 auto *make_new_instance = &::walberla::new_ek_poisson_fft;
69 if (m_gpu) {
70 std::vector<std::string> required_features;
71 required_features.emplace_back("CUDA");
73#ifdef ESPRESSO_CUDA
74 make_new_instance = &::walberla::new_ek_poisson_fft_cuda;
75#endif
76 }
77 m_instance = make_new_instance(m_lattice->lattice(), permittivity,
79 {
80#ifdef ESPRESSO_FPE
81 // cuFFT builds device kernels using CUDA-JIT
82 // (https://docs.nvidia.com/cuda/archive/13.1.1/cufft/#plan-initialization-time)
83 // please note this operation is not guaranteed to succeed for all
84 // mesh sizes, and in rare cases, it can send the SIGFPE signal
86#endif
87 auto const use_gpu_aware =
88 m_gpu and ::communication_environment->is_mpi_gpu_aware();
89 m_instance->setup_fft(use_gpu_aware);
90 }
91 }
92
93public:
94 void do_construct(VariantMap const &args) override {
95 m_gpu = get_value_or<bool>(args, "gpu", false);
96 m_single_precision = get_value_or<bool>(args, "single_precision", m_gpu);
100
101 make_instance(args), m_resources_lock = std::make_unique<ResourceManager>();
102 // MPI communicator is needed to destroy the FFT plans
103 m_resources_lock->acquire_lock(::communication_environment->get_mpi_env());
104 for (auto &vtk : m_vtk_writers) {
105 vtk->attach_to_lattice(m_instance, get_lattice_to_md_units_conversion());
106 }
107 }
108
111 {"tau", AutoParameter::read_only, [this]() { return m_tau; }},
112 {"permittivity",
113 [this](Variant const &v) {
114 m_instance->set_permittivity(get_value<double>(v) *
116 },
117 [this]() {
118 return m_instance->get_permittivity() / m_conv_permittivity;
119 }},
120 {"single_precision", AutoParameter::read_only,
121 [this]() { return m_single_precision; }},
122 {"gpu", AutoParameter::read_only, [this]() { return m_gpu; }},
123 {"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
124 {"shape", AutoParameter::read_only,
125 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
126 });
127 }
128
129 ~EKFFT() override {
130 m_lattice.reset();
131 m_instance.reset();
132 m_resources_lock.reset();
133 }
134
135 [[nodiscard]] std::shared_ptr<::walberla::PoissonSolver>
137 return m_instance;
138 }
139};
140
141} // namespace ScriptInterface::walberla
142
143#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:94
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:136
void set_potential_conversion(double agrid, double tau)
::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