Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
IterateSmoothingLengthDensityBisectingNR.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
16
18#include "shambase/string.hpp"
20#include "shamcomm/logs.hpp"
25
26using namespace shammodels::sph::modules;
27
28template<class Tvec, class SPHKernel>
30 StackEntry stack_loc{};
31
32 auto edges = get_edges();
33
34 auto &thread_counts = edges.sizes.indexes;
35
36 edges.neigh_cache.check_sizes(thread_counts);
37 edges.positions.check_sizes(thread_counts);
38 edges.old_h.check_sizes(thread_counts);
39 edges.new_h.check_sizes(thread_counts);
40 edges.eps_h.check_sizes(thread_counts);
41 edges.h_bisect_lo.check_sizes(thread_counts);
42 edges.h_bisect_hi.check_sizes(thread_counts);
43
44 auto &neigh_cache = edges.neigh_cache.neigh_cache;
45 auto &positions = edges.positions.get_spans();
46 auto &old_h = edges.old_h.get_spans();
47 auto &new_h = edges.new_h.get_spans();
48 auto &eps_h = edges.eps_h.get_spans();
49 auto &h_bisect_lo = edges.h_bisect_lo.get_spans();
50 auto &h_bisect_hi = edges.h_bisect_hi.get_spans();
51
52 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
53
54 static constexpr Tscal Rkern = SPHKernel::Rkern;
55
57 dev_sched,
58 sham::DDMultiRef{neigh_cache, positions, old_h},
59 sham::DDMultiRef{new_h, eps_h, h_bisect_lo, h_bisect_hi},
60 thread_counts,
61 [gpart_mass = this->gpart_mass,
62 h_evol_max = this->h_evol_max,
63 h_evol_iter_max = this->h_evol_iter_max,
64 epsilon_h = this->epsilon_h](
65 u32 id_a,
66 auto ploop_ptrs,
67 const Tvec *__restrict r,
68 const Tscal *__restrict h_old,
69 Tscal *__restrict h_new,
70 Tscal *__restrict eps,
71 Tscal *__restrict h_lo,
72 Tscal *__restrict h_hi) {
73 // attach the neighbor looper on the cache
74 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
75
76 Tscal part_mass = gpart_mass;
77 Tscal h_max_tot_max_evol = h_evol_max;
78 Tscal h_max_evol_p = h_evol_iter_max;
79 Tscal h_max_evol_m = 0.5;
80
81 if (eps[id_a] > epsilon_h) {
82
83 Tvec xyz_a = r[id_a]; // could be recovered from lambda
84
85 Tscal h_a = h_new[id_a];
86 Tscal dint = h_a * h_a * Rkern * Rkern;
87
88 Tscal rho_sum = 0;
89 Tscal sumdWdh = 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) {
96 return; // early return if the particle is too far away
97 }
98
99 Tscal rab = sycl::sqrt(rab2);
100
101 rho_sum += part_mass * SPHKernel::W_3d(rab, h_a);
102 sumdWdh += part_mass * SPHKernel::dhW_3d(rab, h_a);
103 });
104
105 using namespace shamrock::sph;
106
107 Tscal rho_ha = rho_h(part_mass, h_a, SPHKernel::hfactd);
108 Tscal f_iter = rho_sum - rho_ha;
109 Tscal df_iter = sumdWdh + 3 * rho_ha / h_a;
110 Tscal new_h_nr = h_a - f_iter / df_iter;
111
112 Tscal lo = h_lo[id_a];
113 Tscal hi = h_hi[id_a];
114
115 bool accept_nr = lo < new_h_nr && new_h_nr < hi;
116
117 if (f_iter < 0) {
118 lo = h_a;
119 } else {
120 hi = h_a;
121 }
122
123 Tscal new_h;
124 if (accept_nr) {
125 new_h = new_h_nr;
126 } else {
127 new_h = (lo + hi) / 2;
128 }
129
130 if (new_h < h_a * h_max_evol_m)
131 new_h = h_max_evol_m * h_a;
132 if (new_h > h_a * h_max_evol_p)
133 new_h = h_max_evol_p * h_a;
134
135 h_lo[id_a] = lo;
136 h_hi[id_a] = hi;
137
138 Tscal ha_0 = h_old[id_a];
139
140 if (new_h < ha_0 * h_max_tot_max_evol) {
141 h_new[id_a] = new_h;
142 eps[id_a] = sycl::fabs(new_h - h_a) / ha_0;
143 } else {
144 h_new[id_a] = ha_0 * h_max_tot_max_evol;
145 eps[id_a] = -1;
146 }
147 }
148 });
149}
150
151template<class Tvec, class SPHKernel>
153 std::string tex = R"tex(
154 Iterate smoothing length and density (Newton-Raphson bracketed by bisection)
155
156 \begin{align}
157 \rho_i &= \sum_{j \in \mathcal{N}_i} m_j W(r_{ij}, h_i) \\
158 \frac{\partial \rho_i}{\partial h_i} &= \sum_{j \in \mathcal{N}_i} m_j \frac{\partial W}{\partial h}(r_{ij}, h_i) \\
159 f_i &= \rho_i - \rho_h(m_i, h_i) \\
160 h_i^{\rm NR} &= h_i - \frac{f_i}{\frac{\partial \rho_i}{\partial h_i} + \frac{3\rho_h(m_i, h_i)}{h_i}}
161 \end{align}
162
163 The bracket $[h_i^{\rm lo}, h_i^{\rm hi}]$ is tightened from the sign of $f_i$
164 (lower bound updated if $f_i < 0$, upper bound otherwise), and the next iterate is
165 \begin{align}
166 h_i^{\rm new} &= \begin{cases}
167 h_i^{\rm NR} & \text{if } h_i^{\rm lo} < h_i^{\rm NR} < h_i^{\rm hi} \\
168 \frac{h_i^{\rm lo} + h_i^{\rm hi}}{2} & \text{otherwise}
169 \end{cases} \\
170 \epsilon_i &= \frac{|h_i^{\rm new} - h_i|}{h_i^{\rm old}}
171 \end{align}
172
173 where:
174 \begin{itemize}
175 \item $\mathcal{N}_i$ is the set of neighbors of particle $i$
176 \item $W(r, h)$ is the SPH kernel function
177 \item $\rho_h(m, h) = m \left(\frac{h_{\rm fact}}{h}\right)^3$ is the target density
178 \item $h_{\rm fact} = {hfact}$ is the kernel factor
179 \item $R_{\rm kern} = {Rkern}$ is the kernel radius
180 \end{itemize}
181
182 Input: ${sizes}$, ${neigh_cache}$, ${positions}$, ${old_h}$
183 Input/Output: ${h_bisect_lo}$, ${h_bisect_hi}$
184 Output: ${new_h}$, ${eps_h}$
185 )tex";
186
187 replace_edges_tex_symbols(tex);
188
189 shambase::replace_all(tex, "{hfact}", sham::format("{}", SPHKernel::hfactd));
190 shambase::replace_all(tex, "{Rkern}", sham::format("{}", SPHKernel::Rkern));
191
192 return tex;
193}
194
195template class shammodels::sph::modules::
196 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::M4<f64>>;
197template class shammodels::sph::modules::
198 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::M6<f64>>;
199template class shammodels::sph::modules::
200 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::M8<f64>>;
201
202template class shammodels::sph::modules::
203 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::C2<f64>>;
204template class shammodels::sph::modules::
205 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::C4<f64>>;
206template class shammodels::sph::modules::
207 IterateSmoothingLengthDensityBisectingNR<f64_3, shammath::C6<f64>>;
Declares the IterateSmoothingLengthDensityBisectingNR module for iterating smoothing length based on ...
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:106
namespace for the sph model modules
sph kernels
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
A variant of sham::MultiRef for distributed data.