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-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
31#include "core/lb/Solver.hpp"
33
35
39
40#include <utils/Vector.hpp>
42
43#include <cstddef>
44#include <cstdint>
45#include <filesystem>
46#include <memory>
47#include <optional>
48#include <stdexcept>
49#include <string>
50#include <unordered_map>
51#include <vector>
52
54
55class LBVTKHandle : public VTKHandleBase<::LBWalberlaBase> {
56 static std::unordered_map<std::string, int> const obs_map;
57
58 std::unordered_map<std::string, int> const &get_obs_map() const override {
59 return obs_map;
60 }
61};
62
63inline void
64lb_throw_if_expired(std::optional<ResourceObserver> const &mpi_obs) {
65 if (not(mpi_obs and mpi_obs->is_valid())) {
66 throw std::runtime_error(
67 "the MPI Cartesian communicator of this LB object has expired");
68 }
69}
70
71class LBFluid : public LatticeModel<::LBWalberlaBase, LBVTKHandle> {
72protected:
74 std::shared_ptr<::LB::LBWalberlaParams> m_lb_params;
75 std::optional<ResourceObserver> m_mpi_cart_comm_observer;
85
86public:
89 {"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
90 {"single_precision", AutoParameter::read_only,
91 [this]() { return not m_instance->is_double_precision(); }},
93 [this]() { return m_instance->is_gpu(); }},
94 {"is_active", AutoParameter::read_only,
95 [this]() { return m_is_active; }},
97 [this]() { return m_lb_params->get_agrid(); }},
99 [this]() { return m_lb_params->get_tau(); }},
100 {"shape", AutoParameter::read_only,
101 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
103 [this]() { return m_instance->get_kT() / m_conv_energy; }},
105 [this]() { return static_cast<int>(m_instance->get_seed()); }},
106 {"rng_state",
107 [this](Variant const &v) {
108 auto const rng_state = get_value<int>(v);
109 context()->parallel_try_catch([&]() {
110 if (rng_state < 0) {
111 throw std::domain_error("Parameter 'rng_state' must be >= 0");
112 }
113 m_instance->set_rng_state(static_cast<uint64_t>(rng_state));
114 });
115 },
116 [this]() {
117 auto const opt = m_instance->get_rng_state();
118 return (opt) ? Variant{static_cast<int>(*opt)} : Variant{None{}};
119 }},
120 {"density", AutoParameter::read_only,
121 [this]() { return m_instance->get_density() / m_conv_dens; }},
122 {"kinematic_viscosity",
123 [this](Variant const &v) {
124 auto const visc = m_conv_visc * get_value<double>(v);
125 m_instance->set_viscosity(visc);
126 },
127 [this]() { return m_instance->get_viscosity() / m_conv_visc; }},
128 {"ext_force_density",
129 [this](Variant const &v) {
131 m_instance->set_external_force(ext_f);
132 },
133 [this]() {
134 return m_instance->get_external_force() / m_conv_force_dens;
135 }},
136 {"vtk_writers", AutoParameter::read_only,
137 [this]() { return serialize_vtk_writers(); }},
138 });
139 }
140
141 void do_construct(VariantMap const &params) override;
142
143 Variant do_call_method(std::string const &name,
144 VariantMap const &params) override;
145
146 [[nodiscard]] auto get_lb_fluid() const { return m_instance; }
147 [[nodiscard]] auto get_lb_params() const { return m_lb_params; }
151
154 return {
155 {"density", 1. / m_conv_dens},
156 {"velocity", 1. / m_conv_speed},
157 {"pressure", 1. / m_conv_press},
158 };
159 }
160
161protected:
162 void make_instance(VariantMap const &params) override;
163
164private:
165 void load_checkpoint(std::filesystem::path const &path, int mode);
166 void save_checkpoint(std::filesystem::path const &path, int mode);
167 std::vector<Variant> get_average_pressure_tensor() const;
168 Variant get_boundary_force_from_shape(std::vector<int> const &raster) const;
169 Variant get_boundary_force() const;
170 Variant get_interpolated_velocity(Utils::Vector3d const &pos) const;
171};
172
173} // namespace ScriptInterface::walberla
174
175#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:31
Context * context() const
Responsible context.
std::string_view name() const
std::optional< ResourceObserver > m_mpi_cart_comm_observer
Definition LBFluid.hpp:75
void do_construct(VariantMap const &params) override
Definition LBFluid.cpp:170
void make_instance(VariantMap const &params) override
Definition LBFluid.cpp:144
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
Definition LBFluid.hpp:153
Variant do_call_method(std::string const &name, VariantMap const &params) override
Definition LBFluid.cpp:72
std::shared_ptr<::LB::LBWalberlaParams > m_lb_params
Definition LBFluid.hpp:74
std::unordered_map< std::string, int > const & get_obs_map() const override
Definition LBFluid.hpp:58
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
void lb_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
Definition LBFluid.hpp:64
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84