21#ifdef ESPRESSO_WALBERLA
34#include <boost/mpi.hpp>
35#include <boost/mpi/collectives/all_reduce.hpp>
36#include <boost/mpi/collectives/broadcast.hpp>
51std::unordered_map<std::string, int>
const EKVTKHandle::obs_map = {
59 if (method ==
"update_flux_boundary_from_shape") {
62 std::ranges::for_each(values, [
this](
double &v) { v *=
m_conv_flux; });
65 get_value<std::vector<int>>(parameters,
"raster"), values);
68 if (method ==
"update_density_boundary_from_shape") {
70 std::ranges::for_each(values, [
this](
double &v) { v *=
m_conv_density; });
71 m_instance->update_density_boundary_from_shape(
72 get_value<std::vector<int>>(parameters,
"raster"), values);
75 if (method ==
"clear_flux_boundaries") {
79 if (method ==
"clear_density_boundaries") {
83 if (method ==
"save_checkpoint") {
86 save_checkpoint(path, mode);
89 if (method ==
"load_checkpoint") {
92 load_checkpoint(path, mode);
110 std::vector<std::string> required_features;
111 required_features.emplace_back(
"CUDA");
118 m_lattice->lattice(), ek_diffusion, ek_kT,
139 not params.contains(
"seed")) {
140 throw std::invalid_argument(
141 "Parameter 'seed' is required for thermalized EKSpecies");
144 throw std::domain_error(
"Parameter 'seed' must be >= 0");
147 throw std::domain_error(
"Parameter 'tau' must be > 0");
150 throw std::domain_error(
"Parameter 'kT' must be >= 0");
153 throw std::domain_error(
"Parameter 'density' must be >= 0");
169void EKSpecies::load_checkpoint(std::filesystem::path
const &path,
int mode) {
173 auto const expected_grid_size = ek_obj.get_lattice().get_grid_dimensions();
175 cpfile.read(read_grid_size);
176 if (read_grid_size != expected_grid_size) {
177 std::stringstream message;
178 message <<
"grid dimensions mismatch, read [" << read_grid_size <<
"], "
179 <<
"expected [" << expected_grid_size <<
"].";
180 throw std::runtime_error(message.str());
184 auto const read_data = [&ek_obj](CheckpointFile &cpfile) {
185 auto const grid_size = ek_obj.get_lattice().get_grid_dimensions();
186 auto const i_max = grid_size[0];
187 auto const j_max = grid_size[1];
188 auto const k_max = grid_size[2];
190 for (
int i = 0; i < i_max; i++) {
191 for (
int j = 0; j < j_max; j++) {
192 for (
int k = 0; k < k_max; k++) {
203 ek_obj.set_node_density(ind, cpnode.
density);
215 auto const on_success = [&ek_obj]() { ek_obj.ghost_communication(); };
221void EKSpecies::save_checkpoint(std::filesystem::path
const &path,
int mode) {
224 auto const write_metadata = [&ek_obj,
225 mode](std::shared_ptr<CheckpointFile> cpfile_ptr,
227 auto const grid_size = ek_obj.get_lattice().get_grid_dimensions();
229 cpfile_ptr->write(grid_size);
234 auto const on_failure = [](std::shared_ptr<CheckpointFile>
const &,
238 boost::mpi::broadcast(
context.get_comm(), failure, 0);
242 auto const write_data = [&ek_obj,
243 mode](std::shared_ptr<CheckpointFile> cpfile_ptr,
245 auto const get_node_checkpoint =
246 [&](
Utils::Vector3i const &ind) -> std::optional<EKWalberlaNodeState> {
247 auto const density = ek_obj.get_node_density(ind);
248 auto const is_b_d = ek_obj.get_node_is_density_boundary(ind);
249 auto const dens_b = ek_obj.get_node_density_at_boundary(ind);
250 auto const is_b_f = ek_obj.get_node_is_flux_boundary(ind);
251 auto const flux_b = ek_obj.get_node_flux_at_boundary(ind);
252 if (
density and is_b_d and is_b_f and
253 ((*is_b_d) ? dens_b.has_value() :
true) and
254 ((*is_b_f) ? flux_b.has_value() :
true)) {
255 EKWalberlaNodeState cpnode;
270 auto failure =
false;
271 auto const &comm =
context.get_comm();
272 auto const is_head_node =
context.is_head_node();
273 auto const unit_test_mode = (mode !=
static_cast<int>(
CptMode::ascii)) and
275 auto const grid_size = ek_obj.get_lattice().get_grid_dimensions();
276 auto const i_max = grid_size[0];
277 auto const j_max = grid_size[1];
278 auto const k_max = grid_size[2];
279 EKWalberlaNodeState cpnode;
280 for (
int i = 0; i < i_max; i++) {
281 for (
int j = 0; j < j_max; j++) {
282 for (
int k = 0; k < k_max; k++) {
284 auto const result = get_node_checkpoint(ind);
285 if (!unit_test_mode) {
286 assert(1 == boost::mpi::all_reduce(comm,
static_cast<int>(!!result),
288 "Incorrect number of return values");
294 comm.recv(boost::mpi::any_source, 42, cpnode);
296 auto &cpfile = *cpfile_ptr;
306 boost::mpi::broadcast(comm, failure, 0);
309 comm.send(0, 42, *result);
311 boost::mpi::broadcast(comm, failure, 0);
322 write_data, on_failure);
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Context * context() const
Responsible context.
Handle for a checkpoint file.
void flux_boundary_ghost_layer_size_sanity_check() const
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
std::optional< ResourceObserver > m_mpi_cart_comm_observer
void make_instance(VariantMap const ¶ms) override
Variant do_call_method(std::string const &method, VariantMap const ¶meters) override
void do_construct(VariantMap const ¶ms) override
std::shared_ptr< ::EKinWalberlaBase > m_instance
Variant do_call_method(std::string const &method_name, VariantMap const ¶ms) override
std::vector< std::shared_ptr< EKVTKHandle > > m_vtk_writers
std::shared_ptr< LatticeWalberla > m_lattice
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
void check_features(std::vector< std::string > const &features)
Throw an exception when a feature is missing or not recognized.
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 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)
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
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.
std::unordered_map< std::string, Variant > VariantMap
DEVICE_QUALIFIER constexpr T int_pow(T x)
Calculate integer powers.
std::shared_ptr< EKinWalberlaBase > new_ek_walberla_cpu(std::shared_ptr< LatticeWalberla > const &lattice, double diffusion, double kT, double valency, Utils::Vector3d ext_efield, double density, bool advection, bool friction_coupling, bool single_precision, bool thermalized, unsigned int seed)
std::shared_ptr< EKinWalberlaBase > new_ek_walberla_gpu(std::shared_ptr< LatticeWalberla > const &lattice, double diffusion, double kT, double valency, Utils::Vector3d ext_efield, double density, bool advection, bool friction_coupling, bool single_precision, bool thermalized, unsigned int seed)
ResourceObserver get_mpi_cart_comm_observer()
Get an observer on waLBerla's MPI Cartesian communicator status.
Checkpoint data for a EK node.
Utils::Vector3d flux_boundary