Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SetDustStoppingTimeEpstein.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"
22#include "shambackends/vec.hpp"
24#include "shamphys/Dust.hpp"
30#include <vector>
31
32#define NODE_EDGES(X_RO, X_RW) \
33 /* scalars */ \
34 X_RO(shamrock::solvergraph::ScalarEdge<Tscal>, gpart_mass) \
35 X_RO(shamrock::solvergraph::ScalarEdge<Tscal>, gamma) \
36 X_RO(shamrock::solvergraph::ScalarEdge<std::vector<Tscal>>, sgrain_j) \
37 X_RO(shamrock::solvergraph::ScalarEdge<std::vector<Tscal>>, rho_grain_j) \
38 \
39 /* counts */ \
40 X_RO(shamrock::solvergraph::Indexes<u32>, part_counts) \
41 \
42 /* fields */ \
43 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, hpart) \
44 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, cs) \
45 \
46 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, t_j)
47
49
50 template<class Tvec, template<class> class SPHKernel>
51 class SetDustStoppingTimeEpstein : public shamrock::solvergraph::INode {
52
53 using Tscal = shambase::VecComponent<Tvec>;
54 using Kernel = SPHKernel<Tscal>;
55
56 u32 ndust;
57 std::unique_ptr<sham::DeviceBuffer<Tscal>> sgrain_j;
58 std::unique_ptr<sham::DeviceBuffer<Tscal>> rho_grain_j;
59
60 public:
61 SetDustStoppingTimeEpstein(u32 ndust) : ndust(ndust) {}
62
63 EXPAND_NODE_EDGES(NODE_EDGES)
64
66
68
69 auto edges = get_edges();
70
71 auto &part_counts = edges.part_counts.indexes;
72 const std::vector<Tscal> &inputs_sgrain_j = edges.sgrain_j.value;
73 const std::vector<Tscal> &inputs_rho_grain_j = edges.rho_grain_j.value;
74 SHAM_ASSERT(inputs_sgrain_j.size() == ndust);
75 SHAM_ASSERT(inputs_rho_grain_j.size() == ndust);
76
77 // ensure that the output edges are of size part_counts
78 edges.t_j.ensure_sizes(part_counts);
79
80 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
81
82 if (!sgrain_j) {
83 sgrain_j = std::make_unique<sham::DeviceBuffer<Tscal>>(ndust, dev_sched);
84 }
85 if (!rho_grain_j) {
86 rho_grain_j = std::make_unique<sham::DeviceBuffer<Tscal>>(ndust, dev_sched);
87 }
88
89 sgrain_j->resize(ndust);
90 rho_grain_j->resize(ndust);
91 sgrain_j->copy_from_stdvec(inputs_sgrain_j);
92 rho_grain_j->copy_from_stdvec(inputs_rho_grain_j);
93
94 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
95
96 const Tscal pmass = edges.gpart_mass.value;
97 const Tscal gamma = edges.gamma.value;
98
99 part_counts.for_each([&](u64 id, u32 count) {
100 // call the kernel for each patches with part_counts.get(id_patch) threads of patch
101 // id_patch
103 q,
105 *sgrain_j,
106 *rho_grain_j,
107 edges.hpart.get_spans().get(id),
108 edges.cs.get_spans().get(id)},
109 sham::MultiRef{edges.t_j.get_spans().get(id)},
110 count * ndust,
111 [ndust = ndust, pmass, gamma](
112 u32 thread_id,
113 const Tscal *__restrict sgrain_j,
114 const Tscal *__restrict rho_grain_j,
115 const Tscal *__restrict hpart,
116 const Tscal *__restrict cs,
117 Tscal *__restrict t_j) {
118 u32 jdust = thread_id % ndust;
119 u32 id_a = thread_id / ndust;
120
121 Tscal sgrain = sgrain_j[jdust];
122 Tscal rho_grain = rho_grain_j[jdust];
123
124 Tscal h_a = hpart[id_a];
125 Tscal cs_a = cs[id_a];
126
127 using namespace shamrock::sph;
128 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
129
131 rho_grain, sgrain, rho_a, cs_a, gamma);
132
133 t_j[thread_id] = ts;
134 });
135 });
136 }
137
138 inline virtual std::string _impl_get_label() const { return "SetDustStoppingTimeEpstein"; };
139
140 inline virtual std::string _impl_get_tex() const {
141
142 auto gpart_mass = get_ro_edge_base(0).get_tex_symbol();
143 auto gamma = get_ro_edge_base(1).get_tex_symbol();
144 auto sgrain_j = get_ro_edge_base(2).get_tex_symbol();
145 auto rho_grain_j = get_ro_edge_base(3).get_tex_symbol();
146 auto part_counts = get_ro_edge_base(4).get_tex_symbol();
147 auto hpart = get_ro_edge_base(5).get_tex_symbol();
148 auto cs = get_ro_edge_base(6).get_tex_symbol();
149 auto t_j = get_rw_edge_base(0).get_tex_symbol();
150
151 std::string tex = R"tex(
152 SetDustStoppingTimeEpstein (PHANTOM eq.~250, subsonic)
153
154 \begin{align}
155 \rho_i &= {gpart_mass} \left( \frac{h_{\rm fact}}{ {hpart}_i } \right)^3 \\
156 {t_j}_{i,j} &= \frac{ {rho_grain_j}_j \, {sgrain_j}_j }{ \rho_i \, {cs}_i }
157 \sqrt{\frac{\pi \, {gamma}}{8}} \\
158 i &\in [0,{part_counts}) \\
159 j &\in [0,{ndust}) \\
160 h_{\rm fact} &= {hfact}
161 \end{align}
162 )tex";
163
164 shambase::replace_all(tex, "{gpart_mass}", gpart_mass);
165 shambase::replace_all(tex, "{gamma}", gamma);
166 shambase::replace_all(tex, "{sgrain_j}", sgrain_j);
167 shambase::replace_all(tex, "{rho_grain_j}", rho_grain_j);
168 shambase::replace_all(tex, "{part_counts}", part_counts);
169 shambase::replace_all(tex, "{ndust}", shambase::format("{}", ndust));
170 shambase::replace_all(tex, "{hpart}", hpart);
171 shambase::replace_all(tex, "{cs}", cs);
172 shambase::replace_all(tex, "{t_j}", t_j);
173 shambase::replace_all(tex, "{hfact}", shambase::format("{}", Kernel::hfactd));
174
175 return tex;
176 };
177 };
178} // namespace shammodels::sph::modules
179
180#undef NODE_EDGES
Epstein drag stopping time for spherical dust grains.
T epstein_stopping_time(T rho_grain, T s_grain, T rho, T cs, T gamma, T f=T(1.0)) noexcept
Epstein drag stopping time for spherical dust grains.
Definition Dust.hpp:75
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
Definition assert.hpp:67
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex 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 kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
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 class that references multiple buffers or similar objects.