ryujin 2.1.1 revision 955e869188d49b3c97ca7b1cf4fd9ceb0e6f46ef
limiter.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 "hyperbolic_system.h"
9
10#include <compile_time_options.h>
12#include <newton.h>
13#include <simd.h>
14
15namespace ryujin
16{
17 namespace EulerAEOS
18 {
19 template <typename ScalarNumber = double>
20 class LimiterParameters : public dealii::ParameterAcceptor
21 {
22 public:
23 LimiterParameters(const std::string &subsection = "/Limiter")
24 : ParameterAcceptor(subsection)
25 {
26 iterations_ = 2;
27 add_parameter(
28 "iterations", iterations_, "Number of limiter iterations");
29
30 if constexpr (std::is_same_v<ScalarNumber, double>)
31 newton_tolerance_ = 1.e-10;
32 else
33 newton_tolerance_ = 1.e-4;
34 add_parameter("newton tolerance",
35 newton_tolerance_,
36 "Tolerance for the quadratic newton stopping criterion");
37
38 newton_max_iterations_ = 2;
39 add_parameter("newton max iterations",
40 newton_max_iterations_,
41 "Maximal number of quadratic newton iterations performed "
42 "during limiting");
43
44 relaxation_factor_ = ScalarNumber(1.);
45 add_parameter("relaxation factor",
46 relaxation_factor_,
47 "Factor for scaling the relaxation window with r_i = "
48 "factor * (m_i/|Omega|)^(1.5/d).");
49 }
50
51 ACCESSOR_READ_ONLY(iterations);
52 ACCESSOR_READ_ONLY(newton_tolerance);
53 ACCESSOR_READ_ONLY(newton_max_iterations);
54 ACCESSOR_READ_ONLY(relaxation_factor);
55
56 private:
57 unsigned int iterations_;
58 ScalarNumber newton_tolerance_;
59 unsigned int newton_max_iterations_;
60 ScalarNumber relaxation_factor_;
61 };
62
63
96 template <int dim, typename Number = double>
97 class Limiter
98 {
99 public:
104
106
108
110
111 using state_type = typename View::state_type;
112
114
116
118
120
122
125 //
127
130 static constexpr unsigned int n_bounds = 4;
131
135 using Bounds = std::array<Number, n_bounds>;
136
140 Limiter(const HyperbolicSystem &hyperbolic_system,
141 const Parameters &parameters,
142 const PrecomputedVector &precomputed_values)
143 : hyperbolic_system(hyperbolic_system)
144 , parameters(parameters)
145 , precomputed_values(precomputed_values)
146 {
147 }
148
153 Bounds projection_bounds_from_state(const unsigned int i,
154 const state_type &U_i) const;
155
161 Bounds combine_bounds(const Bounds &bounds_left,
162 const Bounds &bounds_right) const;
163
172 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
173
175
193
197 void reset(const unsigned int i,
198 const state_type &U_i,
199 const flux_contribution_type &flux_i);
200
205 void accumulate(const unsigned int *js,
206 const state_type &U_j,
207 const flux_contribution_type &flux_j,
208 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
209 const state_type &affine_shift);
210
214 Bounds bounds(const Number hd_i) const;
215
216 //*}
219
235 std::tuple<Number, bool> limit(const Bounds &bounds,
236 const state_type &U,
237 const state_type &P,
238 const Number t_min = Number(0.),
239 const Number t_max = Number(1.)) const;
240
241 private:
243
245
246 const HyperbolicSystem &hyperbolic_system;
247 const Parameters &parameters;
248 const PrecomputedVector &precomputed_values;
249
250 state_type U_i;
252
253 Bounds bounds_;
254
255 Number rho_relaxation_numerator;
256 Number rho_relaxation_denominator;
257 Number s_interp_max;
258
260 };
261
262
263 /*
264 * -------------------------------------------------------------------------
265 * Inline definitions
266 * -------------------------------------------------------------------------
267 */
268
269
270 template <int dim, typename Number>
271 DEAL_II_ALWAYS_INLINE inline auto
273 const unsigned int i, const state_type &U_i) const -> Bounds
274 {
275 const auto view = hyperbolic_system.view<dim, Number>();
276 const auto rho_i = view.density(U_i);
277 const auto &[p_i, gamma_min_i, s_i, eta_i] =
278 precomputed_values.template get_tensor<Number, precomputed_type>(i);
279
280 return {/*rho_min*/ rho_i,
281 /*rho_max*/ rho_i,
282 /*s_min*/ s_i,
283 /*gamma_min*/ gamma_min_i};
284 }
285
286
287 template <int dim, typename Number>
288 DEAL_II_ALWAYS_INLINE inline auto Limiter<dim, Number>::combine_bounds(
289 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
290 {
291 const auto &[rho_min_l, rho_max_l, s_min_l, gamma_min_l] = bounds_left;
292 const auto &[rho_min_r, rho_max_r, s_min_r, gamma_min_r] = bounds_right;
293
294 return {std::min(rho_min_l, rho_min_r),
295 std::max(rho_max_l, rho_max_r),
296 std::min(s_min_l, s_min_r),
297 std::min(gamma_min_l, gamma_min_r)};
298 }
299
300
301 template <int dim, typename Number>
302 DEAL_II_ALWAYS_INLINE inline auto
304 const Number &hd) const -> Bounds
305 {
306 const auto view = hyperbolic_system.view<dim, Number>();
307
308 const auto &[rho_min, rho_max, s_min, gamma_min] = bounds;
309
310 auto relaxed_bounds = bounds;
311 auto &[rho_min_relaxed, rho_max_relaxed, s_min_relaxed, g_m_relaxed] =
312 relaxed_bounds;
313
314 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
315
316 Number r = std::sqrt(hd); // in 3D: ^ 3/6
317 if constexpr (dim == 2) //
318 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
319 else if constexpr (dim == 1) //
320 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
321 r *= parameters.relaxation_factor();
322
323 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
324 rho_min_relaxed *= std::max(Number(1.) - r, Number(eps));
325 rho_max_relaxed *= (Number(1.) + r);
326 s_min_relaxed *= std::max(Number(1.) - r, Number(eps));
327
328 /*
329 * If we have a maximum compressibility constant, b, the maximum
330 * bound for rho changes. See @cite ryujin-2023-4 for how to define
331 * rho_max.
332 */
333
334 const auto numerator = (gamma_min + Number(1.)) * rho_max;
335 const auto interpolation_b = view.eos_interpolation_b();
336 const auto denominator =
337 gamma_min - Number(1.) + ScalarNumber(2.) * interpolation_b * rho_max;
338 const auto rho_compressibility_bound = numerator / denominator;
339
340 rho_max_relaxed = std::min(rho_compressibility_bound, rho_max_relaxed);
341
342 return relaxed_bounds;
343 }
344
345
346 template <int dim, typename Number>
347 DEAL_II_ALWAYS_INLINE inline void
348 Limiter<dim, Number>::reset(const unsigned int i,
349 const state_type &new_U_i,
350 const flux_contribution_type &new_flux_i)
351 {
352 U_i = new_U_i;
353 flux_i = new_flux_i;
354
355 /* Bounds: */
356
357 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
358
359 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
360 rho_max = Number(0.);
361 s_min = Number(std::numeric_limits<ScalarNumber>::max());
362
363 const auto &[p_i, gamma_min_i, s_i, eta_i] =
364 precomputed_values.template get_tensor<Number, precomputed_type>(i);
365
366 gamma_min = gamma_min_i;
367
368 /* Relaxation: */
369
370 rho_relaxation_numerator = Number(0.);
371 rho_relaxation_denominator = Number(0.);
372 s_interp_max = Number(0.);
373 }
374
375
376 template <int dim, typename Number>
377 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
378 const unsigned int *js,
379 const state_type &U_j,
380 const flux_contribution_type &flux_j,
381 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
382 const state_type &affine_shift)
383 {
384 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
385 // then enters all bounds), but we do not modify s_interp and
386 // rho_relaxation. When actually adding a source term to the Euler
387 // equations verify that this does the right thing.
388 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
389 dealii::ExcNotImplemented());
390
391 const auto view = hyperbolic_system.view<dim, Number>();
392
393 /* Bounds: */
394 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
395
396 const auto rho_i = view.density(U_i);
397 const auto rho_j = view.density(U_j);
398
399 /* bar state shifted by an affine shift: */
400 const auto U_ij_bar =
401 ScalarNumber(0.5) * (U_i + U_j) -
402 ScalarNumber(0.5) * contract(add(flux_j, -flux_i), scaled_c_ij) +
403 affine_shift;
404
405 const auto rho_ij_bar = view.density(U_ij_bar);
406
407 /* Density bounds: */
408
409 rho_min = std::min(rho_min, rho_ij_bar);
410 rho_max = std::max(rho_max, rho_ij_bar);
411
412 /* Density relaxation: */
413
414 /* Use a uniform weight. */
415 const auto beta_ij = Number(1.);
416 rho_relaxation_numerator += beta_ij * (rho_i + rho_j);
417 rho_relaxation_denominator += std::abs(beta_ij);
418
419 /* Surrogate entropy bounds and relaxation: */
420
421 if (view.compute_strict_bounds()) {
422 /*
423 * Compute strict bounds precisely as outlined in @cite ryujin-2023-4
424 *
425 * This means, we compute
426 * - the surrogate entropy at dof j with the gamma_min of index i,
427 * - the surrogate entropy of the bar state U_ij_bar
428 * - an interpolated surrogate entropy at (U_i + U_j) / 2 for
429 * bounds relaxation:
430 */
431
432 const auto s_j = view.surrogate_specific_entropy(U_j, gamma_min);
433
434 const auto s_ij_bar =
435 view.surrogate_specific_entropy(U_ij_bar, gamma_min);
436
437 const Number s_interp = view.surrogate_specific_entropy(
438 (U_i + U_j) * ScalarNumber(.5), gamma_min);
439
440 s_min = std::min(s_min, s_j);
441 s_min = std::min(s_min, s_ij_bar);
442 s_interp_max = std::max(s_interp_max, s_interp);
443
444 } else {
445 /*
446 * Compute a cheaper bound solely relying on the diagonal s_j
447 * (computed with gamma_min_j) and the surrogate entropy s_ij_bar
448 * of the bar state. We use the s_ij_bar for computing the bounds
449 * relaxation as well.
450 */
451 const auto [p_j, gamma_min_j, s_j, eta_j] =
452 precomputed_values.template get_tensor<Number, precomputed_type>(
453 js);
454
455 const auto s_ij_bar =
456 view.surrogate_specific_entropy(U_ij_bar, gamma_min);
457
458 s_min = std::min(s_min, s_j);
459 s_min = std::min(s_min, s_ij_bar);
460 s_interp_max = std::max(s_interp_max, s_ij_bar);
461 }
462 }
463
464
465 template <int dim, typename Number>
466 DEAL_II_ALWAYS_INLINE inline auto
467 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
468 {
469 const auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
470
471 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
472 auto &[rho_min_relaxed, rho_max_relaxed, s_min_relaxed, g_m_relaxed] =
473 relaxed_bounds;
474
475 /* Apply a stricter window: */
476
477 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
478
479 const auto rho_relaxation =
480 ScalarNumber(2. * parameters.relaxation_factor()) *
481 std::abs(rho_relaxation_numerator) /
482 (std::abs(rho_relaxation_denominator) + Number(eps));
483
484 const auto entropy_relaxation =
485 parameters.relaxation_factor() * (s_interp_max - s_min);
486
487 rho_min_relaxed = std::max(rho_min_relaxed, rho_min - rho_relaxation);
488 rho_max_relaxed = std::min(rho_max_relaxed, rho_max + rho_relaxation);
489 s_min_relaxed = std::max(s_min_relaxed, s_min - entropy_relaxation);
490
491 return relaxed_bounds;
492 }
493 } // namespace EulerAEOS
494} // namespace ryujin
dealii::Tensor< 1, problem_dimension, Number > state_type
typename get_value_type< Number >::type ScalarNumber
static constexpr unsigned int problem_dimension
Vectors::MultiComponentVector< ScalarNumber, n_precomputed_values > PrecomputedVector
std::array< Number, n_precomputed_values > precomputed_type
LimiterParameters(const std::string &subsection="/Limiter")
Definition: limiter.h:23
ACCESSOR_READ_ONLY(newton_max_iterations)
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition: limiter.h:303
typename View::state_type state_type
Definition: limiter.h:111
static constexpr unsigned int n_bounds
Definition: limiter.h:130
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const PrecomputedVector &precomputed_values)
Definition: limiter.h:140
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:107
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition: limiter.h:288
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
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:113
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:348
std::array< Number, n_bounds > Bounds
Definition: limiter.h:135
void accumulate(const unsigned int *js, const state_type &U_j, const flux_contribution_type &flux_j, const dealii::Tensor< 1, dim, Number > &scaled_c_ij, const state_type &affine_shift)
Definition: limiter.h:377
Bounds projection_bounds_from_state(const unsigned int i, const state_type &U_i) const
Definition: limiter.h:272
Bounds bounds(const Number hd_i) const
Definition: limiter.h:467
LimiterParameters< ScalarNumber > Parameters
Definition: limiter.h:119
typename View::PrecomputedVector PrecomputedVector
Definition: limiter.h:117
typename View::precomputed_type precomputed_type
Definition: limiter.h:115
static constexpr auto problem_dimension
Definition: limiter.h:109
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)