ryujin 2.1.1 revision 0348cbb53a3e4b1da2a4c037e81f88f2d21ce219
limiter.template.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2020 - 2024 by the ryujin authors
4//
5
6#pragma once
7
8#include "limiter.h"
9// #define DEBUG_OUTPUT_LIMITER
10
11namespace ryujin
12{
13 namespace EulerAEOS
14 {
15 template <int dim, typename Number>
16 std::tuple<Number, bool>
18 const state_type &U,
19 const state_type &P,
20 const Number t_min /* = Number(0.) */,
21 const Number t_max /* = Number(1.) */)
22 {
23 const auto view = hyperbolic_system.view<dim, Number>();
24
25 bool success = true;
26 Number t_r = t_max;
27
28 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
29 const auto small = view.vacuum_state_relaxation_small();
30 const auto large = view.vacuum_state_relaxation_large();
31 const ScalarNumber relax_small = ScalarNumber(1. + small * eps);
32 const ScalarNumber relax = ScalarNumber(1. + large * eps);
33
34 /*
35 * First limit the density rho.
36 *
37 * See [Guermond, Nazarov, Popov, Thomas] (4.8):
38 */
39
40 {
41 const auto &rho_U = view.density(U);
42 const auto &rho_P = view.density(P);
43
44 const auto &rho_min = std::get<0>(bounds);
45 const auto &rho_max = std::get<1>(bounds);
46
47 /*
48 * Verify that rho_U is within bounds. This property might be
49 * violated for relative CFL numbers larger than 1.
50 */
51 const auto test_min = view.filter_vacuum_density(
52 std::max(Number(0.), rho_U - relax * rho_max));
53 const auto test_max = view.filter_vacuum_density(
54 std::max(Number(0.), rho_min - relax * rho_U));
55 if (!(test_min == Number(0.) && test_max == Number(0.))) {
56#ifdef DEBUG_OUTPUT
57 std::cout << std::fixed << std::setprecision(16);
58 std::cout << "Bounds violation: low-order density (critical)!"
59 << "\n\t\trho min: " << rho_min
60 << "\n\t\trho min (delta): "
61 << negative_part(rho_U - rho_min)
62 << "\n\t\trho: " << rho_U
63 << "\n\t\trho max (delta): "
64 << positive_part(rho_U - rho_max)
65 << "\n\t\trho max: " << rho_max << "\n"
66 << std::endl;
67#endif
68 success = false;
69 }
70
71 const Number denominator =
72 ScalarNumber(1.) / (std::abs(rho_P) + eps * rho_max);
73
74 t_r = dealii::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
75 rho_max,
76 rho_U + t_r * rho_P,
77 /*
78 * rho_P is positive.
79 *
80 * Note: Do not take an absolute value here. If we are out of
81 * bounds we have to ensure that t_r is set to t_min.
82 */
83 (rho_max - rho_U) * denominator,
84 t_r);
85
86 t_r = dealii::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
87 rho_U + t_r * rho_P,
88 rho_min,
89 /*
90 * rho_P is negative.
91 *
92 * Note: Do not take an absolute value here. If we are out of
93 * bounds we have to ensure that t_r is set to t_min.
94 */
95 (rho_U - rho_min) * denominator,
96 t_r);
97
98 /*
99 * Ensure that t_min <= t <= t_max. This might not be the case if
100 * rho_U is outside the interval [rho_min, rho_max]. Furthermore,
101 * the quotient we take above is prone to numerical cancellation in
102 * particular in the second pass of the limiter when rho_P might be
103 * small.
104 */
105 t_r = std::min(t_r, t_max);
106 t_r = std::max(t_r, t_min);
107
108#ifdef EXPENSIVE_BOUNDS_CHECK
109 /*
110 * Verify that the new state is within bounds:
111 */
112 const auto rho_new = view.density(U + t_r * P);
113 const auto test_new_min = view.filter_vacuum_density(
114 std::max(Number(0.), rho_new - relax * rho_max));
115 const auto test_new_max = view.filter_vacuum_density(
116 std::max(Number(0.), rho_min - relax * rho_new));
117 if (!(test_new_min == Number(0.) && test_new_max == Number(0.))) {
118#ifdef DEBUG_OUTPUT
119 std::cout << std::fixed << std::setprecision(16);
120 std::cout << "Bounds violation: high-order density!"
121 << "\n\t\trho min: " << rho_min
122 << "\n\t\trho min (delta): "
123 << negative_part(rho_new - rho_min)
124 << "\n\t\trho: " << rho_new
125 << "\n\t\trho max (delta): "
126 << positive_part(rho_new - rho_max)
127 << "\n\t\trho max: " << rho_max << "\n"
128 << std::endl;
129#endif
130 success = false;
131 }
132#endif
133 }
134
135 /*
136 * Then limit the specific entropy:
137 *
138 * See [Guermond, Nazarov, Popov, Thomas],
139 * Section 4.6 + Section 5.1
140 * and @cite clayton2023robust Section 6:
141 */
142
143 Number t_l = t_min; // good state
144
145 const auto &gamma = std::get<3>(bounds) /* = gamma_min*/;
146 const Number gm1 = gamma - Number(1.);
147
148 const auto b = Number(view.eos_interpolation_b());
149 const auto pinf = Number(view.eos_interpolation_pinfty());
150 const auto q = Number(view.eos_interpolation_q());
151
152 {
153 /*
154 * Prepare a quadratic Newton method:
155 *
156 * Given initial limiter values t_l and t_r with psi(t_l) > 0 and
157 * psi(t_r) < 0 we try to find t^\ast with psi(t^\ast) \approx 0.
158 *
159 * Here, psi is a 3-convex function obtained by scaling the specific
160 * entropy s:
161 *
162 * psi = \rho ^ {\gamma + 1} s
163 *
164 * (s in turn was defined as s =\varepsilon \rho ^{-\gamma}, where
165 * \varepsilon = (\rho e - pinf * (1 - b rho)) is the shifted
166 * internal energy.)
167 */
168
169 const auto &s_min = std::get<2>(bounds);
170
171#ifdef DEBUG_OUTPUT_LIMITER
172 std::cout << std::endl;
173 std::cout << std::fixed << std::setprecision(16);
174 std::cout << "t_l: (start) " << t_l << std::endl;
175 std::cout << "t_r: (start) " << t_r << std::endl;
176#endif
177
178 for (unsigned int n = 0; n < parameters.newton_max_iterations(); ++n) {
179
180 const auto U_r = U + t_r * P;
181 const auto rho_r = view.density(U_r);
182 const auto rho_r_gamma = ryujin::pow(rho_r, gamma);
183 const auto covolume_r = Number(1.) - b * rho_r;
184
185 const auto rho_e_r = view.internal_energy(U_r);
186 const auto shift_r = rho_e_r - rho_r * q - pinf * covolume_r;
187
188 auto psi_r =
189 relax_small * rho_r * shift_r -
190 s_min * rho_r * rho_r_gamma * ryujin::pow(covolume_r, -gm1);
191
192#ifndef EXPENSIVE_BOUNDS_CHECK
193 /*
194 * If psi_r > 0 the right state is fine, force returning t_r by
195 * setting t_l = t_r:
196 */
197 t_l = dealii::compare_and_apply_mask<
198 dealii::SIMDComparison::greater_than>(
199 psi_r, Number(0.), t_r, t_l);
200
201 /*
202 * If we have set t_l = t_r everywhere then all states state U_r
203 * with t_r obey the specific entropy inequality and we can
204 * break.
205 *
206 * This is a very important optimization: Only for 1 in (25 to
207 * 50) cases do we actually need to limit on the specific entropy
208 * because one of the right states failed. So we can skip
209 * constructing the left state U_l, which is expensive.
210 *
211 * This implies unfortunately that we might not accurately report
212 * whether the low_order update U itself obeyed bounds because
213 * U_r = U + t_r * P pushed us back into bounds. We thus skip
214 * this shortcut if `EXPENSIVE_BOUNDS_CHECK` is set.
215 */
216 if (t_l == t_r) {
217#ifdef DEBUG_OUTPUT_LIMITER
218 std::cout << "shortcut: t_l == t_r" << std::endl;
219 std::cout << "psi_l: " << psi_l << std::endl;
220 std::cout << "psi_r: " << psi_r << std::endl;
221 std::cout << "t_l: ( " << n << " ) " << t_l << std::endl;
222 std::cout << "t_r: ( " << n << " ) " << t_r << std::endl;
223#endif
224 break;
225 }
226#endif
227
228 const auto U_l = U + t_l * P;
229 const auto rho_l = view.density(U_l);
230 const auto rho_l_gamma = ryujin::pow(rho_l, gamma);
231 const auto covolume_l = Number(1.) - b * rho_l;
232 const auto rho_e_l = view.internal_energy(U_l);
233 const auto shift_l = rho_e_l - rho_l * q - pinf * covolume_l;
234
235 auto psi_l =
236 relax_small * rho_l * shift_l -
237 s_min * rho_l * rho_l_gamma * ryujin::pow(covolume_l, -gm1);
238
239 /*
240 * Verify that the left state is within bounds. This property might
241 * be violated for relative CFL numbers larger than 1.
242 */
243 const auto lower_bound = (ScalarNumber(1.) - relax) * s_min * rho_l *
244 rho_l_gamma * ryujin::pow(covolume_l, -gm1);
245 if (n == 0 &&
246 !(std::min(Number(0.), psi_l - lower_bound) == Number(0.))) {
247#ifdef DEBUG_OUTPUT
248 std::cout << std::fixed << std::setprecision(16);
249 std::cout
250 << "Bounds violation: low-order specific entropy (critical)!\n";
251 std::cout << "\t\tPsi left: 0 <= " << psi_l << "\n" << std::endl;
252#endif
253 success = false;
254 }
255
256#ifdef EXPENSIVE_BOUNDS_CHECK
257 /*
258 * If psi_r > 0 the right state is fine, force returning t_r by
259 * setting t_l = t_r:
260 */
261 t_l = dealii::compare_and_apply_mask<
262 dealii::SIMDComparison::greater_than>(
263 psi_r, Number(0.), t_r, t_l);
264#endif
265
266 /*
267 * Break if the window between t_l and t_r is within the prescribed
268 * tolerance:
269 */
270 const Number tolerance(parameters.newton_tolerance());
271 if (std::max(Number(0.), t_r - t_l - tolerance) == Number(0.)) {
272#ifdef DEBUG_OUTPUT_LIMITER
273 std::cout << "break: t_l and t_r within tolerance" << std::endl;
274 std::cout << "psi_l: " << psi_l << std::endl;
275 std::cout << "psi_r: " << psi_r << std::endl;
276 std::cout << "t_l: ( " << n << " ) " << t_l << std::endl;
277 std::cout << "t_r: ( " << n << " ) " << t_r << std::endl;
278#endif
279 break;
280 }
281
282 /* We got unlucky and have to perform a Newton step: */
283
284 const auto drho = view.density(P);
285 const auto drho_e_l = view.internal_energy_derivative(U_l) * P;
286 const auto drho_e_r = view.internal_energy_derivative(U_r) * P;
287
288 const auto q_pinf_term_l =
289 ScalarNumber(2.) * rho_l * q +
290 pinf * (Number(1.) - ScalarNumber(2.) * b * rho_l);
291 const auto q_pinf_term_r =
292 ScalarNumber(2.) * rho_r * q +
293 pinf * (Number(1.) - ScalarNumber(2.) * b * rho_r);
294
295 const auto extra_term_l = s_min *
296 ryujin::pow(rho_l / covolume_l, gamma) *
297 (covolume_l + gamma - b * rho_l);
298 const auto extra_term_r = s_min *
299 ryujin::pow(rho_r / covolume_r, gamma) *
300 (covolume_r + gamma - b * rho_r);
301
302 const auto dpsi_l = rho_l * drho_e_l +
303 (rho_e_l - q_pinf_term_l - extra_term_l) * drho;
304 const auto dpsi_r = rho_r * drho_e_r +
305 (rho_e_r - q_pinf_term_r - extra_term_r) * drho;
306
308 t_l, t_r, psi_l, psi_r, dpsi_l, dpsi_r, Number(-1.));
309
310#ifdef DEBUG_OUTPUT_LIMITER
311 std::cout << "psi_l: " << psi_l << std::endl;
312 std::cout << "psi_r: " << psi_r << std::endl;
313 std::cout << "dpsi_l: " << dpsi_l << std::endl;
314 std::cout << "dpsi_r: " << dpsi_r << std::endl;
315 std::cout << "t_l: ( " << n << " ) " << t_l << std::endl;
316 std::cout << "t_r: ( " << n << " ) " << t_r << std::endl;
317#endif
318 }
319
320#ifdef EXPENSIVE_BOUNDS_CHECK
321 /*
322 * Verify that the new state is within bounds:
323 */
324 {
325 const auto U_new = U + t_l * P;
326
327 const auto rho_new = view.density(U_new);
328 const auto covolume_new = Number(1.) - b * rho_new;
329
330 const auto rho_new_gamma = ryujin::pow(rho_new, gamma);
331 const auto rho_e_new = view.internal_energy(U_new);
332
333 const auto shift_new = rho_e_new - rho_new * q - pinf * covolume_new;
334
335 const auto psi_new =
336 relax_small * rho_new * shift_new -
337 s_min * rho_new * rho_new_gamma * ryujin::pow(covolume_new, -gm1);
338
339 const auto lower_bound = (ScalarNumber(1.) - relax) * s_min *
340 rho_new * rho_new_gamma *
341 ryujin::pow(covolume_new, -gm1);
342
343 const bool e_valid = std::min(Number(0.), shift_new) == Number(0.);
344 const bool psi_valid =
345 std::min(Number(0.), psi_new - lower_bound) == Number(0.);
346
347 if (!e_valid || !psi_valid) {
348#ifdef DEBUG_OUTPUT
349 std::cout << std::fixed << std::setprecision(16);
350 std::cout << "Bounds violation: high-order specific entropy!\n";
351 std::cout << "\t\trho e: 0 <= " << rho_e_new << "\n";
352 std::cout << "\t\tPsi: 0 <= " << psi_new << "\n" << std::endl;
353#endif
354 success = false;
355 }
356 }
357#endif
358 }
359
360 return {t_l, success};
361 }
362
363 } // namespace EulerAEOS
364} // namespace ryujin
typename View::state_type state_type
Definition: limiter.h:111
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:107
std::tuple< Number, bool > limit(const Bounds &bounds, const state_type &U, const state_type &P, const Number t_min=Number(0.), const Number t_max=Number(1.))
std::array< Number, n_bounds > Bounds
Definition: limiter.h:135
T pow(const T x, const T b)
DEAL_II_ALWAYS_INLINE Number negative_part(const Number number)
Definition: simd.h:124
DEAL_II_ALWAYS_INLINE Number positive_part(const Number number)
Definition: simd.h:112
DEAL_II_ALWAYS_INLINE void quadratic_newton_step(Number &p_1, Number &p_2, const Number phi_p_1, const Number phi_p_2, const Number dphi_p_1, const Number dphi_p_2, const Number sign=Number(1.0))
Definition: newton.h:39