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
27#include "shambase/assert.hpp"
29#include <experimental/mdspan>
30#include <concepts>
31
32namespace shamphys {
33
53 template<class T>
54 inline void compute_gij_k0(
55 auto &&rho_dust,
56 T rho_eps,
57 shambase::is_mdspan_rank<1> auto massgrid,
59 requires requires(decltype(rho_dust) rd, int j) {
60 { rd(j) } -> std::same_as<T>;
61 }
62 {
63
64 SHAM_ASSERT(massgrid.extent(0) == gij.extent(0) + 1);
65
66 for (std::size_t j = 0; j < gij.extent(0); ++j) {
67 T rho_d = rho_dust(j);
68 gij(j) = (rho_d > rho_eps) ? rho_d / (massgrid[j + 1] - massgrid[j]) : 0;
69 }
70 }
71
103 template<class Func>
104 requires requires(Func f, int a, int b) {
105 { f(a, b) };
106 }
108 int nbins,
110 shambase::is_mdspan_rank<3> auto tensor_tabflux_coag,
111 Func &&dv,
112 shambase::is_mdspan_rank<1> auto flux) {
113
114 SHAM_ASSERT(gij.extent(0) == nbins);
115 SHAM_ASSERT(flux.extent(0) == nbins);
116 SHAM_ASSERT(tensor_tabflux_coag.extent(0) == nbins);
117 SHAM_ASSERT(tensor_tabflux_coag.extent(1) == nbins);
118 SHAM_ASSERT(tensor_tabflux_coag.extent(2) == nbins);
119
120 /*
121 * Python version:
122 * flux = np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv, gij, gij)
123 */
124
125 for (int j = 0; j < nbins; ++j) {
126 double sum = 0.0;
127 for (int l = 0; l < nbins; ++l) {
128 for (int m = 0; m < nbins; ++m) {
129 sum += tensor_tabflux_coag(j, l, m) * dv(l, m) * gij[l] * gij[m];
130 }
131 }
132 flux[j] = sum;
133 }
134 }
135
154
155 SHAM_ASSERT(flux.extent(0) == S_coag.extent(0));
156
157 S_coag(0) = -flux(0);
158 for (int j = 1; j < flux.extent(0); ++j) {
159 S_coag(j) = flux(j - 1) - flux(j);
160 }
161 }
162
163 template<class T, class FuncDv, class FuncRhoDust>
164 void coala_k0_source_term(
165 int nbins,
166 /* inputs */
167 FuncDv &&dv,
168 FuncRhoDust &&rho_dust,
169 T rho_eps,
170 shambase::is_mdspan_rank<1> auto massgrid,
171 /* COALA inputs */
172 shambase::is_mdspan_rank<3> auto tabflux_coag,
173 /* internal */
176 /* output */
177 shambase::is_mdspan_rank<1> auto S_coag) {
178
179 // init the gij coefficients
180 shamphys::compute_gij_k0(rho_dust, rho_eps, massgrid, gij);
181
182 // compute flux for all dust bins
183 shamphys::compute_flux_coag_k0_kdv(nbins, gij, tabflux_coag, dv, flux);
184
185 // compute flux diff and store result
186 shamphys::coala_flux_diff(flux, S_coag);
187 }
188
189} // namespace shamphys
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
Definition assert.hpp:67
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 ( ).