Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SumFluxHydro.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
18
19template<class Tvec, class TgridVec>
21 using Tscal = shambase::VecComponent<Tvec>;
23
25
26 inline static shammath::AABB<Tvec> get_cell_aabb(
27 u32 id,
28 const Tvec *__restrict cell0block_aabb_lower,
29 const Tscal *__restrict block_cell_sizes) {
30 const u32 cell_global_id = (u32) id;
31
32 const u32 block_id = cell_global_id / AMRBlock::block_size;
33 const u32 cell_loc_id = cell_global_id % AMRBlock::block_size;
34
35 // fetch current block info
36 const Tvec cblock_min = cell0block_aabb_lower[block_id];
37 const Tscal delta_cell = block_cell_sizes[block_id];
38
39 std::array<u32, 3> lcoord_arr = AMRBlock::get_coord(cell_loc_id);
40 Tvec offset = Tvec{lcoord_arr[0], lcoord_arr[1], lcoord_arr[2]} * delta_cell;
41
42 Tvec aabb_min = cblock_min + offset;
43 Tvec aabb_max = aabb_min + delta_cell;
44
45 return {aabb_min, aabb_max};
46 };
47
48 inline static Tscal get_face_surface(
49 u32 id_a,
50 u32 id_b,
51 const Tvec *__restrict cell0block_aabb_lower,
52 const Tscal *__restrict block_cell_sizes,
53 Tscal dxfact) {
54 shammath::AABB<Tvec> aabb_cell_a
55 = get_cell_aabb(id_a, cell0block_aabb_lower, block_cell_sizes);
56 shammath::AABB<Tvec> aabb_cell_b
57 = get_cell_aabb(id_b, cell0block_aabb_lower, block_cell_sizes);
58
59 shammath::AABB<Tvec> face_aabb = aabb_cell_a.get_intersect(aabb_cell_b);
60
61 Tvec delta_face = face_aabb.delt();
62
63 // smallest possible coordinate delta, anything under this is considered null
64 // cell can not have less than size 1 in the grid space, so 1*dxfact is the minimum size, so
65 // we check against dxfact/2
66 Tscal smd = dxfact / 2;
67
68 delta_face.x() = (delta_face.x() < smd) ? 1 : delta_face.x();
69 delta_face.y() = (delta_face.y() < smd) ? 1 : delta_face.y();
70 delta_face.z() = (delta_face.z() < smd) ? 1 : delta_face.z();
71
72 return delta_face.x() * delta_face.y() * delta_face.z();
73 };
74
75 Tscal dxfact;
76
77 inline void operator()(
78 u32 id_a,
79 const Tscal *__restrict flux_rho_face_xp,
80 const Tscal *__restrict flux_rho_face_xm,
81 const Tscal *__restrict flux_rho_face_yp,
82 const Tscal *__restrict flux_rho_face_ym,
83 const Tscal *__restrict flux_rho_face_zp,
84 const Tscal *__restrict flux_rho_face_zm,
85 const Tvec *__restrict flux_rhov_face_xp,
86 const Tvec *__restrict flux_rhov_face_xm,
87 const Tvec *__restrict flux_rhov_face_yp,
88 const Tvec *__restrict flux_rhov_face_ym,
89 const Tvec *__restrict flux_rhov_face_zp,
90 const Tvec *__restrict flux_rhov_face_zm,
91 const Tscal *__restrict flux_rhoe_face_xp,
92 const Tscal *__restrict flux_rhoe_face_xm,
93 const Tscal *__restrict flux_rhoe_face_yp,
94 const Tscal *__restrict flux_rhoe_face_ym,
95 const Tscal *__restrict flux_rhoe_face_zp,
96 const Tscal *__restrict flux_rhoe_face_zm,
97 const Tscal *__restrict block_cell_sizes,
98 const Tvec *__restrict cell0block_aabb_lower,
99 const AMRGraph::ro_access graph_iter_xp,
100 const AMRGraph::ro_access graph_iter_xm,
101 const AMRGraph::ro_access graph_iter_yp,
102 const AMRGraph::ro_access graph_iter_ym,
103 const AMRGraph::ro_access graph_iter_zp,
104 const AMRGraph::ro_access graph_iter_zm,
105 Tscal *__restrict dt_rho,
106 Tvec *__restrict dt_rhov,
107 Tscal *__restrict dt_rhoe) const {
108 const u32 block_id = id_a / AMRBlock::block_size;
109 const u32 cell_loc_id = id_a % AMRBlock::block_size;
110
111 Tscal V_i = block_cell_sizes[block_id];
112 V_i = V_i * V_i * V_i;
113
114 Tscal dtrho = 0;
115 Tvec dtrhov = {0, 0, 0};
116 Tscal dtrhoe = 0;
117
118 auto add_flux = [&](const auto &graph_iter,
119 const Tscal *flux_rho,
120 const Tvec *flux_rhov,
121 const Tscal *flux_rhoe) {
122 graph_iter.for_each_object_link_id(id_a, [&](u32 id_b, u32 link_id) {
124 id_a, id_b, cell0block_aabb_lower, block_cell_sizes, dxfact);
125 dtrho -= flux_rho[link_id] * S_ij;
126 dtrhov -= flux_rhov[link_id] * S_ij;
127 dtrhoe -= flux_rhoe[link_id] * S_ij;
128 });
129 };
130
131 add_flux(graph_iter_xp, flux_rho_face_xp, flux_rhov_face_xp, flux_rhoe_face_xp);
132 add_flux(graph_iter_xm, flux_rho_face_xm, flux_rhov_face_xm, flux_rhoe_face_xm);
133 add_flux(graph_iter_yp, flux_rho_face_yp, flux_rhov_face_yp, flux_rhoe_face_yp);
134 add_flux(graph_iter_ym, flux_rho_face_ym, flux_rhov_face_ym, flux_rhoe_face_ym);
135 add_flux(graph_iter_zp, flux_rho_face_zp, flux_rhov_face_zp, flux_rhoe_face_zp);
136 add_flux(graph_iter_zm, flux_rho_face_zm, flux_rhov_face_zm, flux_rhoe_face_zm);
137
138 dtrho /= V_i;
139 dtrhov /= V_i;
140 dtrhoe /= V_i;
141
142 dt_rho[id_a] = dtrho;
143 dt_rhov[id_a] = dtrhov;
144 dt_rhoe[id_a] = dtrhoe;
145 }
146};
147
148template<class Tvec, class TgridVec>
150 auto edges = get_edges();
151
152 edges.spans_dtrho.ensure_sizes(edges.block_counts.indexes);
153 edges.spans_dtrhov.ensure_sizes(edges.block_counts.indexes);
154 edges.spans_dtrhoe.ensure_sizes(edges.block_counts.indexes);
155
156 auto get_graph_neigh = [&](Direction dir, u64 patch_id) -> const AMRGraph & {
158
159 const CellGraphEdge &cell_neigh_graph = edges.cell_neigh_graph;
160 const shambase::DistributedData<OrientedAMRGraph> &graph = cell_neigh_graph.graph;
161 const OrientedAMRGraph &oriented_cell_graph = graph.get(patch_id);
162 const std::unique_ptr<AMRGraph> &graph_link = oriented_cell_graph.graph_links[dir];
163
164 return shambase::get_check_ref(graph_link);
165 };
166
167 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
168
169 edges.block_counts.indexes.for_each([&](u64 patch_id, const u64 &block_count) {
170 const AMRGraph &graph_neigh_xp = get_graph_neigh(Direction::xp, patch_id);
171 const AMRGraph &graph_neigh_xm = get_graph_neigh(Direction::xm, patch_id);
172 const AMRGraph &graph_neigh_yp = get_graph_neigh(Direction::yp, patch_id);
173 const AMRGraph &graph_neigh_ym = get_graph_neigh(Direction::ym, patch_id);
174 const AMRGraph &graph_neigh_zp = get_graph_neigh(Direction::zp, patch_id);
175 const AMRGraph &graph_neigh_zm = get_graph_neigh(Direction::zm, patch_id);
176
177 auto get_face_buf = [&](auto &neigh_link_field) -> const auto & {
178 return neigh_link_field.link_fields.get(patch_id).link_graph_field;
179 };
180
181 const auto &buf_flux_rho_face_xp = get_face_buf(edges.flux_rho_face_xp);
182 const auto &buf_flux_rho_face_xm = get_face_buf(edges.flux_rho_face_xm);
183 const auto &buf_flux_rho_face_yp = get_face_buf(edges.flux_rho_face_yp);
184 const auto &buf_flux_rho_face_ym = get_face_buf(edges.flux_rho_face_ym);
185 const auto &buf_flux_rho_face_zp = get_face_buf(edges.flux_rho_face_zp);
186 const auto &buf_flux_rho_face_zm = get_face_buf(edges.flux_rho_face_zm);
187 const auto &buf_flux_rhov_face_xp = get_face_buf(edges.flux_rhov_face_xp);
188 const auto &buf_flux_rhov_face_xm = get_face_buf(edges.flux_rhov_face_xm);
189 const auto &buf_flux_rhov_face_yp = get_face_buf(edges.flux_rhov_face_yp);
190 const auto &buf_flux_rhov_face_ym = get_face_buf(edges.flux_rhov_face_ym);
191 const auto &buf_flux_rhov_face_zp = get_face_buf(edges.flux_rhov_face_zp);
192 const auto &buf_flux_rhov_face_zm = get_face_buf(edges.flux_rhov_face_zm);
193 const auto &buf_flux_rhoe_face_xp = get_face_buf(edges.flux_rhoe_face_xp);
194 const auto &buf_flux_rhoe_face_xm = get_face_buf(edges.flux_rhoe_face_xm);
195 const auto &buf_flux_rhoe_face_yp = get_face_buf(edges.flux_rhoe_face_yp);
196 const auto &buf_flux_rhoe_face_ym = get_face_buf(edges.flux_rhoe_face_ym);
197 const auto &buf_flux_rhoe_face_zp = get_face_buf(edges.flux_rhoe_face_zp);
198 const auto &buf_flux_rhoe_face_zm = get_face_buf(edges.flux_rhoe_face_zm);
199
200 auto &block_cell_sizes = edges.spans_block_cell_sizes.get_spans().get(patch_id);
201 auto &cell0block_aabb_lower = edges.spans_cell0block_aabb_lower.get_spans().get(patch_id);
202
203 auto &dt_rho_patch = edges.spans_dtrho.get_spans().get(patch_id);
204 auto &dt_rhov_patch = edges.spans_dtrhov.get_spans().get(patch_id);
205 auto &dt_rhoe_patch = edges.spans_dtrhoe.get_spans().get(patch_id);
206
207 u32 cell_count = block_count * AMRBlock::block_size;
208
210 q,
211 sham::MultiRef{buf_flux_rho_face_xp, buf_flux_rho_face_xm, buf_flux_rho_face_yp,
212 buf_flux_rho_face_ym, buf_flux_rho_face_zp, buf_flux_rho_face_zm,
213 buf_flux_rhov_face_xp, buf_flux_rhov_face_xm, buf_flux_rhov_face_yp,
214 buf_flux_rhov_face_ym, buf_flux_rhov_face_zp, buf_flux_rhov_face_zm,
215 buf_flux_rhoe_face_xp, buf_flux_rhoe_face_xm, buf_flux_rhoe_face_yp,
216 buf_flux_rhoe_face_ym, buf_flux_rhoe_face_zp, buf_flux_rhoe_face_zm,
217 block_cell_sizes, cell0block_aabb_lower, graph_neigh_xp,
218 graph_neigh_xm, graph_neigh_yp, graph_neigh_ym,
219 graph_neigh_zp, graph_neigh_zm},
220 sham::MultiRef{dt_rho_patch, dt_rhov_patch, dt_rhoe_patch},
221 cell_count,
223 });
224}
225
Sum the fluxes into the time derivative fields for Hydro.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
DeviceQueue & get_queue(u32 id=0)
Get a reference to a DeviceQueue.
Represents a collection of objects distributed across patches identified by a u64 id.
T & get(u64 id)
Returns a reference to an object in the collection.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
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
A class that references multiple buffers or similar objects.
Axis-Aligned bounding box.
Definition AABB.hpp:99
T delt() const
Returns the delta of the AABB.
Definition AABB.hpp:152
AABB get_intersect(AABB other) const noexcept
Compute the intersection of two AABB.
Definition AABB.hpp:234
utility class to handle AMR blocks
Definition AMRBlock.hpp:35