Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Model.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
19
22#include "shambase/string.hpp"
26#include "shambackends/vec.hpp"
27#include "shamcomm/logs.hpp"
43#include <pybind11/functional.h>
44#include <stdexcept>
45#include <vector>
46
47namespace shammodels::sph {
48
55 template<class Tvec, template<class> class SPHKernel>
56 class Model {
57 public:
58 using Tscal = shambase::VecComponent<Tvec>;
59 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
60 using Kernel = SPHKernel<Tscal>;
61
62 using Solver = Solver<Tvec, SPHKernel>;
63 using SolverConfig = typename Solver::Config;
64 // using SolverConfig = typename Solver::Config;
65
66 ShamrockCtx &ctx;
67
68 Solver solver;
69
70 // SolverConfig sconfig;
71
72 Model(ShamrockCtx &ctx) : ctx(ctx), solver(ctx) {};
73
77
79 void init();
80
83 inline void init_scheduler(u32 crit_split, u32 crit_merge) {
84 solver.solver_config.scheduler_conf.split_load_value = crit_split;
85 solver.solver_config.scheduler_conf.merge_load_value = crit_merge;
86 init();
87 }
88
89 template<std::enable_if_t<dim == 3, int> = 0>
90 inline Tvec get_box_dim_fcc_3d(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt) {
91 return generic::setup::generators::get_box_dim(dr, xcnt, ycnt, zcnt);
92 }
93
97 template<std::enable_if_t<dim == 3, int> = 0>
98 inline Tvec get_box_dim_true_fcc_3d(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt) {
99 return shammath::LatticeFCC<Tvec>::get_box_dim(dr, xcnt, ycnt, zcnt);
100 }
101
102 inline void set_cfl_cour(Tscal cfl_cour) {
103 solver.solver_config.cfl_config.cfl_cour = cfl_cour;
104 }
105 inline void set_cfl_force(Tscal cfl_force) {
106 solver.solver_config.cfl_config.cfl_force = cfl_force;
107 }
108 inline void set_eta_sink(Tscal eta_sink) {
109 solver.solver_config.cfl_config.eta_sink = eta_sink;
110 }
111
112 inline Tscal get_time() { return solver.get_time(); }
113 inline void set_time(Tscal t) { solver.set_time(t); }
114 inline Tscal get_dt_sph() { return solver.get_dt_sph(); }
115 inline void set_next_dt(Tscal dt) { solver.set_next_dt(dt); }
116 inline Tscal get_cfl_multipler() { return solver.get_cfl_multipler(); }
117 inline void set_cfl_multipler(Tscal lambda) { solver.set_cfl_multipler(lambda); }
118
119 inline void set_particle_mass(Tscal gpart_mass) {
120 solver.solver_config.gpart_mass = gpart_mass;
121 }
122
123 inline Tscal get_particle_mass() { return solver.solver_config.gpart_mass; }
124
125 inline void resize_simulation_box(std::pair<Tvec, Tvec> box) {
126 ctx.set_coord_domain_bound({box.first, box.second});
127 }
128
129 SolverConfig gen_config_from_phantom_dump(PhantomDump &phdump, bool bypass_error);
130 void init_from_phantom_dump(PhantomDump &phdump, Tscal hpart_fact_load = 1.0);
131 PhantomDump make_phantom_dump();
132
133 void do_vtk_dump(std::string filename, bool add_patch_world_id) {
134 solver.vtk_do_dump(filename, add_patch_world_id);
135 }
136
137 void set_debug_dump(bool _do_debug_dump, std::string _debug_dump_filename) {
138 solver.set_debug_dump(_do_debug_dump, _debug_dump_filename);
139 }
140
141 u64 get_total_part_count();
142
143 f64 total_mass_to_part_mass(f64 totmass);
144
145 Tscal get_hfact() { return Kernel::hfactd; }
146
147 Tscal rho_h(Tscal h) {
148 return shamrock::sph::rho_h(solver.solver_config.gpart_mass, h, Kernel::hfactd);
149 }
150
151 void add_cube_fcc_3d(Tscal dr, std::pair<Tvec, Tvec> _box);
152 void add_cube_hcp_3d(Tscal dr, std::pair<Tvec, Tvec> _box);
153 void add_cube_hcp_3d_v2(Tscal dr, std::pair<Tvec, Tvec> _box);
154
155 inline std::unique_ptr<modules::SPHSetup<Tvec, SPHKernel>> get_setup() {
156 return std::make_unique<modules::SPHSetup<Tvec, SPHKernel>>(
157 ctx, solver.solver_config, solver.storage);
158 }
159
160 // std::function<Tscal(Tscal)> sigma_profile = [=](Tscal r, Tscal r_in, Tscal p){
161 // // we setup with an adimensional mass since it is monte carlo
162 // constexpr Tscal sigma_0 = 1;
163 // return sigma_0*sycl::pow(r/r_in, -p);
164 // };
165 //
166 // std::function<Tscal(Tscal)> cs_law = [=](Tscal r, Tscal r_in, Tscal q){
167 // return sycl::pow(r/r_in, -q);
168 // };
169 //
170 // std::function<Tscal(Tscal)> rot_profile = [=](Tscal r, Tscal central_mass){
171 // Tscal G = solver.solver_config.get_constant_G();
172 // return sycl::sqrt(G * central_mass/r);
173 // };
174 //
175 // std::function<Tscal(Tscal)> cs_profile = [&](Tscal r, Tscal r_in, Tscal H_r_in){
176 // Tscal cs_in = H_r_in*rot_profile(r_in);
177 // return cs_law(r)*cs_in;
178
179 void add_big_disc_3d(
180 Tvec center,
181 Tscal central_mass,
182 u32 Npart,
183 Tscal r_in,
184 Tscal r_out,
185 Tscal disc_mass,
186 Tscal p,
187 Tscal H_r_in,
188 Tscal q,
189 std::mt19937 eng);
190
191 inline void add_sink(Tscal mass, Tvec pos, Tvec velocity, Tscal accretion_radius) {
192 if (!ctx.is_scheduler_initialized()) {
194 "add_sink() requires that the scheduler has been initialized. "
195 "Call init_scheduler(...) before add_sink().");
196 }
197 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
198 if (!sched.synchronized_data.has_edge("sink_pos")) {
200 "add_sink() requires that sink edges are registered. "
201 "Call init_scheduler(...) before add_sink().");
202 }
203 auto edges = get_sink_edges<Tvec>(sched.synchronized_data);
204
205 shamlog_debug_ln("SPH", "add sink :", mass, pos, velocity, accretion_radius);
206
207 shammodels::sph::add_sink(edges, mass, pos, velocity, accretion_radius);
208
210 }
211
215 auto &dust_config = solver.solver_config.dust_config;
216 return dust_config.is_monofluid_tva() ? dust_config.get_dust_nvar() : 0;
217 }
218
226 auto &sync = shambase::get_check_ref(ctx.sched).synchronized_data;
227 if (!has_sink_accreted_dust_mass(sync)) {
228 return;
229 }
230 // qualified, as the unqualified name would find this member function
232 get_sink_accreted_dust_mass<Tvec>(sync),
233 get_sink_pos<Tvec>(sync).size(),
235 }
236
237 template<class T>
238 inline void set_field_value_lambda(
239 std::string field_name, const std::function<T(Tvec)> pos_to_val, const u32 offset) {
240
241 StackEntry stack_loc{};
242
243 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
244
245 u32 ixyz = sched.pdl_old().get_field_idx<Tvec>("xyz");
246 u32 ifield = sched.pdl_old().get_field_idx<T>(field_name);
247
248 sched.patch_data.for_each_patchdata(
249 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
250 PatchDataField<Tvec> &xyz = pdat.template get_field<Tvec>(ixyz);
251 PatchDataField<T> &f = pdat.template get_field<T>(ifield);
252
253 auto f_nvar = f.get_nvar();
254 if (offset >= f_nvar) {
256 "offset ({}) is out of bounds for field '{}' with nvar {}",
257 offset,
258 field_name,
259 f_nvar));
260 }
261
262 auto acc = f.get_buf().copy_to_stdvec();
263 auto acc_xyz = xyz.get_buf().copy_to_stdvec();
264
265 u32 obj_cnt = pdat.get_obj_cnt();
266 for (u32 i = 0; i < obj_cnt; i++) {
267 acc[i * f_nvar + offset] = pos_to_val(acc_xyz[i]);
268 }
269
270 f.get_buf().copy_from_stdvec(acc);
271 });
272 }
273
274 template<class T>
275 inline void overwrite_field_value(
276 std::string field_name,
277 const std::function<std::vector<T>(shamrock::PatchDataLazyGetter &)> field_compute,
278 const u32 offset) {
279
280 StackEntry stack_loc{};
281
282 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
283
284 u32 ifield = sched.pdl_old().get_field_idx<T>(field_name);
285
286 sched.patch_data.for_each_patchdata(
287 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
288 PatchDataField<T> &f = pdat.template get_field<T>(ifield);
289
290 auto f_nvar = f.get_nvar();
291 if (offset >= f_nvar) {
293 "offset ({}) is out of bounds for field '{}' with nvar {}",
294 offset,
295 field_name,
296 f_nvar));
297 }
298
299 shamrock::PatchDataLazyGetter getter(pdat);
300 auto result = field_compute(getter);
301
302 if (result.size() != f.get_obj_cnt()) {
304 "result.size() != f.get_obj_cnt() ({} != {})",
305 result.size(),
306 f.get_obj_cnt()));
307 }
308
309 auto acc = f.get_buf().copy_to_stdvec();
310
311 u32 obj_cnt = pdat.get_obj_cnt();
312 for (u32 i = 0; i < obj_cnt; i++) {
313 acc[i * f_nvar + offset] = result[i];
314 }
315
316 f.get_buf().copy_from_stdvec(acc);
317 });
318 }
319
334 template<std::enable_if_t<dim == 3, int> = 0>
335 inline Tscal add_disc_3d(
336 Tvec center,
337 Tscal central_mass,
338 u32 Npart,
339 Tscal r_in,
340 Tscal r_out,
341 Tscal disc_mass,
342 Tscal p,
343 Tscal H_r_in,
344 Tscal q) {
345
346 Tscal G = solver.solver_config.get_constant_G();
347
348 Tscal eos_gamma;
349 using Config = SolverConfig;
350 using SolverConfigEOS = typename Config::EOSConfig;
351 using SolverEOS_Adiabatic = typename SolverConfigEOS::Adiabatic;
352 if (SolverEOS_Adiabatic *eos_config
353 = std::get_if<SolverEOS_Adiabatic>(&solver.solver_config.eos_config.config)) {
354
355 eos_gamma = eos_config->gamma;
356
357 } else {
358 // dirty hack for disc setup in locally isothermal
359 eos_gamma = 2;
360 // shambase::throw_unimplemented();
361 }
362
364
365 auto sigma_profile = [=](Tscal r) {
366 // we setup with an adimensional mass since it is monte carlo
367 constexpr Tscal sigma_0 = 1;
368 return sigma_0 * sycl::pow(r / r_in, -p);
369 };
370
371 auto cs_law = [=](Tscal r) {
372 return sycl::pow(r / r_in, -q);
373 };
374
375 auto rot_profile = [=](Tscal r) {
376 return sycl::sqrt(G * central_mass / r);
377 };
378
379 Tscal cs_in = H_r_in * rot_profile(r_in);
380 auto cs_profile = [&](Tscal r) {
381 return cs_law(r) * cs_in;
382 };
383
384 std::vector<Out> part_list;
385
387 Npart,
388 r_in,
389 r_out,
390 [&](Tscal r) {
391 return sigma_profile(r);
392 },
393 [&](Tscal r) {
394 return cs_profile(r);
395 },
396 [&](Tscal r) {
397 return rot_profile(r);
398 },
399 [&](Out out) {
400 part_list.push_back(out);
401 });
402
403 Tscal part_mass = disc_mass / Npart;
404
405 using namespace shamrock::patch;
406
407 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
408
409 std::string log = "";
410
411 sched.for_each_local_patchdata([&](const Patch &ptch, PatchDataLayer &pdat) {
412 PatchCoordTransform<Tvec> ptransf = sched.get_sim_box().get_patch_transform<Tvec>();
413
414 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(ptch);
415
416 std::vector<Tvec> vec_pos;
417 std::vector<Tvec> vec_vel;
418 std::vector<Tscal> vec_u;
419 std::vector<Tscal> vec_h;
420
421 std::vector<Tscal> vec_cs;
422
423 Tscal G = solver.solver_config.get_constant_G();
424
425 for (Out o : part_list) {
426 vec_pos.push_back(o.pos + center);
427 vec_vel.push_back(o.velocity);
428
429 // for disc with P = \rho u (/gamma - 1)
430 // the scaleheight : H = \sqrt{u (\gamma -1)}/\Omega_K
431 // therefore the effective soundspeed is : \sqrt{(\gamma -1)u}
432 // whereas the real one is \sqrt{(\gamma -1)\gamma u}
433 vec_u.push_back(o.cs * o.cs / (/*solver.eos_gamma * */ (eos_gamma - 1)));
434 vec_h.push_back(shamrock::sph::h_rho(part_mass, o.rho, Kernel::hfactd));
435 vec_cs.push_back(o.cs);
436 }
437
438 log += sham::format(
439 "\n patch id={}, add N={} particles", ptch.id_patch, vec_pos.size());
440
441 PatchDataLayer tmp(sched.get_layout_ptr_old());
442 tmp.resize(vec_pos.size());
443 tmp.fields_raz();
444
445 {
446 u32 len = vec_pos.size();
448 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
449 f.override(vec_pos, len);
450 }
451
452 {
453 u32 len = vec_pos.size();
455 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("hpart"));
456 f.override(vec_h, len);
457 }
458
459 {
460 u32 len = vec_pos.size();
462 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("uint"));
463 f.override(vec_u, len);
464 }
465
466 if (solver.solver_config.is_eos_locally_isothermal()) {
467 u32 len = vec_pos.size();
469 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("soundspeed"));
470 f.override(vec_cs, len);
471 }
472
473 {
474 u32 len = vec_pos.size();
476 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("vxyz"));
477 f.override(vec_vel, len);
478 }
479
480 pdat.insert_elements(tmp);
481 });
482
483 std::string log_gathered = "";
484 shamalgs::collective::gather_str(log, log_gathered);
485
486 if (shamcomm::world_rank() == 0) {
487 logger::info_ln("Model", "Push particles : ", log_gathered);
488 }
489
491 ctx, solver.solver_config, solver.storage)
492 .update_load_balancing();
493
494 sched.scheduler_step(false, false);
495
496 {
497 auto [m, M] = sched.get_box_tranform<Tvec>();
498
500 sched.patch_tree, sched.get_sim_box().get_patch_transform<Tvec>());
501
502 // sptree.print_status();
503
505
506 sptree.attach_buf();
507 // reatribute_particles(sched, sptree, periodic_mode);
508
509 reatrib.reatribute_patch_objects(sptree, "xyz");
510 }
511
512 sched.check_patchdata_locality_correctness();
513
514 sched.scheduler_step(true, true);
515
516 log = "";
517 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
518 log += sham::format(
519 "\n patch id={}, N={} particles", p.id_patch, pdat.get_obj_cnt());
520 });
521
522 log_gathered = "";
523 shamalgs::collective::gather_str(log, log_gathered);
524
525 if (shamcomm::world_rank() == 0)
526 logger::info_ln("Model", "current particle counts : ", log_gathered);
527 return part_mass;
528 }
529
530 template<std::enable_if_t<dim == 3, int> = 0>
531 inline void add_cube_disc_3d(
532 Tvec center,
533 u32 Npart,
534 Tscal p,
535 Tscal rho_0,
536 Tscal m,
537 Tscal r_in,
538 Tscal r_out,
539 Tscal q,
540 Tscal cmass) {
541
542 Tscal eos_gamma;
543 using Config = SolverConfig;
544 using SolverConfigEOS = typename Config::EOSConfig;
545 using SolverEOS_Adiabatic = typename SolverConfigEOS::Adiabatic;
546 if (SolverEOS_Adiabatic *eos_config
547 = std::get_if<SolverEOS_Adiabatic>(&solver.solver_config.eos_config.config)) {
548
549 eos_gamma = eos_config->gamma;
550
551 } else {
553 }
554
555 auto cs = [&](Tscal u) {
556 return sycl::sqrt(eos_gamma * (eos_gamma - 1) * u);
557 };
558
559 auto U = [&](Tscal cs) {
560 return cs * cs / (eos_gamma * (eos_gamma - 1));
561 };
562
563 using namespace shamrock::patch;
564
565 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
566
567 std::string log = "";
568
569 sched.for_each_local_patchdata([&](const Patch &ptch, PatchDataLayer &pdat) {
570 PatchCoordTransform<Tvec> ptransf = sched.get_sim_box().get_patch_transform<Tvec>();
571
572 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(ptch);
573
574 std::vector<Tvec> vec_acc;
575 std::vector<Tvec> vec_vel;
576 std::vector<Tscal> vec_u;
577
578 Tscal G = solver.solver_config.get_constant_G();
579
581 Npart, p, rho_0, m, r_in, r_out, q, [&](Tvec r, Tscal h) {
582 vec_acc.push_back(r + center);
583
584 Tscal R = sycl::length(r);
585
586 Tscal V = sycl::sqrt(G * cmass / R);
587
588 Tvec etheta = {-r.z(), 0, r.x()};
589 etheta /= sycl::length(etheta);
590
591 vec_vel.push_back(V * etheta);
592
593 Tscal cs0 = 1;
594 Tscal cs = cs0 * sycl::pow(R, -q);
595
596 vec_u.push_back(U(cs));
597 });
598
599 log += sham::format(
600 "\n patch id={}, add N={} particles", ptch.id_patch, vec_acc.size());
601
602 PatchDataLayer tmp(sched.get_layout_ptr_old());
603 tmp.resize(vec_acc.size());
604 tmp.fields_raz();
605
606 {
607 u32 len = vec_acc.size();
608 PatchDataField<Tvec> &f
609 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
610 f.override(vec_acc, len);
611 }
612
613 {
614 PatchDataField<Tscal> &f
615 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("hpart"));
616 f.override(0.01);
617 }
618
619 {
620 u32 len = vec_acc.size();
621 PatchDataField<Tscal> &f
622 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("uint"));
623 f.override(vec_u, len);
624 }
625
626 {
627 u32 len = vec_acc.size();
628 PatchDataField<Tvec> &f
629 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("vxyz"));
630 f.override(vec_vel, len);
631 }
632
633 pdat.insert_elements(tmp);
634 });
635
636 std::string log_gathered = "";
637 shamalgs::collective::gather_str(log, log_gathered);
638
639 if (shamcomm::world_rank() == 0) {
640 logger::info_ln("Model", "Push particles : ", log_gathered);
641 }
642
643 modules::ComputeLoadBalanceValue<Tvec, SPHKernel>(
644 ctx, solver.solver_config, solver.storage)
645 .update_load_balancing();
646
647 sched.scheduler_step(false, false);
648
649 {
650 auto [m, M] = sched.get_box_tranform<Tvec>();
651
652 SerialPatchTree<Tvec> sptree(
653 sched.patch_tree, sched.get_sim_box().get_patch_transform<Tvec>());
654
655 // sptree.print_status();
656
657 shamrock::ReattributeDataUtility reatrib(sched);
658
659 sptree.attach_buf();
660 // reatribute_particles(sched, sptree, periodic_mode);
661
662 reatrib.reatribute_patch_objects(sptree, "xyz");
663 }
664
665 sched.check_patchdata_locality_correctness();
666
667 sched.scheduler_step(true, true);
668
669 log = "";
670 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
671 log += sham::format(
672 "\n patch id={}, N={} particles", p.id_patch, pdat.get_obj_cnt());
673 });
674
675 log_gathered = "";
676 shamalgs::collective::gather_str(log, log_gathered);
677
678 if (shamcomm::world_rank() == 0)
679 logger::info_ln("Model", "current particle counts : ", log_gathered);
680 }
681
682 void remap_positions(std::function<Tvec(Tvec)> map);
683
684 void push_particle(
685 std::vector<Tvec> &part_pos_insert,
686 std::vector<Tscal> &part_hpart_insert,
687 std::vector<Tscal> &part_u_insert);
688
689 void push_particle_mhd(
690 std::vector<Tvec> &part_pos_insert,
691 std::vector<Tscal> &part_hpart_insert,
692 std::vector<Tscal> &part_u_insert,
693 std::vector<Tvec> &part_B_on_rho_insert,
694 std::vector<Tscal> &part_psi_on_ch_insert);
695
696 template<class T>
697 inline void set_value_in_a_box(
698 std::string field_name, T val, std::pair<Tvec, Tvec> box, u32 ivar) {
699 StackEntry stack_loc{};
700 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
701 sched.patch_data.for_each_patchdata(
702 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
703 PatchDataField<Tvec> &xyz
704 = pdat.template get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
705
706 PatchDataField<T> &f
707 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(field_name));
708
709 if (ivar >= f.get_nvar()) {
711 "You are trying to set value in a box for field ({}) with "
712 "ivar ({}) >= f.get_nvar ({})",
713 field_name,
714 ivar,
715 f.get_nvar()));
716 }
717
718 u32 nvar = f.get_nvar();
719
720 {
721 auto acc = f.get_buf().template mirror_to<sham::host>();
722 auto acc_xyz = xyz.get_buf().template mirror_to<sham::host>();
723
724 for (u32 i = 0; i < f.get_obj_cnt(); i++) {
725 Tvec r = acc_xyz[i];
726
727 if (BBAA::is_coord_in_range(r, std::get<0>(box), std::get<1>(box))) {
728 acc[i * nvar + ivar] = val;
729 }
730 }
731 }
732 });
733 }
734
735 template<class T>
736 inline void set_value_in_sphere(std::string field_name, T val, Tvec center, Tscal radius) {
737 StackEntry stack_loc{};
738 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
739 sched.patch_data.for_each_patchdata(
740 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
741 PatchDataField<Tvec> &xyz
742 = pdat.template get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
743
744 PatchDataField<T> &f
745 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(field_name));
746
747 if (f.get_nvar() != 1) {
749 }
750
751 Tscal r2 = radius * radius;
752 {
753 auto acc = f.get_buf().template mirror_to<sham::host>();
754 auto acc_xyz = xyz.get_buf().template mirror_to<sham::host>();
755
756 for (u32 i = 0; i < f.get_obj_cnt(); i++) {
757 Tvec dr = acc_xyz[i] - center;
758
759 if (sycl::dot(dr, dr) < r2) {
760 acc[i] = val;
761 }
762 }
763 }
764 });
765 }
766
767 template<class T>
768 inline void add_kernel_value(std::string field_name, T val, Tvec center, Tscal h_ker) {
769 StackEntry stack_loc{};
770 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
771 sched.patch_data.for_each_patchdata(
772 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
773 PatchDataField<Tvec> &xyz
774 = pdat.template get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
775
776 PatchDataField<T> &f
777 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(field_name));
778
779 if (f.get_nvar() != 1) {
781 }
782
783 {
784 auto acc = f.get_buf().template mirror_to<sham::host>();
785 auto acc_xyz = xyz.get_buf().template mirror_to<sham::host>();
786
787 for (u32 i = 0; i < f.get_obj_cnt(); i++) {
788 Tvec dr = acc_xyz[i] - center;
789
790 Tscal r = sycl::length(dr);
791
792 acc[i] += val * Kernel::W_3d(r, h_ker);
793 }
794 }
795 });
796 }
797
798 template<class T>
799 inline T get_sum(std::string name) {
800 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
801 T sum = shambase::VectorProperties<T>::get_zero();
802
803 StackEntry stack_loc{};
804 sched.patch_data.for_each_patchdata(
805 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
806 PatchDataField<T> &xyz
807 = pdat.template get_field<T>(sched.pdl_old().get_field_idx<T>(name));
808
809 sum += xyz.compute_sum();
810 });
811
812 return shamalgs::collective::allreduce_sum(sum);
813 }
814
815 Tvec get_closest_part_to(Tvec pos);
816
817 inline void apply_momentum_offset(Tvec offset) {
818
819 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
820
821 u32 ivxyz = sched.pdl_old().get_field_idx<Tvec>("vxyz");
822
823 // compute the total mass
824 Tscal tot_mass = 0;
825
826 sched.for_each_patchdata_nonempty(
827 [&](shamrock::patch::Patch p, shamrock::patch::PatchDataLayer &pdat) {
828 tot_mass += solver.solver_config.gpart_mass * pdat.get_obj_cnt();
829 });
830
831 tot_mass = shamalgs::collective::allreduce_sum(tot_mass);
832
833 // add the mass of the sinks
834 auto &sync = sched.synchronized_data;
835 auto &mass = get_sink_mass<Tvec>(sync);
836 if (!mass.empty()) {
837 for (size_t i = 0; i < mass.size(); i++) {
838 tot_mass += mass[i];
839 }
840 }
841
842 // compute the offset velocity
843 Tvec offset_vel = (tot_mass > 0) ? (offset / tot_mass)
844 : shambase::VectorProperties<Tvec>::get_zero();
845
846 // apply the offset velocity to the sinks
847 auto &vel = get_sink_vel<Tvec>(sync);
848 if (!vel.empty()) {
849 for (size_t i = 0; i < vel.size(); i++) {
850 vel[i] += offset_vel;
851 }
852 }
853
854 // apply the offset velocity to the particles
855 sched.for_each_patchdata_nonempty(
856 [&](shamrock::patch::Patch p, shamrock::patch::PatchDataLayer &pdat) {
857 PatchDataField<Tvec> &vxyz = pdat.get_field<Tvec>(ivxyz);
858 vxyz.apply_offset(offset_vel);
859 });
860 }
861
862 inline void apply_position_offset(Tvec offset) {
863
864 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
865
866 u32 ixyz = sched.pdl_old().get_field_idx<Tvec>("xyz");
867
868 // apply the position offset to the sinks
869 auto &pos = get_sink_pos<Tvec>(sched.synchronized_data);
870 if (!pos.empty()) {
871 for (size_t i = 0; i < pos.size(); i++) {
872 pos[i] += offset;
873 }
874 }
875
876 // apply the position offset to the particles
877 sched.for_each_patchdata_nonempty(
878 [&](shamrock::patch::Patch p, shamrock::patch::PatchDataLayer &pdat) {
879 PatchDataField<Tvec> &xyz = pdat.get_field<Tvec>(ixyz);
880 xyz.apply_offset(offset);
881 });
882 }
883
884 // inline void enable_barotropic_mode(){
885 // sconfig.enable_barotropic();
886 // }
887 //
888 // inline void switch_internal_energy_mode(std::string name){
889 // sconfig.switch_internal_energy_mode(name);
890 // }
891
892 inline void set_solver_config(typename Solver::Config cfg) {
893 if (ctx.is_scheduler_initialized()) {
895 "Cannot change solver config after scheduler is initialized");
896 }
897 cfg.check_config();
898 solver.solver_config = cfg;
899 }
900
901 inline f64 solver_logs_last_rate() { return solver.solve_logs.get_last_rate(); }
902 inline u64 solver_logs_last_obj_count() { return solver.solve_logs.get_last_obj_count(); }
903 inline f64 solver_logs_cumulated_step_time() {
904 return solver.solve_logs.get_cumulated_step_time();
905 }
906 inline void solver_logs_reset_cumulated_step_time() {
907 solver.solve_logs.reset_cumulated_step_time();
908 }
909 inline u64 solver_logs_step_count() { return solver.solve_logs.get_step_count(); }
910 inline void solver_logs_reset_step_count() { solver.solve_logs.reset_step_count(); }
911
912 inline void change_htolerances(Tscal in_coarse, Tscal in_fine) {
913 if (in_coarse < in_fine) {
915 "in_coarse ({}) must be greater than in_fine ({})", in_coarse, in_fine));
916 }
917 solver.solver_config.htol_up_coarse_cycle = in_coarse;
918 solver.solver_config.htol_up_fine_cycle = in_fine;
919 }
920
924
928
934 inline void load_from_dump(std::string fname) {
935 if (shamcomm::world_rank() == 0) {
936 logger::info_ln("SPH", "Loading state from dump", fname);
937 }
938
939 // Load the context state and recover user metadata
940 std::string metadata_user{};
941 shamrock::load_shamrock_dump(fname, metadata_user, ctx);
942
944 nlohmann::json j = nlohmann::json::parse(metadata_user);
945 // std::cout << j << std::endl;
946 j.at("solver_config").get_to(solver.solver_config);
947
948 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
949
950 // PR #1928 introduces time/dt/cfl synchronization edges
951 // so checking for time is equivalent to commit >= PR #1928
952 // (must be checked before init_solver_graph, which creates them if missing)
953 bool had_time_edge = sched.synchronized_data.has_edge("time");
954
955 solver.init_ghost_layout();
956
957 solver.init_solver_graph();
958
959 // Migrate old dumps that stored time/dt/cfl in solver_config.time_state
960 if (!had_time_edge) { // before PR #1928
961 if (j.at("solver_config").contains("time_state")) {
962 ON_RANK_0(
964 "SPH",
965 "Migrated time/dt/cfl from solver_config.time_state into scheduler "
966 "edges"));
967 const auto &ts = j.at("solver_config").at("time_state");
968 solver.set_time(ts.at("time").get<Tscal>());
969 solver.set_next_dt(ts.at("dt_sph").get<Tscal>());
970 solver.set_cfl_multipler(ts.at("cfl_multiplier").get<Tscal>());
971 } else {
973 "this should never happen: dump has neither time edges nor "
974 "solver_config.time_state");
975 }
976 }
977
978 // Old dumps stored sinks in the metadata rather than as synchronized edges
979 // (sink edges are ensured by init_solver_graph)
980 if (j.contains("sinks") && !j.at("sinks").is_null()) {
981 std::vector<SinkParticle<Tvec>> out;
982 j.at("sinks").get_to(out);
983 auto edges = get_sink_edges<Tvec>(sched.synchronized_data);
984 set_sink_particles(edges, out);
985 }
986
987 // dumps predating per species accreted dust mass tracking have no entry for it
989
990 shamlog_debug_ln("Sys", "build local scheduler tables");
991 sched.owned_patch_id = sched.patch_list.build_local();
994 sched.update_local_load_value([&](shamrock::patch::Patch p) {
995 return sched.patch_data.owned_data.get(p.id_patch).get_obj_cnt();
996 });
997 }
998
1004 inline void dump(std::string fname) {
1005 if (shamcomm::world_rank() == 0) {
1006 logger::info_ln("SPH", "Dumping state to", fname);
1007 }
1008
1009 solver.update_sync_load_values();
1010
1011 nlohmann::json metadata;
1012 metadata["solver_config"] = solver.solver_config;
1013
1014 // Dump the state of the SPH model to a file
1017 fname, metadata.dump(4), shambase::get_check_ref(ctx.sched));
1018 }
1019
1023
1024 f64 evolve_once_time_expl(f64 t_curr, f64 dt_input);
1025
1026 TimestepLog timestep();
1027
1028 inline void evolve_once() {
1029 solver.evolve_once();
1030 solver.print_timestep_logs();
1031 }
1032
1033 inline EvolveUntilResults evolve_until(
1034 Tscal target_time, i32 niter_max, f64 max_walltime = -1) {
1035 return solver.evolve_until(target_time, niter_max, max_walltime);
1036 }
1037
1038 private:
1039 void add_pdat_to_phantom_block(
1040 PhantomDumpBlock &block, shamrock::patch::PatchDataLayer &pdat);
1041
1042 template<class Tscal>
1043 inline void warp_disc(
1044 std::vector<Tvec> &pos,
1045 std::vector<Tvec> &vel,
1046 Tscal posangle,
1047 Tscal incl,
1048 Tscal Rwarp,
1049 Tscal Hwarp) {
1050 Tvec k = Tvec(-std::sin(posangle), std::cos(posangle), 0.);
1051 Tscal inc;
1052 Tscal psi = 0.;
1053 u32 len = pos.size();
1054
1055 // convert to radians (sycl functions take radians)
1056 Tscal incl_rad = incl * shambase::constants::pi<Tscal> / 180.;
1057
1058 for (i32 i = 0; i < len; i++) {
1059 Tvec R_vec = pos[i];
1060 Tscal R = sycl::sqrt(sycl::dot(R_vec, R_vec));
1061 if (R < Rwarp - Hwarp) {
1062 inc = 0.;
1063 } else if (R < Rwarp + 3. * Hwarp && R > Rwarp - Hwarp) {
1064 inc = sycl::asin(
1065 0.5
1066 * (1.
1067 + sycl::sin(shambase::constants::pi<Tscal> / (2. * Hwarp) * (R - Rwarp)))
1068 * sycl::sin(incl_rad));
1069 psi = shambase::constants::pi<Tscal>
1070 * Rwarp / (4. * Hwarp) * sycl::sin(incl_rad)
1071 / sycl::sqrt(1. - (0.5 * sycl::pow(sycl::sin(incl_rad), 2)));
1072 Tscal psimax = sycl::max(psimax, psi);
1073 Tscal x = pos[i].x();
1074 Tscal y = pos[i].y();
1075 Tscal z = pos[i].z();
1076
1077 // Tscal xp = x * sycl::cos(inc) + y * sycl::sin(inc);
1078 // Tscal yp = - x * sycl::sin(inc) + y * sycl::cos(inc);
1079 // pos[i] = Tvec(xp, yp, z);
1080
1081 Tvec kk = Tvec(0., 0., 1.);
1082 Tvec w = sycl::cross(kk, pos[i]);
1083 // Rodrigues' rotation formula
1084 pos[i] = pos[i] * sycl::cos(inc) + w * sycl::sin(inc)
1085 + kk * sycl::dot(kk, pos[i]) * (1. - sycl::cos(inc));
1086
1087 } else {
1088 inc = 0.;
1089 }
1090 }
1091 }
1092
1093 inline void rotate_vector(Tvec &u, Tvec &v, Tscal theta) {
1094 // normalize the reference direction
1095 Tvec vunit = v / sycl::sqrt(sycl::dot(v, v));
1096 Tvec w = sycl::cross(vunit, u);
1097 // Rodrigues' rotation formula
1098 u = u * sycl::cos(theta) + w * sycl::sin(theta)
1099 + vunit * sycl::dot(vunit, u) * (1. - sycl::cos(theta));
1100 }
1101 };
1102
1103} // namespace shammodels::sph
constexpr const char * vxyz
3-velocity field
constexpr const char * xyz
Position field (3D coordinates).
Header file describing a Node Instance.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
The MPI scheduler.
SchedulerPatchData patch_data
handle the data of the patches of the scheduler
PatchTree patch_tree
handle the tree structure of the patches
void scheduler_step(bool do_split_merge, bool do_load_balancing)
scheduler step
SynchronizedData synchronized_data
data that is synchroneous across all ranks
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())
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
void build_global_idx_map()
recompute id_patch_to_global_idx
static constexpr Tvec get_box_dim(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt)
Get the box size for a FCC lattice of xcnt * ycnt * zcnt lattice cells this function will throw if th...
void load_from_dump(std::string fname)
Load the state of the SPH model from a dump file.
Definition Model.hpp:934
Tvec get_box_dim_true_fcc_3d(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt)
Get the size of a periodic box for a true FCC lattice (ABC stacking).
Definition Model.hpp:98
void init()
Initialise the model and all the related data structures (patch scheduler in particular).
Definition Model.cpp:58
Tscal add_disc_3d(Tvec center, Tscal central_mass, u32 Npart, Tscal r_in, Tscal r_out, Tscal disc_mass, Tscal p, Tscal H_r_in, Tscal q)
Add a disc distribution.
Definition Model.hpp:335
void dump(std::string fname)
Dump the state of the SPH model to a file.
Definition Model.hpp:1004
void init_scheduler(u32 crit_split, u32 crit_merge)
Definition Model.hpp:83
void resize_sink_accreted_dust_mass()
Resize the per species accreted dust mass to the current sink count, zero padding the sinks without a...
Definition Model.hpp:225
Utility class used to move the objects between patches.
void reatribute_patch_objects(SerialPatchTree< T > &sptree, std::string position_field)
Reattribute objects based on a given position field.
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.
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)
bool has_edge(const std::string &name) const
Check whether an edge with the given name exists.
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.
MPI string gather / allgather helpers (declarations; implementations in shamalgs/src/collective/gathe...
void gather_str(const std::string &send_vec, std::string &recv_vec)
Gathers a string from all nodes and store the result in a std::string.
void add_disc2(u32 Npart, flt r_in, flt r_out, std::function< flt(flt)> sigma_profile, std::function< flt(flt)> cs_profile, std::function< flt(flt)> rot_profile, std::function< void(DiscOutput< flt >)> pusher)
void add_disc(u32 Npart, flt p, flt rho_0, flt m, flt r_in, flt r_out, flt q, Tpred_pusher &&part_pusher)
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for the sph model
std::vector< Tvec > & get_sink_pos(shamrock::solvergraph::SolverGraphSerializable &sync)
Named SoA getters (edges must already exist; registered by Solver::init_solver_graph)....
SinkEdges< Tvec > get_sink_edges(shamrock::solvergraph::SolverGraphSerializable &sync)
Fetch mutable references to the sink SoA synchronized edges.
void resize_sink_accreted_dust_mass(std::vector< Tscal > &accreted_dust_mass, size_t sink_count, u32 ndust)
Resize the flattened per species accreted dust mass to sink_count * ndust.
void add_sink(SinkEdges< Tvec > &e, typename SinkEdges< Tvec >::Tscal mass, Tvec pos, Tvec velocity, typename SinkEdges< Tvec >::Tscal accretion_radius)
Append one sink to the SoA edges.
bool has_sink_accreted_dust_mass(shamrock::solvergraph::SolverGraphSerializable &sync)
Whether the per species accreted dust mass sink edge is registered.
void set_sink_particles(SinkEdges< Tvec > &e, const std::vector< SinkParticle< Tvec > > &sinks)
Replace SoA sink edge contents from an AoS sink list (legacy dump migration).
void load_shamrock_dump(std::string fname, std::string &metadata_user, ShamrockCtx &ctx)
Load a Shamrock dump file and restore the state of the patches and retreive user metadata.
void write_shamrock_dump(std::string fname, std::string metadata_user, PatchScheduler &sched)
Write a Shamrock dump file containing the current state of the patches and user supplied metadata.
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
Helpers to access SPH sink particles stored as SoA synchronized data edges.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
The configuration for a sph solver.
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
#define ON_RANK_0(x)
Macro to execute code only on rank 0.
Definition worldInfo.hpp:73