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