ryujin 2.1.1 revision 0348cbb53a3e4b1da2a4c037e81f88f2d21ce219
limiter.template.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0
3// [LANL Copyright Statement]
4// Copyright (C) 2022 - 2024 by the ryujin authors
5// Copyright (C) 2023 - 2024 by Triad National Security, LLC
6//
7
8#pragma once
9
10#include "limiter.h"
11
12namespace ryujin
13{
14 namespace ShallowWater
15 {
16 template <int dim, typename Number>
17 std::tuple<Number, bool>
19 const state_type &U,
20 const state_type &P,
21 const Number t_min /* = Number(0.) */,
22 const Number t_max /* = Number(1.) */)
23 {
24 const auto view = hyperbolic_system.view<dim, Number>();
25
26 bool success = true;
27 Number t_l = t_min;
28 Number t_r = t_max;
29
30 const auto &[h_min, h_max, v2_max] = bounds;
31
32 constexpr ScalarNumber min = std::numeric_limits<ScalarNumber>::min();
33 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
34 const auto small = view.dry_state_relaxation_small();
35 const auto large = view.dry_state_relaxation_large();
36 const auto relax_small = ScalarNumber(1. + small * eps);
37 const auto relax = ScalarNumber(1. + large * eps);
38
39 /*
40 * We first limit the water_depth h.
41 *
42 * See [Guermond et al, 2021] (5.7).
43 */
44
45 {
46 auto h_U = view.water_depth(U);
47 const auto &h_P = view.water_depth(P);
48
49 const auto test_min = view.filter_dry_water_depth(
50 std::max(Number(0.), h_U - relax * h_max));
51 const auto test_max = view.filter_dry_water_depth(
52 std::max(Number(0.), h_min - relax * h_U));
53
54 if (!(test_min == Number(0.) && test_max == Number(0.))) {
55#ifdef DEBUG_OUTPUT
56 std::cout << std::fixed << std::setprecision(16);
57 std::cout << "Bounds violation: low-order water depth (critical)!\n"
58 << "\n\t\th min: " << h_min
59 << "\n\t\th min (delta): " << negative_part(h_U - h_min)
60 << "\n\t\th: " << h_U
61 << "\n\t\th max (delta): " << positive_part(h_U - h_max)
62 << "\n\t\th max: " << h_max << "\n"
63 << std::endl;
64#endif
65 success = false;
66 }
67
68 const Number denominator =
69 ScalarNumber(1.) / (std::abs(h_P) + eps * h_max + min);
70
71 constexpr auto lt = dealii::SIMDComparison::less_than;
72
73 t_r = dealii::compare_and_apply_mask<lt>( //
74 h_max,
75 h_U + t_r * h_P,
76 /*
77 * h_P is positive.
78 *
79 * Note: Do not take an absolute value here. If we are out of
80 * bounds we have to ensure that t_r is set to t_min.
81 */
82 (h_max - h_U) * denominator,
83 t_r);
84
85 t_r = dealii::compare_and_apply_mask<lt>( //
86 h_U + t_r * h_P,
87 h_min,
88 /*
89 * h_P is negative.
90 *
91 * Note: Do not take an absolute value here. If we are out of
92 * bounds we have to ensure that t_r is set to t_min.
93 */
94 (h_U - h_min) * denominator,
95 t_r);
96
97 /*
98 * Ensure that t_min <= t <= t_max. This might not be the case if
99 * h_U is outside the interval [h_min, h_max]. Furthermore, the
100 * quotient we take above is prone to numerical cancellation in
101 * particular in the second pass of the limiter when h_P might be
102 * small.
103 */
104 t_r = std::min(t_r, t_max);
105 t_r = std::max(t_r, t_min);
106
107
108#ifdef EXPENSIVE_BOUNDS_CHECK
109 /*
110 * Verify that the new state is within bounds:
111 */
112 const auto h_new = view.water_depth(U + t_r * P);
113 const auto test_new_min = view.filter_dry_water_depth(
114 std::max(Number(0.), h_new - relax * h_max));
115 const auto test_new_max = view.filter_dry_water_depth(
116 std::max(Number(0.), h_min - relax * h_new));
117
118 if (!(test_new_min == Number(0.) && test_new_max == Number(0.))) {
119#ifdef DEBUG_OUTPUT
120 std::cout << std::fixed << std::setprecision(30);
121 std::cout << "Bounds violation: high-order water depth!\n"
122 << "\n\t\th min: " << h_min
123 << "\n\t\th min (delta): " << negative_part(h_new - h_min)
124 << "\n\t\th: " << h_new
125 << "\n\t\th max (delta): " << positive_part(h_new - h_max)
126 << "\n\t\th max: " << h_max << "\n"
127 << std::endl;
128#endif
129 success = false;
130 }
131#endif
132 }
133
134 /*
135 * Limit the (negative) |v|^2:
136 *
137 * Given initial limiter values t_l and t_r with psi(t_l) > 0 and
138 * psi(t_r) < 0 we try to find t^\ast with psi(t^\ast) \approx 0.
139 *
140 * Here, psi is the function:
141 *
142 * psi = h^2 (|v|^2)^max - |q|^2
143 */
144
145 {
146 /* We first check if t_r is a good state */
147
148 const auto U_r = U + t_r * P;
149 const auto h_r = view.water_depth(U_r);
150 const auto q_r = view.momentum(U_r);
151
152 const auto psi_r = relax_small * h_r * h_r * v2_max - q_r.norm_square();
153
154 /*
155 * If psi_r > 0 the right state is fine, force returning t_r by
156 * setting t_l = t_r:
157 */
158 t_l = dealii::compare_and_apply_mask<
159 dealii::SIMDComparison::greater_than>(psi_r, Number(0.), t_r, t_l);
160
161 /* If we have set t_l = t_r everywhere we can return: */
162 if (t_l == t_r)
163 return {t_l, success};
164
165#ifdef DEBUG_OUTPUT_LIMITER
166 {
167 std::cout << std::endl;
168 std::cout << std::fixed << std::setprecision(16);
169 std::cout << "t_l: (start) " << t_l << std::endl;
170 std::cout << "t_r: (start) " << t_r << std::endl;
171 }
172#endif
173
174 const auto U_l = U + t_l * P;
175 const auto h_l = view.water_depth(U_l);
176 const auto q_l = view.momentum(U_l);
177
178 const auto psi_l = relax_small * h_l * h_l * v2_max - q_l.norm_square();
179
180 /*
181 * Verify that the left state is within bounds. This property might
182 * be violated for relative CFL numbers larger than 1.
183 *
184 * We use a non-scaled eps here to force the lower_bound to be
185 * negative so that we do not accidentally trigger in "perfect" dry
186 * states with h_l equal to zero.
187 */
188 const auto filtered_h_l = view.filter_dry_water_depth(h_l);
189 const auto lower_bound =
190 (ScalarNumber(1.) - relax) * filtered_h_l * filtered_h_l * v2_max -
191 ScalarNumber(100.) * eps;
192 if (!(std::min(Number(0.), psi_l - lower_bound) == Number(0.))) {
193#ifdef DEBUG_OUTPUT
194 std::cout << std::fixed << std::setprecision(16);
195 std::cout
196 << "Bounds violation: low-order square velocity (critical)!\n";
197 std::cout << "\t\tPsi left: 0 <= " << psi_l << "\n" << std::endl;
198#endif
199 success = false;
200 }
201
202 /*
203 * Skip the quadratic Newton step if the window between t_l and t_r
204 * is within the prescribed tolerance:
205 */
206 const Number tolerance(parameters.newton_tolerance());
207 if (!(std::max(Number(0.), t_r - t_l - tolerance) == Number(0.))) {
208 /*
209 * If the bound is not satisfied, we need to find the root of a
210 * quadratic function:
211 *
212 * psi(t) = (h_U + t h_P)^2 v2_max
213 * - (|q_U|^2 + 2(q_U * q_P) t + |q_P|^2 t^2)
214 *
215 * d_psi(t) = 2 (h_U + t * h_P) * h_P v2_max
216 * - 2 (q_U * q_P) - |q_P|^2 t
217 *
218 * We can compute the root of this function efficiently by using our
219 * standard quadratic_newton_step() function that will use the points
220 * [p1, p1, p2] as well as [p1, p2, p2] to construct two quadratic
221 * polynomials to compute new candiates for the bounds [t_l, t_r]. In
222 * case of a quadratic function psi(t) both polynomials will coincide
223 * so that (up to round-off error) t_l = t_r.
224 */
225 const auto &h_U = view.water_depth(U);
226 const auto &h_P = view.water_depth(P);
227 const auto &q_U = view.momentum(U);
228 const auto &q_P = view.momentum(P);
229
230 const auto dpsi_l =
231 (h_U + t_l * h_P) * h_P * v2_max -
232 ScalarNumber(2.) * ((q_U * q_P) - q_P * q_P * t_l);
233 const auto dpsi_r =
234 (h_U + t_r * h_P) * h_P * v2_max -
235 ScalarNumber(2.) * ((q_U * q_P) - q_P * q_P * t_r);
236
238 t_l, t_r, psi_l, psi_r, dpsi_l, dpsi_r, Number(-1.));
239
240#ifdef DEBUG_OUTPUT_LIMITER
241 if (std::max(Number(0.), psi_r + Number(eps)) == Number(0.)) {
242 std::cout << "psi_l: " << psi_l << std::endl;
243 std::cout << "psi_r: " << psi_r << std::endl;
244 std::cout << "dpsi_l: " << dpsi_l << std::endl;
245 std::cout << "dpsi_r: " << dpsi_r << std::endl;
246 std::cout << "t_l: (end) " << t_l << std::endl;
247 std::cout << "t_r: (end) " << t_r << std::endl;
248 }
249#endif
250 }
251
252#ifdef EXPENSIVE_BOUNDS_CHECK
253 /*
254 * Verify that the new state is within bounds:
255 */
256 {
257 const auto U_new = U + t_l * P;
258 const auto h_new = view.water_depth(U_new);
259 const auto q_new = view.momentum(U_new);
260
261 const auto psi_new =
262 relax_small * h_new * h_new * v2_max - q_new.norm_square();
263
264 const auto lower_bound =
265 (ScalarNumber(1.) - relax) * h_new * h_new * v2_max -
266 ScalarNumber(100.) * eps;
267
268 const bool psi_valid =
269 std::min(Number(0.), psi_new - lower_bound) == Number(0.);
270 if (!psi_valid) {
271#ifdef DEBUG_OUTPUT
272 std::cout << std::fixed << std::setprecision(16);
273 std::cout << "Bounds violation: high-order square velocity!\n";
274 std::cout << "\t\tPsi: 0 <= " << psi_new << "\n" << std::endl;
275#endif
276 success = false;
277 }
278 }
279#endif
280 }
281
282 return {t_l, success};
283 }
284
285 } // namespace ShallowWater
286} // namespace ryujin
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.))
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:81
std::array< Number, n_bounds > Bounds
Definition: limiter.h:108
typename View::state_type state_type
Definition: limiter.h:85
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