Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
GeneratorMCDisc.cpp
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
17
22#include "shamalgs/random.hpp"
25
26template<class Tvec, template<class> class SPHKernel>
27auto shammodels::sph::modules::GeneratorMCDisc<Tvec, SPHKernel>::DiscIterator::compute_fmax()
28 -> Tscal {
29 // f_func is not necessarily monotonic so the rejection sampling
30 // envelope has to be the max over the whole range, not f_func(r_out)
31
32 // dichotomic search of the max of f_func (assumed unimodal) on [r_in, r_out]
33 constexpr Tscal rel_tol = 1e-8;
34 constexpr u32 max_iter = 200;
35
36 Tscal a = r_in;
37 Tscal b = r_out;
38
39 Tscal f_prev = sycl::fmax(f_func(a), f_func(b));
40 Tscal f_est = f_prev;
41
42 for (u32 iter = 0; iter < max_iter; iter++) {
43 Tscal m = (a + b) / 2;
44 Tscal delta = (b - a) * Tscal(1e-3);
45
46 Tscal f1 = f_func(m - delta);
47 Tscal f2 = f_func(m + delta);
48
49 // keep the half containing the max
50 if (f1 < f2) {
51 a = m - delta;
52 } else {
53 b = m + delta;
54 }
55
56 f_est = sycl::fmax(f1, f2);
57
58 if (sycl::fabs(f_est - f_prev) <= rel_tol * sycl::fabs(f_est)) {
59 break;
60 }
61 f_prev = f_est;
62 }
63
64 // the max may sit on the boundary if f_func is monotonic
65 return sycl::fmax(f_est, sycl::fmax(f_func(r_in), f_func(r_out)));
66}
67
68template<class Tvec, template<class> class SPHKernel>
69auto shammodels::sph::modules::GeneratorMCDisc<Tvec, SPHKernel>::DiscIterator::next(u64 seed)
70 -> DiscOutput {
71
72 std::mt19937_64 eng_local(seed); // ensure that 1 part = 1 random draw
73
74 auto find_r = [&]() {
75 while (true) {
76 Tscal u2 = shamalgs::primitives::mock_value<Tscal>(eng_local, 0, fmax);
77 Tscal r = shamalgs::primitives::mock_value<Tscal>(eng_local, r_in, r_out);
78 Tscal f = f_func(r);
79 // fmax is only converged to a relative 1e-8, so allow for slight overshoots.
80 // A larger one means the max was missed (f_func not unimodal) and the sampled
81 // distribution would be silently biased
82 if (f > fmax * Tscal(1 + 1e-6)) {
84 "GeneratorMCDisc: r * sigma_profile(r) = {} at r = {} exceeds the "
85 "rejection sampling bound fmax = {}",
86 f,
87 r,
88 fmax));
89 }
90 if (u2 < f) {
91 return r;
92 }
93 }
94 };
95
96 auto theta = shamalgs::primitives::mock_value<Tscal>(eng_local, 0, _2pi);
97 auto Gauss = shamalgs::random::mock_gaussian<Tscal>(eng_local);
98
99 // depends on sigma profile
100 Tscal r = find_r();
101 Tscal sigma = sigma_profile(r);
102
103 // depends on H profile & sigma profile (through r)
104 Tscal H = H_profile(r);
105 Tscal z = H * Gauss;
106
107 auto pos = sycl::vec<Tscal, 3>{r * sycl::cos(theta), r * sycl::sin(theta), z};
108
109 // extrapolate the density from sigma profile
110 Tscal fs = 1;
111 Tscal rho = (sigma * fs) * sycl::exp(-z * z / (2 * H * H));
112
113 DiscOutput out{.pos = pos, .rho = rho};
114
115 // increase counter + check if finished
116 current_index++;
117 if (current_index == Npart) {
118 done = true;
119 }
120
121 return out;
122}
123
124template<class Tvec, template<class> class SPHKernel>
128
129template<class Tvec, template<class> class SPHKernel>
131 u32 nmax) {
132
133 using namespace shamrock::patch;
134 PatchScheduler &sched = shambase::get_check_ref(context.sched);
135
136 std::vector<DiscOutput> pos_data;
137
138 // Fill pos_data if the scheduler has some patchdata in this rank
139 if (!generator.is_done()) {
140 u64 loc_gen_count = nmax;
141 pos_data = generator.next_n(loc_gen_count);
142 }
143
144 // extract data from disc output
145 std::vector<Tvec> vec_pos;
146 std::vector<Tscal> vec_rho;
147
148 vec_pos.reserve(pos_data.size());
149 vec_rho.reserve(pos_data.size());
150
151 for (DiscOutput o : pos_data) {
152 vec_pos.push_back(o.pos);
153 vec_rho.push_back(o.rho);
154 }
155
156 // compute the hpart from the rho
157 std::vector<Tscal> vec_h;
158 vec_h.reserve(pos_data.size());
159 for (Tscal rho : vec_rho) {
160 vec_h.push_back(shamrock::sph::h_rho(pmass, rho, Kernel::hfactd) * init_h_factor);
161 }
162
163 // compute velocities
164 std::vector<Tvec> vec_vel;
165 vec_vel.reserve(pos_data.size());
166 for (size_t i = 0; i < vec_pos.size(); i++) {
167 Tvec vel = vel_profile(vec_pos[i]);
168 vec_vel.push_back(vel);
169 }
170
171 // compute the cs
172 bool need_cs = solver_config.is_eos_locally_isothermal();
173
174 std::vector<Tscal> vec_cs;
175 if (need_cs) {
176 if (!cs_profile) {
178 "With this EOS you need to provide a cs_profile");
179 }
180 vec_cs.reserve(pos_data.size());
181 for (size_t i = 0; i < vec_pos.size(); i++) {
182 Tscal cs = cs_profile(vec_pos[i]);
183 vec_cs.push_back(cs);
184 }
185 }
186
187 // Make a patchdata from pos_data
188 PatchDataLayer tmp(sched.get_layout_ptr_old());
189 if (!pos_data.empty()) {
190 tmp.resize(pos_data.size());
191 tmp.fields_raz();
192
193 {
194 u32 len = pos_data.size();
196 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
197 f.override(vec_pos, len);
198 }
199
200 {
201 u32 len = pos_data.size();
203 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("vxyz"));
204 f.override(vec_vel, len);
205 }
206 {
207 u32 len = vec_pos.size();
209 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("hpart"));
210 f.override(vec_h, len);
211 }
212
213 if (need_cs) {
214 u32 len = vec_pos.size();
216 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("soundspeed"));
217 f.override(vec_cs, len);
218 }
219 }
220
221 return tmp;
222}
223
224using namespace shammath;
228
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
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...
bool is_done()
This function return true if the setup is done.
PatchDataLayer container class, the layout is described in patchdata_layout.
constexpr Unit sigma
Stefan-Boltzmann constant.
Class holding the value of numerous constants generated from the following source.
This header file contains utility functions related to exception handling in the code.
Core formatting functions: format, vformat, and format_printf.
T mock_value(Engine &eng, T min_bound, T max_bound)
Generates a random mock value within specified bounds.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for math utility
Definition AABB.hpp:26