51#include <nlohmann/json.hpp>
57namespace shammodels::gsph {
65 template<
class Tvec,
template<
class>
class SPHKernel>
82 u32 max_neigh_count = 500;
85 using mode = std::variant<DensityBased, DensityBasedNeighLim>;
89 void set_density_based() { config = DensityBased{}; }
90 void set_density_based_neigh_lim(
u32 max_neigh_count) {
91 config = DensityBasedNeighLim{max_neigh_count};
94 bool is_density_based_neigh_lim()
const {
95 return std::holds_alternative<DensityBasedNeighLim>(config);
101template<
class Tvec,
template<
class>
class SPHKernel>
104 using Tscal = shambase::VecComponent<Tvec>;
105 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
106 using Kernel = SPHKernel<Tscal>;
107 using u_morton =
u32;
111 static constexpr Tscal Rkern = Kernel::Rkern;
123 std::optional<shamunits::UnitSystem<Tscal>> unit_sys = {};
127 inline Tscal get_constant_G()
const {
132 return unit_sys->template get<shamunits::constants::G>();
144 using RiemannConfig = RiemannConfig<Tvec>;
145 RiemannConfig riemann_config;
147 inline void set_riemann_iterative(Tscal tol = Tscal{1e-6},
u32 max_iter = 20) {
148 riemann_config.set_iterative(tol, max_iter);
151 inline void set_riemann_hllc() { riemann_config.set_hllc(); }
153 inline void set_riemann_exact(Tscal tol = Tscal{1e-8},
u32 max_iter = 100) {
154 riemann_config.set_exact(tol, max_iter);
165 using ReconstructConfig = ReconstructConfig<Tvec>;
166 ReconstructConfig reconstruct_config;
168 inline void set_reconstruct_piecewise_constant() {
169 reconstruct_config.set_piecewise_constant();
172 inline void set_reconstruct_muscl(
174 reconstruct_config.set_muscl(limiter);
177 inline bool requires_gradients()
const {
return reconstruct_config.requires_gradients(); }
187 using ForceFormulationConfig = ForceFormulationConfig<Tvec>;
188 ForceFormulationConfig force_formulation_config;
190 inline void set_force_cha_whitworth() { force_formulation_config.set_cha_whitworth(); }
192 inline void set_force_inutsuka_v2() { force_formulation_config.set_inutsuka_v2(); }
194 inline bool is_force_inutsuka_v2()
const {
return force_formulation_config.is_inutsuka_v2(); }
204 using EOSConfig = shammodels::EOSConfig<Tvec>;
205 EOSConfig eos_config;
207 inline bool is_eos_adiabatic()
const {
209 return bool(std::get_if<T>(&eos_config.config));
212 inline bool is_eos_isothermal()
const {
214 return bool(std::get_if<T>(&eos_config.config));
225 if (
const auto *eos = std::get_if<Adiabatic>(&eos_config.config)) {
227 }
else if (
const auto *eos = std::get_if<Polytropic>(&eos_config.config)) {
233 inline void set_eos_adiabatic(Tscal gamma) { eos_config.set_adiabatic(gamma); }
235 inline void set_eos_isothermal(Tscal cs) { eos_config.set_isothermal(cs); }
246 BCConfig boundary_config;
248 inline void set_boundary_free() { boundary_config.
set_free(); }
249 inline void set_boundary_periodic() { boundary_config.set_periodic(); }
262 boundary_config.set_shearing_periodic(shear_base, shear_dir, speed);
276 inline void add_ext_force_point_mass(
277 Tscal central_mass, Tscal Racc, Tvec central_pos = Tvec{}) {
278 ext_force_config.add_point_mass(central_mass, Racc, central_pos);
289 u32 tree_reduction_level = 3;
296 inline void set_tree_reduction_level(
u32 level) { tree_reduction_level = level; }
309 "GSPH::SolverConfig",
310 "set_two_stage_search() is deprecated,\n"
311 " -> use set_neigh_cache_strategy(NeighCacheStrategy.TwoStage) or\n"
312 " set_neigh_cache_strategy(NeighCacheStrategy.SingleStage) instead"););
336 inline void set_smoothing_length_density_based() {
337 smoothing_length_config.set_density_based();
339 inline void set_smoothing_length_density_based_neigh_lim(
u32 max_neigh_count) {
340 smoothing_length_config.set_density_based_neigh_lim(max_neigh_count);
343 bool enable_particle_reordering =
false;
344 inline void set_enable_particle_reordering(
bool enable) { enable_particle_reordering = enable; }
345 u64 particle_reordering_step_freq = 1000;
346 inline void set_particle_reordering_step_freq(
u64 freq) {
349 "particle_reordering_step_freq cannot be zero");
351 particle_reordering_step_freq = freq;
354 bool save_dt_to_fields =
false;
355 inline void set_save_dt_to_fields(
bool enable) { save_dt_to_fields = enable; }
356 inline bool should_save_dt_to_fields()
const {
return save_dt_to_fields; }
358 bool show_ghost_zone_graph =
false;
359 inline void set_show_ghost_zone_graph(
bool enable) { show_ghost_zone_graph = enable; }
365 inline bool has_field_uint()
const {
return is_eos_adiabatic(); }
367 inline void print_status() {
371 logger::raw_ln(
"----- GSPH Solver configuration -----");
373 riemann_config.print_status();
374 reconstruct_config.print_status();
375 force_formulation_config.print_status();
376 eos_config.print_status();
377 logger::raw_ln(
"--------------------------------------");
380 inline void check_config()
const {
389 if (force_formulation_config.is_inutsuka_v2() && riemann_config.is_hllc()) {
391 "InutsukaV2 force formulation is not yet supported with the HLLC Riemann "
392 "solver. Use set_riemann_iterative() or set_riemann_exact() instead.");
396 inline void check_config_runtime()
const {
400 "gpart_mass must be positive. Call set_particle_mass() before evolving.");
405 void set_layout(shamrock::patch::PatchDataLayerLayout &pdl);
406 void set_ghost_layout(shamrock::patch::PatchDataLayerLayout &ghost_layout);
409namespace shammodels::gsph {
411 template<
class Tscal>
414 {
"cfl_cour", p.cfl_cour},
415 {
"cfl_force", p.cfl_force},
419 template<
class Tscal>
421 j.at(
"cfl_cour").get_to(p.cfl_cour);
422 j.at(
"cfl_force").get_to(p.cfl_force);
428 = std::get_if<SmoothingLengthConfig::DensityBased>(&p.config)) {
430 {
"type",
"density_based"},
435 = std::get_if<SmoothingLengthConfig::DensityBasedNeighLim>(&p.config)) {
438 {
"type",
"density_based_neigh_lim"},
439 {
"max_neigh_count", conf->max_neigh_count},
447 if (j.at(
"type").get<std::string>() ==
"density_based") {
449 }
else if (j.at(
"type").get<std::string>() ==
"density_based_neigh_lim") {
457 template<
class Tvec,
template<
class>
class SPHKernel>
460 using Tkernel =
typename T::Kernel;
462 std::string kernel_id = shambase::get_type_name<Tkernel>();
463 std::string type_id = shambase::get_type_name<Tvec>();
466 {
"solver_type",
"gsph"},
467 {
"kernel_id", kernel_id},
468 {
"type_id", type_id},
469 {
"scheduler_config", p.scheduler_conf},
470 {
"gpart_mass", p.gpart_mass},
471 {
"cfl_config", p.cfl_config},
472 {
"unit_sys", p.unit_sys},
473 {
"riemann_config", p.riemann_config},
474 {
"reconstruct_config", p.reconstruct_config},
475 {
"force_formulation_config", p.force_formulation_config},
476 {
"eos_config", p.eos_config},
477 {
"boundary_config", p.boundary_config},
478 {
"tree_reduction_level", p.tree_reduction_level},
480 {
"htol_up_coarse_cycle", p.htol_up_coarse_cycle},
481 {
"htol_up_fine_cycle", p.htol_up_fine_cycle},
482 {
"epsilon_h", p.epsilon_h},
483 {
"h_iter_per_subcycles", p.h_iter_per_subcycles},
484 {
"h_max_subcycles_count", p.h_max_subcycles_count},
485 {
"combined_dtdiv_divcurlv_compute", p.combined_dtdiv_divcurlv_compute},
486 {
"enable_particle_reordering", p.enable_particle_reordering},
487 {
"particle_reordering_step_freq", p.particle_reordering_step_freq},
488 {
"set_save_dt_to_fields", p.save_dt_to_fields},
489 {
"show_ghost_zone_graph", p.show_ghost_zone_graph},
493 template<
class Tvec,
template<
class>
class SPHKernel>
496 using Tkernel =
typename T::Kernel;
498 std::string kernel_id = j.at(
"kernel_id").get<std::string>();
499 if (kernel_id != shambase::get_type_name<Tkernel>()) {
501 "Invalid kernel type: expected " + shambase::get_type_name<Tkernel>() +
" but got "
505 std::string type_id = j.at(
"type_id").get<std::string>();
506 if (type_id != shambase::get_type_name<Tvec>()) {
508 "Invalid vector type: expected " + shambase::get_type_name<Tvec>() +
" but got "
512 bool has_used_defaults =
false;
513 bool has_updated_config =
false;
515 auto _get_to_if_contains = [&](
const std::string &key,
auto &value) {
519 _get_to_if_contains(
"scheduler_config", p.scheduler_conf);
520 _get_to_if_contains(
"gpart_mass", p.gpart_mass);
521 _get_to_if_contains(
"cfl_config", p.cfl_config);
522 _get_to_if_contains(
"unit_sys", p.unit_sys);
523 _get_to_if_contains(
"riemann_config", p.riemann_config);
524 _get_to_if_contains(
"reconstruct_config", p.reconstruct_config);
525 _get_to_if_contains(
"force_formulation_config", p.force_formulation_config);
526 _get_to_if_contains(
"eos_config", p.eos_config);
527 _get_to_if_contains(
"boundary_config", p.boundary_config);
528 _get_to_if_contains(
"tree_reduction_level", p.tree_reduction_level);
531 j, p.neigh_cache_strategy,
"GSPH::SolverConfig", has_used_defaults, has_updated_config);
532 _get_to_if_contains(
"htol_up_coarse_cycle", p.htol_up_coarse_cycle);
533 _get_to_if_contains(
"htol_up_fine_cycle", p.htol_up_fine_cycle);
534 _get_to_if_contains(
"epsilon_h", p.epsilon_h);
535 _get_to_if_contains(
"h_iter_per_subcycles", p.h_iter_per_subcycles);
536 _get_to_if_contains(
"h_max_subcycles_count", p.h_max_subcycles_count);
538 if (has_used_defaults || has_updated_config) {
541 "GSPH::SolverConfig",
Header file describing a Node Instance.
Configuration for reconstruction methods in GSPH.
Configuration for Riemann solvers in GSPH.
A unit system: the values of the seven SI base units in code units.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
A unit system, defined by the SI value of its base units.
The table of physical constants: each constant is a Unit (its SI value and dimension).
Neighbour cache build strategy enum + json serialization/deserialization.
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
constexpr const char * neigh_cache_strategy_json_key
Json key holding the neighbour cache strategy.
void get_to_neigh_cache_strategy(const nlohmann::json &j, NeighCacheStrategy &value, const std::string &log_ctx, bool &has_used_defaults, bool &has_updated_config)
Deserialize the neighbour cache strategy, falling back on the legacy boolean key.
NeighCacheStrategy neigh_cache_strategy_from_two_stage_search(bool use_two_stage_search)
Map the legacy use_two_stage_search boolean onto the strategy enum.
NeighCacheStrategy
Strategy used to build the neighbour cache out of the tree traversal.
@ TwoStage
Two stage neighbours search (see shamrock paper).
void get_to_if_contains(const nlohmann::json &j, const std::string &key, T &value, bool &has_used_defaults)
std::string log_json_changes(const nlohmann::json &j_current, const nlohmann::json &j, bool has_used_defaults, bool has_updated_config)
Shown the changes between two JSON objects to log config changes.
Contains traits and utilities for backend related types.
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
shamphys::EOS_Config_Polytropic< Tscal > Polytropic
Polytropic equation of state configuration.
shamphys::EOS_Config_Isothermal< Tscal > Isothermal
Isothermal equation of state configuration.
shamphys::EOS_Config_Adiabatic< Tscal > Adiabatic
Adiabatic equation of state configuration.
The configuration for the CFL condition in GSPH.
Tscal cfl_force
CFL condition for the force.
Tscal cfl_cour
CFL condition for the courant factor.
Limiter
Slope limiter types for MUSCL reconstruction.
@ VanLeer
van Leer limiter (smooth)
The configuration for a GSPH solver.
u32 h_iter_per_subcycles
Maximum number of iterations per subcycle.
void set_neigh_cache_strategy(NeighCacheStrategy strategy)
Setter for the neighbours cache strategy.
void set_boundary_shearing_periodic(i32_3 shear_base, i32_3 shear_dir, Tscal speed)
Set shearing periodic boundary conditions.
NeighCacheStrategy neigh_cache_strategy
Tscal get_eos_gamma() const
Get the adiabatic index (gamma) from the EOS config.
Tscal htol_up_coarse_cycle
Tscal gpart_mass
The mass of each gas particle (must be set before use).
u32 h_max_subcycles_count
Maximum number of subcycles before solver crash.
Tscal htol_up_fine_cycle
Maximum factor of the smoothing length evolution per subcycles.
bool combined_dtdiv_divcurlv_compute
void set_two_stage_search(bool enable)
Setter for the two stage search.
CFLConfig< Tscal > cfl_config
CFL configuration.
Tscal epsilon_h
Convergence criteria for the smoothing length.
Boundary conditions configuration.
void set_free()
Set the boundary condition to free boundaries.
Functions related to the MPI communicator.
#define ON_RANK_0(x)
Macro to execute code only on rank 0.