Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ComputeNeighStats.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
21
23
24 template<class Tvec>
26 auto edges = get_edges();
27
28 const shambase::DistributedData<u32> &counts = edges.part_counts.indexes;
29
30 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
31
32 Tscal Rker2 = kernel_radius * kernel_radius;
33
34 counts.for_each([&](u64 id, u32 count) {
35 const shamrock::tree::ObjectCache &pcache = edges.neigh_cache.get_cache(id);
36
37 sham::DeviceBuffer<u32> neigh_count_all(
38 count, shamsys::instance::get_compute_scheduler_ptr());
39 sham::DeviceBuffer<u32> neigh_count_true(
40 count, shamsys::instance::get_compute_scheduler_ptr());
41
43 q,
45 pcache, edges.xyz.get_spans().get(id), edges.hpart.get_spans().get(id)},
46 sham::MultiRef{neigh_count_true, neigh_count_all},
47 count,
48 [&, Rker2](
49 u32 id_a,
50 const auto ploop_ptrs,
51 const Tvec *xyz,
52 const Tscal *hpart,
53 u32 *neigh_count_true,
54 u32 *neigh_count_all) {
55 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
56
57 Tscal h_a = hpart[id_a];
58 Tvec xyz_a = xyz[id_a];
59
60 u32 cnt_all = 0;
61 u32 cnt_true = 0;
62 particle_looper.for_each_object(id_a, [&](u32 id_b) {
63 cnt_all++;
64
65 Tvec r_ab = xyz_a - xyz[id_b];
66 Tscal rab2 = sycl::dot(r_ab, r_ab);
67 Tscal h_b = hpart[id_b];
68
69 if (rab2 > h_a * h_a * Rker2 && rab2 > h_b * h_b * Rker2) {
70 return;
71 }
72
73 cnt_true++;
74 });
75
76 neigh_count_all[id_a] = cnt_all;
77 neigh_count_true[id_a] = cnt_true;
78 });
79
80 {
81 std::vector<u32> neigh_cnt_vec = neigh_count_true.copy_to_stdvec();
82
83 double max = *std::max_element(neigh_cnt_vec.begin(), neigh_cnt_vec.end());
84 double min = *std::min_element(neigh_cnt_vec.begin(), neigh_cnt_vec.end());
85 double mean = std::accumulate(neigh_cnt_vec.begin(), neigh_cnt_vec.end(), 0.0)
86 / neigh_cnt_vec.size();
87 double stddev = std::sqrt(
88 std::accumulate(
89 neigh_cnt_vec.begin(),
90 neigh_cnt_vec.end(),
91 0.0,
92 [mean](double acc, double x) {
93 return acc + (x - mean) * (x - mean);
94 })
95 / neigh_cnt_vec.size());
96 logger::raw_ln("(true) max, min, mean, stddev =", max, min, mean, stddev);
97 }
98
99 {
100 std::vector<u32> neigh_cnt_vec = neigh_count_all.copy_to_stdvec();
101
102 double max = *std::max_element(neigh_cnt_vec.begin(), neigh_cnt_vec.end());
103 double min = *std::min_element(neigh_cnt_vec.begin(), neigh_cnt_vec.end());
104 double mean = std::accumulate(neigh_cnt_vec.begin(), neigh_cnt_vec.end(), 0.0)
105 / neigh_cnt_vec.size();
106 double stddev = std::sqrt(
107 std::accumulate(
108 neigh_cnt_vec.begin(),
109 neigh_cnt_vec.end(),
110 0.0,
111 [mean](double acc, double x) {
112 return acc + (x - mean) * (x - mean);
113 })
114 / neigh_cnt_vec.size());
115 logger::raw_ln("(all) max, min, mean, stddev =", max, min, mean, stddev);
116 }
117 });
118 }
119
120 template<class Tvec>
122 return "TODO";
123 }
124
125} // namespace shammodels::sph::modules
126
A module to compute and display statistics on neighbor counts for SPH particles.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
A SYCL queue associated with a device and a context.
DeviceQueue & get_queue(u32 id=0)
Get a reference to a DeviceQueue.
Represents a collection of objects distributed across patches identified by a u64 id.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
virtual std::string _impl_get_tex() const
get the tex of the node
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.
namespace for the sph model modules
A class that references multiple buffers or similar objects.