26template<
class Tvec,
template<
class>
class SPHKernel>
27auto shammodels::sph::modules::GeneratorMCDisc<Tvec, SPHKernel>::DiscIterator::compute_fmax()
33 constexpr Tscal rel_tol = 1e-8;
34 constexpr u32 max_iter = 200;
39 Tscal f_prev = sycl::fmax(f_func(a), f_func(b));
42 for (
u32 iter = 0; iter < max_iter; iter++) {
43 Tscal m = (a + b) / 2;
44 Tscal delta = (b - a) * Tscal(1e-3);
46 Tscal f1 = f_func(m - delta);
47 Tscal f2 = f_func(m + delta);
56 f_est = sycl::fmax(f1, f2);
58 if (sycl::fabs(f_est - f_prev) <= rel_tol * sycl::fabs(f_est)) {
65 return sycl::fmax(f_est, sycl::fmax(f_func(r_in), f_func(r_out)));
68template<
class Tvec,
template<
class>
class SPHKernel>
69auto shammodels::sph::modules::GeneratorMCDisc<Tvec, SPHKernel>::DiscIterator::next(
u64 seed)
72 std::mt19937_64 eng_local(seed);
82 if (f > fmax * Tscal(1 + 1e-6)) {
84 "GeneratorMCDisc: r * sigma_profile(r) = {} at r = {} exceeds the "
85 "rejection sampling bound fmax = {}",
97 auto Gauss = shamalgs::random::mock_gaussian<Tscal>(eng_local);
101 Tscal
sigma = sigma_profile(r);
104 Tscal H = H_profile(r);
107 auto pos = sycl::vec<Tscal, 3>{r * sycl::cos(theta), r * sycl::sin(theta), z};
111 Tscal rho = (
sigma * fs) * sycl::exp(-z * z / (2 * H * H));
113 DiscOutput out{.pos = pos, .rho = rho};
117 if (current_index == Npart) {
124template<
class Tvec,
template<
class>
class SPHKernel>
126 return generator.is_done();
129template<
class Tvec,
template<
class>
class SPHKernel>
133 using namespace shamrock::patch;
136 std::vector<DiscOutput> pos_data;
139 if (!generator.is_done()) {
140 u64 loc_gen_count = nmax;
141 pos_data = generator.next_n(loc_gen_count);
145 std::vector<Tvec> vec_pos;
146 std::vector<Tscal> vec_rho;
148 vec_pos.reserve(pos_data.size());
149 vec_rho.reserve(pos_data.size());
151 for (DiscOutput o : pos_data) {
152 vec_pos.push_back(o.pos);
153 vec_rho.push_back(o.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);
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);
172 bool need_cs = solver_config.is_eos_locally_isothermal();
174 std::vector<Tscal> vec_cs;
178 "With this EOS you need to provide a cs_profile");
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);
189 if (!pos_data.empty()) {
190 tmp.resize(pos_data.size());
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);
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);
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);
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);
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
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.
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...
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for math utility