49#include <nlohmann/json.hpp>
55namespace shammodels::gsph {
63 template<
class Tvec,
template<
class>
class SPHKernel>
72 struct SolverStatusVar;
89 using Tscal = shambase::VecComponent<Tvec>;
95template<
class Tvec,
template<
class>
class SPHKernel>
98 using Tscal = shambase::VecComponent<Tvec>;
100 using Kernel = SPHKernel<Tscal>;
101 using u_morton =
u32;
105 static constexpr Tscal Rkern = Kernel::Rkern;
117 std::optional<shamunits::UnitSystem<Tscal>> unit_sys = {};
121 inline Tscal get_constant_G()
const {
123 ON_RANK_0(logger::warn_ln(
"gsph::Config",
"the unit system is not set"));
139 using SolverStatusVar = SolverStatusVar<Tvec>;
140 SolverStatusVar time_state;
142 inline void set_time(Tscal t) { time_state.time = t; }
143 inline void set_next_dt(Tscal dt) { time_state.dt = dt; }
144 inline Tscal get_time()
const {
return time_state.time; }
145 inline Tscal get_dt()
const {
return time_state.dt; }
155 using RiemannConfig = RiemannConfig<Tvec>;
156 RiemannConfig riemann_config;
158 inline void set_riemann_iterative(Tscal tol = Tscal{1e-6},
u32 max_iter = 20) {
159 riemann_config.set_iterative(tol, max_iter);
162 inline void set_riemann_hllc() { riemann_config.set_hllc(); }
172 using ReconstructConfig = ReconstructConfig<Tvec>;
173 ReconstructConfig reconstruct_config;
175 inline void set_reconstruct_piecewise_constant() {
176 reconstruct_config.set_piecewise_constant();
179 inline void set_reconstruct_muscl(
181 reconstruct_config.set_muscl(limiter);
184 inline bool requires_gradients()
const {
return reconstruct_config.requires_gradients(); }
195 EOSConfig eos_config;
197 inline bool is_eos_adiabatic()
const {
199 return bool(std::get_if<T>(&eos_config.config));
202 inline bool is_eos_isothermal()
const {
204 return bool(std::get_if<T>(&eos_config.config));
215 if (
const auto *eos = std::get_if<Adiabatic>(&eos_config.config)) {
217 }
else if (
const auto *eos = std::get_if<Polytropic>(&eos_config.config)) {
223 inline void set_eos_adiabatic(Tscal gamma) { eos_config.set_adiabatic(gamma); }
225 inline void set_eos_isothermal(Tscal cs) { eos_config.set_isothermal(cs); }
236 BCConfig boundary_config;
238 inline void set_boundary_free() { boundary_config.
set_free(); }
239 inline void set_boundary_periodic() { boundary_config.set_periodic(); }
252 boundary_config.set_shearing_periodic(shear_base, shear_dir, speed);
266 inline void add_ext_force_point_mass(Tscal central_mass, Tscal Racc) {
267 ext_force_config.add_point_mass(central_mass, Racc);
278 u32 tree_reduction_level = 3;
279 bool use_two_stage_search =
true;
281 inline void set_tree_reduction_level(
u32 level) { tree_reduction_level = level; }
282 inline void set_two_stage_search(
bool enable) { use_two_stage_search = enable; }
302 inline bool has_field_uint()
const {
return is_eos_adiabatic(); }
304 inline void print_status() {
308 logger::raw_ln(
"----- GSPH Solver configuration -----");
310 riemann_config.print_status();
311 reconstruct_config.print_status();
312 eos_config.print_status();
313 logger::raw_ln(
"--------------------------------------");
316 inline void check_config()
const {
324 inline void check_config_runtime()
const {
328 "gpart_mass must be positive. Call set_particle_mass() before evolving.");
337namespace shammodels::gsph {
339 template<
class Tscal>
340 inline void to_json(nlohmann::json &j,
const CFLConfig<Tscal> &p) {
342 {
"cfl_cour", p.cfl_cour},
343 {
"cfl_force", p.cfl_force},
347 template<
class Tscal>
348 inline void from_json(
const nlohmann::json &j, CFLConfig<Tscal> &p) {
349 j.at(
"cfl_cour").get_to(p.cfl_cour);
350 j.at(
"cfl_force").get_to(p.cfl_force);
354 inline void to_json(nlohmann::json &j,
const SolverStatusVar<Tvec> &p) {
362 inline void from_json(
const nlohmann::json &j, SolverStatusVar<Tvec> &p) {
363 using Tscal =
typename SolverStatusVar<Tvec>::Tscal;
364 j.at(
"time").get_to<Tscal>(p.time);
365 j.at(
"dt").get_to<Tscal>(p.dt);
368 template<
class Tvec,
template<
class>
class SPHKernel>
369 inline void to_json(nlohmann::json &j,
const SolverConfig<Tvec, SPHKernel> &p) {
370 using T = SolverConfig<Tvec, SPHKernel>;
371 using Tkernel =
typename T::Kernel;
377 {
"solver_type",
"gsph"},
378 {
"kernel_id", kernel_id},
379 {
"type_id", type_id},
380 {
"scheduler_config", p.scheduler_conf},
381 {
"gpart_mass", p.gpart_mass},
382 {
"cfl_config", p.cfl_config},
383 {
"unit_sys", p.unit_sys},
384 {
"time_state", p.time_state},
385 {
"riemann_config", p.riemann_config},
386 {
"reconstruct_config", p.reconstruct_config},
387 {
"eos_config", p.eos_config},
388 {
"boundary_config", p.boundary_config},
389 {
"tree_reduction_level", p.tree_reduction_level},
390 {
"use_two_stage_search", p.use_two_stage_search},
391 {
"htol_up_coarse_cycle", p.htol_up_coarse_cycle},
392 {
"htol_up_fine_cycle", p.htol_up_fine_cycle},
393 {
"epsilon_h", p.epsilon_h},
394 {
"h_iter_per_subcycles", p.h_iter_per_subcycles},
395 {
"h_max_subcycles_count", p.h_max_subcycles_count},
399 template<
class Tvec,
template<
class>
class SPHKernel>
400 inline void from_json(
const nlohmann::json &j, SolverConfig<Tvec, SPHKernel> &p) {
401 using T = SolverConfig<Tvec, SPHKernel>;
402 using Tkernel =
typename T::Kernel;
404 std::string kernel_id = j.at(
"kernel_id").get<std::string>();
411 std::string type_id = j.at(
"type_id").get<std::string>();
418 bool has_used_defaults =
false;
419 bool has_updated_config =
false;
421 auto _get_to_if_contains = [&](
const std::string &key,
auto &value) {
425 _get_to_if_contains(
"scheduler_config", p.scheduler_conf);
426 _get_to_if_contains(
"gpart_mass", p.gpart_mass);
427 _get_to_if_contains(
"cfl_config", p.cfl_config);
428 _get_to_if_contains(
"unit_sys", p.unit_sys);
429 _get_to_if_contains(
"time_state", p.time_state);
430 _get_to_if_contains(
"riemann_config", p.riemann_config);
431 _get_to_if_contains(
"reconstruct_config", p.reconstruct_config);
432 _get_to_if_contains(
"eos_config", p.eos_config);
433 _get_to_if_contains(
"boundary_config", p.boundary_config);
434 _get_to_if_contains(
"tree_reduction_level", p.tree_reduction_level);
435 _get_to_if_contains(
"use_two_stage_search", p.use_two_stage_search);
436 _get_to_if_contains(
"htol_up_coarse_cycle", p.htol_up_coarse_cycle);
437 _get_to_if_contains(
"htol_up_fine_cycle", p.htol_up_fine_cycle);
438 _get_to_if_contains(
"epsilon_h", p.epsilon_h);
439 _get_to_if_contains(
"h_iter_per_subcycles", p.h_iter_per_subcycles);
440 _get_to_if_contains(
"h_max_subcycles_count", p.h_max_subcycles_count);
442 if (has_used_defaults || has_updated_config) {
445 "GSPH::SolverConfig",
Header file describing a Node Instance.
Configuration for reconstruction methods in GSPH.
Configuration for Riemann solvers in GSPH.
std::uint32_t u32
32 bit unsigned integer
A Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
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.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
void to_json(nlohmann::json &j, const EOSConfig< Tvec > &p)
Serialize EOSConfig to json.
void from_json(const nlohmann::json &j, EOSConfig< Tvec > &p)
Deserializes an EOSConfig<Tvec> from a JSON object.
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.
Configuration struct for the equation of state used in the hydrodynamic models.
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
Max iterations per subcycle.
void set_boundary_shearing_periodic(i32_3 shear_base, i32_3 shear_dir, Tscal speed)
Set shearing periodic boundary conditions.
Tscal get_eos_gamma() const
Get the adiabatic index (gamma) from the EOS config.
Tscal htol_up_coarse_cycle
Factor for neighbors search.
Tscal gpart_mass
The mass of each gas particle (must be set before use)
u32 h_max_subcycles_count
Max subcycles before crash.
Tscal htol_up_fine_cycle
Max smoothing length evolution per subcycle.
CFLConfig< Tscal > cfl_config
CFL configuration.
Tscal epsilon_h
Convergence criteria for smoothing length.
Solver status variables for GSPH.
Tscal dt
Current time step.
Boundary conditions configuration.
void set_free()
Set the boundary condition to free boundaries.
constexpr T G()
get the value of G in the current unit system units
Functions related to the MPI communicator.
#define ON_RANK_0(x)
Macro to execute code only on rank 0.