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"
31#include <algorithm>
32#include <functional>
33#include <optional>
34#include <vector>
35
37 template<class Tvec, class TgridVec>
38 class Solver {
39 public:
40 using Tscal = shambase::VecComponent<Tvec>;
41 using Tgridscal = shambase::VecComponent<TgridVec>;
42 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
43
44 using u_morton = u64;
45 using Config = SolverConfig<Tvec, TgridVec>;
46
47 using AMRBlock = typename Config::AMRBlock;
48
49 ShamrockCtx &context;
50 inline PatchScheduler &scheduler() { return shambase::get_check_ref(context.sched); }
51
52 Config solver_config;
53 SolverLog solve_logs;
54
56
58 inline Tscal &time_edge_value() {
59 return scheduler()
60 .synchronized_data
61 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("time")
62 .data;
63 }
64
66 inline Tscal &dt_edge_value() {
67 return scheduler()
68 .synchronized_data
69 .template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<Tscal>>("dt")
70 .data;
71 }
72
73 inline Tscal get_time() { return time_edge_value(); }
74 inline void set_time(Tscal t) { time_edge_value() = t; }
75 inline Tscal get_dt() { return dt_edge_value(); }
76 inline void set_next_dt(Tscal dt) { dt_edge_value() = dt; }
77
79 std::optional<std::function<void(void)>> step_begin_callback;
80 std::optional<std::function<void(void)>> step_end_callback;
81 };
82 std::vector<SolverStepCallback> timestep_callbacks{};
83
84 inline void init_required_fields() { solver_config.set_layout(context.get_pdl_write()); }
85
86 Solver(ShamrockCtx &context) : context(context) {}
87
88 void do_debug_vtk_dump(std::string filename);
89
90 inline void print_timestep_logs() {
91 if (shamcomm::world_rank() == 0) {
92 // logger::info_ln("Godunov", "iteration since start :",
93 // solve_logs.get_iteration_count());
95 "Godunov", "time since start :", shambase::details::get_wtime(), "(s)");
96 }
97 }
98
99 void evolve_once();
100
101 inline Tscal evolve_once_time_expl(Tscal t_current, Tscal dt_input) {
102 set_time(t_current);
103 set_next_dt(dt_input);
104 evolve_once();
105 return get_dt();
106 }
107
108 inline bool evolve_until(Tscal target_time, i32 niter_max) {
109 auto step = [&]() {
110 Tscal dt = get_dt();
111 Tscal t = get_time();
112
113 if (t > target_time) {
115 "the target time is lower than the current time");
116 }
117
118 if (t + dt > target_time) {
119 set_next_dt(target_time - t);
120 }
121 evolve_once();
122 };
123
124 i32 iter_count = 0;
125
126 while (get_time() < target_time) {
127 step();
128 iter_count++;
129
130 if ((iter_count >= niter_max) && (niter_max != -1)) {
131 logger::info_ln("SPH", "stopping evolve until because of niter =", iter_count);
132 return false;
133 }
134 }
135
136 print_timestep_logs();
137
138 return true;
139 }
140
141 void init_solver_graph();
142 };
143
144} // 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:58
Tscal & dt_edge_value()
Access synchronized next dt (scheduler edge "dt").
Definition Solver.hpp:66
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