ryujin 2.1.1 revision 7b33fa74945ccba789651fca714670471a34aef7
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 Euler
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
94 template <int dim, typename Number = double>
95 class Limiter
96 {
97 public:
102
104
106
108
109 using state_type = typename View::state_type;
110
112
114
116
118
120
138
142 static constexpr unsigned int n_bounds = 3;
143
147 using Bounds = std::array<Number, n_bounds>;
148
152 Limiter(const HyperbolicSystem &hyperbolic_system,
153 const Parameters &parameters,
154 const PrecomputedVector &precomputed_values)
155 : hyperbolic_system(hyperbolic_system)
156 , parameters(parameters)
157 , precomputed_values(precomputed_values)
158 {
159 }
160
164 void reset(const unsigned int i,
165 const state_type &U_i,
166 const flux_contribution_type &flux_i);
167
172 void accumulate(const unsigned int *js,
173 const state_type &U_j,
174 const flux_contribution_type &flux_j,
175 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
176 const state_type &affine_shift);
177
181 Bounds bounds(const Number hd_i) const;
182
188 static Bounds combine_bounds(const Bounds &bounds_left,
189 const Bounds &bounds_right);
190
191 //*}
194
209 std::tuple<Number, bool> limit(const Bounds &bounds,
210 const state_type &U,
211 const state_type &P,
212 const Number t_min = Number(0.),
213 const Number t_max = Number(1.));
214 //*}
219
226 static bool
227 is_in_invariant_domain(const HyperbolicSystem &hyperbolic_system,
228 const Bounds &bounds,
229 const state_type &U);
230
231 private:
233
235
236 const HyperbolicSystem &hyperbolic_system;
237 const Parameters &parameters;
238 const PrecomputedVector &precomputed_values;
239
240 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 /* Inline definitions */
253
254
255 template <int dim, typename Number>
256 DEAL_II_ALWAYS_INLINE inline void
257 Limiter<dim, Number>::reset(const unsigned int /*i*/,
258 const state_type &new_U_i,
259 const flux_contribution_type & /*new_flux_i*/)
260 {
261 U_i = new_U_i;
262
263 /* Bounds: */
264
265 auto &[rho_min, rho_max, s_min] = bounds_;
266
267 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
268 rho_max = Number(0.);
269 s_min = Number(std::numeric_limits<ScalarNumber>::max());
270
271 /* Relaxation: */
272
273 rho_relaxation_numerator = Number(0.);
274 rho_relaxation_denominator = Number(0.);
275 s_interp_max = Number(0.);
276 }
277
278
279 template <int dim, typename Number>
280 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
281 const unsigned int *js,
282 const state_type &U_j,
283 const flux_contribution_type & /*flux_j*/,
284 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
285 const state_type &affine_shift)
286 {
287 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
288 // then enters all bounds), but we do not modify s_interp and
289 // rho_relaxation. When actually adding a source term to the Euler
290 // equations verify that this does the right thing.
291 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
292 dealii::ExcNotImplemented());
293
294 const auto view = hyperbolic_system.view<dim, Number>();
295
296 /* Bounds: */
297 auto &[rho_min, rho_max, s_min] = bounds_;
298
299 const auto rho_i = view.density(U_i);
300 const auto m_i = view.momentum(U_i);
301 const auto rho_j = view.density(U_j);
302 const auto m_j = view.momentum(U_j);
303 const auto rho_affine_shift = view.density(affine_shift);
304
305 /* bar state shifted by an affine shift: */
306 const auto rho_ij_bar =
307 ScalarNumber(0.5) * (rho_i + rho_j + (m_i - m_j) * scaled_c_ij) +
308 rho_affine_shift;
309
310 rho_min = std::min(rho_min, rho_ij_bar);
311 rho_max = std::max(rho_max, rho_ij_bar);
312
313 const auto &[s_j, eta_j] =
314 precomputed_values.template get_tensor<Number, precomputed_type>(js);
315 s_min = std::min(s_min, s_j);
316
317 /* Relaxation: */
318
319 /* Use a uniform weight. */
320 const auto beta_ij = Number(1.);
321 rho_relaxation_numerator += beta_ij * (rho_i + rho_j);
322 rho_relaxation_denominator += std::abs(beta_ij);
323
324 const Number s_interp =
325 view.specific_entropy((U_i + U_j) * ScalarNumber(.5));
326 s_interp_max = std::max(s_interp_max, s_interp);
327 }
328
329
330 template <int dim, typename Number>
331 DEAL_II_ALWAYS_INLINE inline auto
332 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
333 {
334 auto relaxed_bounds = bounds_;
335 auto &[rho_min, rho_max, s_min] = relaxed_bounds;
336
337 /* Use r_i = factor * (m_i / |Omega|) ^ (1.5 / d): */
338
339 Number r_i = std::sqrt(hd_i); // in 3D: ^ 3/6
340 if constexpr (dim == 2) //
341 r_i = dealii::Utilities::fixed_power<3>(std::sqrt(r_i)); // in 2D: ^ 3/4
342 else if constexpr (dim == 1) //
343 r_i = dealii::Utilities::fixed_power<3>(r_i); // in 1D: ^ 3/2
344 r_i *= parameters.relaxation_factor();
345
346 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
347 const Number rho_relaxation =
348 std::abs(rho_relaxation_numerator) /
349 (std::abs(rho_relaxation_denominator) + Number(eps));
350
351 const auto relaxation =
352 ScalarNumber(2. * parameters.relaxation_factor()) * rho_relaxation;
353
354 rho_min = std::max((Number(1.) - r_i) * rho_min, rho_min - relaxation);
355 rho_max = std::min((Number(1.) + r_i) * rho_max, rho_max + relaxation);
356
357 const auto entropy_relaxation =
358 parameters.relaxation_factor() * (s_interp_max - s_min);
359
360 s_min = std::max((Number(1.) - r_i) * s_min, s_min - entropy_relaxation);
361
362 return relaxed_bounds;
363 }
364
365
366 template <int dim, typename Number>
367 DEAL_II_ALWAYS_INLINE inline auto
369 const Bounds &bounds_right) -> Bounds
370 {
371 const auto &[rho_min_l, rho_max_l, s_min_l] = bounds_left;
372 const auto &[rho_min_r, rho_max_r, s_min_r] = bounds_right;
373
374 return {std::min(rho_min_l, rho_min_r),
375 std::max(rho_max_l, rho_max_r),
376 std::min(s_min_l, s_min_r)};
377 }
378
379
380 template <int dim, typename Number>
381 DEAL_II_ALWAYS_INLINE inline bool
383 const HyperbolicSystem & /*hyperbolic_system*/,
384 const Bounds & /*bounds*/,
385 const state_type & /*U*/)
386 {
387 AssertThrow(false, dealii::ExcNotImplemented());
388 __builtin_trap();
389 return true;
390 }
391
392 } // namespace Euler
393} // 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:23
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const PrecomputedVector &precomputed_values)
Definition: limiter.h:152
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:257
typename View::PrecomputedVector PrecomputedVector
Definition: limiter.h:115
static Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right)
Definition: limiter.h:368
static constexpr auto problem_dimension
Definition: limiter.h:107
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:280
typename View::precomputed_type precomputed_type
Definition: limiter.h:113
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:105
typename View::state_type state_type
Definition: limiter.h:109
LimiterParameters< ScalarNumber > Parameters
Definition: limiter.h:117
Bounds bounds(const Number hd_i) const
Definition: limiter.h:332
static constexpr unsigned int n_bounds
Definition: limiter.h:142
std::array< Number, n_bounds > Bounds
Definition: limiter.h:147
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:111
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.))
static bool is_in_invariant_domain(const HyperbolicSystem &hyperbolic_system, const Bounds &bounds, const state_type &U)
Definition: limiter.h:382