Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SumFluxDust.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 u32 ndust;
27 Tscal dxfact;
28
29 inline static shammath::AABB<Tvec> get_cell_aabb(
30 u32 id,
31 const Tvec *__restrict cell0block_aabb_lower,
32 const Tscal *__restrict block_cell_sizes) {
33 const u32 cell_global_id = (u32) id;
34
35 const u32 block_id = cell_global_id / AMRBlock::block_size;
36 const u32 cell_loc_id = cell_global_id % AMRBlock::block_size;
37
38 // fetch current block info
39 const Tvec cblock_min = cell0block_aabb_lower[block_id];
40 const Tscal delta_cell = block_cell_sizes[block_id];
41
42 std::array<u32, 3> lcoord_arr = AMRBlock::get_coord(cell_loc_id);
43 Tvec offset = Tvec{lcoord_arr[0], lcoord_arr[1], lcoord_arr[2]} * delta_cell;
44
45 Tvec aabb_min = cblock_min + offset;
46 Tvec aabb_max = aabb_min + delta_cell;
47
48 return {aabb_min, aabb_max};
49 };
50
51 inline static Tscal get_face_surface(
52 u32 id_a,
53 u32 id_b,
54 const Tvec *__restrict cell0block_aabb_lower,
55 const Tscal *__restrict block_cell_sizes,
56 Tscal dxfact) {
57 shammath::AABB<Tvec> aabb_cell_a
58 = get_cell_aabb(id_a, cell0block_aabb_lower, block_cell_sizes);
59 shammath::AABB<Tvec> aabb_cell_b
60 = get_cell_aabb(id_b, cell0block_aabb_lower, block_cell_sizes);
61
62 shammath::AABB<Tvec> face_aabb = aabb_cell_a.get_intersect(aabb_cell_b);
63
64 Tvec delta_face = face_aabb.delt();
65
66 // smallest possible coordinate delta, anything under this is considered null
67 // cell can not have less than size 1 in the grid space, so 1*dxfact is the minimum size, so
68 // we check against dxfact/2
69 Tscal smd = dxfact / 2;
70
71 delta_face.x() = (delta_face.x() < smd) ? 1 : delta_face.x();
72 delta_face.y() = (delta_face.y() < smd) ? 1 : delta_face.y();
73 delta_face.z() = (delta_face.z() < smd) ? 1 : delta_face.z();
74
75 return delta_face.x() * delta_face.y() * delta_face.z();
76 };
77
78 inline void operator()(
79 u32 tid,
80 const Tscal *__restrict flux_rho_face_xp,
81 const Tscal *__restrict flux_rho_face_xm,
82 const Tscal *__restrict flux_rho_face_yp,
83 const Tscal *__restrict flux_rho_face_ym,
84 const Tscal *__restrict flux_rho_face_zp,
85 const Tscal *__restrict flux_rho_face_zm,
86 const Tvec *__restrict flux_rhov_face_xp,
87 const Tvec *__restrict flux_rhov_face_xm,
88 const Tvec *__restrict flux_rhov_face_yp,
89 const Tvec *__restrict flux_rhov_face_ym,
90 const Tvec *__restrict flux_rhov_face_zp,
91 const Tvec *__restrict flux_rhov_face_zm,
92 const Tscal *__restrict block_cell_sizes,
93 const Tvec *__restrict cell0block_aabb_lower,
94 const AMRGraph::ro_access graph_iter_xp,
95 const AMRGraph::ro_access graph_iter_xm,
96 const AMRGraph::ro_access graph_iter_yp,
97 const AMRGraph::ro_access graph_iter_ym,
98 const AMRGraph::ro_access graph_iter_zp,
99 const AMRGraph::ro_access graph_iter_zm,
100 Tscal *__restrict dt_rho,
101 Tvec *__restrict dt_rhov) const {
102
103 // cell id in the global space of index
104 const u32 id_a = tid / ndust;
105 // variable id in the cell of icell_a id
106 const u32 ndust_off_loc = tid % ndust;
107
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
117 auto add_flux = [&](const auto &graph_iter, const Tscal *flux_rho, const Tvec *flux_rhov) {
118 graph_iter.for_each_object_link_id(id_a, [&](u32 id_b, u32 link_id) {
120 id_a, id_b, cell0block_aabb_lower, block_cell_sizes, dxfact);
121 dtrho -= flux_rho[link_id * ndust + ndust_off_loc] * S_ij;
122 dtrhov -= flux_rhov[link_id * ndust + ndust_off_loc] * S_ij;
123 });
124 };
125
126 add_flux(graph_iter_xp, flux_rho_face_xp, flux_rhov_face_xp);
127 add_flux(graph_iter_xm, flux_rho_face_xm, flux_rhov_face_xm);
128 add_flux(graph_iter_yp, flux_rho_face_yp, flux_rhov_face_yp);
129 add_flux(graph_iter_ym, flux_rho_face_ym, flux_rhov_face_ym);
130 add_flux(graph_iter_zp, flux_rho_face_zp, flux_rhov_face_zp);
131 add_flux(graph_iter_zm, flux_rho_face_zm, flux_rhov_face_zm);
132
133 dtrho /= V_i;
134 dtrhov /= V_i;
135
136 dt_rho[id_a * ndust + ndust_off_loc] = dtrho;
137 dt_rhov[id_a * ndust + ndust_off_loc] = dtrhov;
138 }
139};
140
141template<class Tvec, class TgridVec>
143 auto edges = get_edges();
144
145 edges.spans_dtrho.ensure_sizes(edges.block_counts.indexes);
146 edges.spans_dtrhov.ensure_sizes(edges.block_counts.indexes);
147
148 auto get_graph_neigh = [&](Direction dir, u64 patch_id) -> const AMRGraph & {
150
151 const CellGraphEdge &cell_neigh_graph = edges.cell_neigh_graph;
152 const shambase::DistributedData<OrientedAMRGraph> &graph = cell_neigh_graph.graph;
153 const OrientedAMRGraph &oriented_cell_graph = graph.get(patch_id);
154 const std::unique_ptr<AMRGraph> &graph_link = oriented_cell_graph.graph_links[dir];
155
156 return shambase::get_check_ref(graph_link);
157 };
158
159 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
160
161 edges.block_counts.indexes.for_each([&](u64 patch_id, const u64 &block_count) {
162 const AMRGraph &graph_neigh_xp = get_graph_neigh(Direction::xp, patch_id);
163 const AMRGraph &graph_neigh_xm = get_graph_neigh(Direction::xm, patch_id);
164 const AMRGraph &graph_neigh_yp = get_graph_neigh(Direction::yp, patch_id);
165 const AMRGraph &graph_neigh_ym = get_graph_neigh(Direction::ym, patch_id);
166 const AMRGraph &graph_neigh_zp = get_graph_neigh(Direction::zp, patch_id);
167 const AMRGraph &graph_neigh_zm = get_graph_neigh(Direction::zm, patch_id);
168
169 auto get_face_buf = [&](auto &neigh_link_field) -> const auto & {
170 return neigh_link_field.link_fields.get(patch_id).link_graph_field;
171 };
172
173 const auto &buf_flux_rho_face_xp = get_face_buf(edges.flux_rho_face_xp);
174 const auto &buf_flux_rho_face_xm = get_face_buf(edges.flux_rho_face_xm);
175 const auto &buf_flux_rho_face_yp = get_face_buf(edges.flux_rho_face_yp);
176 const auto &buf_flux_rho_face_ym = get_face_buf(edges.flux_rho_face_ym);
177 const auto &buf_flux_rho_face_zp = get_face_buf(edges.flux_rho_face_zp);
178 const auto &buf_flux_rho_face_zm = get_face_buf(edges.flux_rho_face_zm);
179 const auto &buf_flux_rhov_face_xp = get_face_buf(edges.flux_rhov_face_xp);
180 const auto &buf_flux_rhov_face_xm = get_face_buf(edges.flux_rhov_face_xm);
181 const auto &buf_flux_rhov_face_yp = get_face_buf(edges.flux_rhov_face_yp);
182 const auto &buf_flux_rhov_face_ym = get_face_buf(edges.flux_rhov_face_ym);
183 const auto &buf_flux_rhov_face_zp = get_face_buf(edges.flux_rhov_face_zp);
184 const auto &buf_flux_rhov_face_zm = get_face_buf(edges.flux_rhov_face_zm);
185
186 auto &block_cell_sizes = edges.spans_block_cell_sizes.get_spans().get(patch_id);
187 auto &cell0block_aabb_lower = edges.spans_cell0block_aabb_lower.get_spans().get(patch_id);
188
189 auto &dt_rho_patch = edges.spans_dtrho.get_spans().get(patch_id);
190 auto &dt_rhov_patch = edges.spans_dtrhov.get_spans().get(patch_id);
191
192 u32 cell_count = block_count * AMRBlock::block_size * ndust;
193
195 q,
196 sham::MultiRef{buf_flux_rho_face_xp, buf_flux_rho_face_xm, buf_flux_rho_face_yp,
197 buf_flux_rho_face_ym, buf_flux_rho_face_zp, buf_flux_rho_face_zm,
198 buf_flux_rhov_face_xp, buf_flux_rhov_face_xm, buf_flux_rhov_face_yp,
199 buf_flux_rhov_face_ym, buf_flux_rhov_face_zp, buf_flux_rhov_face_zm,
200 block_cell_sizes, cell0block_aabb_lower, graph_neigh_xp,
201 graph_neigh_xm, graph_neigh_yp, graph_neigh_ym,
202 graph_neigh_zp, graph_neigh_zm},
203 sham::MultiRef{dt_rho_patch, dt_rhov_patch},
204 cell_count,
205 KernelSumFluxDust<Tvec, TgridVec>{ndust, dxfact});
206 });
207}
208
Sum the fluxes into the time derivative fields for Dust.
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