Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Model.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
20#include "shambase/memory.hpp"
22#include "shambase/string.hpp"
23#include "shamcomm/logs.hpp"
35#include <functional>
36#include <stdexcept>
37#include <utility>
38#include <vector>
39
40template<class Tvec, template<class> class SPHKernel>
42
43 if (solver.solver_config.scheduler_conf.split_load_value == 0) {
45 "Scheduler load value should be greater than 0");
46 }
47
48 solver.init_required_fields();
49 ctx.init_sched(
50 solver.solver_config.scheduler_conf.split_load_value,
51 solver.solver_config.scheduler_conf.merge_load_value);
52
53 using namespace shamrock::patch;
54
55 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
56
57 sched.add_root_patch();
58
59 shamlog_debug_ln("Sys", "build local scheduler tables");
62 sched.update_local_load_value([&](shamrock::patch::Patch p) {
63 return sched.patch_data.owned_data.get(p.id_patch).get_obj_cnt();
64 });
65 solver.init_ghost_layout();
66
67 solver.init_solver_graph();
68}
69
70template<class Tvec>
71inline void post_insert_data(PatchScheduler &sched) {
72 StackEntry stack_loc{};
73
74 sched.scheduler_step(false, false);
75
76 auto [m, M] = sched.get_box_tranform<Tvec>();
77
78 {
79 StackEntry stack_loc{};
81 sched.patch_tree, sched.get_sim_box().get_patch_transform<Tvec>());
83 sptree.attach_buf();
84 reatrib.reatribute_patch_objects(sptree, "xyz");
85 sched.check_patchdata_locality_correctness();
86 }
87
88 sched.scheduler_step(true, true);
89
90 {
91 StackEntry stack_loc{};
93 sched.patch_tree, sched.get_sim_box().get_patch_transform<Tvec>());
94
96 sptree.attach_buf();
97 reatrib.reatribute_patch_objects(sptree, "xyz");
98 sched.check_patchdata_locality_correctness();
99 }
100
101 std::string log = "";
102
103 using namespace shamrock::patch;
104
105 u32 smallest_count = u32_max;
106 u32 largest_count = 0;
107
108 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
109 u32 tmp = pdat.get_obj_cnt();
110 smallest_count = sham::min(tmp, smallest_count);
111 largest_count = sham::max(tmp, largest_count);
112 });
113
114 smallest_count = shamalgs::collective::allreduce_min(smallest_count);
115 largest_count = shamalgs::collective::allreduce_max(largest_count);
116
117 if (shamcomm::world_rank() == 0) {
119 "Model", "current particle counts : min = ", smallest_count, "max = ", largest_count);
120 }
121}
122
123template<class Tvec, template<class> class SPHKernel>
124u64 shammodels::gsph::Model<Tvec, SPHKernel>::get_total_part_count() {
125 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
126 return shamalgs::collective::allreduce_sum(sched.get_rank_count());
127}
128
129template<class Tvec, template<class> class SPHKernel>
130f64 shammodels::gsph::Model<Tvec, SPHKernel>::total_mass_to_part_mass(f64 totmass) {
131 return totmass / get_total_part_count();
132}
133
134template<class Tvec, template<class> class SPHKernel>
135auto shammodels::gsph::Model<Tvec, SPHKernel>::get_ideal_fcc_box(
136 Tscal dr, std::pair<Tvec, Tvec> box) -> std::pair<Tvec, Tvec> {
137 StackEntry stack_loc{};
138 auto [a, b] = generic::setup::generators::get_ideal_fcc_box<Tscal>(
139 dr, std::make_tuple(box.first, box.second));
140 return {a, b};
141}
142
143template<class Tvec, template<class> class SPHKernel>
144auto shammodels::gsph::Model<Tvec, SPHKernel>::get_ideal_hcp_box(
145 Tscal dr, std::pair<Tvec, Tvec> box) -> std::pair<Tvec, Tvec> {
146 StackEntry stack_loc{};
147 auto [a, b] = generic::setup::generators::get_ideal_fcc_box<Tscal>(
148 dr, std::make_tuple(box.first, box.second));
149 return {a, b};
150}
151
152template<class Tvec, template<class> class SPHKernel>
153void shammodels::gsph::Model<Tvec, SPHKernel>::add_cube_fcc_3d(
154 Tscal dr, std::pair<Tvec, Tvec> _box) {
155 StackEntry stack_loc{};
156
158
159 using namespace shamrock::patch;
160
161 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
162
163 std::string log = "";
164
165 auto make_sliced = [&]() {
166 std::vector<Tvec> vec_lst;
167 generic::setup::generators::add_particles_fcc(
168 dr,
169 std::make_tuple(box.lower, box.upper),
170 [&](Tvec r) {
171 return box.contain_pos(r);
172 },
173 [&](Tvec r, Tscal h) {
174 vec_lst.push_back(r);
175 });
176
177 std::vector<std::vector<Tvec>> sliced_buf;
178
179 u32 sz_buf = sched.crit_patch_split * 4;
180
181 std::vector<Tvec> cur_buf;
182 for (u32 i = 0; i < vec_lst.size(); i++) {
183 cur_buf.push_back(vec_lst[i]);
184
185 if (cur_buf.size() > sz_buf) {
186 sliced_buf.push_back(std::exchange(cur_buf, std::vector<Tvec>{}));
187 }
188 }
189
190 if (cur_buf.size() > 0) {
191 sliced_buf.push_back(std::exchange(cur_buf, std::vector<Tvec>{}));
192 }
193
194 return sliced_buf;
195 };
196
197 std::vector<std::vector<Tvec>> sliced_buf = make_sliced();
198
199 for (std::vector<Tvec> to_ins : sliced_buf) {
200
201 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
203 = sched.get_sim_box().template get_patch_transform<Tvec>();
204
205 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(p);
206
207 std::vector<Tvec> vec_acc;
208 for (Tvec r : to_ins) {
209 if (patch_coord.contain_pos(r)) {
210 vec_acc.push_back(r);
211 }
212 }
213
214 if (vec_acc.size() == 0) {
215 return;
216 }
217
218 log += sham::format(
219 "\n rank = {} patch id={}, add N={} particles, coords = {} {}",
221 p.id_patch,
222 vec_acc.size(),
223 patch_coord.lower,
224 patch_coord.upper);
225
226 PatchDataLayer tmp(sched.get_layout_ptr_old());
227 tmp.resize(vec_acc.size());
228 tmp.fields_raz();
229
230 {
231 u32 len = vec_acc.size();
232 PatchDataField<Tvec> &f = tmp.template get_field<Tvec>(
233 sched.pdl_old().template get_field_idx<Tvec>("xyz"));
234 f.override(vec_acc, len);
235 }
236
237 {
238 PatchDataField<Tscal> &f = tmp.template get_field<Tscal>(
239 sched.pdl_old().template get_field_idx<Tscal>("hpart"));
240 using Kernel = SPHKernel<Tscal>;
241 f.override(Kernel::hfactd * dr);
242 }
243
244 pdat.insert_elements(tmp);
245 });
246
247 sched.check_patchdata_locality_correctness();
248 modules::ComputeLoadBalanceValue<Tvec, SPHKernel>(ctx, solver.solver_config, solver.storage)
249 .update_load_balancing();
250 post_insert_data<Tvec>(sched);
251 }
252
253 shamlog_debug_ln("setup", log);
254}
255
256template<class Tvec, template<class> class SPHKernel>
257void shammodels::gsph::Model<Tvec, SPHKernel>::add_cube_hcp_3d(
258 Tscal dr, std::pair<Tvec, Tvec> _box) {
259 StackEntry stack_loc{};
260
262
263 using namespace shamrock::patch;
264
265 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
266
267 std::string log = "";
268
269 auto make_sliced = [&]() {
270 std::vector<Tvec> vec_lst;
271 generic::setup::generators::add_particles_fcc(
272 dr,
273 std::make_tuple(box.lower, box.upper),
274 [&](Tvec r) {
275 return box.contain_pos(r);
276 },
277 [&](Tvec r, Tscal h) {
278 vec_lst.push_back(r);
279 });
280
281 std::vector<std::vector<Tvec>> sliced_buf;
282
283 u32 sz_buf = sched.crit_patch_split * 4;
284
285 std::vector<Tvec> cur_buf;
286 for (u32 i = 0; i < vec_lst.size(); i++) {
287 cur_buf.push_back(vec_lst[i]);
288
289 if (cur_buf.size() > sz_buf) {
290 sliced_buf.push_back(std::exchange(cur_buf, std::vector<Tvec>{}));
291 }
292 }
293
294 if (cur_buf.size() > 0) {
295 sliced_buf.push_back(std::exchange(cur_buf, std::vector<Tvec>{}));
296 }
297
298 return sliced_buf;
299 };
300
301 std::vector<std::vector<Tvec>> sliced_buf = make_sliced();
302
303 for (std::vector<Tvec> to_ins : sliced_buf) {
304
305 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
307 = sched.get_sim_box().template get_patch_transform<Tvec>();
308
309 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(p);
310
311 std::vector<Tvec> vec_acc;
312 for (Tvec r : to_ins) {
313 if (patch_coord.contain_pos(r)) {
314 vec_acc.push_back(r);
315 }
316 }
317
318 if (vec_acc.size() == 0) {
319 return;
320 }
321
322 log += sham::format(
323 "\n rank = {} patch id={}, add N={} particles, coords = {} {}",
325 p.id_patch,
326 vec_acc.size(),
327 patch_coord.lower,
328 patch_coord.upper);
329
330 PatchDataLayer tmp(sched.get_layout_ptr_old());
331 tmp.resize(vec_acc.size());
332 tmp.fields_raz();
333
334 {
335 u32 len = vec_acc.size();
336 PatchDataField<Tvec> &f = tmp.template get_field<Tvec>(
337 sched.pdl_old().template get_field_idx<Tvec>("xyz"));
338 f.override(vec_acc, len);
339 }
340
341 {
342 PatchDataField<Tscal> &f = tmp.template get_field<Tscal>(
343 sched.pdl_old().template get_field_idx<Tscal>("hpart"));
344 using Kernel = SPHKernel<Tscal>;
345 f.override(Kernel::hfactd * dr);
346 }
347
348 pdat.insert_elements(tmp);
349 });
350
351 sched.check_patchdata_locality_correctness();
352 modules::ComputeLoadBalanceValue<Tvec, SPHKernel>(ctx, solver.solver_config, solver.storage)
353 .update_load_balancing();
354 post_insert_data<Tvec>(sched);
355 }
356
357 shamlog_debug_ln("setup", log);
358}
359
360// Explicit template instantiations for all supported kernel types
Header file describing a Node Instance.
MPI scheduler.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
SchedulerPatchData patch_data
handle the data of the patches of the scheduler
u64 crit_patch_split
splitting limit (if load value > crit_patch_split => patch split)
PatchTree patch_tree
handle the tree structure of the patches
void scheduler_step(bool do_split_merge, bool do_load_balancing)
scheduler step
SchedulerPatchList patch_list
handle the list of the patches of the scheduler
std::unordered_set< u64 > owned_patch_id
(owned_patch_id = patch_list.build_local())
void add_root_patch()
add patch to the scheduler
std::unordered_set< u64 > build_local()
select owned patches owned by the node to rebuild local
void build_local_idx_map()
recompute id_patch_to_local_idx
The GSPH Model class.
Definition Model.hpp:63
void init()
Initialise the model and all the related data structures (patch scheduler in particular).
Definition Model.cpp:41
Utility class used to move the objects between patches.
PatchDataLayer container class, the layout is described in patchdata_layout.
u32 get_obj_cnt() const
get the number of objects (particles) stored in this layer
PatchCoordTransform< T > get_patch_transform() const
Get a PatchCoordTransform object that describes the conversion between patch coordinates and domain c...
Definition SimBox.hpp:286
shambase::DistributedData< PatchData > owned_data
map container for patchdata owned by the current node (layout : id_patch,data)
This header file contains utility functions related to exception handling in the code.
GSPH Model class - high-level interface for GSPH simulations.
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.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
constexpr u32 u32_max
u32 max value
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
sph kernels
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86