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
21
22#include "shambackends/vec.hpp"
23#include "shamcomm/logs.hpp"
33#include <algorithm>
34#include <functional>
35#include <optional>
36#include <vector>
37
39 template<class Tvec, class TgridVec>
40 class Solver {
41 public:
42 using Tscal = shambase::VecComponent<Tvec>;
43 using Tgridscal = shambase::VecComponent<TgridVec>;
44 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
45
46 using u_morton = u64;
47 using Config = SolverConfig<Tvec, TgridVec>;
48
49 using AMRBlock = typename Config::AMRBlock;
50
51 ShamrockCtx &context;
52 inline PatchScheduler &scheduler() { return shambase::get_check_ref(context.sched); }
53
54 Config solver_config;
55 SolverLog solve_logs;
56
58
60 inline Tscal &time_edge_value() {
61 return scheduler()
62 .synchronized_data
63 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("time")
64 .data;
65 }
66
68 inline Tscal &dt_edge_value() {
69 return scheduler()
70 .synchronized_data
71 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("dt")
72 .data;
73 }
74
75 inline Tscal get_time() { return time_edge_value(); }
76 inline void set_time(Tscal t) { time_edge_value() = t; }
77 inline Tscal get_dt() { return dt_edge_value(); }
78 inline void set_next_dt(Tscal dt) { dt_edge_value() = dt; }
79
82 auto &sync = scheduler().synchronized_data;
83 auto names = sync.get_edge_names();
84 auto has_edge = [&](const std::string &name) {
85 return std::find(names.begin(), names.end(), name) != names.end();
86 };
87
88 if (!has_edge("time")) {
89 auto edge = sync.register_edge(
91 edge->data = 0;
92 }
93 if (!has_edge("dt")) {
94 auto edge = sync.register_edge(
96 edge->data = 0;
97 }
98 }
99
101 std::optional<std::function<void(void)>> step_begin_callback;
102 std::optional<std::function<void(void)>> step_end_callback;
103 };
104 std::vector<SolverStepCallback> timestep_callbacks{};
105
106 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
107
108 Solver(ShamrockCtx &context) : context(context) {}
109
110 void do_debug_vtk_dump(std::string filename);
111
112 inline void print_timestep_logs() {
113 if (shamcomm::world_rank() == 0) {
114 // logger::info_ln("Godunov", "iteration since start :",
115 // solve_logs.get_iteration_count());
117 "Godunov", "time since start :", shambase::details::get_wtime(), "(s)");
118 }
119 }
120
121 void evolve_once();
122
123 inline Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input) {
124 set_time(t_current);
125 set_next_dt(dt_input);
126 evolve_once();
127 return get_dt();
128 }
129
130 inline bool evolve_until(Tscal target_time, i32 niter_max) {
131 auto step = [&]() {
132 Tscal dt = get_dt();
133 Tscal t = get_time();
134
135 if (t > target_time) {
137 "the target time is lower than the current time");
138 }
139
140 if (t + dt > target_time) {
141 set_next_dt(target_time - t);
142 }
143 evolve_once();
144 };
145
146 i32 iter_count = 0;
147
148 while (get_time() < target_time) {
149 step();
150 iter_count++;
151
152 if ((iter_count >= niter_max) && (niter_max != -1)) {
153 logger::info_ln("SPH", "stopping evolve until because of niter =", iter_count);
154 return false;
155 }
156 }
157
158 print_timestep_logs();
159
160 return true;
161 }
162
163 void init_solver_graph();
164 };
165
166} // namespace shammodels::basegodunov
utility to manipulate AMR blocks
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.
Tscal & time_edge_value()
Access synchronized simulation time (scheduler edge "time").
Definition Solver.hpp:60
void ensure_time_state_edges()
Register time/dt synchronized edges if missing (idempotent).
Definition Solver.hpp:81
Tscal & dt_edge_value()
Access synchronized next dt (scheduler edge "dt").
Definition Solver.hpp:68
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 basegodunov 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