Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AnalysisBarycenter.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19
20#include "shambase/memory.hpp"
27
29
30 template<class Tvec, template<class> class SPHKernel>
31 class AnalysisBarycenter {
32 public:
33 using Tscal = shambase::VecComponent<Tvec>;
34 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
35
36 using Solver = Solver<Tvec, SPHKernel>;
37
39 Solver &solver;
40 ShamrockCtx &ctx;
41
42 AnalysisBarycenter(Model<Tvec, SPHKernel> &model)
43 : model(model), ctx(model.ctx), solver(model.solver) {};
44
45 struct result {
46 Tvec barycenter;
47 Tscal mass_disc;
48 };
49
52 auto get_barycenter() -> result {
53 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
54 auto dev_sched_ptr = shamsys::instance::get_compute_scheduler_ptr();
55 sham::DeviceQueue &q = shambase::get_check_ref(dev_sched_ptr).get_queue();
56
57 const u32 ixyz = sched.pdl_old().template get_field_idx<Tvec>("xyz");
58 const Tscal pmass = solver.solver_config.gpart_mass;
59
60 Tvec barycenter = {0, 0, 0}; // Not really barycenter per se but Mdisc * barycenter
61 Tscal mass_disc = 0;
62
63 sched.for_each_patchdata_nonempty(
65 // auto &xyz_buf = pdat.get_field_buf_ref<Tvec>(ixyz);
66 // u32 len = pdat.get_obj_cnt();
67 // {
68 // auto acc_xyz = xyz_buf.copy_to_stdvec();
69 // for (u32 i = 0; i < len; i++) {
70 // Tvec xyz = acc_xyz[i];
71
72 // barycenter += pmass * xyz;
73 // mass_disc += pmass;
74 // }
75 // }
76 u32 len = pdat.get_obj_cnt();
77 mass_disc += pmass * len;
78
79 sham::DeviceBuffer<Tvec> pm(len, dev_sched_ptr);
80 sham::DeviceBuffer<Tvec> &xyz_buf = pdat.get_field_buf_ref<Tvec>(ixyz);
81
83 q,
84 sham::MultiRef{xyz_buf},
86 len,
87 [pmass](u32 i, const Tvec *__restrict xyz, Tvec *__restrict pm) {
88 pm[i] = pmass * xyz[i];
89 });
90 barycenter += shamalgs::primitives::sum(dev_sched_ptr, pm, 0, len);
91 });
92
93 Tvec tot_barycenter = shamalgs::collective::allreduce_sum(barycenter);
94 Tscal tot_mass_disc = shamalgs::collective::allreduce_sum(mass_disc);
95 Tscal tot_mass = tot_mass_disc;
96
97 auto &pos = get_sink_pos<Tvec>(sched.synchronized_data);
98 auto &mass = get_sink_mass<Tvec>(sched.synchronized_data);
99 if (!pos.empty()) {
100 for (size_t i = 0; i < pos.size(); i++) {
101 tot_barycenter += pos[i] * mass[i];
102 tot_mass += mass[i];
103 }
104 }
105 tot_barycenter /= tot_mass;
106 return result{.barycenter = tot_barycenter, .mass_disc = tot_mass_disc};
107 }
108 };
109} // namespace shammodels::sph::modules
MPI scheduler.
std::uint32_t u32
32 bit unsigned integer
The MPI scheduler.
SynchronizedData synchronized_data
data that is synchroneous across all ranks
A buffer allocated in USM (Unified Shared Memory).
A SYCL queue associated with a device and a context.
The shamrock SPH model.
Definition Model.hpp:56
PatchDataLayer container class, the layout is described in patchdata_layout.
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.
T sum(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of elements in a device buffer within a specified range.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:110
namespace for the sph model modules
std::vector< Tvec > & get_sink_pos(shamrock::solvergraph::SolverGraphSerializable &sync)
Named SoA getters (edges must already exist; call ensure_sink_edges first). Prefer these when a funct...
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Patch object that contain generic patch information.
Definition Patch.hpp:33