Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AnalysisEnergyKinetic.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#include "shambase/memory.hpp"
25
27
28 template<class Tvec, template<class> class SPHKernel>
30 public:
31 using Tscal = shambase::VecComponent<Tvec>;
33
35
37 Solver &solver;
38 ShamrockCtx &ctx;
39
41 : model(model), ctx(model.ctx), solver(model.solver) {};
42
43 auto get_kinetic_energy() -> Tscal {
44 PatchScheduler &sched = shambase::get_check_ref(ctx.sched);
45 auto dev_sched_ptr = shamsys::instance::get_compute_scheduler_ptr();
46 sham::DeviceQueue &q = shambase::get_check_ref(dev_sched_ptr).get_queue();
47
48 const u32 ivxyz = sched.pdl_old().template get_field_idx<Tvec>("vxyz");
49 const Tscal pmass = solver.solver_config.gpart_mass;
50
51 Tscal ekin = 0;
52
53 sched.for_each_patchdata_nonempty(
55 u32 len = pdat.get_obj_cnt();
56
57 sham::DeviceBuffer<Tscal> ekin_part(len, dev_sched_ptr);
58 sham::DeviceBuffer<Tvec> &vxyz_buf = pdat.get_field_buf_ref<Tvec>(ivxyz);
59
61 q,
62 sham::MultiRef{vxyz_buf},
63 sham::MultiRef{ekin_part},
64 len,
65 [pmass](u32 i, const Tvec *__restrict vxyz, Tscal *__restrict ekin_part) {
66 ekin_part[i] = Tscal{0.5} * pmass * sham::dot(vxyz[i], vxyz[i]);
67 });
68
69 ekin += shamalgs::primitives::sum(dev_sched_ptr, ekin_part, 0, len);
70 });
71
72 Tscal tot_ekin = shamalgs::collective::allreduce_sum(ekin);
73
74 if (!solver.storage.sinks.is_empty()) {
75 for (auto &sink : solver.storage.sinks.get()) {
76 tot_ekin += Tscal{0.5} * sink.mass * sham::dot(sink.velocity, sink.velocity);
77 }
78 }
79
80 return tot_ekin;
81 }
82 };
83} // 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
The shamrock SPH model.
Definition Solver.hpp:61
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.
Tscal gpart_mass
The mass of each gas particle.
Patch object that contain generic patch information.
Definition Patch.hpp:33