43#include <boost/mpi.hpp>
44#include <boost/mpi/collectives/all_reduce.hpp>
45#include <boost/mpi/collectives/broadcast.hpp>
60std::unordered_map<std::string, int>
const LBVTKHandle::obs_map = {
68 if (
name ==
"activate") {
75 if (
name ==
"deactivate") {
82 if (
name ==
"add_force_at_pos") {
84 auto const pos = get_value<Utils::Vector3d>(
params,
"pos");
85 auto const f = get_value<Utils::Vector3d>(
params,
"force");
86 auto const folded_pos = box_geo.folded_position(pos);
90 if (
name ==
"get_interpolated_velocity") {
91 auto const pos = get_value<Utils::Vector3d>(
params,
"pos");
92 return get_interpolated_velocity(pos);
94 if (
name ==
"get_pressure_tensor") {
95 return get_average_pressure_tensor();
97 if (
name ==
"load_checkpoint") {
98 auto const path = get_value<std::string>(
params,
"path");
99 auto const mode = get_value<int>(
params,
"mode");
100 load_checkpoint(path, mode);
103 if (
name ==
"save_checkpoint") {
104 auto const path = get_value<std::string>(
params,
"path");
105 auto const mode = get_value<int>(
params,
"mode");
106 save_checkpoint(path, mode);
109 if (
name ==
"clear_boundaries") {
114 if (
name ==
"add_boundary_from_shape") {
120 if (
name ==
"get_lattice_speed") {
128 auto const visc = get_value<double>(
params,
"kinematic_viscosity");
129 auto const dens = get_value<double>(
params,
"density");
130 auto const precision = get_value<bool>(
params,
"single_precision");
131 auto const lb_lattice =
m_lattice->lattice();
139 auto const visc = get_value<double>(
params,
"kinematic_viscosity");
140 auto const dens = get_value<double>(
params,
"density");
141 auto const precision = get_value<bool>(
params,
"single_precision");
142 auto const blocks_per_mpi_rank = get_value<Utils::Vector3i>(
143 m_lattice->get_parameter(
"blocks_per_mpi_rank"));
145 throw std::runtime_error(
146 "Using more than one block per MPI rank is not supported for GPU LB");
148 auto const lb_lattice =
m_lattice->lattice();
156 m_lattice = get_value<std::shared_ptr<LatticeWalberla>>(
params,
"lattice");
158 get_value_or<decltype(m_vtk_writers)>(
params,
"vtk_writers", {});
159 auto const tau = get_value<double>(
params,
"tau");
160 auto const agrid = get_value<double>(
m_lattice->get_parameter(
"agrid"));
161 auto const visc = get_value<double>(
params,
"kinematic_viscosity");
162 auto const dens = get_value<double>(
params,
"density");
163 auto const kT = get_value<double>(
params,
"kT");
164 auto const ext_f = get_value<Utils::Vector3d>(
params,
"ext_force_density");
165 m_lb_params = std::make_shared<::LB::LBWalberlaParams>(agrid, tau);
167 auto const seed = get_value<int>(
params,
"seed");
170 throw std::domain_error(
"Parameter 'tau' must be > 0");
173 m_conv_visc = Utils::int_pow<1>(tau) / Utils::int_pow<2>(agrid);
174 m_conv_energy = Utils::int_pow<2>(tau) / Utils::int_pow<2>(agrid);
176 m_conv_speed = Utils::int_pow<1>(tau) / Utils::int_pow<1>(agrid);
177 m_conv_press = Utils::int_pow<2>(tau) * Utils::int_pow<1>(agrid);
178 m_conv_force = Utils::int_pow<2>(tau) / Utils::int_pow<1>(agrid);
185 throw std::domain_error(
"Parameter 'seed' must be >= 0");
188 throw std::domain_error(
"Parameter 'kT' must be >= 0");
191 throw std::domain_error(
"Parameter 'density' must be > 0");
194 throw std::domain_error(
"Parameter 'kinematic_viscosity' must be >= 0");
198 static_cast<unsigned int>(seed));
207std::vector<Variant> LBFluid::get_average_pressure_tensor()
const {
211 std::copy(tensor_flat.begin(), tensor_flat.end(), tensor.m_data.begin());
212 return std::vector<Variant>{tensor.row<0>().as_vector(),
213 tensor.row<1>().as_vector(),
214 tensor.row<2>().as_vector()};
219 auto const lb_pos = box_geo.folded_position(pos) *
m_conv_dist;
220 auto const result =
m_instance->get_velocity_at_pos(lb_pos);
225void LBFluid::load_checkpoint(std::string
const &filename,
int mode) {
228 auto const read_metadata = [&lb_obj](CheckpointFile &cpfile) {
229 auto const expected_grid_size = lb_obj.get_lattice().get_grid_dimensions();
230 auto const expected_pop_size = lb_obj.stencil_size();
232 std::size_t read_pop_size;
233 cpfile.read(read_grid_size);
234 cpfile.read(read_pop_size);
235 if (read_grid_size != expected_grid_size) {
236 std::stringstream message;
237 message <<
"grid dimensions mismatch, read [" << read_grid_size <<
"], "
238 <<
"expected [" << expected_grid_size <<
"].";
239 throw std::runtime_error(message.str());
241 if (read_pop_size != expected_pop_size) {
242 throw std::runtime_error(
"population size mismatch, read " +
243 std::to_string(read_pop_size) +
", expected " +
244 std::to_string(expected_pop_size) +
".");
248 auto const read_data = [&lb_obj](CheckpointFile &cpfile) {
249 auto const grid_size = lb_obj.get_lattice().get_grid_dimensions();
255 for (
int i = 0; i < i_max; i++) {
256 for (
int j = 0; j < j_max; j++) {
257 for (
int k = 0; k < k_max; k++) {
265 lb_obj.set_node_population(ind, cpnode.
populations);
268 lb_obj.set_node_velocity_at_boundary(ind, cpnode.
slip_velocity);
275 auto const on_success = [&lb_obj]() {
276 lb_obj.ghost_communication();
277 lb_obj.reallocate_ubb_field();
281 read_data, on_success);
284void LBFluid::save_checkpoint(std::string
const &filename,
int mode) {
287 auto const write_metadata = [&lb_obj,
288 mode](std::shared_ptr<CheckpointFile> cpfile_ptr,
290 auto const grid_size = lb_obj.get_lattice().get_grid_dimensions();
291 auto const pop_size = lb_obj.stencil_size();
293 cpfile_ptr->write(grid_size);
294 cpfile_ptr->write(pop_size);
299 auto const on_failure = [](std::shared_ptr<CheckpointFile>
const &,
307 auto const write_data = [&lb_obj,
308 mode](std::shared_ptr<CheckpointFile> cpfile_ptr,
310 auto const get_node_checkpoint =
311 [&](
Utils::Vector3i const &ind) -> std::optional<LBWalberlaNodeState> {
312 auto const pop = lb_obj.get_node_population(ind);
313 auto const laf = lb_obj.get_node_last_applied_force(ind);
314 auto const lbb = lb_obj.get_node_is_boundary(ind);
315 auto const vbb = lb_obj.get_node_velocity_at_boundary(ind);
316 if (pop and laf and lbb and ((*lbb) ? vbb.has_value() :
true)) {
329 auto failure =
false;
332 auto const unit_test_mode = (mode !=
static_cast<int>(
CptMode::ascii)) and
334 auto const grid_size = lb_obj.get_lattice().get_grid_dimensions();
339 for (
int i = 0; i < i_max; i++) {
340 for (
int j = 0; j < j_max; j++) {
341 for (
int k = 0; k < k_max; k++) {
343 auto const result = get_node_checkpoint(ind);
344 if (!unit_test_mode) {
345 assert(1 == boost::mpi::all_reduce(comm,
static_cast<int>(!!result),
347 "Incorrect number of return values");
353 comm.recv(boost::mpi::any_source, 42, cpnode);
355 auto &cpfile = *cpfile_ptr;
362 boost::mpi::broadcast(comm, failure, 0);
365 comm.send(0, 42, *result);
367 boost::mpi::broadcast(comm, failure, 0);
378 write_data, on_failure);
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
boost::string_ref name() const
Context * context() const
Responsible context.
void make_instance(VariantMap const ¶ms) override
void make_instance(VariantMap const ¶ms) override
void do_construct(VariantMap const ¶ms) override
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
std::shared_ptr<::LB::LBWalberlaParams > m_lb_params
::LatticeModel::units_map get_latice_to_md_units_conversion() const override
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
virtual void make_instance(VariantMap const ¶ms)=0
void on_lb_boundary_conditions_change()
Called when the LB boundary conditions change (geometry, slip velocity, or both).
std::shared_ptr< BoxGeometry > box_geo
This file contains the defaults for ESPResSo.
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 save_checkpoint_common(Context const &context, std::string const classname, std::string const &filename, int mode, F1 const write_metadata, F2 const write_data, F3 const on_failure)
void unit_test_handle(int mode)
Inject code for unit tests.
void load_checkpoint_common(Context const &context, std::string const classname, std::string const &filename, 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 mpi_reduce_sum(boost::mpi::communicator const &comm, T const &result)
Reduce object by sum on the head node.
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type 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.
static FUNC_PREFIX double *RESTRICT int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const double grid_size
static SteepestDescentParameters params
Currently active steepest descent instance.
Checkpoint data for a LB node.
Utils::Vector3d last_applied_force
std::vector< double > populations
Utils::Vector3d slip_velocity
void update_collision_model()
void reset()
Remove the LB solver.
void set(Args... args)
Set the LB solver.
Matrix representation with static size.