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
47 public:
48 NodeMonofluidTVASmoothSPositivityLimiter(u32 ndust) : ndust(ndust) {}
49
50 EXPAND_NODE_EDGES(NODE_EDGES)
51
53
55
56 auto edges = get_edges();
57
58 auto &part_counts = edges.part_counts.indexes;
59
60 edges.ds_j_dt.ensure_sizes(part_counts);
61
62 auto total_specie_count = part_counts.template map<u32>([&](u64 id, u32 count) {
63 return count * ndust;
64 });
65
67 shamsys::instance::get_compute_scheduler_ptr(),
68 sham::DDMultiRef{edges.s_j.get_spans(), edges.Ttilde_sj.get_spans()},
69 sham::DDMultiRef{edges.ds_j_dt.get_spans()},
70 total_specie_count,
71 [](u32 thread_id,
72 const Tscal *__restrict s_j,
73 const Tscal *__restrict Ttilde_sj,
74 Tscal *__restrict ds_j_dt) {
75 Tscal s_j_a = s_j[thread_id];
76 Tscal Ttilde_sj_a = Ttilde_sj[thread_id];
77 Tscal ds_j_dt_a = ds_j_dt[thread_id];
78
79 // if we dip in the negative range do not dip further
80 ds_j_dt_a *= (s_j_a < 0 && ds_j_dt_a < 0) ? 0 : 1;
81
82 // restore it slowly to 0
83 ds_j_dt_a += (s_j_a < 0) ? -s_j_a / (10 * Ttilde_sj_a) : 0;
84
85 ds_j_dt[thread_id] = ds_j_dt_a;
86 });
87 }
88
89 inline virtual std::string _impl_get_label() const {
90 return "NodeMonofluidTVASmoothSPositivityLimiter";
91 };
92
93 inline virtual std::string _impl_get_tex() const {
94
95 auto part_counts = get_ro_edge_base(0).get_tex_symbol();
96 auto s_j = get_ro_edge_base(1).get_tex_symbol();
97 auto Ttilde_sj = get_ro_edge_base(2).get_tex_symbol();
98 auto ds_j_dt = get_rw_edge_base(0).get_tex_symbol();
99
100 std::string tex = R"tex(
101 NodeMonofluidTVASmoothSPositivityLimiter
102
103 For gas particle $a$ and dust bin $j$:
104
105 \begin{align}
106 {ds_j_dt}_{j,a} &\leftarrow
107 \begin{cases}
108 0 & {s_j}_{j,a} < 0 \land {ds_j_dt}_{j,a} < 0 \\
109 {ds_j_dt}_{j,a} & \text{otherwise}
110 \end{cases} \\
111 {ds_j_dt}_{j,a} &\mathrel{+}=
112 \begin{cases}
113 -{s_j}_{j,a} / (10\, {Ttilde_sj}_{j,a}) & {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 shambase::replace_all(tex, "{part_counts}", part_counts);
122 shambase::replace_all(tex, "{s_j}", s_j);
123 shambase::replace_all(tex, "{Ttilde_sj}", Ttilde_sj);
124 shambase::replace_all(tex, "{ds_j_dt}", ds_j_dt);
125 shambase::replace_all(tex, "{ndust}", shambase::format("{}", ndust));
126
127 return tex;
128 }
129 };
130} // namespace shammodels::sph::modules
131
132#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
IEdge & get_rw_edge_base(int slot)
Get a reference to a read write edge and cast it to the type IEdge.
Definition INode.hpp:137
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:128
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:110
namespace for the sph model modules
#define __shamrock_stack_entry()
Macro to create a stack entry.
A variant of sham::MultiRef for distributed data.