Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Solver.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19
21#include "SolverConfig.hpp"
22#include "shambackends/vec.hpp"
34#include <functional>
35#include <limits>
36#include <map>
37#include <memory>
38#include <optional>
39#include <stdexcept>
40#include <string>
41#include <variant>
42#include <vector>
43namespace shammodels::sph {
44
45 struct TimestepLog {
46 i32 rank;
47 f64 rate;
48 u64 npart;
49 f64 tcompute;
50
51 inline f64 rate_sum() { return shamalgs::collective::allreduce_sum(rate); }
52
53 inline u64 npart_sum() { return shamalgs::collective::allreduce_sum(npart); }
54
55 inline f64 tcompute_max() { return shamalgs::collective::allreduce_max(tcompute); }
56 };
57
59 bool reach_target_time;
60 bool reach_niter_max;
61 bool reach_max_walltime;
62
63 i32 iter_count;
64 };
65
70 struct WalltimeLimiter {
71 bool active;
72 f64 max_walltime;
73 f64 start_wall_time;
74 i32 next_check_iter;
75
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();
80 }
81
82 inline f64 synced_wtime() {
83 if (active) {
84 return shamalgs::collective::allreduce_max(shambase::details::get_wtime());
85 }
86 return 0;
87 }
88
90 inline bool due(i32 iter_count) const { return active && iter_count >= next_check_iter; }
91
94 inline bool exceeded(i32 iter_count) {
95 f64 global_walltime = synced_wtime();
96
97 // if the global walltime is greater than the max walltime
98 if (global_walltime >= max_walltime) {
99 if (shamcomm::world_rank() == 0) {
101 "SPH",
102 sham::format(
103 "stopping evolve until because of "
104 "max_walltime = {:.2f}s > {:.2f}s",
105 global_walltime,
106 max_walltime));
107 }
108 return true;
109 }
110
111 f64 sec_per_iter = (global_walltime - start_wall_time) / static_cast<f64>(iter_count);
112
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();
118 }
119 return static_cast<i32>(tmp);
120 }
121 return 1000; // default to 1000 iterations if sec_per_iter is 0
122 };
123
124 i32 iters_to_limit = get_remaining_iters(max_walltime - global_walltime, 0.25);
125 i32 iters_to_next_check = iters_to_limit;
126
127 next_check_iter = iter_count + std::max(1, iters_to_next_check);
128
129 if (shamcomm::world_rank() == 0) {
131 "SPH",
132 sham::format(
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,
136 iters_to_next_check,
137 global_walltime,
138 max_walltime));
139 }
140
141 return false;
142 }
143 };
144
151 template<class Tvec, template<class> class SPHKernel>
152 class Solver {
153 public:
154 using Tscal = shambase::VecComponent<Tvec>;
155 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
156 using Kernel = SPHKernel<Tscal>;
157
158 using Config = SolverConfig<Tvec, SPHKernel>;
159 using Cfg_MHD = typename Config::MHDConfig;
160
161 using NoneMHD = typename Cfg_MHD::None;
162 using IdealMHD = typename Cfg_MHD::IdealMhdConstrainedHyperPara;
163 using NonIdealMHD = typename Cfg_MHD::NonIdealMHD;
164
165 using u_morton = typename Config::u_morton;
166
167 static constexpr Tscal Rkern = Kernel::Rkern;
168
169 ShamrockCtx &context;
170 inline PatchScheduler &scheduler() { return shambase::get_check_ref(context.sched); }
171
173
174 Config solver_config;
175 SolverLog solve_logs;
176
178 inline Tscal &time_edge_value() {
179 return scheduler()
180 .synchronized_data
181 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("time")
182 .data;
183 }
184
186 inline Tscal &dt_edge_value() {
187 return scheduler()
188 .synchronized_data
189 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("dt")
190 .data;
191 }
192
195 return scheduler()
196 .synchronized_data
197 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(
198 "cfl_multiplier")
199 .data;
200 }
201
202 inline Tscal get_time() { return time_edge_value(); }
203 inline void set_time(Tscal t) { time_edge_value() = t; }
204 inline Tscal get_dt_sph() { return dt_edge_value(); }
205 inline void set_next_dt(Tscal dt) { dt_edge_value() = dt; }
206 inline Tscal get_cfl_multipler() { return cfl_multiplier_edge_value(); }
207 inline void set_cfl_multipler(Tscal lambda) { cfl_multiplier_edge_value() = lambda; }
208
210 static constexpr const char *cfl_dt_edge_name = "cfl_dt";
212 static constexpr const char *cfl_dt_detail_edge_prefix = "cfl_dt_";
213
215 static inline bool is_cfl_dt_edge_name(const std::string &name) {
216 return name == cfl_dt_edge_name || name.starts_with(cfl_dt_detail_edge_prefix);
217 }
218
225 inline void set_cfl_dt_edge_value(const std::string &name, Tscal value) {
226 scheduler()
227 .synchronized_data
228 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>(name)
229 .data = value;
230 }
231
240 inline std::map<std::string, Tscal> get_last_cfl_dts() {
242 auto &sync = scheduler().synchronized_data;
243
244 std::map<std::string, Tscal> ret;
245 for (const std::string &name : sync.get_edge_names()) {
246 if (is_cfl_dt_edge_name(name)) {
247 ret[name] = sync.template get_edge_ref<Edge>(name).data;
248 }
249 }
250 return ret;
251 }
252
254 std::optional<std::function<void(void)>> step_begin_callback;
255 std::optional<std::function<void(void)>> step_end_callback;
256 };
257 std::vector<SolverStepCallback> timestep_callbacks{};
258
259 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
260
261 // serial patch tree control
262 void gen_serial_patch_tree();
263 inline void reset_serial_patch_tree() { storage.serial_patch_tree.reset(); }
264
265 // interface_control
266 using GhostHandle = sph::BasicSPHGhostHandler<Tvec>;
267 using GhostHandleCache = typename GhostHandle::CacheMap;
268
269 inline void reset_ghost_handler() { storage.ghost_handler.reset(); }
270
274 void build_ghost_cache(Tscal time_val);
276 void clear_ghost_cache();
277
280
281 // trees
282 using RTree = typename Config::RTree;
287
291 void reset_presteps_rint();
292
297
299 void sph_prestep(Tscal time_val, Tscal dt);
300
302 void apply_position_boundary(Tscal time_val);
303
305 void update_artificial_viscosity(Tscal dt);
306
308 void update_j();
309
311 void init_ghost_layout();
312
317
319 void compute_eos_fields();
320
322 void reset_eos_fields();
323
325 void prepare_corrector();
327 void update_derivs(Tscal dt_hydro);
328
335 bool apply_corrector(Tscal dt, u64 Npart_all);
336
339
340 Solver(ShamrockCtx &context) : context(context) {}
341
343 void init_solver_graph();
344
346 void vtk_do_dump(std::string filename, bool add_patch_world_id);
347
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);
350 }
351
352 inline void print_timestep_logs() {
353 if (shamcomm::world_rank() == 0) {
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)");
356 }
357 }
358
360 TimestepLog evolve_once();
361
363 Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input) {
364 set_time(t_current);
365 set_next_dt(dt_input);
366 evolve_once();
367 return get_dt_sph();
368 }
369
370 inline EvolveUntilResults evolve_until(
371 Tscal target_time, i32 niter_max, f64 max_walltime = -1) {
372
373 const bool niter_limit_active = (niter_max >= 0);
374 const bool walltime_limit_active = (max_walltime >= 0);
375
376 if (shamcomm::world_rank() == 0) {
378 "SPH",
379 sham::format(
380 "evolve_until (target_time = {:.2f}s, niter_max = {}, max_walltime = "
381 "{:.2f}s)",
382 target_time,
383 niter_max,
384 max_walltime));
385 }
386
387 auto step = [&]() {
388 Tscal dt = get_dt_sph();
389 Tscal t = get_time();
390
391 if (t > target_time) {
393 "the target time is higher than the current time");
394 }
395
396 if (t + dt > target_time) {
397 set_next_dt(target_time - t);
398 }
399 evolve_once();
400 };
401
402 WalltimeLimiter walltime_limiter(walltime_limit_active, max_walltime);
403
404 i32 iter_count = 0;
405
406 while (get_time() < target_time) {
407 step();
408 iter_count++;
409
410 // if the iteration count is greater than the maximum iteration count
411 if (niter_limit_active && iter_count >= niter_max) {
412 if (shamcomm::world_rank() == 0) {
413 logger::info_ln(
414 "SPH", "stopping evolve until because of niter =", iter_count);
415 }
416 return {
417 .reach_target_time = false,
418 .reach_niter_max = true,
419 .reach_max_walltime = false,
420 .iter_count = iter_count,
421 };
422 }
423
424 // if walltime limit is active and the next walltime check is due
425 if (walltime_limiter.due(iter_count)) {
426 // must be inside the .due if since there is a MPI reduction in exceeded
427 if (walltime_limiter.exceeded(iter_count)) {
428 return {
429 .reach_target_time = false,
430 .reach_niter_max = false,
431 .reach_max_walltime = true,
432 .iter_count = iter_count,
433 };
434 }
435 }
436 }
437
438 print_timestep_logs();
439
440 return {
441 .reach_target_time = true,
442 .reach_niter_max = false,
443 .reach_max_walltime = false,
444 .iter_count = iter_count,
445 };
446 }
447 };
448
449} // namespace shammodels::sph
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
The MPI scheduler.
void reset_presteps_rint()
Resets tree radius interval field.
Definition Solver.cpp:2260
static constexpr const char * cfl_dt_detail_edge_prefix
Prefix of the synchronized edges holding the last CFL dt per CFL type.
Definition Solver.hpp:212
void reset_merge_ghosts_fields()
Resets merged ghost field data.
Definition Solver.cpp:2542
void update_sync_load_values()
Updates load balancing values and synchronizes patch ownership.
Definition Solver.cpp:2898
Tscal & cfl_multiplier_edge_value()
Access synchronized CFL multiplier (scheduler edge "cfl_multiplier").
Definition Solver.hpp:194
bool apply_corrector(Tscal dt, u64 Npart_all)
Definition Solver.cpp:2893
void merge_position_ghost()
Merges ghost particle positions from neighboring patches.
Definition Solver.cpp:1868
void reset_eos_fields()
Frees memory allocated for EOS fields.
Definition Solver.cpp:2625
void set_cfl_dt_edge_value(const std::string &name, Tscal value)
Store a CFL dt value in its synchronized edge.
Definition Solver.hpp:225
void build_ghost_cache(Tscal time_val)
Generates the ghost handler and builds the ghost particle interface cache for inter-patch communicati...
Definition Solver.cpp:1634
void prepare_corrector()
Saves old derivative fields for predictor-corrector integration.
Definition Solver.cpp:2631
void update_artificial_viscosity(Tscal dt)
Updates artificial viscosity coefficients for shock capturing.
Definition Solver.cpp:2551
TimestepLog evolve_once()
Performs one complete SPH timestep evolution.
Definition Solver.cpp:2905
void update_j()
Updates the magnetic current field (for NIMHD).
Definition Solver.cpp:2558
std::map< std::string, Tscal > get_last_cfl_dts()
Get the last CFL dt values stored in the synchronized edges.
Definition Solver.hpp:240
void vtk_do_dump(std::string filename, bool add_patch_world_id)
Writes VTK dump file for visualization.
Definition Solver.cpp:1411
void update_derivs(Tscal dt_hydro)
Updates time derivatives and applies external forces.
Definition Solver.cpp:2727
void build_merged_pos_trees()
Builds spatial BVH trees for merged positions including ghosts.
Definition Solver.cpp:1911
void clear_merged_pos_trees()
Clears merged position trees to free memory.
Definition Solver.cpp:1916
void init_solver_graph()
Initializes the solver graph for computation pipeline.
Definition Solver.cpp:273
void sph_prestep(Tscal time_val, Tscal dt)
Performs pre-step operations for SPH timestep.
Definition Solver.cpp:1922
void compute_presteps_rint()
Computes maximum smoothing length in tree nodes for neighbor search.
Definition Solver.cpp:2223
void compute_eos_fields()
Computes equation of state fields (pressure, sound speed).
Definition Solver.cpp:2619
void apply_position_boundary(Tscal time_val)
Applies position-based boundary conditions.
Definition Solver.cpp:1588
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.
Definition Solver.hpp:215
void reset_neighbors_cache()
Resets neighbor cache.
Definition Solver.cpp:2295
Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input)
Evolves system by one explicit timestep with specified time and dt.
Definition Solver.hpp:363
static constexpr const char * cfl_dt_edge_name
Name of the synchronized edge holding the last global CFL dt.
Definition Solver.hpp:210
Tscal & dt_edge_value()
Access synchronized next dt (scheduler edge "dt", not solver_graph "dt").
Definition Solver.hpp:186
void communicate_merge_ghosts_fields()
Communicates and merges ghost particle fields across processes.
Definition Solver.cpp:2300
void clear_ghost_cache()
Clears ghost particle cache to free memory.
Definition Solver.cpp:1862
void init_ghost_layout()
Initializes data layout for ghost particle fields.
Definition Solver.cpp:2208
void start_neighbors_cache()
Builds neighbor particle cache for SPH calculations.
Definition Solver.cpp:2265
Tscal & time_edge_value()
Access synchronized simulation time (scheduler edge "time").
Definition Solver.hpp:178
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...
Definition memory.hpp:112
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.
Definition worldInfo.cpp:41
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.
Definition logs.hpp:132
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 ...
Definition SolverLog.hpp:34
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.
Definition Solver.hpp:90
bool exceeded(i32 iter_count)
Definition Solver.hpp:94