Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AnalysisAngularMomentum.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
18
19#include "shambase/memory.hpp"
26#include <shambackends/sycl.hpp>
27
29
30 template<class Tvec, template<class> class SPHKernel>
31 class AnalysisAngularMomentum {
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 AnalysisAngularMomentum(Model<Tvec, SPHKernel> &model)
43 : model(model), ctx(model.ctx), solver(model.solver) {};
44
45 auto get_angular_momentum() -> Tvec {
46 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
47 auto dev_sched_ptr = shamsys::instance::get_compute_scheduler_ptr();
48 sham::DeviceQueue &q = shambase::get_check_ref(dev_sched_ptr).get_queue();
49
50 const u32 ivxyz = sched.pdl_old().template get_field_idx<Tvec>("vxyz");
51 const u32 ixyz = sched.pdl_old().template get_field_idx<Tvec>("xyz");
52 const Tscal pmass = solver.solver_config.gpart_mass;
53
54 Tvec angular_momentum = {};
55
56 sham::DeviceBuffer<Tvec> angular_momentum_part(0, dev_sched_ptr);
57
58 sched.for_each_patchdata_nonempty(
60 u32 len = pdat.get_obj_cnt();
61
62 angular_momentum_part.resize(len);
63
64 sham::DeviceBuffer<Tvec> &xyz_buf = pdat.get_field_buf_ref<Tvec>(ixyz);
65 sham::DeviceBuffer<Tvec> &vxyz_buf = pdat.get_field_buf_ref<Tvec>(ivxyz);
66
68 q,
69 sham::MultiRef{xyz_buf, vxyz_buf},
70 sham::MultiRef{angular_momentum_part},
71 len,
72 [pmass](
73 u32 i,
74 const Tvec *__restrict xyz,
75 const Tvec *__restrict vxyz,
76 Tvec *__restrict angular_momentum_part) {
77 angular_momentum_part[i] = pmass * sycl::cross(xyz[i], vxyz[i]);
78 });
79
80 angular_momentum
81 += shamalgs::primitives::sum(dev_sched_ptr, angular_momentum_part, 0, len);
82 });
83
84 Tvec tot_angular_momentum = shamalgs::collective::allreduce_sum(angular_momentum);
85
86 auto &mass = get_sink_mass<Tvec>(sched.synchronized_data);
87 if (!mass.empty()) {
88 auto &pos = get_sink_pos<Tvec>(sched.synchronized_data);
89 auto &vel = get_sink_vel<Tvec>(sched.synchronized_data);
90 auto &ang = get_sink_angular_momentum<Tvec>(sched.synchronized_data);
91 for (size_t i = 0; i < mass.size(); i++) {
92 tot_angular_momentum += mass[i] * sycl::cross(pos[i], vel[i]) + ang[i];
93 }
94 }
95
96 return tot_angular_momentum;
97 }
98 };
99} // 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).
void resize(size_t new_size, bool keep_data=true)
Resizes the buffer to a given size.
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