Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ComputeGravWave.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
17
28
29namespace shammodels::common::modules {
30
31 namespace {
32
33 // 3x3 mdspan view over a fixed-size array
34 template<class Tscal>
35 using Mat3 = std::mdspan<Tscal, std::extents<std::size_t, 3, 3>>;
36
37 } // namespace
38
39 template<class Tvec>
41
43
44 auto edges = get_edges();
45
46 edges.spans_positions.check_sizes(edges.sizes.indexes);
47 edges.spans_velocities.check_sizes(edges.sizes.indexes);
48 edges.spans_accelerations.check_sizes(edges.sizes.indexes);
49 edges.spans_masses.check_sizes(edges.sizes.indexes);
50 edges.spans_accel_ext.check_sizes(edges.sizes.indexes);
51
52 const Tvec x0 = edges.central_pos.data;
53 const Tvec v0 = edges.central_vel.data;
54 const Tvec a0 = edges.central_acc.data;
55 const Tscal fac = edges.gw_prefactor.data;
56 const Tscal theta_deg = edges.theta_gw.data;
57 const Tscal phi_deg = edges.phi_gw.data;
58
59 constexpr Tscal pi = shambase::constants::pi<Tscal>;
60
61 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
62
63 // thought you could pass a DeviceBuffer to distributed_data_kernel_call through Multiref ?
64 // guess NOT motherfcker
65 // sham::DeviceBuffer<Tscal> ddq0{npart, dev_sched};
66
67 auto make_ddq = [&]() {
68 return edges.sizes.indexes.template map<sham::DeviceBuffer<Tscal>>(
69 [&](u64 id, const u32 &n) {
70 return sham::DeviceBuffer<Tscal>{n, dev_sched};
71 });
72 };
73
80
82 dev_sched,
84 edges.spans_positions.get_spans(),
85 edges.spans_velocities.get_spans(),
86 edges.spans_accelerations.get_spans(),
87 edges.spans_masses.get_spans(),
88 edges.spans_accel_ext.get_spans()},
89 sham::DDMultiRef{ddq0, ddq1, ddq2, ddq3, ddq4, ddq5},
90 edges.sizes.indexes,
91 [x0, v0, a0](
92 u32 gid,
93 const Tvec *xyz,
94 const Tvec *vxyz,
95 const Tvec *axyz,
96 const Tscal *mass,
97 const Tvec *axyz_ext,
98 Tscal *ddq0,
99 Tscal *ddq1,
100 Tscal *ddq2,
101 Tscal *ddq3,
102 Tscal *ddq4,
103 Tscal *ddq5) {
104 const Tscal m = mass[gid];
105
106 const Tscal x = xyz[gid][0] - x0[0];
107 const Tscal y = xyz[gid][1] - x0[1];
108 const Tscal z = xyz[gid][2] - x0[2];
109 const Tscal vx = vxyz[gid][0] - v0[0];
110 const Tscal vy = vxyz[gid][1] - v0[1];
111 const Tscal vz = vxyz[gid][2] - v0[2];
112
113 // normally axyz should have axyzext added to it already
114 const Tscal ax = axyz[gid][0] - a0[0];
115 const Tscal ay = axyz[gid][1] - a0[1];
116 const Tscal az = axyz[gid][2] - a0[2];
117
118 ddq0[gid] = m * (Tscal(2.) * vx * vx + x * ax + x * ax);
119 ddq1[gid] = m * (Tscal(2.) * vx * vy + x * ay + y * ax);
120 ddq2[gid] = m * (Tscal(2.) * vx * vz + x * az + z * ax);
121 ddq3[gid] = m * (Tscal(2.) * vy * vy + y * ay + y * ay);
122 ddq4[gid] = m * (Tscal(2.) * vy * vz + y * az + z * ay);
123 ddq5[gid] = m * (Tscal(2.) * vz * vz + z * az + z * az);
124 });
125
126 // Tscal ddq0_per_rank = shamalgs::primitives::sum(dev_sched, ddq0, 0, npart);
127
128 Tscal ddq0_per_rank = 0, ddq1_per_rank = 0, ddq2_per_rank = 0, ddq3_per_rank = 0,
129 ddq4_per_rank = 0, ddq5_per_rank = 0;
130
131 edges.sizes.indexes.for_each([&](u64 id, const u32 &n) {
132 ddq0_per_rank += shamalgs::primitives::sum(dev_sched, ddq0.get(id), 0, n);
133 ddq1_per_rank += shamalgs::primitives::sum(dev_sched, ddq1.get(id), 0, n);
134 ddq2_per_rank += shamalgs::primitives::sum(dev_sched, ddq2.get(id), 0, n);
135 ddq3_per_rank += shamalgs::primitives::sum(dev_sched, ddq3.get(id), 0, n);
136 ddq4_per_rank += shamalgs::primitives::sum(dev_sched, ddq4.get(id), 0, n);
137 ddq5_per_rank += shamalgs::primitives::sum(dev_sched, ddq5.get(id), 0, n);
138 });
139
140 edges.ddq.data[0] = shamalgs::collective::allreduce_sum(ddq0_per_rank);
141 edges.ddq.data[1] = shamalgs::collective::allreduce_sum(ddq1_per_rank);
142 edges.ddq.data[2] = shamalgs::collective::allreduce_sum(ddq2_per_rank);
143 edges.ddq.data[3] = shamalgs::collective::allreduce_sum(ddq3_per_rank);
144 edges.ddq.data[4] = shamalgs::collective::allreduce_sum(ddq4_per_rank);
145 edges.ddq.data[5] = shamalgs::collective::allreduce_sum(ddq5_per_rank);
146
147 std::array<Tscal, 9> Q_arr{};
148 Mat3<Tscal> Q(Q_arr.data());
149 Q(0, 0) = edges.ddq.data[0];
150 Q(0, 1) = Q(1, 0) = edges.ddq.data[1];
151 Q(0, 2) = Q(2, 0) = edges.ddq.data[2];
152 Q(1, 1) = edges.ddq.data[3];
153 Q(1, 2) = Q(2, 1) = edges.ddq.data[4];
154 Q(2, 2) = edges.ddq.data[5];
155
156 std::array<Tscal, 9> ddq_xy_arr{};
157 Mat3<Tscal> ddq_xy(ddq_xy_arr.data());
158
159 const bool rotate = std::abs(theta_deg) > static_cast<Tscal>(1e-30);
160 if (rotate) {
161 const Tscal lam = theta_deg * pi / static_cast<Tscal>(180);
162 const Tscal c = std::cos(lam);
163 const Tscal s = std::sin(lam);
164
165 std::array<Tscal, 9> R_arr
166 = {c, Tscal(0), s, Tscal(0), Tscal(1), Tscal(0), -s, Tscal(0), c};
167 Mat3<Tscal> R(R_arr.data());
168
169 std::array<Tscal, 9> inter_arr{};
170 Mat3<Tscal> inter(inter_arr.data());
171 for (std::size_t i = 0; i < 3; ++i) {
172 for (std::size_t j = 0; j < 3; ++j) {
173 Tscal sum = Tscal(0);
174 for (std::size_t k = 0; k < 3; ++k) {
175 sum += Q(i, k) * R(k, j);
176 }
177 inter(i, j) = sum;
178 }
179 }
180
181 for (std::size_t i = 0; i < 3; ++i) {
182 for (std::size_t j = 0; j < 3; ++j) {
183 Tscal sum = Tscal(0);
184 for (std::size_t k = 0; k < 3; ++k) {
185 sum += R(k, i) * inter(k, j);
186 }
187 ddq_xy(i, j) = sum;
188 }
189 }
190 } else {
191 for (std::size_t i = 0; i < 9; ++i) {
192 ddq_xy_arr[i] = Q_arr[i];
193 }
194 }
195
196 // h+ / hx
197 const Tscal phi = phi_deg * pi / static_cast<Tscal>(180);
198 const Tscal sinphi = std::sin(phi);
199 const Tscal cosphi = std::cos(phi);
200 const Tscal sinphi2 = sinphi * sinphi;
201 const Tscal cosphi2 = cosphi * cosphi;
202 const Tscal sin2phi = std::sin(Tscal(2) * phi);
203 const Tscal cos2phi = std::cos(Tscal(2) * phi);
204
205 std::array<Tscal, 4> hx_out{};
206 std::array<Tscal, 4> hp_out{};
207
208 for (u32 i = 0; i < 4; ++i) {
209 const Tscal eta = static_cast<Tscal>(i) * pi / static_cast<Tscal>(6);
210 const Tscal sineta = std::sin(eta);
211 const Tscal coseta = std::cos(eta);
212 const Tscal sineta2 = sineta * sineta;
213 const Tscal coseta2 = coseta * coseta;
214 const Tscal sin2eta = std::sin(Tscal(2) * eta);
215
216 hp_out[i] = fac
217 * (ddq_xy(0, 0) * (cosphi2 - sinphi2 * coseta2)
218 + ddq_xy(1, 1) * (sinphi2 - cosphi2 * coseta2) - ddq_xy(2, 2) * sineta2
219 - ddq_xy(0, 1) * sin2phi * (Tscal(1) + coseta2)
220 + ddq_xy(0, 2) * sinphi * sin2eta + ddq_xy(1, 2) * cosphi * sin2eta);
221
222 hx_out[i] = Tscal(2) * fac
223 * (Tscal(0.5) * (ddq_xy(0, 0) - ddq_xy(1, 1)) * sin2phi * coseta
224 + ddq_xy(0, 1) * cos2phi * coseta - ddq_xy(0, 2) * cosphi * sineta
225 + ddq_xy(1, 2) * sinphi * sineta);
226 }
227
228 edges.ddq_xy.data = ddq_xy_arr;
229 edges.hx.data = hx_out;
230 edges.hp.data = hp_out;
231 }
232
233 template<class Tvec>
234 inline std::string ComputeGravWave<Tvec>::_impl_get_tex() const {
235 std::string tex = R"tex(
236 Gravitational-wave strain from the quadrupole formula
237 (Toscani et al. 2021)
238
239 \begin{align}
240 r_i &= {spans_positions}_i - {central_pos}_i\\
241 v_i &= {spans_velocities}_i - {central_vel}_i\\
242 a_i &= {spans_accelerations}_i - {central_acc}_i + {spans_accel_ext}_i\\
243 \ddot Q_{ij} &= \sum_p {spans_masses}_p
244 \left(2 v_i v_j + r_i a_j + r_j a_i\right)\\
245 R &= \begin{pmatrix}
246 \cos\lambda & 0 & \sin\lambda\\
247 0 & 1 & 0\\
248 -\sin\lambda & 0 & \cos\lambda
249 \end{pmatrix},\quad
250 \lambda = {theta_gw}\,\frac{\pi}{180}\\
251 \ddot Q^{xy} &= R^{T}\,\ddot Q\,R\\
252 h_+(\eta,\phi) &= {gw_prefactor}\,
253 \Big[\ddot Q^{xy}_{11}(\cos^2\phi - \sin^2\phi\cos^2\eta)
254 + \ddot Q^{xy}_{22}(\sin^2\phi - \cos^2\phi\cos^2\eta)
255 - \ddot Q^{xy}_{33}\sin^2\eta\\
256 &\qquad - \ddot Q^{xy}_{12}\sin 2\phi\,(1 + \cos^2\eta)
257 + \ddot Q^{xy}_{13}\sin\phi\,\sin 2\eta
258 + \ddot Q^{xy}_{23}\cos\phi\,\sin 2\eta\Big]\\
259 h_\times(\eta,\phi) &= 2\,{gw_prefactor}\,
260 \Big[\tfrac12(\ddot Q^{xy}_{11} - \ddot Q^{xy}_{22})\sin 2\phi\,\cos\eta\\
261 &\qquad + \ddot Q^{xy}_{12}\cos 2\phi\,\cos\eta
262 - \ddot Q^{xy}_{13}\cos\phi\,\sin\eta
263 + \ddot Q^{xy}_{23}\sin\phi\,\sin\eta\Big]
264 \end{align}
265
266 Evaluated at $\eta = 0,\ \pi/6,\ \pi/3,\ \pi/2$
267 with $\phi = {phi_gw}^\circ$, giving {hx} and {hp}.
268 )tex";
269
270 replace_edges_tex_symbols(tex);
271
272 return tex;
273 }
274
276
277} // namespace shammodels::common::modules
Compute the gravitational wave quadrupole. Based on Toscani et. al. 2021.
Header file describing a Node Instance.
Header file for the patch struct and related function.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
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.
virtual std::string _impl_get_tex() const
get the tex of the node
Class holding the value of numerous constants generated from the following source.
void distributed_data_kernel_call(sham::DeviceScheduler_ptr dev_sched, RefIn in, RefOut in_out, const shambase::DistributedData< index_t > &thread_counts, Functor &&func)
A variant of sham::kernel_call for distributed data.
T sum(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of elements in a device buffer within a specified range.
#define __shamrock_stack_entry()
Macro to create a stack entry.
A variant of sham::MultiRef for distributed data.