Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeMonofluidTVADustDensityClamp.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
19#include "shambase/string.hpp"
21#include "shambackends/vec.hpp"
28
29#define NODE_EDGES(X_RO, X_RW) \
30 /* counts */ \
31 X_RO(shamrock::solvergraph::Indexes<u32>, part_counts) \
32 \
33 /* scalars */ \
34 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, gpart_mass) \
35 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
36 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, clamp_frac) \
37 \
38 /* fields */ \
39 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, hpart) \
40 \
41 /* inout */ \
42 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, s_j)
43
45
46 template<class Tvec>
47 class NodeMonofluidTVADustDensityClamp : public shamrock::solvergraph::INode {
48
49 using Tscal = shambase::VecComponent<Tvec>;
50
51 u32 ndust;
52
53 public:
54 NodeMonofluidTVADustDensityClamp(u32 ndust) : ndust(ndust) {}
55
56 EXPAND_NODE_EDGES(NODE_EDGES)
57
59
61
62 auto edges = get_edges();
63
64 Tscal pmass = edges.gpart_mass.data;
65 Tscal hfactd = edges.hfactd.data;
66 Tscal clamp_frac = edges.clamp_frac.data;
67
69 shamsys::instance::get_compute_scheduler_ptr(),
70 sham::DDMultiRef{edges.hpart.get_spans()},
71 sham::DDMultiRef{edges.s_j.get_spans()},
72 edges.part_counts.indexes,
73 [pmass, hfactd, clamp_frac, ndust = this->ndust](
74 u32 id_a, const Tscal *__restrict hpart, Tscal *__restrict s_j) {
75 u32 id_a_d = id_a * ndust;
76
77 Tscal h_a = hpart[id_a];
78 Tscal rho_a = shamrock::sph::rho_h(pmass, h_a, hfactd);
79 Tscal eps_max = clamp_frac;
80
81 // pass 1 : clamp each species individually, accumulate the (post-clamp) sum
82 Tscal eps_sum = 0;
83 for (u32 j = 0; j < ndust; j++) {
84 Tscal sj = s_j[id_a_d + j];
85 Tscal eps_j = sj * sj / rho_a;
86 if (eps_j > eps_max) {
87 eps_j = eps_max;
88 s_j[id_a_d + j] = sycl::sqrt(eps_j * rho_a);
89 }
90 eps_sum += eps_j;
91 }
92
93 // pass 2 : if the summed dust fraction still exceeds the threshold, scale
94 // every species down by the same factor so the sum lands at the threshold
95 if (eps_sum > eps_max && eps_sum > 0) {
96 Tscal scale = eps_max / eps_sum;
97 for (u32 j = 0; j < ndust; j++) {
98 Tscal sj = s_j[id_a_d + j];
99 Tscal eps_j = sj * sj / rho_a;
100 Tscal eps_j_sc = eps_j * scale;
101 s_j[id_a_d + j] = sycl::sqrt(eps_j_sc * rho_a);
102 }
103 }
104 });
105 }
106
107 inline virtual std::string _impl_get_label() const {
108 return "NodeMonofluidTVADustDensityClamp";
109 };
110
111 inline virtual std::string _impl_get_tex() const {
112 std::string tex = R"tex(
113 NodeMonofluidTVADustDensityClamp
114
115 For gas particle $a$ and dust bin $j$, with
116 $\rho_a = \rho({hpart}_a)$ the total density implied by the
117 smoothing length and $f = {clamp_frac}$ the clamp fraction
118 (the maximum allowed dust-to-gas ratio $\epsilon_{\max}$):
119
120 \begin{align}
121 \epsilon_{j,a} &= \min\left({s_j}_{j,a}^2 / \rho_a,\ f\right) \\
122 \epsilon_a &= \sum_j \epsilon_{j,a} \\
123 \epsilon_{j,a} &\leftarrow
124 \begin{cases}
125 \epsilon_{j,a} \cdot f / \epsilon_a
126 & \epsilon_a > f \\
127 \epsilon_{j,a} & \text{otherwise}
128 \end{cases} \\
129 {s_j}_{j,a} &\leftarrow \sqrt{\epsilon_{j,a}\, \rho_a}
130 \end{align}
131
132 $a \in [0,{part_counts})$, $j \in [0,{ndust})$.
133
134 $m = {gpart_mass}$, $h_{{\rm fact}} = {hfactd}$.
135 )tex";
136
137 replace_edges_tex_symbols(tex);
138
139 shambase::replace_all(tex, "{ndust}", sham::format("{}", ndust));
140
141 return tex;
142 }
143 };
144} // namespace shammodels::sph::modules
145
146#undef NODE_EDGES
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
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.