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-2023 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 <filesystem>
40#include <memory>
41#include <optional>
42#include <stdexcept>
43#include <string>
44#include <unordered_map>
45
47
48class EKVTKHandle : public VTKHandleBase<::EKinWalberlaBase> {
49 static std::unordered_map<std::string, int> const obs_map;
50
51 std::unordered_map<std::string, int> const &get_obs_map() const override {
52 return obs_map;
53 }
54};
55
56inline void
57ek_throw_if_expired(std::optional<ResourceObserver> const &mpi_obs) {
58 if (not(mpi_obs and mpi_obs->is_valid())) {
59 throw std::runtime_error(
60 "the MPI Cartesian communicator of this EK object has expired");
61 }
62}
63
64class EKSpecies : public LatticeModel<::EKinWalberlaBase, EKVTKHandle> {
65protected:
67 std::optional<ResourceObserver> m_mpi_cart_comm_observer;
73 double m_tau;
74 double m_density;
75
76public:
79 {{"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
80 {"single_precision", AutoParameter::read_only,
81 [this]() { return not m_instance->is_double_precision(); }},
83 [this]() { return m_instance->is_gpu(); }},
84 {"diffusion",
85 [this](Variant const &v) {
87 },
88 [this]() { return m_instance->get_diffusion() / m_conv_diffusion; }},
89 {"kT",
90 [this](Variant const &v) {
92 auto const kT = get_value<double>(v);
93 if (kT < 0.) {
94 throw std::domain_error("Parameter 'kT' must be >= 0");
95 }
96 m_instance->set_kT(kT * m_conv_energy);
97 });
98 },
99 [this]() { return m_instance->get_kT() / m_conv_energy; }},
100 {"valency",
101 [this](Variant const &v) {
102 m_instance->set_valency(get_value<double>(v));
103 },
104 [this]() { return m_instance->get_valency(); }},
105 {"ext_efield",
106 [this](Variant const &v) {
107 m_instance->set_ext_efield(get_value<Utils::Vector3d>(v) *
109 },
110 [this]() {
111 return m_instance->get_ext_efield() / m_conv_ext_efield;
112 }},
113 {"advection",
114 [this](Variant const &v) {
115 m_instance->set_advection(get_value<bool>(v));
116 },
117 [this]() { return m_instance->get_advection(); }},
118 {"friction_coupling",
119 [this](Variant const &v) {
120 m_instance->set_friction_coupling(get_value<bool>(v));
121 },
122 [this]() { return m_instance->get_friction_coupling(); }},
123 {"tau", AutoParameter::read_only, [this]() { return m_tau; }},
124 {"density", AutoParameter::read_only,
125 [this]() { return m_density / m_conv_density; }},
126 {"thermalized", AutoParameter::read_only,
127 [this]() { return m_instance->is_thermalized(); }},
129 [this]() { return static_cast<int>(m_instance->get_seed()); }},
130 {"rng_state",
131 [this](Variant const &v) {
132 auto const rng_state = get_value<int>(v);
133 context()->parallel_try_catch([&]() {
134 if (rng_state < 0) {
135 throw std::domain_error("Parameter 'rng_state' must be >= 0");
136 }
137 m_instance->set_rng_state(static_cast<uint64_t>(rng_state));
138 });
139 },
140 [this]() {
141 auto const opt = m_instance->get_rng_state();
142 return (opt) ? Variant{static_cast<int>(*opt)} : Variant{None{}};
143 }},
144 {"shape", AutoParameter::read_only,
145 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
146 {"vtk_writers", AutoParameter::read_only,
147 [this]() { return serialize_vtk_writers(); }}});
148 }
149
150 void do_construct(VariantMap const &params) override;
151
152 [[nodiscard]] auto get_ekinstance() const { return m_instance; }
153 [[nodiscard]] auto get_lattice() const { return m_lattice; }
157
158 Variant do_call_method(std::string const &method,
159 VariantMap const &parameters) override;
160
167
170 return {
171 {"density", 1. / m_conv_density},
172 {"flux", 1. / m_conv_flux},
173 };
174 }
175
176protected:
177 void make_instance(VariantMap const &params) override;
178
179private:
180 void load_checkpoint(std::filesystem::path const &path, int mode);
181 void save_checkpoint(std::filesystem::path const &path, int mode);
182};
183
184} // namespace ScriptInterface::walberla
185
186#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:32
Context * context() const
Responsible context.
auto get_conversion_factor_flux() const noexcept
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:67
void make_instance(VariantMap const &params) override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
Definition EKSpecies.cpp:56
void do_construct(VariantMap const &params) override
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition EKSpecies.hpp:51
void ek_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
Definition EKSpecies.hpp:57
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
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84