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
20#include "shambase/memory.hpp"
27
29
30 template<class Tvec, template<class> class SPHKernel>
32 public:
33 using Tscal = shambase::VecComponent<Tvec>;
35
36 using Solver = Solver<Tvec, SPHKernel>;
37
39 Solver &solver;
40 ShamrockCtx &ctx;
41
43 : model(model), ctx(model.ctx), solver(model.solver) {};
44
45 struct result {
46 Tvec barycenter;
47 Tscal mass_disc;
48 };
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 if (!solver.storage.sinks.is_empty()) {
98 for (auto &sink : solver.storage.sinks.get()) {
99 Tvec star_xyz = sink.pos;
100 Tscal star_mass = sink.mass;
101
102 tot_barycenter += star_xyz * star_mass;
103 tot_mass += star_mass;
104 }
105 }
106 tot_barycenter /= tot_mass;
107 return result{tot_barycenter, tot_mass_disc};
108 }
109 };
110} // namespace shammodels::sph::modules
MPI scheduler.
std::uint32_t u32
32 bit unsigned integer
The MPI scheduler.
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:55
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
A class that references multiple buffers or similar objects.
Patch object that contain generic patch information.
Definition Patch.hpp:33