ryujin 2.1.1 revision 955e869188d49b3c97ca7b1cf4fd9ceb0e6f46ef
limiter.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0
3// [LANL Copyright Statement]
4// Copyright (C) 2023 - 2024 by the ryujin authors
5// Copyright (C) 2023 - 2024 by Triad National Security, LLC
6//
7
8#pragma once
9
10#include "hyperbolic_system.h"
11
12#include <compile_time_options.h>
14#include <newton.h>
15
16namespace ryujin
17{
18 namespace ShallowWater
19 {
20 template <typename ScalarNumber = double>
21 class LimiterParameters : public dealii::ParameterAcceptor
22 {
23 public:
24 LimiterParameters(const std::string &subsection = "/Limiter")
25 : ParameterAcceptor(subsection)
26 {
27 iterations_ = 2;
28 add_parameter(
29 "iterations", iterations_, "Number of limiter iterations");
30
31 if constexpr (std::is_same_v<ScalarNumber, double>)
32 newton_tolerance_ = 1.e-10;
33 else
34 newton_tolerance_ = 1.e-4;
35 add_parameter("newton tolerance",
36 newton_tolerance_,
37 "Tolerance for the quadratic newton stopping criterion");
38
39 newton_max_iterations_ = 2;
40 add_parameter("newton max iterations",
41 newton_max_iterations_,
42 "Maximal number of quadratic newton iterations performed "
43 "during limiting");
44
45 relaxation_factor_ = ScalarNumber(1.);
46 add_parameter("relaxation factor",
47 relaxation_factor_,
48 "Factor for scaling the relaxation window with r_i = "
49 "factor * (m_i/|Omega|)^(1.5/d).");
50 }
51
52 ACCESSOR_READ_ONLY(iterations);
53 ACCESSOR_READ_ONLY(newton_tolerance);
54 ACCESSOR_READ_ONLY(newton_max_iterations);
55 ACCESSOR_READ_ONLY(relaxation_factor);
56
57 private:
58 unsigned int iterations_;
59 ScalarNumber newton_tolerance_;
60 unsigned int newton_max_iterations_;
61 ScalarNumber relaxation_factor_;
62 };
63
64
70 template <int dim, typename Number = double>
71 class Limiter
72 {
73 public:
78
80
82
84
85 using state_type = typename View::state_type;
86
88
90
92
94
96
103 static constexpr unsigned int n_bounds = 3;
104
108 using Bounds = std::array<Number, n_bounds>;
109
113 Limiter(const HyperbolicSystem &hyperbolic_system,
114 const Parameters &parameters,
115 const PrecomputedVector &precomputed_values)
116 : hyperbolic_system(hyperbolic_system)
117 , parameters(parameters)
118 , precomputed_values(precomputed_values)
119 {
120 }
121
126 Bounds projection_bounds_from_state(const unsigned int i,
127 const state_type &U_i) const;
128
134 Bounds combine_bounds(const Bounds &bounds_left,
135 const Bounds &bounds_right) const;
136
144 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
145
147
165
169 void reset(const unsigned int i,
170 const state_type &U_i,
171 const flux_contribution_type &flux_i);
172
177 void accumulate(const state_type &U_j,
178 const state_type &U_star_ij,
179 const state_type &U_star_ji,
180 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
181 const state_type &affine_shift);
182
186 Bounds bounds(const Number hd_i) const;
187
188 //*}
191
198 std::tuple<Number, bool> limit(const Bounds &bounds,
199 const state_type &U,
200 const state_type &P,
201 const Number t_min = Number(0.),
202 const Number t_max = Number(1.)) const;
203
204 private:
206
208
209 const HyperbolicSystem &hyperbolic_system;
210 const Parameters &parameters;
211 const PrecomputedVector &precomputed_values;
212
213 state_type U_i;
214
215 Bounds bounds_;
216
217 /* for relaxation */
218
219 Number h_relaxation_numerator;
220 Number v2_relaxation_numerator;
221 Number relaxation_denominator;
222
224 };
225
226
227 /*
228 * -------------------------------------------------------------------------
229 * Inline definitions
230 * -------------------------------------------------------------------------
231 */
232
233
234 template <int dim, typename Number>
235 DEAL_II_ALWAYS_INLINE inline auto
237 const unsigned int /*i*/, const state_type &U_i) const -> Bounds
238 {
239 const auto view = hyperbolic_system.view<dim, Number>();
240 const auto h_i = view.water_depth(U_i);
241 const auto v_i =
242 view.momentum(U_i) * view.inverse_water_depth_mollified(U_i);
243 const auto v2_i = v_i.norm_square();
244
245 return {/*h_min*/ h_i, /*h_max*/ h_i, /*v2_max*/ v2_i};
246 }
247
248
249 template <int dim, typename Number>
250 DEAL_II_ALWAYS_INLINE inline auto
252 const Bounds &bounds_r) const -> Bounds
253 {
254 const auto &[h_min_l, h_max_l, v2_max_l] = bounds_l;
255 const auto &[h_min_r, h_max_r, v2_max_r] = bounds_r;
256
257 return {std::min(h_min_l, h_min_r),
258 std::max(h_max_l, h_max_r),
259 std::max(v2_max_l, v2_max_r)};
260 }
261
262
263 template <int dim, typename Number>
264 DEAL_II_ALWAYS_INLINE inline auto
266 const Number &hd) const -> Bounds
267 {
268 auto relaxed_bounds = bounds;
269 auto &[h_min, h_max, v2_max] = relaxed_bounds;
270
271 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
272
273 Number r = std::sqrt(hd); // in 3D: ^ 3/6
274 if constexpr (dim == 2) //
275 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
276 else if constexpr (dim == 1) //
277 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
278 r *= parameters.relaxation_factor();
279
280 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
281 h_min *= std::max((Number(1.) - r), Number(eps));
282 h_max *= (Number(1.) + r);
283 v2_max *= (Number(1.) + r);
284
285 return relaxed_bounds;
286 }
287
288
289 template <int dim, typename Number>
290 DEAL_II_ALWAYS_INLINE inline void
291 Limiter<dim, Number>::reset(unsigned int /*i*/,
292 const state_type &new_U_i,
293 const flux_contribution_type & /*new_flux_i*/)
294 {
295 U_i = new_U_i;
296
297 auto &[h_min, h_max, v2_max] = bounds_;
298
299 h_min = Number(std::numeric_limits<ScalarNumber>::max());
300 h_max = Number(0.);
301 v2_max = Number(0.);
302
303 h_relaxation_numerator = Number(0.);
304 v2_relaxation_numerator = Number(0.);
305 relaxation_denominator = Number(0.);
306 }
307
308
309 template <int dim, typename Number>
310 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
311 const state_type &U_j,
312 const state_type &U_star_ij,
313 const state_type &U_star_ji,
314 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
315 const state_type &affine_shift)
316 {
317 const auto view = hyperbolic_system.view<dim, Number>();
318
319 /* The bar states: */
320
321 const auto f_star_ij = view.f(U_star_ij);
322 const auto f_star_ji = view.f(U_star_ji);
323
324 /* bar state shifted by an affine shift: */
325 const auto U_ij_bar =
326 ScalarNumber(0.5) *
327 (U_star_ij + U_star_ji +
328 contract(add(f_star_ij, -f_star_ji), scaled_c_ij)) +
329 affine_shift;
330
331 /* Bounds: */
332
333 auto &[h_min, h_max, v2_max] = bounds_;
334
335 const auto h_bar_ij = view.water_depth(U_ij_bar);
336 h_min = std::min(h_min, h_bar_ij);
337 h_max = std::max(h_max, h_bar_ij);
338
339 const auto v_bar_ij = view.momentum(U_ij_bar) *
340 view.inverse_water_depth_mollified(U_ij_bar);
341 const auto v2_bar_ij = v_bar_ij.norm_square();
342 v2_max = std::max(v2_max, v2_bar_ij);
343
344 /* Relaxation: */
345
346 /* Use a uniform weight. */
347 const auto beta_ij = Number(1.);
348
349 relaxation_denominator += std::abs(beta_ij);
350
351 const auto h_i = view.water_depth(U_i);
352 const auto h_j = view.water_depth(U_j);
353 h_relaxation_numerator += beta_ij * (h_i + h_j);
354
355 const auto vel_i =
356 view.momentum(U_i) * view.inverse_water_depth_mollified(U_i);
357 const auto vel_j =
358 view.momentum(U_j) * view.inverse_water_depth_mollified(U_j);
359 v2_relaxation_numerator +=
360 beta_ij * (-vel_i.norm_square() + vel_j.norm_square());
361 }
362
363
364 template <int dim, typename Number>
365 DEAL_II_ALWAYS_INLINE inline auto
366 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
367 {
368 const auto &[h_min, h_max, v2_max] = bounds_;
369
370 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
371 auto &[h_min_relaxed, h_max_relaxed, v2_max_relaxed] = relaxed_bounds;
372
373 /* Apply a stricter window: */
374
375 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
376
377 const Number h_relaxed =
378 ScalarNumber(2. * parameters.relaxation_factor()) *
379 std::abs(h_relaxation_numerator) /
380 (relaxation_denominator + Number(eps));
381
382 const Number v2_relaxed =
383 ScalarNumber(2. * parameters.relaxation_factor()) *
384 std::abs(v2_relaxation_numerator) /
385 (relaxation_denominator + Number(eps));
386
387 h_min_relaxed = std::max(h_min_relaxed, h_min - h_relaxed);
388 h_max_relaxed = std::min(h_max_relaxed, h_max + h_relaxed);
389 v2_max_relaxed = std::min(v2_max_relaxed, v2_max + v2_relaxed);
390
391 return relaxed_bounds;
392 }
393 } // namespace ShallowWater
394} // namespace ryujin
typename get_value_type< Number >::type ScalarNumber
Vectors::MultiComponentVector< ScalarNumber, n_precomputed_values > PrecomputedVector
dealii::Tensor< 1, problem_dimension, Number > state_type
std::array< Number, n_precomputed_values > precomputed_type
static constexpr unsigned int problem_dimension
std::tuple< state_type, Number > flux_contribution_type
ACCESSOR_READ_ONLY(newton_max_iterations)
LimiterParameters(const std::string &subsection="/Limiter")
Definition: limiter.h:24
Bounds projection_bounds_from_state(const unsigned int i, const state_type &U_i) const
Definition: limiter.h:236
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:81
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:87
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition: limiter.h:265
std::array< Number, n_bounds > Bounds
Definition: limiter.h:108
typename View::PrecomputedVector PrecomputedVector
Definition: limiter.h:91
void accumulate(const state_type &U_j, const state_type &U_star_ij, const state_type &U_star_ji, const dealii::Tensor< 1, dim, Number > &scaled_c_ij, const state_type &affine_shift)
Definition: limiter.h:310
Bounds bounds(const Number hd_i) const
Definition: limiter.h:366
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.)) const
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const PrecomputedVector &precomputed_values)
Definition: limiter.h:113
typename View::precomputed_type precomputed_type
Definition: limiter.h:89
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition: limiter.h:251
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:291
typename View::state_type state_type
Definition: limiter.h:85
static constexpr auto problem_dimension
Definition: limiter.h:83
static constexpr unsigned int n_bounds
Definition: limiter.h:103
LimiterParameters< ScalarNumber > Parameters
Definition: limiter.h:93
DEAL_II_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)
DEAL_II_ALWAYS_INLINE dealii::Tensor< 1, problem_dim, T > contract(const FT &flux_ij, const TT &c_ij)