Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeMonofluidTVASmoothSPositivityLimiter.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"
26
27#define NODE_EDGES(X_RO, X_RW) \
28 /* counts */ \
29 X_RO(shamrock::solvergraph::Indexes<u32>, part_counts) \
30 \
31 /* fields */ \
32 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, s_j) \
33 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, Ttilde_sj) \
34 \
35 /* outputs */ \
36 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, ds_j_dt)
37
39
40 template<class Tvec>
41 class NodeMonofluidTVASmoothSPositivityLimiter : public shamrock::solvergraph::INode {
42
43 using Tscal = shambase::VecComponent<Tvec>;
44
45 u32 ndust;
46
49 bool apply_xi;
50
51 public:
52 NodeMonofluidTVASmoothSPositivityLimiter(u32 ndust, bool apply_xi = true)
53 : ndust(ndust), apply_xi(apply_xi) {}
54
55 EXPAND_NODE_EDGES(NODE_EDGES)
56
58
60
61 auto edges = get_edges();
62
63 auto &part_counts = edges.part_counts.indexes;
64
65 edges.ds_j_dt.ensure_sizes(part_counts);
66
67 auto total_specie_count = part_counts.template map<u32>([&](u64 id, u32 count) {
68 return count * ndust;
69 });
70
72 shamsys::instance::get_compute_scheduler_ptr(),
73 sham::DDMultiRef{edges.s_j.get_spans(), edges.Ttilde_sj.get_spans()},
74 sham::DDMultiRef{edges.ds_j_dt.get_spans()},
75 total_specie_count,
76 [apply_xi = apply_xi](
77 u32 thread_id,
78 const Tscal *__restrict s_j,
79 const Tscal *__restrict Ttilde_sj,
80 Tscal *__restrict ds_j_dt) {
81 Tscal s_j_a = s_j[thread_id];
82 Tscal Ttilde_sj_a = Ttilde_sj[thread_id];
83 Tscal ds_j_dt_a = ds_j_dt[thread_id];
84
85 // if we dip in the negative range do not dip further
86 if (apply_xi) {
87 ds_j_dt_a *= (s_j_a < 0 && ds_j_dt_a < 0) ? 0 : 1;
88 }
89
90 // restore it slowly to 0
91 // (|Ttilde_sj| as Ttilde_sj can become negative, which would push s_j further
92 // into the negative range)
93 ds_j_dt_a += (s_j_a < 0) ? -s_j_a / (10 * sycl::fabs(Ttilde_sj_a)) : 0;
94
95 ds_j_dt[thread_id] = ds_j_dt_a;
96 });
97 }
98
99 inline virtual std::string _impl_get_label() const {
100 return apply_xi ? "NodeMonofluidTVASmoothSPositivityLimiter"
101 : "NodeMonofluidTVASmoothSPositivityLimiter (phi only)";
102 };
103
104 inline virtual std::string _impl_get_tex() const {
105 std::string tex = R"tex(
106 NodeMonofluidTVASmoothSPositivityLimiter
107
108 For gas particle $a$ and dust bin $j$:
109
110 \begin{align}
111 {xi_switch}{ds_j_dt}_{j,a} &\mathrel{+}=
112 \begin{cases}
113 -{s_j}_{j,a} / (10\, \lvert {Ttilde_sj}_{j,a} \rvert) & {s_j}_{j,a} < 0 \\
114 0 & \text{otherwise}
115 \end{cases}
116 \end{align}
117
118 $a \in [0,{part_counts})$, $j \in [0,{ndust})$.
119 )tex";
120
121 const std::string xi_switch = apply_xi ? R"tex({ds_j_dt}_{j,a} &\leftarrow
122 \begin{cases}
123 0 & {s_j}_{j,a} < 0 \land {ds_j_dt}_{j,a} < 0 \\
124 {ds_j_dt}_{j,a} & \text{otherwise}
125 \end{cases} \\
126 )tex"
127 : "";
128 shambase::replace_all(tex, "{xi_switch}", xi_switch);
129
130 replace_edges_tex_symbols(tex);
131
132 shambase::replace_all(tex, "{ndust}", sham::format("{}", ndust));
133
134 return tex;
135 }
136 };
137} // namespace shammodels::sph::modules
138
139#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
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.