Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
forces.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
29
31#include "shambackends/math.hpp"
32#include "shambackends/sycl.hpp"
36
37namespace shammodels::gsph {
38
39 // Note: For GSPH acceleration, use shamrock::sph::sph_pressure_symetric() with p_star
40 // as both P_a and P_b. This provides proper handling of zero denominators via
41 // sham::inv_sat_zero() and avoids code duplication.
42
69 template<class Tvec, class Tscal>
70 inline Tscal gsph_energy_rate(
71 Tscal m_b,
72 Tscal p_star,
73 Tscal v_star,
74 Tscal rho_a_sq,
75 Tscal rho_b_sq,
76 Tscal omega_a,
77 Tscal omega_b,
78 Tvec v_a,
79 Tvec r_ab_unit,
80 Tvec nabla_W_a,
81 Tvec nabla_W_b) {
82
83 // Interface velocity vector (in direction of pair axis)
84 Tvec v_star_vec = v_star * r_ab_unit;
85
86 // Compute symmetric force (same as momentum equation)
87 // f = m_b * p* * (nabla_W_a / (rho_a^2 * Omega_a) + nabla_W_b / (rho_b^2 * Omega_b))
88 Tscal sub_fact_a = rho_a_sq * omega_a;
89 Tscal sub_fact_b = rho_b_sq * omega_b;
90 Tvec f = m_b * p_star
91 * (nabla_W_a * sham::inv_sat_zero(sub_fact_a)
92 + nabla_W_b * sham::inv_sat_zero(sub_fact_b));
93
94 // Energy rate: -f dot (v* - v_a)
95 return -sycl::dot(f, v_star_vec - v_a);
96 }
97
120 template<class Tvec, class Tscal>
122 Tscal m_b,
123 Tscal p_star,
124 Tscal v_star,
125 Tscal rho_a,
126 Tscal rho_b,
127 Tscal omega_a,
128 Tscal omega_b,
129 Tscal Fab_a,
130 Tscal Fab_b,
131 Tvec r_ab_unit,
132 Tvec v_a,
133 Tvec &dv_dt,
134 Tscal &du_dt) {
135
136 const Tscal rho_a_sq = rho_a * rho_a;
137 const Tscal rho_b_sq = rho_b * rho_b;
138
139 // Kernel gradient vectors (pointing from a to b)
140 Tvec nabla_W_a = Fab_a * r_ab_unit;
141 Tvec nabla_W_b = Fab_b * r_ab_unit;
142
143 // Acceleration: use sph_pressure_symetric with p_star as both P_a and P_b
144 // This provides proper handling of zero denominators via sham::inv_sat_zero()
146 m_b, rho_a_sq, rho_b_sq, p_star, p_star, omega_a, omega_b, nabla_W_a, nabla_W_b);
147
148 // Energy rate (uses symmetric force, same as momentum equation)
150 m_b,
151 p_star,
152 v_star,
153 rho_a_sq,
154 rho_b_sq,
155 omega_a,
156 omega_b,
157 v_a,
158 r_ab_unit,
159 nabla_W_a,
160 nabla_W_b);
161 }
162
163 // Note: For velocity projection onto pair axis, use sycl::dot(v, r_ab_unit) directly.
164 // For density from smoothing length, use shamrock::sph::rho_h() from density.hpp.
165
186 template<class Tvec, class Tscal>
188 Tscal m_b,
189 Tscal p_star,
190 Tscal v_star,
191 Tscal V2_ij,
192 Tvec grad_W_ij,
193 Tvec r_ab_unit,
194 Tvec v_a,
195 Tvec &dv_dt,
196 Tscal &du_dt) {
197
198 dv_dt -= m_b * p_star * V2_ij * grad_W_ij;
199
200 Tvec v_star_vec = v_star * r_ab_unit;
201 du_dt -= m_b * p_star * V2_ij * sycl::dot(grad_W_ij, v_star_vec - v_a);
202 }
203
231 template<class Kernel, class Tvec, class Tscal>
233 bool use_inutsuka_v2,
234 Tscal pmass,
235 Tscal p_star,
236 Tscal v_star,
237 Tscal rho_a,
238 Tscal rho_b,
239 Tscal omega_a,
240 Tscal omega_b,
241 Tscal rab,
242 Tscal rab_inv,
243 Tscal h_a,
244 Tscal h_b,
245 Tvec r_ab_unit,
246 Tvec vxyz_a,
247 Tvec &sum_axyz,
248 Tscal &sum_du_a) {
249
250 if (use_inutsuka_v2) {
251 // Effective volume/face interpolation (Inutsuka 2002), linear
252 // (1st order): specific volume is 1/rho for equal-mass particles.
253 const Tscal vol_a = Tscal{1} / rho_a;
254 const Tscal vol_b = Tscal{1} / rho_b;
255
256 auto face = lin_v2_sast_ij<Tscal>(vol_a, vol_b, h_a, h_b, rab_inv);
257
258 // Pair-symmetrized kernel gradient at sqrt(2)*h (Inutsuka 2002)
259 constexpr Tscal sqrt2 = shambase::constants::sqrt_2<Tscal>;
260 const Tscal Fab2_a = Kernel::dW_3d(rab, sqrt2 * h_a);
261 const Tscal Fab2_b = Kernel::dW_3d(rab, sqrt2 * h_b);
262 const Tvec grad_W_ij = (Fab2_a + Fab2_b) * r_ab_unit;
263
265 pmass, p_star, v_star, face.V2, grad_W_ij, r_ab_unit, vxyz_a, sum_axyz, sum_du_a);
266 } else {
267 const Tscal Fab_a = Kernel::dW_3d(rab, h_a);
268 const Tscal Fab_b = Kernel::dW_3d(rab, h_b);
269
271 pmass,
272 p_star,
273 v_star,
274 rho_a,
275 rho_b,
276 omega_a,
277 omega_b,
278 Fab_a,
279 Fab_b,
280 r_ab_unit,
281 vxyz_a,
282 sum_axyz,
283 sum_du_a);
284 }
285 }
286
287} // namespace shammodels::gsph
Class holding the value of numerous constants generated from the following source.
void accumulate_gsph_pair_force(bool use_inutsuka_v2, Tscal pmass, Tscal p_star, Tscal v_star, Tscal rho_a, Tscal rho_b, Tscal omega_a, Tscal omega_b, Tscal rab, Tscal rab_inv, Tscal h_a, Tscal h_b, Tvec r_ab_unit, Tvec vxyz_a, Tvec &sum_axyz, Tscal &sum_du_a)
Dispatch a single neighbor pair's force contribution to ChaWhitworth or InutsukaV2,...
Definition forces.hpp:232
void add_gsph_force_contribution_inutsuka(Tscal m_b, Tscal p_star, Tscal v_star, Tscal V2_ij, Tvec grad_W_ij, Tvec r_ab_unit, Tvec v_a, Tvec &dv_dt, Tscal &du_dt)
Add Inutsuka (2002) GSPH force contribution from a single neighbor pair.
Definition forces.hpp:187
void add_gsph_force_contribution(Tscal m_b, Tscal p_star, Tscal v_star, Tscal rho_a, Tscal rho_b, Tscal omega_a, Tscal omega_b, Tscal Fab_a, Tscal Fab_b, Tvec r_ab_unit, Tvec v_a, Tvec &dv_dt, Tscal &du_dt)
Add GSPH force contribution from a single neighbor pair.
Definition forces.hpp:121
Tscal gsph_energy_rate(Tscal m_b, Tscal p_star, Tscal v_star, Tscal rho_a_sq, Tscal rho_b_sq, Tscal omega_a, Tscal omega_b, Tvec v_a, Tvec r_ab_unit, Tvec nabla_W_a, Tvec nabla_W_b)
Compute GSPH energy equation contribution.
Definition forces.hpp:70
Iterative Riemann solver for GSPH (van Leer 1997).
T inv_sat_zero(T v, T satval=T{0.}) noexcept
inverse saturated (zero version)
Definition math.hpp:870
Effective face (volume element) interpolation for the Inutsuka (2002) GSPH formulation.
EffectiveFace< Tscal > lin_v2_sast_ij(Tscal vol_a, Tscal vol_b, Tscal h_a, Tscal h_b, Tscal rab_inv)
Linear (1st order) interpolation of the effective face between a pair.
file containing formulas for sph forces
Tvec sph_pressure_symetric(const Tscal &m_b, const Tscal &rho_a_sq, const Tscal &rho_b_sq, const Tscal &P_a, const Tscal &P_b, const Tscal &omega_a, const Tscal &omega_b, const Tvec &nabla_Wab_ha, const Tvec &nabla_Wab_hb)
phantom_2018 eq.34, with
Definition forces.hpp:60