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
30
#include <
walberla_bridge/LatticeModel.hpp
>
31
#include <
walberla_bridge/LatticeWalberla.hpp
>
32
#include <
walberla_bridge/electrokinetics/EKinWalberlaBase.hpp
>
33
#include <
walberla_bridge/utils/ResourceManager.hpp
>
34
35
#include <
script_interface/ScriptInterface.hpp
>
36
37
#include <
utils/math/int_pow.hpp
>
38
39
#include <filesystem>
40
#include <memory>
41
#include <optional>
42
#include <stdexcept>
43
#include <string>
44
#include <unordered_map>
45
46
namespace
ScriptInterface::walberla
{
47
48
class
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
56
inline
void
57
ek_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
64
class
EKSpecies
:
public
LatticeModel
<::EKinWalberlaBase, EKVTKHandle> {
65
protected
:
66
using
Base
=
LatticeModel<::EKinWalberlaBase, EKVTKHandle>
;
67
std::optional<ResourceObserver>
m_mpi_cart_comm_observer
;
68
double
m_conv_diffusion
;
69
double
m_conv_ext_efield
;
70
double
m_conv_energy
;
71
double
m_conv_density
;
72
double
m_conv_flux
;
73
double
m_tau
;
74
double
m_density
;
75
76
public
:
77
EKSpecies
() {
78
add_parameters
(
79
{{
"lattice"
,
AutoParameter::read_only
, [
this
]() {
return
m_lattice
; }},
80
{
"single_precision"
,
AutoParameter::read_only
,
81
[
this
]() {
return
not
m_instance
->is_double_precision(); }},
82
{
"gpu"
,
AutoParameter::read_only
,
83
[
this
]() {
return
m_instance
->is_gpu(); }},
84
{
"diffusion"
,
85
[
this
](
Variant
const
&v) {
86
m_instance
->set_diffusion(
get_value<double>
(v) *
m_conv_diffusion
);
87
},
88
[
this
]() {
return
m_instance
->get_diffusion() /
m_conv_diffusion
; }},
89
{
"kT"
,
90
[
this
](
Variant
const
&v) {
91
context
()->
parallel_try_catch
([&]() {
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) *
108
m_conv_ext_efield
);
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(); }},
128
{
"seed"
,
AutoParameter::read_only
,
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
¶ms)
override
;
151
152
[[
nodiscard
]]
auto
get_ekinstance
()
const
{
return
m_instance
; }
153
[[
nodiscard
]]
auto
get_lattice
()
const
{
return
m_lattice
; }
154
[[
nodiscard
]]
auto
get_mpi_cart_comm_observer
()
const
{
155
return
m_mpi_cart_comm_observer
;
156
}
157
158
Variant
do_call_method
(std::string
const
&
method
,
159
VariantMap
const
&
parameters
)
override
;
160
161
[[
nodiscard
]]
auto
get_conversion_factor_density
()
const
noexcept
{
162
return
m_conv_density
;
163
}
164
[[
nodiscard
]]
auto
get_conversion_factor_flux
()
const
noexcept
{
165
return
m_conv_flux
;
166
}
167
168
::LatticeModel::units_map
169
get_lattice_to_md_units_conversion
()
const override
{
170
return
{
171
{
"density"
, 1. /
m_conv_density
},
172
{
"flux"
, 1. /
m_conv_flux
},
173
};
174
}
175
176
protected
:
177
void
make_instance
(
VariantMap
const
¶ms)
override
;
178
179
private
:
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
EKinWalberlaBase.hpp
ResourceManager.hpp
ScriptInterface.hpp
LatticeModel::units_map
std::unordered_map< std::string, double > units_map
Definition
walberla_bridge/include/walberla_bridge/LatticeModel.hpp:33
ScriptInterface::AutoParameters::add_parameters
void add_parameters(std::vector< AutoParameter > &¶ms)
Definition
AutoParameters.hpp:123
ScriptInterface::Context::parallel_try_catch
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
ScriptInterface::None
Type to indicate no value in Variant.
Definition
None.hpp:32
ScriptInterface::ObjectHandle::context
Context * context() const
Responsible context.
Definition
ObjectHandle.hpp:57
ScriptInterface::walberla::EKSpecies
Definition
EKSpecies.hpp:64
ScriptInterface::walberla::EKSpecies::get_conversion_factor_flux
auto get_conversion_factor_flux() const noexcept
Definition
EKSpecies.hpp:164
ScriptInterface::walberla::EKSpecies::m_conv_density
double m_conv_density
Definition
EKSpecies.hpp:71
ScriptInterface::walberla::EKSpecies::m_conv_diffusion
double m_conv_diffusion
Definition
EKSpecies.hpp:68
ScriptInterface::walberla::EKSpecies::get_ekinstance
auto get_ekinstance() const
Definition
EKSpecies.hpp:152
ScriptInterface::walberla::EKSpecies::get_conversion_factor_density
auto get_conversion_factor_density() const noexcept
Definition
EKSpecies.hpp:161
ScriptInterface::walberla::EKSpecies::m_density
double m_density
Definition
EKSpecies.hpp:74
ScriptInterface::walberla::EKSpecies::m_conv_flux
double m_conv_flux
Definition
EKSpecies.hpp:72
ScriptInterface::walberla::EKSpecies::get_lattice_to_md_units_conversion
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
Definition
EKSpecies.hpp:169
ScriptInterface::walberla::EKSpecies::get_mpi_cart_comm_observer
auto get_mpi_cart_comm_observer() const
Definition
EKSpecies.hpp:154
ScriptInterface::walberla::EKSpecies::get_lattice
auto get_lattice() const
Definition
EKSpecies.hpp:153
ScriptInterface::walberla::EKSpecies::m_mpi_cart_comm_observer
std::optional< ResourceObserver > m_mpi_cart_comm_observer
Definition
EKSpecies.hpp:67
ScriptInterface::walberla::EKSpecies::EKSpecies
EKSpecies()
Definition
EKSpecies.hpp:77
ScriptInterface::walberla::EKSpecies::make_instance
void make_instance(VariantMap const ¶ms) override
Definition
EKSpecies.cpp:106
ScriptInterface::walberla::EKSpecies::do_call_method
Variant do_call_method(std::string const &method, VariantMap const ¶meters) override
Definition
EKSpecies.cpp:56
ScriptInterface::walberla::EKSpecies::m_conv_energy
double m_conv_energy
Definition
EKSpecies.hpp:70
ScriptInterface::walberla::EKSpecies::m_conv_ext_efield
double m_conv_ext_efield
Definition
EKSpecies.hpp:69
ScriptInterface::walberla::EKSpecies::m_tau
double m_tau
Definition
EKSpecies.hpp:73
ScriptInterface::walberla::EKSpecies::do_construct
void do_construct(VariantMap const ¶ms) override
Definition
EKSpecies.cpp:136
ScriptInterface::walberla::EKVTKHandle
Definition
EKSpecies.hpp:48
ScriptInterface::walberla::EKVTKHandle::get_obs_map
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition
EKSpecies.hpp:51
ScriptInterface::walberla::LatticeModel
Definition
script_interface/walberla/LatticeModel.hpp:40
ScriptInterface::walberla::LatticeModel<::EKinWalberlaBase, EKVTKHandle >::m_instance
std::shared_ptr< ::EKinWalberlaBase > m_instance
Definition
script_interface/walberla/LatticeModel.hpp:43
ScriptInterface::walberla::LatticeModel<::EKinWalberlaBase, EKVTKHandle >::serialize_vtk_writers
auto serialize_vtk_writers() const
Definition
script_interface/walberla/LatticeModel.hpp:54
ScriptInterface::walberla::LatticeModel<::EKinWalberlaBase, EKVTKHandle >::m_lattice
std::shared_ptr< LatticeWalberla > m_lattice
Definition
script_interface/walberla/LatticeModel.hpp:42
ScriptInterface::walberla::VTKHandleBase
Definition
script_interface/walberla/VTKHandle.hpp:46
config.hpp
int_pow.hpp
ScriptInterface::walberla
Definition
script_interface/walberla/EKContainer.hpp:49
ScriptInterface::walberla::ek_throw_if_expired
void ek_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
Definition
EKSpecies.hpp:57
ScriptInterface::get_value
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
Definition
get_value.hpp:398
ScriptInterface::VariantMap
std::unordered_map< std::string, Variant > VariantMap
Definition
Variant.hpp:133
ScriptInterface::AutoParameter::read_only
static constexpr const ReadOnly read_only
Definition
AutoParameter.hpp:42
ScriptInterface::impl::recursive_variant
Recursive variant implementation.
Definition
Variant.hpp:84
LatticeModel.hpp
LatticeWalberla.hpp
VTKHandle.hpp
src
script_interface
walberla
EKSpecies.hpp
Generated on Thu Feb 12 2026 02:30:09 for ESPResSo by
1.9.8