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
29#include "SolverConfig.hpp"
30#include "shambackends/vec.hpp"
41#include <memory>
42#include <stdexcept>
43#include <variant>
44
45namespace shammodels::gsph {
46
47 struct TimestepLog {
48 i32 rank;
49 f64 rate;
50 u64 npart;
51 f64 tcompute;
52
53 inline f64 rate_sum() { return shamalgs::collective::allreduce_sum(rate); }
54 inline u64 npart_sum() { return shamalgs::collective::allreduce_sum(npart); }
55 inline f64 tcompute_max() { return shamalgs::collective::allreduce_max(tcompute); }
56 };
57
67 template<class Tvec, template<class> class SPHKernel>
68 class Solver {
69 public:
70 using Tscal = shambase::VecComponent<Tvec>;
72 using Kernel = SPHKernel<Tscal>;
73
75
76 using u_morton = u32;
77
78 static constexpr Tscal Rkern = Kernel::Rkern;
79
80 ShamrockCtx &context;
81 inline PatchScheduler &scheduler() { return shambase::get_check_ref(context.sched); }
82
84
85 Config solver_config;
86 sph::SolverLog solve_logs;
87
88 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
89
90 // Serial patch tree control
91 void gen_serial_patch_tree();
92 inline void reset_serial_patch_tree() { storage.serial_patch_tree.reset(); }
93
94 // Ghost handling - use GSPH ghost handler with Newtonian field names
96 using GhostHandleCache = typename GhostHandle::CacheMap;
97
98 void gen_ghost_handler(Tscal time_val);
99 inline void reset_ghost_handler() {
100 shambase::get_check_ref(storage.ghost_handler).free_alloc();
101 }
102
103 void build_ghost_cache();
104 void clear_ghost_cache();
105
106 void merge_position_ghost();
107
108 // Tree operations
109 using RTree = typename Config::RTree;
110 void build_merged_pos_trees();
111 void clear_merged_pos_trees();
112
113 void compute_presteps_rint();
114 void reset_presteps_rint();
115
116 void start_neighbors_cache();
117 void reset_neighbors_cache();
118
119 void gsph_prestep(Tscal time_val, Tscal dt);
120
121 void apply_position_boundary(Tscal time_val);
122
123 void do_predictor_leapfrog(Tscal dt);
124
125 void init_ghost_layout();
126
127 void communicate_merge_ghosts_fields();
128 void reset_merge_ghosts_fields();
129
130 void compute_omega();
131 void compute_eos_fields();
132 void reset_eos_fields();
133
142
152 void compute_gradients();
153
154 void prepare_corrector();
155
163 void update_derivs();
164
174 Tscal compute_dt_cfl();
175
176 bool apply_corrector(Tscal dt, u64 Npart_all);
177
178 void update_sync_load_values();
179
180 Solver(ShamrockCtx &context) : context(context) {}
181
182 void init_solver_graph();
183
184 void vtk_do_dump(std::string filename, bool add_patch_world_id);
185
186 inline void print_timestep_logs() {
187 if (shamcomm::world_rank() == 0) {
188 logger::info_ln(
189 "GSPH", "iteration since start :", solve_logs.get_iteration_count());
190 logger::info_ln(
191 "GSPH", "time since start :", shambase::details::get_wtime(), "(s)");
192 }
193 }
194
196
197 Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input) {
198 solver_config.set_time(t_current);
199 solver_config.set_next_dt(dt_input);
200 evolve_once();
201 return solver_config.get_dt();
202 }
203
204 inline bool evolve_until(Tscal target_time, i32 niter_max = -1) {
205 auto step = [&]() {
206 Tscal dt = solver_config.get_dt();
207 Tscal t = solver_config.get_time();
208
209 if (t > target_time) {
211 "the target time is higher than the current time");
212 }
213
214 if (t + dt > target_time) {
215 solver_config.set_next_dt(target_time - t);
216 }
217 evolve_once();
218 };
219
220 i32 iter_count = 0;
221
222 while (solver_config.get_time() < target_time) {
223 step();
224 iter_count++;
225
226 if ((iter_count >= niter_max) && (niter_max != -1)) {
227 logger::info_ln("GSPH", "stopping evolve until because of niter =", iter_count);
228 return false;
229 }
230 }
231
232 print_timestep_logs();
233
234 return true;
235 }
236 };
237
238} // 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.
Container for objects shared between two distributed data elements.
The GSPH Solver class.
Definition Solver.hpp:68
void copy_eos_to_patchdata()
Copy EOS fields from solvergraph to patchdata for persistence.
Definition Solver.cpp:1224
void compute_gradients()
Compute gradients for MUSCL reconstruction.
Definition Solver.cpp:1286
TimestepLog evolve_once()
Definition Solver.cpp:1716
void update_derivs()
Update derivatives using GSPH Riemann solver.
Definition Solver.cpp:1534
Tscal compute_dt_cfl()
Compute CFL timestep constraint.
Definition Solver.cpp:1542
A Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
This header file contains utility functions related to exception handling in the code.
Storage for GSPH solver runtime data.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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:110
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
The configuration for a GSPH solver.
Runtime storage for GSPH solver.
Component< SerialPatchTree< Tvec > > serial_patch_tree
Serial patch tree for load balancing.
std::shared_ptr< solvergraph::GhostHandlerEdge< Tvec > > ghost_handler
Ghost handler for boundary particles.
Class holding the logs of the solver /todo add a variable to keep only a definite number of steps in ...
Definition SolverLog.hpp:33