Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
GeneratorMCDisc.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
25
27
28 template<class Tvec, template<class> class SPHKernel>
29 class GeneratorMCDisc : public ISPHSetupNode {
30 using Tscal = shambase::VecComponent<Tvec>;
31 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
32 using Kernel = SPHKernel<Tscal>;
33
34 using Config = SolverConfig<Tvec, SPHKernel>;
35
36 ShamrockCtx &context;
37 Config &solver_config;
38
39 struct DiscOutput {
40 sycl::vec<Tscal, 3> pos;
41 Tscal rho;
42 };
43
44 Tscal pmass;
45
46 class DiscIterator;
47 DiscIterator generator;
48 Tscal init_h_factor;
49
50 std::function<Tvec(Tvec)> vel_profile;
51 std::function<Tscal(Tvec)> cs_profile;
52
53 static DiscIterator make_generator(
54 Tscal part_mass,
55 Tscal disc_mass,
56 Tscal r_in,
57 Tscal r_out,
58 std::function<Tscal(Tscal)> sigma_profile,
59 std::function<Tscal(Tscal)> H_profile,
60 std::mt19937_64 eng) {
61 return DiscIterator(part_mass, disc_mass, r_in, r_out, sigma_profile, H_profile, eng);
62 }
63
64 public:
65 GeneratorMCDisc(
66 ShamrockCtx &context,
67 Config &solver_config,
68 Tscal part_mass,
69 Tscal disc_mass,
70 Tscal r_in,
71 Tscal r_out,
72 std::function<Tscal(Tscal)> sigma_profile,
73 std::function<Tscal(Tscal)> H_profile,
74 std::function<Tvec(Tvec)> vel_profile,
75 std::function<Tscal(Tvec)> cs_profile,
76 std::mt19937_64 eng,
77 Tscal init_h_factor)
78 : context(context), solver_config(solver_config),
79 generator(
80 make_generator(part_mass, disc_mass, r_in, r_out, sigma_profile, H_profile, eng)),
81 init_h_factor(init_h_factor), pmass(part_mass), vel_profile(vel_profile),
82 cs_profile(cs_profile) {}
83
84 bool is_done();
85
87
88 std::string get_name() { return "GeneratorMCDisc"; }
90 };
91
92} // namespace shammodels::sph::modules
93
94template<class Tvec, template<class> class SPHKernel>
95class shammodels::sph::modules::GeneratorMCDisc<Tvec, SPHKernel>::DiscIterator {
96
97 bool done = false;
98 u64 current_index = 0;
99
100 Tscal part_mass;
101 Tscal disc_mass;
102 u64 Npart;
103
104 Tscal r_in;
105 Tscal r_out;
106 std::function<Tscal(Tscal)> sigma_profile;
107 std::function<Tscal(Tscal)> H_profile;
108
110
111 static constexpr Tscal _2pi = 2 * shambase::constants::pi<Tscal>;
112
113 Tscal f_func(Tscal r) { return r * sigma_profile(r); }
114
116 Tscal compute_fmax();
117
119 Tscal fmax;
120
121 DiscOutput next(u64 seed);
122
123 public:
124 DiscIterator(
125 Tscal part_mass,
126 Tscal disc_mass,
127 Tscal r_in,
128 Tscal r_out,
129 std::function<Tscal(Tscal)> sigma_profile,
130 std::function<Tscal(Tscal)> H_profile,
131 std::mt19937_64 eng)
132 : DiscIterator(
133 part_mass,
134 disc_mass,
135 r_in,
136 r_out,
137 sigma_profile,
138 H_profile,
139 eng,
140 disc_mass / part_mass) {}
141
142 DiscIterator(
143 Tscal part_mass,
144 Tscal disc_mass,
145 Tscal r_in,
146 Tscal r_out,
147 std::function<Tscal(Tscal)> sigma_profile,
148 std::function<Tscal(Tscal)> H_profile,
149 std::mt19937_64 eng,
150 u64 Npart)
151 : part_mass(part_mass), disc_mass(disc_mass), Npart(Npart), r_in(r_in), r_out(r_out),
152 sigma_profile(sigma_profile), H_profile(H_profile), generator(eng, Npart),
153 current_index(0) {
154
155 shamlog_debug_ln(
156 "GeneratorMCDisc",
157 "part_mass",
158 part_mass,
159 "disc_mass",
160 disc_mass,
161 "r_in",
162 r_in,
163 "r_out",
164 r_out,
165 "Npart",
166 Npart);
167
168 fmax = compute_fmax();
169
170 shamlog_debug_ln("GeneratorMCDisc", "fmax", fmax);
171 }
172
173 inline bool is_done() {
174 return generator.is_done();
175 } // just to make sure the result is not tempered with
176
177 inline std::vector<DiscOutput> next_n(u64 nmax) {
178 std::vector<u64> seeds = generator.next_n(nmax);
179 std::vector<DiscOutput> ret{};
180 for (u64 seed : seeds) {
181 ret.push_back(next(seed));
182 }
183 return ret;
184 }
185};
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A parallel generator that will spit the same sequence regardless of the number of ranks.
shamrock::patch::PatchDataLayer next_n(u32 nmax)
This function generate patchdata with at most nmax per MPI ranks This function is always assumed as c...
ISPHSetupNode_Dot get_dot_subgraph()
Get a dot subgraph describing the node and its childrens (recursively).
std::string get_name()
Get the name of the node.
bool is_done()
This function return true if the setup is done.
This class is an interface that all SPH setup nodes must implement. It describe an operation associat...
PatchDataLayer container class, the layout is described in patchdata_layout.
Class holding the value of numerous constants generated from the following source.
namespace for the sph model modules
The configuration for a sph solver.
This struct is used to generate a dot graph of the setup tree.