ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LBFluid.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-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
31#include "core/lb/Solver.hpp"
33
35
39
40#include <utils/Vector.hpp>
42
43#include <cstddef>
44#include <filesystem>
45#include <memory>
46#include <optional>
47#include <stdexcept>
48#include <string>
49#include <unordered_map>
50#include <vector>
51
53
54class LBVTKHandle : public VTKHandleBase<::LBWalberlaBase> {
55 static std::unordered_map<std::string, int> const obs_map;
56
57 std::unordered_map<std::string, int> const &get_obs_map() const override {
58 return obs_map;
59 }
60};
61
62inline void
63lb_throw_if_expired(std::optional<ResourceObserver> const &mpi_obs) {
64 if (not(mpi_obs and mpi_obs->is_valid())) {
65 throw std::runtime_error(
66 "the MPI Cartesian communicator of this LB object has expired");
67 }
68}
69
70class LBFluid : public LatticeModel<::LBWalberlaBase, LBVTKHandle> {
71protected:
73 std::shared_ptr<::LB::LBWalberlaParams> m_lb_params;
74 std::optional<ResourceObserver> m_mpi_cart_comm_observer;
84
85public:
88 {"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
89 {"single_precision", AutoParameter::read_only,
90 [this]() { return not m_instance->is_double_precision(); }},
92 [this]() { return m_instance->is_gpu(); }},
93 {"is_active", AutoParameter::read_only,
94 [this]() { return m_is_active; }},
96 [this]() { return m_lb_params->get_agrid(); }},
98 [this]() { return m_lb_params->get_tau(); }},
100 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
102 [this]() { return m_instance->get_kT() / m_conv_energy; }},
104 [this]() { return static_cast<int>(m_instance->get_seed()); }},
105 {"rng_state",
106 [this](Variant const &v) {
107 auto const rng_state = get_value<int>(v);
108 context()->parallel_try_catch([&]() {
109 if (rng_state < 0) {
110 throw std::domain_error("Parameter 'rng_state' must be >= 0");
111 }
112 m_instance->set_rng_state(static_cast<uint64_t>(rng_state));
113 });
114 },
115 [this]() {
116 auto const opt = m_instance->get_rng_state();
117 return (opt) ? Variant{static_cast<int>(*opt)} : Variant{None{}};
118 }},
119 {"density", AutoParameter::read_only,
120 [this]() { return m_instance->get_density() / m_conv_dens; }},
121 {"kinematic_viscosity",
122 [this](Variant const &v) {
123 auto const visc = m_conv_visc * get_value<double>(v);
124 m_instance->set_viscosity(visc);
125 },
126 [this]() { return m_instance->get_viscosity() / m_conv_visc; }},
127 {"ext_force_density",
128 [this](Variant const &v) {
130 m_instance->set_external_force(ext_f);
131 },
132 [this]() {
133 return m_instance->get_external_force() / m_conv_force_dens;
134 }},
135 {"vtk_writers", AutoParameter::read_only,
136 [this]() { return serialize_vtk_writers(); }},
137 });
138 }
139
140 void do_construct(VariantMap const &params) override;
141
142 Variant do_call_method(std::string const &name,
143 VariantMap const &params) override;
144
145 [[nodiscard]] auto get_lb_fluid() const { return m_instance; }
146 [[nodiscard]] auto get_lb_params() const { return m_lb_params; }
150
153 return {
154 {"density", 1. / m_conv_dens},
155 {"velocity", 1. / m_conv_speed},
156 {"pressure", 1. / m_conv_press},
157 };
158 }
159
160protected:
161 void make_instance(VariantMap const &params) override;
162
163private:
164 void load_checkpoint(std::filesystem::path const &path, int mode);
165 void save_checkpoint(std::filesystem::path const &path, int mode);
166 std::vector<Variant> get_average_pressure_tensor() const;
167 Variant get_boundary_force_from_shape(std::vector<int> const &raster) const;
168 Variant get_boundary_force() const;
169 Variant get_interpolated_velocity(Utils::Vector3d const &pos) const;
170};
171
172} // namespace ScriptInterface::walberla
173
174#endif // ESPRESSO_WALBERLA
LBWalberlaBase provides the public interface of the LB waLBerla bridge.
Vector implementation and trait types for boost qvm interoperability.
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.
std::string_view name() const
std::optional< ResourceObserver > m_mpi_cart_comm_observer
Definition LBFluid.hpp:74
void do_construct(VariantMap const &params) override
Definition LBFluid.cpp:169
void make_instance(VariantMap const &params) override
Definition LBFluid.cpp:143
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
Definition LBFluid.hpp:152
Variant do_call_method(std::string const &name, VariantMap const &params) override
Definition LBFluid.cpp:71
std::shared_ptr<::LB::LBWalberlaParams > m_lb_params
Definition LBFluid.hpp:73
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition LBFluid.hpp:57
void lb_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
Definition LBFluid.hpp:63
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