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
20#include "shamalgs/random.hpp"
23
24template<class Tvec, template<class> class SPHKernel>
26 -> DiscOutput {
27
28 std::mt19937_64 eng_local(seed); // ensure that 1 part = 1 random draw
29
30 Tscal fmax = f_func(r_out);
31
32 auto find_r = [&]() {
33 while (true) {
34 Tscal u2 = shamalgs::primitives::mock_value<Tscal>(eng_local, 0, fmax);
35 Tscal r = shamalgs::primitives::mock_value<Tscal>(eng_local, r_in, r_out);
36 if (u2 < f_func(r)) {
37 return r;
38 }
39 }
40 };
41
42 auto theta = shamalgs::primitives::mock_value<Tscal>(eng_local, 0, _2pi);
43 auto Gauss = shamalgs::random::mock_gaussian<Tscal>(eng_local);
44
45 Tscal r = find_r();
46
47 Tscal rot_speed = rot_profile(r);
48 Tscal cs = cs_profile(r);
49 Tscal sigma = sigma_profile(r);
50 Tscal H = H_profile(r);
51
52 Tscal z = H * Gauss;
53
54 auto pos = sycl::vec<Tscal, 3>{r * sycl::cos(theta), r * sycl::sin(theta), z};
55
56 auto etheta = sycl::vec<Tscal, 3>{-pos.y(), pos.x(), 0};
57 etheta /= sycl::length(etheta);
58
59 auto vel = rot_speed * etheta;
60
61 // Tscal fs = 1. - sycl::sqrt(r_in / r);
62 Tscal fs = 1;
63 Tscal rho = (sigma * fs) * sycl::exp(-z * z / (2 * H * H));
64
65 DiscOutput out{pos, vel, cs, rho};
66
67 // increase counter + check if finished
68 current_index++;
69 if (current_index == Npart) {
70 done = true;
71 }
72
73 return out;
74}
75
76template<class Tvec, template<class> class SPHKernel>
80
81template<class Tvec, template<class> class SPHKernel>
83 u32 nmax) {
84
85 using namespace shamrock::patch;
86 PatchScheduler &sched = shambase::get_check_ref(context.sched);
87
88 std::vector<DiscOutput> pos_data;
89
90 // Fill pos_data if the scheduler has some patchdata in this rank
91 if (!generator.is_done()) {
92 u64 loc_gen_count = nmax;
93 pos_data = generator.next_n(loc_gen_count);
94 }
95
96 // extract the pos from part_list
97 std::vector<Tvec> vec_pos;
98 std::vector<Tvec> vec_vel;
99 std::vector<Tscal> vec_u;
100 std::vector<Tscal> vec_h;
101 std::vector<Tscal> vec_cs;
102
103 for (DiscOutput o : pos_data) {
104 vec_pos.push_back(o.pos);
105 vec_vel.push_back(o.velocity);
106 // vec_u.push_back(o.cs * o.cs / (/*solver.eos_gamma * */ (eos_gamma - 1)));
107 Tscal h = shamrock::sph::h_rho(pmass, o.rho, Kernel::hfactd) * init_h_factor;
108 vec_h.push_back(h);
109 vec_cs.push_back(o.cs);
110 }
111
112 // Make a patchdata from pos_data
113 PatchDataLayer tmp(sched.get_layout_ptr_old());
114 if (!pos_data.empty()) {
115 tmp.resize(pos_data.size());
116 tmp.fields_raz();
117
118 {
119 u32 len = pos_data.size();
121 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
122 sycl::buffer<Tvec> buf(vec_pos.data(), len);
123 f.override(buf, len);
124 }
125
126 {
127 u32 len = pos_data.size();
129 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("vxyz"));
130 sycl::buffer<Tvec> buf(vec_vel.data(), len);
131 f.override(buf, len);
132 }
133 {
134 u32 len = vec_pos.size();
136 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("hpart"));
137 sycl::buffer<Tscal> buf(vec_h.data(), len);
138 f.override(buf, len);
139 }
140
141 if (solver_config.is_eos_locally_isothermal()) {
142 u32 len = vec_pos.size();
144 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("soundspeed"));
145 sycl::buffer<Tscal> buf(vec_cs.data(), len);
146 f.override(buf, len);
147 }
148 }
149
150 return tmp;
151}
152
153using namespace shammath;
157
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.
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
PatchDataLayer container class, the layout is described in patchdata_layout.
Class holding the value of numerous constants generated from the following source.
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:110
namespace for math utility
Definition AABB.hpp:26