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>
11#include <deal.II/base/config.h>
13#include <newton.h>
14#include <simd.h>
15
16namespace ryujin
17{
18 namespace Euler
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<ScalarNumber, double>::value)
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
95 template <int dim, typename Number = double>
96 class Limiter
97 {
98 public:
103
105
107
109
110 using state_type = typename View::state_type;
111
113
115
117
119
121
124 //
126
129 static constexpr unsigned int n_bounds = 3;
130
134 using Bounds = std::array<Number, n_bounds>;
135
139 Limiter(const HyperbolicSystem &hyperbolic_system,
140 const Parameters &parameters,
141 const PrecomputedVector &precomputed_values)
142 : hyperbolic_system(hyperbolic_system)
143 , parameters(parameters)
144 , precomputed_values(precomputed_values)
145 {
146 }
147
152 Bounds projection_bounds_from_state(const unsigned int i,
153 const state_type &U_i) const;
154
160 Bounds combine_bounds(const Bounds &bounds_left,
161 const Bounds &bounds_right) const;
162
171 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
172
174
192
196 void reset(const unsigned int i,
197 const state_type &U_i,
198 const flux_contribution_type &flux_i);
199
204 void accumulate(const unsigned int *js,
205 const state_type &U_j,
206 const flux_contribution_type &flux_j,
207 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
208 const state_type &affine_shift);
209
213 Bounds bounds(const Number hd_i) const;
214
215 //*}
218
234 std::tuple<Number, bool> limit(const Bounds &bounds,
235 const state_type &U,
236 const state_type &P,
237 const Number t_min = Number(0.),
238 const Number t_max = Number(1.)) const;
239
240 private:
242
244
245 const HyperbolicSystem &hyperbolic_system;
246 const Parameters &parameters;
247 const PrecomputedVector &precomputed_values;
248
249 state_type U_i;
250
251 Bounds bounds_;
252
253 Number rho_relaxation_numerator;
254 Number rho_relaxation_denominator;
255 Number s_interp_max;
256
258 };
259
260
261 /*
262 * -------------------------------------------------------------------------
263 * Inline definitions
264 * -------------------------------------------------------------------------
265 */
266
267
268 template <int dim, typename Number>
269 DEAL_II_ALWAYS_INLINE inline auto
271 const unsigned int i, const state_type &U_i) const -> Bounds
272 {
273 const auto view = hyperbolic_system.view<dim, Number>();
274 const auto rho_i = view.density(U_i);
275 const auto &[s_i, eta_i] =
276 precomputed_values.template get_tensor<Number, precomputed_type>(i);
277
278 return {/*rho_min*/ rho_i, /*rho_max*/ rho_i, /*s_min*/ s_i};
279 }
280
281
282 template <int dim, typename Number>
283 DEAL_II_ALWAYS_INLINE inline auto Limiter<dim, Number>::combine_bounds(
284 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
285 {
286 const auto &[rho_min_l, rho_max_l, s_min_l] = bounds_left;
287 const auto &[rho_min_r, rho_max_r, s_min_r] = bounds_right;
288
289 return {std::min(rho_min_l, rho_min_r),
290 std::max(rho_max_l, rho_max_r),
291 std::min(s_min_l, s_min_r)};
292 }
293
294
295 template <int dim, typename Number>
296 DEAL_II_ALWAYS_INLINE inline auto
298 const Number &hd) const -> Bounds
299 {
300 auto relaxed_bounds = bounds;
301 auto &[rho_min, rho_max, s_min] = relaxed_bounds;
302
303 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
304
305 Number r = std::sqrt(hd); // in 3D: ^ 3/6
306 if constexpr (dim == 2) //
307 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
308 else if constexpr (dim == 1) //
309 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
310 r *= parameters.relaxation_factor();
311
312 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
313 rho_min *= std::max(Number(1.) - r, Number(eps));
314 rho_max *= (Number(1.) + r);
315 s_min *= std::max(Number(1.) - r, Number(eps));
316
317 return relaxed_bounds;
318 }
319
320
321 template <int dim, typename Number>
322 DEAL_II_ALWAYS_INLINE inline void
323 Limiter<dim, Number>::reset(const unsigned int /*i*/,
324 const state_type &new_U_i,
325 const flux_contribution_type & /*new_flux_i*/)
326 {
327 U_i = new_U_i;
328
329 /* Bounds: */
330
331 auto &[rho_min, rho_max, s_min] = bounds_;
332
333 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
334 rho_max = Number(0.);
335 s_min = Number(std::numeric_limits<ScalarNumber>::max());
336
337 /* Relaxation: */
338
339 rho_relaxation_numerator = Number(0.);
340 rho_relaxation_denominator = Number(0.);
341 s_interp_max = Number(0.);
342 }
343
344
345 template <int dim, typename Number>
346 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
347 const unsigned int *js,
348 const state_type &U_j,
349 const flux_contribution_type & /*flux_j*/,
350 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
351 const state_type &affine_shift)
352 {
353 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
354 // then enters all bounds), but we do not modify s_interp and
355 // rho_relaxation. When actually adding a source term to the Euler
356 // equations verify that this does the right thing.
357 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
358 dealii::ExcNotImplemented());
359
360 const auto view = hyperbolic_system.view<dim, Number>();
361
362 /* Bounds: */
363 auto &[rho_min, rho_max, s_min] = bounds_;
364
365 const auto rho_i = view.density(U_i);
366 const auto m_i = view.momentum(U_i);
367 const auto rho_j = view.density(U_j);
368 const auto m_j = view.momentum(U_j);
369 const auto rho_affine_shift = view.density(affine_shift);
370
371 /* bar state shifted by an affine shift: */
372 const auto rho_ij_bar =
373 ScalarNumber(0.5) * (rho_i + rho_j + (m_i - m_j) * scaled_c_ij) +
374 rho_affine_shift;
375
376 rho_min = std::min(rho_min, rho_ij_bar);
377 rho_max = std::max(rho_max, rho_ij_bar);
378
379 const auto &[s_j, eta_j] =
380 precomputed_values.template get_tensor<Number, precomputed_type>(js);
381 s_min = std::min(s_min, s_j);
382
383 /* Relaxation: */
384
385 /* Use a uniform weight. */
386 const auto beta_ij = Number(1.);
387 rho_relaxation_numerator += beta_ij * (rho_i + rho_j);
388 rho_relaxation_denominator += std::abs(beta_ij);
389
390 const Number s_interp =
391 view.specific_entropy((U_i + U_j) * ScalarNumber(.5));
392 s_interp_max = std::max(s_interp_max, s_interp);
393 }
394
395
396 template <int dim, typename Number>
397 DEAL_II_ALWAYS_INLINE inline auto
398 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
399 {
400 const auto &[rho_min, rho_max, s_min] = bounds_;
401
402 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
403 auto &[rho_min_relaxed, rho_max_relaxed, s_min_relaxed] = relaxed_bounds;
404
405 /* Apply a stricter window: */
406
407 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
408
409 const auto rho_relaxation =
410 ScalarNumber(2. * parameters.relaxation_factor()) *
411 std::abs(rho_relaxation_numerator) /
412 (std::abs(rho_relaxation_denominator) + Number(eps));
413
414 const auto entropy_relaxation =
415 parameters.relaxation_factor() * (s_interp_max - s_min);
416
417 rho_min_relaxed = std::max(rho_min_relaxed, rho_min - rho_relaxation);
418 rho_max_relaxed = std::min(rho_max_relaxed, rho_max + rho_relaxation);
419 s_min_relaxed = std::max(s_min_relaxed, s_min - entropy_relaxation);
420
421 return relaxed_bounds;
422 }
423 } // namespace Euler
424} // namespace ryujin
typename get_value_type< Number >::type ScalarNumber
Vectors::MultiComponentVector< ScalarNumber, n_precomputed_values > PrecomputedVector
static constexpr unsigned int problem_dimension
dealii::Tensor< 1, problem_dimension, Number > state_type
std::array< Number, n_precomputed_values > precomputed_type
ACCESSOR_READ_ONLY(newton_max_iterations)
ACCESSOR_READ_ONLY(newton_tolerance)
ACCESSOR_READ_ONLY(relaxation_factor)
LimiterParameters(const std::string &subsection="/Limiter")
Definition: limiter.h:24
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const PrecomputedVector &precomputed_values)
Definition: limiter.h:139
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:323
typename View::PrecomputedVector PrecomputedVector
Definition: limiter.h:116
static constexpr auto problem_dimension
Definition: limiter.h:108
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition: limiter.h:297
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
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:346
typename View::precomputed_type precomputed_type
Definition: limiter.h:114
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:106
typename View::state_type state_type
Definition: limiter.h:110
LimiterParameters< ScalarNumber > Parameters
Definition: limiter.h:118
Bounds bounds(const Number hd_i) const
Definition: limiter.h:398
static constexpr unsigned int n_bounds
Definition: limiter.h:129
std::array< Number, n_bounds > Bounds
Definition: limiter.h:134
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:112
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition: limiter.h:283
Bounds projection_bounds_from_state(const unsigned int i, const state_type &U_i) const
Definition: limiter.h:270