Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ComputeCFL.cpp
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
16
18#include "fmt/core.h"
19#include "shammath/riemann.hpp"
22
23template<class Tvec, class TgridVec>
24template<shammath::FluidStateSpec FSpec>
25void shammodels::basegodunov::modules::ComputeCFL<Tvec, TgridVec>::compute_cfl_gas(
26 shamrock::ComputeField<Tscal> &cfl_dt, FSpec fluid) {
27
28 StackEntry stack_loc{};
29
30 using namespace shamrock::patch;
31 using namespace shamrock;
32 using namespace shammath;
33
34 // load layout info
35 PatchDataLayerLayout &pdl = scheduler().pdl_old();
36
37 const u32 icell_min = pdl.get_field_idx<TgridVec>("cell_min");
38 const u32 icell_max = pdl.get_field_idx<TgridVec>("cell_max");
39 const u32 irho = pdl.get_field_idx<Tscal>("rho");
40 const u32 irhoetot = pdl.get_field_idx<Tscal>("rhoetot");
41 const u32 irhovel = pdl.get_field_idx<Tvec>("rhovel");
42
43 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
44 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
45
46 u32 cell_count = pdat.get_obj_cnt() * AMRBlock::block_size;
47
48 sham::DeviceBuffer<TgridVec> &buf_block_min = pdat.get_field_buf_ref<TgridVec>(0);
49 sham::DeviceBuffer<TgridVec> &buf_block_max = pdat.get_field_buf_ref<TgridVec>(1);
50
51 sham::DeviceBuffer<Tscal> &buf_rho = pdat.get_field_buf_ref<Tscal>(irho);
52 sham::DeviceBuffer<Tvec> &buf_rhov = pdat.get_field_buf_ref<Tvec>(irhovel);
53 sham::DeviceBuffer<Tscal> &buf_rhoe = pdat.get_field_buf_ref<Tscal>(irhoetot);
54
55 sham::DeviceBuffer<Tscal> &cfl_dt_buf = cfl_dt.get_buf_check(cur_p.id_patch);
56
57 sham::DeviceBuffer<Tscal> &block_cell_sizes
58 = shambase::get_check_ref(storage.block_cell_sizes)
59 .get_refs()
60 .get(cur_p.id_patch)
61 .get()
62 .get_buf();
63
64 sham::EventList depends_list;
65 auto cfl_dt = cfl_dt_buf.get_write_access(depends_list);
66 auto acc_block_min = buf_block_min.get_read_access(depends_list);
67 auto acc_block_max = buf_block_max.get_read_access(depends_list);
68 auto rho = buf_rho.get_read_access(depends_list);
69 auto rhov = buf_rhov.get_read_access(depends_list);
70 auto rhoe = buf_rhoe.get_read_access(depends_list);
71
72 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
73 Tscal C_safe = solver_config.Csafe;
74
75 Tscal one_over_Nside = 1. / AMRBlock::Nside;
76
77 Tscal dxfact = solver_config.grid_coord_to_pos_fact;
78
79 auto fspec = fluid;
80
81 shambase::parallel_for(cgh, cell_count, "compute_cfl", [=](u64 gid) {
82 const u32 cell_global_id = (u32) gid;
83
84 const u32 block_id = cell_global_id / AMRBlock::block_size;
85 const u32 cell_loc_id = cell_global_id % AMRBlock::block_size;
86
87 TgridVec lower = acc_block_min[block_id];
88 TgridVec upper = acc_block_max[block_id];
89 // size from the int coords difference, so that it does not depend on the block
90 // position
91 Tvec block_cell_size
92 = (upper - lower).template convert<Tscal>() * (dxfact * one_over_Nside);
93 Tscal dx = block_cell_size.x();
94
95 // rhoe is ignored by the barotropic cons_to_prim
96 auto conststate = shammath::ConsState<Tvec>{rho[gid], rhoe[gid], rhov[gid]};
97
98 auto prim_state = fspec.cons_to_prim(conststate);
99
100 constexpr Tscal div = 1. / 3.;
101
102 Tscal cs = fspec.sound_speed(prim_state);
103 Tscal vnorm = sycl::length(prim_state.vel);
104 Tscal dt = C_safe * dx * div / (cs + vnorm);
105
106 cfl_dt[gid] = dt;
107 });
108 });
109
110 cfl_dt_buf.complete_event_state(e);
111 buf_block_min.complete_event_state(e);
112 buf_block_max.complete_event_state(e);
113 buf_rho.complete_event_state(e);
114 buf_rhov.complete_event_state(e);
115 buf_rhoe.complete_event_state(e);
116 });
117}
118
119template<class Tvec, class TgridVec>
121
122 StackEntry stack_loc{};
123
124 using namespace shamrock;
125
126 SchedulerUtility utility(scheduler());
127 ComputeField<Tscal> cfl_dt = utility.make_compute_field<Tscal>("cfl_dt", AMRBlock::block_size);
128
129 // each EOSConfig alternative maps to its shammath fluid spec (FluidStateAdiabatic or
130 // FluidStateBarotropic) through get_spec()
131 std::visit(
132 [&](const auto &eos_cfg) {
133 compute_cfl_gas(cfl_dt, eos_cfg.get_spec());
134 },
135 solver_config.eos_config.config);
136
137 Tscal rank_dt = cfl_dt.compute_rank_min();
138
139 shamlog_debug_ln("basegodunov", "rank", shamcomm::world_rank(), "found cfl dt =", rank_dt);
140
141 Tscal next_cfl = shamalgs::collective::allreduce_min(rank_dt);
142
143 if (shamcomm::world_rank() == 0) {
144 logger::info_ln("amr::basegodunov", "cfl dt =", next_cfl);
145 }
146
147 return next_cfl;
148}
149
150template<class Tvec, class TgridVec>
151auto shammodels::basegodunov::modules::ComputeCFL<Tvec, TgridVec>::compute_dust_cfl() -> Tscal {
152
153 StackEntry stack_loc{};
154
155 using namespace shamrock::patch;
156 using namespace shamrock;
157 using namespace shammath;
158
159 SchedulerUtility utility(scheduler());
160 u32 ndust = solver_config.dust_config.ndust;
161 ComputeField<Tscal> dust_cfl_dt
162 = utility.make_compute_field<Tscal>("dust_cfl_dt", ndust * AMRBlock::block_size);
163
164 // load layout info
165 PatchDataLayerLayout &pdl = scheduler().pdl_old();
166
167 const u32 icell_min = pdl.get_field_idx<TgridVec>("cell_min");
168 const u32 icell_max = pdl.get_field_idx<TgridVec>("cell_max");
169 const u32 irho_dust = pdl.get_field_idx<Tscal>("rho_dust");
170 const u32 irhovel_dust = pdl.get_field_idx<Tvec>("rhovel_dust");
171
172 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
173 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
174
175 u32 cell_count = pdat.get_obj_cnt() * AMRBlock::block_size;
176
177 sham::DeviceBuffer<Tscal> &buf_rho_dust = pdat.get_field_buf_ref<Tscal>(irho_dust);
178 sham::DeviceBuffer<Tvec> &buf_rhov_dust = pdat.get_field_buf_ref<Tvec>(irhovel_dust);
179
180 sham::DeviceBuffer<Tscal> &dust_cfl_dt_buf = dust_cfl_dt.get_buf_check(cur_p.id_patch);
181
182 sham::DeviceBuffer<Tscal> &block_cell_sizes
183 = shambase::get_check_ref(storage.block_cell_sizes)
184 .get_refs()
185 .get(cur_p.id_patch)
186 .get()
187 .get_buf();
188
189 sham::EventList depends_list;
190 auto dust_cfl_dt = dust_cfl_dt_buf.get_write_access(depends_list);
191 auto rho_dust = buf_rho_dust.get_read_access(depends_list);
192 auto rhov_dust = buf_rhov_dust.get_read_access(depends_list);
193 auto acc_aabb_cell_size = block_cell_sizes.get_read_access(depends_list);
194
195 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
196 Tscal C_safe = solver_config.Csafe;
197
198 shambase::parallel_for(cgh, ndust * cell_count, "compute_dust_cfl", [=](u64 gid) {
199 const u32 tmp_gid = (u32) gid;
200 const u32 cell_global_id = tmp_gid / ndust;
201 const u32 ndust_off_loc = tmp_gid % ndust;
202
203 const u32 block_id = cell_global_id / AMRBlock::block_size;
204 const u32 cell_loc_id = cell_global_id % AMRBlock::block_size;
205
206 auto conststate = shammath::DustConsState<Tvec>{
207 rho_dust[ndust * cell_global_id + ndust_off_loc],
208 rhov_dust[ndust * cell_global_id + ndust_off_loc]};
209 Tscal dx = acc_aabb_cell_size[block_id];
210
212 auto prim_state = dust_fluid.cons_to_prim(conststate);
213
214 constexpr Tscal div = 1. / 3.;
215
216 Tscal vnorm = sycl::length(prim_state.vel);
217 Tscal dt = C_safe * dx * div / (vnorm);
218
219 dust_cfl_dt[ndust * cell_global_id + ndust_off_loc] = dt;
220 });
221 });
222
223 dust_cfl_dt_buf.complete_event_state(e);
224 buf_rho_dust.complete_event_state(e);
225 buf_rhov_dust.complete_event_state(e);
226 block_cell_sizes.complete_event_state(e);
227 });
228
229 Tscal rank_dust_dt = dust_cfl_dt.compute_rank_min();
230
231 shamlog_debug_ln(
232 "basegodunov", "rank", shamcomm::world_rank(), "found dust cfl dt =", rank_dust_dt);
233
234 Tscal next_dust_cfl = shamalgs::collective::allreduce_min(rank_dust_dt);
235
236 if (shamcomm::world_rank() == 0) {
237 logger::info_ln("amr::basegodunov", "dust cfl dt =", next_dust_cfl);
238 }
239
240 return next_dust_cfl;
241}
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
const T * get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const
Get a read-only pointer to the buffer's data.
A SYCL queue associated with a device and a context.
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
Tscal compute_cfl()
Gas CFL timestep, using the equation of state selected in solver_config.eos_config.
ComputeField< T > make_compute_field(std::string new_name, u32 nvar)
create a compute field and init it to zeros
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
PatchDataLayer container class, the layout is described in patchdata_layout.
u32 get_obj_cnt() const
get the number of objects (particles) stored in this layer
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
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for math utility
Definition AABB.hpp:26
namespace for the main framework
Definition __init__.py:1
Umbrella header pulling in the gas states and all gas Riemann solvers (Rusanov, HLL,...
Umbrella header pulling in the dust states and all dust Riemann solvers (HLL, Huang & Bai).
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
cons_to_prim/prim_to_cons/vn/flux wrapper for a pressureless (dust) fluid. Unlike FluidStateAdiabatic...
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86