51 inline f64 rate_sum() {
return shamalgs::collective::allreduce_sum(rate); }
53 inline u64 npart_sum() {
return shamalgs::collective::allreduce_sum(npart); }
55 inline f64 tcompute_max() {
return shamalgs::collective::allreduce_max(tcompute); }
59 bool reach_target_time;
61 bool reach_max_walltime;
70 struct WalltimeLimiter {
76 inline WalltimeLimiter(
bool active,
f64 max_walltime)
77 : active(active), max_walltime(max_walltime) {
78 start_wall_time = active ? synced_wtime() : 0;
79 next_check_iter = active ? 1 : std::numeric_limits<i32>::max();
82 inline f64 synced_wtime() {
90 inline bool due(
i32 iter_count)
const {
return active && iter_count >= next_check_iter; }
95 f64 global_walltime = synced_wtime();
98 if (global_walltime >= max_walltime) {
103 "stopping evolve until because of "
104 "max_walltime = {:.2f}s > {:.2f}s",
111 f64 sec_per_iter = (global_walltime - start_wall_time) /
static_cast<f64>(iter_count);
113 auto get_remaining_iters = [&](
f64 delta_walltime,
f64 factor) ->
i32 {
114 if (sec_per_iter > 0) {
115 f64 tmp = factor * delta_walltime / sec_per_iter;
116 if (tmp > std::numeric_limits<i32>::max()) {
117 return std::numeric_limits<i32>::max();
119 return static_cast<i32>(tmp);
124 i32 iters_to_limit = get_remaining_iters(max_walltime - global_walltime, 0.25);
125 i32 iters_to_next_check = iters_to_limit;
127 next_check_iter = iter_count + std::max(1, iters_to_next_check);
133 "next walltime check in {:.2f}s (niter = {}) global walltime = "
134 "{:.2f}s (max_walltime = {:.2f}s)",
135 iters_to_next_check * sec_per_iter,
151 template<
class Tvec,
template<
class>
class SPHKernel>
154 using Tscal = shambase::VecComponent<Tvec>;
155 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
156 using Kernel = SPHKernel<Tscal>;
159 using Cfg_MHD =
typename Config::MHDConfig;
161 using NoneMHD =
typename Cfg_MHD::None;
162 using IdealMHD =
typename Cfg_MHD::IdealMhdConstrainedHyperPara;
163 using NonIdealMHD =
typename Cfg_MHD::NonIdealMHD;
167 static constexpr Tscal Rkern = Kernel::Rkern;
174 Config solver_config;
181 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(
"time")
189 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(
"dt")
197 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(
228 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(name)
242 auto &sync = scheduler().synchronized_data;
244 std::map<std::string, Tscal> ret;
245 for (
const std::string &name : sync.get_edge_names()) {
247 ret[name] = sync.template get_edge_ref<Edge>(name).data;
254 std::optional<std::function<void(
void)>> step_begin_callback;
255 std::optional<std::function<void(
void)>> step_end_callback;
257 std::vector<SolverStepCallback> timestep_callbacks{};
259 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
262 void gen_serial_patch_tree();
263 inline void reset_serial_patch_tree() { storage.serial_patch_tree.reset(); }
266 using GhostHandle = sph::BasicSPHGhostHandler<Tvec>;
267 using GhostHandleCache =
typename GhostHandle::CacheMap;
269 inline void reset_ghost_handler() { storage.ghost_handler.reset(); }
282 using RTree =
typename Config::RTree;
340 Solver(ShamrockCtx &context) : context(context) {}
346 void vtk_do_dump(std::string filename,
bool add_patch_world_id);
348 void set_debug_dump(
bool _do_debug_dump, std::string _debug_dump_filename) {
349 solver_config.set_debug_dump(_do_debug_dump, _debug_dump_filename);
352 inline void print_timestep_logs() {
354 logger::info_ln(
"SPH",
"iteration since start :", solve_logs.get_iteration_count());
355 logger::info_ln(
"SPH",
"time since start :", shambase::details::get_wtime(),
"(s)");
365 set_next_dt(dt_input);
371 Tscal target_time,
i32 niter_max,
f64 max_walltime = -1) {
373 const bool niter_limit_active = (niter_max >= 0);
374 const bool walltime_limit_active = (max_walltime >= 0);
380 "evolve_until (target_time = {:.2f}s, niter_max = {}, max_walltime = "
388 Tscal dt = get_dt_sph();
389 Tscal t = get_time();
391 if (t > target_time) {
393 "the target time is higher than the current time");
396 if (t + dt > target_time) {
397 set_next_dt(target_time - t);
402 WalltimeLimiter walltime_limiter(walltime_limit_active, max_walltime);
406 while (get_time() < target_time) {
411 if (niter_limit_active && iter_count >= niter_max) {
414 "SPH",
"stopping evolve until because of niter =", iter_count);
417 .reach_target_time =
false,
418 .reach_niter_max =
true,
419 .reach_max_walltime =
false,
420 .iter_count = iter_count,
425 if (walltime_limiter.due(iter_count)) {
427 if (walltime_limiter.exceeded(iter_count)) {
429 .reach_target_time =
false,
430 .reach_niter_max =
false,
431 .reach_max_walltime =
true,
432 .iter_count = iter_count,
438 print_timestep_logs();
441 .reach_target_time =
true,
442 .reach_niter_max =
false,
443 .reach_max_walltime =
false,
444 .iter_count = iter_count,
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
void reset_presteps_rint()
Resets tree radius interval field.
static constexpr const char * cfl_dt_detail_edge_prefix
Prefix of the synchronized edges holding the last CFL dt per CFL type.
void reset_merge_ghosts_fields()
Resets merged ghost field data.
void update_sync_load_values()
Updates load balancing values and synchronizes patch ownership.
Tscal & cfl_multiplier_edge_value()
Access synchronized CFL multiplier (scheduler edge "cfl_multiplier").
bool apply_corrector(Tscal dt, u64 Npart_all)
void merge_position_ghost()
Merges ghost particle positions from neighboring patches.
void reset_eos_fields()
Frees memory allocated for EOS fields.
void set_cfl_dt_edge_value(const std::string &name, Tscal value)
Store a CFL dt value in its synchronized edge.
void build_ghost_cache(Tscal time_val)
Generates the ghost handler and builds the ghost particle interface cache for inter-patch communicati...
void prepare_corrector()
Saves old derivative fields for predictor-corrector integration.
void update_artificial_viscosity(Tscal dt)
Updates artificial viscosity coefficients for shock capturing.
TimestepLog evolve_once()
Performs one complete SPH timestep evolution.
void update_j()
Updates the magnetic current field (for NIMHD).
std::map< std::string, Tscal > get_last_cfl_dts()
Get the last CFL dt values stored in the synchronized edges.
void vtk_do_dump(std::string filename, bool add_patch_world_id)
Writes VTK dump file for visualization.
void update_derivs(Tscal dt_hydro)
Updates time derivatives and applies external forces.
void build_merged_pos_trees()
Builds spatial BVH trees for merged positions including ghosts.
void clear_merged_pos_trees()
Clears merged position trees to free memory.
void init_solver_graph()
Initializes the solver graph for computation pipeline.
void sph_prestep(Tscal time_val, Tscal dt)
Performs pre-step operations for SPH timestep.
void compute_presteps_rint()
Computes maximum smoothing length in tree nodes for neighbor search.
void compute_eos_fields()
Computes equation of state fields (pressure, sound speed).
void apply_position_boundary(Tscal time_val)
Applies position-based boundary conditions.
static bool is_cfl_dt_edge_name(const std::string &name)
Check whether a synchronized edge name is one of the last CFL dt edges.
void reset_neighbors_cache()
Resets neighbor cache.
Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input)
Evolves system by one explicit timestep with specified time and dt.
static constexpr const char * cfl_dt_edge_name
Name of the synchronized edge holding the last global CFL dt.
Tscal & dt_edge_value()
Access synchronized next dt (scheduler edge "dt", not solver_graph "dt").
void communicate_merge_ghosts_fields()
Communicates and merges ghost particle fields across processes.
void clear_ghost_cache()
Clears ghost particle cache to free memory.
void init_ghost_layout()
Initializes data layout for ghost particle fields.
void start_neighbors_cache()
Builds neighbor particle cache for SPH calculations.
Tscal & time_edge_value()
Access synchronized simulation time (scheduler edge "time").
This header file contains utility functions related to exception handling in the code.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
namespace for the sph model
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
f64 get_wtime()
Returns the current wall clock time in seconds.
Class holding the logs of the solver /todo add a variable to keep only a definite number of steps in ...
The configuration for a sph solver.
u32 u_morton
The type of the Morton code for the tree.
bool due(i32 iter_count) const
True if the next walltime check is due at this iteration count.
bool exceeded(i32 iter_count)