Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Solver.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
16
17#include "shambase/assert.hpp"
20#include "shambase/memory.hpp"
23#include "shambase/string.hpp"
24#include "shambase/tabulate.hpp"
25#include "shambase/time.hpp"
33#include "shambackends/math.hpp"
34#include "shamcomm/logs.hpp"
36#include "shamcomm/wrapper.hpp"
97#include "shamphys/mhd.hpp"
133#include "shamsys/legacy/log.hpp"
137#include <memory>
138#include <stdexcept>
139#include <vector>
140
141namespace shambase {
142
143 template<class T>
144 std::shared_ptr<T> to_shared(T &&t) {
145 return std::make_shared<T>(std::forward<T>(t));
146 }
147} // namespace shambase
148
149namespace shammodels::sph {
150
151 namespace {
153 template<class Tvec>
154 std::shared_ptr<shamrock::solvergraph::INode> build_self_gravity_node(
155 SelfGravConfig &self_grav_config,
156 std::shared_ptr<shamrock::solvergraph::Indexes<u32>> sizes,
157 std::shared_ptr<shamrock::solvergraph::IDataEdge<shambase::VecComponent<Tvec>>>
158 gpart_mass,
159 std::shared_ptr<shamrock::solvergraph::IDataEdge<shambase::VecComponent<Tvec>>>
160 constant_G,
161 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> field_xyz,
162 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> field_axyz_ext) {
163
164 using Tscal = shambase::VecComponent<Tvec>;
165
166 Tscal eps_grav = shambase::get_check_ref(
167 std::get_if<SelfGravConfig::SofteningPlummer>(
168 &self_grav_config.softening_mode))
169 .epsilon;
170
171 std::shared_ptr<shamrock::solvergraph::INode> sg_inode;
172
173 if (self_grav_config.is_none()) {
175 "How did you get there ?\?\?!!!");
176 } else if (self_grav_config.is_direct()) {
177
179 std::get_if<SelfGravConfig::Direct>(&self_grav_config.config));
180
181 modules::SGDirectPlummer<Tvec> self_gravity_direct_node(
182 eps_grav, direct_config.reference_mode);
183 self_gravity_direct_node.set_edges(
184 sizes, gpart_mass, constant_G, field_xyz, field_axyz_ext);
185
186 sg_inode = shambase::to_shared(std::move(self_gravity_direct_node));
187
188 } else if (self_grav_config.is_mm()) {
189
191 std::get_if<SelfGravConfig::MM>(&self_grav_config.config));
192
193 auto run_sg_mm = [&](auto mm_order_tag) {
194 constexpr u32 order = decltype(mm_order_tag)::value;
195 modules::SGMMPlummer<Tvec, order> self_gravity_mm_node(
196 eps_grav, mm_config.opening_angle, mm_config.reduction_level);
197 self_gravity_mm_node.set_edges(
198 sizes, gpart_mass, constant_G, field_xyz, field_axyz_ext);
199 sg_inode = shambase::to_shared(std::move(self_gravity_mm_node));
200 };
201
202 switch (mm_config.order) {
203 case 1 : run_sg_mm(std::integral_constant<u32, 1>{}); break;
204 case 2 : run_sg_mm(std::integral_constant<u32, 2>{}); break;
205 case 3 : run_sg_mm(std::integral_constant<u32, 3>{}); break;
206 case 4 : run_sg_mm(std::integral_constant<u32, 4>{}); break;
207 case 5 : run_sg_mm(std::integral_constant<u32, 5>{}); break;
209 }
210
211 } else if (self_grav_config.is_fmm()) {
212
214 std::get_if<SelfGravConfig::FMM>(&self_grav_config.config));
215
216 auto run_sg_fmm = [&](auto fmm_order_tag) {
217 constexpr u32 order = decltype(fmm_order_tag)::value;
218 modules::SGFMMPlummer<Tvec, order> self_gravity_fmm_node(
219 eps_grav, fmm_config.opening_angle, fmm_config.reduction_level);
220 self_gravity_fmm_node.set_edges(
221 sizes, gpart_mass, constant_G, field_xyz, field_axyz_ext);
222 sg_inode = shambase::to_shared(std::move(self_gravity_fmm_node));
223 };
224
225 switch (fmm_config.order) {
226 case 1 : run_sg_fmm(std::integral_constant<u32, 1>{}); break;
227 case 2 : run_sg_fmm(std::integral_constant<u32, 2>{}); break;
228 case 3 : run_sg_fmm(std::integral_constant<u32, 3>{}); break;
229 case 4 : run_sg_fmm(std::integral_constant<u32, 4>{}); break;
230 case 5 : run_sg_fmm(std::integral_constant<u32, 5>{}); break;
232 }
233
234 } else if (self_grav_config.is_sfmm()) {
235
237 std::get_if<SelfGravConfig::SFMM>(&self_grav_config.config));
238
239 auto run_sg_sfmm = [&](auto sfmm_order_tag) {
240 constexpr u32 order = decltype(sfmm_order_tag)::value;
241 modules::SGSFMMPlummer<Tvec, order> self_gravity_sfmm_node(
242 eps_grav,
243 sfmm_config.opening_angle,
244 sfmm_config.leaf_lowering,
245 sfmm_config.reduction_level);
246 self_gravity_sfmm_node.set_edges(
247 sizes, gpart_mass, constant_G, field_xyz, field_axyz_ext);
248 sg_inode = shambase::to_shared(std::move(self_gravity_sfmm_node));
249 };
250
251 switch (sfmm_config.order) {
252 case 1 : run_sg_sfmm(std::integral_constant<u32, 1>{}); break;
253 case 2 : run_sg_sfmm(std::integral_constant<u32, 2>{}); break;
254 case 3 : run_sg_sfmm(std::integral_constant<u32, 3>{}); break;
255 case 4 : run_sg_sfmm(std::integral_constant<u32, 4>{}); break;
256 case 5 : run_sg_sfmm(std::integral_constant<u32, 5>{}); break;
258 }
259
260 } else {
262 "Self gravity config not supported, current state is : \n"
263 + nlohmann::json{self_grav_config}.dump(4));
264 }
265
266 return sg_inode;
267 }
268 } // namespace
269
270} // namespace shammodels::sph
271
272template<class Tvec, template<class> class Kern>
274
275 PatchScheduler &sched = scheduler();
276
277 auto &sync_data = sched.synchronized_data;
278
279 shamrock::patch::PatchDataLayerLayout &pdl = scheduler().pdl_old();
280 bool has_b_field = solver_config.has_field_b_on_rho();
281 bool has_psi_field = solver_config.has_field_psi_on_ch();
282 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
283 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
284 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
285
286 using namespace shamrock::solvergraph;
287
289 // synchronized edges
291
292 // register a synchronized edge only if missing, as it may have been restored from a dump
293 auto ensure_sync_edge
294 = [&](const std::string &name, const std::string &tex_symbol, auto init_value) {
295 using T = decltype(init_value);
296 if (!sync_data.has_edge(name)) {
297 auto edge
298 = sync_data.register_edge(name, IDataEdgeSerializable<T>(name, tex_symbol));
299 edge->data = std::move(init_value);
300 }
301 };
302
303 ensure_sync_edge("time", "t", Tscal{0});
304 ensure_sync_edge("dt", "dt", Tscal{0});
305 ensure_sync_edge("cfl_multiplier", "C_{\\rm CFL}", Tscal{1e-2});
306
307 // last cfl dt, plus one edge per cfl type if cfl detail is on
308 // (the types must match the ones evaluated in evolve_once)
309 ensure_sync_edge(cfl_dt_edge_name, "\\Delta t_{\\rm cfl}", Tscal{0});
310 if (solver_config.show_cfl_detail) {
311 auto ensure_cfl_detail_edge = [&](const std::string &type, const std::string &tex_type) {
312 ensure_sync_edge(
314 "\\Delta t_{\\rm cfl, " + tex_type + "}",
315 Tscal{0});
316 };
317 ensure_cfl_detail_edge("courant", "courant");
318 ensure_cfl_detail_edge("force", "force");
319 if (solver_config.do_nimhd()) {
320 ensure_cfl_detail_edge("NIMHD", "NIMHD");
321 }
322 if (has_psi_field) {
323 ensure_cfl_detail_edge("divB_cleaning", "\\nabla \\cdot B\\ cleaning");
324 }
325 if (has_s_j_field) {
326 ensure_cfl_detail_edge("dust1_fluid", "dust\\ 1\\ fluid");
327 ensure_cfl_detail_edge("dust_drift", "dust\\ drift");
328 }
329 // sinks can be added at any time, so this edge is always registered
330 ensure_cfl_detail_edge("sink_sink", "sink-sink");
331 }
332
333 ensure_sync_edge("sink_pos", "\\bf{r}_{\\mathrm{sink}}", std::vector<Tvec>{});
334 ensure_sync_edge("sink_vel", "\\bf{v}_{\\mathrm{sink}}", std::vector<Tvec>{});
335 ensure_sync_edge("sink_acc_sph", "\\bf{a}_{\\mathrm{sink, sph}}", std::vector<Tvec>{});
336 ensure_sync_edge("sink_acc_ext", "\\bf{a}_{\\mathrm{sink, ext}}", std::vector<Tvec>{});
337 ensure_sync_edge("sink_mass", "m_{\\mathrm{sink}}", std::vector<Tscal>{});
338 ensure_sync_edge("sink_angular_momentum", "\\bf{L}_{\\mathrm{sink}}", std::vector<Tvec>{});
339 ensure_sync_edge("sink_accretion_radius", "r_{\\mathrm{accretion}}", std::vector<Tscal>{});
340 // per species accreted dust mass, flattened as [i_sink * ndust + j_dust]
341 if (has_s_j_field) {
342 ensure_sync_edge(
343 "sink_accreted_dust_mass", "m_{\\mathrm{sink, acc}, j}", std::vector<Tscal>{});
344 }
345
347 // main solver graph
349
350 SolverGraph &solver_graph = storage.solver_graph;
351
352 solver_graph.register_edge(
353 "scheduler_patchdata", PatchDataLayerRefs("patchdatas", "\\mathbb{U}_{\\rm patch}"));
354 solver_graph.register_edge("part_counts", Indexes<u32>("Npart_patch", "N_{\\rm part}_p"));
355
356 solver_graph.register_edge("dt_half", IDataEdge<Tscal>("dt_half", "\\frac{dt}{2}"));
357 solver_graph.register_edge("gpart_mass", IDataEdge<Tscal>("m", "m"));
358
359 solver_graph.register_edge("xyz", FieldRefs<Tvec>("xyz", "\\mathbf{r}"));
360 solver_graph.register_edge("vxyz", FieldRefs<Tvec>("vxyz", "\\mathbf{v}"));
361 solver_graph.register_edge("axyz", FieldRefs<Tvec>("axyz", "\\mathbf{a}"));
362 solver_graph.register_edge("uint", FieldRefs<Tscal>("uint", "u_{\\rm int}"));
363 solver_graph.register_edge("duint", FieldRefs<Tscal>("duint", "du_{\\rm int}"));
364 solver_graph.register_edge("hpart", FieldRefs<Tscal>("hpart", "h_{\\rm part}"));
365
366 if (has_b_field) {
367 solver_graph.register_edge("B/rho", FieldRefs<Tvec>("B/rho", "B_{\\rho}"));
368 solver_graph.register_edge("dB/rho", FieldRefs<Tvec>("dB/rho", "dB_{\\rho}"));
369 }
370 if (has_psi_field) {
371 solver_graph.register_edge("psi/ch", FieldRefs<Tscal>("psi/ch", "\\psi_{\\rm ch}"));
372 solver_graph.register_edge("dpsi/ch", FieldRefs<Tscal>("dpsi/ch", "d\\psi_{\\rm ch}"));
373 }
374 if (has_epsilon_field) {
375 solver_graph.register_edge("epsilon", FieldRefs<Tscal>("epsilon", "\\epsilon"));
376 solver_graph.register_edge("dtepsilon", FieldRefs<Tscal>("dtepsilon", "d\\epsilon"));
377 }
378 if (has_deltav_field) {
379 solver_graph.register_edge("deltav", FieldRefs<Tvec>("deltav", "\\Delta v"));
380 solver_graph.register_edge("dtdeltav", FieldRefs<Tvec>("dtdeltav", "d\\Delta v"));
381 }
382 if (has_s_j_field) {
383 solver_graph.register_edge("s_j", FieldRefs<Tscal>("s_j", "S_j"));
384 solver_graph.register_edge("ds_j_dt", FieldRefs<Tscal>("ds_j_dt", "dS_j/dt"));
385
386 u32 ndust = solver_config.dust_config.get_dust_nvar();
387 solver_graph.register_edge("Ts_j", Field<Tscal>(ndust, "Ts_j", "Ts_j"));
388 }
389
390 {
391 auto set_gpart_mass = solver_graph.register_node(
392 "set_gpart_mass", NodeSetEdge<IDataEdge<Tscal>>([&](IDataEdge<Tscal> &gpart_mass) {
393 gpart_mass.data = solver_config.gpart_mass;
394 }));
395 shambase::get_check_ref(set_gpart_mass)
396 .set_edges(solver_graph.get_edge_ptr<IDataEdge<Tscal>>("gpart_mass"));
397 }
398
400 // attach fields to scheduler
402 {
403 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> attach_field_sequence;
404
405 {
406 auto set_scheduler_patchdata = solver_graph.register_node(
407 "set_scheduler_patchdata",
408 NodeSetEdge<PatchDataLayerRefs>([&](PatchDataLayerRefs &scheduler_patchdata) {
409 scheduler_patchdata.free_alloc();
410 scheduler().for_each_patchdata_nonempty(
411 [&](const shamrock::patch::Patch &p,
413 scheduler_patchdata.patchdatas.add_obj(p.id_patch, std::ref(pdat));
414 });
415 }));
416 shambase::get_check_ref(set_scheduler_patchdata)
417 .set_edges(solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"));
418 attach_field_sequence.push_back(set_scheduler_patchdata);
419 }
420
421 {
422 auto attach_part_counts
423 = solver_graph.register_node("attach_part_counts", GetObjCntFromLayer{});
424 shambase::get_check_ref(attach_part_counts)
425 .set_edges(
426 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
427 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"));
428 attach_field_sequence.push_back(attach_part_counts);
429 }
430
431 {
432 auto attach_xyz
433 = solver_graph.register_node("attach_xyz", GetFieldRefFromLayer<Tvec>(pdl, "xyz"));
434 shambase::get_check_ref(attach_xyz)
435 .set_edges(
436 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
437 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"));
438 attach_field_sequence.push_back(attach_xyz);
439 }
440
441 {
442 auto attach_vxyz = solver_graph.register_node(
443 "attach_vxyz", GetFieldRefFromLayer<Tvec>(pdl, "vxyz"));
444 shambase::get_check_ref(attach_vxyz)
445 .set_edges(
446 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
447 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("vxyz"));
448 attach_field_sequence.push_back(attach_vxyz);
449 }
450
451 {
452 auto attach_axyz = solver_graph.register_node(
453 "attach_axyz", GetFieldRefFromLayer<Tvec>(pdl, "axyz"));
454 shambase::get_check_ref(attach_axyz)
455 .set_edges(
456 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
457 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("axyz"));
458 attach_field_sequence.push_back(attach_axyz);
459 }
460
461 {
462 auto attach_uint = solver_graph.register_node(
463 "attach_uint", GetFieldRefFromLayer<Tscal>(pdl, "uint"));
464 shambase::get_check_ref(attach_uint)
465 .set_edges(
466 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
467 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("uint"));
468 attach_field_sequence.push_back(attach_uint);
469 }
470
471 {
472 auto attach_duint = solver_graph.register_node(
473 "attach_duint", GetFieldRefFromLayer<Tscal>(pdl, "duint"));
474 shambase::get_check_ref(attach_duint)
475 .set_edges(
476 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
477 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("duint"));
478 attach_field_sequence.push_back(attach_duint);
479 }
480
481 {
482 auto attach_hpart = solver_graph.register_node(
483 "attach_hpart", GetFieldRefFromLayer<Tscal>(pdl, "hpart"));
484 shambase::get_check_ref(attach_hpart)
485 .set_edges(
486 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
487 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("hpart"));
488 attach_field_sequence.push_back(attach_hpart);
489 }
490
491 if (has_b_field) {
492 auto attach_B_on_rho = solver_graph.register_node(
493 "attach_B_on_rho", GetFieldRefFromLayer<Tvec>(pdl, "B/rho"));
494 shambase::get_check_ref(attach_B_on_rho)
495 .set_edges(
496 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
497 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("B/rho"));
498 attach_field_sequence.push_back(attach_B_on_rho);
499 }
500
501 if (has_b_field) {
502 auto attach_dB_on_rho = solver_graph.register_node(
503 "attach_dB_on_rho", GetFieldRefFromLayer<Tvec>(pdl, "dB/rho"));
504 shambase::get_check_ref(attach_dB_on_rho)
505 .set_edges(
506 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
507 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("dB/rho"));
508 attach_field_sequence.push_back(attach_dB_on_rho);
509 }
510
511 if (has_psi_field) {
512 auto attach_psi_on_ch = solver_graph.register_node(
513 "attach_psi_on_ch", GetFieldRefFromLayer<Tscal>(pdl, "psi/ch"));
514 shambase::get_check_ref(attach_psi_on_ch)
515 .set_edges(
516 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
517 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("psi/ch"));
518 attach_field_sequence.push_back(attach_psi_on_ch);
519 }
520
521 if (has_psi_field) {
522 auto attach_dpsi_on_ch = solver_graph.register_node(
523 "attach_dpsi_on_ch", GetFieldRefFromLayer<Tscal>(pdl, "dpsi/ch"));
524 shambase::get_check_ref(attach_dpsi_on_ch)
525 .set_edges(
526 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
527 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("dpsi/ch"));
528 attach_field_sequence.push_back(attach_dpsi_on_ch);
529 }
530
531 if (has_epsilon_field) {
532 auto attach_epsilon = solver_graph.register_node(
533 "attach_epsilon", GetFieldRefFromLayer<Tscal>(pdl, "epsilon"));
534 shambase::get_check_ref(attach_epsilon)
535 .set_edges(
536 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
537 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("epsilon"));
538 attach_field_sequence.push_back(attach_epsilon);
539 }
540
541 if (has_epsilon_field) {
542 auto attach_dtepsilon = solver_graph.register_node(
543 "attach_dtepsilon", GetFieldRefFromLayer<Tscal>(pdl, "dtepsilon"));
544 shambase::get_check_ref(attach_dtepsilon)
545 .set_edges(
546 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
547 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("dtepsilon"));
548 attach_field_sequence.push_back(attach_dtepsilon);
549 }
550
551 if (has_deltav_field) {
552 auto attach_deltav = solver_graph.register_node(
553 "attach_deltav", GetFieldRefFromLayer<Tvec>(pdl, "deltav"));
554 shambase::get_check_ref(attach_deltav)
555 .set_edges(
556 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
557 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("deltav"));
558 attach_field_sequence.push_back(attach_deltav);
559 }
560
561 if (has_deltav_field) {
562 auto attach_dtdeltav = solver_graph.register_node(
563 "attach_dtdeltav", GetFieldRefFromLayer<Tvec>(pdl, "dtdeltav"));
564 shambase::get_check_ref(attach_dtdeltav)
565 .set_edges(
566 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
567 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("dtdeltav"));
568 attach_field_sequence.push_back(attach_dtdeltav);
569 }
570
571 if (has_s_j_field) {
572 auto attach_s_j
573 = solver_graph.register_node("attach_s_j", GetFieldRefFromLayer<Tscal>(pdl, "s_j"));
574 shambase::get_check_ref(attach_s_j)
575 .set_edges(
576 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
577 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("s_j"));
578 attach_field_sequence.push_back(attach_s_j);
579 }
580
581 if (has_s_j_field) {
582 auto attach_ds_j_dt = solver_graph.register_node(
583 "attach_ds_j_dt", GetFieldRefFromLayer<Tscal>(pdl, "ds_j_dt"));
584 shambase::get_check_ref(attach_ds_j_dt)
585 .set_edges(
586 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"),
587 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("ds_j_dt"));
588 attach_field_sequence.push_back(attach_ds_j_dt);
589 }
590 solver_graph.register_node(
591 "attach fields to scheduler",
592 OperationSequence("attach fields", std::move(attach_field_sequence)));
593 }
594
596 // leapfrog predictor
598
599 {
600
601 auto make_half_step_sequence = [&](std::string prefix) {
602 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> half_step_sequence;
603
604 {
605 auto half_step_vxyz = solver_graph.register_node(
607 shambase::get_check_ref(half_step_vxyz)
608 .set_edges(
609 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
610 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("axyz"),
611 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
612 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("vxyz"));
613 half_step_sequence.push_back(half_step_vxyz);
614 }
615
616 {
617 auto half_step_uint = solver_graph.register_node(
619 shambase::get_check_ref(half_step_uint)
620 .set_edges(
621 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
622 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("duint"),
623 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
624 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("uint"));
625 half_step_sequence.push_back(half_step_uint);
626 }
627
628 if (has_b_field) {
629 auto half_step_B_on_rho = solver_graph.register_node(
631 shambase::get_check_ref(half_step_B_on_rho)
632 .set_edges(
633 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
634 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("dB/rho"),
635 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
636 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("B/rho"));
637 half_step_sequence.push_back(half_step_B_on_rho);
638 }
639
640 if (has_psi_field) {
641 auto half_step_psi_on_ch = solver_graph.register_node(
642 prefix + "_psi_on_ch", shammodels::common::modules::ForwardEuler<Tscal>{});
643 shambase::get_check_ref(half_step_psi_on_ch)
644 .set_edges(
645 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
646 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("dpsi/ch"),
647 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
648 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("psi/ch"));
649 half_step_sequence.push_back(half_step_psi_on_ch);
650 }
651
652 if (has_epsilon_field) {
653 auto half_step_epsilon = solver_graph.register_node(
655 shambase::get_check_ref(half_step_epsilon)
656 .set_edges(
657 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
658 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("dtepsilon"),
659 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
660 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("epsilon"));
661 half_step_sequence.push_back(half_step_epsilon);
662 }
663
664 if (has_deltav_field) {
665 auto half_step_deltav = solver_graph.register_node(
667 shambase::get_check_ref(half_step_deltav)
668 .set_edges(
669 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
670 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("dtdeltav"),
671 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
672 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("deltav"));
673 half_step_sequence.push_back(half_step_deltav);
674 }
675
676 if (has_s_j_field) {
677 u32 ndust = solver_config.dust_config.get_dust_nvar();
678
679 auto &cfg = solver_config.dust_config.get_monofluid_tva();
680
681 if (cfg.ensure_s_j_positivity) {
682 auto half_step_s_j = solver_graph.register_node(
683 prefix + "_s_j",
685 shambase::get_check_ref(half_step_s_j)
686 .set_edges(
687 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
688 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("ds_j_dt"),
689 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
690 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("s_j"));
691 half_step_sequence.push_back(half_step_s_j);
692 } else {
693 auto half_step_s_j = solver_graph.register_node(
695 shambase::get_check_ref(half_step_s_j)
696 .set_edges(
697 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
698 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("ds_j_dt"),
699 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
700 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("s_j"));
701 half_step_sequence.push_back(half_step_s_j);
702 }
703
704 if (cfg.should_clamp_dust_density()) {
705 auto hfactd_edge = IDataEdge<Tscal>::make_shared("hfactd", "hfactd");
706 hfactd_edge->data = Kernel::hfactd;
707
708 auto clamp_frac_edge
709 = IDataEdge<Tscal>::make_shared("clamp_frac", "clamp_frac");
710 clamp_frac_edge->data = cfg.get_clamp_dust_frac();
711
712 auto half_step_s_j_density_clamp = solver_graph.register_node(
713 prefix + "_s_j_density_clamp",
715 shambase::get_check_ref(half_step_s_j_density_clamp)
716 .set_edges(
717 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
718 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("gpart_mass"),
719 hfactd_edge,
720 clamp_frac_edge,
721 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("hpart"),
722 solver_graph.get_edge_ptr<FieldRefs<Tscal>>("s_j"));
723 half_step_sequence.push_back(half_step_s_j_density_clamp);
724 }
725 }
726
727 return OperationSequence("half step", std::move(half_step_sequence));
728 };
729
730 solver_graph.register_node("half_step1", make_half_step_sequence("half_step1"));
731 solver_graph.register_node("half_step2", make_half_step_sequence("half_step2"));
732
733 {
734 auto full_step_xyz = solver_graph.register_node(
736 shambase::get_check_ref(full_step_xyz)
737 .set_edges(
738 sync_data.get_edge_ptr<IDataEdge<Tscal>>("dt"),
739 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("vxyz"),
740 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
741 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"));
742 }
743
744 {
745 auto leapfrog_predictor = solver_graph.register_node(
746 "leapfrog predictor",
748 "leapfrog predictor",
749 {
750 solver_graph.get_node_ptr_base("half_step1"),
751 solver_graph.get_node_ptr_base("full_step_xyz"),
752 solver_graph.get_node_ptr_base("half_step2"),
753 }));
754 }
755 }
756
758 // Part killing step
760 bool do_part_killing_step = solver_config.particle_killing.kill_list.size() > 0;
761
762 if (do_part_killing_step) {
763
764 auto patchdatas = solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata");
765 auto xyz_edge = solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz");
766
767 auto part_to_remove = solver_graph.register_edge(
768 "part_to_remove", DistributedBuffers<u32>("part_to_remove", "part_to_remove"));
769
770 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> part_kill_sequence{};
771
772 {
773
774 auto empty_part_to_remove
775 = solver_graph.register_node("empty_part_to_remove", NodeFreeAlloc{});
776 shambase::get_check_ref(empty_part_to_remove).set_edges(part_to_remove);
777 part_kill_sequence.push_back(empty_part_to_remove);
778 }
779
780 using kill_t = typename ParticleKillingConfig<Tvec>::kill_t;
781 using kill_sphere = typename ParticleKillingConfig<Tvec>::Sphere;
782
783 // selectors
784 for (kill_t &kill_obj : solver_config.particle_killing.kill_list) {
785 if (kill_sphere *kill_info = std::get_if<kill_sphere>(&kill_obj)) {
786
788 kill_info->center, kill_info->radius);
789 node_selector.set_edges(xyz_edge, part_to_remove);
790
791 part_kill_sequence.push_back(
792 std::make_shared<decltype(node_selector)>(std::move(node_selector)));
793 }
794 }
795
796 { // killing
797 modules::KillParticles node_killer{};
798 node_killer.set_edges(part_to_remove, patchdatas);
799
800 part_kill_sequence.push_back(
801 std::make_shared<decltype(node_killer)>(std::move(node_killer)));
802 }
803
804 // update part counts and spans since particles have been killed and thus
805 // patches can become empty
806 part_kill_sequence.push_back(solver_graph.get_node_ptr_base("attach fields to scheduler"));
807
808 solver_graph.register_node(
809 "part killing step",
810 OperationSequence("part killing step", std::move(part_kill_sequence)));
811 }
812
813 {
814 auto dt_to_half_dt = solver_graph.register_node(
815 "dt_to_half_dt",
817 [](const IDataEdge<Tscal> &dt, IDataEdge<Tscal> &half_dt) {
818 half_dt.data = dt.data / 2;
819 }});
820 shambase::get_check_ref(dt_to_half_dt)
821 .set_edges(
822 sync_data.get_edge_ptr<IDataEdge<Tscal>>("dt"),
823 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"));
824 }
825
826 storage.part_counts
827 = std::make_shared<shamrock::solvergraph::Indexes<u32>>("part_counts", "N_{\\rm part}");
828
829 storage.part_counts_with_ghost = std::make_shared<shamrock::solvergraph::Indexes<u32>>(
830 "part_counts_with_ghost", "N_{\\rm part, with ghost}");
831
832 storage.patch_rank_owner = std::make_shared<shamrock::solvergraph::RankGetter>(
833 [&](u64 patch_id) -> u32 {
834 return scheduler().get_patch_rank_owner(patch_id);
835 },
836 "patch_rank_owner",
837 "rank");
838
839 // merged ghost spans
840 storage.positions_with_ghosts
841 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>("part_pos", "\\mathbf{r}");
842 storage.hpart_with_ghosts
843 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("h_part", "h");
844
845 storage.neigh_cache
846 = std::make_shared<shammodels::sph::solvergraph::NeighCache>("neigh_cache", "neigh");
847
848 storage.omega = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "omega", "\\Omega");
849
850 storage.MagCurrentJ
851 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "MagCurrentJ", "\\mathbf{J}");
852
853 if (solver_config.has_field_alphaAV()) {
854 storage.alpha_av_updated = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
855 1, "alpha_av_updated", "\\alpha_{\\rm AV}");
856 }
857
858 storage.pressure = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "pressure", "P");
859 storage.soundspeed
860 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "soundspeed", "c_s");
861
862 storage.exchange_gz_alpha
863 = std::make_shared<shamrock::solvergraph::ExchangeGhostField<Tscal>>();
864 storage.exchange_gz_node
865 = std::make_shared<shamrock::solvergraph::ExchangeGhostLayer>(storage.ghost_layout);
866 storage.exchange_gz_positions
867 = std::make_shared<shamrock::solvergraph::ExchangeGhostLayer>(storage.xyzh_ghost_layout);
868
869 storage.exchange_gz_J = std::make_shared<shamrock::solvergraph::ExchangeGhostField<Tvec>>();
870
872 // sink accretion
874 {
875 solver_graph.register_edge("has_sinks", IDataEdge<bool>("has_sinks", "has_sinks"));
876
877 auto set_has_sinks = solver_graph.register_node(
878 "set_has_sinks", NodeSetEdge<IDataEdge<bool>>([&](IDataEdge<bool> &has_sinks_edge) {
879 has_sinks_edge.data = has_sinks<Tvec>(sync_data);
880 }));
881 shambase::get_check_ref(set_has_sinks)
882 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_sinks"));
883
884 auto free_xyz = solver_graph.register_node("free_xyz_refs", NodeFreeAlloc{});
885 shambase::get_check_ref(free_xyz).set_edges(
886 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"));
887
888 auto free_vxyz = solver_graph.register_node("free_vxyz_refs", NodeFreeAlloc{});
889 shambase::get_check_ref(free_vxyz).set_edges(
890 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("vxyz"));
891
892 auto free_axyz = solver_graph.register_node("free_axyz_refs", NodeFreeAlloc{});
893 shambase::get_check_ref(free_axyz).set_edges(
894 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("axyz"));
895
896 // dust fields needed to track the accreted dust mass per species (monofluid TVA only)
897 u32 sink_acc_ndust = has_s_j_field ? solver_config.dust_config.get_dust_nvar() : 0;
898
899 std::optional<std::shared_ptr<FieldRefs<Tscal>>> sink_acc_hpart = std::nullopt;
900 std::optional<std::shared_ptr<FieldRefs<Tscal>>> sink_acc_s_j = std::nullopt;
901
902 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> attach_sink_dust_fields{};
903 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> free_sink_dust_fields{};
904
905 auto add_sink_dust_field = [&](const std::string &name) {
906 auto edge = solver_graph.get_edge_ptr<FieldRefs<Tscal>>(name);
907 attach_sink_dust_fields.push_back(solver_graph.get_node_ptr_base("attach_" + name));
908 auto free_node
909 = solver_graph.register_node("free_" + name + "_refs_sink", NodeFreeAlloc{});
910 shambase::get_check_ref(free_node).set_edges(edge);
911 free_sink_dust_fields.push_back(free_node);
912 return edge;
913 };
914
915 if (has_s_j_field) {
916 sink_acc_hpart = add_sink_dust_field("hpart");
917 sink_acc_s_j = add_sink_dust_field("s_j");
918 }
919
920 // sink synchronized edges, kept around as they are used by several nodes below
921 auto sink_positions
922 = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_pos");
923 auto sink_velocities
924 = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_vel");
925 auto sink_accelerations
926 = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_sph");
927 auto sink_angmom = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>(
928 "sink_angular_momentum");
929 auto sink_mass
930 = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tscal>>>("sink_mass");
931 auto sink_accr_radii = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tscal>>>(
932 "sink_accretion_radius");
933 // only registered with monofluid TVA dust (see synchronized edges above)
934 std::optional<std::shared_ptr<IDataEdgeSerializable<std::vector<Tscal>>>>
935 sink_accreted_dust_mass = std::nullopt;
936 if (sink_acc_ndust > 0) {
937 sink_accreted_dust_mass
938 = sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tscal>>>(
939 "sink_accreted_dust_mass");
940 }
941
942 solver_graph.register_edge(
943 "sink_accretion_table", Field<u32>(1, "sink_accretion_table", "\\mathrm{acc}"));
944
945 auto flag_node = solver_graph.register_node(
946 "flag_accrete_hard", modules::SinkParticlesFlagAccreteHard<Tvec>{});
947 shambase::get_check_ref(flag_node).set_edges(
948 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
949 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"),
950 sink_positions,
951 sink_accr_radii,
952 solver_graph.get_edge_ptr<Field<u32>>("sink_accretion_table"));
953
954 auto qty_node = solver_graph.register_node(
955 "accrete_quantities",
956 modules::SinkParticlesAccreteQuantities<Tvec>{sink_acc_ndust, Kernel::hfactd});
957 shambase::get_check_ref(qty_node).set_edges(
958 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("gpart_mass"),
959 sync_data.get_edge_ptr<IDataEdge<Tscal>>("dt"),
960 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
961 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"),
962 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("vxyz"),
963 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("axyz"),
964 sink_acc_hpart,
965 sink_acc_s_j,
966 solver_graph.get_edge_ptr<Field<u32>>("sink_accretion_table"),
967 sink_positions,
968 sink_velocities,
969 sink_accelerations,
970 sink_angmom,
971 sink_mass,
972 sink_accreted_dust_mass);
973
974 auto evict_node = solver_graph.register_node(
975 "evict_accreted_particles", modules::SinkParticlesEvictAccretedParticles<Tvec>{});
976 shambase::get_check_ref(evict_node)
977 .set_edges(
978 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
979 solver_graph.get_edge_ptr<Field<u32>>("sink_accretion_table"),
980 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"));
981
982 // the "time_step" sequence (set_gpart_mass, attach fields to scheduler, ...)
983 // has not run yet at this stage of the timestep
984 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> accretion_sequence{
985 solver_graph.get_node_ptr_base("set_gpart_mass"),
986 solver_graph.get_node_ptr_base("set_scheduler_patchdata"),
987 solver_graph.get_node_ptr_base("attach_part_counts"),
988 solver_graph.get_node_ptr_base("attach_xyz"),
989 solver_graph.get_node_ptr_base("attach_vxyz"),
990 solver_graph.get_node_ptr_base("attach_axyz"),
991 };
992 accretion_sequence.insert(
993 accretion_sequence.end(),
994 attach_sink_dust_fields.begin(),
995 attach_sink_dust_fields.end());
996
997 // Actually perform the accretion
998 accretion_sequence.push_back(flag_node);
999 accretion_sequence.push_back(qty_node);
1000 accretion_sequence.push_back(evict_node);
1001
1002 // free the refs since the particle counts may have changed
1003 accretion_sequence.push_back(free_xyz);
1004 accretion_sequence.push_back(free_vxyz);
1005 accretion_sequence.push_back(free_axyz);
1006 accretion_sequence.insert(
1007 accretion_sequence.end(), free_sink_dust_fields.begin(), free_sink_dust_fields.end());
1008
1009 auto accretion_body = solver_graph.register_node(
1010 "sink accretion body",
1011 OperationSequence("sink accretion body", std::move(accretion_sequence)));
1012
1013 // skip the accretion entirely when there are no sinks
1014 auto if_has_sinks = solver_graph.register_node(
1015 "if_has_sinks", OperationIf("if_has_sinks", accretion_body));
1016 shambase::get_check_ref(if_has_sinks)
1017 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_sinks"));
1018
1019 // register the actual node that will be used
1020 solver_graph.register_node(
1021 "sink accretion",
1023 "sink accretion",
1024 {
1025 set_has_sinks,
1026 if_has_sinks,
1027 }));
1028 }
1029
1031 // sink ext force (pairwise self-gravity between sink particles)
1033 {
1034 solver_graph.register_edge("sink_ext_force_G", IDataEdge<Tscal>("G", "G"));
1035 solver_graph.register_edge(
1036 "sink_ext_force_epsilon", IDataEdge<Tscal>("epsilon_grav_sink", "\\epsilon"));
1037
1038 auto set_G = solver_graph.register_node(
1039 "set_sink_ext_force_G", NodeSetEdge<IDataEdge<Tscal>>([&](IDataEdge<Tscal> &g_edge) {
1040 g_edge.data = solver_config.get_constant_G();
1041 }));
1042 shambase::get_check_ref(set_G).set_edges(
1043 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_ext_force_G"));
1044
1045 auto set_epsilon = solver_graph.register_node(
1046 "set_sink_ext_force_epsilon",
1047 NodeSetEdge<IDataEdge<Tscal>>([&](IDataEdge<Tscal> &epsilon_edge) {
1048 epsilon_edge.data = 1e-9;
1049 }));
1050 shambase::get_check_ref(set_epsilon)
1051 .set_edges(solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_ext_force_epsilon"));
1052
1053 auto reset_acc_ext = solver_graph.register_node(
1054 "reset_sink_acc_ext",
1055 NodeSetEdge<IDataEdgeSerializable<std::vector<Tvec>>>(
1056 [](IDataEdgeSerializable<std::vector<Tvec>> &acc_ext) {
1057 for (Tvec &a : acc_ext.data) {
1058 a = Tvec{};
1059 }
1060 }));
1061 shambase::get_check_ref(reset_acc_ext)
1062 .set_edges(
1063 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_ext"));
1064
1065 auto self_gravity
1066 = solver_graph.register_node("sink_self_gravity", modules::SinkSelfGravityHost<Tvec>{});
1067 shambase::get_check_ref(self_gravity)
1068 .set_edges(
1069 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_ext_force_G"),
1070 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_ext_force_epsilon"),
1071 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_pos"),
1072 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tscal>>>("sink_mass"),
1073 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_ext"));
1074
1075 auto ext_force_body = solver_graph.register_node(
1076 "sink_ext_force_body",
1078 "sink ext force body",
1079 {
1080 set_G,
1081 set_epsilon,
1082 reset_acc_ext,
1083 self_gravity,
1084 }));
1085
1086 // register the actual node that will be used, gated on the same "has_sinks" edge
1087 // maintained by the "sink accretion" section above
1088 auto sink_ext_force = solver_graph.register_node(
1089 "sink ext force", OperationIf("sink ext force", ext_force_body));
1090 shambase::get_check_ref(sink_ext_force)
1091 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_sinks"));
1092 }
1093
1095 // sink predictor step (leapfrog kick-drift of the sink particles themselves)
1097 {
1098 solver_graph.register_edge(
1099 "sink_predictor_dt_half", IDataEdge<Tscal>("dt_half", "\\frac{dt}{2}"));
1100
1101 auto sink_predictor_dt_to_half_dt = solver_graph.register_node(
1102 "sink_predictor_dt_to_half_dt",
1104 [](const IDataEdge<Tscal> &dt, IDataEdge<Tscal> &half_dt) {
1105 half_dt.data = dt.data / 2;
1106 }});
1107 shambase::get_check_ref(sink_predictor_dt_to_half_dt)
1108 .set_edges(
1109 sync_data.get_edge_ptr<IDataEdge<Tscal>>("dt"),
1110 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_predictor_dt_half"));
1111
1112 auto sink_predictor_vel_update = solver_graph.register_node(
1113 "sink_predictor_vel_update", ForwardEulerHost2Deriv<Tvec, Tscal>{});
1114 shambase::get_check_ref(sink_predictor_vel_update)
1115 .set_edges(
1116 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("sink_predictor_dt_half"),
1117 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_sph"),
1118 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_ext"),
1119 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_vel"));
1120
1121 auto sink_predictor_pos_update = solver_graph.register_node(
1122 "sink_predictor_pos_update", ForwardEulerHost<Tvec, Tscal>{});
1123 shambase::get_check_ref(sink_predictor_pos_update)
1124 .set_edges(
1125 sync_data.get_edge_ptr<IDataEdge<Tscal>>("dt"),
1126 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_vel"),
1127 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_pos"));
1128
1129 auto sink_predictor_body = solver_graph.register_node(
1130 "sink_predictor_body",
1132 "sink predictor body",
1133 {
1134 // recompute the sink self-gravity at the current (pre-predictor) sink
1135 // positions before using it to kick the sink velocities
1136 solver_graph.get_node_ptr_base("sink ext force"),
1137 sink_predictor_dt_to_half_dt,
1138 sink_predictor_vel_update,
1139 sink_predictor_pos_update,
1140 }));
1141
1142 // register the actual node that will be used, gated on the same "has_sinks" edge
1143 // maintained by the "sink accretion" section above
1144 auto sink_predictor = solver_graph.register_node(
1145 "sink predictor", OperationIf("sink predictor", sink_predictor_body));
1146 shambase::get_check_ref(sink_predictor)
1147 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_sinks"));
1148 }
1149
1151 // sink corrector step (leapfrog kick of the sink particles themselves)
1153 {
1154 auto sink_corrector_vel_update = solver_graph.register_node(
1155 "sink_corrector_vel_update", ForwardEulerHost2Deriv<Tvec, Tscal>{});
1156 shambase::get_check_ref(sink_corrector_vel_update)
1157 .set_edges(
1158 solver_graph.get_edge_ptr<IDataEdge<Tscal>>("dt_half"),
1159 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_sph"),
1160 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_acc_ext"),
1161 sync_data.get_edge_ptr<IDataEdgeSerializable<std::vector<Tvec>>>("sink_vel"));
1162
1163 // register the actual node that will be used, gated on the same "has_sinks" edge
1164 // maintained by the "sink accretion" section above
1165 auto sink_corrector = solver_graph.register_node(
1166 "sink corrector", OperationIf("sink corrector", sink_corrector_vel_update));
1167 shambase::get_check_ref(sink_corrector)
1168 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_sinks"));
1169 }
1170
1172 // external force (point mass) accretion
1174 {
1175 using EF_PointMass = typename Config::ExtForceConfig::PointMass;
1176 using EF_PN_PW = typename Config::ExtForceConfig::PN_PW;
1177 using EF_LenseThirring = typename Config::ExtForceConfig::LenseThirring;
1178
1179 // Collect the accretion centers and radii of every external force able to accrete.
1180 // This is re-done at every timestep, hence a lambda rather than a one shot list.
1181 auto collect_accretors = [&]() {
1182 std::vector<Tvec> positions{};
1183 std::vector<Tscal> radii{};
1184 for (auto &var_force : solver_config.ext_force_config.ext_forces) {
1185 if (EF_PointMass *ext_force = std::get_if<EF_PointMass>(&var_force.val)) {
1186 positions.push_back(ext_force->central_pos);
1187 radii.push_back(ext_force->Racc);
1188 } else if (EF_PN_PW *ext_force = std::get_if<EF_PN_PW>(&var_force.val)) {
1189 positions.push_back(ext_force->central_pos);
1190 radii.push_back(ext_force->Racc);
1191 } else if (
1192 EF_LenseThirring *ext_force = std::get_if<EF_LenseThirring>(&var_force.val)) {
1193 positions.push_back(ext_force->central_pos);
1194 radii.push_back(ext_force->Racc);
1195 }
1196 }
1197 return std::pair{std::move(positions), std::move(radii)};
1198 };
1199
1200 solver_graph.register_edge(
1201 "ext_force_accretion_pos",
1202 IDataEdge<std::vector<Tvec>>("ext_force_accretion_pos", "\\mathbf{r}_{\\rm acc, ext}"));
1203 solver_graph.register_edge(
1204 "ext_force_accretion_racc",
1205 IDataEdge<std::vector<Tscal>>("ext_force_accretion_racc", "R_{\\rm acc, ext}"));
1206 solver_graph.register_edge(
1207 "ext_force_accretion_table",
1208 Field<u32>(1, "ext_force_accretion_table", "\\mathrm{acc}"));
1209 solver_graph.register_edge(
1210 "has_ext_force_accretion",
1211 IDataEdge<bool>("has_ext_force_accretion", "\\rm has\\_ext\\_force\\_accretion"));
1212
1213 auto set_accretion_pos = solver_graph.register_node(
1214 "set_ext_force_accretion_pos",
1215 NodeSetEdge<IDataEdge<std::vector<Tvec>>>(
1216 [collect_accretors](IDataEdge<std::vector<Tvec>> &accretion_pos) {
1217 accretion_pos.data = std::get<0>(collect_accretors());
1218 }));
1219 shambase::get_check_ref(set_accretion_pos)
1220 .set_edges(
1221 solver_graph.get_edge_ptr<IDataEdge<std::vector<Tvec>>>("ext_force_accretion_pos"));
1222
1223 auto set_accretion_racc = solver_graph.register_node(
1224 "set_ext_force_accretion_racc",
1225 NodeSetEdge<IDataEdge<std::vector<Tscal>>>(
1226 [collect_accretors](IDataEdge<std::vector<Tscal>> &accretion_racc) {
1227 accretion_racc.data = std::get<1>(collect_accretors());
1228 }));
1229 shambase::get_check_ref(set_accretion_racc)
1230 .set_edges(solver_graph.get_edge_ptr<IDataEdge<std::vector<Tscal>>>(
1231 "ext_force_accretion_racc"));
1232
1233 auto set_has_accretion = solver_graph.register_node(
1234 "set_has_ext_force_accretion",
1235 NodeMapEdge<IDataEdge<std::vector<Tvec>>, IDataEdge<bool>>{
1236 [](const IDataEdge<std::vector<Tvec>> &accretion_pos,
1237 IDataEdge<bool> &has_ext_force_accretion) {
1238 has_ext_force_accretion.data = !accretion_pos.data.empty();
1239 }});
1240 shambase::get_check_ref(set_has_accretion)
1241 .set_edges(
1242 solver_graph.get_edge_ptr<IDataEdge<std::vector<Tvec>>>("ext_force_accretion_pos"),
1243 solver_graph.get_edge_ptr<IDataEdge<bool>>("has_ext_force_accretion"));
1244
1245 // reuse the sink accretion nodes, without the quantity accretion step since external
1246 // forces have no dynamical state to conserve onto
1247 auto flag_node = solver_graph.register_node(
1248 "ext_force_flag_accrete_hard", modules::SinkParticlesFlagAccreteHard<Tvec>{});
1249 shambase::get_check_ref(flag_node).set_edges(
1250 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
1251 solver_graph.get_edge_ptr<FieldRefs<Tvec>>("xyz"),
1252 solver_graph.get_edge_ptr<IDataEdge<std::vector<Tvec>>>("ext_force_accretion_pos"),
1253 solver_graph.get_edge_ptr<IDataEdge<std::vector<Tscal>>>("ext_force_accretion_racc"),
1254 solver_graph.get_edge_ptr<Field<u32>>("ext_force_accretion_table"));
1255
1256 auto evict_node = solver_graph.register_node(
1257 "ext_force_evict_accreted", modules::SinkParticlesEvictAccretedParticles<Tvec>{});
1258 shambase::get_check_ref(evict_node)
1259 .set_edges(
1260 solver_graph.get_edge_ptr<Indexes<u32>>("part_counts"),
1261 solver_graph.get_edge_ptr<Field<u32>>("ext_force_accretion_table"),
1262 solver_graph.get_edge_ptr<PatchDataLayerRefs>("scheduler_patchdata"));
1263
1264 auto accretion_body = solver_graph.register_node(
1265 "ext_force_accretion_body",
1267 "ext force accretion",
1268 {
1269 // the "time_step" sequence (attach fields to scheduler, ...) has not run
1270 // yet at this stage of the timestep
1271 solver_graph.get_node_ptr_base("set_scheduler_patchdata"),
1272 solver_graph.get_node_ptr_base("attach_part_counts"),
1273 solver_graph.get_node_ptr_base("attach_xyz"),
1274 // Actually perform the accretion
1275 flag_node,
1276 evict_node,
1277 // free the refs since the particle counts may have changed
1278 solver_graph.get_node_ptr_base("free_xyz_refs"),
1279 }));
1280
1281 auto if_has_accretion = solver_graph.register_node(
1282 "if_has_ext_force_accretion",
1283 OperationIf("if_has_ext_force_accretion", accretion_body));
1284 shambase::get_check_ref(if_has_accretion)
1285 .set_edges(solver_graph.get_edge_ptr<IDataEdge<bool>>("has_ext_force_accretion"));
1286
1287 // register the actual node that will be used
1288 solver_graph.register_node(
1289 "point mass accretion",
1291 "point mass accretion",
1292 {
1293 set_accretion_pos,
1294 set_accretion_racc,
1295 set_has_accretion,
1296 if_has_accretion,
1297 }));
1298 }
1299
1300 {
1301 std::vector<std::shared_ptr<shamrock::solvergraph::INode>> seq{};
1302
1303 seq.push_back(solver_graph.get_node_ptr_base("sink accretion"));
1304 seq.push_back(solver_graph.get_node_ptr_base("point mass accretion"));
1305 seq.push_back(solver_graph.get_node_ptr_base("sink predictor"));
1306 seq.push_back(solver_graph.get_node_ptr_base("dt_to_half_dt"));
1307 seq.push_back(solver_graph.get_node_ptr_base("set_gpart_mass"));
1308 seq.push_back(solver_graph.get_node_ptr_base("attach fields to scheduler"));
1309 seq.push_back(solver_graph.get_node_ptr_base("leapfrog predictor"));
1310 if (do_part_killing_step) {
1311 seq.push_back(solver_graph.get_node_ptr_base("part killing step"));
1312 }
1313 seq.push_back(solver_graph.get_node_ptr_base("sink ext force"));
1314
1315 storage.solver_sequence = solver_graph.register_node(
1316 "time_step", OperationSequence("time step", std::move(seq)));
1317 }
1318
1320 // self gravity sequence
1322 if (solver_config.self_grav_config.is_sg_on()) {
1323
1324 const u32 ixyz = pdl.get_field_idx<Tvec>("xyz");
1325
1326 auto constant_G = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("", "");
1327
1330 constant_G.data = solver_config.get_constant_G();
1331 });
1332
1333 set_constant_G.set_edges(constant_G);
1334
1335 auto field_xyz = shamrock::solvergraph::FieldRefs<Tvec>::make_shared("", "");
1336
1338 [&, ixyz](shamrock::solvergraph::FieldRefs<Tvec> &field_xyz_edge) {
1340 scheduler().for_each_patchdata_nonempty(
1342 auto &field = pdat.get_field<Tvec>(ixyz);
1343 field_xyz_refs.add_obj(p.id_patch, std::ref(field));
1344 });
1345 field_xyz_edge.set_refs(field_xyz_refs);
1346 });
1347 set_field_xyz.set_edges(field_xyz);
1348
1349 const u32 iaxyz_ext = pdl.get_field_idx<Tvec>("axyz_ext");
1350
1351 auto field_axyz_ext = shamrock::solvergraph::FieldRefs<Tvec>::make_shared("", "");
1352
1354 set_field_axyz_ext(
1355 [&, iaxyz_ext](shamrock::solvergraph::FieldRefs<Tvec> &field_axyz_ext_edge) {
1356 shamrock::solvergraph::DDPatchDataFieldRef<Tvec> field_axyz_ext_refs = {};
1357 scheduler().for_each_patchdata_nonempty(
1359 auto &field = pdat.get_field<Tvec>(iaxyz_ext);
1360 field_axyz_ext_refs.add_obj(p.id_patch, std::ref(field));
1361 });
1362 field_axyz_ext_edge.set_refs(field_axyz_ext_refs);
1363 });
1364 set_field_axyz_ext.set_edges(field_axyz_ext);
1365
1366 auto sizes = shamrock::solvergraph::Indexes<u32>::make_shared("", "");
1367
1370 sizes.indexes = {};
1371 scheduler().for_each_patchdata_nonempty(
1373 sizes.indexes.add_obj(p.id_patch, pdat.get_obj_cnt());
1374 });
1375 });
1376 set_sizes.set_edges(sizes);
1377
1378 auto gpart_mass = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("", "");
1379
1382 gpart_mass.data = solver_config.gpart_mass;
1383 });
1384
1385 set_gpart_mass.set_edges(gpart_mass);
1386
1387 std::shared_ptr<shamrock::solvergraph::INode> sg_inode = build_self_gravity_node<Tvec>(
1388 solver_config.self_grav_config,
1389 sizes,
1390 gpart_mass,
1391 constant_G,
1392 field_xyz,
1393 field_axyz_ext);
1394
1395 solver_graph.register_node(
1396 "self gravity sequence",
1398 "self gravity",
1399 {
1400 shambase::to_shared(std::move(set_gpart_mass)),
1401 shambase::to_shared(std::move(set_constant_G)),
1402 shambase::to_shared(std::move(set_field_xyz)),
1403 shambase::to_shared(std::move(set_field_axyz_ext)),
1404 shambase::to_shared(std::move(set_sizes)),
1405 sg_inode,
1406 }));
1407 }
1408}
1409
1410template<class Tvec, template<class> class Kern>
1412 std::string filename, bool add_patch_world_id) {
1413
1414 modules::VTKDump(context, solver_config).do_dump(filename, add_patch_world_id);
1415}
1416
1418// Debug interface dump
1420
1421namespace shammodels::sph {
1422
1423 template<class Tvec>
1425 using Tscal = shambase::VecComponent<Tvec>;
1426
1427 u64 nobj;
1428 f64 gpart_mass;
1429
1430 sycl::buffer<Tvec> &buf_xyz;
1431 sycl::buffer<Tscal> &buf_hpart;
1432 sycl::buffer<Tvec> &buf_vxyz;
1433 };
1434
1435 template<class Tvec>
1436 void fill_blocks(PhantomDumpBlock &block, Debug_ph_dump<Tvec> &info) {
1437
1438 using Tscal = shambase::VecComponent<Tvec>;
1439 std::vector<Tvec> xyz = shamalgs::memory::buf_to_vec(info.buf_xyz, info.nobj);
1440
1441 u64 xid = block.get_ref_fort_real("x");
1442 u64 yid = block.get_ref_fort_real("y");
1443 u64 zid = block.get_ref_fort_real("z");
1444
1445 for (auto vec : xyz) {
1446 block.blocks_fort_real[xid].vals.push_back(vec.x());
1447 block.blocks_fort_real[yid].vals.push_back(vec.y());
1448 block.blocks_fort_real[zid].vals.push_back(vec.z());
1449 }
1450
1451 std::vector<Tscal> h = shamalgs::memory::buf_to_vec(info.buf_hpart, info.nobj);
1452 u64 hid = block.get_ref_f32("h");
1453 for (auto h_ : h) {
1454 block.blocks_f32[hid].vals.push_back(h_);
1455 }
1456
1457 std::vector<Tvec> vxyz = shamalgs::memory::buf_to_vec(info.buf_vxyz, info.nobj);
1458
1459 u64 vxid = block.get_ref_fort_real("vx");
1460 u64 vyid = block.get_ref_fort_real("vy");
1461 u64 vzid = block.get_ref_fort_real("vz");
1462
1463 for (auto vec : vxyz) {
1464 block.blocks_fort_real[vxid].vals.push_back(vec.x());
1465 block.blocks_fort_real[vyid].vals.push_back(vec.y());
1466 block.blocks_fort_real[vzid].vals.push_back(vec.z());
1467 }
1468
1469 block.tot_count = block.blocks_fort_real[xid].vals.size();
1470 }
1471
1472 template<class Tvec>
1473 shammodels::sph::PhantomDump make_interface_debug_phantom_dump(Debug_ph_dump<Tvec> info) {
1474
1475 using Tscal = shambase::VecComponent<Tvec>;
1476 PhantomDump dump;
1477
1478 dump.override_magic_number();
1479 dump.iversion = 1;
1480 dump.fileid = sham::format("{:100s}", "FT:Phantom Shamrock writer");
1481
1482 u32 Ntot = info.nobj;
1483 dump.table_header_fort_int.add("nparttot", Ntot);
1484 dump.table_header_fort_int.add("ntypes", 8);
1485 dump.table_header_fort_int.add("npartoftype", Ntot);
1486 dump.table_header_fort_int.add("npartoftype", 0);
1487 dump.table_header_fort_int.add("npartoftype", 0);
1488 dump.table_header_fort_int.add("npartoftype", 0);
1489 dump.table_header_fort_int.add("npartoftype", 0);
1490 dump.table_header_fort_int.add("npartoftype", 0);
1491 dump.table_header_fort_int.add("npartoftype", 0);
1492 dump.table_header_fort_int.add("npartoftype", 0);
1493
1494 dump.table_header_i64.add("nparttot", Ntot);
1495 dump.table_header_i64.add("ntypes", 8);
1496 dump.table_header_i64.add("npartoftype", Ntot);
1497 dump.table_header_i64.add("npartoftype", 0);
1498 dump.table_header_i64.add("npartoftype", 0);
1499 dump.table_header_i64.add("npartoftype", 0);
1500 dump.table_header_i64.add("npartoftype", 0);
1501 dump.table_header_i64.add("npartoftype", 0);
1502 dump.table_header_i64.add("npartoftype", 0);
1503 dump.table_header_i64.add("npartoftype", 0);
1504
1505 dump.table_header_fort_int.add("nblocks", 1);
1506 dump.table_header_fort_int.add("nptmass", 0);
1507 dump.table_header_fort_int.add("ndustlarge", 0);
1508 dump.table_header_fort_int.add("ndustsmall", 0);
1509 dump.table_header_fort_int.add("idust", 7);
1510 dump.table_header_fort_int.add("idtmax_n", 1);
1511 dump.table_header_fort_int.add("idtmax_frac", 0);
1512 dump.table_header_fort_int.add("idumpfile", 0);
1513 dump.table_header_fort_int.add("majorv", 2023);
1514 dump.table_header_fort_int.add("minorv", 0);
1515 dump.table_header_fort_int.add("microv", 0);
1516 dump.table_header_fort_int.add("isink", 0);
1517
1518 dump.table_header_i32.add("iexternalforce", 0);
1519 dump.table_header_i32.add("ieos", 2);
1520 dump.table_header_fort_real.add("gamma", 1.66667);
1521 dump.table_header_fort_real.add("RK2", 0);
1522 dump.table_header_fort_real.add("polyk2", 0);
1523 dump.table_header_fort_real.add("qfacdisc", 0.75);
1524 dump.table_header_fort_real.add("qfacdisc2", 0.75);
1525
1526 dump.table_header_fort_real.add("time", 0);
1527 dump.table_header_fort_real.add("dtmax", 0.1);
1528
1529 dump.table_header_fort_real.add("rhozero", 0);
1530 dump.table_header_fort_real.add("hfact", 1.2);
1531 dump.table_header_fort_real.add("tolh", 0.0001);
1532 dump.table_header_fort_real.add("C_cour", 0);
1533 dump.table_header_fort_real.add("C_force", 0);
1534 dump.table_header_fort_real.add("alpha", 0);
1535 dump.table_header_fort_real.add("alphau", 1);
1536 dump.table_header_fort_real.add("alphaB", 1);
1537
1538 dump.table_header_fort_real.add("massoftype", info.gpart_mass);
1539 dump.table_header_fort_real.add("massoftype", 0);
1540 dump.table_header_fort_real.add("massoftype", 0);
1541 dump.table_header_fort_real.add("massoftype", 0);
1542 dump.table_header_fort_real.add("massoftype", 0);
1543 dump.table_header_fort_real.add("massoftype", 0);
1544 dump.table_header_fort_real.add("massoftype", 0);
1545 dump.table_header_fort_real.add("massoftype", 0);
1546
1547 dump.table_header_fort_real.add("Bextx", 0);
1548 dump.table_header_fort_real.add("Bexty", 0);
1549 dump.table_header_fort_real.add("Bextz", 0);
1550 dump.table_header_fort_real.add("dum", 0);
1551
1552 dump.table_header_fort_real.add("get_conserv", -1);
1553 dump.table_header_fort_real.add("etot_in", 0.59762);
1554 dump.table_header_fort_real.add("angtot_in", 0.0189694);
1555 dump.table_header_fort_real.add("totmom_in", 0.0306284);
1556
1557 dump.table_header_f64.add("udist", 1);
1558 dump.table_header_f64.add("umass", 1);
1559 dump.table_header_f64.add("utime", 1);
1560 dump.table_header_f64.add("umagfd", 3.54491);
1561
1562 PhantomDumpBlock block_part;
1563
1564 fill_blocks(block_part, info);
1565
1566 dump.blocks.push_back(std::move(block_part));
1567
1568 return dump;
1569 }
1570
1571} // namespace shammodels::sph
1572
1573template<class Tvec, template<class> class Kern>
1574void shammodels::sph::Solver<Tvec, Kern>::gen_serial_patch_tree() {
1575 StackEntry stack_loc{};
1576
1577 SerialPatchTree<Tvec> _sptree = SerialPatchTree<Tvec>::build(scheduler());
1578 _sptree.attach_buf();
1579 storage.serial_patch_tree.set(std::move(_sptree));
1580}
1581
1587template<class Tvec, template<class> class Kern>
1589
1590 StackEntry stack_loc{};
1591
1592 shamlog_debug_ln("SphSolver", "apply position boundary");
1593
1594 PatchScheduler &sched = scheduler();
1595
1596 shamrock::SchedulerUtility integrators(sched);
1597 shamrock::ReattributeDataUtility reatrib(sched);
1598
1599 auto &pdl = sched.pdl_old();
1600
1601 const u32 ixyz = pdl.get_field_idx<Tvec>("xyz");
1602 const u32 ivxyz = pdl.get_field_idx<Tvec>("vxyz");
1603 auto [bmin, bmax] = sched.get_box_volume<Tvec>();
1604
1605 using SolverConfigBC = typename Config::BCConfig;
1606 using SolverBCFree = typename SolverConfigBC::Free;
1607 using SolverBCPeriodic = typename SolverConfigBC::Periodic;
1608 using SolverBCShearingPeriodic = typename SolverConfigBC::ShearingPeriodic;
1609 if (SolverBCFree *c = std::get_if<SolverBCFree>(&solver_config.boundary_config.config)) {
1610 if (shamcomm::world_rank() == 0) {
1611 logger::info_ln("PositionUpdated", "free boundaries skipping geometry update");
1612 }
1613 } else if (
1614 SolverBCPeriodic *c
1615 = std::get_if<SolverBCPeriodic>(&solver_config.boundary_config.config)) {
1616 integrators.fields_apply_periodicity(ixyz, std::pair{bmin, bmax});
1617 } else if (
1618 SolverBCShearingPeriodic *c
1619 = std::get_if<SolverBCShearingPeriodic>(&solver_config.boundary_config.config)) {
1620 integrators.fields_apply_shearing_periodicity(
1621 ixyz,
1622 ivxyz,
1623 std::pair{bmin, bmax},
1624 c->shear_base,
1625 c->shear_dir,
1626 c->shear_speed * time_val,
1627 c->shear_speed);
1628 }
1629
1630 reatrib.reatribute_patch_objects(storage.serial_patch_tree.get(), "xyz");
1631}
1632
1633template<class Tvec, template<class> class Kern>
1635
1636 StackEntry stack_loc{};
1637
1638 using namespace shamrock::patch;
1639 using namespace shamrock::solvergraph;
1640
1641 using InterfaceBuildInfos = typename GhostHandle::InterfaceBuildInfos;
1642 using InterfaceIdTable = typename GhostHandle::InterfaceIdTable;
1643
1644 using BCConfig = typename Config::BCConfig;
1645 using BCFree = typename BCConfig::Free;
1646 using BCPeriodic = typename BCConfig::Periodic;
1647 using BCShearingPeriodic = typename BCConfig::ShearingPeriodic;
1648
1649 const auto &bc_config = solver_config.boundary_config.config;
1650
1651 // ----------------------------------------------------------------------------------------
1652 // Generate the ghost handler
1653 // ----------------------------------------------------------------------------------------
1654
1655 storage.ghost_handler.set(
1656 GhostHandle{scheduler(), storage.patch_rank_owner, storage.xyzh_ghost_layout});
1657
1658 // ----------------------------------------------------------------------------------------
1659 // Build the ghost cache
1660 // ----------------------------------------------------------------------------------------
1661
1662 PatchScheduler &sched = scheduler();
1663 SerialPatchTree<Tvec> &sptree = storage.serial_patch_tree.get();
1664 const Tscal h_evol_max = solver_config.htol_up_coarse_cycle;
1665
1666 const u32 ihpart = sched.pdl_old().template get_field_idx<Tscal>("hpart");
1667
1668 PatchField<Tscal> interactR_patch = sched.map_owned_to_patch_field_simple<Tscal>(
1669 [&](const Patch p, PatchDataLayer &pdat) -> Tscal {
1670 if (!pdat.is_empty()) {
1671 Tscal rghost = pdat.get_field<Tscal>(ihpart).compute_max() * h_evol_max * Rkern;
1672 shamlog_debug_ln("Ghost", "patch", p.id_patch, "Rghost =", rghost);
1673 return rghost;
1674 } else {
1675 return shambase::VectorProperties<Tscal>::get_min();
1676 }
1677 });
1678
1679 PatchtreeField<Tscal> interactR_mpi_tree = sptree.make_patch_tree_field(
1680 sched,
1682 interactR_patch,
1683 [](Tscal h0, Tscal h1, Tscal h2, Tscal h3, Tscal h4, Tscal h5, Tscal h6, Tscal h7) {
1684 return sham::max_8points(h0, h1, h2, h3, h4, h5, h6, h7);
1685 });
1686
1687 // ----------------------------------------------------------------------------------------
1688 // Edges
1689 // ----------------------------------------------------------------------------------------
1690
1691 auto sim_box_edge = std::make_shared<ScalarEdge<shammath::AABB<Tvec>>>("", "");
1692 auto patch_tree_edge = std::make_shared<SerialPatchTreeRefEdge<Tvec>>("", "");
1693 auto interact_radius_tree = std::make_shared<PatchtreeFieldEdge<Tscal>>("", "");
1694 auto interact_radius = std::make_shared<ScalarsEdge<Tscal>>("", "");
1695 auto local_patch_boxes = std::make_shared<ScalarsEdge<shammath::CoordRange<Tvec>>>("", "");
1696 auto shear_value = IDataEdge<Tscal>::make_shared("", "");
1697 auto positions = std::make_shared<FieldRefs<Tvec>>("", "");
1698 auto interface_infos = std::make_shared<DDSharedScalar<InterfaceBuildInfos>>("", "");
1699 auto interface_id_table = std::make_shared<DDSharedScalar<InterfaceIdTable>>("", "");
1700
1701 // ----------------------------------------------------------------------------------------
1702 // Nodes
1703 // ----------------------------------------------------------------------------------------
1704
1705 std::vector<std::shared_ptr<INode>> build_ghost_cache_sequence;
1706
1707 std::visit(
1709 [&](const BCFree &) {
1710 auto node = std::make_shared<modules::FindGhostInterfacesFree<Tvec>>();
1711 node->set_edges(
1712 sim_box_edge,
1713 patch_tree_edge,
1714 interact_radius_tree,
1715 interact_radius,
1716 local_patch_boxes,
1717 interface_infos);
1718 build_ghost_cache_sequence.push_back(node);
1719 },
1720 [&](const BCPeriodic &) {
1721 auto node = std::make_shared<modules::FindGhostInterfacesPeriodic<Tvec>>();
1722 node->set_edges(
1723 sim_box_edge,
1724 patch_tree_edge,
1725 interact_radius_tree,
1726 interact_radius,
1727 local_patch_boxes,
1728 interface_infos);
1729 build_ghost_cache_sequence.push_back(node);
1730 },
1731 [&](const BCShearingPeriodic &cfg) {
1732 auto node = std::make_shared<modules::FindGhostInterfacesShearingPeriodic<Tvec>>(
1733 cfg.shear_base, cfg.shear_dir, cfg.shear_speed);
1734 node->set_edges(
1735 sim_box_edge,
1736 patch_tree_edge,
1737 interact_radius_tree,
1738 interact_radius,
1739 local_patch_boxes,
1740 shear_value,
1741 interface_infos);
1742 build_ghost_cache_sequence.push_back(node);
1743 },
1744 },
1745 bc_config);
1746
1748
1749 if (solver_config.has_filter_empty_patch_gz()) {
1750 auto filter_empty_patch_gz
1751 = std::make_shared<NodeSetEdge<DDSharedScalar<InterfaceBuildInfos>>>(
1752 [&](DDSharedScalar<InterfaceBuildInfos> &interface_infos_edge) {
1753 GeneratorMap &interf_map = interface_infos_edge.values;
1754
1755 PatchField<u32> patchdata_obj_cnt = sched.map_owned_to_patch_field_simple<u32>(
1756 [&](const Patch p, PatchDataLayer &pdat) -> u32 {
1757 return pdat.get_obj_cnt();
1758 });
1759
1760 GeneratorMap filtered_interf_map = {};
1761
1762 interf_map.for_each([&](u64 sender, u64 receiver, InterfaceBuildInfos &build) {
1763 u32 sender_obj_cnt = patchdata_obj_cnt.get(sender);
1764 u32 receiver_obj_cnt = patchdata_obj_cnt.get(receiver);
1765
1766 if (sender_obj_cnt == 0 || receiver_obj_cnt == 0) {
1767 shamlog_debug_ln(
1768 "Ghost", "patch", sender, "->", receiver, "is empty, skipping");
1769 return;
1770 }
1771
1772 filtered_interf_map.add_obj(sender, receiver, InterfaceBuildInfos{build});
1773 });
1774 interf_map = std::move(filtered_interf_map);
1775 });
1776 filter_empty_patch_gz->set_edges(interface_infos);
1777 build_ghost_cache_sequence.push_back(filter_empty_patch_gz);
1778 }
1779
1780 auto build_id_table = std::make_shared<modules::BuildGhostInterfaceIdTable<Tvec>>();
1781 build_id_table->set_edges(positions, interface_infos, interface_id_table);
1782 build_ghost_cache_sequence.push_back(build_id_table);
1783
1784 OperationSequence build_ghost_cache_seq(
1785 "build ghost cache", std::move(build_ghost_cache_sequence));
1786
1787 // ----------------------------------------------------------------------------------------
1788 // Set edges
1789 // ----------------------------------------------------------------------------------------
1790
1791 SimulationBoxInfo &sim_box = sched.get_sim_box();
1792 PatchCoordTransform<Tvec> patch_coord_transf = sim_box.get_patch_transform<Tvec>();
1793 auto [bmin, bmax] = sim_box.get_bounding_box<Tvec>();
1794
1795 sim_box_edge->value = shammath::AABB<Tvec>(bmin, bmax);
1796
1797 patch_tree_edge->patch_tree = std::ref(sptree);
1798
1799 interact_radius_tree->patchtree_field = std::move(interactR_mpi_tree);
1800
1801 // interactR_patch is not used afterwards, so move it (O(1)) instead of copying over all
1802 // global patches
1803 interact_radius->values = std::move(interactR_patch.field_all);
1804
1805 sched.for_each_local_patch([&](const Patch &p) {
1806 local_patch_boxes->values.add_obj(p.id_patch, patch_coord_transf.to_obj_coord(p));
1807 });
1808
1809 if (const BCShearingPeriodic *cfg = std::get_if<BCShearingPeriodic>(&bc_config)) {
1810 shear_value->data = cfg->shear_speed * time_val;
1811 }
1812
1813 DDPatchDataFieldRef<Tvec> positions_refs = {};
1814 sched.for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1815 positions_refs.add_obj(p.id_patch, std::ref(pdat.get_field<Tvec>(0)));
1816 });
1817 positions->set_refs(positions_refs);
1818
1819 // ----------------------------------------------------------------------------------------
1820 // Evaluate
1821 // ----------------------------------------------------------------------------------------
1822
1823 build_ghost_cache_seq.evaluate();
1824
1825 // ----------------------------------------------------------------------------------------
1826
1827 u32 total = 0;
1828 u32 empty_receiver = 0;
1829 u32 empty_sender = 0;
1830 u32 empty_either = 0;
1831 interface_infos->values.for_each([&](u64 sender, u64 receiver, InterfaceBuildInfos &build) {
1832 bool receiver_empty = sched.get_patch(receiver).load_value == 0;
1833 bool sender_empty = sched.get_patch(sender).load_value == 0;
1834 total++;
1835 if (receiver_empty) {
1836 empty_receiver++;
1837 }
1838 if (sender_empty) {
1839 empty_sender++;
1840 }
1841 if (receiver_empty || sender_empty) {
1842 empty_either++;
1843 }
1844 });
1845 shamlog_debug_ln(
1846 "Ghost",
1847 "total:",
1848 total,
1849 "empty_receiver:",
1850 empty_receiver,
1851 "empty_sender:",
1852 empty_sender,
1853 "empty_either:",
1854 empty_either);
1855
1856 storage.ghost_patch_cache.set(std::move(interface_id_table->values));
1857
1858 // storage.ghost_handler.get().gen_debug_patch_ghost(storage.ghost_patch_cache.get());
1859}
1860
1861template<class Tvec, template<class> class Kern>
1863 StackEntry stack_loc{};
1864 storage.ghost_patch_cache.reset();
1865}
1866
1867template<class Tvec, template<class> class Kern>
1869
1870 StackEntry stack_loc{};
1871
1872 storage.merged_xyzh.set(storage.ghost_handler.get().build_comm_merge_positions(
1873 storage.ghost_patch_cache.get(),
1874 storage.exchange_gz_positions,
1875 solver_config.show_ghost_zone_graph));
1876
1877 { // set element counts
1878 shambase::get_check_ref(storage.part_counts).indexes
1879 = storage.merged_xyzh.get().template map<u32>(
1880 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
1881 return scheduler().patch_data.get_pdat(id).get_obj_cnt();
1882 });
1883 }
1884
1885 { // set element counts
1886 shambase::get_check_ref(storage.part_counts_with_ghost).indexes
1887 = storage.merged_xyzh.get().template map<u32>(
1888 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
1889 return mpdat.get_obj_cnt();
1890 });
1891 }
1892
1893 { // Attach spans to block coords
1894 shambase::get_check_ref(storage.positions_with_ghosts)
1895 .set_refs(storage.merged_xyzh.get()
1896 .template map<std::reference_wrapper<PatchDataField<Tvec>>>(
1897 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
1898 return std::ref(mpdat.get_field<Tvec>(0));
1899 }));
1900
1901 shambase::get_check_ref(storage.hpart_with_ghosts)
1902 .set_refs(storage.merged_xyzh.get()
1903 .template map<std::reference_wrapper<PatchDataField<Tscal>>>(
1904 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
1905 return std::ref(mpdat.get_field<Tscal>(1));
1906 }));
1907 }
1908}
1909
1910template<class Tvec, template<class> class Kern>
1914
1915template<class Tvec, template<class> class Kern>
1917 StackEntry stack_loc{};
1918 storage.merged_pos_trees.reset();
1919}
1920
1921template<class Tvec, template<class> class Kern>
1923 StackEntry stack_loc{};
1924
1925 using namespace shamrock;
1926 using namespace shamrock::patch;
1927
1929 shamrock::SchedulerUtility utility(scheduler());
1930
1931 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1932 const u32 ihpart = pdl.get_field_idx<Tscal>("hpart");
1933
1934 ComputeField<Tscal> _epsilon_h, _h_old;
1935
1936 auto should_set_omega_mask = std::make_shared<shamrock::solvergraph::Field<u32>>(
1937 1, "should_set_omega_mask", "should_set_omega_mask");
1938
1939 u32 hstep_cnt = 0;
1940 u32 hstep_max = solver_config.h_max_subcycles_count;
1941 for (; hstep_cnt < hstep_max; hstep_cnt++) {
1942
1943 build_ghost_cache(time_val + dt);
1948
1949 _epsilon_h = utility.make_compute_field<Tscal>("epsilon_h", 1, Tscal(100));
1950 _h_old = utility.save_field<Tscal>(ihpart, "h_old");
1951
1952 Tscal max_eps_h;
1953
1954 if (solver_config.gpart_mass == 0) {
1956 "invalid gpart_mass {}, this configuration can not converge.\n"
1957 "Please set it using either model.set_particle_mass(pmass) or "
1958 "cfg.set_particle_mass(pmass)",
1959 solver_config.gpart_mass));
1960 }
1961
1962 // sizes
1963 std::shared_ptr<shamrock::solvergraph::Indexes<u32>> sizes
1964 = std::make_shared<shamrock::solvergraph::Indexes<u32>>("", "");
1965 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1966 sizes->indexes.add_obj(p.id_patch, pdat.get_obj_cnt());
1967 });
1968
1969 // neigh cache
1970 auto &neigh_cache = storage.neigh_cache;
1971
1972 // positions
1973 auto &pos_merged = storage.positions_with_ghosts;
1974
1975 // old smoothing length field
1976 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hold
1977 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
1979 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1980 auto &field = _h_old.get_field(p.id_patch);
1981 hold_refs.add_obj(p.id_patch, std::ref(field));
1982 });
1983 hold->set_refs(hold_refs);
1984
1985 // new smoothing length field
1986 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hnew
1987 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
1989 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1990 auto &field = pdat.get_field<Tscal>(ihpart);
1991 hnew_refs.add_obj(p.id_patch, std::ref(field));
1992 });
1993 hnew->set_refs(hnew_refs);
1994
1995 // epsilon field
1996 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> eps_h
1997 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
1999 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2000 auto &field = _epsilon_h.get_field(p.id_patch);
2001 eps_h_refs.add_obj(p.id_patch, std::ref(field));
2002 });
2003 eps_h->set_refs(eps_h_refs);
2004
2005 std::shared_ptr<shamrock::solvergraph::INode> smth_h_iter_ptr;
2006
2007 using h_conf_density_based = typename SmoothingLengthConfig::DensityBased;
2008 using h_conf_neigh_lim = typename SmoothingLengthConfig::DensityBasedNeighLim;
2009 using h_conf_bisect_nr = typename SmoothingLengthConfig::DensityBasedBisectingNR;
2010
2011 if (h_conf_density_based *conf
2012 = std::get_if<h_conf_density_based>(&solver_config.smoothing_length_config.config)) {
2013 std::shared_ptr<shammodels::sph::modules::IterateSmoothingLengthDensity<Tvec, Kernel>>
2014 smth_h_iter = std::make_shared<
2016 solver_config.gpart_mass,
2017 solver_config.htol_up_coarse_cycle,
2018 solver_config.htol_up_fine_cycle,
2019 solver_config.epsilon_h);
2020 smth_h_iter->set_edges(sizes, neigh_cache, pos_merged, hold, hnew, eps_h);
2021 smth_h_iter_ptr = smth_h_iter;
2022 } else if (
2023 h_conf_neigh_lim *conf
2024 = std::get_if<h_conf_neigh_lim>(&solver_config.smoothing_length_config.config)) {
2025 std::shared_ptr<
2027 smth_h_iter_neigh_lim = std::make_shared<
2029 solver_config.gpart_mass,
2030 solver_config.htol_up_coarse_cycle,
2031 solver_config.htol_up_fine_cycle,
2032 conf->max_neigh_count,
2033 solver_config.epsilon_h);
2034 smth_h_iter_neigh_lim->set_edges(
2035 sizes, neigh_cache, pos_merged, hold, hnew, eps_h, should_set_omega_mask);
2036 smth_h_iter_ptr = smth_h_iter_neigh_lim;
2037 } else if (
2038 h_conf_bisect_nr *conf
2039 = std::get_if<h_conf_bisect_nr>(&solver_config.smoothing_length_config.config)) {
2040
2041 if (!storage.h_bisect_lo) {
2042 storage.h_bisect_lo = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
2043 1, "h_bisect_lo", "h_{\\rm lo}");
2044 }
2045 if (!storage.h_bisect_hi) {
2046 storage.h_bisect_hi = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
2047 1, "h_bisect_hi", "h_{\\rm hi}");
2048 }
2049 storage.h_bisect_lo->ensure_sizes(sizes->indexes);
2050 storage.h_bisect_hi->ensure_sizes(sizes->indexes);
2051
2052 if (hstep_cnt == 0) {
2053 // fresh bisection bracket for this call to sph_prestep, mirrors the Python
2054 // bisect_NR_iterate_new_h init (lo=0, hi=+inf); NOT reset on ghost-rebuild
2055 // retries of the hstep_cnt loop.
2056 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2057 storage.h_bisect_lo->get(p.id_patch).override(Tscal(0));
2058 storage.h_bisect_hi->get(p.id_patch).override(shambase::get_infty<Tscal>());
2059 });
2060 }
2061
2062 std::shared_ptr<
2064 smth_h_iter_bisect_nr
2065 = std::make_shared<shammodels::sph::modules::
2066 IterateSmoothingLengthDensityBisectingNR<Tvec, Kernel>>(
2067 solver_config.gpart_mass,
2068 solver_config.htol_up_coarse_cycle,
2069 solver_config.htol_up_fine_cycle,
2070 solver_config.epsilon_h);
2071 smth_h_iter_bisect_nr->set_edges(
2072 sizes,
2073 neigh_cache,
2074 pos_merged,
2075 hold,
2076 hnew,
2077 eps_h,
2078 storage.h_bisect_lo,
2079 storage.h_bisect_hi);
2080 smth_h_iter_ptr = smth_h_iter_bisect_nr;
2081 } else {
2082 shambase::throw_with_loc<std::runtime_error>("Invalid smoothing length configuration");
2083 }
2084 // iterate smoothing length
2085
2086 std::shared_ptr<shamrock::solvergraph::IDataEdge<bool>> is_converged
2087 = shamrock::solvergraph::IDataEdge<bool>::make_shared("", "");
2088
2090 smth_h_iter_ptr, solver_config.epsilon_h, solver_config.h_iter_per_subcycles, false);
2091 loop_smth_h_iter.set_edges(eps_h, is_converged);
2092
2093 loop_smth_h_iter.evaluate();
2094
2095 if (!is_converged->data) {
2096
2097 Tscal largest_h = 0;
2098
2099 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2100 largest_h = sham::max(largest_h, pdat.get_field<Tscal>(ihpart).compute_max());
2101 });
2102 Tscal global_largest_h = shamalgs::collective::allreduce_max(largest_h);
2103
2104 std::string add_info = "";
2105 u64 cnt_unconverged = 0;
2106 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2108 = _epsilon_h.get_field(p.id_patch).get_ids_where([](auto access, u32 id) {
2109 return access[id] == -1;
2110 });
2111
2112 if (hstep_cnt == hstep_max - 1) {
2113 if (idx_err.get_size() > 0) {
2114 add_info += "\n patch " + std::to_string(p.id_patch) + " ";
2115 add_info += "errored parts : \n";
2116
2117 sham::DeviceBuffer<Tvec> &xyz = pdat.get_field_buf_ref<Tvec>(0);
2118 sham::DeviceBuffer<Tscal> &hpart = pdat.get_field_buf_ref<Tscal>(ihpart);
2119
2120 auto pos = xyz.copy_to_stdvec();
2121 auto h = hpart.copy_to_stdvec();
2122
2123 for (u32 i : idx_err.copy_to_stdvec()) {
2124 add_info
2125 += sham::format("{} - pos : {}, hpart : {}\n", i, pos[i], h[i]);
2126 }
2127 }
2128 }
2129
2130 cnt_unconverged += idx_err.get_size();
2131 });
2132
2133 u64 global_cnt_unconverged = shamalgs::collective::allreduce_sum(cnt_unconverged);
2134
2135 if (shamcomm::world_rank() == 0) {
2137 "Smoothinglength",
2138 "smoothing length is not converged, rerunning the iterator ...\n largest h "
2139 "=",
2140 global_largest_h,
2141 "unconverged cnt =",
2142 global_cnt_unconverged,
2143 add_info);
2144 }
2145
2146 reset_ghost_handler();
2148
2149 shambase::get_check_ref(storage.part_counts).free_alloc();
2150 shambase::get_check_ref(storage.part_counts_with_ghost).free_alloc();
2151 shambase::get_check_ref(storage.positions_with_ghosts).free_alloc();
2152 shambase::get_check_ref(storage.hpart_with_ghosts).free_alloc();
2153
2154 storage.merged_xyzh.reset();
2155
2159
2160 // scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchData &pdat) {
2161 // pdat.synchronize_buf();
2162 // });
2163
2164 continue;
2165 }
2166
2167 // The hpart is not valid anymore in ghost zones since we iterated it's value
2168 shambase::get_check_ref(storage.hpart_with_ghosts).free_alloc();
2169
2170 _epsilon_h.reset();
2171 _h_old.reset();
2172 break;
2173 }
2174
2175 if (hstep_cnt == hstep_max) {
2176 logger::err_ln("SPH", "the h iterator is not converged after", hstep_cnt, "iterations");
2177 }
2178
2179 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hnew_edge
2180 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
2182 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2183 auto &field = pdat.get_field<Tscal>(ihpart);
2184 hnew_refs.add_obj(p.id_patch, std::ref(field));
2185 });
2186 hnew_edge->set_refs(hnew_refs);
2187
2188 modules::NodeComputeOmega<Tvec, Kern> compute_omega{solver_config.gpart_mass};
2189 compute_omega.set_edges(
2190 storage.part_counts,
2191 storage.neigh_cache,
2192 storage.positions_with_ghosts,
2193 hnew_edge,
2194 storage.omega);
2195 compute_omega.evaluate();
2196
2197 if (solver_config.smoothing_length_config.is_density_based_neigh_lim()) {
2198 // if the h limiter is triggered, omega does not hold it's sense of dh/dr anymore
2199 // so we set it to 1, this effectively is equivalent of disabling the energy correction
2200 // term corresponding to dh/dr
2201 modules::SetWhenMask<Tscal> set_omega_mask{1};
2202 set_omega_mask.set_edges(storage.part_counts, should_set_omega_mask, storage.omega);
2203 set_omega_mask.evaluate();
2204 }
2205}
2206
2207template<class Tvec, template<class> class Kern>
2209
2210 storage.ghost_layout = std::make_shared<shamrock::patch::PatchDataLayerLayout>();
2211
2213 = shambase::get_check_ref(storage.ghost_layout);
2214
2215 solver_config.set_ghost_layout(ghost_layout);
2216
2217 storage.xyzh_ghost_layout = std::make_shared<shamrock::patch::PatchDataLayerLayout>();
2218 storage.xyzh_ghost_layout->template add_field<Tvec>("xyz", 1);
2219 storage.xyzh_ghost_layout->template add_field<Tscal>("hpart", 1);
2220}
2221
2222template<class Tvec, template<class> class Kern>
2224
2225 StackEntry stack_loc{};
2226
2227 auto &xyzh_merged = storage.merged_xyzh.get();
2228 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
2229
2230 storage.rtree_rint_field.set(
2231 storage.merged_pos_trees.get().template map<shamtree::KarrasRadixTreeField<Tscal>>(
2232 [&](u64 id, RTree &rtree) -> shamtree::KarrasRadixTreeField<Tscal> {
2233 shamrock::patch::PatchDataLayer &tmp = xyzh_merged.get(id);
2234 auto &buf = tmp.get_field_buf_ref<Tscal>(1);
2235 auto buf_int = shamtree::new_empty_karras_radix_tree_field<Tscal>();
2236
2237 auto ret = shamtree::compute_tree_field_max_field<Tscal>(
2238 rtree.structure,
2239 rtree.reduced_morton_set.get_leaf_cell_iterator(),
2240 std::move(buf_int),
2241 buf);
2242
2243 // the old tree used to increase the size of the hmax of the tree nodes by the
2244 // tolerance so we do it also with the new tree, maybe we should move that somewhere
2245 // else.
2246 sham::kernel_call(
2247 dev_sched->get_queue(),
2248 sham::MultiRef{},
2249 sham::MultiRef{ret.buf_field},
2250 ret.buf_field.get_size(),
2251 [htol = solver_config.htol_up_coarse_cycle](u32 i, Tscal *h_tree) {
2252 h_tree[i] *= htol;
2253 });
2254
2255 return std::move(ret);
2256 }));
2257}
2258
2259template<class Tvec, template<class> class Kern>
2261 storage.rtree_rint_field.reset();
2262}
2263
2264template<class Tvec, template<class> class Kern>
2267 context, solver_config, storage);
2268
2269 switch (solver_config.neigh_cache_strategy) {
2270 case NeighCacheStrategy::SingleStage: neigh_cache_builder.start_neighbors_cache(); break;
2271 case NeighCacheStrategy::TwoStage: neigh_cache_builder.start_neighbors_cache_2stages(); break;
2273 neigh_cache_builder.start_neighbors_cache_shared_offload();
2274 break;
2276 neigh_cache_builder.start_neighbors_cache_2stages_shared_offload();
2277 break;
2278 default: shambase::throw_unimplemented("unknown neighbours cache strategy");
2279 }
2280
2281 if (solver_config.show_neigh_stats) {
2282 auto &pos_merged = storage.positions_with_ghosts;
2283 auto &neigh_cache = storage.neigh_cache;
2284 auto &hpart_with_ghosts = storage.hpart_with_ghosts;
2285 auto &part_counts = storage.part_counts;
2286
2287 modules::ComputeNeighStats<Tvec> compute_neigh_stats(Kernel::Rkern);
2288
2289 compute_neigh_stats.set_edges(part_counts, neigh_cache, pos_merged, hpart_with_ghosts);
2290 compute_neigh_stats.evaluate();
2291 }
2292}
2293
2294template<class Tvec, template<class> class Kern>
2296 // storage.neighbors_cache.reset();
2297}
2298
2299template<class Tvec, template<class> class Kern>
2301
2302 StackEntry stack_loc{};
2303
2304 shambase::Timer timer_interf;
2305 timer_interf.start();
2306
2307 using namespace shamrock;
2308 using namespace shamrock::patch;
2309
2310 bool has_alphaAV_field = solver_config.has_field_alphaAV();
2311 bool has_soundspeed_field = solver_config.ghost_has_soundspeed();
2312
2313 bool has_b_field = solver_config.has_field_b_on_rho();
2314 bool has_psi_field = solver_config.has_field_psi_on_ch();
2315 bool has_curl_b_field = solver_config.has_field_curl_b();
2316 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
2317 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
2318 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
2319
2320 PatchDataLayerLayout &pdl = scheduler().pdl_old();
2321 const u32 ixyz = pdl.get_field_idx<Tvec>("xyz");
2322 const u32 ivxyz = pdl.get_field_idx<Tvec>("vxyz");
2323 const u32 iaxyz = pdl.get_field_idx<Tvec>("axyz");
2324 const u32 iuint = pdl.get_field_idx<Tscal>("uint");
2325 const u32 iduint = pdl.get_field_idx<Tscal>("duint");
2326 const u32 ihpart = pdl.get_field_idx<Tscal>("hpart");
2327
2328 const u32 ialpha_AV = (has_alphaAV_field) ? pdl.get_field_idx<Tscal>("alpha_AV") : 0;
2329 const u32 isoundspeed = (has_soundspeed_field) ? pdl.get_field_idx<Tscal>("soundspeed") : 0;
2330
2331 const u32 iB_on_rho = (has_b_field) ? pdl.get_field_idx<Tvec>("B/rho") : 0;
2332 const u32 idB_on_rho = (has_b_field) ? pdl.get_field_idx<Tvec>("dB/rho") : 0;
2333 const u32 ipsi_on_ch = (has_psi_field) ? pdl.get_field_idx<Tscal>("psi/ch") : 0;
2334 const u32 idpsi_on_ch = (has_psi_field) ? pdl.get_field_idx<Tscal>("dpsi/ch") : 0;
2335 const u32 icurlB = (has_curl_b_field) ? pdl.get_field_idx<Tvec>("curlB") : 0;
2336
2337 bool do_MHD_debug = solver_config.do_MHD_debug();
2338 const u32 imag_pressure = (do_MHD_debug) ? pdl.get_field_idx<Tvec>("mag_pressure") : -1;
2339 const u32 imag_tension = (do_MHD_debug) ? pdl.get_field_idx<Tvec>("mag_tension") : -1;
2340 const u32 igas_pressure = (do_MHD_debug) ? pdl.get_field_idx<Tvec>("gas_pressure") : -1;
2341 const u32 itensile_corr = (do_MHD_debug) ? pdl.get_field_idx<Tvec>("tensile_corr") : -1;
2342 const u32 ipsi_propag = (do_MHD_debug) ? pdl.get_field_idx<Tscal>("psi_propag") : -1;
2343 const u32 ipsi_diff = (do_MHD_debug) ? pdl.get_field_idx<Tscal>("psi_diff") : -1;
2344 const u32 ipsi_cons = (do_MHD_debug) ? pdl.get_field_idx<Tscal>("psi_cons") : -1;
2345 const u32 iu_mhd = (do_MHD_debug) ? pdl.get_field_idx<Tscal>("u_mhd") : -1;
2346
2347 const u32 iepsilon = (has_epsilon_field) ? pdl.get_field_idx<Tscal>("epsilon") : 0;
2348 const u32 ideltav = (has_deltav_field) ? pdl.get_field_idx<Tvec>("deltav") : 0;
2349 const u32 is_j = (has_s_j_field) ? pdl.get_field_idx<Tscal>("s_j") : 0;
2350
2351 auto &ghost_layout_ptr = storage.ghost_layout;
2352 shamrock::patch::PatchDataLayerLayout &ghost_layout = shambase::get_check_ref(ghost_layout_ptr);
2353 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>("hpart");
2354 u32 iuint_interf = ghost_layout.get_field_idx<Tscal>("uint");
2355 u32 ivxyz_interf = ghost_layout.get_field_idx<Tvec>("vxyz");
2356 u32 iomega_interf = ghost_layout.get_field_idx<Tscal>("omega");
2357
2358 const u32 iaxyz_interf
2359 = (solver_config.has_axyz_in_ghost()) ? ghost_layout.get_field_idx<Tvec>("axyz") : 0;
2360
2361 const u32 isoundspeed_interf
2362 = (has_soundspeed_field) ? ghost_layout.get_field_idx<Tscal>("soundspeed") : 0;
2363
2364 const u32 iB_interf = (has_b_field) ? ghost_layout.get_field_idx<Tvec>("B/rho") : 0;
2365 const u32 ipsi_interf = (has_psi_field) ? ghost_layout.get_field_idx<Tscal>("psi/ch") : 0;
2366 const u32 icurlB_interf = (has_curl_b_field) ? ghost_layout.get_field_idx<Tvec>("curlB") : 0;
2367
2368 const u32 iepsilon_interf
2369 = (has_epsilon_field) ? ghost_layout.get_field_idx<Tscal>("epsilon") : 0;
2370 const u32 ideltav_interf = (has_deltav_field) ? ghost_layout.get_field_idx<Tvec>("deltav") : 0;
2371 const u32 is_j_interf = (has_s_j_field) ? ghost_layout.get_field_idx<Tscal>("s_j") : 0;
2372
2373 using InterfaceBuildInfos = typename sph::BasicSPHGhostHandler<Tvec>::InterfaceBuildInfos;
2374
2375 sph::BasicSPHGhostHandler<Tvec> &ghost_handle = storage.ghost_handler.get();
2377
2378 auto pdat_interf = ghost_handle.template build_interface_native<PatchDataLayer>(
2379 storage.ghost_patch_cache.get(),
2380 [&](u64 sender, u64, InterfaceBuildInfos binfo, sham::DeviceBuffer<u32> &buf_idx, u32 cnt) {
2381 PatchDataLayer pdat(ghost_layout_ptr);
2382
2383 pdat.reserve(cnt);
2384
2385 return pdat;
2386 });
2387
2388 ghost_handle.template modify_interface_native<PatchDataLayer>(
2389 storage.ghost_patch_cache.get(),
2390 pdat_interf,
2391 [&](u64 sender,
2392 u64,
2393 InterfaceBuildInfos binfo,
2394 sham::DeviceBuffer<u32> &buf_idx,
2395 u32 cnt,
2396 PatchDataLayer &pdat) {
2397 PatchDataLayer &sender_patch = scheduler().patch_data.get_pdat(sender);
2398 PatchDataField<Tscal> &sender_omega = omega.get(sender);
2399
2400 sender_patch.get_field<Tscal>(ihpart).append_subset_to(
2401 buf_idx, cnt, pdat.get_field<Tscal>(ihpart_interf));
2402 sender_patch.get_field<Tscal>(iuint).append_subset_to(
2403 buf_idx, cnt, pdat.get_field<Tscal>(iuint_interf));
2404
2405 if (solver_config.has_axyz_in_ghost()) {
2406 sender_patch.get_field<Tvec>(iaxyz).append_subset_to(
2407 buf_idx, cnt, pdat.get_field<Tvec>(iaxyz_interf));
2408 }
2409
2410 sender_patch.get_field<Tvec>(ivxyz).append_subset_to(
2411 buf_idx, cnt, pdat.get_field<Tvec>(ivxyz_interf));
2412
2413 sender_omega.append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(iomega_interf));
2414
2415 if (has_soundspeed_field) {
2416 sender_patch.get_field<Tscal>(isoundspeed)
2417 .append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(isoundspeed_interf));
2418 }
2419
2420 if (has_b_field) {
2421 sender_patch.get_field<Tvec>(iB_on_rho).append_subset_to(
2422 buf_idx, cnt, pdat.get_field<Tvec>(iB_interf));
2423 }
2424
2425 if (has_psi_field) {
2426 sender_patch.get_field<Tscal>(ipsi_on_ch)
2427 .append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(ipsi_interf));
2428 }
2429
2430 if (has_curl_b_field) {
2431 sender_patch.get_field<Tvec>(icurlB).append_subset_to(
2432 buf_idx, cnt, pdat.get_field<Tvec>(icurlB_interf));
2433 }
2434
2435 if (has_epsilon_field) {
2436 sender_patch.get_field<Tscal>(iepsilon).append_subset_to(
2437 buf_idx, cnt, pdat.get_field<Tscal>(iepsilon_interf));
2438 }
2439
2440 if (has_deltav_field) {
2441 sender_patch.get_field<Tvec>(ideltav).append_subset_to(
2442 buf_idx, cnt, pdat.get_field<Tvec>(ideltav_interf));
2443 }
2444
2445 if (has_s_j_field) {
2446 sender_patch.get_field<Tscal>(is_j).append_subset_to(
2447 buf_idx, cnt, pdat.get_field<Tscal>(is_j_interf));
2448 }
2449 });
2450
2451 ghost_handle.template modify_interface_native<PatchDataLayer>(
2452 storage.ghost_patch_cache.get(),
2453 pdat_interf,
2454 [&](u64 sender,
2455 u64,
2456 InterfaceBuildInfos binfo,
2457 sham::DeviceBuffer<u32> &buf_idx,
2458 u32 cnt,
2459 PatchDataLayer &pdat) {
2460 if (sycl::length(binfo.offset_speed) > 0) {
2461 pdat.get_field<Tvec>(ivxyz_interf).apply_offset(binfo.offset_speed);
2462 }
2463 });
2464
2465 shambase::DistributedDataShared<PatchDataLayer> interf_pdat = ghost_handle.communicate_pdat(
2466 ghost_layout_ptr,
2467 std::move(pdat_interf),
2468 storage.exchange_gz_node,
2469 solver_config.show_ghost_zone_graph);
2470
2471 std::map<u64, u64> sz_interf_map;
2472 interf_pdat.for_each([&](u64 s, u64 r, PatchDataLayer &pdat_interf) {
2473 sz_interf_map[r] += pdat_interf.get_obj_cnt();
2474 });
2475
2476 storage.merged_patchdata_ghost.set(
2477 ghost_handle.template merge_native<PatchDataLayer, PatchDataLayer>(
2478 std::move(interf_pdat),
2480 PatchDataLayer pdat_new(ghost_layout_ptr);
2481
2482 u32 or_elem = pdat.get_obj_cnt();
2483 pdat_new.reserve(or_elem + sz_interf_map[p.id_patch]);
2484 u32 total_elements = or_elem;
2485
2486 PatchDataField<Tscal> &cur_omega = omega.get(p.id_patch);
2487
2488 pdat_new.get_field<Tscal>(ihpart_interf).insert(pdat.get_field<Tscal>(ihpart));
2489 pdat_new.get_field<Tscal>(iuint_interf).insert(pdat.get_field<Tscal>(iuint));
2490 pdat_new.get_field<Tvec>(ivxyz_interf).insert(pdat.get_field<Tvec>(ivxyz));
2491
2492 if (solver_config.has_axyz_in_ghost()) {
2493 pdat_new.get_field<Tvec>(iaxyz_interf).insert(pdat.get_field<Tvec>(iaxyz));
2494 }
2495
2496 pdat_new.get_field<Tscal>(iomega_interf).insert(cur_omega);
2497
2498 if (has_soundspeed_field) {
2499 pdat_new.get_field<Tscal>(isoundspeed_interf)
2500 .insert(pdat.get_field<Tscal>(isoundspeed));
2501 }
2502
2503 if (has_b_field) {
2504 pdat_new.get_field<Tvec>(iB_interf).insert(pdat.get_field<Tvec>(iB_on_rho));
2505 }
2506
2507 if (has_psi_field) {
2508 pdat_new.get_field<Tscal>(ipsi_interf)
2509 .insert(pdat.get_field<Tscal>(ipsi_on_ch));
2510 }
2511
2512 if (has_curl_b_field) {
2513 pdat_new.get_field<Tvec>(icurlB_interf).insert(pdat.get_field<Tvec>(icurlB));
2514 }
2515
2516 if (has_epsilon_field) {
2517 pdat_new.get_field<Tscal>(iepsilon_interf)
2518 .insert(pdat.get_field<Tscal>(iepsilon));
2519 }
2520
2521 if (has_deltav_field) {
2522 pdat_new.get_field<Tvec>(ideltav_interf).insert(pdat.get_field<Tvec>(ideltav));
2523 }
2524
2525 if (has_s_j_field) {
2526 pdat_new.get_field<Tscal>(is_j_interf).insert(pdat.get_field<Tscal>(is_j));
2527 }
2528
2529 pdat_new.check_field_obj_cnt_match();
2530
2531 return pdat_new;
2532 },
2533 [](PatchDataLayer &pdat, PatchDataLayer &pdat_interf) {
2534 pdat.insert_elements(pdat_interf);
2535 }));
2536
2537 timer_interf.stop();
2538 storage.timings_details.interface += timer_interf.elapsed_sec();
2539}
2540
2541template<class Tvec, template<class> class Kern>
2543 storage.merged_patchdata_ghost.reset();
2544}
2545
2547// start artificial viscosity section //////////////////////////////////////////////////////////////
2549
2550template<class Tvec, template<class> class Kern>
2552
2553 sph::modules::UpdateViscosity<Tvec, Kern>(context, solver_config, storage)
2554 .update_artificial_viscosity(dt);
2555}
2556
2557template<class Tvec, template<class> class Kern>
2559
2560 using namespace shamrock::patch;
2561 PatchDataLayerLayout &pdl = scheduler().pdl_old();
2562
2563 const u32 iB_on_rho = pdl.get_field_idx<Tvec>("B/rho");
2564 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> B_on_rho_edge
2565 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>("", "");
2566
2568 = shambase::get_check_ref(storage.ghost_layout.get());
2569 u32 iB_on_rho_interf = ghost_layout.get_field_idx<Tvec>("B/rho");
2570 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>("hpart");
2571
2573 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2574 auto &field = storage.merged_patchdata_ghost.get()
2575 .get(p.id_patch)
2576 .template get_field<Tvec>(iB_on_rho_interf);
2577 B_on_rho_refs.add_obj(p.id_patch, std::ref(field));
2578 });
2579
2580 B_on_rho_edge->set_refs(B_on_rho_refs);
2581
2582 // Use the "hpart" field of merged_patchdata_ghost (refreshed every corrector iteration by
2583 // communicate_merge_ghosts_fields(), same as update_derivs)
2584 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hpart_edge
2585 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
2586
2588 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2589 auto &field = storage.merged_patchdata_ghost.get()
2590 .get(p.id_patch)
2591 .template get_field<Tscal>(ihpart_interf);
2592 hpart_refs.add_obj(p.id_patch, std::ref(field));
2593 });
2594
2595 hpart_edge->set_refs(hpart_refs);
2596
2597 Tscal const mu_0 = solver_config.get_constant_mu_0();
2598
2599 shambase::get_check_ref(storage.MagCurrentJ);
2600 // use MagCurrenJ: on active particles (no gz)
2601 modules::NodeComputeJ<Tvec, Kern> computeJ{solver_config.gpart_mass, mu_0};
2602 computeJ.set_edges(
2603 storage.part_counts,
2604 storage.neigh_cache,
2605 storage.positions_with_ghosts,
2606 hpart_edge,
2607 storage.omega,
2608 B_on_rho_edge,
2609 storage.MagCurrentJ);
2610
2611 computeJ.evaluate();
2612}
2613
2615// end artificial viscosity section ////////////////////////////////////////////////////////////////
2617
2618template<class Tvec, template<class> class Kern>
2623
2624template<class Tvec, template<class> class Kern>
2626 shambase::get_check_ref(storage.pressure).free_alloc();
2627 shambase::get_check_ref(storage.soundspeed).free_alloc();
2628}
2629
2630template<class Tvec, template<class> class Kern>
2632
2633 StackEntry stack_loc{};
2634
2635 using namespace shamrock;
2636 using namespace shamrock::patch;
2637 shamrock::SchedulerUtility utility(scheduler());
2638 PatchDataLayerLayout &pdl = scheduler().pdl_old();
2639
2640 bool has_b_field = solver_config.has_field_b_on_rho();
2641 bool has_psi_field = solver_config.has_field_psi_on_ch();
2642 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
2643 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
2644 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
2645
2646 const u32 iduint = pdl.get_field_idx<Tscal>("duint");
2647 const u32 iaxyz = pdl.get_field_idx<Tvec>("axyz");
2648 const u32 idB_on_rho = (has_b_field) ? pdl.get_field_idx<Tvec>("dB/rho") : 0;
2649 const u32 idpsi_on_ch = (has_psi_field) ? pdl.get_field_idx<Tscal>("dpsi/ch") : 0;
2650
2651 shamlog_debug_ln("sph::BasicGas", "save old fields");
2652 storage.old_axyz.set(utility.save_field<Tvec>(iaxyz, "axyz_old"));
2653 storage.old_duint.set(utility.save_field<Tscal>(iduint, "duint_old"));
2654
2655 if (has_b_field) {
2656 storage.old_dB_on_rho.set(utility.save_field<Tvec>(idB_on_rho, "dB/rho_old"));
2657 }
2658 if (has_psi_field) {
2659 storage.old_dpsi_on_ch.set(utility.save_field<Tscal>(idpsi_on_ch, "dpsi/ch_old"));
2660 }
2661 if (has_epsilon_field) {
2662 storage.old_dtepsilon.set(
2663 utility.save_field<Tscal>(pdl.get_field_idx<Tscal>("dtepsilon"), "dtepsilon_old"));
2664 }
2665 if (has_deltav_field) {
2666 storage.old_dtdeltav.set(
2667 utility.save_field<Tvec>(pdl.get_field_idx<Tvec>("dtdeltav"), "dtdeltav_old"));
2668 }
2669 if (has_s_j_field) {
2670 storage.old_ds_j_dt.set(
2671 utility.save_field<Tscal>(pdl.get_field_idx<Tscal>("ds_j_dt"), "ds_j_dt_old"));
2672 }
2673}
2674
2675template<class T>
2676void map_field_refs(
2677 PatchScheduler &sched, u32 field_idx, shamrock::solvergraph::FieldRefs<T> &refs) {
2678
2679 using namespace shamrock::solvergraph;
2680 using namespace shamrock::patch;
2681
2683 sched.for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2684 auto &field = pdat.get_field<T>(field_idx);
2685 field_refs.add_obj(p.id_patch, std::ref(field));
2686 });
2687 refs.set_refs(field_refs);
2688}
2689
2690template<class T>
2691void map_field_refs_ext(
2692 PatchScheduler &sched,
2694 u32 field_idx,
2696
2697 using namespace shamrock::solvergraph;
2698 using namespace shamrock::patch;
2699
2701 sched.for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2702 PatchDataLayer &mpdat = mpdats.get(p.id_patch);
2703 auto &field = mpdat.get_field<T>(field_idx);
2704 field_refs.add_obj(p.id_patch, std::ref(field));
2705 });
2706 refs.set_refs(field_refs);
2707}
2708
2709template<class T>
2710void map_field_refs_ext(
2711 PatchScheduler &sched,
2712 shamrock::ComputeField<T> &field_data,
2714
2715 using namespace shamrock::solvergraph;
2716 using namespace shamrock::patch;
2717
2719 sched.for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
2720 auto &field = field_data.get_field(p.id_patch);
2721 field_refs.add_obj(p.id_patch, std::ref(field));
2722 });
2723 refs.set_refs(field_refs);
2724}
2725
2726template<class Tvec, template<class> class Kern>
2728
2729 // if one fluid is enabled time to compute the stopping times
2730 if (solver_config.dust_config.has_s_j_field()) {
2731
2732 auto &cfg = solver_config.dust_config;
2733 u32 ndust = cfg.get_dust_nvar();
2734
2735 using DustConfig = typename Config::DustConfig;
2736
2737 using None = typename DustConfig::None;
2738 using ConstantStoppingTimes = typename DustConfig::ConstantStoppingTimes;
2739 using EpsteinDrag = typename DustConfig::EpsteinDrag;
2740
2742 = shambase::get_check_ref(storage.ghost_layout.get());
2743 shamrock::patch::PatchDataLayerLayout &pdl = scheduler().pdl_old();
2744
2745 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>("hpart");
2746
2747 auto &part_counts_with_ghost = storage.part_counts_with_ghost;
2748 auto &part_counts = storage.part_counts;
2749
2751 = storage.merged_patchdata_ghost.get();
2752
2753 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hpart_refs
2754 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("hpart", "h");
2755 { // if was just reset before this call
2756 shambase::get_check_ref(hpart_refs)
2757 .set_refs(mpdats.map<std::reference_wrapper<PatchDataField<Tscal>>>(
2758 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
2759 return std::ref(mpdat.get_field<Tscal>(ihpart_interf));
2760 }));
2761 }
2762
2763 auto gpart_mass
2764 = storage.solver_graph.template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
2765 "gpart_mass");
2766 auto t_j_field
2767 = storage.solver_graph.template get_edge_ptr<shamrock::solvergraph::Field<Tscal>>(
2768 "Ts_j");
2769
2770 if (std::holds_alternative<None>(cfg.dust_drag_mode)) {
2771
2772 throw "bro WTF";
2773
2774 } else if (
2775 ConstantStoppingTimes *cfg_drag
2776 = std::get_if<ConstantStoppingTimes>(&cfg.dust_drag_mode)) {
2777
2778 std::shared_ptr<shamrock::solvergraph::IDataEdge<std::vector<Tscal>>> input_t_j
2780 input_t_j->data = cfg_drag->stopping_times;
2781
2782 std::shared_ptr<modules::SetDustStoppingTimeConstant<Tvec>> node_set_tj
2783 = std::make_shared<modules::SetDustStoppingTimeConstant<Tvec>>(ndust);
2784 {
2785 node_set_tj->set_edges(input_t_j, part_counts_with_ghost, t_j_field);
2786 }
2787 node_set_tj->evaluate();
2788
2789 } else if (EpsteinDrag *cfg_drag = std::get_if<EpsteinDrag>(&cfg.dust_drag_mode)) {
2790
2791 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> input_gamma
2792 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("", "");
2793 input_gamma->data = cfg_drag->gamma;
2794
2795 std::shared_ptr<shamrock::solvergraph::IDataEdge<std::vector<Tscal>>> input_sgrain_j
2797 input_sgrain_j->data = cfg_drag->grains_sizes;
2798
2799 std::shared_ptr<shamrock::solvergraph::IDataEdge<std::vector<Tscal>>> input_rho_grain_j
2801 input_rho_grain_j->data = cfg_drag->grains_densities;
2802
2803 std::shared_ptr<modules::SetDustStoppingTimeEpstein<Tvec, Kern>> node_set_tj
2804 = std::make_shared<modules::SetDustStoppingTimeEpstein<Tvec, Kern>>(ndust);
2805 {
2806 node_set_tj->set_edges(
2807 gpart_mass,
2808 input_gamma,
2809 input_sgrain_j,
2810 input_rho_grain_j,
2811 part_counts_with_ghost,
2812 hpart_refs,
2813 storage.soundspeed,
2814 t_j_field);
2815 }
2816 node_set_tj->evaluate();
2817 }
2818
2819 if (cfg.ballabio_ts_limiter) {
2820 std::shared_ptr<modules::BallabioTsLimiter<Tvec>> node_ballabio_ts_limiter
2821 = std::make_shared<modules::BallabioTsLimiter<Tvec>>(ndust);
2822 {
2823 node_ballabio_ts_limiter->set_edges(
2824 part_counts_with_ghost, hpart_refs, storage.soundspeed, t_j_field);
2825 }
2826 node_ballabio_ts_limiter->evaluate();
2827 }
2828
2829 // delta v computation (for CFL or other uses e.g. COALA)
2830 auto &pressure_field = storage.pressure;
2831 auto &xyz_refs = storage.positions_with_ghosts;
2832
2833 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> omega_refs
2834 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("omega", "omega");
2835 {
2836 u32 iomega_interf = ghost_layout.get_field_idx<Tscal>("omega");
2837 shambase::get_check_ref(omega_refs)
2838 .set_refs(mpdats.map<std::reference_wrapper<PatchDataField<Tscal>>>(
2839 [iomega_interf](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
2840 return std::ref(mpdat.get_field<Tscal>(iomega_interf));
2841 }));
2842 }
2843
2844 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> s_j_refs
2845 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("s_j", "s_j");
2846 {
2847 u32 is_j_interf = ghost_layout.get_field_idx<Tscal>("s_j");
2848 shambase::get_check_ref(s_j_refs).set_refs(
2849 mpdats.map<std::reference_wrapper<PatchDataField<Tscal>>>(
2850 [is_j_interf](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
2851 return std::ref(mpdat.get_field<Tscal>(is_j_interf));
2852 }));
2853 }
2854
2855 std::shared_ptr<shamrock::solvergraph::Field<Tvec>> grad_P_on_rho
2856 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad P/rho", "grad P/rho");
2857
2858 u32 idelta_v = pdl.get_field_idx<Tvec>("delta_v");
2859
2860 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> delta_v
2861 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>("Delta v", "Delta v");
2862 map_field_refs(scheduler(), idelta_v, *delta_v);
2863
2864 auto press_grad_node = std::make_shared<modules::NodeComputePressureGrad<Tvec, Kern>>();
2865 auto delta_v_node = std::make_shared<modules::MonoFluidTVADeltav<Tvec, Kern>>(ndust);
2866
2867 press_grad_node->set_edges(
2868 gpart_mass,
2869 part_counts,
2870 part_counts_with_ghost,
2871 xyz_refs,
2872 hpart_refs,
2873 omega_refs,
2874 pressure_field,
2875 storage.neigh_cache,
2876 grad_P_on_rho);
2877
2878 delta_v_node->set_edges(
2879 gpart_mass, part_counts, hpart_refs, grad_P_on_rho, s_j_refs, t_j_field, delta_v);
2880
2881 press_grad_node->evaluate();
2882 delta_v_node->evaluate();
2883 }
2884
2885 modules::UpdateDerivs<Tvec, Kern> derivs(context, solver_config, storage);
2886 derivs.update_derivs(dt_hydro);
2887
2888 modules::ExternalForces<Tvec, Kern> ext_forces(context, solver_config, storage);
2889 ext_forces.add_ext_forces();
2890}
2891
2892template<class Tvec, template<class> class Kern>
2894 return false;
2895}
2896
2897template<class Tvec, template<class> class Kern>
2899 modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
2900 .update_load_balancing();
2901 scheduler().scheduler_step(false, false);
2902}
2903
2904template<class Tvec, template<class> class Kern>
2906
2907 // has to be first since there is a barrier that may mess the other timers
2908 shamsys::SystemMetrics system_metrics_start = shamsys::get_system_metrics();
2909
2911 f64 mpi_timer_start = shamcomm::mpi::get_timer("total");
2912
2913 for (auto &callbacks : timestep_callbacks) {
2914 if (callbacks.step_begin_callback) {
2915 shambase::get_check_ref(callbacks.step_begin_callback)();
2916 }
2917 }
2918
2919 Tscal t_current = get_time();
2920 Tscal dt = get_dt_sph();
2921
2922 StackEntry stack_loc{};
2923
2924 if (shamcomm::world_rank() == 0) {
2926 sham::format("---------------- t = {}, dt = {} ----------------", t_current, dt));
2927 }
2928
2929 shambase::Timer tstep;
2930 tstep.start();
2931
2932 // if(shamcomm::world_rank() == 0) std::cout << scheduler().dump_status() << std::endl;
2933 modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
2934 .update_load_balancing();
2935 scheduler().scheduler_step(true, true);
2936 modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
2937 .update_load_balancing();
2938 // if(shamcomm::world_rank() == 0) std::cout << scheduler().dump_status() << std::endl;
2939 scheduler().scheduler_step(false, false);
2940 // if(shamcomm::world_rank() == 0) std::cout << scheduler().dump_status() << std::endl;
2941
2943
2944 using namespace shamrock;
2945 using namespace shamrock::patch;
2946
2947 bool has_b_field = solver_config.has_field_b_on_rho();
2948 bool has_psi_field = solver_config.has_field_psi_on_ch();
2949 bool has_epsilon_field = solver_config.dust_config.has_epsilon_field();
2950 bool has_deltav_field = solver_config.dust_config.has_deltav_field();
2951 bool has_s_j_field = solver_config.dust_config.has_s_j_field();
2952
2953 bool do_nimhd = solver_config.do_nimhd();
2954
2955 PatchDataLayerLayout &pdl = scheduler().pdl_old();
2956
2957 const u32 ixyz = pdl.get_field_idx<Tvec>("xyz");
2958 const u32 ivxyz = pdl.get_field_idx<Tvec>("vxyz");
2959 const u32 iaxyz = pdl.get_field_idx<Tvec>("axyz");
2960 const u32 iuint = pdl.get_field_idx<Tscal>("uint");
2961 const u32 iduint = pdl.get_field_idx<Tscal>("duint");
2962 const u32 ihpart = pdl.get_field_idx<Tscal>("hpart");
2963 const u32 iB_on_rho = (has_b_field) ? pdl.get_field_idx<Tvec>("B/rho") : 0;
2964 const u32 idB_on_rho = (has_b_field) ? pdl.get_field_idx<Tvec>("dB/rho") : 0;
2965 const u32 ipsi_on_ch = (has_psi_field) ? pdl.get_field_idx<Tscal>("psi/ch") : 0;
2966 const u32 idpsi_on_ch = (has_psi_field) ? pdl.get_field_idx<Tscal>("dpsi/ch") : 0;
2967 const u32 iepsilon = (has_epsilon_field) ? pdl.get_field_idx<Tscal>("epsilon") : 0;
2968 const u32 idtepsilon = (has_epsilon_field) ? pdl.get_field_idx<Tscal>("dtepsilon") : 0;
2969 const u32 is_j = (has_s_j_field) ? pdl.get_field_idx<Tscal>("s_j") : 0;
2970 const u32 ids_j_dt = (has_s_j_field) ? pdl.get_field_idx<Tscal>("ds_j_dt") : 0;
2971 const u32 ideltav = (has_deltav_field) ? pdl.get_field_idx<Tvec>("deltav") : 0;
2972 const u32 idtdeltav = (has_deltav_field) ? pdl.get_field_idx<Tvec>("dtdeltav") : 0;
2973
2974 shamrock::SchedulerUtility utility(scheduler());
2975
2976 {
2977 // beginning of SolverGraph migration
2978
2979 using namespace shamrock::solvergraph;
2980
2981 SolverGraph &solver_graph = storage.solver_graph;
2982
2984 // Solver evaluation
2986
2987 shambase::get_check_ref(storage.solver_sequence).evaluate();
2988 }
2989
2990 modules::ExternalForces<Tvec, Kern> ext_forces(context, solver_config, storage);
2991 ext_forces.compute_ext_forces_indep_v();
2992
2993 gen_serial_patch_tree();
2994
2995 apply_position_boundary(t_current + dt);
2996
2997 u64 Npart_all = scheduler().get_total_obj_count();
2998
2999 if (solver_config.enable_particle_reordering
3000 && solve_logs.step_count % solver_config.particle_reordering_step_freq == 0) {
3001 logger::info_ln("SPH", "Reordering particles at step ", solve_logs.step_count);
3002 modules::ParticleReordering<Tvec, u_morton, Kern>(context, solver_config, storage)
3004 }
3005
3006 {
3007 // update part counts and spans since particles have been moved and thus
3008 // new patch can be non-empty/empty
3009 using namespace shamrock::solvergraph;
3010 SolverGraph &solver_graph = storage.solver_graph;
3011 solver_graph.get_node_ref_base("attach fields to scheduler").evaluate();
3012 }
3013
3014 sph_prestep(t_current, dt);
3015
3017
3018 // Here we will add self grav to the external forces indep of vel (this will be moved into a
3019 // sperate module later)
3020 if (solver_config.self_grav_config.is_sg_on()) {
3021 using namespace shamrock::solvergraph;
3022 SolverGraph &solver_graph = storage.solver_graph;
3023 solver_graph.get_node_ref_base("self gravity sequence").evaluate();
3024 }
3025
3026 sph::BasicSPHGhostHandler<Tvec> &ghost_handle = storage.ghost_handler.get();
3027 auto &merged_xyzh = storage.merged_xyzh.get();
3028 shambase::DistributedData<RTree> &trees = storage.merged_pos_trees.get();
3029 // ComputeField<Tscal> &omega = storage.omega.get();
3030
3032 = shambase::get_check_ref(storage.ghost_layout.get());
3033 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>("hpart");
3034 u32 iuint_interf = ghost_layout.get_field_idx<Tscal>("uint");
3035 u32 ivxyz_interf = ghost_layout.get_field_idx<Tvec>("vxyz");
3036 u32 iomega_interf = ghost_layout.get_field_idx<Tscal>("omega");
3037 u32 iB_on_rho_interf = (has_b_field) ? ghost_layout.get_field_idx<Tvec>("B/rho") : 0;
3038 u32 ipsi_on_rho_interf = (has_psi_field) ? ghost_layout.get_field_idx<Tscal>("psi/ch") : 0;
3039
3040 using RTreeField = RadixTreeField<Tscal>;
3042
3043 Tscal next_cfl = 0;
3044
3045 u32 corrector_iter_cnt = 0;
3046 bool need_rerun_corrector = false;
3047 do {
3048
3051
3052 if (corrector_iter_cnt == 50) {
3054 "the corrector has made over 50 loops, either their is a bug, either you are using "
3055 "a dt that is too large");
3056 }
3057
3058 // communicate fields
3060
3061 if (solver_config.has_field_alphaAV()) {
3062
3063 std::shared_ptr<shamrock::solvergraph::PatchDataLayerRefs> patchdatas
3064 = std::make_shared<shamrock::solvergraph::PatchDataLayerRefs>(
3065 "patchdata_layer_ref", "patchdata_layer_ref");
3066
3067 auto node_set_edge = scheduler().get_node_set_edge_patchdata_layer_refs();
3068 node_set_edge->set_edges(patchdatas);
3069 node_set_edge->evaluate();
3070
3072 scheduler().get_layout_ptr_old(), "alpha_AV");
3073 node_copy.set_edges(patchdatas, storage.alpha_av_updated);
3074 node_copy.evaluate();
3075 }
3076
3077 if (solver_config.has_field_dtdivv()) {
3078
3079 if (solver_config.combined_dtdiv_divcurlv_compute) {
3080 if (solver_config.has_field_dtdivv()) {
3081 sph::modules::DiffOperatorDtDivv<Tvec, Kern>(context, solver_config, storage)
3082 .update_dtdivv(true);
3083 }
3084 } else {
3085
3086 if (solver_config.has_field_divv()) {
3087 sph::modules::DiffOperators<Tvec, Kern>(context, solver_config, storage)
3088 .update_divv();
3089 }
3090
3091 if (solver_config.has_field_curlv()) {
3092 sph::modules::DiffOperators<Tvec, Kern>(context, solver_config, storage)
3093 .update_curlv();
3094 }
3095
3096 if (solver_config.has_field_dtdivv()) {
3097 sph::modules::DiffOperatorDtDivv<Tvec, Kern>(context, solver_config, storage)
3098 .update_dtdivv(false);
3099 }
3100 }
3101
3102 } else {
3103 if (solver_config.has_field_divv()) {
3104 sph::modules::DiffOperators<Tvec, Kern>(context, solver_config, storage)
3105 .update_divv();
3106 }
3107
3108 if (solver_config.has_field_curlv()) {
3109 sph::modules::DiffOperators<Tvec, Kern>(context, solver_config, storage)
3110 .update_curlv();
3111 }
3112 }
3113
3114 // if (solver_config.has_field_div_b()) {
3115 // sph::modules::DiffOperatorsB<Tvec, Kern>(context, solver_config, storage)
3116 // .update_divB();
3117 // }
3118
3119 // if (solver_config.has_field_curl_b()) {
3120 // sph::modules::DiffOperatorsB<Tvec, Kern>(context, solver_config, storage)
3121 // .update_curlB();
3122 // }
3124
3125 if (solver_config.has_field_alphaAV()) {
3126
3128 = shambase::get_check_ref(storage.alpha_av_updated);
3129
3130 using InterfaceBuildInfos =
3132
3133 shambase::Timer time_interf;
3134 time_interf.start();
3135
3136 auto field_interf = ghost_handle.template build_interface_native<PatchDataField<Tscal>>(
3137 storage.ghost_patch_cache.get(),
3138 [&](u64 sender,
3139 u64 /*receiver*/,
3140 InterfaceBuildInfos binfo,
3141 sham::DeviceBuffer<u32> &buf_idx,
3142 u32 cnt) -> PatchDataField<Tscal> {
3143 PatchDataField<Tscal> &sender_field = comp_field_send.get_field(sender);
3144
3145 return sender_field.make_new_from_subset(buf_idx, cnt);
3146 });
3147
3149 = ghost_handle.communicate_pdatfield(
3150 std::move(field_interf), 1, storage.exchange_gz_alpha);
3151
3153 = ghost_handle.template merge_native<PatchDataField<Tscal>, PatchDataField<Tscal>>(
3154 std::move(interf_pdat),
3156 PatchDataField<Tscal> &receiver_field
3157 = comp_field_send.get_field(p.id_patch);
3158 return receiver_field.duplicate();
3159 },
3160 [](PatchDataField<Tscal> &mpdat, PatchDataField<Tscal> &pdat_interf) {
3161 mpdat.insert(pdat_interf);
3162 });
3163
3164 time_interf.stop();
3165 storage.timings_details.interface += time_interf.elapsed_sec();
3166
3167 storage.alpha_av_ghost.set(std::move(merged_field));
3168 }
3169
3170 if (do_nimhd) {
3171
3172 // communicate needed fields (B,b, hb): done just before
3173
3174 // compute J field
3175 update_j();
3176
3177 // communicate J field
3178 shamrock::solvergraph::Field<Tvec> &comp_field_send
3179 = shambase::get_check_ref(storage.MagCurrentJ);
3180
3181 using InterfaceBuildInfos =
3183
3184 shambase::Timer time_interf;
3185 time_interf.start();
3186
3187 auto field_interf = ghost_handle.template build_interface_native<PatchDataField<Tvec>>(
3188 storage.ghost_patch_cache.get(),
3189 [&](u64 sender,
3190 u64 /*receiver*/,
3191 InterfaceBuildInfos binfo,
3192 sham::DeviceBuffer<u32> &buf_idx,
3193 u32 cnt) -> PatchDataField<Tvec> {
3194 PatchDataField<Tvec> &sender_field = comp_field_send.get_field(sender);
3195
3196 return sender_field.make_new_from_subset(buf_idx, cnt);
3197 });
3198
3200 = ghost_handle.communicate_pdatfield(
3201 std::move(field_interf), 1, storage.exchange_gz_J);
3202
3204 = ghost_handle.template merge_native<PatchDataField<Tvec>, PatchDataField<Tvec>>(
3205 std::move(interf_pdat),
3207 PatchDataField<Tvec> &receiver_field
3208 = comp_field_send.get_field(p.id_patch);
3209 return receiver_field.duplicate();
3210 },
3211 [](PatchDataField<Tvec> &mpdat, PatchDataField<Tvec> &pdat_interf) {
3212 mpdat.insert(pdat_interf);
3213 });
3214
3215 time_interf.stop();
3216 storage.timings_details.interface += time_interf.elapsed_sec();
3217
3218 // we get J with ghosts !
3219 storage.MagCurrentJ_ghost.set(std::move(merged_field));
3220 }
3221
3222 // compute pressure
3224
3225 constexpr bool debug_interfaces = false;
3226 if constexpr (debug_interfaces) {
3227
3228 if (solver_config.do_debug_dump) {
3229
3231 = storage.merged_patchdata_ghost.get();
3232
3233 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3234 MergedPatchData &merged_patch = mpdat.get(cur_p.id_patch);
3235 PatchDataLayer &mpdat = merged_patch.pdat;
3236
3237 sycl::buffer<Tvec> &buf_xyz = shambase::get_check_ref(
3238 merged_xyzh.get(cur_p.id_patch).field_pos.get_buf());
3239 sycl::buffer<Tvec> &buf_vxyz = mpdat.get_field_buf_ref<Tvec>(ivxyz_interf);
3240 sycl::buffer<Tscal> &buf_hpart = mpdat.get_field_buf_ref<Tscal>(ihpart_interf);
3241
3242 u32 total_elements = shambase::get_check_ref(storage.part_counts_with_ghost)
3243 .indexes.get(cur_p.id_patch);
3244 SHAM_ASSERT(merged_patch.total_elements == total_elements);
3245
3247 total_elements,
3248 solver_config.gpart_mass,
3249
3250 buf_xyz,
3251 buf_hpart,
3252 buf_vxyz};
3253
3254 make_interface_debug_phantom_dump(info).gen_file().write_to_file(
3255 solver_config.debug_dump_filename);
3256 logger::raw_ln("writing : ", solver_config.debug_dump_filename);
3257 });
3258 }
3259 }
3260
3261 // compute force
3262 shamlog_debug_ln("sph::BasicGas", "compute force");
3263
3264 // save old acceleration
3266
3267 update_derivs(dt);
3268
3270 // Gravitational Wave emission
3272 bool compute_GW = solver_config.compute_gw;
3273
3274 if (compute_GW) {
3275 using namespace shamrock::solvergraph;
3277
3278 auto central_pos = IDataEdge<Tvec>::make_shared("x_0", "\\mathbf{x}_0");
3279 central_pos->data = Tvec{0, 0, 0};
3280
3281 auto central_vel = IDataEdge<Tvec>::make_shared("v_0", "\\mathbf{v}_0");
3282 central_vel->data = Tvec{0, 0, 0};
3283
3284 auto central_acc = IDataEdge<Tvec>::make_shared("a_0", "\\mathbf{a}_0");
3285 central_acc->data = Tvec{0, 0, 0};
3286
3287 auto gw_prefactor = IDataEdge<Tscal>::make_shared("gw_prefactor", "gw_prefactor");
3288 gw_prefactor->data = Tscal(1); // should be G/c^2D
3289
3290 auto theta_gw = IDataEdge<Tscal>::make_shared("theta_gw", "\\theta_{\\rm gw}");
3291 theta_gw->data = Tscal(0);
3292
3293 auto phi_gw = IDataEdge<Tscal>::make_shared("phi_gw", "\\phi_{\\rm gw}");
3294 phi_gw->data = Tscal(0);
3295
3296 ComputeField<Tscal> gw_mass_field
3297 = utility.make_compute_field<Tscal>("gw_mass", 1, solver_config.gpart_mass);
3298
3299 auto spans_masses = std::make_shared<FieldRefs<Tscal>>("m", "m");
3300 map_field_refs_ext(scheduler(), gw_mass_field, *spans_masses);
3301
3302 const u32 iaxyz_ext = pdl.get_field_idx<Tvec>("axyz_ext");
3303 auto spans_accel_ext
3304 = std::make_shared<FieldRefs<Tvec>>("axyz_ext", "\\mathbf{a}_{\\rm ext}");
3305 map_field_refs(scheduler(), iaxyz_ext, *spans_accel_ext);
3306
3307 auto ddq = IDataEdge<typename GW::Tddq>::make_shared("ddq", "\\ddot{Q}");
3308 auto ddq_xy = IDataEdge<typename GW::Tddqxy>::make_shared("ddq_xy", "\\ddot{Q}_{xy}");
3309 auto hx = IDataEdge<typename GW::Th>::make_shared("hx", "h_x");
3310 auto hp = IDataEdge<typename GW::Th>::make_shared("hp", "h_+");
3311
3312 GW node_computeGW{};
3313 node_computeGW.set_edges(
3314 storage.solver_graph.template get_edge_ptr<FieldRefs<Tvec>>("xyz"),
3315 storage.solver_graph.template get_edge_ptr<FieldRefs<Tvec>>("vxyz"),
3316 storage.solver_graph.template get_edge_ptr<FieldRefs<Tvec>>("axyz"),
3317 spans_masses,
3318 spans_accel_ext,
3319 central_pos,
3320 central_vel,
3321 central_acc,
3322 gw_prefactor,
3323 theta_gw,
3324 phi_gw,
3325 storage.part_counts,
3326 ddq,
3327 ddq_xy,
3328 hx,
3329 hp);
3330
3331 node_computeGW.evaluate();
3332
3333 // TODO: send that somewhere rather than doing a print
3334 logger::raw_ln("################## hx = ", hx->data);
3335 logger::raw_ln("################## hp = ", hp->data);
3336 logger::raw_ln("################## ddq = ", ddq->data);
3337 logger::raw_ln("################## ddq_xy = ", ddq_xy->data);
3338 }
3339
3340 bool has_luminosity = solver_config.compute_luminosity;
3341
3342 if (has_luminosity) {
3343 const u32 iluminosity = pdl.get_field_idx<Tscal>("luminosity");
3344
3345 shambase::get_check_ref(storage.hpart_with_ghosts)
3346 .set_refs(storage.merged_xyzh.get()
3347 .template map<std::reference_wrapper<PatchDataField<Tscal>>>(
3348 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
3349 return std::ref(mpdat.get_field<Tscal>(
3350 1)); // hpart is at index 1 in merged_xyzh
3351 }));
3352
3353 shambase::get_check_ref(storage.hpart_with_ghosts)
3354 .set_refs(storage.merged_xyzh.get()
3355 .template map<std::reference_wrapper<PatchDataField<Tscal>>>(
3356 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
3357 return std::ref(mpdat.get_field<Tscal>(1));
3358 }));
3359
3360 auto uint_with_ghost = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("", "");
3362 set_uint_with_ghost_refs(
3363 [&](shamrock::solvergraph::FieldRefs<Tscal> &field_uint_with_ghost_edge) {
3365 = storage.merged_patchdata_ghost.get();
3366
3367 shamrock::solvergraph::DDPatchDataFieldRef<Tscal> field_uint_with_ghost_refs
3368 = {};
3369
3370 scheduler().for_each_patchdata_nonempty(
3371 [&](const Patch p, PatchDataLayer &pdat) {
3372 PatchDataLayer &mpdat = mpdats.get(p.id_patch);
3373
3374 auto &field = mpdat.get_field<Tscal>(iuint_interf);
3375 field_uint_with_ghost_refs.add_obj(p.id_patch, std::ref(field));
3376 });
3377
3378 field_uint_with_ghost_edge.set_refs(field_uint_with_ghost_refs);
3379 });
3380
3381 set_uint_with_ghost_refs.set_edges(uint_with_ghost);
3382
3383 auto omega_with_ghost = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("", "");
3385 set_omega_with_ghost_refs([&](shamrock::solvergraph::FieldRefs<Tscal>
3386 &field_omega_with_ghost_edge) {
3388 = storage.merged_patchdata_ghost.get();
3389
3390 shamrock::solvergraph::DDPatchDataFieldRef<Tscal> field_omega_with_ghost_refs
3391 = {};
3392
3393 scheduler().for_each_patchdata_nonempty(
3394 [&](const Patch p, PatchDataLayer &pdat) {
3395 PatchDataLayer &mpdat = mpdats.get(p.id_patch);
3396
3397 auto &field = mpdat.get_field<Tscal>(iomega_interf);
3398 field_omega_with_ghost_refs.add_obj(p.id_patch, std::ref(field));
3399 });
3400
3401 field_omega_with_ghost_edge.set_refs(field_omega_with_ghost_refs);
3402 });
3403
3404 set_omega_with_ghost_refs.set_edges(omega_with_ghost);
3405
3406 auto luminosity = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("", "");
3407
3409 set_luminosity_refs(
3410 [&](shamrock::solvergraph::FieldRefs<Tscal> &field_luminosity_edge) {
3412 = storage.merged_patchdata_ghost.get();
3413
3415 = {};
3416
3417 scheduler().for_each_patchdata_nonempty(
3418 [&](const Patch p, PatchDataLayer &pdat) {
3419 auto &field = pdat.get_field<Tscal>(iluminosity);
3420 field_luminosity_refs.add_obj(p.id_patch, std::ref(field));
3421 });
3422 field_luminosity_edge.set_refs(field_luminosity_refs);
3423 });
3424
3425 set_luminosity_refs.set_edges(luminosity);
3426
3427 set_uint_with_ghost_refs.evaluate();
3428 set_omega_with_ghost_refs.evaluate();
3429 set_luminosity_refs.evaluate();
3430
3431 Tscal alpha_u = solver_config.artif_viscosity.get_alpha_u().value();
3432
3434 solver_config.gpart_mass, alpha_u};
3435
3436 compute_luminosity.set_edges(
3437 storage.part_counts,
3438 storage.part_counts_with_ghost,
3439 storage.neigh_cache,
3440 storage.positions_with_ghosts,
3441 storage.hpart_with_ghosts,
3442 omega_with_ghost,
3443 uint_with_ghost,
3444 storage.pressure,
3445 luminosity);
3446
3447 compute_luminosity.evaluate();
3448 }
3449
3450 modules::ConservativeCheck<Tvec, Kern> cv_check(context, solver_config, storage);
3451 cv_check.check_conservation();
3452
3453 ComputeField<Tscal> vepsilon_v_sq
3454 = utility.make_compute_field<Tscal>("vmean epsilon_v^2", 1);
3455 ComputeField<Tscal> uepsilon_u_sq
3456 = utility.make_compute_field<Tscal>("umean epsilon_u^2", 1);
3457
3458 // corrector
3459 shamlog_debug_ln("sph::BasicGas", "leapfrog corrector");
3460 utility.fields_leapfrog_corrector<Tvec>(
3461 ivxyz, iaxyz, storage.old_axyz.get(), vepsilon_v_sq, dt / 2);
3462 utility.fields_leapfrog_corrector<Tscal>(
3463 iuint, iduint, storage.old_duint.get(), uepsilon_u_sq, dt / 2);
3464
3465 if (solver_config.has_field_b_on_rho()) {
3466 ComputeField<Tscal> BOR_epsilon_BOR_sq
3467 = utility.make_compute_field<Tscal>("B/rho epsilon_B/rho^2", 1);
3468 utility.fields_leapfrog_corrector<Tvec>(
3469 iB_on_rho, idB_on_rho, storage.old_dB_on_rho.get(), BOR_epsilon_BOR_sq, dt / 2);
3470 }
3471 if (solver_config.has_field_b_on_rho()) {
3472 ComputeField<Tscal> POC_epsilon_POC_sq
3473 = utility.make_compute_field<Tscal>("psi/ch epsilon_psi/ch^2", 1);
3474 utility.fields_leapfrog_corrector<Tscal>(
3475 ipsi_on_ch, idpsi_on_ch, storage.old_dpsi_on_ch.get(), POC_epsilon_POC_sq, dt / 2);
3476 }
3477
3478 if (solver_config.dust_config.has_epsilon_field()) {
3479 ComputeField<Tscal> epsilon_epsilon_sq
3480 = utility.make_compute_field<Tscal>("epsilon epsilon^2", 1);
3481 utility.fields_leapfrog_corrector<Tscal>(
3482 iepsilon, idtepsilon, storage.old_dtepsilon.get(), epsilon_epsilon_sq, dt / 2);
3483 }
3484
3485 if (solver_config.dust_config.has_deltav_field()) {
3486 ComputeField<Tscal> epsilon_deltav_sq
3487 = utility.make_compute_field<Tscal>("deltav deltav^2", 1);
3488 utility.fields_leapfrog_corrector<Tvec>(
3489 ideltav, idtdeltav, storage.old_dtdeltav.get(), epsilon_deltav_sq, dt / 2);
3490 }
3491
3492 if (solver_config.dust_config.has_s_j_field()) {
3493 ComputeField<Tscal> s_j_s_j_sq = utility.make_compute_field<Tscal>(
3494 "s_j s_j^2", solver_config.dust_config.get_dust_nvar());
3495 bool ensure_positivity
3496 = solver_config.dust_config.get_monofluid_tva().ensure_s_j_positivity;
3497 if (ensure_positivity) {
3498 utility.fields_leapfrog_corrector_positive_only<Tscal>(
3499 is_j, ids_j_dt, storage.old_ds_j_dt.get(), s_j_s_j_sq, dt / 2);
3500 } else {
3501 utility.fields_leapfrog_corrector<Tscal>(
3502 is_j, ids_j_dt, storage.old_ds_j_dt.get(), s_j_s_j_sq, dt / 2);
3503 }
3504
3505 auto &monofluid_tva_cfg = solver_config.dust_config.get_monofluid_tva();
3506 if (monofluid_tva_cfg.should_clamp_dust_density()) {
3507 auto hfactd_edge
3508 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("hfactd", "hfactd");
3509 hfactd_edge->data = Kernel::hfactd;
3510
3511 auto clamp_frac_edge = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
3512 "clamp_frac", "clamp_frac");
3513 clamp_frac_edge->data = monofluid_tva_cfg.get_clamp_dust_frac();
3514
3516 solver_config.dust_config.get_dust_nvar());
3517 density_clamp.set_edges(
3518 storage.solver_graph.template get_edge_ptr<shamrock::solvergraph::Indexes<u32>>(
3519 "part_counts"),
3520 storage.solver_graph
3521 .template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
3522 "gpart_mass"),
3523 hfactd_edge,
3524 clamp_frac_edge,
3525 storage.solver_graph
3526 .template get_edge_ptr<shamrock::solvergraph::FieldRefs<Tscal>>("hpart"),
3527 storage.solver_graph
3528 .template get_edge_ptr<shamrock::solvergraph::FieldRefs<Tscal>>("s_j"));
3529 density_clamp.evaluate();
3530 }
3531 }
3532
3533 storage.old_axyz.reset();
3534 storage.old_duint.reset();
3535 if (solver_config.has_field_b_on_rho()) {
3536 storage.old_dB_on_rho.reset();
3537 }
3538 if (solver_config.has_field_b_on_rho()) {
3539 storage.old_dpsi_on_ch.reset();
3540 }
3541
3542 if (solver_config.dust_config.has_epsilon_field()) {
3543 storage.old_dtepsilon.reset();
3544 }
3545
3546 if (solver_config.dust_config.has_deltav_field()) {
3547 storage.old_dtdeltav.reset();
3548 }
3549
3550 if (solver_config.dust_config.has_s_j_field()) {
3551 storage.old_ds_j_dt.reset();
3552 }
3553
3554 Tscal rank_veps_v = sycl::sqrt(vepsilon_v_sq.compute_rank_max());
3556 // compute means //////////////////////////
3558
3559 Tscal sum_vsq = utility.compute_rank_dot_sum<Tvec>(ivxyz);
3560
3561 Tscal vmean_sq = shamalgs::collective::allreduce_sum(sum_vsq) / Tscal(Npart_all);
3562
3563 Tscal vmean = sycl::sqrt(vmean_sq);
3564
3565 Tscal rank_eps_v = rank_veps_v / vmean;
3566
3567 if (vmean <= 0) {
3568 rank_eps_v = 0;
3569 }
3570
3571 Tscal eps_v = shamalgs::collective::allreduce_max(rank_eps_v);
3572
3573 shamlog_debug_ln("BasicGas", "epsilon v :", eps_v);
3574
3575 if (eps_v > 1e-2) {
3576 if (shamcomm::world_rank() == 0) {
3578 "BasicGasSPH",
3579 sham::format(
3580 "the corrector tolerance are broken the step will "
3581 "be re rerunned\n eps_v = {}",
3582 eps_v));
3583 }
3584 need_rerun_corrector = true;
3585 set_cfl_multipler(get_cfl_multipler() / 2);
3586
3587 // logger::info_ln("rerun corrector ...");
3588 } else {
3589 need_rerun_corrector = false;
3590 }
3591
3592 if (!need_rerun_corrector) {
3593
3594 storage.solver_graph.get_node_ref_base("sink corrector").evaluate();
3595
3596 // write back alpha av field
3597 if (solver_config.has_field_alphaAV()) {
3598
3599 const u32 ialpha_AV = pdl.get_field_idx<Tscal>("alpha_AV");
3600 shamrock::solvergraph::Field<Tscal> &alpha_av_updated
3601 = shambase::get_check_ref(storage.alpha_av_updated);
3602
3603 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3604 sham::DeviceBuffer<Tscal> &buf_alpha_av
3605 = pdat.get_field<Tscal>(ialpha_AV).get_buf();
3606 sham::DeviceBuffer<Tscal> &buf_alpha_av_updated
3607 = alpha_av_updated.get_field(cur_p.id_patch).get_buf();
3608
3609 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3610
3612 q,
3613 sham::MultiRef{buf_alpha_av_updated},
3614 sham::MultiRef{buf_alpha_av},
3615 pdat.get_obj_cnt(),
3616 [](u32 id_a,
3617 const Tscal *__restrict alpha_av_updated,
3618 Tscal *__restrict alpha_av) {
3619 alpha_av[id_a] = alpha_av_updated[id_a];
3620 });
3621 });
3622 }
3623
3624 if (do_nimhd) {
3625
3626 const u32 iJ = pdl.get_field_idx<Tvec>("J");
3628 = shambase::get_check_ref(storage.MagCurrentJ);
3629
3630 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3631 sham::DeviceBuffer<Tvec> &buf_J = pdat.get_field<Tvec>(iJ).get_buf();
3632
3633 sham::DeviceBuffer<Tvec> &buf_MagCurrentJ
3634 = MagCurrentJ.get_field(cur_p.id_patch).get_buf();
3635
3636 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3637
3639 q,
3640 sham::MultiRef{buf_MagCurrentJ},
3641 sham::MultiRef{buf_J},
3642 pdat.get_obj_cnt(),
3643 [](u32 id_a, const Tvec *__restrict MagCurrentJ, Tvec *__restrict J) {
3644 J[id_a] = MagCurrentJ[id_a];
3645 });
3646 });
3647 }
3648
3649 shamlog_debug_ln("BasicGas", "computing next CFL");
3650
3651 // Update element counts
3652 shambase::get_check_ref(storage.part_counts).indexes
3653 = storage.merged_xyzh.get().template map<u32>(
3654 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
3655 return scheduler().patch_data.get_pdat(id).get_obj_cnt();
3656 });
3657
3658 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> vsig_max_dt
3659 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3660 1, "vsig_a", "v_{\\rm sig}");
3661 vsig_max_dt->ensure_sizes(shambase::get_check_ref(storage.part_counts).indexes);
3662
3663 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> vclean_dt;
3664 if (has_psi_field) {
3665 vclean_dt = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3666 1, "vclean_a", "v_{\\rm clean}");
3667 vclean_dt->ensure_sizes(shambase::get_check_ref(storage.part_counts).indexes);
3668 }
3669
3671 = storage.merged_patchdata_ghost.get();
3672
3673 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3674 PatchDataLayer &mpdat = mpdats.get(cur_p.id_patch);
3675
3677 = merged_xyzh.get(cur_p.id_patch).template get_field_buf_ref<Tvec>(0);
3678 sham::DeviceBuffer<Tvec> &buf_vxyz = mpdat.get_field_buf_ref<Tvec>(ivxyz_interf);
3679 sham::DeviceBuffer<Tscal> &buf_hpart
3680 = mpdat.get_field_buf_ref<Tscal>(ihpart_interf);
3681 sham::DeviceBuffer<Tscal> &buf_uint = mpdat.get_field_buf_ref<Tscal>(iuint_interf);
3682 sham::DeviceBuffer<Tscal> &buf_pressure
3683 = shambase::get_check_ref(storage.pressure).get_field(cur_p.id_patch).get_buf();
3684 sham::DeviceBuffer<Tscal> &cs_buf = shambase::get_check_ref(storage.soundspeed)
3685 .get_field(cur_p.id_patch)
3686 .get_buf();
3687
3688 sham::DeviceBuffer<Tscal> &vsig_buf = vsig_max_dt->get_buf(cur_p.id_patch);
3689
3690 sycl::range range_npart{pdat.get_obj_cnt()};
3691
3692 tree::ObjectCache &pcache
3693 = shambase::get_check_ref(storage.neigh_cache).get_cache(cur_p.id_patch);
3694
3696
3697 {
3698
3699 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3700 sham::EventList depends_list;
3701
3702 auto xyz = buf_xyz.get_read_access(depends_list);
3703 auto vxyz = buf_vxyz.get_read_access(depends_list);
3704 auto hpart = buf_hpart.get_read_access(depends_list);
3705 auto u = buf_uint.get_read_access(depends_list);
3706 auto pressure = buf_pressure.get_read_access(depends_list);
3707 auto cs = cs_buf.get_read_access(depends_list);
3708 auto vsig = vsig_buf.get_write_access(depends_list);
3709 auto particle_looper_ptrs = pcache.get_read_access(depends_list);
3710
3711 NamedStackEntry tmppp{"compute vsig"};
3712 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
3713 const Tscal pmass = solver_config.gpart_mass;
3714 const Tscal alpha_u = 1.0;
3715 const Tscal alpha_AV = 1.0;
3716 const Tscal beta_AV = 2.0;
3717
3718 tree::ObjectCacheIterator particle_looper(particle_looper_ptrs);
3719
3720 constexpr Tscal Rker2 = Kernel::Rkern * Kernel::Rkern;
3721
3722 shambase::parallel_for(
3723 cgh, pdat.get_obj_cnt(), "compute vsig", [=](i32 id_a) {
3724 using namespace shamrock::sph;
3725
3726 Tvec sum_axyz = {0, 0, 0};
3727 Tscal sum_du_a = 0;
3728 Tscal h_a = hpart[id_a];
3729
3730 Tvec xyz_a = xyz[id_a];
3731 Tvec vxyz_a = vxyz[id_a];
3732
3733 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
3734 Tscal rho_a_sq = rho_a * rho_a;
3735 Tscal rho_a_inv = 1. / rho_a;
3736
3737 Tscal P_a = pressure[id_a];
3738
3739 const Tscal u_a = u[id_a];
3740
3741 Tscal cs_a = cs[id_a];
3742
3743 Tscal vsig_max = 0;
3744
3745 particle_looper.for_each_object(id_a, [&](u32 id_b) {
3746 // compute only omega_a
3747 Tvec dr = xyz_a - xyz[id_b];
3748 Tscal rab2 = sycl::dot(dr, dr);
3749 Tscal h_b = hpart[id_b];
3750
3751 if (rab2 > h_a * h_a * Rker2 && rab2 > h_b * h_b * Rker2) {
3752 return;
3753 }
3754
3755 Tscal rab = sycl::sqrt(rab2);
3756 Tvec vxyz_b = vxyz[id_b];
3757 Tvec v_ab = vxyz_a - vxyz_b;
3758 const Tscal u_b = u[id_b];
3759
3760 Tvec r_ab_unit = dr / rab;
3761
3762 if (rab < 1e-9) {
3763 r_ab_unit = {0, 0, 0};
3764 }
3765
3766 Tscal rho_b = rho_h(pmass, h_b, Kernel::hfactd);
3767 Tscal P_b = pressure[id_b];
3768 Tscal cs_b = cs[id_b];
3769 Tscal v_ab_r_ab = sycl::dot(v_ab, r_ab_unit);
3770 Tscal abs_v_ab_r_ab = sycl::fabs(v_ab_r_ab);
3771
3773 // internal energy update
3774 // scalar : f32 | vector : f32_3
3775 const Tscal alpha_a = alpha_AV;
3776 const Tscal alpha_b = alpha_AV;
3777
3778 Tscal vsig_a = alpha_a * cs_a + beta_AV * abs_v_ab_r_ab;
3779
3780 vsig_max = sycl::fmax(vsig_max, vsig_a);
3781 });
3782
3783 vsig[id_a] = vsig_max;
3784 });
3785 });
3786
3787 if (has_psi_field) {
3788 NamedStackEntry tmppp{"compute vclean"};
3789 Tscal const mu_0 = solver_config.get_constant_mu_0();
3790 sham::DeviceBuffer<Tscal> &vclean_buf = vclean_dt->get_buf(cur_p.id_patch);
3791
3792 Tvec *B_on_rho = mpdat.get_field_buf_ref<Tvec>(iB_on_rho_interf)
3793 .get_write_access(depends_list);
3794
3795 auto vclean = vclean_buf.get_write_access(depends_list);
3796
3797 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
3798 const Tscal pmass = solver_config.gpart_mass;
3799
3800 tree::ObjectCacheIterator particle_looper(particle_looper_ptrs);
3801
3802 constexpr Tscal Rker2 = Kernel::Rkern * Kernel::Rkern;
3803
3804 shambase::parallel_for(
3805 cgh, pdat.get_obj_cnt(), "compute vclean", [=](i32 id_a) {
3806 using namespace shamrock::sph;
3807
3808 Tscal h_a = hpart[id_a];
3809 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
3810 const Tscal u_a = u[id_a];
3811 Tscal cs_a = cs[id_a];
3812 Tvec B_a = B_on_rho[id_a] * rho_a;
3813
3814 Tscal vclean_a = shamphys::MHD_physics<Tvec, Tscal>::v_shock(
3815 cs_a, B_a, rho_a, mu_0);
3816
3817 vclean[id_a] = vclean_a;
3818 });
3819 });
3820 mpdat.get_field_buf_ref<Tvec>(iB_on_rho_interf).complete_event_state(e);
3821 vclean_buf.complete_event_state(e);
3822 };
3823
3824 buf_xyz.complete_event_state(e);
3825 buf_vxyz.complete_event_state(e);
3826 buf_hpart.complete_event_state(e);
3827 buf_uint.complete_event_state(e);
3828 buf_pressure.complete_event_state(e);
3829 cs_buf.complete_event_state(e);
3830 vsig_buf.complete_event_state(e);
3831
3832 sham::EventList resulting_events;
3833 resulting_events.add_event(e);
3834 pcache.complete_event_state(resulting_events);
3835 }
3836 });
3837
3838 std::shared_ptr<shamrock::solvergraph::Field<Tscal>> cfl_dt
3839 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(
3840 1, "cfl_dt", "\\Delta t_{cfl}");
3841 cfl_dt->ensure_sizes(shambase::get_check_ref(storage.part_counts).indexes);
3842
3843 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> axyz_refs
3844 = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>("axyz", "\\mathbf{a}");
3845 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hpart_refs
3846 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("hpart", "h");
3847
3848 map_field_refs(scheduler(), iaxyz, *axyz_refs);
3849 map_field_refs_ext(scheduler(), mpdats, ihpart_interf, *hpart_refs);
3850
3851 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
3852
3853 auto reset_dt_part_field = [&]() {
3854 if (solver_config.should_save_dt_to_fields()) {
3855 const u32 idt_part = pdl.get_field_idx<Tscal>("dt_part");
3856 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3857 sham::DeviceBuffer<Tscal> &buf_dt_part
3858 = pdat.get_field_buf_ref<Tscal>(idt_part);
3859 buf_dt_part.fill(shambase::get_infty<Tscal>());
3860 });
3861 }
3862 };
3863
3864 auto save_dt_min_to_dt_part = [&]() {
3865 if (solver_config.should_save_dt_to_fields()) {
3866 const u32 idt_part = pdl.get_field_idx<Tscal>("dt_part");
3867 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3868 sham::DeviceBuffer<Tscal> &buf_dt_part
3869 = pdat.get_field_buf_ref<Tscal>(idt_part);
3870 sham::DeviceBuffer<Tscal> &buf_dt = cfl_dt->get_buf(cur_p.id_patch);
3871
3873 q,
3874 sham::MultiRef{buf_dt},
3875 sham::MultiRef{buf_dt_part},
3876 pdat.get_obj_cnt(),
3877 [](u32 id_a, const Tscal *dt, Tscal *dt_part) {
3878 dt_part[id_a] = sycl::min(dt_part[id_a], dt[id_a]);
3879 });
3880 });
3881 }
3882 };
3883
3884 // reset the cfl_dt field
3885 auto reset_cfl_dt = [&]() {
3886 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
3887 cfl_dt->get_buf(cur_p.id_patch).fill(shambase::get_infty<Tscal>());
3888 });
3889 };
3890
3891 Tscal C_cour = solver_config.cfl_config.cfl_cour * get_cfl_multipler();
3892 Tscal C_force = solver_config.cfl_config.cfl_force * get_cfl_multipler();
3893 Tscal eta_phi = solver_config.cfl_config.eta_sink;
3894
3895 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_cour_edge
3896 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("C_cour", "C_{cour}");
3897 C_cour_edge->data = C_cour;
3898 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_force_edge
3899 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("C_force", "C_{force}");
3900 C_force_edge->data = C_force;
3901 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_phi_edge
3902 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("eta_phi", "\\eta_{\\phi}");
3903 eta_phi_edge->data = eta_phi;
3904
3905 std::shared_ptr<ComputeCFLCourant<Tscal>> compute_cfl_courant
3906 = std::make_shared<ComputeCFLCourant<Tscal>>();
3907 compute_cfl_courant->set_edges(
3908 storage.part_counts, C_cour_edge, hpart_refs, vsig_max_dt, cfl_dt);
3909
3910 std::shared_ptr<ComputeCFLForce<Tvec>> compute_cfl_force
3911 = std::make_shared<ComputeCFLForce<Tvec>>();
3912 compute_cfl_force->set_edges(
3913 storage.part_counts, C_force_edge, hpart_refs, axyz_refs, cfl_dt);
3914
3915 std::shared_ptr<ComputeCFLNIMHD<Tvec>> compute_cfl_NIMHD;
3916 if (do_nimhd) {
3917 compute_cfl_NIMHD = std::make_shared<ComputeCFLNIMHD<Tvec>>();
3918
3919 Tscal C_NIMHD = solver_config.cfl_config.cfl_NIMHD * get_cfl_multipler();
3920 Cfg_MHD cfg_mhd = solver_config.mhd_config;
3921 auto *nimhd = std::get_if<typename Cfg_MHD::NonIdealMHD>(&cfg_mhd.configMHD);
3922
3923 Tscal eta_AD = nimhd->etaAD;
3924 Tscal eta_O = nimhd->etaO;
3925 Tscal eta_H = nimhd->etaH;
3926 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_NIMHD_edge
3927 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("C_NIMHD", "C_{NIMHD}");
3928 C_NIMHD_edge->data = C_NIMHD;
3929 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_O_edge
3930 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("eta_O", "eta_{O}");
3931 eta_O_edge->data = eta_O;
3932 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_AD_edge
3933 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("eta_AD", "eta_{AD}");
3934 eta_AD_edge->data = eta_AD;
3935 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> eta_H_edge
3936 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("eta_H", "eta_{H}");
3937 eta_H_edge->data = eta_H;
3938
3939 compute_cfl_NIMHD->set_edges(
3940 storage.part_counts,
3941 C_NIMHD_edge,
3942 eta_O_edge,
3943 eta_AD_edge,
3944 eta_H_edge,
3945 hpart_refs,
3946 cfl_dt);
3947 }
3948
3949 std::shared_ptr<ComputeCFLDivBCleaning<Tscal>> compute_cfl_divB_cleaning;
3950 if (has_psi_field) {
3951 compute_cfl_divB_cleaning = std::make_shared<ComputeCFLDivBCleaning<Tscal>>();
3952 compute_cfl_divB_cleaning->set_edges(
3953 storage.part_counts, C_cour_edge, hpart_refs, vclean_dt, cfl_dt);
3954 }
3955
3956 std::shared_ptr<ComputeCFLDust1Fluid<Tvec>> compute_cfl_dust1_fluid;
3957 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> s_j_refs;
3958 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> hfactd_edge;
3959
3960 if (solver_config.dust_config.has_s_j_field()) {
3961 u32 ndust = solver_config.dust_config.get_dust_nvar();
3962
3963 compute_cfl_dust1_fluid = std::make_shared<ComputeCFLDust1Fluid<Tvec>>(ndust);
3964
3965 auto t_j_field
3966 = storage.solver_graph
3967 .template get_edge_ptr<shamrock::solvergraph::Field<Tscal>>("Ts_j");
3968
3969 auto pmass_edge
3970 = storage.solver_graph
3971 .template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
3972 "gpart_mass");
3973
3974 s_j_refs = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("s_j", "s_j");
3975
3976 hfactd_edge
3977 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("hfactd", "hfactd");
3978 hfactd_edge->data = Kernel::hfactd;
3979
3980 map_field_refs(scheduler(), is_j, *s_j_refs);
3981
3982 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_1fluid_edge
3983 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
3984 "C_1fluid", "C_{1fluid}");
3985 C_1fluid_edge->data
3986 = solver_config.dust_config.get_monofluid_tva().C_1_fluid * get_cfl_multipler();
3987
3988 compute_cfl_dust1_fluid->set_edges(
3989 storage.part_counts,
3990 C_1fluid_edge,
3991 pmass_edge,
3992 hfactd_edge,
3993 hpart_refs,
3994 storage.soundspeed,
3995 s_j_refs,
3996 t_j_field,
3997 cfl_dt);
3998 }
3999
4000 std::shared_ptr<ComputeCFLDustDrift<Tvec>> compute_cfl_dust_drift;
4001 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> C_drift_edge;
4002 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> cfl_density_threshold_edge;
4003 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tvec>> delta_v_refs;
4004
4005 if (solver_config.dust_config.has_s_j_field()) {
4006 u32 ndust = solver_config.dust_config.get_dust_nvar();
4007
4008 compute_cfl_dust_drift = std::make_shared<ComputeCFLDustDrift<Tvec>>(ndust);
4009
4010 delta_v_refs = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>(
4011 "delta_v", "delta_v");
4012 const u32 idelta_v = pdl.get_field_idx<Tvec>("delta_v");
4013 map_field_refs(scheduler(), idelta_v, *delta_v_refs);
4014
4015 auto &cfg_monofluid_tva = solver_config.dust_config.get_monofluid_tva();
4016
4017 C_drift_edge
4018 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("C_drift", "C_{drift}");
4019 C_drift_edge->data = cfg_monofluid_tva.C_drift * get_cfl_multipler();
4020
4021 cfl_density_threshold_edge = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
4022 "cfl_density_threshold", "cfl_density_threshold");
4023 cfl_density_threshold_edge->data = cfg_monofluid_tva.cfl_density_threshold;
4024
4025 auto pmass_edge
4026 = storage.solver_graph
4027 .template get_edge_ptr<shamrock::solvergraph::IDataEdge<Tscal>>(
4028 "gpart_mass");
4029
4030 compute_cfl_dust_drift->set_edges(
4031 storage.part_counts,
4032 C_drift_edge,
4033 cfl_density_threshold_edge,
4034 pmass_edge,
4035 hfactd_edge,
4036 hpart_refs,
4037 s_j_refs,
4038 delta_v_refs,
4039 cfl_dt);
4040 }
4041
4042 bool show_cfl_detail = solver_config.show_cfl_detail;
4043 std::vector<std::pair<std::string, Tscal>> cfl_detail;
4044
4045 auto save_cfl_detail = [&](const char *key) {
4046 if (show_cfl_detail) {
4047 save_dt_min_to_dt_part();
4048 cfl_detail.push_back(
4049 {std::string(key), cfl_dt->get_native().compute_rank_min()});
4050 reset_cfl_dt();
4051 }
4052 };
4053
4054 reset_dt_part_field();
4055 reset_cfl_dt();
4056
4057 compute_cfl_courant->evaluate();
4058 save_cfl_detail("courant");
4059
4060 compute_cfl_force->evaluate();
4061 save_cfl_detail("force");
4062
4063 if (do_nimhd) {
4064 compute_cfl_NIMHD->evaluate();
4065 save_cfl_detail("NIMHD");
4066 }
4067
4068 if (has_psi_field) {
4069 compute_cfl_divB_cleaning->evaluate();
4070 save_cfl_detail("divB_cleaning");
4071 }
4072
4073 if (solver_config.dust_config.has_s_j_field()) {
4074 compute_cfl_dust1_fluid->evaluate();
4075 save_cfl_detail("dust1_fluid");
4076
4077 compute_cfl_dust_drift->evaluate();
4078 save_cfl_detail("dust_drift");
4079 }
4080
4081 if (!show_cfl_detail) {
4082 save_dt_min_to_dt_part();
4083 cfl_detail.push_back({"all SPH", cfl_dt->get_native().compute_rank_min()});
4084 }
4085
4086 auto &sync = scheduler().synchronized_data;
4087 auto &pos = get_sink_pos<Tvec>(sync);
4088 if (!pos.empty()) {
4089 // sink sink CFL
4090
4091 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> G_edge
4092 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("G", "G");
4093 G_edge->data = solver_config.get_constant_G();
4094
4095 std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> sink_sink_cfl
4096 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared(
4097 "sink_sink_cfl", "\\Delta t_{\\rm sink-sink}");
4098
4100 using SinkScalEdge
4102
4103 ComputeCFLSinkSink<Tvec> compute_cfl_sink_sink{};
4104 compute_cfl_sink_sink.set_edges(
4105 G_edge,
4106 C_force_edge,
4107 eta_phi_edge,
4108 sync.template get_edge_ptr<SinkVecEdge>("sink_pos"),
4109 sync.template get_edge_ptr<SinkScalEdge>("sink_mass"),
4110 sync.template get_edge_ptr<SinkVecEdge>("sink_acc_ext"),
4111 sink_sink_cfl);
4112 compute_cfl_sink_sink.evaluate();
4113
4114 cfl_detail.push_back({"sink_sink", sink_sink_cfl->data});
4115 }
4116
4117 Tscal rank_dt = shambase::get_infty<Tscal>();
4118 for (auto &[key, value] : cfl_detail) {
4119 rank_dt = sham::min(rank_dt, value);
4120 }
4121
4122 if (show_cfl_detail) {
4123 for (auto &[key, value] : cfl_detail) {
4124 value = shamalgs::collective::allreduce_min(value);
4125 }
4126
4127 if (shamcomm::world_rank() == 0) {
4128 shambase::table table(2);
4129 table.add_double_rule();
4130 table.add_data({"key", "value"}, shambase::table::center);
4131 table.add_double_rule();
4132 for (auto &[key, value] : cfl_detail) {
4133 table.add_data(
4134 {key, sham::format("{:.2e}", value)}, shambase::table::right);
4135 }
4136 table.add_rule();
4137 logger::info_ln("sph::Model", "CFL detail :", table.render());
4138 }
4139 }
4140
4141 next_cfl = shamalgs::collective::allreduce_min(rank_dt);
4142
4143 // store the last cfl dt in synchronized edges (values are reduced over all ranks)
4145 if (show_cfl_detail) {
4146 for (auto &[key, value] : cfl_detail) {
4148 }
4149 }
4150
4151 if (shamcomm::world_rank() == 0) {
4153 "sph::Model", "cfl dt =", next_cfl, "cfl multiplier :", get_cfl_multipler());
4154 }
4155
4156 // this should not be needed idealy, but we need the pressure on the ghosts and
4157 // we don't want to communicate it as it can be recomputed from the other fields
4158 // hence we copy the soundspeed at the end of the step to a field in the patchdata
4159 if (solver_config.has_field_soundspeed()) {
4160
4161 const u32 isoundspeed = pdl.get_field_idx<Tscal>("soundspeed");
4162
4163 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
4164 sham::DeviceBuffer<Tscal> &buf_cs = pdat.get_field_buf_ref<Tscal>(isoundspeed);
4165 sham::DeviceBuffer<Tscal> &buf_cs_in
4166 = shambase::get_check_ref(storage.soundspeed)
4167 .get_field(cur_p.id_patch)
4168 .get_buf();
4169
4170 sycl::range range_npart{pdat.get_obj_cnt()};
4171
4173
4174 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
4175 sham::EventList depends_list;
4176
4177 auto cs_in = buf_cs_in.get_read_access(depends_list);
4178 auto cs = buf_cs.get_write_access(depends_list);
4179
4180 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
4181 const Tscal pmass = solver_config.gpart_mass;
4182
4183 cgh.parallel_for(
4184 sycl::range<1>{pdat.get_obj_cnt()}, [=](sycl::item<1> item) {
4185 cs[item] = cs_in[item];
4186 });
4187 });
4188
4189 buf_cs_in.complete_event_state(e);
4190 buf_cs.complete_event_state(e);
4191 });
4192 }
4193
4194 } // if (!need_rerun_corrector) {
4195
4196 corrector_iter_cnt++;
4197
4198 if (solver_config.has_field_alphaAV()) {
4199 storage.alpha_av_ghost.reset();
4200 }
4201
4202 if (do_nimhd) {
4203 storage.MagCurrentJ_ghost.reset();
4204 }
4205
4206 } while (need_rerun_corrector);
4207
4208 reset_merge_ghosts_fields();
4209 reset_eos_fields();
4210
4211 // if delta too big jump to compute force
4212
4213 tstep.stop();
4214
4215 for (auto it = timestep_callbacks.rbegin(); it != timestep_callbacks.rend(); ++it) {
4216 if (it->step_end_callback) {
4217 shambase::get_check_ref(it->step_end_callback)();
4218 }
4219 }
4220
4221 f64 delta_mpi_timer = shamcomm::mpi::get_timer("total") - mpi_timer_start;
4223
4225 shamsys::SystemMetrics system_metrics_end = shamsys::get_system_metrics();
4226 shamsys::SystemMetrics system_metrics_delta = system_metrics_end - system_metrics_start;
4227
4228 f64 t_dev_alloc
4229 = (mem_perf_infos_end.time_alloc_device - mem_perf_infos_start.time_alloc_device)
4230 + (mem_perf_infos_end.time_free_device - mem_perf_infos_start.time_free_device);
4231 f64 t_host_alloc = (mem_perf_infos_end.time_alloc_host - mem_perf_infos_start.time_alloc_host)
4232 + (mem_perf_infos_end.time_free_host - mem_perf_infos_start.time_free_host);
4233
4234 u64 rank_count = scheduler().get_rank_count();
4235 f64 rate = f64(rank_count) / tstep.elapsed_sec();
4236
4237 u64 npatch = scheduler().patch_list.local.size();
4238
4239 // number of ghost particles on this rank (sum over patches of with_ghost - without)
4240 std::optional<u64> nobj_ghost = std::nullopt;
4241 if (solver_config.show_ghost_stats) {
4242 u64 cnt_ghost = 0;
4243 shambase::get_check_ref(storage.part_counts_with_ghost)
4244 .indexes.for_each([&](u64 id, u32 cnt_with_ghost) {
4245 cnt_ghost += cnt_with_ghost;
4246 });
4247 shambase::get_check_ref(storage.part_counts).indexes.for_each([&](u64 id, u32 cnt) {
4248 cnt_ghost -= cnt;
4249 });
4250 nobj_ghost = cnt_ghost;
4251 }
4252
4253 // logger::info_ln("SPHSolver", "process rate : ", rate, "particle.s-1");
4254
4255 std::string log_step = report_perf_timestep(
4256 rate,
4257 rank_count,
4258 npatch,
4259 tstep.elapsed_sec(),
4260 delta_mpi_timer,
4261 t_dev_alloc,
4262 t_host_alloc,
4263 mem_perf_infos_end.max_allocated_byte_device,
4264 mem_perf_infos_end.max_allocated_byte_host,
4265 system_metrics_delta,
4266 shamsys::has_reporter(),
4267 nobj_ghost);
4268
4269 if (shamcomm::world_rank() == 0) {
4270 logger::info_ln("sph::Model", log_step);
4272 "sph::Model", "estimated rate :", dt * (3600 / tstep.elapsed_sec()), "(tsim/hr)");
4273 }
4274
4275 solve_logs.register_log(
4276 {t_current, // f64 solver_t;
4277 dt, // f64 solver_dt;
4278 shamcomm::world_rank(), // i32 world_rank;
4279 rank_count, // u64 rank_count;
4280 rate, // f64 rate;
4281 tstep.elapsed_sec(), // f64 elapsed_sec;
4283 system_metrics_delta});
4284
4285 storage.timings_details.reset();
4286
4287 reset_serial_patch_tree();
4288 reset_ghost_handler();
4289
4290 shambase::get_check_ref(storage.part_counts).free_alloc();
4291 shambase::get_check_ref(storage.part_counts_with_ghost).free_alloc();
4292 shambase::get_check_ref(storage.positions_with_ghosts).free_alloc();
4293 shambase::get_check_ref(storage.hpart_with_ghosts).free_alloc();
4294 storage.merged_xyzh.reset();
4295 shambase::get_check_ref(storage.omega).free_alloc();
4296 clear_merged_pos_trees();
4297 clear_ghost_cache();
4298 reset_presteps_rint();
4299 reset_neighbors_cache();
4300
4301 shambase::get_check_ref(storage.neigh_cache).free_alloc();
4302
4303 set_next_dt(next_cfl);
4304 set_time(t_current + dt);
4305
4306 auto get_next_cfl_mult = [&]() {
4307 Tscal cfl_m = get_cfl_multipler();
4308 Tscal stiff = solver_config.cfl_config.cfl_multiplier_stiffness;
4309
4310 return (cfl_m * stiff + 1.) / (stiff + 1.);
4311 };
4312
4313 set_cfl_multipler(get_next_cfl_mult());
4314
4315 TimestepLog log;
4316 log.rank = shamcomm::world_rank();
4317 log.rate = rate;
4318 log.npart = rank_count;
4319 log.tcompute = tstep.elapsed_sec();
4320
4321 return log;
4322}
4323
4324using namespace shammath;
4325
4329
Solvergraph node selecting the ids of the particles sent through each ghost interface.
Host-side CFL condition from the pairwise potential between sink particles.
Compute the gravitational wave quadrupole. Based on Toscani et. al. 2021.
A module to compute and display statistics on neighbor counts for SPH particles.
Defines the CopyPatchDataFieldFromLayer class for copying fields between patch data layers.
Defines the DistributedBuffers class for managing distributed device buffers in a solver graph.
Solvergraph nodes finding the SPH ghost interfaces, one node per boundary condition.
Host-side (std::vector) forward Euler integration node with two derivative contributions.
Host-side (std::vector) forward Euler integration node.
Implements a forward Euler integration step as a solver graph node.
Implements a forward Euler integration step as a solver graph node.
Defines the GetFieldRefFromLayer class for extracting field references from patch data layers.
Defines the GetObjCntFromLayer class for extracting object counts from patch data layers.
Declares the GetParticlesOutsideSphere module for removing particles.
shambase::DistributedData< PatchDataFieldRef< T > > DDPatchDataFieldRef
Alias for a DistributedData of PatchDataFieldRefs.
Declares the IterateSmoothingLengthDensityBisectingNR module for iterating smoothing length based on ...
Declares the IterateSmoothingLengthDensityNeighLim module for iterating smoothing length based on the...
Declares the IterateSmoothingLengthDensity module for iterating smoothing length based on the SPH den...
Declares the KillParticles module for removing particles.
Declares the LoopSmoothingLengthIter module for looping over the smoothing length iteration until con...
Field variant object to instanciate a variant on the patch types.
Header file describing a Node Instance.
sycl::queue & get_compute_queue(u32 id=0)
Node that maps a read-only input edge into a read-write output edge.
Node that applies a custom function to modify connected edges.
Meta node that evaluates an optional then-node when a bool edge is true, and an optional else-node wh...
Defines the PatchDataLayerRefs class for managing distributed references to patch data layers.
MPI scheduler.
Header file for the patch struct and related function.
Solvergraph edge holding a field defined on the nodes of the serial patch tree.
Solver graph node overwriting a field with a constant value where a mask is set.
Accrete flagged SPH particles onto sinks (mass, CoM, spin, etc.).
Remove SPH particles flagged for sink accretion from patch data.
Flag SPH particles inside sink accretion radii into an accretion table.
Host-side pairwise gravitational self-interaction between sink particles.
Declare a class to register and retrieve nodes and edges from a unique container.
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
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
Definition assert.hpp:67
Host-side (N^2) sink-sink CFL condition.
The MPI scheduler.
SynchronizedData synchronized_data
data that is synchroneous across all ranks
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
DeviceQueue & get_queue() const
Gets the DeviceQueue associated with the held allocation.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
void fill(T value, std::array< size_t, 2 > idx_range)
Fill a subpart of the buffer with a given value.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
size_t get_size() const
Gets the number of elements in the buffer.
const T * get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const
Get a read-only pointer to the buffer's data.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
void add_event(sycl::event e)
Add an event to the list of events.
Definition EventList.hpp:88
Container for objects shared between two distributed data elements.
void for_each(std::function< void(u64, u64, T &)> &&f)
Apply a function to all stored objects.
Represents a collection of objects distributed across patches identified by a u64 id.
iterator add_obj(u64 id, T &&obj)
Adds a new object to the collection.
DistributedData< Tmap > map(std::function< Tmap(u64, T &)> map_func)
Apply a function to all objects in the collection and return a new collection containing the results.
T & get(u64 id)
Returns a reference to an object in the collection.
Class Timer measures the time elapsed since the timer was started.
Definition Timer.hpp:36
f64 elapsed_sec() const
Converts the stored nanosecond time to a floating point representation in seconds.
Definition Timer.hpp:88
void start()
Starts the timer.
Definition Timer.hpp:51
void stop()
Stops the timer and stores the elapsed time in nanoseconds.
Definition Timer.hpp:65
Vector class based on std::array storage and mdspan.
Definition matrix.hpp:98
The shamrock SPH model.
Definition Solver.hpp:152
void reset_presteps_rint()
Resets tree radius interval field.
Definition Solver.cpp:2260
static constexpr const char * cfl_dt_detail_edge_prefix
Prefix of the synchronized edges holding the last CFL dt per CFL type.
Definition Solver.hpp:212
void reset_merge_ghosts_fields()
Resets merged ghost field data.
Definition Solver.cpp:2542
void update_sync_load_values()
Updates load balancing values and synchronizes patch ownership.
Definition Solver.cpp:2898
bool apply_corrector(Tscal dt, u64 Npart_all)
Definition Solver.cpp:2893
void merge_position_ghost()
Merges ghost particle positions from neighboring patches.
Definition Solver.cpp:1868
void reset_eos_fields()
Frees memory allocated for EOS fields.
Definition Solver.cpp:2625
void set_cfl_dt_edge_value(const std::string &name, Tscal value)
Store a CFL dt value in its synchronized edge.
Definition Solver.hpp:225
void build_ghost_cache(Tscal time_val)
Generates the ghost handler and builds the ghost particle interface cache for inter-patch communicati...
Definition Solver.cpp:1634
void prepare_corrector()
Saves old derivative fields for predictor-corrector integration.
Definition Solver.cpp:2631
void update_artificial_viscosity(Tscal dt)
Updates artificial viscosity coefficients for shock capturing.
Definition Solver.cpp:2551
TimestepLog evolve_once()
Performs one complete SPH timestep evolution.
Definition Solver.cpp:2905
void update_j()
Updates the magnetic current field (for NIMHD).
Definition Solver.cpp:2558
void vtk_do_dump(std::string filename, bool add_patch_world_id)
Writes VTK dump file for visualization.
Definition Solver.cpp:1411
void update_derivs(Tscal dt_hydro)
Updates time derivatives and applies external forces.
Definition Solver.cpp:2727
void build_merged_pos_trees()
Builds spatial BVH trees for merged positions including ghosts.
Definition Solver.cpp:1911
void clear_merged_pos_trees()
Clears merged position trees to free memory.
Definition Solver.cpp:1916
void init_solver_graph()
Initializes the solver graph for computation pipeline.
Definition Solver.cpp:273
void sph_prestep(Tscal time_val, Tscal dt)
Performs pre-step operations for SPH timestep.
Definition Solver.cpp:1922
void compute_presteps_rint()
Computes maximum smoothing length in tree nodes for neighbor search.
Definition Solver.cpp:2223
void compute_eos_fields()
Computes equation of state fields (pressure, sound speed).
Definition Solver.cpp:2619
void apply_position_boundary(Tscal time_val)
Applies position-based boundary conditions.
Definition Solver.cpp:1588
void reset_neighbors_cache()
Resets neighbor cache.
Definition Solver.cpp:2295
static constexpr const char * cfl_dt_edge_name
Name of the synchronized edge holding the last global CFL dt.
Definition Solver.hpp:210
void communicate_merge_ghosts_fields()
Communicates and merges ghost particle fields across processes.
Definition Solver.cpp:2300
void clear_ghost_cache()
Clears ghost particle cache to free memory.
Definition Solver.cpp:1862
void init_ghost_layout()
Initializes data layout for ghost particle fields.
Definition Solver.cpp:2208
void start_neighbors_cache()
Builds neighbor particle cache for SPH calculations.
Definition Solver.cpp:2265
Module for constructing spatial tree structures for SPH neighbor searches.
void build_merged_pos_trees()
Builds compressed leaf BVH trees for merged particle positions including ghosts.
Module for computing equation of state quantities.
void compute_eos()
Computes pressure and sound speed from equation of state.
Module for checking conservation of physical quantities.
void check_conservation()
Verifies conservation of mass, momentum, and energy.
void add_ext_forces()
add external forces to the particle acceleration, note that forces dependant on velocity shlould be a...
void compute_ext_forces_indep_v()
is ran once per timestep, it computes the forces that are independant of velocity
Module for reordering particles to improve cache locality.
void reorder_particles()
Reorders particles by Morton code for improved memory access patterns.
Overwrite every variable of a field with a constant where the per-object mask is non-zero.
Host-side pairwise (N^2) gravitational self-interaction between sink particles.
Module for writing VTK format output files.
Definition VTKDump.hpp:33
void do_dump(std::string filename, bool add_patch_world_id)
Writes particle data to VTK file for visualization.
Definition VTKDump.cpp:37
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.
ComputeField< T > make_compute_field(std::string new_name, u32 nvar)
create a compute field and init it to zeros
ComputeField< T > save_field(u32 field_idx, std::string new_name)
save a field in patchdata to a compute 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
Store the information related to the size of the simulation box to convert patch integer coordinates ...
Definition SimBox.hpp:36
std::tuple< T, T > get_bounding_box() const
Get the stored bounding box of the domain.
Definition SimBox.hpp:248
PatchCoordTransform< T > get_patch_transform() const
Get a PatchCoordTransform object that describes the conversion between patch coordinates and domain c...
Definition SimBox.hpp:286
Interface for a solver graph edge representing a field as spans.
Forward Euler integration of a host-side (std::vector) field with two derivative contributions.
Forward Euler integration of a host-side (std::vector) field.
PatchDataField< T > & get_field(u64 id) const
Get the underlying PatchDataField at the given id.
void evaluate()
Evaluate the node.
Definition INode.hpp:156
A node that simply frees the allocation of the connected node.
A node that maps an input edge into an output edge.
A node that applies a custom function to modify connected edges.
void set_edges(std::shared_ptr< IEdge > to_set)
Set the edges of the node.
Conditional meta node: if condition is true, evaluate then_node when one was provided; otherwise eval...
virtual void free_alloc() override
Free allocated memory.
A graph container for managing solver nodes and edges with type-safe access.
std::shared_ptr< INode > & get_node_ptr_base(const std::string &name)
Retrieve a node by name as a shared pointer to the base interface.
std::shared_ptr< T > get_edge_ptr(const std::string &name)
Get a typed shared pointer to an edge by name.
std::shared_ptr< T > register_edge(const std::string &name, T &&edge)
Register an edge with automatic type deduction and shared pointer creation.
std::shared_ptr< T > register_node(const std::string &name, T &&node)
Register a node with automatic type deduction and shared pointer creation.
INode & get_node_ref_base(const std::string &name)
Get a reference to a node by name through the base interface.
A Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
A data structure representing a Karras Radix Tree Field.
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...
MemPerfInfos get_mem_perf_info()
Retrieve the memory performance information.
This file contains the declaration of the memory handling and its methods.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
std::vector< T > buf_to_vec(sycl::buffer< T > &buf, u32 len)
Convert a sycl::buffer to a std::vector.
Definition memory.cpp:34
namespace for basic c++ utilities
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 math utility
Definition AABB.hpp:26
namespace for the sph model
bool has_sinks(shamrock::solvergraph::SolverGraphSerializable &sync)
Check whether any sinks are present by inspecting sink_pos only.
std::vector< Tvec > & get_sink_pos(shamrock::solvergraph::SolverGraphSerializable &sync)
Named SoA getters (edges must already exist; registered by Solver::init_solver_graph)....
@ SingleStage
Single tree traversal per particle.
@ TwoStage
Two stage neighbours search (see shamrock paper).
namespace for the main framework
Definition __init__.py:1
void info(std::string module_name, Types... var2)
Prints a log message with multiple arguments.
Definition logs.hpp:132
void raw_ln(Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:89
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
void err_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.
file containing formulas for sph forces
sph kernels
shambase::details::NamedBasicStackEntry NamedStackEntry
Alias for shambase::details::NamedBasicStackEntry.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
f64 get_wtime()
Returns the current wall clock time in seconds.
Structure to store the performance informations about memory allocation and deallocation.
f64 time_alloc_host
Time spent allocating memory on the host.
size_t max_allocated_byte_host
max bytes allocated on the host
f64 time_free_device
Time spent deallocating memory on the device.
size_t max_allocated_byte_device
max bytes allocated on the device
f64 time_alloc_device
Time spent allocating memory on the device.
f64 time_free_host
Time spent deallocating memory on the host.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Build an overload set out of several callables, for use with std::visit.
Axis-Aligned bounding box.
Definition AABB.hpp:99
Free boundary condition.
Definition BCConfig.hpp:52
Periodic boundary condition.
Definition BCConfig.hpp:65
Shearing periodic boundary condition.
Definition BCConfig.hpp:73
Boundary conditions configuration.
Definition BCConfig.hpp:40
A class to represent a single block of data in a Phantom dump.
u64 get_ref_f32(std::string s)
Gets the index of a block of type f32 with the given name.
u64 get_ref_fort_real(std::string s)
Gets the index of a block of type fort_real with the given name.
i64 tot_count
The total number of values in the block.
std::vector< PhantomDumpBlockArray< fort_real > > blocks_fort_real
The blocks of values of type fort_real.
std::vector< PhantomDumpBlockArray< f32 > > blocks_f32
The blocks of values of type f32.
Class representing a Phantom dump file.
void override_magic_number()
Overrides the magic numbers used in the PhantomDump struct.
BCConfig< Tvec > BCConfig
Configuration of the boundary conditions.
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 load_value
if synchronized contain the load value of the patch
Definition Patch.hpp:88
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
Functions related to the MPI communicator.
f64 get_timer(std::string timername)
get a timer value
Definition wrapper.cpp:46