ryujin 2.1.1 revision 0348cbb53a3e4b1da2a4c037e81f88f2d21ce219
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<ScalarNumber, double>::value)
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
165
183
187 void reset(const unsigned int i,
188 const state_type &U_i,
189 const flux_contribution_type &flux_i);
190
195 void accumulate(const unsigned int *js,
196 const state_type &U_j,
197 const flux_contribution_type &flux_j,
198 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
199 const state_type &affine_shift);
200
204 Bounds bounds(const Number hd_i) const;
205
206 //*}
209
224 std::tuple<Number, bool> limit(const Bounds &bounds,
225 const state_type &U,
226 const state_type &P,
227 const Number t_min = Number(0.),
228 const Number t_max = Number(1.));
229
230 private:
232
234
235 const HyperbolicSystem &hyperbolic_system;
236 const Parameters &parameters;
237 const PrecomputedVector &precomputed_values;
238
239 state_type U_i;
241
242 Bounds bounds_;
243
244 Number rho_relaxation_numerator;
245 Number rho_relaxation_denominator;
246 Number s_interp_max;
247
249 };
250
251
252 /*
253 * -------------------------------------------------------------------------
254 * Inline definitions
255 * -------------------------------------------------------------------------
256 */
257
258
259 template <int dim, typename Number>
260 DEAL_II_ALWAYS_INLINE inline auto
262 const unsigned int i, const state_type &U_i) const -> Bounds
263 {
264 const auto view = hyperbolic_system.view<dim, Number>();
265 const auto rho_i = view.density(U_i);
266 const auto &[p_i, gamma_min_i, s_i, eta_i] =
267 precomputed_values.template get_tensor<Number, precomputed_type>(i);
268
269 return {/*rho_min*/ rho_i,
270 /*rho_max*/ rho_i,
271 /*s_min*/ s_i,
272 /*gamma_min*/ gamma_min_i};
273 }
274
275
276 template <int dim, typename Number>
277 DEAL_II_ALWAYS_INLINE inline auto Limiter<dim, Number>::combine_bounds(
278 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
279 {
280 const auto &[rho_min_l, rho_max_l, s_min_l, gamma_min_l] = bounds_left;
281 const auto &[rho_min_r, rho_max_r, s_min_r, gamma_min_r] = bounds_right;
282
283 return {std::min(rho_min_l, rho_min_r),
284 std::max(rho_max_l, rho_max_r),
285 std::min(s_min_l, s_min_r),
286 std::min(gamma_min_l, gamma_min_r)};
287 }
288
289
290 template <int dim, typename Number>
291 DEAL_II_ALWAYS_INLINE inline void
292 Limiter<dim, Number>::reset(const unsigned int i,
293 const state_type &new_U_i,
294 const flux_contribution_type &new_flux_i)
295 {
296 U_i = new_U_i;
297 flux_i = new_flux_i;
298
299 /* Bounds: */
300
301 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
302
303 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
304 rho_max = Number(0.);
305 s_min = Number(std::numeric_limits<ScalarNumber>::max());
306
307 const auto &[p_i, gamma_min_i, s_i, eta_i] =
308 precomputed_values.template get_tensor<Number, precomputed_type>(i);
309
310 gamma_min = gamma_min_i;
311
312 /* Relaxation: */
313
314 rho_relaxation_numerator = Number(0.);
315 rho_relaxation_denominator = Number(0.);
316 s_interp_max = Number(0.);
317 }
318
319
320 template <int dim, typename Number>
321 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
322 const unsigned int *js,
323 const state_type &U_j,
324 const flux_contribution_type &flux_j,
325 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
326 const state_type &affine_shift)
327 {
328 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
329 // then enters all bounds), but we do not modify s_interp and
330 // rho_relaxation. When actually adding a source term to the Euler
331 // equations verify that this does the right thing.
332 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
333 dealii::ExcNotImplemented());
334
335 const auto view = hyperbolic_system.view<dim, Number>();
336
337 /* Bounds: */
338 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
339
340 const auto rho_i = view.density(U_i);
341 const auto rho_j = view.density(U_j);
342
343 /* bar state shifted by an affine shift: */
344 const auto U_ij_bar =
345 ScalarNumber(0.5) * (U_i + U_j) -
346 ScalarNumber(0.5) * contract(add(flux_j, -flux_i), scaled_c_ij) +
347 affine_shift;
348
349 const auto rho_ij_bar = view.density(U_ij_bar);
350
351 /* Density bounds: */
352
353 rho_min = std::min(rho_min, rho_ij_bar);
354 rho_max = std::max(rho_max, rho_ij_bar);
355
356 /* Density relaxation: */
357
358 /* Use a uniform weight. */
359 const auto beta_ij = Number(1.);
360 rho_relaxation_numerator += beta_ij * (rho_i + rho_j);
361 rho_relaxation_denominator += std::abs(beta_ij);
362
363 /* Surrogate entropy bounds and relaxation: */
364
365 if (view.compute_strict_bounds()) {
366 /*
367 * Compute strict bounds precisely as outlined in @cite ryujin-2023-4
368 *
369 * This means, we compute
370 * - the surrogate entropy at dof j with the gamma_min of index i,
371 * - the surrogate entropy of the bar state U_ij_bar
372 * - an interpolated surrogate entropy at (U_i + U_j) / 2 for
373 * bounds relaxation:
374 */
375
376 const auto s_j = view.surrogate_specific_entropy(U_j, gamma_min);
377
378 const auto s_ij_bar =
379 view.surrogate_specific_entropy(U_ij_bar, gamma_min);
380
381 const Number s_interp = view.surrogate_specific_entropy(
382 (U_i + U_j) * ScalarNumber(.5), gamma_min);
383
384 s_min = std::min(s_min, s_j);
385 s_min = std::min(s_min, s_ij_bar);
386 s_interp_max = std::max(s_interp_max, s_interp);
387
388 } else {
389 /*
390 * Compute a cheaper bound solely relying on the diagonal s_j
391 * (computed with gamma_min_j) and the surrogate entropy s_ij_bar
392 * of the bar state. We use the s_ij_bar for computing the bounds
393 * relaxation as well.
394 */
395 const auto [p_j, gamma_min_j, s_j, eta_j] =
396 precomputed_values.template get_tensor<Number, precomputed_type>(
397 js);
398
399 const auto s_ij_bar =
400 view.surrogate_specific_entropy(U_ij_bar, gamma_min);
401
402 s_min = std::min(s_min, s_j);
403 s_min = std::min(s_min, s_ij_bar);
404 s_interp_max = std::max(s_interp_max, s_ij_bar);
405 }
406 }
407
408
409 template <int dim, typename Number>
410 DEAL_II_ALWAYS_INLINE inline auto
411 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
412 {
413 const auto view = hyperbolic_system.view<dim, Number>();
414
415 auto relaxed_bounds = bounds_;
416 auto &[rho_min, rho_max, s_min, gamma_min] = relaxed_bounds;
417
418 /* Use r_i = factor * (m_i / |Omega|) ^ (1.5 / d): */
419
420 Number r_i = std::sqrt(hd_i); // in 3D: ^ 3/6
421 if constexpr (dim == 2) //
422 r_i = dealii::Utilities::fixed_power<3>(std::sqrt(r_i)); // in 2D: ^ 3/4
423 else if constexpr (dim == 1) //
424 r_i = dealii::Utilities::fixed_power<3>(r_i); // in 1D: ^ 3/2
425 r_i *= parameters.relaxation_factor();
426
427 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
428 const Number rho_relaxation =
429 std::abs(rho_relaxation_numerator) /
430 (std::abs(rho_relaxation_denominator) + Number(eps));
431
432 const auto relaxation =
433 ScalarNumber(2. * parameters.relaxation_factor()) * rho_relaxation;
434
435 rho_min = std::max((Number(1.) - r_i) * rho_min, rho_min - relaxation);
436 rho_max = std::min((Number(1.) + r_i) * rho_max, rho_max + relaxation);
437
438 const auto entropy_relaxation =
439 parameters.relaxation_factor() * (s_interp_max - s_min);
440
441 s_min = std::max((Number(1.) - r_i) * s_min, s_min - entropy_relaxation);
442
443 /*
444 * If we have a maximum compressibility constant, b, the maximum
445 * bound for rho changes. See @cite ryujin-2023-4 for how to define
446 * rho_max.
447 */
448
449 const auto numerator = (gamma_min + Number(1.)) * rho_max;
450 const auto interpolation_b = view.eos_interpolation_b();
451 const auto denominator =
452 gamma_min - Number(1.) + ScalarNumber(2.) * interpolation_b * rho_max;
453 const auto upper_bound = numerator / denominator;
454
455 rho_max = std::min(upper_bound, rho_max);
456
457 return relaxed_bounds;
458 }
459 } // namespace EulerAEOS
460} // 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)
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:277
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:292
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
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:321
Bounds projection_bounds_from_state(const unsigned int i, const state_type &U_i) const
Definition: limiter.h:261
Bounds bounds(const Number hd_i) const
Definition: limiter.h:411
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)