Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
IterateSmoothingLengthDensityNeighLim.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
19#include "shamcomm/logs.hpp"
24
25using namespace shammodels::sph::modules;
26
27template<class Tvec, class SPHKernel>
29 StackEntry stack_loc{};
30
31 auto edges = get_edges();
32
33 auto &thread_counts = edges.sizes.indexes;
34
35 edges.neigh_cache.check_sizes(thread_counts);
36 edges.positions.check_sizes(thread_counts);
37 edges.old_h.check_sizes(thread_counts);
38 edges.new_h.ensure_sizes(thread_counts);
39 edges.eps_h.ensure_sizes(thread_counts);
40 edges.was_limited.ensure_sizes(thread_counts);
41
42 auto &neigh_cache = edges.neigh_cache.neigh_cache;
43 auto &positions = edges.positions.get_spans();
44 auto &old_h = edges.old_h.get_spans();
45 auto &new_h = edges.new_h.get_spans();
46 auto &eps_h = edges.eps_h.get_spans();
47 auto &was_limited = edges.was_limited.get_spans();
48
49 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
50
51 static constexpr Tscal Rkern = SPHKernel::Rkern;
52
54 dev_sched,
55 sham::DDMultiRef{neigh_cache, positions, old_h},
56 sham::DDMultiRef{new_h, eps_h, was_limited},
57 thread_counts,
58 [gpart_mass = this->gpart_mass,
59 h_evol_max = this->h_evol_max,
60 h_evol_iter_max = this->h_evol_iter_max,
61 trigger_threshold = this->trigger_threshold](
62 u32 id_a,
63 auto ploop_ptrs,
64 const Tvec *__restrict r,
65 const Tscal *__restrict h_old,
66 Tscal *__restrict h_new,
67 Tscal *__restrict eps,
68 u32 *__restrict was_limited) {
69 // attach the neighbor looper on the cache
70 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
71
72 Tscal part_mass = gpart_mass;
73 Tscal h_max_tot_max_evol = h_evol_max;
74 Tscal h_max_evol_p = h_evol_iter_max;
75 Tscal h_max_evol_m = 1 / h_evol_iter_max;
76
77 // TODO: make this tolerance configurable
78 if (eps[id_a] > 1e-6) {
79
80 Tvec xyz_a = r[id_a]; // could be recovered from lambda
81
82 Tscal h_a = h_new[id_a];
83 Tscal dint = h_a * h_a * Rkern * Rkern;
84
85 Tscal rho_sum = 0;
86 Tscal sumdWdh = 0;
87
88 u32 count_within = 0;
89 u32 count_within_next = 0;
90
91 particle_looper.for_each_object(id_a, [&](u32 id_b) {
92 Tvec dr = xyz_a - r[id_b];
93 Tscal rab2 = sycl::dot(dr, dr);
94
95 if (rab2 <= dint * h_max_evol_p * h_max_evol_p) {
96 count_within_next++;
97 }
98
99 if (rab2 > dint) {
100 return; // early return if the particle is too far away
101 }
102
103 Tscal rab = sycl::sqrt(rab2);
104
105 rho_sum += part_mass * SPHKernel::W_3d(rab, h_a);
106 sumdWdh += part_mass * SPHKernel::dhW_3d(rab, h_a);
107
108 count_within++;
109 });
110
111 using namespace shamrock::sph;
112
113 Tscal rho_ha = rho_h(part_mass, h_a, SPHKernel::hfactd);
114 Tscal new_h = newton_iterate_new_h(rho_ha, rho_sum, sumdWdh, h_a);
115
116 bool exceed_inner_threshold = count_within > trigger_threshold;
117 bool exceed_outer_threshold = count_within_next > trigger_threshold;
118
119 if (exceed_inner_threshold) {
120 h_new[id_a] = h_max_evol_m * h_a;
121 eps[id_a] = 0;
122 was_limited[id_a] = 1;
123 return;
124 }
125
126 if (exceed_outer_threshold && new_h > h_a) {
127 eps[id_a] = 0;
128 was_limited[id_a] = 1;
129 return;
130 }
131
132 if (new_h < h_a * h_max_evol_m)
133 new_h = h_max_evol_m * h_a;
134 if (new_h > h_a * h_max_evol_p)
135 new_h = h_max_evol_p * h_a;
136
137 Tscal ha_0 = h_old[id_a];
138
139 if (new_h < ha_0 * h_max_tot_max_evol) {
140 h_new[id_a] = new_h;
141 eps[id_a] = sycl::fabs(new_h - h_a) / ha_0;
142 } else {
143 h_new[id_a] = ha_0 * h_max_tot_max_evol;
144 eps[id_a] = -1;
145 }
146 was_limited[id_a] = 0;
147 }
148 });
149}
150
151template<class Tvec, class SPHKernel>
153 auto sizes = get_ro_edge_base(0).get_tex_symbol();
154 auto neigh_cache = get_ro_edge_base(1).get_tex_symbol();
155 auto positions = get_ro_edge_base(2).get_tex_symbol();
156 auto old_h = get_ro_edge_base(3).get_tex_symbol();
157 auto new_h = get_rw_edge_base(0).get_tex_symbol();
158 auto eps_h = get_rw_edge_base(1).get_tex_symbol();
159
160 std::string tex = R"tex(
161 Iterate smoothing length and density
162
163 \begin{align}
164 \rho_i &= \sum_{j \in \mathcal{N}_i} m_j W(r_{ij}, h_i) \\
165 \frac{\partial \rho_i}{\partial h_i} &= \sum_{j \in \mathcal{N}_i} m_j \frac{\partial W}{\partial h}(r_{ij}, h_i) \\
166 h_i^{\rm new} &= h_i - \frac{\rho_i - \rho_h(m_i, h_i)}{\frac{\partial \rho_i}{\partial h_i} + \frac{3\rho_h(m_i, h_i)}{h_i}} \\
167 \epsilon_i &= \frac{|h_i^{\rm new} - h_i|}{h_i^{\rm old}}
168 \end{align}
169
170 where:
171 \begin{itemize}
172 \item $\mathcal{N}_i$ is the set of neighbors of particle $i$
173 \item $W(r, h)$ is the SPH kernel function
174 \item $\rho_h(m, h) = m \left(\frac{h_{\rm fact}}{h}\right)^3$ is the target density
175 \item $h_{\rm fact} = {hfact}$ is the kernel factor
176 \item $R_{\rm kern} = {Rkern}$ is the kernel radius
177 \end{itemize}
178
179 Input: ${sizes}$, ${neigh_cache}$, ${positions}$, ${old_h}$
180 Output: ${new_h}$, ${eps_h}$
181 )tex";
182
183 shambase::replace_all(tex, "{sizes}", sizes);
184 shambase::replace_all(tex, "{neigh_cache}", neigh_cache);
185 shambase::replace_all(tex, "{positions}", positions);
186 shambase::replace_all(tex, "{old_h}", old_h);
187 shambase::replace_all(tex, "{new_h}", new_h);
188 shambase::replace_all(tex, "{eps_h}", eps_h);
189 shambase::replace_all(tex, "{hfact}", shambase::format("{}", SPHKernel::hfactd));
190 shambase::replace_all(tex, "{Rkern}", shambase::format("{}", SPHKernel::Rkern));
191
192 return tex;
193}
194
195template class shammodels::sph::modules::
196 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::M4<f64>>;
197template class shammodels::sph::modules::
198 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::M6<f64>>;
199template class shammodels::sph::modules::
200 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::M8<f64>>;
201
202template class shammodels::sph::modules::
203 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::C2<f64>>;
204template class shammodels::sph::modules::
205 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::C4<f64>>;
206template class shammodels::sph::modules::
207 IterateSmoothingLengthDensityNeighLim<f64_3, shammath::C6<f64>>;
Declares the IterateSmoothingLengthDensityNeighLim module for iterating smoothing length based on the...
std::uint32_t u32
32 bit unsigned integer
void distributed_data_kernel_call(sham::DeviceScheduler_ptr dev_sched, RefIn in, RefOut in_out, const shambase::DistributedData< index_t > &thread_counts, Functor &&func)
A variant of sham::kernel_call for distributed data.
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:183
namespace for the sph model modules
sph kernels
This file contains the definition for the stacktrace related functionality.
A variant of sham::MultiRef for distributed data.