Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
MonoFluidTVIDeltav.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/vec.hpp"
27
28#define NODE_EDGES(X_RO, X_RW) \
29 /* scalars */ \
30 X_RO(shamrock::solvergraph::ScalarEdge<Tscal>, gpart_mass) \
31 \
32 /* counts */ \
33 X_RO(shamrock::solvergraph::Indexes<u32>, part_counts) \
34 \
35 /* fields */ \
36 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, hpart) \
37 X_RO(shamrock::solvergraph::IFieldSpan<Tvec>, grad_P_on_rho) \
38 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, s_j) \
39 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, t_j) \
40 \
41 /* outputs */ \
42 X_RW(shamrock::solvergraph::IFieldSpan<Tvec>, delta_v)
43
45
46 template<class Tvec, template<class> class SPHKernel>
47 class MonoFluidTVIDeltav : public shamrock::solvergraph::INode {
48
49 using Tscal = shambase::VecComponent<Tvec>;
50 using Kernel = SPHKernel<Tscal>;
51
52 u32 ndust;
53
54 public:
55 MonoFluidTVIDeltav(u32 ndust) : ndust(ndust) {}
56
57 EXPAND_NODE_EDGES(NODE_EDGES)
58
60
62
63 auto edges = get_edges();
64
65 auto &part_counts = edges.part_counts.indexes;
66
67 // check that all input edges have the particles with ghosts zones
68 edges.hpart.check_sizes(part_counts);
69 edges.grad_P_on_rho.check_sizes(part_counts);
70 edges.s_j.check_sizes(part_counts);
71 edges.t_j.check_sizes(part_counts);
72
73 // ensure that the output edges are of size part_counts (output without ghosts zones)
74 edges.delta_v.ensure_sizes(part_counts);
75
76 const Tscal pmass = edges.gpart_mass.value;
77
78 auto total_specie_count = part_counts.template map<u32>([&](u64 id, u32 count) {
79 return count * ndust;
80 });
81
82 // call the kernel for each patches with part_counts.get(id_patch) threads of patch
83 // id_patch
85 shamsys::instance::get_compute_scheduler_ptr(),
87 edges.hpart.get_spans(),
88 edges.grad_P_on_rho.get_spans(),
89 edges.s_j.get_spans(),
90 edges.t_j.get_spans()},
91 sham::DDMultiRef{edges.delta_v.get_spans()},
92 total_specie_count,
93 [pmass, ndust = ndust](
94 u32 thread_id,
95 const Tscal *__restrict hpart, // npart
96 const Tvec *__restrict grad_P_on_rho, // npart
97 const Tscal *__restrict s_j, // npart * nbins
98 const Tscal *__restrict t_j, // npart * nbins
99 Tvec *__restrict delta_v // npart * nbins
100 ) {
101 u32 id_a = thread_id / ndust;
102 u32 jdust = thread_id % ndust;
103
104 Tscal h_a = hpart[id_a];
105 Tvec grad_P_on_rho_a = grad_P_on_rho[id_a];
106 Tscal sj_a = s_j[thread_id];
107 Tscal tj_a = t_j[thread_id];
108
109 using namespace shamrock::sph;
110 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
111
112 auto epsilon = [&](Tscal sj) {
113 return sj * sj / rho_a;
114 };
115
116 Tscal eps_j_a = epsilon(sj_a);
117
118 /*
119 * Hutchison 2018 eq 15
120 * T_{sj} = \epsilon_j (1 - \epsilon_j) t_j
121 * delta_v = \epsilon_j t_j \nabla P / rho = T_{sj} \nabla P / rho_g
122 * but now if i assume that Tsj in Hutchison 2018 meant tsj, then
123 * delta_v = t_j \nabla P / rho_g
124 */
125
126 // old with ts_a
127 // delta_v[thread_id] = (eps_j_a * tj_a) * grad_P_on_rho_a;
128
129 Tscal sum_eps = 0;
130 for (u32 k = 0; k < ndust; k++) {
131 Tscal sk_a = s_j[id_a * ndust + k];
132 Tscal eps_k_a = epsilon(sk_a);
133 sum_eps += eps_k_a;
134 }
135
136 Tvec grad_P_on_rho_g_a = grad_P_on_rho_a / (1 - sum_eps);
137
138 delta_v[thread_id] = tj_a * grad_P_on_rho_g_a;
139 });
140 }
141
142 inline virtual std::string _impl_get_label() const { return "MonoFluidTVIDeltav"; };
143
144 inline virtual std::string _impl_get_tex() const {
145
146 auto gpart_mass = get_ro_edge_base(0).get_tex_symbol();
147 auto part_counts = get_ro_edge_base(1).get_tex_symbol();
148 auto hpart = get_ro_edge_base(2).get_tex_symbol();
149 auto grad_p_on_rho = get_ro_edge_base(3).get_tex_symbol();
150 auto s_j = get_ro_edge_base(4).get_tex_symbol();
151 auto t_j = get_ro_edge_base(5).get_tex_symbol();
152 auto delta_v = get_rw_edge_base(0).get_tex_symbol();
153
154 std::string tex = R"tex(
155 MonoFluidTVIDeltav
156
157 \begin{align}
158 \epsilon_{i,j} = \frac{{s_j}_{i,j}^2}{{rho}_i ({hpart}_i)} \\
159 {delta_v}_{i,j} = \epsilon_{i,j} {t_j}_{i,j} {grad_p_on_rho}_i \\
160 i \in [0,{part_counts}] \\
161 j \in [0,{ndust}]
162 \end{align}
163 )tex";
164
165 shambase::replace_all(tex, "{gpart_mass}", gpart_mass);
166 shambase::replace_all(tex, "{part_counts}", part_counts);
167 shambase::replace_all(tex, "{ndust}", shambase::format("{}", ndust));
168 shambase::replace_all(tex, "{hpart}", hpart);
169 shambase::replace_all(tex, "{grad_p_on_rho}", grad_p_on_rho);
170 shambase::replace_all(tex, "{s_j}", s_j);
171 shambase::replace_all(tex, "{t_j}", t_j);
172 shambase::replace_all(tex, "{delta_v}", delta_v);
173
174 return tex;
175 };
176 };
177} // namespace shammodels::sph::modules
178
179#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
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:30
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:100
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:91
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.