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) 2023 - 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 <simd.h>
14
15namespace ryujin
16{
17 namespace ScalarConservation
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 relaxation_factor_ = ScalarNumber(1.);
31 add_parameter("relaxation factor",
32 relaxation_factor_,
33 "Factor for scaling the relaxation window with r_i = "
34 "factor * (m_i/|Omega|)^(1.5/d).");
35 }
36
37 ACCESSOR_READ_ONLY(iterations);
38 ACCESSOR_READ_ONLY(relaxation_factor);
39
40 private:
41 unsigned int iterations_;
42 ScalarNumber relaxation_factor_;
43 };
44
45
51 template <int dim, typename Number = double>
52 class Limiter
53 {
54 public:
59
61
63
65
66 using state_type = typename View::state_type;
67
69
71
73
75
77
80 //
82
86 static constexpr unsigned int n_bounds = 2;
87
91 using Bounds = std::array<Number, n_bounds>;
92
96 Limiter(const HyperbolicSystem &hyperbolic_system,
97 const Parameters &parameters,
98 const PrecomputedVector &precomputed_values)
99 : hyperbolic_system(hyperbolic_system)
100 , parameters(parameters)
101 , precomputed_values(precomputed_values)
102 {
103 }
104
110 const state_type &U_i) const;
111
117 Bounds combine_bounds(const Bounds &bounds_left,
118 const Bounds &bounds_right) const;
119
125 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
126
128
146
150 void reset(const unsigned int i,
151 const state_type &U_i,
152 const flux_contribution_type &flux_i);
153
158 void accumulate(const unsigned int *js,
159 const state_type &U_j,
160 const flux_contribution_type &flux_j,
161 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
162 const state_type &affine_shift);
163
167 Bounds bounds(const Number hd_i) const;
168
169 //*}
172
179 std::tuple<Number, bool> limit(const Bounds &bounds,
180 const state_type &U,
181 const state_type &P,
182 const Number t_min = Number(0.),
183 const Number t_max = Number(1.)) const;
184
185 private:
187
189
190 const HyperbolicSystem &hyperbolic_system;
191 const Parameters &parameters;
192 const PrecomputedVector &precomputed_values;
193
194 state_type U_i;
196
197 Bounds bounds_;
198
199 Number u_relaxation_numerator;
200 Number u_relaxation_denominator;
202 };
203
204
205 /*
206 * -------------------------------------------------------------------------
207 * Inline definitions
208 * -------------------------------------------------------------------------
209 */
210
211
212 template <int dim, typename Number>
213 DEAL_II_ALWAYS_INLINE inline auto
215 const unsigned int /*i*/, const state_type &U_i) const -> Bounds
216 {
217 const auto view = hyperbolic_system.view<dim, Number>();
218 const auto u_i = view.state(U_i);
219 return {/*u_min*/ u_i, /*u_max*/ u_i};
220 }
221
222
223 template <int dim, typename Number>
224 DEAL_II_ALWAYS_INLINE inline auto Limiter<dim, Number>::combine_bounds(
225 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
226 {
227 const auto &[u_min_l, u_max_l] = bounds_left;
228 const auto &[u_min_r, u_max_r] = bounds_right;
229
230 return {std::min(u_min_l, u_min_r), std::max(u_max_l, u_max_r)};
231 }
232
233
234 template <int dim, typename Number>
235 DEAL_II_ALWAYS_INLINE inline auto
237 const Number &hd) const -> Bounds
238 {
239 auto relaxed_bounds = bounds;
240 auto &[u_min, u_max] = relaxed_bounds;
241
242 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
243
244 Number r = std::sqrt(hd); // in 3D: ^ 3/6
245 if constexpr (dim == 2) //
246 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
247 else if constexpr (dim == 1) //
248 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
249 r *= parameters.relaxation_factor();
250
251 u_min = std::min((Number(1.) - r) * u_min, (Number(1.) + r) * u_min);
252 u_max = std::max((Number(1.) + r) * u_max, (Number(1.) - r) * u_max);
253
254 return relaxed_bounds;
255 }
256
257
258 template <int dim, typename Number>
259 DEAL_II_ALWAYS_INLINE inline void
260 Limiter<dim, Number>::reset(const unsigned int /*i*/,
261 const state_type &new_U_i,
262 const flux_contribution_type &new_flux_i)
263 {
264 U_i = new_U_i;
265 flux_i = new_flux_i;
266
267 /* Bounds: */
268
269 auto &[u_min, u_max] = bounds_;
270
271 u_min = Number(std::numeric_limits<ScalarNumber>::max());
272 u_max = Number(std::numeric_limits<ScalarNumber>::lowest());
273
274 /* Relaxation: */
275
276 u_relaxation_numerator = Number(0.);
277 u_relaxation_denominator = Number(0.);
278 }
279
280
281 template <int dim, typename Number>
282 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
283 const unsigned int * /*js*/,
284 const state_type &U_j,
285 const flux_contribution_type &flux_j,
286 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
287 const state_type &affine_shift)
288 {
289 const auto view = hyperbolic_system.view<dim, Number>();
290
291 /* Bounds: */
292 auto &[u_min, u_max] = bounds_;
293
294 const auto u_i = view.state(U_i);
295 const auto u_j = view.state(U_j);
296
297 const auto U_ij_bar =
298 ScalarNumber(0.5) * (U_i + U_j) -
299 ScalarNumber(0.5) * contract(add(flux_j, -flux_i), scaled_c_ij) +
300 affine_shift;
301
302 const auto u_ij_bar = view.state(U_ij_bar);
303
304 /* Bounds: */
305
306 u_min = std::min(u_min, u_ij_bar);
307 u_max = std::max(u_max, u_ij_bar);
308
309 /* Relaxation: */
310
311 /* Use a uniform weight. */
312 const auto beta_ij = Number(1.);
313 u_relaxation_numerator += beta_ij * (u_i + u_j);
314 u_relaxation_denominator += std::abs(beta_ij);
315 }
316
317
318 template <int dim, typename Number>
319 DEAL_II_ALWAYS_INLINE inline auto
320 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
321 {
322 const auto &[u_min, u_max] = bounds_;
323
324 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
325 auto &[u_min_relaxed, u_max_relaxed] = relaxed_bounds;
326
327 /* Apply a stricter window: */
328
329 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
330
331 const Number u_relaxation =
332 ScalarNumber(2. * parameters.relaxation_factor()) *
333 std::abs(u_relaxation_numerator) /
334 (std::abs(u_relaxation_denominator) + Number(eps));
335
336 u_min_relaxed = std::max(u_min_relaxed, u_min - u_relaxation);
337 u_max_relaxed = std::min(u_max_relaxed, u_max + u_relaxation);
338
339 return relaxed_bounds;
340 }
341 } // namespace ScalarConservation
342} // namespace ryujin
std::array< Number, n_precomputed_values > precomputed_type
Vectors::MultiComponentVector< ScalarNumber, n_precomputed_values > PrecomputedVector
typename get_value_type< Number >::type ScalarNumber
dealii::Tensor< 1, problem_dimension, Number > state_type
LimiterParameters(const std::string &subsection="/Limiter")
Definition: limiter.h:23
std::array< Number, n_bounds > Bounds
Definition: limiter.h:91
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const PrecomputedVector &precomputed_values)
Definition: limiter.h:96
typename View::PrecomputedVector PrecomputedVector
Definition: limiter.h:72
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition: limiter.h:236
typename View::state_type state_type
Definition: limiter.h:66
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition: limiter.h:224
Bounds projection_bounds_from_state(const unsigned int i, const state_type &U_i) const
Definition: limiter.h:214
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:260
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
static constexpr unsigned int n_bounds
Definition: limiter.h:86
typename View::ScalarNumber ScalarNumber
Definition: limiter.h:62
static constexpr auto problem_dimension
Definition: limiter.h:64
Bounds bounds(const Number hd_i) const
Definition: limiter.h:320
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:68
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:282
typename View::precomputed_type precomputed_type
Definition: limiter.h:70
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)