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
25
28#include "shambase/memory.hpp"
30#include "shambase/string.hpp"
31#include "shambase/time.hpp"
36#include "shambackends/math.hpp"
37#include "shamcomm/logs.hpp"
78#include <memory>
79#include <stdexcept>
80#include <vector>
81
82template<class Tvec, template<class> class Kern>
83void shammodels::gsph::Solver<Tvec, Kern>::init_solver_graph() {
84
86 // synchronized edges
88
89 auto &sync_data = scheduler().synchronized_data;
90
91 // register a synchronized edge only if missing, as it may have been restored from a dump
92 auto ensure_sync_edge
93 = [&](const std::string &name, const std::string &tex_symbol, auto init_value) {
94 using T = decltype(init_value);
95 if (!sync_data.has_edge(name)) {
96 auto edge = sync_data.register_edge(
98 edge->data = std::move(init_value);
99 }
100 };
101
102 ensure_sync_edge("time", "t", Tscal{0});
103 ensure_sync_edge("dt", "dt", Tscal{0});
104
106 // main solver graph
108
109 storage.part_counts = std::make_shared<shamrock::solvergraph::Indexes<u32>>(
110 edges::part_counts, "N_{\\rm part}");
111
112 storage.part_counts_with_ghost = std::make_shared<shamrock::solvergraph::Indexes<u32>>(
113 edges::part_counts_with_ghost, "N_{\\rm part, with ghost}");
114
115 storage.patch_rank_owner = std::make_shared<shamrock::solvergraph::RankGetter>(
116 [&](u64 patch_id) -> u32 {
117 return scheduler().get_patch_rank_owner(patch_id);
118 },
119 "patch_rank_owner",
120 "rank");
121
122 // Merged ghost spans
123 storage.positions_with_ghosts = std::make_shared<shamrock::solvergraph::FieldRefs<Tvec>>(
124 edges::positions_with_ghosts, "\\mathbf{r}");
125 storage.hpart_with_ghosts
126 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>(edges::hpart_with_ghosts, "h");
127
128 storage.neigh_cache
129 = std::make_shared<shammodels::sph::solvergraph::NeighCache>(edges::neigh_cache, "neigh");
130
131 // Register ghost handler in solvergraph for explicit data dependency tracking
132 storage.ghost_handler = storage.solver_graph.register_edge(
133 "ghost_handler", solvergraph::GhostHandlerEdge<Tvec>("ghost_handler", "\\mathcal{G}"));
134
135 storage.omega = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "omega", "\\Omega");
136 storage.density = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "density", "\\rho");
137 storage.pressure = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "pressure", "P");
138 storage.soundspeed
139 = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "soundspeed", "c_s");
140
141 // Initialize gradient fields for MUSCL reconstruction
142 // These are only used when reconstruct_config.is_muscl() == true
143 storage.grad_density
144 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad_density", "\\nabla\\rho");
145 storage.grad_pressure
146 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad_pressure", "\\nabla P");
147 storage.grad_vx
148 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad_vx", "\\nabla v_x");
149 storage.grad_vy
150 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad_vy", "\\nabla v_y");
151 storage.grad_vz
152 = std::make_shared<shamrock::solvergraph::Field<Tvec>>(1, "grad_vz", "\\nabla v_z");
153}
154
155template<class Tvec, template<class> class Kern>
156void shammodels::gsph::Solver<Tvec, Kern>::vtk_do_dump(
157 std::string filename, bool add_patch_world_id) {
158
159 modules::VTKDump<Tvec, Kern>(context, solver_config).do_dump(filename, add_patch_world_id);
160}
161
162template<class Tvec, template<class> class Kern>
163void shammodels::gsph::Solver<Tvec, Kern>::gen_serial_patch_tree() {
164 StackEntry stack_loc{};
165
166 SerialPatchTree<Tvec> _sptree = SerialPatchTree<Tvec>::build(scheduler());
167 _sptree.attach_buf();
168 storage.serial_patch_tree.set(std::move(_sptree));
169}
170
171template<class Tvec, template<class> class Kern>
173 StackEntry stack_loc{};
174
175 using namespace shamrock::patch;
176 using namespace shamrock::solvergraph;
177
178 using InterfaceBuildInfos = typename GhostHandle::InterfaceBuildInfos;
179
180 using BCConfig = typename Config::BCConfig;
181 using BCFree = typename BCConfig::Free;
182 using BCPeriodic = typename BCConfig::Periodic;
183 using BCShearingPeriodic = typename BCConfig::ShearingPeriodic;
184
185 const auto &bc_config = solver_config.boundary_config.config;
186
187 // ----------------------------------------------------------------------------------------
188 // Generate the ghost handler
189 // ----------------------------------------------------------------------------------------
190
191 shambase::get_check_ref(storage.ghost_handler)
192 .set(GhostHandle{scheduler(), storage.patch_rank_owner, storage.xyzh_ghost_layout});
193
194 // ----------------------------------------------------------------------------------------
195 // Build the ghost cache
196 // ----------------------------------------------------------------------------------------
197
198 PatchScheduler &sched = scheduler();
199 GhostHandle &ghost_handle = shambase::get_check_ref(storage.ghost_handler).get();
200 SerialPatchTree<Tvec> &sptree = storage.serial_patch_tree.get();
201
202 // Same widening as compute_presteps_rint()/start_neighbors_cache(): with
203 // InutsukaV2 the kernel support is sqrt(2)*h, so the patch/rank ghost
204 // interface radius must widen too, or particles near a patch boundary
205 // could be missing valid neighbors from the adjacent patch.
206 Tscal h_evol_max = solver_config.htol_up_coarse_cycle;
207 if (solver_config.is_force_inutsuka_v2()) {
208 h_evol_max *= shambase::constants::sqrt_2<Tscal>;
209 }
210
211 const u32 ihpart = sched.pdl_old().template get_field_idx<Tscal>("hpart");
212
213 PatchField<Tscal> interactR_patch = sched.map_owned_to_patch_field_simple<Tscal>(
214 [&](const Patch p, PatchDataLayer &pdat) -> Tscal {
215 if (!pdat.is_empty()) {
216 return pdat.get_field<Tscal>(ihpart).compute_max() * h_evol_max * Rkern;
217 } else {
218 return shambase::VectorProperties<Tscal>::get_min();
219 }
220 });
221
222 PatchtreeField<Tscal> interactR_mpi_tree = sptree.make_patch_tree_field(
223 sched,
225 interactR_patch,
226 [](Tscal h0, Tscal h1, Tscal h2, Tscal h3, Tscal h4, Tscal h5, Tscal h6, Tscal h7) {
227 return sham::max_8points(h0, h1, h2, h3, h4, h5, h6, h7);
228 });
229
230 // ----------------------------------------------------------------------------------------
231 // temporary wrapper to slowly migrate to the new solvergraph
232 SimulationBoxInfo &sim_box = sched.get_sim_box();
233 PatchCoordTransform<Tvec> patch_coord_transf = sim_box.get_patch_transform<Tvec>();
234 auto [bmin, bmax] = sim_box.get_bounding_box<Tvec>();
235
236 auto sim_box_edge = std::make_shared<ScalarEdge<shammath::AABB<Tvec>>>("", "");
237 sim_box_edge->value = shammath::AABB<Tvec>(bmin, bmax);
238
239 auto patch_tree_edge = std::make_shared<SerialPatchTreeRefEdge<Tvec>>("", "");
240 patch_tree_edge->patch_tree = std::ref(sptree);
241
242 auto interact_radius_tree = std::make_shared<PatchtreeFieldEdge<Tscal>>("", "");
243 interact_radius_tree->patchtree_field = std::move(interactR_mpi_tree);
244
245 // interactR_patch is not used afterwards, so move it (O(1)) instead of copying over all
246 // global patches
247 auto interact_radius = std::make_shared<ScalarsEdge<Tscal>>("", "");
248 interact_radius->values = std::move(interactR_patch.field_all);
249
250 auto local_patch_boxes = std::make_shared<ScalarsEdge<shammath::CoordRange<Tvec>>>("", "");
251 sched.for_each_local_patch([&](const Patch &p) {
252 local_patch_boxes->values.add_obj(p.id_patch, patch_coord_transf.to_obj_coord(p));
253 });
254
255 auto interface_infos = std::make_shared<DDSharedScalar<InterfaceBuildInfos>>("", "");
256
257 std::visit(
259 [&](const BCFree &) {
261 node.set_edges(
262 sim_box_edge,
263 patch_tree_edge,
264 interact_radius_tree,
265 interact_radius,
266 local_patch_boxes,
267 interface_infos);
268 node.evaluate();
269 },
270 [&](const BCPeriodic &) {
272 node.set_edges(
273 sim_box_edge,
274 patch_tree_edge,
275 interact_radius_tree,
276 interact_radius,
277 local_patch_boxes,
278 interface_infos);
279 node.evaluate();
280 },
281 [&](const BCShearingPeriodic &cfg) {
282 auto shear_value = IDataEdge<Tscal>::make_shared("", "");
283 shear_value->data = cfg.shear_speed * time_val;
284
286 cfg.shear_base, cfg.shear_dir, cfg.shear_speed);
287 node.set_edges(
288 sim_box_edge,
289 patch_tree_edge,
290 interact_radius_tree,
291 interact_radius,
292 local_patch_boxes,
293 shear_value,
294 interface_infos);
295 node.evaluate();
296 },
297 },
298 bc_config);
299
300 // ----------------------------------------------------------------------------------------
301
302 storage.ghost_patch_cache.set(
303 ghost_handle.gen_id_table_interfaces(std::move(interface_infos->values)));
304}
305
306template<class Tvec, template<class> class Kern>
307void shammodels::gsph::Solver<Tvec, Kern>::clear_ghost_cache() {
308 StackEntry stack_loc{};
309 storage.ghost_patch_cache.reset();
310}
311
312template<class Tvec, template<class> class Kern>
313void shammodels::gsph::Solver<Tvec, Kern>::merge_position_ghost() {
314 StackEntry stack_loc{};
315
316 storage.merged_xyzh.set(
317 shambase::get_check_ref(storage.ghost_handler)
318 .get()
319 .build_comm_merge_positions(storage.ghost_patch_cache.get()));
320
321 // Get field indices from xyzh_ghost_layout
322 const u32 ixyz_ghost
323 = storage.xyzh_ghost_layout->template get_field_idx<Tvec>(gsph::names::common::xyz);
324 const u32 ihpart_ghost
325 = storage.xyzh_ghost_layout->template get_field_idx<Tscal>(gsph::names::common::hpart);
326
327 // Set element counts
328 shambase::get_check_ref(storage.part_counts).indexes
329 = storage.merged_xyzh.get().template map<u32>(
330 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
331 return scheduler().patch_data.get_pdat(id).get_obj_cnt();
332 });
333
334 // Set element counts with ghost
335 shambase::get_check_ref(storage.part_counts_with_ghost).indexes
336 = storage.merged_xyzh.get().template map<u32>(
337 [&](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
338 return mpdat.get_obj_cnt();
339 });
340
341 // Attach spans to block coords
342 shambase::get_check_ref(storage.positions_with_ghosts)
343 .set_refs(
344 storage.merged_xyzh.get().template map<std::reference_wrapper<PatchDataField<Tvec>>>(
345 [&, ixyz_ghost](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
346 return std::ref(mpdat.get_field<Tvec>(ixyz_ghost));
347 }));
348
349 shambase::get_check_ref(storage.hpart_with_ghosts)
350 .set_refs(
351 storage.merged_xyzh.get().template map<std::reference_wrapper<PatchDataField<Tscal>>>(
352 [&, ihpart_ghost](u64 id, shamrock::patch::PatchDataLayer &mpdat) {
353 return std::ref(mpdat.get_field<Tscal>(ihpart_ghost));
354 }));
355}
356
357template<class Tvec, template<class> class Kern>
358void shammodels::gsph::Solver<Tvec, Kern>::build_merged_pos_trees() {
359 StackEntry stack_loc{};
360
361 auto &merged_xyzh = storage.merged_xyzh.get();
362 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
363
364 // Get field index from xyzh_ghost_layout
365 const u32 ixyz_ghost
366 = storage.xyzh_ghost_layout->template get_field_idx<Tvec>(gsph::names::common::xyz);
367
368 shambase::DistributedData<RTree> trees = merged_xyzh.template map<RTree>(
369 [&, ixyz_ghost](u64 id, shamrock::patch::PatchDataLayer &merged) {
370 PatchDataField<Tvec> &pos = merged.template get_field<Tvec>(ixyz_ghost);
371 Tvec bmax = pos.compute_max();
372 Tvec bmin = pos.compute_min();
373
374 shammath::AABB<Tvec> aabb(bmin, bmax);
375
376 Tscal infty = std::numeric_limits<Tscal>::infinity();
377
378 // Ensure that no particle is on the boundary of the AABB
379 aabb.lower[0] = std::nextafter(aabb.lower[0], -infty);
380 aabb.lower[1] = std::nextafter(aabb.lower[1], -infty);
381 aabb.lower[2] = std::nextafter(aabb.lower[2], -infty);
382 aabb.upper[0] = std::nextafter(aabb.upper[0], infty);
383 aabb.upper[1] = std::nextafter(aabb.upper[1], infty);
384 aabb.upper[2] = std::nextafter(aabb.upper[2], infty);
385
386 auto bvh = RTree::make_empty(dev_sched);
387 bvh.rebuild_from_positions(
388 pos.get_buf(), pos.get_obj_cnt(), aabb, solver_config.tree_reduction_level);
389
390 return bvh;
391 });
392
393 storage.merged_pos_trees.set(std::move(trees));
394}
395
396template<class Tvec, template<class> class Kern>
397void shammodels::gsph::Solver<Tvec, Kern>::clear_merged_pos_trees() {
398 StackEntry stack_loc{};
399 storage.merged_pos_trees.reset();
400}
401
402template<class Tvec, template<class> class Kern>
403void shammodels::gsph::Solver<Tvec, Kern>::compute_presteps_rint() {
404 StackEntry stack_loc{};
405
406 auto &xyzh_merged = storage.merged_xyzh.get();
407 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
408
409 // The Inutsuka V2 force formulation evaluates the kernel gradient at an
410 // effective smoothing length sqrt(2)*h (Inutsuka 2002). This tree-node
411 // interaction-range field is used to prune tree traversal in
412 // start_neighbors_cache(), so it must be widened by the same factor, or
413 // whole subtrees containing valid sqrt(2)*h-range neighbors get pruned
414 // before the leaf-level search even runs.
415 Tscal htol = solver_config.htol_up_coarse_cycle;
416 if (solver_config.is_force_inutsuka_v2()) {
417 htol *= shambase::constants::sqrt_2<Tscal>;
418 }
419
420 storage.rtree_rint_field.set(
421 storage.merged_pos_trees.get().template map<shamtree::KarrasRadixTreeField<Tscal>>(
422 [&](u64 id, RTree &rtree) -> shamtree::KarrasRadixTreeField<Tscal> {
423 shamrock::patch::PatchDataLayer &tmp = xyzh_merged.get(id);
424 auto &buf = tmp.get_field_buf_ref<Tscal>(1);
425 auto buf_int = shamtree::new_empty_karras_radix_tree_field<Tscal>();
426
427 auto ret = shamtree::compute_tree_field_max_field<Tscal>(
428 rtree.structure,
429 rtree.reduced_morton_set.get_leaf_cell_iterator(),
430 std::move(buf_int),
431 buf);
432
433 // Increase the size by tolerance factor
434 sham::kernel_call(
435 dev_sched->get_queue(),
436 sham::MultiRef{},
437 sham::MultiRef{ret.buf_field},
438 ret.buf_field.get_size(),
439 [htol](u32 i, Tscal *h_tree) {
440 h_tree[i] *= htol;
441 });
442
443 return std::move(ret);
444 }));
445}
446
447template<class Tvec, template<class> class Kern>
448void shammodels::gsph::Solver<Tvec, Kern>::reset_presteps_rint() {
449 storage.rtree_rint_field.reset();
450}
451
452template<class Tvec, template<class> class Kern>
453void shammodels::gsph::Solver<Tvec, Kern>::start_neighbors_cache() {
454 StackEntry stack_loc{};
455
456 shambase::Timer time_neigh;
457 time_neigh.start();
458
459 Tscal h_tolerance = solver_config.htol_up_coarse_cycle;
460
461 // The Inutsuka V2 force formulation evaluates the kernel gradient at an
462 // effective smoothing length sqrt(2)*h (Inutsuka 2002), so its support radius
463 // is sqrt(2) times larger than the standard h*Rkern cutoff used below. Widen
464 // the cached search radius accordingly, or pairs in (h*Rkern, sqrt(2)*h*Rkern)
465 // would silently be missing from the cache for that formulation.
466 if (solver_config.is_force_inutsuka_v2()) {
467 h_tolerance *= shambase::constants::sqrt_2<Tscal>;
468 }
469
470 // Build neighbor cache using tree traversal - same approach as SPH module
471 auto build_neigh_cache = [&](u64 patch_id) -> shamrock::tree::ObjectCache {
472 auto &mfield = storage.merged_xyzh.get().get(patch_id);
473
474 sham::DeviceBuffer<Tvec> &buf_xyz = mfield.template get_field_buf_ref<Tvec>(0);
475 sham::DeviceBuffer<Tscal> &buf_hpart = mfield.template get_field_buf_ref<Tscal>(1);
476
477 sham::DeviceBuffer<Tscal> &tree_field_rint
478 = storage.rtree_rint_field.get().get(patch_id).buf_field;
479
480 RTree &tree = storage.merged_pos_trees.get().get(patch_id);
481 auto obj_it = tree.get_object_iterator();
482
483 u32 obj_cnt = shambase::get_check_ref(storage.part_counts).indexes.get(patch_id);
484
485 constexpr Tscal Rker2 = Kernel::Rkern * Kernel::Rkern;
486
487 // Allocate neighbor count buffer
488 sham::DeviceBuffer<u32> neigh_count(
489 obj_cnt, shamsys::instance::get_compute_scheduler_ptr());
490
491 shamsys::instance::get_compute_queue().wait_and_throw();
492
493 // First pass: count neighbors
494 {
495 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
496 sham::EventList depends_list;
497
498 auto xyz = buf_xyz.get_read_access(depends_list);
499 auto hpart = buf_hpart.get_read_access(depends_list);
500 auto rint_tree = tree_field_rint.get_read_access(depends_list);
501 auto neigh_cnt = neigh_count.get_write_access(depends_list);
502 auto particle_looper = obj_it.get_read_access(depends_list);
503
504 auto e = q.submit(depends_list, [&, h_tolerance](sycl::handler &cgh) {
505 shambase::parallel_for(cgh, obj_cnt, "gsph_count_neighbors", [=](u64 gid) {
506 u32 id_a = (u32) gid;
507
508 Tscal rint_a = hpart[id_a] * h_tolerance;
509 Tvec xyz_a = xyz[id_a];
510
511 Tvec inter_box_a_min = xyz_a - rint_a * Kernel::Rkern;
512 Tvec inter_box_a_max = xyz_a + rint_a * Kernel::Rkern;
513
514 u32 cnt = 0;
515
516 particle_looper.rtree_for(
517 [&](u32 node_id, shammath::AABB<Tvec> node_aabb) -> bool {
518 Tscal int_r_max_cell = rint_tree[node_id] * Kernel::Rkern;
519
520 using namespace walker::interaction_crit;
521
522 return sph_radix_cell_crit(
523 xyz_a,
524 inter_box_a_min,
525 inter_box_a_max,
526 node_aabb.lower,
527 node_aabb.upper,
528 int_r_max_cell);
529 },
530 [&](u32 id_b) {
531 Tvec dr = xyz_a - xyz[id_b];
532 Tscal rab2 = sycl::dot(dr, dr);
533 Tscal rint_b = hpart[id_b] * h_tolerance;
534
535 bool no_interact
536 = rab2 > rint_a * rint_a * Rker2 && rab2 > rint_b * rint_b * Rker2;
537
538 cnt += (no_interact) ? 0 : 1;
539 });
540
541 neigh_cnt[id_a] = cnt;
542 });
543 });
544
545 buf_xyz.complete_event_state(e);
546 buf_hpart.complete_event_state(e);
547 neigh_count.complete_event_state(e);
548 tree_field_rint.complete_event_state(e);
549 obj_it.complete_event_state(e);
550 }
551
552 // Use tree::prepare_object_cache to do prefix sum and allocate buffers
554 = shamrock::tree::prepare_object_cache(std::move(neigh_count), obj_cnt);
555
556 // Second pass: fill neighbor indices
557 {
558 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
559 sham::EventList depends_list;
560
561 auto xyz = buf_xyz.get_read_access(depends_list);
562 auto hpart = buf_hpart.get_read_access(depends_list);
563 auto rint_tree = tree_field_rint.get_read_access(depends_list);
564 auto scanned_neigh_cnt = pcache.scanned_cnt.get_read_access(depends_list);
565 auto neigh = pcache.index_neigh_map.get_write_access(depends_list);
566 auto particle_looper = obj_it.get_read_access(depends_list);
567
568 auto e = q.submit(depends_list, [&, h_tolerance](sycl::handler &cgh) {
569 shambase::parallel_for(cgh, obj_cnt, "gsph_fill_neighbors", [=](u64 gid) {
570 u32 id_a = (u32) gid;
571
572 Tscal rint_a = hpart[id_a] * h_tolerance;
573 Tvec xyz_a = xyz[id_a];
574
575 Tvec inter_box_a_min = xyz_a - rint_a * Kernel::Rkern;
576 Tvec inter_box_a_max = xyz_a + rint_a * Kernel::Rkern;
577
578 u32 write_idx = scanned_neigh_cnt[id_a];
579
580 particle_looper.rtree_for(
581 [&](u32 node_id, shammath::AABB<Tvec> node_aabb) -> bool {
582 Tscal int_r_max_cell = rint_tree[node_id] * Kernel::Rkern;
583
584 using namespace walker::interaction_crit;
585
586 return sph_radix_cell_crit(
587 xyz_a,
588 inter_box_a_min,
589 inter_box_a_max,
590 node_aabb.lower,
591 node_aabb.upper,
592 int_r_max_cell);
593 },
594 [&](u32 id_b) {
595 Tvec dr = xyz_a - xyz[id_b];
596 Tscal rab2 = sycl::dot(dr, dr);
597 Tscal rint_b = hpart[id_b] * h_tolerance;
598
599 bool no_interact
600 = rab2 > rint_a * rint_a * Rker2 && rab2 > rint_b * rint_b * Rker2;
601
602 if (!no_interact) {
603 neigh[write_idx++] = id_b;
604 }
605 });
606 });
607 });
608
609 buf_xyz.complete_event_state(e);
610 buf_hpart.complete_event_state(e);
611 tree_field_rint.complete_event_state(e);
612 pcache.scanned_cnt.complete_event_state(e);
613 pcache.index_neigh_map.complete_event_state(e);
614 obj_it.complete_event_state(e);
615 }
616
617 return pcache;
618 };
619
620 shambase::get_check_ref(storage.neigh_cache).free_alloc();
621
622 using namespace shamrock::patch;
623 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
624 auto &ncache = shambase::get_check_ref(storage.neigh_cache);
625 ncache.neigh_cache.add_obj(cur_p.id_patch, build_neigh_cache(cur_p.id_patch));
626 });
627
628 time_neigh.stop();
629 storage.timings_details.neighbors += time_neigh.elapsed_sec();
630}
631
632template<class Tvec, template<class> class Kern>
633void shammodels::gsph::Solver<Tvec, Kern>::reset_neighbors_cache() {
634 storage.neigh_cache->neigh_cache = {};
635}
636
637template<class Tvec, template<class> class Kern>
639 StackEntry stack_loc{};
640
641 using namespace shamrock;
642 using namespace shamrock::patch;
643
644 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
645 const Tscal pmass = solver_config.gpart_mass;
646 static constexpr Tscal Rkern = Kernel::Rkern;
647
648 if (pmass == 0) {
650 "invalid gpart_mass 0 in compute_density, this configuration can not converge.");
651 }
652
653 shamrock::solvergraph::Field<Tscal> &density_field = shambase::get_check_ref(storage.density);
654
655 // Density is only needed for local particles here; it gets propagated to
656 // ghosts afterwards by communicate_merge_ghosts_fields().
657 shambase::DistributedData<u32> &counts = shambase::get_check_ref(storage.part_counts).indexes;
658 density_field.ensure_sizes(counts);
659
660 PatchDataLayerLayout &pdl = scheduler().pdl_old();
661 const u32 ihpart = pdl.get_field_idx<Tscal>(gsph::names::common::hpart);
662
663 auto &merged_xyzh = storage.merged_xyzh.get();
664 auto &neigh_cache = storage.neigh_cache->neigh_cache;
665
666 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
667 u32 cnt = pdat.get_obj_cnt();
668 if (cnt == 0)
669 return;
670
671 auto &mfield = merged_xyzh.get(p.id_patch);
672 auto &pcache = neigh_cache.get(p.id_patch);
673
674 // Position from merged data (includes ghosts for neighbor search)
675 auto &buf_xyz = mfield.template get_field_buf_ref<Tvec>(0);
676 auto &buf_hpart = pdat.get_field_buf_ref<Tscal>(ihpart);
677
678 auto &dens_field = density_field.get_field(p.id_patch);
679
680 sham::DeviceQueue &q = dev_sched->get_queue();
681 sham::EventList depends_list;
682
683 auto ploop_ptrs = pcache.get_read_access(depends_list);
684 auto xyz_acc = buf_xyz.get_read_access(depends_list);
685 auto h_acc = buf_hpart.get_read_access(depends_list);
686 auto density_acc = dens_field.get_buf().get_write_access(depends_list);
687
688 auto e = q.submit(depends_list, [&, pmass](sycl::handler &cgh) {
689 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
690
691 shambase::parallel_for(cgh, cnt, "gsph_compute_density", [=](u64 gid) {
692 u32 id_a = (u32) gid;
693
694 Tvec xyz_a = xyz_acc[id_a];
695 Tscal h_a = h_acc[id_a];
696 Tscal dint = h_a * h_a * Rkern * Rkern;
697
698 // SPH density summation: rho_a = sum_b m_b W(|r_a - r_b|, h_a)
699 // (includes the self contribution b == a)
700 Tscal rho_sum = pmass * Kernel::W_3d(Tscal(0), h_a);
701
702 particle_looper.for_each_object(id_a, [&](u32 id_b) {
703 if (id_b == id_a) {
704 return;
705 }
706
707 Tvec dr = xyz_a - xyz_acc[id_b];
708 Tscal rab2 = sycl::dot(dr, dr);
709
710 if (rab2 > dint) {
711 return;
712 }
713
714 Tscal rab = sycl::sqrt(rab2);
715 rho_sum += pmass * Kernel::W_3d(rab, h_a);
716 });
717
718 density_acc[id_a] = sycl::max(rho_sum, Tscal(1e-30));
719 });
720 });
721
722 pcache.complete_event_state({e});
723 buf_xyz.complete_event_state(e);
724 buf_hpart.complete_event_state(e);
725 dens_field.get_buf().complete_event_state(e);
726 });
727}
728
729template<class Tvec, template<class> class Kern>
730void shammodels::gsph::Solver<Tvec, Kern>::gsph_prestep(Tscal time_val, Tscal dt) {
731 StackEntry stack_loc{};
732
733 shamlog_debug_ln("GSPH", "Prestep at t =", time_val, "dt =", dt);
734
735 using namespace shamrock;
736 using namespace shamrock::patch;
737
739 shamrock::SchedulerUtility utility(scheduler());
740
741 PatchDataLayerLayout &pdl = scheduler().pdl_old();
742 const u32 ihpart = pdl.get_field_idx<Tscal>("hpart");
743
744 ComputeField<Tscal> _epsilon_h, _h_old;
745
746 auto should_set_omega_mask = std::make_shared<shamrock::solvergraph::Field<u32>>(
747 1, "should_set_omega_mask", "should_set_omega_mask");
748
749 u32 hstep_cnt = 0;
750 u32 hstep_max = solver_config.h_max_subcycles_count;
751 for (; hstep_cnt < hstep_max; hstep_cnt++) {
752
753 build_ghost_cache(time_val + dt);
754 merge_position_ghost();
755 build_merged_pos_trees();
756 compute_presteps_rint();
757 start_neighbors_cache();
758
759 _epsilon_h = utility.make_compute_field<Tscal>("epsilon_h", 1, Tscal(100));
760 _h_old = utility.save_field<Tscal>(ihpart, "h_old");
761
762 Tscal max_eps_h;
763
764 if (solver_config.gpart_mass == 0) {
766 "invalid gpart_mass {}, this configuration can not converge.\n"
767 "Please set it using either model.set_particle_mass(pmass) or "
768 "cfg.set_particle_mass(pmass)",
769 solver_config.gpart_mass));
770 }
771
772 // sizes
773 std::shared_ptr<shamrock::solvergraph::Indexes<u32>> sizes
774 = std::make_shared<shamrock::solvergraph::Indexes<u32>>("", "");
775 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
776 sizes->indexes.add_obj(p.id_patch, pdat.get_obj_cnt());
777 });
778
779 // neigh cache
780 auto &neigh_cache = storage.neigh_cache;
781
782 // positions
783 auto &pos_merged = storage.positions_with_ghosts;
784
785 // old smoothing length field
786 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hold
787 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
789 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
790 auto &field = _h_old.get_field(p.id_patch);
791 hold_refs.add_obj(p.id_patch, std::ref(field));
792 });
793 hold->set_refs(hold_refs);
794
795 // new smoothing length field
796 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hnew
797 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
799 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
800 auto &field = pdat.get_field<Tscal>(ihpart);
801 hnew_refs.add_obj(p.id_patch, std::ref(field));
802 });
803 hnew->set_refs(hnew_refs);
804
805 // epsilon field
806 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> eps_h
807 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
809 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
810 auto &field = _epsilon_h.get_field(p.id_patch);
811 eps_h_refs.add_obj(p.id_patch, std::ref(field));
812 });
813 eps_h->set_refs(eps_h_refs);
814
815 std::shared_ptr<shamrock::solvergraph::INode> smth_h_iter_ptr;
816
817 using h_conf_density_based = typename gsph::SmoothingLengthConfig::DensityBased;
818 using h_conf_neigh_lim = typename gsph::SmoothingLengthConfig::DensityBasedNeighLim;
819
820 if (h_conf_density_based *conf
821 = std::get_if<h_conf_density_based>(&solver_config.smoothing_length_config.config)) {
822 std::shared_ptr<shammodels::sph::modules::IterateSmoothingLengthDensity<Tvec, Kernel>>
823 smth_h_iter = std::make_shared<
825 solver_config.gpart_mass,
826 solver_config.htol_up_coarse_cycle,
827 solver_config.htol_up_fine_cycle,
828 solver_config.epsilon_h);
829 smth_h_iter->set_edges(sizes, neigh_cache, pos_merged, hold, hnew, eps_h);
830 smth_h_iter_ptr = smth_h_iter;
831 } else if (
832 h_conf_neigh_lim *conf
833 = std::get_if<h_conf_neigh_lim>(&solver_config.smoothing_length_config.config)) {
834 std::shared_ptr<
836 smth_h_iter_neigh_lim = std::make_shared<
838 solver_config.gpart_mass,
839 solver_config.htol_up_coarse_cycle,
840 solver_config.htol_up_fine_cycle,
841 conf->max_neigh_count,
842 solver_config.epsilon_h);
843 smth_h_iter_neigh_lim->set_edges(
844 sizes, neigh_cache, pos_merged, hold, hnew, eps_h, should_set_omega_mask);
845 smth_h_iter_ptr = smth_h_iter_neigh_lim;
846 } else {
847 shambase::throw_with_loc<std::runtime_error>("Invalid smoothing length configuration");
848 }
849 // iterate smoothing length
850
851 std::shared_ptr<shamrock::solvergraph::IDataEdge<bool>> is_converged
852 = shamrock::solvergraph::IDataEdge<bool>::make_shared("", "");
853
855 smth_h_iter_ptr, solver_config.epsilon_h, solver_config.h_iter_per_subcycles, false);
856 loop_smth_h_iter.set_edges(eps_h, is_converged);
857
858 loop_smth_h_iter.evaluate();
859
860 if (!is_converged->data) {
861
862 Tscal largest_h = 0;
863
864 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
865 largest_h = sham::max(largest_h, pdat.get_field<Tscal>(ihpart).compute_max());
866 });
867 Tscal global_largest_h = shamalgs::collective::allreduce_max(largest_h);
868
869 std::string add_info = "";
870 u64 cnt_unconverged = 0;
871 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
873 = _epsilon_h.get_field(p.id_patch).get_ids_where([](auto access, u32 id) {
874 return access[id] == -1;
875 });
876
877 if (hstep_cnt == hstep_max - 1) {
878 if (idx_err.get_size() > 0) {
879 add_info += "\n patch " + std::to_string(p.id_patch) + " ";
880 add_info += "errored parts : \n";
881
882 sham::DeviceBuffer<Tvec> &xyz = pdat.get_field_buf_ref<Tvec>(0);
883 sham::DeviceBuffer<Tscal> &hpart = pdat.get_field_buf_ref<Tscal>(ihpart);
884
885 auto pos = xyz.copy_to_stdvec();
886 auto h = hpart.copy_to_stdvec();
887
888 for (u32 i : idx_err.copy_to_stdvec()) {
889 add_info
890 += sham::format("{} - pos : {}, hpart : {}\n", i, pos[i], h[i]);
891 }
892 }
893 }
894
895 cnt_unconverged += idx_err.get_size();
896 });
897
898 u64 global_cnt_unconverged = shamalgs::collective::allreduce_sum(cnt_unconverged);
899
900 if (shamcomm::world_rank() == 0) {
902 "Smoothinglength",
903 "smoothing length is not converged, rerunning the iterator ...\n largest h "
904 "=",
905 global_largest_h,
906 "unconverged cnt =",
907 global_cnt_unconverged,
908 add_info);
909 }
910
911 reset_ghost_handler();
912 clear_ghost_cache();
913
914 shambase::get_check_ref(storage.part_counts).free_alloc();
915 shambase::get_check_ref(storage.part_counts_with_ghost).free_alloc();
916 shambase::get_check_ref(storage.positions_with_ghosts).free_alloc();
917 shambase::get_check_ref(storage.hpart_with_ghosts).free_alloc();
918
919 storage.merged_xyzh.reset();
920
921 clear_merged_pos_trees();
922 reset_presteps_rint();
923 reset_neighbors_cache();
924
925 // scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchData &pdat) {
926 // pdat.synchronize_buf();
927 // });
928
929 continue;
930 }
931
932 // The hpart is not valid anymore in ghost zones since we iterated it's value
933 shambase::get_check_ref(storage.hpart_with_ghosts).free_alloc();
934
935 _epsilon_h.reset();
936 _h_old.reset();
937 break;
938 }
939
940 if (hstep_cnt == hstep_max) {
941 logger::err_ln("GSPH", "the h iterator is not converged after", hstep_cnt, "iterations");
942 }
943
944 std::shared_ptr<shamrock::solvergraph::FieldRefs<Tscal>> hnew_edge
945 = std::make_shared<shamrock::solvergraph::FieldRefs<Tscal>>("", "");
947 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
948 auto &field = pdat.get_field<Tscal>(ihpart);
949 hnew_refs.add_obj(p.id_patch, std::ref(field));
950 });
951 hnew_edge->set_refs(hnew_refs);
952
953 sph::modules::NodeComputeOmega<Tvec, Kern> compute_omega{solver_config.gpart_mass};
954 compute_omega.set_edges(
955 storage.part_counts,
956 storage.neigh_cache,
957 storage.positions_with_ghosts,
958 hnew_edge,
959 storage.omega);
960 compute_omega.evaluate();
961
962 if (solver_config.smoothing_length_config.is_density_based_neigh_lim()) {
963 // if the h limiter is triggered, omega does not hold it's sense of dh/dr anymore
964 // so we set it to 1, this effectively is equivalent of disabling the energy correction
965 // term corresponding to dh/dr
966 sph::modules::SetWhenMask<Tscal> set_omega_mask{1};
967 set_omega_mask.set_edges(storage.part_counts, should_set_omega_mask, storage.omega);
968 set_omega_mask.evaluate();
969 }
970
971 // NodeComputeOmega only produces the grad-h correction factor, not density
972 // itself (see Solver.hpp::compute_density() for why GSPH needs an explicit
973 // summed density field, unlike plain SPH). Compute it now, on the same
974 // converged h / neighbor cache / merged positions used above.
975 compute_density();
976}
977
978template<class Tvec, template<class> class Kern>
979void shammodels::gsph::Solver<Tvec, Kern>::apply_position_boundary(Tscal time_val) {
980 StackEntry stack_loc{};
981
982 shamlog_debug_ln("GSPH", "apply position boundary");
983
984 PatchScheduler &sched = scheduler();
985 shamrock::SchedulerUtility integrators(sched);
987
988 auto &pdl = sched.pdl_old();
989 const u32 ixyz = pdl.get_field_idx<Tvec>(gsph::names::common::xyz);
990 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
991 auto [bmin, bmax] = sched.get_box_volume<Tvec>();
992
993 using SolverConfigBC = typename Config::BCConfig;
994 using SolverBCFree = typename SolverConfigBC::Free;
995 using SolverBCPeriodic = typename SolverConfigBC::Periodic;
996 using SolverBCShearingPeriodic = typename SolverConfigBC::ShearingPeriodic;
997
998 if (SolverBCFree *c = std::get_if<SolverBCFree>(&solver_config.boundary_config.config)) {
999 if (shamcomm::world_rank() == 0) {
1000 logger::info_ln("PositionUpdated", "free boundaries skipping geometry update");
1001 }
1002 } else if (
1003 SolverBCPeriodic *c
1004 = std::get_if<SolverBCPeriodic>(&solver_config.boundary_config.config)) {
1005 integrators.fields_apply_periodicity(ixyz, std::pair{bmin, bmax});
1006 } else if (
1007 SolverBCShearingPeriodic *c
1008 = std::get_if<SolverBCShearingPeriodic>(&solver_config.boundary_config.config)) {
1009 // Apply shearing periodic boundaries (Stone 2010) - reuse SPH implementation
1010 integrators.fields_apply_shearing_periodicity(
1011 ixyz,
1012 ivxyz,
1013 std::pair{bmin, bmax},
1014 c->shear_base,
1015 c->shear_dir,
1016 c->shear_speed * time_val,
1017 c->shear_speed);
1018 } else {
1019 shambase::throw_with_loc<std::runtime_error>("GSPH: Unsupported boundary condition type.");
1020 }
1021
1022 reatrib.reatribute_patch_objects(storage.serial_patch_tree.get(), gsph::names::common::xyz);
1023}
1024
1025template<class Tvec, template<class> class Kern>
1026void shammodels::gsph::Solver<Tvec, Kern>::do_predictor_leapfrog(Tscal dt) {
1027 StackEntry stack_loc{};
1028 using namespace shamrock::patch;
1029
1030 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1031 const u32 ixyz = pdl.get_field_idx<Tvec>(gsph::names::common::xyz);
1032 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
1033 const u32 iaxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::axyz);
1034
1035 const bool has_uint = solver_config.has_field_uint();
1036 const u32 iuint = has_uint ? pdl.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
1037 const u32 iduint = has_uint ? pdl.get_field_idx<Tscal>(gsph::names::newtonian::duint) : 0;
1038
1039 Tscal half_dt = dt / 2;
1040
1041 // Predictor step: leapfrog kick-drift-kick
1042 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1043 u32 cnt = pdat.get_obj_cnt();
1044 if (cnt == 0)
1045 return;
1046
1047 auto &xyz_field = pdat.get_field<Tvec>(ixyz);
1048 auto &vxyz_field = pdat.get_field<Tvec>(ivxyz);
1049 auto &axyz_field = pdat.get_field<Tvec>(iaxyz);
1050
1051 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1052
1053 // Leapfrog KDK: first half-kick, then drift
1054 // The second half-kick (corrector) happens AFTER force recomputation
1056 dev_sched->get_queue(),
1057 sham::MultiRef{axyz_field.get_buf()},
1058 sham::MultiRef{xyz_field.get_buf(), vxyz_field.get_buf()},
1059 cnt,
1060 [half_dt, dt](u32 i, const Tvec *axyz, Tvec *xyz, Tvec *vxyz) {
1061 // First kick: v += a*dt/2 (using OLD acceleration)
1062 vxyz[i] += axyz[i] * half_dt;
1063 // Drift: x += v*dt
1064 xyz[i] += vxyz[i] * dt;
1065 });
1066
1067 // Internal energy integration (if adiabatic EOS)
1068 // Predictor: u += du*dt/2 (first half-step)
1069 // The second half-step happens in the corrector after force recomputation
1070 if (has_uint) {
1071 auto &uint_field = pdat.get_field<Tscal>(iuint);
1072 auto &duint_field = pdat.get_field<Tscal>(iduint);
1073
1075 dev_sched->get_queue(),
1076 sham::MultiRef{duint_field.get_buf()},
1077 sham::MultiRef{uint_field.get_buf()},
1078 cnt,
1079 [half_dt](u32 i, const Tscal *duint, Tscal *uint) {
1080 // u += du*dt/2 (first half-step)
1081 uint[i] += duint[i] * half_dt;
1082 });
1083 }
1084 });
1085}
1086
1087template<class Tvec, template<class> class Kern>
1088void shammodels::gsph::Solver<Tvec, Kern>::init_ghost_layout() {
1089 StackEntry stack_loc{};
1090
1091 // Initialize xyzh_ghost_layout for BasicSPHGhostHandler (position + smoothing length)
1092 storage.xyzh_ghost_layout = std::make_shared<shamrock::patch::PatchDataLayerLayout>();
1093 storage.xyzh_ghost_layout->template add_field<Tvec>(gsph::names::common::xyz, 1);
1094 storage.xyzh_ghost_layout->template add_field<Tscal>(gsph::names::common::hpart, 1);
1095
1096 // Reset first in case it was set from a previous timestep
1097 storage.ghost_layout = std::make_shared<shamrock::patch::PatchDataLayerLayout>();
1098
1100 = shambase::get_check_ref(storage.ghost_layout.get());
1101
1102 solver_config.set_ghost_layout(ghost_layout);
1103}
1104
1105template<class Tvec, template<class> class Kern>
1106void shammodels::gsph::Solver<Tvec, Kern>::communicate_merge_ghosts_fields() {
1107 StackEntry stack_loc{};
1108
1109 shambase::Timer timer_interf;
1110 timer_interf.start();
1111
1112 using namespace shamrock;
1113 using namespace shamrock::patch;
1114
1115 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1116 const u32 ixyz = pdl.get_field_idx<Tvec>(gsph::names::common::xyz);
1117 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
1118 const u32 ihpart = pdl.get_field_idx<Tscal>(gsph::names::common::hpart);
1119
1120 const bool has_uint = solver_config.has_field_uint();
1121 const u32 iuint = has_uint ? pdl.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
1122
1123 auto &ghost_layout_ptr = storage.ghost_layout;
1124 shamrock::patch::PatchDataLayerLayout &ghost_layout = shambase::get_check_ref(ghost_layout_ptr);
1125 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>(gsph::names::common::hpart);
1126 u32 ivxyz_interf = ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
1127 u32 iomega_interf = ghost_layout.get_field_idx<Tscal>(gsph::names::newtonian::omega);
1128 u32 idensity_interf = ghost_layout.get_field_idx<Tscal>(gsph::names::newtonian::density);
1129 u32 iuint_interf
1130 = has_uint ? ghost_layout.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
1131
1132 // Gradient field indices (for MUSCL reconstruction)
1133 const bool has_grads = solver_config.requires_gradients();
1134 u32 igrad_d_interf
1135 = has_grads ? ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::grad_density) : 0;
1136 u32 igrad_p_interf
1137 = has_grads ? ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::grad_pressure) : 0;
1138 u32 igrad_vx_interf
1139 = has_grads ? ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::grad_vx) : 0;
1140 u32 igrad_vy_interf
1141 = has_grads ? ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::grad_vy) : 0;
1142 u32 igrad_vz_interf
1143 = has_grads ? ghost_layout.get_field_idx<Tvec>(gsph::names::newtonian::grad_vz) : 0;
1144
1145 using InterfaceBuildInfos = typename gsph::GSPHGhostHandler<Tvec>::InterfaceBuildInfos;
1146
1147 gsph::GSPHGhostHandler<Tvec> &ghost_handle
1148 = shambase::get_check_ref(storage.ghost_handler).get();
1151
1152 // Get gradient fields (for MUSCL)
1153 shamrock::solvergraph::Field<Tvec> *grad_density_ptr
1154 = has_grads ? &shambase::get_check_ref(storage.grad_density) : nullptr;
1155 shamrock::solvergraph::Field<Tvec> *grad_pressure_ptr
1156 = has_grads ? &shambase::get_check_ref(storage.grad_pressure) : nullptr;
1158 = has_grads ? &shambase::get_check_ref(storage.grad_vx) : nullptr;
1160 = has_grads ? &shambase::get_check_ref(storage.grad_vy) : nullptr;
1162 = has_grads ? &shambase::get_check_ref(storage.grad_vz) : nullptr;
1163
1164 // Build interface data from ghost cache
1165 auto pdat_interf = ghost_handle.template build_interface_native<PatchDataLayer>(
1166 storage.ghost_patch_cache.get(),
1167 [&](u64 sender, u64, InterfaceBuildInfos binfo, sham::DeviceBuffer<u32> &buf_idx, u32 cnt) {
1168 PatchDataLayer pdat(ghost_layout_ptr);
1169 pdat.reserve(cnt);
1170 return pdat;
1171 });
1172
1173 // Populate interface data with field values
1174 ghost_handle.template modify_interface_native<PatchDataLayer>(
1175 storage.ghost_patch_cache.get(),
1176 pdat_interf,
1177 [&](u64 sender,
1178 u64,
1179 InterfaceBuildInfos binfo,
1180 sham::DeviceBuffer<u32> &buf_idx,
1181 u32 cnt,
1182 PatchDataLayer &pdat) {
1183 PatchDataLayer &sender_patch = scheduler().patch_data.get_pdat(sender);
1184 PatchDataField<Tscal> &sender_omega = omega.get(sender);
1185 PatchDataField<Tscal> &sender_density = density.get(sender);
1186
1187 sender_patch.get_field<Tscal>(ihpart).append_subset_to(
1188 buf_idx, cnt, pdat.get_field<Tscal>(ihpart_interf));
1189 sender_patch.get_field<Tvec>(ivxyz).append_subset_to(
1190 buf_idx, cnt, pdat.get_field<Tvec>(ivxyz_interf));
1191 sender_omega.append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(iomega_interf));
1192 sender_density.append_subset_to(buf_idx, cnt, pdat.get_field<Tscal>(idensity_interf));
1193
1194 if (has_uint) {
1195 sender_patch.get_field<Tscal>(iuint).append_subset_to(
1196 buf_idx, cnt, pdat.get_field<Tscal>(iuint_interf));
1197 }
1198
1199 // Communicate gradient fields for MUSCL reconstruction
1200 if (has_grads) {
1201 grad_density_ptr->get(sender).append_subset_to(
1202 buf_idx, cnt, pdat.get_field<Tvec>(igrad_d_interf));
1203 grad_pressure_ptr->get(sender).append_subset_to(
1204 buf_idx, cnt, pdat.get_field<Tvec>(igrad_p_interf));
1205 grad_vx_ptr->get(sender).append_subset_to(
1206 buf_idx, cnt, pdat.get_field<Tvec>(igrad_vx_interf));
1207 grad_vy_ptr->get(sender).append_subset_to(
1208 buf_idx, cnt, pdat.get_field<Tvec>(igrad_vy_interf));
1209 grad_vz_ptr->get(sender).append_subset_to(
1210 buf_idx, cnt, pdat.get_field<Tvec>(igrad_vz_interf));
1211 }
1212 });
1213
1214 // Apply velocity offset for periodic boundaries
1215 ghost_handle.template modify_interface_native<PatchDataLayer>(
1216 storage.ghost_patch_cache.get(),
1217 pdat_interf,
1218 [&](u64 sender,
1219 u64,
1220 InterfaceBuildInfos binfo,
1221 sham::DeviceBuffer<u32> &buf_idx,
1222 u32 cnt,
1223 PatchDataLayer &pdat) {
1224 if (sycl::length(binfo.offset_speed) > 0) {
1225 pdat.get_field<Tvec>(ivxyz_interf).apply_offset(binfo.offset_speed);
1226 }
1227 });
1228
1229 // Communicate ghost data across MPI ranks
1231 = ghost_handle.communicate_pdat(ghost_layout_ptr, std::move(pdat_interf));
1232
1233 // Count total ghost particles per patch
1234 std::map<u64, u64> sz_interf_map;
1235 interf_pdat.for_each([&](u64 s, u64 r, PatchDataLayer &pdat_interf) {
1236 sz_interf_map[r] += pdat_interf.get_obj_cnt();
1237 });
1238
1239 // Merge local and ghost data
1240 storage.merged_patchdata_ghost.set(
1241 ghost_handle.template merge_native<PatchDataLayer, PatchDataLayer>(
1242 std::move(interf_pdat),
1244 PatchDataLayer pdat_new(ghost_layout_ptr);
1245
1246 u32 or_elem = pdat.get_obj_cnt();
1247 pdat_new.reserve(or_elem + sz_interf_map[p.id_patch]);
1248
1249 PatchDataField<Tscal> &cur_omega = omega.get(p.id_patch);
1250 PatchDataField<Tscal> &cur_density = density.get(p.id_patch);
1251
1252 // Insert local particle data
1253 pdat_new.get_field<Tscal>(ihpart_interf).insert(pdat.get_field<Tscal>(ihpart));
1254 pdat_new.get_field<Tvec>(ivxyz_interf).insert(pdat.get_field<Tvec>(ivxyz));
1255 pdat_new.get_field<Tscal>(iomega_interf).insert(cur_omega);
1256 pdat_new.get_field<Tscal>(idensity_interf).insert(cur_density);
1257
1258 if (has_uint) {
1259 pdat_new.get_field<Tscal>(iuint_interf).insert(pdat.get_field<Tscal>(iuint));
1260 }
1261
1262 // Insert local gradient data for MUSCL reconstruction
1263 if (has_grads) {
1264 pdat_new.get_field<Tvec>(igrad_d_interf)
1265 .insert(grad_density_ptr->get(p.id_patch));
1266 pdat_new.get_field<Tvec>(igrad_p_interf)
1267 .insert(grad_pressure_ptr->get(p.id_patch));
1268 pdat_new.get_field<Tvec>(igrad_vx_interf).insert(grad_vx_ptr->get(p.id_patch));
1269 pdat_new.get_field<Tvec>(igrad_vy_interf).insert(grad_vy_ptr->get(p.id_patch));
1270 pdat_new.get_field<Tvec>(igrad_vz_interf).insert(grad_vz_ptr->get(p.id_patch));
1271 }
1272
1273 pdat_new.check_field_obj_cnt_match();
1274 return pdat_new;
1275 },
1276 [](PatchDataLayer &pdat, PatchDataLayer &pdat_interf) {
1277 pdat.insert_elements(pdat_interf);
1278 }));
1279
1280 timer_interf.stop();
1281 storage.timings_details.interface += timer_interf.elapsed_sec();
1282}
1283
1284template<class Tvec, template<class> class Kern>
1285void shammodels::gsph::Solver<Tvec, Kern>::reset_merge_ghosts_fields() {
1286 storage.merged_patchdata_ghost.reset();
1287}
1288
1289template<class Tvec, template<class> class Kern>
1290void shammodels::gsph::Solver<Tvec, Kern>::compute_eos_fields() {
1291 StackEntry stack_loc{};
1292
1293 using namespace shamrock;
1294 using namespace shamrock::patch;
1295
1296 // GSPH EOS: Following reference implementation (g_pre_interaction.cpp)
1297 // P = (\gamma - 1) * \rho * u where \rho is from SPH summation
1298 // c = sqrt(\gamma * (\gamma - 1) * u) -- from internal energy, not from P/\rho
1299
1300 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1301 const Tscal gamma = solver_config.get_eos_gamma();
1302 const bool has_uint = solver_config.has_field_uint();
1303
1304 // Get ghost layout field indices
1306 = shambase::get_check_ref(storage.ghost_layout.get());
1307 u32 idensity_interf = ghost_layout.get_field_idx<Tscal>(gsph::names::newtonian::density);
1308 u32 iuint_interf
1309 = has_uint ? ghost_layout.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
1310
1311 shamrock::solvergraph::Field<Tscal> &pressure_field = shambase::get_check_ref(storage.pressure);
1312 shamrock::solvergraph::Field<Tscal> &soundspeed_field
1313 = shambase::get_check_ref(storage.soundspeed);
1314
1315 // Size buffers to part_counts_with_ghost (includes ghosts!)
1316 shambase::DistributedData<u32> &counts_with_ghosts
1317 = shambase::get_check_ref(storage.part_counts_with_ghost).indexes;
1318
1319 pressure_field.ensure_sizes(counts_with_ghosts);
1320 soundspeed_field.ensure_sizes(counts_with_ghosts);
1321
1322 // Iterate over merged_patchdata_ghost (includes local + ghost particles)
1323 storage.merged_patchdata_ghost.get().for_each([&](u64 id, PatchDataLayer &mpdat) {
1324 u32 total_elements
1325 = shambase::get_check_ref(storage.part_counts_with_ghost).indexes.get(id);
1326 if (total_elements == 0)
1327 return;
1328
1329 // Use SPH-summation density from communicated ghost data
1330 sham::DeviceBuffer<Tscal> &buf_density = mpdat.get_field_buf_ref<Tscal>(idensity_interf);
1331 auto &pressure_buf = pressure_field.get_field(id).get_buf();
1332 auto &soundspeed_buf = soundspeed_field.get_field(id).get_buf();
1333
1334 sham::DeviceQueue &q = dev_sched->get_queue();
1335 sham::EventList depends_list;
1336
1337 auto density = buf_density.get_read_access(depends_list);
1338 auto pressure = pressure_buf.get_write_access(depends_list);
1339 auto soundspeed = soundspeed_buf.get_write_access(depends_list);
1340
1341 const Tscal *uint_ptr = nullptr;
1342 if (has_uint) {
1343 uint_ptr = mpdat.get_field_buf_ref<Tscal>(iuint_interf).get_read_access(depends_list);
1344 }
1345
1346 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1347 shambase::parallel_for(cgh, total_elements, "compute_eos_gsph", [=](u64 gid) {
1348 u32 i = (u32) gid;
1349
1350 // Use SPH-summation density (from compute_omega, communicated to ghosts)
1351 Tscal rho = density[i];
1352 rho = sycl::max(rho, Tscal(1e-30));
1353
1354 if (has_uint && uint_ptr != nullptr) {
1355 // Adiabatic EOS (reference: g_pre_interaction.cpp line 107)
1356 // P = (\gamma - 1) * \rho * u
1357 Tscal u = uint_ptr[i];
1358 u = sycl::max(u, Tscal(1e-30));
1359 Tscal P = (gamma - Tscal(1.0)) * rho * u;
1360
1361 // Sound speed from internal energy (reference: solver.cpp line 2661)
1362 // c = sqrt(\gamma * (\gamma - 1) * u)
1363 Tscal cs = sycl::sqrt(gamma * (gamma - Tscal(1.0)) * u);
1364
1365 // Clamp to reasonable values
1366 P = sycl::clamp(P, Tscal(1e-30), Tscal(1e30));
1367 cs = sycl::clamp(cs, Tscal(1e-10), Tscal(1e10));
1368
1369 pressure[i] = P;
1370 soundspeed[i] = cs;
1371 } else {
1372 // Isothermal case
1373 Tscal cs = Tscal(1.0);
1374 Tscal P = cs * cs * rho;
1375
1376 pressure[i] = P;
1377 soundspeed[i] = cs;
1378 }
1379 });
1380 });
1381
1382 // Complete all buffer event states
1383 buf_density.complete_event_state(e);
1384 if (has_uint) {
1385 mpdat.get_field_buf_ref<Tscal>(iuint_interf).complete_event_state(e);
1386 }
1387 pressure_buf.complete_event_state(e);
1388 soundspeed_buf.complete_event_state(e);
1389 });
1390}
1391
1392template<class Tvec, template<class> class Kern>
1393void shammodels::gsph::Solver<Tvec, Kern>::reset_eos_fields() {
1394 // Reset computed EOS fields - they're recomputed each timestep
1395}
1396
1397template<class Tvec, template<class> class Kern>
1399 StackEntry stack_loc{};
1400
1401 using namespace shamrock;
1402 using namespace shamrock::patch;
1403
1404 // Copy density, pressure, and soundspeed from solvergraph fields to patchdata
1405 // This ensures the values persist across restarts and can be read by VTKDump
1406
1407 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1408 u32 idensity = pdl.get_field_idx<Tscal>(names::newtonian::density);
1409 u32 ipressure = pdl.get_field_idx<Tscal>(names::newtonian::pressure);
1410 u32 isoundspeed = pdl.get_field_idx<Tscal>(names::newtonian::soundspeed);
1411
1412 auto &density_field = shambase::get_check_ref(storage.density);
1413 auto &pressure_field = shambase::get_check_ref(storage.pressure);
1414 auto &soundspeed_field = shambase::get_check_ref(storage.soundspeed);
1415
1416 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
1417 u32 npart = pdat.get_obj_cnt();
1418 if (npart == 0) {
1419 return;
1420 }
1421
1422 // Get patchdata buffers
1423 sham::DeviceBuffer<Tscal> &buf_rho = pdat.get_field_buf_ref<Tscal>(idensity);
1424 sham::DeviceBuffer<Tscal> &buf_P = pdat.get_field_buf_ref<Tscal>(ipressure);
1425 sham::DeviceBuffer<Tscal> &buf_cs = pdat.get_field_buf_ref<Tscal>(isoundspeed);
1426
1427 // Get solvergraph field buffers (source data)
1428 sham::DeviceBuffer<Tscal> &buf_rho_in = density_field.get_field(cur_p.id_patch).get_buf();
1429 sham::DeviceBuffer<Tscal> &buf_P_in = pressure_field.get_field(cur_p.id_patch).get_buf();
1430 sham::DeviceBuffer<Tscal> &buf_cs_in = soundspeed_field.get_field(cur_p.id_patch).get_buf();
1431
1432 auto &q = shamsys::instance::get_compute_scheduler().get_queue();
1433 sham::EventList depends_list;
1434
1435 auto rho_in = buf_rho_in.get_read_access(depends_list);
1436 auto P_in = buf_P_in.get_read_access(depends_list);
1437 auto cs_in = buf_cs_in.get_read_access(depends_list);
1438 auto rho = buf_rho.get_write_access(depends_list);
1439 auto P = buf_P.get_write_access(depends_list);
1440 auto cs = buf_cs.get_write_access(depends_list);
1441
1442 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1443 cgh.parallel_for(sycl::range<1>{npart}, [=](sycl::item<1> item) {
1444 rho[item] = rho_in[item];
1445 P[item] = P_in[item];
1446 cs[item] = cs_in[item];
1447 });
1448 });
1449
1450 buf_rho_in.complete_event_state(e);
1451 buf_P_in.complete_event_state(e);
1452 buf_cs_in.complete_event_state(e);
1453 buf_rho.complete_event_state(e);
1454 buf_P.complete_event_state(e);
1455 buf_cs.complete_event_state(e);
1456 });
1457}
1458
1459template<class Tvec, template<class> class Kern>
1461 StackEntry stack_loc{};
1462
1463 // Only compute gradients for MUSCL reconstruction
1464 if (!solver_config.requires_gradients()) {
1465 return;
1466 }
1467
1468 using namespace shamrock;
1469 using namespace shamrock::patch;
1470
1471 const Tscal pmass = solver_config.gpart_mass;
1472 const Tscal gamma = solver_config.get_eos_gamma();
1473
1474 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1475
1476 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1477 const u32 ihpart = pdl.get_field_idx<Tscal>(gsph::names::common::hpart);
1478 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
1479 const bool has_uint = solver_config.has_field_uint();
1480 const u32 iuint = has_uint ? pdl.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
1481
1482 // Get gradient fields from storage
1483 shamrock::solvergraph::Field<Tvec> &grad_density_field
1484 = shambase::get_check_ref(storage.grad_density);
1485 shamrock::solvergraph::Field<Tvec> &grad_pressure_field
1486 = shambase::get_check_ref(storage.grad_pressure);
1487 shamrock::solvergraph::Field<Tvec> &grad_vx_field = shambase::get_check_ref(storage.grad_vx);
1488 shamrock::solvergraph::Field<Tvec> &grad_vy_field = shambase::get_check_ref(storage.grad_vy);
1489 shamrock::solvergraph::Field<Tvec> &grad_vz_field = shambase::get_check_ref(storage.grad_vz);
1490
1491 // Get density field for SPH-summation density
1492 shamrock::solvergraph::Field<Tscal> &density_field = shambase::get_check_ref(storage.density);
1493
1494 // Ensure gradient fields have correct sizes
1495 shambase::DistributedData<u32> &counts = shambase::get_check_ref(storage.part_counts).indexes;
1496
1497 grad_density_field.ensure_sizes(counts);
1498 grad_pressure_field.ensure_sizes(counts);
1499 grad_vx_field.ensure_sizes(counts);
1500 grad_vy_field.ensure_sizes(counts);
1501 grad_vz_field.ensure_sizes(counts);
1502
1503 auto &merged_xyzh = storage.merged_xyzh.get();
1504 auto &neigh_cache = storage.neigh_cache->neigh_cache;
1505
1506 static constexpr Tscal Rkern = Kernel::Rkern;
1507
1508 // Compute gradients following reference implementation (g_pre_interaction.cpp)
1509 // grad_d = \sigma_j m_j \nabla W_ij
1510 // grad_p = (grad_d * u_i + du) * (\gamma - 1) where du = \sigma_j m_j (u_j - u_i) \nabla W_ij
1511 // grad_v = \sigma_j m_j (v_j - v_i) \nabla W_ij / \rho_i
1512 scheduler().for_each_patchdata_nonempty([&](const Patch p, PatchDataLayer &pdat) {
1513 u32 cnt = pdat.get_obj_cnt();
1514 if (cnt == 0)
1515 return;
1516
1517 auto &mfield = merged_xyzh.get(p.id_patch);
1518 auto &pcache = neigh_cache.get(p.id_patch);
1519
1520 // Get position, h, velocity from merged data
1521 auto &buf_xyz = mfield.template get_field_buf_ref<Tvec>(0);
1522 auto &buf_hpart = mfield.template get_field_buf_ref<Tscal>(1);
1523 auto &buf_vxyz = pdat.get_field_buf_ref<Tvec>(ivxyz);
1524
1525 // Get density (local particles only)
1526 auto &dens_field = density_field.get_field(p.id_patch);
1527
1528 // Get gradient output fields
1529 auto &grad_d_field = grad_density_field.get_field(p.id_patch);
1530 auto &grad_p_field = grad_pressure_field.get_field(p.id_patch);
1531 auto &grad_vx_buf = grad_vx_field.get_field(p.id_patch);
1532 auto &grad_vy_buf = grad_vy_field.get_field(p.id_patch);
1533 auto &grad_vz_buf = grad_vz_field.get_field(p.id_patch);
1534
1535 sham::DeviceQueue &q = dev_sched->get_queue();
1536 sham::EventList depends_list;
1537
1538 auto ploop_ptrs = pcache.get_read_access(depends_list);
1539 auto xyz_acc = buf_xyz.get_read_access(depends_list);
1540 auto h_acc = buf_hpart.get_read_access(depends_list);
1541 auto v_acc = buf_vxyz.get_read_access(depends_list);
1542 auto dens_acc = dens_field.get_buf().get_read_access(depends_list);
1543 auto grad_d_acc = grad_d_field.get_buf().get_write_access(depends_list);
1544 auto grad_p_acc = grad_p_field.get_buf().get_write_access(depends_list);
1545 auto grad_vx_acc = grad_vx_buf.get_buf().get_write_access(depends_list);
1546 auto grad_vy_acc = grad_vy_buf.get_buf().get_write_access(depends_list);
1547 auto grad_vz_acc = grad_vz_buf.get_buf().get_write_access(depends_list);
1548
1549 // Get internal energy if adiabatic
1550 const Tscal *uint_ptr = nullptr;
1551 if (has_uint) {
1552 uint_ptr = pdat.get_field_buf_ref<Tscal>(iuint).get_read_access(depends_list);
1553 }
1554
1555 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1556 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
1557
1558 shambase::parallel_for(cgh, cnt, "gsph_compute_gradients", [=](u64 gid) {
1559 u32 id_a = (u32) gid;
1560
1561 Tvec xyz_a = xyz_acc[id_a];
1562 Tscal h_a = h_acc[id_a];
1563 Tvec v_a = v_acc[id_a];
1564 Tscal rho_a = sycl::max(dens_acc[id_a], Tscal(1e-30));
1565 Tscal dint = h_a * h_a * Rkern * Rkern;
1566
1567 // Get internal energy for particle a
1568 Tscal u_a = Tscal(0);
1569 if (uint_ptr != nullptr) {
1570 u_a = uint_ptr[id_a];
1571 }
1572
1573 // Initialize gradient accumulators
1574 Tvec grad_d = {0, 0, 0}; // Density gradient
1575 Tvec grad_u = {0, 0, 0}; // Internal energy difference gradient
1576 Tvec grad_vx = {0, 0, 0}; // Velocity component gradients
1577 Tvec grad_vy = {0, 0, 0};
1578 Tvec grad_vz = {0, 0, 0};
1579
1580 particle_looper.for_each_object(id_a, [&](u32 id_b) {
1581 Tvec dr = xyz_a - xyz_acc[id_b];
1582 Tscal rab2 = sycl::dot(dr, dr);
1583
1584 if (rab2 > dint || id_a == id_b) {
1585 return;
1586 }
1587
1588 Tscal rab = sycl::sqrt(rab2);
1589
1590 // Kernel gradient: \nabla W = (dW/dr) * (r/|r|)
1591 Tscal dWdr = Kernel::dW_3d(rab, h_a);
1592 Tvec gradW = dr * (dWdr * sham::inv_sat_positive(rab));
1593
1594 // Accumulate gradients (reference: g_pre_interaction.cpp lines 130-147)
1595 grad_d += gradW * pmass;
1596
1597 // Internal energy gradient for pressure
1598 Tscal u_b = (uint_ptr != nullptr) ? uint_ptr[id_b] : Tscal(0);
1599 grad_u += gradW * (pmass * (u_b - u_a));
1600
1601 // Velocity gradients
1602 Tvec v_b = v_acc[id_b];
1603 grad_vx += gradW * (pmass * (v_b[0] - v_a[0]));
1604 grad_vy += gradW * (pmass * (v_b[1] - v_a[1]));
1605 grad_vz += gradW * (pmass * (v_b[2] - v_a[2]));
1606 });
1607
1608 // Store density gradient
1609 grad_d_acc[id_a] = grad_d;
1610
1611 // Compute pressure gradient: \nabla P = (\nabla \rho * u + du) * (\gamma - 1)
1612 // (reference: g_pre_interaction.cpp line 143)
1613 Tvec grad_p = (grad_d * u_a + grad_u) * (gamma - Tscal(1));
1614 grad_p_acc[id_a] = grad_p;
1615
1616 // Normalize velocity gradients by density
1617 // (reference: g_pre_interaction.cpp lines 144-147)
1618 Tscal rho_inv = sham::inv_sat_positive(rho_a);
1619 grad_vx_acc[id_a] = grad_vx * rho_inv;
1620 grad_vy_acc[id_a] = grad_vy * rho_inv;
1621 grad_vz_acc[id_a] = grad_vz * rho_inv;
1622 });
1623 });
1624
1625 // Complete event states
1626 pcache.complete_event_state({e});
1627 buf_xyz.complete_event_state(e);
1628 buf_hpart.complete_event_state(e);
1629 buf_vxyz.complete_event_state(e);
1630 dens_field.get_buf().complete_event_state(e);
1631 grad_d_field.get_buf().complete_event_state(e);
1632 grad_p_field.get_buf().complete_event_state(e);
1633 grad_vx_buf.get_buf().complete_event_state(e);
1634 grad_vy_buf.get_buf().complete_event_state(e);
1635 grad_vz_buf.get_buf().complete_event_state(e);
1636 if (has_uint) {
1637 pdat.get_field_buf_ref<Tscal>(iuint).complete_event_state(e);
1638 }
1639 });
1640}
1641
1642template<class Tvec, template<class> class Kern>
1643void shammodels::gsph::Solver<Tvec, Kern>::prepare_corrector() {
1644 StackEntry stack_loc{};
1645
1646 shamrock::SchedulerUtility utility(scheduler());
1647 shamrock::patch::PatchDataLayerLayout &pdl = scheduler().pdl_old();
1648
1649 const u32 iaxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::axyz);
1650
1651 // Create compute field to store old acceleration
1652 auto old_axyz = utility.make_compute_field<Tvec>(gsph::names::internal::old_axyz, 1);
1653
1654 // Copy current acceleration to old_axyz
1655 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1656
1657 scheduler().for_each_patchdata_nonempty(
1659 u32 cnt = pdat.get_obj_cnt();
1660 if (cnt == 0)
1661 return;
1662
1663 auto &axyz_field = pdat.get_field<Tvec>(iaxyz);
1664 auto &old_axyz_field = old_axyz.get_field(p.id_patch);
1665
1666 // Copy using kernel_call
1668 dev_sched->get_queue(),
1669 sham::MultiRef{axyz_field.get_buf()},
1670 sham::MultiRef{old_axyz_field.get_buf()},
1671 cnt,
1672 [](u32 i, const Tvec *src, Tvec *dst) {
1673 dst[i] = src[i];
1674 });
1675 });
1676
1677 storage.old_axyz.set(std::move(old_axyz));
1678
1679 if (solver_config.has_field_uint()) {
1680 const u32 iduint = pdl.get_field_idx<Tscal>(gsph::names::newtonian::duint);
1681 auto old_duint = utility.make_compute_field<Tscal>(gsph::names::internal::old_duint, 1);
1682
1683 scheduler().for_each_patchdata_nonempty(
1685 u32 cnt = pdat.get_obj_cnt();
1686 if (cnt == 0)
1687 return;
1688
1689 auto &duint_field = pdat.get_field<Tscal>(iduint);
1690 auto &old_duint_field = old_duint.get_field(p.id_patch);
1691
1692 // Copy using kernel_call
1694 dev_sched->get_queue(),
1695 sham::MultiRef{duint_field.get_buf()},
1696 sham::MultiRef{old_duint_field.get_buf()},
1697 cnt,
1698 [](u32 i, const Tscal *src, Tscal *dst) {
1699 dst[i] = src[i];
1700 });
1701 });
1702
1703 storage.old_duint.set(std::move(old_duint));
1704 }
1705}
1706
1707template<class Tvec, template<class> class Kern>
1709 StackEntry stack_loc{};
1710 // GSPH derivative update using Riemann solver
1711 gsph::modules::UpdateDerivs<Tvec, Kern>(context, solver_config, storage).update_derivs();
1712}
1713
1714template<class Tvec, template<class> class Kern>
1715typename shammodels::gsph::Solver<Tvec, Kern>::Tscal shammodels::gsph::Solver<Tvec, Kern>::
1717 StackEntry stack_loc{};
1718
1719 using namespace shamrock;
1720 using namespace shamrock::patch;
1721
1722 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1723
1724 PatchDataLayerLayout &pdl = scheduler().pdl_old();
1725 const u32 ihpart = pdl.get_field_idx<Tscal>(gsph::names::common::hpart);
1726 const u32 iaxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::axyz);
1727
1728 shamrock::solvergraph::Field<Tscal> &soundspeed_field
1729 = shambase::get_check_ref(storage.soundspeed);
1730
1731 Tscal C_cour = solver_config.cfl_config.cfl_cour;
1732 Tscal C_force = solver_config.cfl_config.cfl_force;
1733
1734 // Use ComputeField for proper reduction support
1735 shamrock::SchedulerUtility utility(scheduler());
1736 ComputeField<Tscal> cfl_dt = utility.make_compute_field<Tscal>("cfl_dt", 1);
1737
1738 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
1739 u32 cnt = pdat.get_obj_cnt();
1740 if (cnt == 0)
1741 return;
1742
1743 auto &buf_hpart = pdat.get_field_buf_ref<Tscal>(ihpart);
1744 auto &buf_axyz = pdat.get_field_buf_ref<Tvec>(iaxyz);
1745 auto &buf_cs = soundspeed_field.get_field(cur_p.id_patch).get_buf();
1746 auto &cfl_dt_buf = cfl_dt.get_buf_check(cur_p.id_patch);
1747
1748 sham::DeviceQueue &q = dev_sched->get_queue();
1749 sham::EventList depends_list;
1750
1751 auto hpart = buf_hpart.get_read_access(depends_list);
1752 auto axyz = buf_axyz.get_read_access(depends_list);
1753 auto cs = buf_cs.get_read_access(depends_list);
1754 auto cfl_dt_acc = cfl_dt_buf.get_write_access(depends_list);
1755
1756 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1757 shambase::parallel_for(cgh, cnt, "gsph_compute_cfl_dt", [=](u64 gid) {
1758 u32 i = (u32) gid;
1759
1760 Tscal h_i = hpart[i];
1761 Tscal cs_i = cs[i];
1762 Tscal abs_a = sycl::length(axyz[i]);
1763
1764 // Guard against invalid values (NaN/Inf)
1765 if (!sycl::isfinite(h_i) || h_i <= Tscal(0))
1766 h_i = Tscal(1e-10);
1767 if (!sycl::isfinite(cs_i) || cs_i <= Tscal(0))
1768 cs_i = Tscal(1e-10);
1769 if (!sycl::isfinite(abs_a))
1770 abs_a = Tscal(1e30);
1771
1772 // Sound CFL condition: dt = C_cour * h / c_s
1773 // Following Kitajima et al. (2025) simple form for GSPH
1774 Tscal dt_c = C_cour * h_i / cs_i;
1775
1776 // Force condition: dt = C_force * sqrt(h / |a|)
1777 Tscal dt_f = C_force * sycl::sqrt(h_i / (abs_a + Tscal(1e-30)));
1778
1779 Tscal dt_min = sycl::min(dt_c, dt_f);
1780
1781 // Ensure a valid finite timestep with minimum floor
1782 if (!sycl::isfinite(dt_min) || dt_min <= Tscal(0)) {
1783 dt_min = Tscal(1e-10); // Minimum timestep floor
1784 }
1785
1786 cfl_dt_acc[i] = dt_min;
1787 });
1788 });
1789
1790 buf_hpart.complete_event_state(e);
1791 buf_axyz.complete_event_state(e);
1792 buf_cs.complete_event_state(e);
1793 cfl_dt_buf.complete_event_state(e);
1794 });
1795
1796 // Compute minimum across all patches on this rank
1797 Tscal rank_dt = cfl_dt.compute_rank_min();
1798
1799 // Guard against invalid reduction result
1800 if (!std::isfinite(rank_dt) || rank_dt <= Tscal(0)) {
1801 rank_dt = Tscal(1e-6); // Reasonable floor for SPH simulations
1802 }
1803
1804 // Global reduction across MPI ranks
1805 Tscal global_min_dt = shamalgs::collective::allreduce_min(rank_dt);
1806
1807 // Final safety floor to prevent simulation stalling
1808 // For typical SPH simulations, timestep should be O(h/cs) ~ O(1e-4)
1809 // Use 1e-6 as minimum floor to prevent extreme stalling
1810 const Tscal dt_min_floor = Tscal(1e-6);
1811 if (!std::isfinite(global_min_dt) || global_min_dt < dt_min_floor) {
1812 global_min_dt = dt_min_floor;
1813 }
1814
1815 return global_min_dt;
1816}
1817
1818template<class Tvec, template<class> class Kern>
1819bool shammodels::gsph::Solver<Tvec, Kern>::apply_corrector(Tscal dt, u64 Npart_all) {
1820 StackEntry stack_loc{};
1821
1822 shamrock::patch::PatchDataLayerLayout &pdl = scheduler().pdl_old();
1823
1824 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
1825 const u32 iaxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::axyz);
1826
1827 Tscal half_dt = Tscal{0.5} * dt;
1828
1829 // Corrector: v = v + 0.5*a_new*dt (completing the leapfrog kick)
1830 // Predictor already added 0.5*a_old*dt, so total is 0.5*(a_old + a_new)*dt
1831 scheduler().for_each_patchdata_nonempty(
1833 u32 cnt = pdat.get_obj_cnt();
1834 if (cnt == 0)
1835 return;
1836
1837 auto &vxyz = pdat.get_field<Tvec>(ivxyz);
1838 auto &axyz = pdat.get_field<Tvec>(iaxyz);
1839
1840 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1841
1843 dev_sched->get_queue(),
1844 sham::MultiRef{axyz.get_buf()},
1845 sham::MultiRef{vxyz.get_buf()},
1846 cnt,
1847 [half_dt](u32 i, const Tvec *axyz_new, Tvec *vxyz) {
1848 vxyz[i] += half_dt * axyz_new[i];
1849 });
1850 });
1851
1852 if (solver_config.has_field_uint()) {
1853 const u32 iuint = pdl.get_field_idx<Tscal>(gsph::names::newtonian::uint);
1854 const u32 iduint = pdl.get_field_idx<Tscal>(gsph::names::newtonian::duint);
1855
1856 scheduler().for_each_patchdata_nonempty(
1858 u32 cnt = pdat.get_obj_cnt();
1859 if (cnt == 0)
1860 return;
1861
1862 auto &uint_field = pdat.get_field<Tscal>(iuint);
1863 auto &duint = pdat.get_field<Tscal>(iduint);
1864
1865 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1866
1867 // Corrector: u = u + 0.5*du_new*dt (completing the leapfrog)
1869 dev_sched->get_queue(),
1870 sham::MultiRef{duint.get_buf()},
1871 sham::MultiRef{uint_field.get_buf()},
1872 cnt,
1873 [half_dt](u32 i, const Tscal *duint_new, Tscal *uint) {
1874 uint[i] += half_dt * duint_new[i];
1875 });
1876 });
1877
1878 storage.old_duint.reset();
1879 }
1880
1881 storage.old_axyz.reset();
1882
1883 return true;
1884}
1885
1886template<class Tvec, template<class> class Kern>
1887void shammodels::gsph::Solver<Tvec, Kern>::update_sync_load_values() {
1888 modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
1889 .update_load_balancing();
1890 scheduler().scheduler_step(false, false);
1891}
1892
1893template<class Tvec, template<class> class Kern>
1895
1896 // Validate configuration before running
1897 solver_config.check_config_runtime();
1898
1899 Tscal t_current = get_time();
1900 Tscal dt = get_dt();
1901
1902 StackEntry stack_loc{};
1903
1904 if (shamcomm::world_rank() == 0) {
1906 sham::format("---------------- GSPH t = {}, dt = {} ----------------", t_current, dt));
1907 }
1908
1909 shambase::Timer tstep;
1910 tstep.start();
1911
1912 // Load balancing step
1913 gsph::modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
1914 .update_load_balancing();
1915 scheduler().scheduler_step(true, true);
1916 gsph::modules::ComputeLoadBalanceValue<Tvec, Kern>(context, solver_config, storage)
1917 .update_load_balancing();
1918 scheduler().scheduler_step(false, false);
1919
1921
1922 using namespace shamrock;
1923 using namespace shamrock::patch;
1924
1925 u64 Npart_all = scheduler().get_total_obj_count();
1926
1927 // =========================================================================
1928 // CORRECTED SIMULATION LOOP ORDER (matching reference SPH code)
1929 // =========================================================================
1930 // The key insight from the reference code is that density/EOS must be
1931 // computed AFTER the predictor step, on the NEW positions. Otherwise,
1932 // the forces are computed using stale EOS values.
1933 //
1934 // Loop order:
1935 // 1. PREDICTOR: move particles using OLD accelerations
1936 // 2. BOUNDARY: apply periodic/free boundary conditions
1937 // 3. PRESTEP: converge smoothing length h, then compute omega and density
1938 // (density is now computed inside gsph_prestep(), on the same
1939 // converged h / merged positions / neighbor cache - see compute_density())
1940 // 4. DENSITY/EOS: compute density, pressure, soundspeed on NEW positions
1941 // 5. FORCES: compute accelerations using FRESH EOS
1942 // 6. CORRECTOR: refine velocities using average of old/new accelerations
1943 // 7. CFL: compute next timestep
1944 // =========================================================================
1945
1946 // STEP 1: PREDICTOR - move particles using OLD accelerations
1947 // (On first iteration, accelerations are zero, so this is just position drift)
1948 do_predictor_leapfrog(dt);
1949
1950 // STEP 2: BOUNDARY - apply boundary conditions to NEW positions
1951 // Build serial patch tree first (needed for boundary application)
1952 gen_serial_patch_tree();
1953 apply_position_boundary(t_current + dt);
1954
1955 // STEP 3: PRESTEP - converges h, builds ghost/tree/neighbor-cache state on
1956 // the new positions, and computes omega + density (see gsph_prestep() /
1957 // compute_density()). Everything it builds (ghost handler, ghost cache,
1958 // merged positions, merged trees, rint field, neighbor cache) stays valid
1959 // and is reused below - no need to rebuild it a second time here.
1960 gsph_prestep(t_current, dt);
1961
1962 // STEP 4a: GRADIENTS - compute for MUSCL reconstruction (if enabled)
1963 // Computed BEFORE ghost communication so gradients are included in ghost data
1964 // Gradients are computed on local particles using neighbor data
1966
1967 // Initialize ghost layout BEFORE communication
1968 // (includes gradients if MUSCL is enabled)
1969 init_ghost_layout();
1970
1971 // Communicate ghost fields (hpart, uint, vxyz, omega, and gradients if MUSCL)
1972 // This MUST happen BEFORE compute_eos_fields so EOS can be computed for ghosts
1973 communicate_merge_ghosts_fields();
1974
1975 // STEP 4b: EOS - compute AFTER ghost communication (CRITICAL!)
1976 // This ensures P and cs are computed for ALL particles (local + ghost)
1977 // Following SPH pattern: EOS is computed on merged_patchdata_ghost
1978 compute_eos_fields();
1979
1980 // STEP 5: FORCES - compute accelerations using FRESH EOS
1981 // Save old accelerations for corrector
1982 prepare_corrector();
1983
1984 // Update derivatives using GSPH Riemann solver
1985 update_derivs();
1986
1987 // STEP 6: CORRECTOR - refine velocities
1988 apply_corrector(dt, Npart_all);
1989
1990 // STEP 7: CFL - compute next timestep
1991 Tscal dt_next = compute_dt_cfl();
1992
1993 // Ensure dt doesn't grow too fast (max 2x per step), but allow any value if dt was 0
1994 if (dt > Tscal(0)) {
1995 dt_next = sham::min(dt_next, Tscal(2) * dt);
1996 }
1997
1998 // Copy EOS fields to patchdata for persistence and VTKDump access
2000
2001 // Cleanup for next iteration
2002 reset_neighbors_cache();
2003 reset_presteps_rint();
2004 clear_merged_pos_trees();
2005 reset_merge_ghosts_fields();
2006 storage.merged_xyzh.reset();
2007 clear_ghost_cache();
2008 reset_serial_patch_tree();
2009 reset_ghost_handler();
2010 storage.ghost_layout.reset();
2011
2012 // Update time
2013 set_time(t_current + dt);
2014 set_next_dt(dt_next);
2015
2016 solve_logs.step_count++;
2017
2018 tstep.stop();
2019
2020 // Prepare timing log
2021 TimestepLog log;
2022 log.rank = shamcomm::world_rank();
2023 log.rate = Tscal(Npart_all) / tstep.elapsed_sec();
2024 log.npart = Npart_all;
2025 log.tcompute = tstep.elapsed_sec();
2026
2027 return log;
2028}
2029
2030// Template instantiations
2031using namespace shammath;
2032
2033// M-spline kernels (Monaghan)
2037
2038// Wendland kernels (C2, C4, C6) - recommended for GSPH (Inutsuka 2002)
Constants for field names in GSPH solver, organized by physics mode.
constexpr const char * duint
Time derivative of internal energy du/dt.
constexpr const char * axyz
3-acceleration field
constexpr const char * uint
Specific internal energy u.
constexpr const char * vxyz
3-velocity field
constexpr const char * pos_merged
Position merged references (for h-iteration).
constexpr const char * old_axyz
Old acceleration (for corrector step).
constexpr const char * xyz
Position field (3D coordinates).
constexpr const char * density
Density \rho (derived from h).
constexpr const char * eps_h
Epsilon h references (for h-iteration convergence).
constexpr const char * soundspeed
Sound speed c_s (derived from EOS).
constexpr const char * pressure
Pressure P (derived from EOS).
constexpr const char * hpart
Smoothing length field.
constexpr const char * neigh_cache
Neighbor cache.
constexpr const char * omega
Grad-h correction factor \Omega.
Solvergraph nodes finding the SPH ghost interfaces, one node per boundary condition.
shambase::DistributedData< PatchDataFieldRef< T > > DDPatchDataFieldRef
Alias for a DistributedData of PatchDataFieldRefs.
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 LoopSmoothingLengthIter module for looping over the smoothing length iteration until con...
Header file describing a Node Instance.
sycl::queue & get_compute_queue(u32 id=0)
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.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
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.
A SYCL queue associated with a device and a context.
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
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.
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
typename sph::BasicSPHGhostHandler< Tvec >::InterfaceBuildInfos InterfaceBuildInfos
The GSPH Solver class.
Definition Solver.hpp:71
void compute_density()
Compute SPH-summation density for GSPH.
Definition Solver.cpp:638
void copy_eos_to_patchdata()
Copy EOS fields from solvergraph to patchdata for persistence.
Definition Solver.cpp:1398
void compute_gradients()
Compute gradients for MUSCL reconstruction.
Definition Solver.cpp:1460
TimestepLog evolve_once()
Definition Solver.cpp:1894
void update_derivs()
Update derivatives using GSPH Riemann solver.
Definition Solver.cpp:1708
Tscal compute_dt_cfl()
Compute CFL timestep constraint.
Definition Solver.cpp:1716
void build_ghost_cache(Tscal time_val)
Definition Solver.cpp:172
GSPH derivative update module.
void update_derivs()
Update all derivatives using GSPH Riemann solver approach.
Ghost interface finder for free boundaries: only direct neighbouring patches.
Ghost interface finder for periodic boundaries: neighbouring patches, including through the 26 period...
Ghost interface finder for shearing periodic boundaries.
Utility class used to move the objects between patches.
ComputeField< T > make_compute_field(std::string new_name, u32 nvar)
create a compute field and init it to zeros
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
virtual void ensure_sizes(const shambase::DistributedData< u32 > &sizes)
Ensure that the sizes of the patches in the field match the given sizes (Can resize the underlying fi...
Definition Field.hpp:92
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 Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
A data structure representing a Karras Radix Tree Field.
constexpr Unit h
Planck constant.
Class holding the value of numerous constants generated from the following source.
This header file contains utility functions related to exception handling in the code.
MPI string gather / allgather helpers (declarations; implementations in shamalgs/src/collective/gathe...
Configuration for the Godunov SPH (GSPH) solver.
GSPH Solver class.
GSPH derivative update module.
VTK dump module for GSPH solver.
T inv_sat_positive(T v, T minvsat=T{1e-9}, T satval=T{0.}) noexcept
inverse saturated (positive numbers only)
Definition math.hpp:841
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.
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
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 main framework
Definition __init__.py:1
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
sph kernels
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
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
T lower
Lower bound of the AABB.
Definition AABB.hpp:104
T upper
Upper bound of the AABB.
Definition AABB.hpp:105
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
Functions related to the MPI communicator.