21#ifdef ESPRESSO_WALBERLA
46#include <boost/mpi.hpp>
47#include <boost/mpi/collectives/all_reduce.hpp>
48#include <boost/mpi/collectives/broadcast.hpp>
60#include <unordered_map>
65std::unordered_map<std::string, int>
const LBVTKHandle::obs_map = {
73 if (
name ==
"activate") {
74 auto &
system = get_system();
82 if (
name ==
"deactivate") {
87 if (
not name.starts_with(
"get_")) {
91 if (
name ==
"add_force_at_pos") {
92 auto const &box_geo = *get_system().
box_geo;
95 auto const folded_pos = box_geo.folded_position(pos);
99 if (
name ==
"get_interpolated_velocity") {
101 return get_interpolated_velocity(pos);
103 if (
name ==
"get_boundary_force_from_shape") {
104 return get_boundary_force_from_shape(
105 get_value<std::vector<int>>(params,
"raster"));
107 if (
name ==
"get_boundary_force") {
108 return get_boundary_force();
110 if (
name ==
"get_pressure_tensor") {
111 return get_average_pressure_tensor();
113 if (
name ==
"load_checkpoint") {
116 load_checkpoint(path,
mode);
119 if (
name ==
"save_checkpoint") {
122 save_checkpoint(path,
mode);
125 if (
name ==
"clear_boundaries") {
130 if (
name ==
"add_boundary_from_shape") {
132 get_value<std::vector<int>>(params,
"raster"),
133 get_value<std::vector<double>>(params,
"values"));
136 if (
name ==
"get_lattice_speed") {
158 m_lattice->get_parameter(
"blocks_per_mpi_rank"));
160 throw std::runtime_error(
161 "Using more than one block per MPI rank is not supported for GPU LB");
184 throw std::domain_error(
"Parameter 'tau' must be > 0");
199 throw std::domain_error(
"Parameter 'seed' must be >= 0");
202 throw std::domain_error(
"Parameter 'kT' must be >= 0");
205 throw std::domain_error(
"Parameter 'density' must be > 0");
208 throw std::domain_error(
"Parameter 'kinematic_viscosity' must be >= 0");
222LBFluid::get_boundary_force_from_shape(std::vector<int>
const &raster)
const {
228Variant LBFluid::get_boundary_force()
const {
233std::vector<Variant> LBFluid::get_average_pressure_tensor()
const {
238 return std::vector<Variant>{
tensor.row<0>().as_vector(),
239 tensor.row<1>().as_vector(),
240 tensor.row<2>().as_vector()};
251void LBFluid::load_checkpoint(std::filesystem::path
const &path,
int mode) {
262 std::stringstream message;
263 message <<
"grid dimensions mismatch, read [" <<
read_grid_size <<
"], "
265 throw std::runtime_error(message.str());
268 throw std::runtime_error(
"population size mismatch, read " +
281 for (
int i = 0; i <
i_max; i++) {
283 for (
int k = 0; k <
k_max; k++) {
292 lb_obj.set_node_last_applied_force(ind,
cpnode.last_applied_force);
294 lb_obj.set_node_velocity_at_boundary(ind,
cpnode.slip_velocity);
302 lb_obj.ghost_communication();
303 lb_obj.reallocate_ubb_field();
310void LBFluid::save_checkpoint(std::filesystem::path
const &path,
int mode) {
325 auto const on_failure = [](std::shared_ptr<CheckpointFile>
const &,
337 [&](
Utils::Vector3i const &ind) -> std::optional<LBWalberlaNodeState> {
338 auto const pop =
lb_obj.get_node_population(ind);
339 auto const laf =
lb_obj.get_node_last_applied_force(ind);
340 auto const lbb =
lb_obj.get_node_is_boundary(ind);
341 auto const vbb =
lb_obj.get_node_velocity_at_boundary(ind);
355 auto failure =
false;
365 for (
int i = 0; i <
i_max; i++) {
367 for (
int k = 0; k <
k_max; k++) {
371 assert(1 == boost::mpi::all_reduce(comm,
static_cast<int>(!!result),
373 "Incorrect number of return values");
379 comm.recv(boost::mpi::any_source, 42,
cpnode);
388 boost::mpi::broadcast(comm, failure, 0);
391 comm.send(0, 42, *result);
393 boost::mpi::broadcast(comm, failure, 0);
Vector implementation and trait types for boost qvm interoperability.
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual bool is_head_node() const =0
virtual boost::mpi::communicator const & get_comm() const =0
Context * context() const
Responsible context.
std::string_view name() const
std::optional< ResourceObserver > m_mpi_cart_comm_observer
void do_construct(VariantMap const ¶ms) override
void make_instance(VariantMap const ¶ms) override
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
std::shared_ptr<::LB::LBWalberlaParams > m_lb_params
std::shared_ptr< ::LBWalberlaBase > m_instance
Variant do_call_method(std::string const &method_name, VariantMap const ¶ms) override
std::vector< std::shared_ptr< LBVTKHandle > > m_vtk_writers
std::shared_ptr< LatticeWalberla > m_lattice
void on_lb_boundary_conditions_change()
Called when the LB boundary conditions change (geometry, slip velocity, or both).
std::shared_ptr< BoxGeometry > box_geo
std::shared_ptr< LBWalberlaBase > new_lb_walberla_cpu(std::shared_ptr< LatticeWalberla > const &lattice, double viscosity, double density, bool single_precision)
std::shared_ptr< LBWalberlaBase > new_lb_walberla_gpu(std::shared_ptr< LatticeWalberla > const &lattice, double viscosity, double density, bool single_precision)
Matrix implementation and trait types for boost qvm interoperability.
void check_features(std::vector< std::string > const &features)
void save_checkpoint_common(Context const &context, std::string const classname, std::filesystem::path const &path, int mode, F1 const write_metadata, F2 const write_data, F3 const on_failure)
void lb_throw_if_expired(std::optional< ResourceObserver > const &mpi_obs)
void unit_test_handle(int mode)
Inject code for unit tests.
void load_checkpoint_common(Context const &context, std::string const classname, std::filesystem::path const &path, int mode, F1 const read_metadata, F2 const read_data, F3 const on_success)
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
T get_value_or(VariantMap const &vals, std::string const &name, T const &default_)
Get a value from a VariantMap by name, or return a default value if it does not exist.
T mpi_reduce_sum(boost::mpi::communicator const &comm, T const &result)
Reduce object by sum on the head node.
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
ResourceObserver get_mpi_cart_comm_observer()
Get an observer on waLBerla's MPI Cartesian communicator status.
Checkpoint data for a LB node.
std::vector< double > populations
void update_collision_model()
void reset()
Remove the LB solver.
Recursive variant implementation.
Matrix representation with static size.