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
28
30#include "SolverConfig.hpp"
31#include "shambackends/vec.hpp"
43#include <algorithm>
44#include <memory>
45#include <stdexcept>
46#include <variant>
47
48namespace shammodels::gsph {
49
50 struct TimestepLog {
51 i32 rank;
52 f64 rate;
53 u64 npart;
54 f64 tcompute;
55
56 inline f64 rate_sum() { return shamalgs::collective::allreduce_sum(rate); }
57 inline u64 npart_sum() { return shamalgs::collective::allreduce_sum(npart); }
58 inline f64 tcompute_max() { return shamalgs::collective::allreduce_max(tcompute); }
59 };
60
70 template<class Tvec, template<class> class SPHKernel>
71 class Solver {
72 public:
73 using Tscal = shambase::VecComponent<Tvec>;
74 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
75 using Kernel = SPHKernel<Tscal>;
76
77 using Config = SolverConfig<Tvec, SPHKernel>;
78
79 using u_morton = u32;
80
81 static constexpr Tscal Rkern = Kernel::Rkern;
82
83 ShamrockCtx &context;
84 inline PatchScheduler &scheduler() { return shambase::get_check_ref(context.sched); }
85
87
88 Config solver_config;
89 SolverLog solve_logs;
90
92 inline Tscal &time_edge_value() {
93 return scheduler()
94 .synchronized_data
95 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("time")
96 .data;
97 }
98
100 inline Tscal &dt_edge_value() {
101 return scheduler()
102 .synchronized_data
103 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("dt")
104 .data;
105 }
106
107 inline Tscal get_time() { return time_edge_value(); }
108 inline void set_time(Tscal t) { time_edge_value() = t; }
109 inline Tscal get_dt() { return dt_edge_value(); }
110 inline void set_next_dt(Tscal dt) { dt_edge_value() = dt; }
111
112 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
113
114 // Serial patch tree control
115 void gen_serial_patch_tree();
116 inline void reset_serial_patch_tree() { storage.serial_patch_tree.reset(); }
117
118 // Ghost handling - use GSPH ghost handler with Newtonian field names
119 using GhostHandle = GSPHGhostHandler<Tvec>;
120 using GhostHandleCache = typename GhostHandle::CacheMap;
121
122 inline void reset_ghost_handler() {
123 shambase::get_check_ref(storage.ghost_handler).free_alloc();
124 }
125
128 void build_ghost_cache(Tscal time_val);
129 void clear_ghost_cache();
130
131 void merge_position_ghost();
132
133 // Tree operations
134 using RTree = typename Config::RTree;
135 void build_merged_pos_trees();
136 void clear_merged_pos_trees();
137
138 void compute_presteps_rint();
139 void reset_presteps_rint();
140
141 void start_neighbors_cache();
142 void reset_neighbors_cache();
143
144 void gsph_prestep(Tscal time_val, Tscal dt);
145
146 void apply_position_boundary(Tscal time_val);
147
148 void do_predictor_leapfrog(Tscal dt);
149
150 void init_ghost_layout();
151
152 void communicate_merge_ghosts_fields();
153 void reset_merge_ghosts_fields();
154
155 void compute_eos_fields();
156 void reset_eos_fields();
157
166
178 void compute_density();
179
189 void compute_gradients();
190
191 void prepare_corrector();
192
200 void update_derivs();
201
211 Tscal compute_dt_cfl();
212
213 bool apply_corrector(Tscal dt, u64 Npart_all);
214
215 void update_sync_load_values();
216
217 Solver(ShamrockCtx &context) : context(context) {}
218
219 void init_solver_graph();
220
221 void vtk_do_dump(std::string filename, bool add_patch_world_id);
222
223 inline void print_timestep_logs() {
224 if (shamcomm::world_rank() == 0) {
225 logger::info_ln(
226 "GSPH", "iteration since start :", solve_logs.get_iteration_count());
227 logger::info_ln(
228 "GSPH", "time since start :", shambase::details::get_wtime(), "(s)");
229 }
230 }
231
232 TimestepLog evolve_once();
233
234 Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input) {
235 set_time(t_current);
236 set_next_dt(dt_input);
237 evolve_once();
238 return get_dt();
239 }
240
241 inline bool evolve_until(Tscal target_time, i32 niter_max = -1) {
242 auto step = [&]() {
243 Tscal dt = get_dt();
244 Tscal t = get_time();
245
246 if (t > target_time) {
248 "the target time is higher than the current time");
249 }
250
251 if (t + dt > target_time) {
252 set_next_dt(target_time - t);
253 }
254 evolve_once();
255 };
256
257 i32 iter_count = 0;
258
259 while (get_time() < target_time) {
260 step();
261 iter_count++;
262
263 if ((iter_count >= niter_max) && (niter_max != -1)) {
264 logger::info_ln("GSPH", "stopping evolve until because of niter =", iter_count);
265 return false;
266 }
267 }
268
269 print_timestep_logs();
270
271 return true;
272 }
273 };
274
275} // namespace shammodels::gsph
GSPH-specific ghost handler using Newtonian physics field names.
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 compute_density()
Compute SPH-summation density for GSPH.
Definition Solver.cpp:638
Tscal & time_edge_value()
Access synchronized simulation time (scheduler edge "time").
Definition Solver.hpp:92
void copy_eos_to_patchdata()
Copy EOS fields from solvergraph to patchdata for persistence.
Definition Solver.cpp:1398
void compute_gradients()
Compute gradients for MUSCL reconstruction.
Definition Solver.cpp:1460
TimestepLog evolve_once()
Definition Solver.cpp:1894
void update_derivs()
Update derivatives using GSPH Riemann solver.
Definition Solver.cpp:1708
Tscal & dt_edge_value()
Access synchronized next dt (scheduler edge "dt").
Definition Solver.hpp:100
Tscal compute_dt_cfl()
Compute CFL timestep constraint.
Definition Solver.cpp:1716
void build_ghost_cache(Tscal time_val)
Definition Solver.cpp:172
This header file contains utility functions related to exception handling in the code.
Configuration for the Godunov SPH (GSPH) solver.
Storage for GSPH solver runtime data.
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
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 GSPH solver.
Runtime storage for GSPH solver.