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
33
35
37
38#include <filesystem>
39#include <memory>
40#include <stdexcept>
41#include <string>
42#include <unordered_map>
43
45
46class EKVTKHandle : public VTKHandleBase<::EKinWalberlaBase> {
47 static std::unordered_map<std::string, int> const obs_map;
48
49 std::unordered_map<std::string, int> const &get_obs_map() const override {
50 return obs_map;
51 }
52};
53
54class EKSpecies : public LatticeModel<::EKinWalberlaBase, EKVTKHandle> {
55protected:
62 double m_tau;
63 double m_density;
64
65public:
68 {{"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
69 {"diffusion",
70 [this](Variant const &v) {
72 },
73 [this]() { return m_instance->get_diffusion() / m_conv_diffusion; }},
74 {"kT",
75 [this](Variant const &v) {
77 auto const kT = get_value<double>(v);
78 if (kT < 0.) {
79 throw std::domain_error("Parameter 'kT' must be >= 0");
80 }
81 m_instance->set_kT(kT * m_conv_energy);
82 });
83 },
84 [this]() { return m_instance->get_kT() / m_conv_energy; }},
85 {"valency",
86 [this](Variant const &v) {
87 m_instance->set_valency(get_value<double>(v));
88 },
89 [this]() { return m_instance->get_valency(); }},
90 {"ext_efield",
91 [this](Variant const &v) {
92 m_instance->set_ext_efield(get_value<Utils::Vector3d>(v) *
94 },
95 [this]() {
96 return m_instance->get_ext_efield() / m_conv_ext_efield;
97 }},
98 {"advection",
99 [this](Variant const &v) {
100 m_instance->set_advection(get_value<bool>(v));
101 },
102 [this]() { return m_instance->get_advection(); }},
103 {"friction_coupling",
104 [this](Variant const &v) {
105 m_instance->set_friction_coupling(get_value<bool>(v));
106 },
107 [this]() { return m_instance->get_friction_coupling(); }},
108 {"single_precision", AutoParameter::read_only,
109 [this]() { return not m_instance->is_double_precision(); }},
110 {"tau", AutoParameter::read_only, [this]() { return m_tau; }},
111 {"density", AutoParameter::read_only,
112 [this]() { return m_density / m_conv_density; }},
113 {"thermalized", AutoParameter::read_only,
114 [this]() { return m_instance->is_thermalized(); }},
116 [this]() { return static_cast<int>(m_instance->get_seed()); }},
117 {"rng_state",
118 [this](Variant const &v) {
119 auto const rng_state = get_value<int>(v);
120 context()->parallel_try_catch([&]() {
121 if (rng_state < 0) {
122 throw std::domain_error("Parameter 'rng_state' must be >= 0");
123 }
124 m_instance->set_rng_state(static_cast<uint64_t>(rng_state));
125 });
126 },
127 [this]() {
128 auto const opt = m_instance->get_rng_state();
129 return (opt) ? Variant{static_cast<int>(*opt)} : Variant{None{}};
130 }},
131 {"shape", AutoParameter::read_only,
132 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
133 {"vtk_writers", AutoParameter::read_only,
134 [this]() { return serialize_vtk_writers(); }}});
135 }
136
137 void do_construct(VariantMap const &params) override;
138
139 [[nodiscard]] auto get_ekinstance() const { return m_instance; }
140 [[nodiscard]] auto get_lattice() const { return m_lattice; }
141
142 Variant do_call_method(std::string const &method,
143 VariantMap const &parameters) override;
144
151
154 return {
155 {"density", 1. / m_conv_density},
156 {"flux", 1. / m_conv_flux},
157 };
158 }
159
160private:
161 void load_checkpoint(std::filesystem::path const &path, int mode);
162 void save_checkpoint(std::filesystem::path const &path, int mode);
163};
164
165class EKSpeciesCPU : public EKSpecies {
166protected:
167 void make_instance(VariantMap const &params) override;
168};
169
170#ifdef ESPRESSO_CUDA
171class EKSpeciesGPU : public EKSpecies {
172protected:
173 void make_instance(VariantMap const &params) override;
174};
175#endif // ESPRESSO_CUDA
176
177} // namespace ScriptInterface::walberla
178
179#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.
void make_instance(VariantMap const &params) override
void make_instance(VariantMap const &params) override
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
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
Definition EKSpecies.cpp:52
void do_construct(VariantMap const &params) override
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition EKSpecies.hpp:49
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 SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84