ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
lb/Solver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 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#include "config/config.hpp"
21
22#include "lb/Implementation.hpp"
23#include "lb/Solver.hpp"
24#include "lb/utils.hpp"
25
26#include "lb/LBNone.hpp"
27#include "lb/LBWalberla.hpp"
28
29#include "BoxGeometry.hpp"
30#include "system/System.hpp"
31#include "thermostat.hpp"
32
33#ifdef WALBERLA
35#endif
36
37#include <utils/Vector.hpp>
38
39#include <cassert>
40#include <cmath>
41#include <limits>
42#include <memory>
43#include <optional>
44#include <stdexcept>
45#include <string>
46#include <tuple>
47#include <variant>
48#include <vector>
49
50namespace LB {
51
52Solver::Solver() { impl = std::make_unique<Implementation>(); }
53
54static auto is_solver_set(std::unique_ptr<Solver::Implementation> const &ptr) {
55 return ptr != nullptr and ptr->solver.has_value();
56}
57
58static void check_solver(std::unique_ptr<Solver::Implementation> const &ptr) {
59 if (not is_solver_set(ptr)) {
60 throw NoLBActive{};
61 }
62}
63
64bool Solver::is_solver_set() const { return LB::is_solver_set(impl); }
65
67 System::get_system().lb.impl->solver = std::nullopt;
68 m_conv = Conversions{};
69}
70
72 check_solver(impl);
73 std::visit([](auto &ptr) { ptr->propagate(); }, *impl->solver);
74}
75
77 check_solver(impl);
78 std::visit([](auto &ptr) { ptr->ghost_communication(); }, *impl->solver);
79}
80
82 check_solver(impl);
83 std::visit([](auto &ptr) { ptr->ghost_communication_pdf(); }, *impl->solver);
84}
85
87 check_solver(impl);
88 std::visit([](auto &ptr) { ptr->ghost_communication_vel(); }, *impl->solver);
89}
90
92 if (impl->solver) {
93 auto const &system = get_system();
94 std::visit([&](auto &ptr) { ptr->sanity_checks(system); }, *impl->solver);
95 }
96}
97
98void Solver::veto_time_step(double time_step) const {
99 if (impl->solver) {
100 std::visit([=](auto &ptr) { ptr->veto_time_step(time_step); },
101 *impl->solver);
102 }
103}
104
105void Solver::veto_kT(double kT) const {
106 if (impl->solver) {
107 std::visit([=](auto &ptr) { ptr->veto_kT(kT); }, *impl->solver);
108 }
109}
110
111void Solver::lebc_sanity_checks(unsigned int shear_direction,
112 unsigned int shear_plane_normal) const {
113 if (impl->solver) {
114 auto const callback = [=](auto &ptr) {
115 ptr->lebc_sanity_checks(shear_direction, shear_plane_normal);
116 };
117 std::visit(callback, *impl->solver);
118 }
119}
120
122 if (impl->solver) {
123 auto &solver = *impl->solver;
124 std::visit([](auto &ptr) { ptr->on_cell_structure_change(); }, solver);
125 }
126}
127
129 if (impl->solver) {
130 std::visit([](auto const &ptr) { ptr->veto_boxl_change(); }, *impl->solver);
131 }
132}
133
135 if (impl->solver) {
136 std::visit([](auto &ptr) { ptr->on_boxl_change(); }, *impl->solver);
137 }
138}
139
141 if (impl->solver) {
142 std::visit([](auto &ptr) { ptr->on_node_grid_change(); }, *impl->solver);
143 }
144}
145
147 if (impl->solver) {
148 std::visit([](auto &ptr) { ptr->on_timestep_change(); }, *impl->solver);
149 }
150}
151
153 if (impl->solver) {
154 std::visit([](auto &ptr) { ptr->on_temperature_change(); }, *impl->solver);
155 }
156}
157
159 if (impl->solver) {
160 std::visit([](auto &ptr) { ptr->on_lees_edwards_change(); }, *impl->solver);
161 }
162}
163
165 if (impl->solver) {
166 std::visit([](auto &ptr) { ptr->update_collision_model(); }, *impl->solver);
167 }
168}
169
170bool Solver::is_gpu() const {
171 check_solver(impl);
172 return std::visit([](auto &ptr) { return ptr->is_gpu(); }, *impl->solver);
173}
174
175double Solver::get_agrid() const {
176 check_solver(impl);
177 return std::visit([](auto &ptr) { return ptr->get_agrid(); }, *impl->solver);
178}
179
180double Solver::get_tau() const {
181 check_solver(impl);
182 return std::visit([](auto &ptr) { return ptr->get_tau(); }, *impl->solver);
183}
184
185double Solver::get_kT() const {
186 check_solver(impl);
187 return std::visit([](auto &ptr) { return ptr->get_kT(); }, *impl->solver);
188}
189
191 check_solver(impl);
192 return std::visit([](auto &ptr) { return ptr->get_pressure_tensor(); },
193 *impl->solver);
194}
195
196std::optional<Utils::Vector3d>
198 /* calculate fluid velocity at particle's position
199 this is done by linear interpolation
200 (Eq. (11) Ahlrichs and Duenweg, JCP 111(17):8225 (1999)) */
201 return std::visit(
202 [&](auto &ptr) {
203 auto const &box_geo = *System::get_system().box_geo;
204 auto const lb_pos = box_geo.folded_position(pos) * m_conv.pos_to_lb;
205 return ptr->get_velocity_at_pos(lb_pos, false);
206 },
207 *impl->solver);
208}
209
210std::optional<double>
212 return std::visit(
213 [&](auto &ptr) {
214 auto const &box_geo = *System::get_system().box_geo;
215 auto const lb_pos = box_geo.folded_position(pos) * m_conv.pos_to_lb;
216 return ptr->get_density_at_pos(lb_pos, false);
217 },
218 *impl->solver);
219}
220
223 return std::visit(
224 [&](auto &ptr) {
225 auto const res = ptr->get_velocity_at_pos(pos * m_conv.pos_to_lb, true);
226 assert(res);
227 return *res * m_conv.vel_to_md;
228 },
229 *impl->solver);
230}
231
233 std::vector<Utils::Vector3d> const &pos) const {
234 return std::visit(
235 [&](auto &ptr) {
236 std::vector<Utils::Vector3d> pos_lb;
237 pos_lb.reserve(pos.size());
238 for (auto const &pos_md : pos) {
239 pos_lb.emplace_back(pos_md * m_conv.pos_to_lb);
240 }
241 auto res = ptr->get_velocities_at_pos(pos_lb);
242 for (auto &v : res) {
243 v *= m_conv.vel_to_md;
244 }
245 return res;
246 },
247 *impl->solver);
248}
249
250void Solver::add_forces_at_pos(std::vector<Utils::Vector3d> const &pos,
251 std::vector<Utils::Vector3d> const &forces) {
252 std::visit(
253 [&](auto &ptr) {
254 std::vector<Utils::Vector3d> pos_lb;
255 std::vector<Utils::Vector3d> force_lb;
256 pos_lb.reserve(pos.size());
257 force_lb.reserve(pos.size());
258 for (auto const &pos_md : pos) {
259 pos_lb.emplace_back(pos_md * m_conv.pos_to_lb);
260 }
261 for (auto const &force_md : forces) {
262 force_lb.emplace_back(force_md * m_conv.force_to_lb);
263 }
264 ptr->add_forces_at_pos(pos_lb, force_lb);
265 },
266 *impl->solver);
267}
268
270 Utils::Vector3d const &force_density) {
271 std::visit(
272 [&](auto &ptr) {
273 if (not ptr->add_force_at_pos(pos * m_conv.pos_to_lb,
274 force_density * m_conv.force_to_lb)) {
275 throw std::runtime_error("Cannot apply force to LB");
276 }
277 },
278 *impl->solver);
279}
280
282 check_solver(impl);
283 return std::visit([](auto const &ptr) { return ptr->get_momentum(); },
284 *impl->solver);
285}
286
287template <> void Solver::set<LBNone>(std::shared_ptr<LBNone> lb_instance) {
288 assert(impl);
289 assert(not impl->solver.has_value());
290 impl->solver = lb_instance;
291}
292
293#ifdef WALBERLA
294template <>
295void Solver::set<LBWalberla>(std::shared_ptr<LBWalberlaBase> lb_fluid,
296 std::shared_ptr<LBWalberlaParams> lb_params) {
297 assert(impl);
298 assert(not impl->solver.has_value());
299 auto const &system = get_system();
300 auto lb_instance = std::make_shared<LBWalberla>(lb_fluid, lb_params);
301 lb_instance->sanity_checks(system);
302 auto const &lebc = system.box_geo->lees_edwards_bc();
303 lb_fluid->check_lebc(lebc.shear_direction, lebc.shear_plane_normal);
304 impl->solver = lb_instance;
305 auto const agrid = lb_instance->get_agrid();
306 auto const tau = lb_instance->get_tau();
307 m_conv = Conversions{1. / agrid, agrid / tau, tau * tau / agrid};
308}
309#endif // WALBERLA
310
311} // namespace LB
LBWalberlaBase provides the public interface of the LB waLBerla bridge.
Vector implementation and trait types for boost qvm interoperability.
std::shared_ptr< BoxGeometry > box_geo
This file contains the defaults for ESPResSo.
static auto is_solver_set(std::unique_ptr< Solver::Implementation > const &ptr)
Definition lb/Solver.cpp:54
static void check_solver(std::unique_ptr< Solver::Implementation > const &ptr)
Definition lb/Solver.cpp:58
System & get_system()
void veto_boxl_change() const
void lebc_sanity_checks(unsigned int shear_direction, unsigned int shear_plane_normal) const
Perform LB LEbc parameter checks.
void add_force_density(Utils::Vector3d const &pos, Utils::Vector3d const &force_density)
Add a force density to the fluid at the given position.
Utils::Vector3d get_coupling_interpolated_velocity(Utils::Vector3d const &pos) const
Calculate the interpolated fluid velocity in MD units.
void sanity_checks() const
Perform LB parameter and boundary velocity checks.
Definition lb/Solver.cpp:91
void on_timestep_change()
void on_boxl_change()
void ghost_communication()
Perform a full ghost communication.
Definition lb/Solver.cpp:76
void propagate()
Propagate the LB fluid.
Definition lb/Solver.cpp:71
void reset()
Remove the LB solver.
Definition lb/Solver.cpp:66
std::optional< Utils::Vector3d > get_interpolated_velocity(Utils::Vector3d const &pos) const
Calculate the interpolated fluid velocity in LB units.
bool is_solver_set() const
Return true if a LB solver is active.
Definition lb/Solver.cpp:64
void ghost_communication_pdf()
Perform a ghost communication of the PDF field.
Definition lb/Solver.cpp:81
double get_tau() const
Get the LB time step.
void on_temperature_change()
std::optional< double > get_interpolated_density(Utils::Vector3d const &pos) const
Calculate the interpolated fluid density in LB units.
void add_forces_at_pos(std::vector< Utils::Vector3d > const &pos, std::vector< Utils::Vector3d > const &forces)
void veto_kT(double kT) const
Check if a thermostat is compatible with the LB temperature.
double get_agrid() const
Get the LB grid spacing.
Utils::VectorXd< 9 > get_pressure_tensor() const
void update_collision_model()
void veto_time_step(double time_step) const
Check if a MD time step is compatible with the LB tau.
Definition lb/Solver.cpp:98
void ghost_communication_vel()
Perform a ghost communication of the velocity field.
Definition lb/Solver.cpp:86
void on_lees_edwards_change()
void on_node_grid_change()
Utils::Vector3d get_momentum() const
void on_cell_structure_change()
std::vector< Utils::Vector3d > get_coupling_interpolated_velocities(std::vector< Utils::Vector3d > const &pos) const
bool is_gpu() const
double get_kT() const
Get the thermal energy parameter of the LB fluid.