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