Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
coala_interface.hpp
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
10#pragma once
11
26
28#include "shambase/assert.hpp"
30#include <experimental/mdspan>
31#include <concepts>
32#include <vector>
33
34namespace shamphys {
35
55 template<class T>
56 inline void compute_gij_k0(
57 auto &&rho_dust,
58 T rho_eps,
59 shambase::is_mdspan_rank<1> auto massgrid,
61 requires requires(decltype(rho_dust) rd, int j) {
62 { rd(j) } -> std::same_as<T>;
63 }
64 {
65
66 SHAM_ASSERT(massgrid.extent(0) == gij.extent(0) + 1);
67
68 for (std::size_t j = 0; j < gij.extent(0); ++j) {
69 T rho_d = rho_dust(j);
70 gij(j) = (rho_d > rho_eps) ? rho_d / (massgrid[j + 1] - massgrid[j]) : 0;
71 }
72 }
73
105 template<class Func>
106 requires requires(Func f, int a, int b) {
107 { f(a, b) };
108 }
110 int nbins,
112 shambase::is_mdspan_rank<3> auto tensor_tabflux_coag,
113 Func &&dv,
114 shambase::is_mdspan_rank<1> auto flux) {
115
116 SHAM_ASSERT(gij.extent(0) == nbins);
117 SHAM_ASSERT(flux.extent(0) == nbins);
118 SHAM_ASSERT(tensor_tabflux_coag.extent(0) == nbins);
119 SHAM_ASSERT(tensor_tabflux_coag.extent(1) == nbins);
120 SHAM_ASSERT(tensor_tabflux_coag.extent(2) == nbins);
121
122 // initialize flux to 0
123 for (int j = 0; j < nbins; ++j) {
124 flux[j] = 0;
125 }
126
127 /*
128 * Python version:
129 * flux = np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv, gij, gij)
130 */
131
132 for (int l = 0; l < nbins; ++l) {
133 for (int m = 0; m < nbins; ++m) {
134 auto term = dv(l, m) * gij[l] * gij[m];
135 for (int j = 0; j < nbins; ++j) {
136 flux[j] += tensor_tabflux_coag(j, l, m) * term;
137 }
138 }
139 }
140 }
141
155 template<class T, class Tidx = u32>
158 std::vector<Tidx> pair_offset;
160 std::vector<Tidx> pair_jmin;
162 std::vector<T> values;
163 };
164
168 template<class T, class Tidx = u32>
170 Tidx nbins;
171 const Tidx *pair_offset;
172 const Tidx *pair_jmin;
173 const T *values;
174
177 struct PairEntries {
178 const T *values;
179 Tidx jmin;
180 Tidx count;
181
183 inline bool is_empty() const { return count == 0; }
184
186 template<class Func>
187 inline void for_each_sparse(Func &&func) const {
188 for (Tidx k = 0; k < count; ++k) {
189 func(jmin + k, values[k]);
190 }
191 }
192 };
193
195 inline PairEntries get_entries(Tidx l, Tidx m) const {
196 Tidx p = l * nbins + m;
197 Tidx beg = pair_offset[p];
198 return PairEntries{values + beg, pair_jmin[p], pair_offset[p + 1] - beg};
199 }
200 };
201
209 template<class T, class Tidx = u32>
211 int nbins, shambase::is_mdspan_rank<3> auto tensor_tabflux_coag) {
212
213 SHAM_ASSERT(tensor_tabflux_coag.extent(0) == nbins);
214 SHAM_ASSERT(tensor_tabflux_coag.extent(1) == nbins);
215 SHAM_ASSERT(tensor_tabflux_coag.extent(2) == nbins);
216
218 ret.pair_offset.push_back(0);
219
220 for (int l = 0; l < nbins; ++l) {
221 for (int m = 0; m < nbins; ++m) {
222 auto tab = [&](int j) -> T {
223 return tensor_tabflux_coag(j, l, m);
224 };
225
226 int jmin = 0;
227 int jend = nbins;
228 while (jmin < jend && tab(jmin) == 0) {
229 ++jmin;
230 }
231 while (jend > jmin && tab(jend - 1) == 0) {
232 --jend;
233 }
234
235 for (int j = jmin; j < jend; ++j) {
236 ret.values.push_back(tab(j));
237 }
238 ret.pair_jmin.push_back(jmin);
239 ret.pair_offset.push_back(ret.values.size());
240 }
241 }
242
243 return ret;
244 }
245
258 template<class T, class Tidx, class Func>
259 requires requires(Func f, int a, int b) {
260 { f(a, b) };
261 }
263 int nbins,
266 Func &&dv,
267 shambase::is_mdspan_rank<1> auto flux) {
268
269 SHAM_ASSERT(gij.extent(0) == nbins);
270 SHAM_ASSERT(flux.extent(0) == nbins);
271
272 for (int j = 0; j < nbins; ++j) {
273 flux[j] = 0;
274 }
275
276 for (int l = 0; l < nbins; ++l) {
277 for (int m = 0; m < nbins; ++m) {
278 auto entries = tabflux.get_entries(l, m);
279
280 // skip the pairs without entries before evaluating dv
281 if (entries.is_empty()) {
282 continue;
283 }
284
285 auto term = dv(l, m) * gij[l] * gij[m];
286 entries.for_each_sparse([&](Tidx j, T val) {
287 flux[j] += val * term;
288 });
289 }
290 }
291 }
292
311
312 SHAM_ASSERT(flux.extent(0) == S_coag.extent(0));
313
314 S_coag(0) = -flux(0);
315 for (int j = 1; j < flux.extent(0); ++j) {
316 S_coag(j) = flux(j - 1) - flux(j);
317 }
318 }
319
320 template<class T, class FuncDv, class FuncRhoDust>
321 void coala_k0_source_term(
322 int nbins,
323 /* inputs */
324 FuncDv &&dv,
325 FuncRhoDust &&rho_dust,
326 T rho_eps,
327 shambase::is_mdspan_rank<1> auto massgrid,
328 /* COALA inputs (dense rank-3 mdspan or TabfluxCoagK0SparseView) */
329 auto tabflux_coag,
330 /* internal */
333 /* output */
334 shambase::is_mdspan_rank<1> auto S_coag) {
335
336 // init the gij coefficients
337 shamphys::compute_gij_k0(rho_dust, rho_eps, massgrid, gij);
338
339 // compute flux for all dust bins
340 shamphys::compute_flux_coag_k0_kdv(nbins, gij, tabflux_coag, dv, flux);
341
342 // compute flux diff and store result
343 shamphys::coala_flux_diff(flux, S_coag);
344 }
345
346} // namespace shamphys
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
Definition assert.hpp:67
TabfluxCoagK0Sparse< T, Tidx > make_tabflux_coag_k0_sparse(int nbins, shambase::is_mdspan_rank< 3 > auto tensor_tabflux_coag)
Build the TabfluxCoagK0Sparse form of tensor_tabflux_coag.
void compute_flux_coag_k0_kdv(int nbins, shambase::is_mdspan_rank< 1 > auto gij, shambase::is_mdspan_rank< 3 > auto tensor_tabflux_coag, Func &&dv, shambase::is_mdspan_rank< 1 > auto flux)
Coagulation flux at bin right edges for a ballistic kernel ( ).
void coala_flux_diff(shambase::is_mdspan_rank< 1 > auto flux, shambase::is_mdspan_rank< 1 > auto S_coag)
Convert interface fluxes to a mass-bin coagulation source term.
void compute_gij_k0(auto &&rho_dust, T rho_eps, shambase::is_mdspan_rank< 1 > auto massgrid, shambase::is_mdspan_rank< 1 > auto gij)
Build coefficients on the piecewise-constant DG basis ( ).
void for_each_sparse(Func &&func) const
Call func(j, tensor_tabflux_coag[j,l,m]) for each stored entry of the pair.
bool is_empty() const
Whether the pair has no stored entry (it does not contribute to the flux).
Device view of a TabfluxCoagK0Sparse (see its documentation for the layout).
PairEntries get_entries(Tidx l, Tidx m) const
Stored entries of the pair .
Sparse storage of tensor_tabflux_coag ( ).
std::vector< T > values
Stored entries.
std::vector< Tidx > pair_offset
Offset in values of each pair, size nbins^2 + 1.
std::vector< Tidx > pair_jmin
First bin stored for each pair, size nbins^2.