ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKSpecies.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
25
26#include "LatticeModel.hpp"
27#include "LatticeWalberla.hpp"
28#include "VTKHandle.hpp"
29
34
36
38
39#include <cstdint>
40#include <filesystem>
41#include <memory>
42#include <optional>
43#include <stdexcept>
44#include <string>
45#include <unordered_map>
46
48
49class EKVTKHandle : public VTKHandleBase<::EKinWalberlaBase> {
50 static std::unordered_map<std::string, int> const obs_map;
51
52 std::unordered_map<std::string, int> const &get_obs_map() const override {
53 return obs_map;
54 }
55};
56
57inline void
58ek_throw_if_expired(std::optional<ResourceObserver> const &mpi_obs) {
59 if (not(mpi_obs and mpi_obs->is_valid())) {
60 throw std::runtime_error(
61 "the MPI Cartesian communicator of this EK object has expired");
62 }
63}
64
65class EKSpecies : public LatticeModel<::EKinWalberlaBase, EKVTKHandle> {
66protected:
68 std::optional<ResourceObserver> m_mpi_cart_comm_observer;
74 double m_tau;
75 double m_density;
76
77public:
80 {{"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
81 {"single_precision", AutoParameter::read_only,
82 [this]() { return not m_instance->is_double_precision(); }},
84 [this]() { return m_instance->is_gpu(); }},
85 {"diffusion",
86 [this](Variant const &v) {
88 },
89 [this]() { return m_instance->get_diffusion() / m_conv_diffusion; }},
90 {"kT",
91 [this](Variant const &v) {
93 auto const kT = get_value<double>(v);
94 if (kT < 0.) {
95 throw std::domain_error("Parameter 'kT' must be >= 0");
96 }
97 m_instance->set_kT(kT * m_conv_energy);
98 });
99 },
100 [this]() { return m_instance->get_kT() / m_conv_energy; }},
101 {"valency",
102 [this](Variant const &v) {
103 m_instance->set_valency(get_value<double>(v));
104 },
105 [this]() { return m_instance->get_valency(); }},
106 {"ext_efield",
107 [this](Variant const &v) {
108 m_instance->set_ext_efield(get_value<Utils::Vector3d>(v) *
110 },
111 [this]() {
112 return m_instance->get_ext_efield() / m_conv_ext_efield;
113 }},
114 {"advection",
115 [this](Variant const &v) {
116 m_instance->set_advection(get_value<bool>(v));
117 },
118 [this]() { return m_instance->get_advection(); }},
119 {"friction_coupling",
120 [this](Variant const &v) {
121 m_instance->set_friction_coupling(get_value<bool>(v));
122 },
123 [this]() { return m_instance->get_friction_coupling(); }},
124 {"tau", AutoParameter::read_only, [this]() { return m_tau; }},
125 {"density", AutoParameter::read_only,
126 [this]() { return m_density / m_conv_density; }},
127 {"thermalized", AutoParameter::read_only,
128 [this]() { return m_instance->is_thermalized(); }},
130 [this]() { return static_cast<int>(m_instance->get_seed()); }},
131 {"rng_state",
132 [this](Variant const &v) {
133 auto const rng_state = get_value<int>(v);
134 context()->parallel_try_catch([&]() {
135 if (rng_state < 0) {
136 throw std::domain_error("Parameter 'rng_state' must be >= 0");
137 }
138 m_instance->set_rng_state(static_cast<uint64_t>(rng_state));
139 });
140 },
141 [this]() {
142 auto const opt = m_instance->get_rng_state();
143 return (opt) ? Variant{static_cast<int>(*opt)} : Variant{None{}};
144 }},
145 {"shape", AutoParameter::read_only,
146 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
147 {"vtk_writers", AutoParameter::read_only,
148 [this]() { return serialize_vtk_writers(); }}});
149 }
150
151 void do_construct(VariantMap const &params) override;
152
153 [[nodiscard]] auto get_ekinstance() const { return m_instance; }
154 [[nodiscard]] auto get_lattice() const { return m_lattice; }
158
159 Variant do_call_method(std::string const &method,
160 VariantMap const &parameters) override;
161
168
171 return {
172 {"density", 1. / m_conv_density},
173 {"flux", 1. / m_conv_flux},
174 };
175 }
176
178 context()->parallel_try_catch([&]() {
179 if (get_lattice()->lattice()->get_ghost_layers() < 2 and
180 context()->get_comm().size() > 1) {
181 throw std::runtime_error("The number of ghostlayers should be > 1 "
182 "when using flux boundaries and MPI");
183 }
184 });
185 }
186
187protected:
188 void make_instance(VariantMap const &params) override;
189
190private:
191 void load_checkpoint(std::filesystem::path const &path, int mode);
192 void save_checkpoint(std::filesystem::path const &path, int mode);
193};
194
195} // namespace ScriptInterface::walberla
196
197#endif // ESPRESSO_WALBERLA
std::unordered_map< std::string, double > units_map
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Type to indicate no value in Variant.
Definition None.hpp:31
Context * context() const
Responsible context.
auto get_conversion_factor_flux() const noexcept
void flux_boundary_ghost_layer_size_sanity_check() const
auto get_conversion_factor_density() const noexcept
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
std::optional< ResourceObserver > m_mpi_cart_comm_observer
Definition EKSpecies.hpp:68
void make_instance(VariantMap const &params) override
Definition EKSpecies.cpp:98
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
Definition EKSpecies.cpp:57
void do_construct(VariantMap const &params) override
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition EKSpecies.hpp:52
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
void ek_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
Definition EKSpecies.hpp:58
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84