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 ESPRESSO_WALBERLA
35#endif
36
37#include <utils/Vector.hpp>
38
39#include <cassert>
40#include <cmath>
41#include <functional>
42#include <limits>
43#include <memory>
44#include <optional>
45#include <stdexcept>
46#include <string>
47#include <tuple>
48#include <variant>
49#include <vector>
50
51namespace LB {
52
53Solver::Solver() { impl = std::make_unique<Implementation>(); }
54
55static auto is_solver_set(std::unique_ptr<Solver::Implementation> const &ptr) {
56 return ptr != nullptr and ptr->solver.has_value();
57}
58
59static void check_solver(std::unique_ptr<Solver::Implementation> const &ptr) {
60 if (not is_solver_set(ptr)) {
61 throw NoLBActive{};
62 }
63}
64
65bool Solver::is_solver_set() const { return LB::is_solver_set(impl); }
66
68 if (impl->solver) {
69 std::visit([this](auto &ptr) { ptr->detach_system(m_system.lock()); },
70 *impl->solver);
71 impl->solver = std::nullopt;
72 }
73 m_conv = Conversions{};
74}
75
77 check_solver(impl);
78 std::visit([](auto &ptr) { ptr->propagate(); }, *impl->solver);
79}
80
82 check_solver(impl);
83 std::visit([](auto &ptr) { ptr->ghost_communication(); }, *impl->solver);
84}
85
87 check_solver(impl);
88 std::visit([](auto &ptr) { ptr->ghost_communication_pdf(); }, *impl->solver);
89}
90
92 check_solver(impl);
93 std::visit([](auto &ptr) { ptr->ghost_communication_vel(); }, *impl->solver);
94}
95
97 if (impl->solver) {
98 auto const &system = get_system();
99 std::visit([&](auto &ptr) { ptr->sanity_checks(system); }, *impl->solver);
100 }
101}
102
103void Solver::veto_time_step(double time_step) const {
104 if (impl->solver) {
105 std::visit([=](auto &ptr) { ptr->veto_time_step(time_step); },
106 *impl->solver);
107 }
108}
109
110void Solver::veto_kT(double kT) const {
111 if (impl->solver) {
112 std::visit([=](auto &ptr) { ptr->veto_kT(kT); }, *impl->solver);
113 }
114}
115
116void Solver::lebc_sanity_checks(unsigned int shear_direction,
117 unsigned int shear_plane_normal) const {
118 if (impl->solver) {
119 auto const callback = [=](auto &ptr) {
120 ptr->lebc_sanity_checks(shear_direction, shear_plane_normal);
121 };
122 std::visit(callback, *impl->solver);
123 }
124}
125
127 if (impl->solver) {
128 auto &solver = *impl->solver;
129 std::visit([](auto &ptr) { ptr->on_cell_structure_change(); }, solver);
130 }
131}
132
134 if (impl->solver) {
135 std::visit([](auto const &ptr) { ptr->veto_boxl_change(); }, *impl->solver);
136 }
137}
138
140 if (impl->solver) {
141 std::visit([](auto &ptr) { ptr->on_boxl_change(); }, *impl->solver);
142 }
143}
144
146 if (impl->solver) {
147 std::visit([](auto &ptr) { ptr->on_node_grid_change(); }, *impl->solver);
148 }
149}
150
152 if (impl->solver) {
153 std::visit([](auto &ptr) { ptr->on_timestep_change(); }, *impl->solver);
154 }
155}
156
158 if (impl->solver) {
159 std::visit([](auto &ptr) { ptr->on_temperature_change(); }, *impl->solver);
160 }
161}
162
164 if (impl->solver) {
165 std::visit([](auto &ptr) { ptr->on_lees_edwards_change(); }, *impl->solver);
166 }
167}
168
170 if (impl->solver) {
171 std::visit([](auto &ptr) { ptr->update_collision_model(); }, *impl->solver);
172 }
173}
174
175bool Solver::is_gpu() const {
176 check_solver(impl);
177 return std::visit([](auto &ptr) { return ptr->is_gpu(); }, *impl->solver);
178}
179
180double Solver::get_agrid() const {
181 check_solver(impl);
182 return std::visit([](auto &ptr) { return ptr->get_agrid(); }, *impl->solver);
183}
184
185double Solver::get_tau() const {
186 check_solver(impl);
187 return std::visit([](auto &ptr) { return ptr->get_tau(); }, *impl->solver);
188}
189
190double Solver::get_kT() const {
191 check_solver(impl);
192 return std::visit([](auto &ptr) { return ptr->get_kT(); }, *impl->solver);
193}
194
196 check_solver(impl);
197 return std::visit([](auto &ptr) { return ptr->get_pressure_tensor(); },
198 *impl->solver);
199}
200
201std::function<bool(Utils::Vector3d const &)>
202Solver::make_lattice_position_checker(bool consider_points_in_halo) const {
203 return std::visit(
204 [&](auto &ptr) -> std::function<bool(Utils::Vector3d const &)> {
205 auto const &box_geo = *System::get_system().box_geo;
206 return [&, kernel = ptr->make_lattice_position_checker(
207 consider_points_in_halo)](Utils::Vector3d const &pos) {
208 return kernel(box_geo.folded_position(pos) * m_conv.pos_to_lb);
209 };
210 },
211 *impl->solver);
212}
213
214std::optional<Utils::Vector3d>
216 /* calculate fluid velocity at particle's position
217 this is done by linear interpolation
218 (Eq. (11) Ahlrichs and Duenweg, JCP 111(17):8225 (1999)) */
219 return std::visit(
220 [&](auto &ptr) {
221 auto const &box_geo = *System::get_system().box_geo;
222 auto const lb_pos = box_geo.folded_position(pos) * m_conv.pos_to_lb;
223 return ptr->get_velocity_at_pos(lb_pos, false);
224 },
225 *impl->solver);
226}
227
228std::optional<double>
230 return std::visit(
231 [&](auto &ptr) {
232 auto const &box_geo = *System::get_system().box_geo;
233 auto const lb_pos = box_geo.folded_position(pos) * m_conv.pos_to_lb;
234 return ptr->get_density_at_pos(lb_pos, false);
235 },
236 *impl->solver);
237}
238
240 std::vector<Utils::Vector3d> const &pos) const {
241 return std::visit(
242 [&](auto &ptr) {
243 auto const &box_geo = *System::get_system().box_geo;
244 std::vector<Utils::Vector3d> pos_lb;
245 pos_lb.reserve(pos.size());
246 for (auto const &pos_md : pos) {
247 pos_lb.emplace_back(box_geo.folded_position(pos_md) *
248 m_conv.pos_to_lb);
249 }
250 return ptr->get_densities_at_pos(pos_lb);
251 },
252 *impl->solver);
253}
254
257 return std::visit(
258 [&](auto &ptr) {
259 auto const res = ptr->get_velocity_at_pos(pos * m_conv.pos_to_lb, true);
260 assert(res);
261 return *res * m_conv.vel_to_md;
262 },
263 *impl->solver);
264}
265
267 std::vector<Utils::Vector3d> const &pos) const {
268 return std::visit(
269 [&](auto &ptr) {
270 std::vector<Utils::Vector3d> pos_lb;
271 pos_lb.reserve(pos.size());
272 for (auto const &pos_md : pos) {
273 pos_lb.emplace_back(pos_md * m_conv.pos_to_lb);
274 }
275 auto res = ptr->get_velocities_at_pos(pos_lb);
276 for (auto &v : res) {
277 v *= m_conv.vel_to_md;
278 }
279 return res;
280 },
281 *impl->solver);
282}
283
284void Solver::add_forces_at_pos(std::vector<Utils::Vector3d> const &pos,
285 std::vector<Utils::Vector3d> const &forces) {
286 std::visit(
287 [&](auto &ptr) {
288 std::vector<Utils::Vector3d> pos_lb;
289 std::vector<Utils::Vector3d> force_lb;
290 pos_lb.reserve(pos.size());
291 force_lb.reserve(pos.size());
292 for (auto const &pos_md : pos) {
293 pos_lb.emplace_back(pos_md * m_conv.pos_to_lb);
294 }
295 for (auto const &force_md : forces) {
296 force_lb.emplace_back(force_md * m_conv.force_to_lb);
297 }
298 ptr->add_forces_at_pos(pos_lb, force_lb);
299 },
300 *impl->solver);
301}
302
304 Utils::Vector3d const &force_density) {
305 std::visit(
306 [&](auto &ptr) {
307 if (not ptr->add_force_at_pos(pos * m_conv.pos_to_lb,
308 force_density * m_conv.force_to_lb)) {
309 throw std::runtime_error("Cannot apply force to LB");
310 }
311 },
312 *impl->solver);
313}
314
316 check_solver(impl);
317 return std::visit([](auto const &ptr) { return ptr->get_momentum(); },
318 *impl->solver);
319}
320
321template <> void Solver::set<LBNone>(std::shared_ptr<LBNone> lb_instance) {
322 assert(impl);
323 assert(not impl->solver.has_value());
324 lb_instance->bind_system(m_system.lock());
325 impl->solver = lb_instance;
326}
327
328#ifdef ESPRESSO_WALBERLA
329template <>
330void Solver::set<LBWalberla>(std::shared_ptr<LBWalberlaBase> lb_fluid,
331 std::shared_ptr<LBWalberlaParams> lb_params) {
332 assert(impl);
333 assert(not impl->solver.has_value());
334 auto const &system = get_system();
335 auto lb_instance = std::make_shared<LBWalberla>(lb_fluid, lb_params);
336 lb_instance->bind_system(m_system.lock());
337 lb_instance->sanity_checks(system);
338 auto const &lebc = system.box_geo->lees_edwards_bc();
339 lb_fluid->check_lebc(lebc.shear_direction, lebc.shear_plane_normal);
340 impl->solver = lb_instance;
341 auto const agrid = lb_instance->get_agrid();
342 auto const tau = lb_instance->get_tau();
343 m_conv = Conversions{1. / agrid, agrid / tau, tau * tau / agrid};
344}
345#endif // ESPRESSO_WALBERLA
346
347} // namespace LB
LBWalberlaBase provides the public interface of the LB waLBerla bridge.
Vector implementation and trait types for boost qvm interoperability.
std::weak_ptr< System > m_system
std::shared_ptr< BoxGeometry > box_geo
static auto is_solver_set(std::unique_ptr< Solver::Implementation > const &ptr)
Definition lb/Solver.cpp:55
static void check_solver(std::unique_ptr< Solver::Implementation > const &ptr)
Definition lb/Solver.cpp:59
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.
std::vector< double > get_interpolated_densities(std::vector< Utils::Vector3d > const &pos) const
Calculate the interpolated fluid densities in LB units.
void add_force_density(Utils::Vector3d const &pos, Utils::Vector3d const &force_density)
Add a force density to the fluid at the given position.
std::function< bool(Utils::Vector3d const &)> make_lattice_position_checker(bool consider_points_in_halo) const
Make a functor to check if a position is in the local domain.
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:96
void on_timestep_change()
void on_boxl_change()
void ghost_communication()
Perform a full ghost communication.
Definition lb/Solver.cpp:81
void propagate()
Propagate the LB fluid.
Definition lb/Solver.cpp:76
void reset()
Remove the LB solver.
Definition lb/Solver.cpp:67
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:65
void ghost_communication_pdf()
Perform a ghost communication of the PDF field.
Definition lb/Solver.cpp:86
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.
void ghost_communication_vel()
Perform a ghost communication of the velocity field.
Definition lb/Solver.cpp:91
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
Calculate the interpolated fluid velocities in MD units.
bool is_gpu() const
double get_kT() const
Get the thermal energy parameter of the LB fluid.