Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeSelfGravityAcceleration.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
19#include "shambase/memory.hpp"
26#include <array>
27
28namespace {
29
31 using Direction = shammodels::basegodunov::modules::Direction;
32
34 template<class Tscal, class ACCField>
35 inline std::array<Tscal, 3> get_3d_minus_phi_grad(
36 const Tscal *cell_sizes,
37 const u32 block_size,
38 const u32 cell_global_id,
39 const AMRGraphLinkiterator &graph_iter_xp,
40 const AMRGraphLinkiterator &graph_iter_xm,
41 const AMRGraphLinkiterator &graph_iter_yp,
42 const AMRGraphLinkiterator &graph_iter_ym,
43 const AMRGraphLinkiterator &graph_iter_zp,
44 const AMRGraphLinkiterator &graph_iter_zm,
45 ACCField &&field_access) {
46
47 auto cur_cell_block_id = cell_global_id / block_size;
48
49 auto get_gradient_dir = [&](auto &graph_links, Direction dir) -> Tscal {
50 Tscal acc = 0;
51 auto cell_center_dist = cell_sizes[cur_cell_block_id];
52 Tscal fac = 1.;
53 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
54 auto neigh_block_id = id_b / block_size;
55
56 int sign = 1 - 2 * (dir % 2);
57 acc += sign * (field_access(id_b) - field_access(cell_global_id));
58
59 if (cell_sizes[neigh_block_id] > cell_sizes[cur_cell_block_id]) {
60 fac = (3. / 2.);
61 }
62 // This logic supposes that the last (4-th) cell at the interface has the same size
63 // as the other three cells. This is consistent with 2:1 refinement.
64 // TODO: extend to anisotropic mesh
65 if (cell_sizes[neigh_block_id] < cell_sizes[cur_cell_block_id]) {
66 fac = (3. / 4.);
67 }
68 });
69 return (cnt > 0) ? acc / (cell_center_dist * fac * cnt) : Tscal{0};
70 };
71
72 Tscal delta_xp = get_gradient_dir(graph_iter_xp, Direction::xp);
73 Tscal delta_xm = get_gradient_dir(graph_iter_xm, Direction::xm);
74 Tscal delta_yp = get_gradient_dir(graph_iter_yp, Direction::yp);
75 Tscal delta_ym = get_gradient_dir(graph_iter_ym, Direction::ym);
76 Tscal delta_zp = get_gradient_dir(graph_iter_zp, Direction::zp);
77 Tscal delta_zm = get_gradient_dir(graph_iter_zm, Direction::zm);
78
79 Tscal phi_gx = -0.5 * (delta_xp + delta_xm);
80 Tscal phi_gy = -0.5 * (delta_yp + delta_ym);
81 Tscal phi_gz = -0.5 * (delta_zp + delta_zm);
82
83 return {phi_gx, phi_gy, phi_gz};
84 }
85
86} // namespace
87
89
90 template<class Tvec, class TgridVec>
92 StackEntry stack_loc{};
93 auto edges = get_edges();
94
95 edges.spans_block_cell_sizes.check_sizes(edges.sizes.indexes);
96 edges.spans_phi.check_sizes(edges.sizes.indexes);
97 edges.spans_phi_g.ensure_sizes(edges.sizes.indexes);
98
99 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
100
101 edges.cell_neigh_graph.graph.for_each(
102 [&](u64 id, const OrientedAMRGraph<Tvec, TgridVec> &oriented_cell_graph) {
103 auto &cell_sizes_span = edges.spans_block_cell_sizes.get_spans().get(id);
104 auto &phi_span = edges.spans_phi.get_spans().get(id);
105 auto &phi_g_span = edges.spans_phi_g.get_spans().get(id);
106
107 auto get_graph = [&](Direction dir) -> const AMRGraph & {
108 return shambase::get_check_ref(oriented_cell_graph.graph_links[dir]);
109 };
110
111 const AMRGraph &graph_neigh_xp = get_graph(Direction::xp);
112 const AMRGraph &graph_neigh_xm = get_graph(Direction::xm);
113 const AMRGraph &graph_neigh_yp = get_graph(Direction::yp);
114 const AMRGraph &graph_neigh_ym = get_graph(Direction::ym);
115 const AMRGraph &graph_neigh_zp = get_graph(Direction::zp);
116 const AMRGraph &graph_neigh_zm = get_graph(Direction::zm);
117
118 u32 cell_count = edges.sizes.indexes.get(id) * block_size;
119
121 q,
123 cell_sizes_span,
124 phi_span,
125 graph_neigh_xp,
126 graph_neigh_xm,
127 graph_neigh_yp,
128 graph_neigh_ym,
129 graph_neigh_zp,
130 graph_neigh_zm},
131 sham::MultiRef{phi_g_span},
132 cell_count,
133 [block_size = block_size](
134 u32 cell_global_id,
135 const Tscal *__restrict cell_sizes,
136 const Tscal *__restrict phi,
137 const AMRGraph::ro_access graph_iter_xp,
138 const AMRGraph::ro_access graph_iter_xm,
139 const AMRGraph::ro_access graph_iter_yp,
140 const AMRGraph::ro_access graph_iter_ym,
141 const AMRGraph::ro_access graph_iter_zp,
142 const AMRGraph::ro_access graph_iter_zm,
143 Tvec *__restrict phi_g) {
144 auto g = get_3d_minus_phi_grad<Tscal>(
145 cell_sizes,
146 block_size,
147 cell_global_id,
148 graph_iter_xp,
149 graph_iter_xm,
150 graph_iter_yp,
151 graph_iter_ym,
152 graph_iter_zp,
153 graph_iter_zm,
154 [=](u32 id) {
155 return phi[id];
156 });
157 phi_g[cell_global_id] = {g[0], g[1], g[2]};
158 });
159 });
160 }
161
162 template<class Tvec, class TgridVec>
164 std::string tex = R"tex(
165 Self-gravity acceleration
166 \begin{equation}
167 {spans_phi_g}_i = -(\nabla {spans_phi})_i
168 \end{equation}
169 )tex";
170
171 replace_edges_tex_symbols(tex);
172
173 return tex;
174 }
175
176} // namespace shammodels::basegodunov::modules
177
Header file describing a Node Instance.
Self-gravity acceleration on the AMR grid.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
Compute the gravitational acceleration of every cell.
virtual std::string _impl_get_tex() const
get the tex of the node
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:112
namespace for the basegodunov model modules
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33