Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
IterateSmoothingLengthDensity.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
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
48 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
49
50 static constexpr Tscal Rkern = SPHKernel::Rkern;
51
53 dev_sched,
54 sham::DDMultiRef{neigh_cache, positions, old_h},
55 sham::DDMultiRef{new_h, eps_h},
56 thread_counts,
57 [gpart_mass = this->gpart_mass,
58 h_evol_max = this->h_evol_max,
59 h_evol_iter_max = this->h_evol_iter_max,
60 epsilon_h = this->epsilon_h](
61 u32 id_a,
62 auto ploop_ptrs,
63 const Tvec *__restrict r,
64 const Tscal *__restrict h_old,
65 Tscal *__restrict h_new,
66 Tscal *__restrict eps) {
67 // attach the neighbor looper on the cache
68 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
69
70 Tscal part_mass = gpart_mass;
71 Tscal h_max_tot_max_evol = h_evol_max;
72 Tscal h_max_evol_p = h_evol_iter_max;
73 Tscal h_max_evol_m = 1 / h_evol_iter_max;
74
75 if (eps[id_a] > epsilon_h) {
76
77 Tvec xyz_a = r[id_a]; // could be recovered from lambda
78
79 Tscal h_a = h_new[id_a];
80 Tscal dint = h_a * h_a * Rkern * Rkern;
81
82 Tscal rho_sum = 0;
83 Tscal sumdWdh = 0;
84
85 particle_looper.for_each_object(id_a, [&](u32 id_b) {
86 Tvec dr = xyz_a - r[id_b];
87 Tscal rab2 = sycl::dot(dr, dr);
88
89 if (rab2 > dint) {
90 return; // early return if the particle is too far away
91 }
92
93 Tscal rab = sycl::sqrt(rab2);
94
95 rho_sum += part_mass * SPHKernel::W_3d(rab, h_a);
96 sumdWdh += part_mass * SPHKernel::dhW_3d(rab, h_a);
97 });
98
99 using namespace shamrock::sph;
100
101 Tscal rho_ha = rho_h(part_mass, h_a, SPHKernel::hfactd);
102 Tscal new_h = newton_iterate_new_h(rho_ha, rho_sum, sumdWdh, h_a);
103
104 if (new_h < h_a * h_max_evol_m)
105 new_h = h_max_evol_m * h_a;
106 if (new_h > h_a * h_max_evol_p)
107 new_h = h_max_evol_p * h_a;
108
109 Tscal ha_0 = h_old[id_a];
110
111 if (new_h < ha_0 * h_max_tot_max_evol) {
112 h_new[id_a] = new_h;
113 eps[id_a] = sycl::fabs(new_h - h_a) / ha_0;
114 } else {
115 h_new[id_a] = ha_0 * h_max_tot_max_evol;
116 eps[id_a] = -1;
117 }
118 }
119 });
120}
121
122template<class Tvec, class SPHKernel>
124 std::string tex = R"tex(
125 Iterate smoothing length and density
126
127 \begin{align}
128 \rho_i &= \sum_{j \in \mathcal{N}_i} m_j W(r_{ij}, h_i) \\
129 \frac{\partial \rho_i}{\partial h_i} &= \sum_{j \in \mathcal{N}_i} m_j \frac{\partial W}{\partial h}(r_{ij}, h_i) \\
130 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}} \\
131 \epsilon_i &= \frac{|h_i^{\rm new} - h_i|}{h_i^{\rm old}}
132 \end{align}
133
134 where:
135 \begin{itemize}
136 \item $\mathcal{N}_i$ is the set of neighbors of particle $i$
137 \item $W(r, h)$ is the SPH kernel function
138 \item $\rho_h(m, h) = m \left(\frac{h_{\rm fact}}{h}\right)^3$ is the target density
139 \item $h_{\rm fact} = {hfact}$ is the kernel factor
140 \item $R_{\rm kern} = {Rkern}$ is the kernel radius
141 \end{itemize}
142
143 Input: ${sizes}$, ${neigh_cache}$, ${positions}$, ${old_h}$
144 Output: ${new_h}$, ${eps_h}$
145 )tex";
146
147 replace_edges_tex_symbols(tex);
148
149 shambase::replace_all(tex, "{hfact}", sham::format("{}", SPHKernel::hfactd));
150 shambase::replace_all(tex, "{Rkern}", sham::format("{}", SPHKernel::Rkern));
151
152 return tex;
153}
154
158
Declares the IterateSmoothingLengthDensity module for iterating smoothing length based on the SPH den...
std::uint32_t u32
32 bit unsigned integer
virtual std::string _impl_get_tex() const
get the tex of the node
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.