Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeMonofluidTVAAddSourceTerm.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
18
20#include "shambackends/math.hpp"
21#include "shambackends/vec.hpp"
27#include <experimental/mdspan>
28
29#define NODE_EDGES(X_RO, X_RW) \
30 /* counts */ \
31 X_RO(shamrock::solvergraph::Indexes<u32>, part_counts) \
32 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, rhodust_eps) \
33 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, tau_frac) \
34 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, dt_hydro) \
35 \
36 /* fields */ \
37 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, S) \
38 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, s_j) \
39 \
40 /* outputs */ \
41 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, ds_j_dt)
42
44
45 template<class Tvec>
46 class NodeMonofluidTVAAddSourceTerm : public shamrock::solvergraph::INode {
47
48 using Tscal = shambase::VecComponent<Tvec>;
49
50 u32 nbins;
51
52 public:
53 NodeMonofluidTVAAddSourceTerm(u32 nbins) : nbins(nbins) {}
54
55 EXPAND_NODE_EDGES(NODE_EDGES)
56
58
60
61 auto edges = get_edges();
62
63 edges.S.check_sizes(edges.part_counts.indexes);
64 edges.s_j.check_sizes(edges.part_counts.indexes);
65 edges.ds_j_dt.check_sizes(edges.part_counts.indexes);
66
67 auto rhodust_eps = edges.rhodust_eps.data;
68
69 shambase::DistributedData<u32> counts = edges.part_counts.indexes.template map<u32>(
70 [nbins = this->nbins](u64 /**/, u32 count) -> u32 {
71 return count * nbins;
72 });
73
74 auto dt_hydro = edges.dt_hydro.data;
75 auto drho_min = edges.tau_frac.data * rhodust_eps;
76
78 shamsys::instance::get_compute_scheduler_ptr(),
79 sham::DDMultiRef{edges.S.get_spans(), edges.s_j.get_spans()},
80 sham::DDMultiRef{edges.ds_j_dt.get_spans()},
81 counts,
82 [drho_min, dt_hydro, nbins = this->nbins](
83 u32 id,
84 const Tscal *__restrict S,
85 const Tscal *__restrict s_j,
86 Tscal *__restrict ds_j_dt) {
87 Tscal src = S[id];
88
89 // no source, no contribution (and dt_eff below would be infinite)
90 if (src == 0) {
91 return;
92 }
93
94 Tscal sj = s_j[id];
95
96 // implicit step with a floor on dt: dividing by a tiny dt_hydro (e.g. right
97 // before a normal step) would give a very large derivative, that explodes at
98 // the next step when the leapfrog applies it over a larger dt.
99
100 // dt_eps = time to fill the fraction tau_frac of rho_eps at the current rate
101 Tscal dt_eps = drho_min / sham::abs(src);
102 Tscal dt_eff = sham::max(dt_hydro, dt_eps);
103 Tscal s_next = sycl::sqrt(sham::max(sj * sj + src * dt_eff, Tscal(0)));
104
105 ds_j_dt[id] += (s_next - sj) / dt_eff;
106 });
107 }
108
109 inline virtual std::string _impl_get_label() const {
110 return "NodeMonofluidTVAAddSourceTerm";
111 };
112
113 inline virtual std::string _impl_get_tex() const {
114 std::string tex = R"tex(
115 Monofluid TVA: dust-density source term $\rightarrow$ ${s_j}$ time derivative
116
117 Per gas particle $a$ and mass bin $j$ (monofluid: $\rho_{{\rm d},j,a} = {s_j}_{j,a}^2$):
118
119 \begin{align}
120 \rho_{{\rm d},j,a} &= {s_j}_{j,a}^2 \\
121 {S}_{j,a} &= \text{dust density source term } (\mathrm{d}\rho_{{\rm d},j,a}/\mathrm{d}t) \\
122 \Delta t_{{\rm eff},j,a} &= \max\bigl({dt_hydro}, {tau_frac}\, \rho_{\rm eps} / |{S}_{j,a}|\bigr) \\
123 \delta_{j,a} &= \Bigl(\sqrt{\max({s_j}_{j,a}^2 + {S}_{j,a} \Delta t_{{\rm eff},j,a}, 0)}
124 - {s_j}_{j,a}\Bigr) / \Delta t_{{\rm eff},j,a} \\
125 {ds_j_dt}_{j,a} &\mathrel{+}= \delta_{j,a}
126 \end{align}
127
128 Implicit in $\rho_{{\rm d},j,a}$: $\delta_{j,a}$ is the mean rate of ${s_j}_{j,a}$ over
129 $\Delta t_{\rm eff}$ when $\rho_{{\rm d},j,a}$ moves by ${S}_{j,a} \Delta t_{\rm eff}$
130 (clamped at $0$), which tends to ${S}_{j,a}/(2|{s_j}_{j,a}|)$ for small steps.
131 $\Delta t_{\rm eff}$ moves at least ${tau_frac}\, \rho_{\rm eps}$, so that a null or tiny
132 ${dt_hydro}$ cannot give a large rate for an empty bin.
133 ${S}_{j,a} = 0$ gives $\delta_{j,a} = 0$.
134
135 $a \in [0, {part_counts})$, $j \in [0, N_{\rm bins})$,
136 $\rho_{\rm eps} = {rhodust_eps}$, $N_{\rm bins} = {nbins}$
137 )tex";
138
139 replace_edges_tex_symbols(tex);
140
141 shambase::replace_all(tex, "{nbins}", sham::format("{}", nbins));
142
143 return tex;
144 }
145 };
146} // namespace shammodels::sph::modules
147
148#undef NODE_EDGES
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
Represents a collection of objects distributed across patches identified by a u64 id.
virtual std::string _impl_get_tex() const
get the tex of the node
virtual std::string _impl_get_label() const
get the label of the node
Inode is node between data edges, takes multiple inputs, multiple outputs.
Definition INode.hpp:31
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.
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:106
namespace for the sph model modules
#define __shamrock_stack_entry()
Macro to create a stack entry.
A variant of sham::MultiRef for distributed data.