ryujin 2.1.1 revision ee5cbcbf2346c1299c942d0e1f13b46449973c18
Loading...
Searching...
No Matches
limiter.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2023 - 2025 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 <observer_pointer.h>
14#include <simd.h>
15
16namespace ryujin
17{
18 namespace ScalarConservation
19 {
20 template <int dim, typename Number = double>
21 class LimiterView;
22
28 template <typename ScalarNumber = double>
29 class Limiter : public dealii::ParameterAcceptor
30 {
31 public:
36
41 template <int dim, typename Number = double>
43
45
49
53 Limiter(const HyperbolicSystem &hyperbolic_system,
54 const std::string &subsection = "/Limiter")
55 : ParameterAcceptor(subsection)
56 , hyperbolic_system_(&hyperbolic_system)
57 {
58 iterations_ = 2;
59 add_parameter(
60 "iterations", iterations_, "Number of limiter iterations");
61
62 relaxation_factor_ = ScalarNumber(1.);
63 add_parameter("relaxation factor",
64 relaxation_factor_,
65 "Factor for scaling the relaxation window with r_i = "
66 "factor * (m_i/|Omega|)^(1.5/d).");
67 }
68
74 template <int dim, typename Number>
75 auto view() const
76 {
77 return View<dim, Number>{
78 hyperbolic_system_->template view<dim, Number>(), *this};
79 }
80
81 private:
83
87
88 unsigned int iterations_;
89 ScalarNumber relaxation_factor_;
90
92
96
97 dealii::ObserverPointer<const HyperbolicSystem> hyperbolic_system_;
98
100
101 template <int, typename>
102 friend class LimiterView;
103 };
104
105
114 template <int dim, typename Number>
116 {
117 public:
122
124
126
128
129 using state_type = typename View::state_type;
130
132
134
136
138
142
146 static constexpr unsigned int n_bounds = 2;
147
151 using Bounds = std::array<Number, n_bounds>;
152
157 LimiterView(const View &view, const Limiter<ScalarNumber> &limiter)
158 : view_(view)
159 , limiter_(limiter)
160 {
161 }
162
166 unsigned int iterations() const
167 {
168 return limiter_.iterations_;
169 }
170
175 {
176 return limiter_.relaxation_factor_;
177 }
178
184 const unsigned int i,
185 const state_type &U_i) const;
186
192 Bounds combine_bounds(const Bounds &bounds_left,
193 const Bounds &bounds_right) const;
194
200 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
201
203
222
226 void reset(const PrecomputedVectorView &pv,
227 const unsigned int i,
228 const state_type &U_i,
229 const flux_contribution_type &flux_i);
230
235 void accumulate(const PrecomputedVectorView &pv,
236 const unsigned int *js,
237 const state_type &U_j,
238 const flux_contribution_type &flux_j,
239 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
240 const state_type &affine_shift);
241
245 Bounds bounds(const Number hd_i) const;
246
248
252
259 std::tuple<Number, bool> limit(const Bounds &bounds,
260 const state_type &U,
261 const state_type &P,
262 const Number t_min = Number(0.),
263 const Number t_max = Number(1.)) const;
264
265 private:
267
271
272 const View view_;
273 const Limiter<ScalarNumber> &limiter_;
274
275 state_type U_i_;
277
278 Bounds bounds_;
279
280 Number u_relaxation_numerator_;
281 Number u_relaxation_denominator_;
283 };
284
285
286 /*
287 * -------------------------------------------------------------------------
288 * Inline definitions
289 * -------------------------------------------------------------------------
290 */
291
292
293 template <int dim, typename Number>
294 DEAL_II_ALWAYS_INLINE inline auto
296 const PrecomputedVectorView & /*pv*/,
297 const unsigned int /*i*/,
298 const state_type &U_i) const -> Bounds
299 {
300 const auto u_i = view_.state(U_i);
301 return {/*u_min*/ u_i, /*u_max*/ u_i};
302 }
303
304
305 template <int dim, typename Number>
306 DEAL_II_ALWAYS_INLINE inline auto LimiterView<dim, Number>::combine_bounds(
307 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
308 {
309 const auto &[u_min_l, u_max_l] = bounds_left;
310 const auto &[u_min_r, u_max_r] = bounds_right;
311
312 return {std::min(u_min_l, u_min_r), std::max(u_max_l, u_max_r)};
313 }
314
315
316 template <int dim, typename Number>
317 DEAL_II_ALWAYS_INLINE inline auto
319 const Number &hd) const
320 -> Bounds
321 {
322 auto relaxed_bounds = bounds;
323 auto &[u_min, u_max] = relaxed_bounds;
324
325 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
326
327 Number r = std::sqrt(hd); // in 3D: ^ 3/6
328 if constexpr (dim == 2) //
329 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
330 else if constexpr (dim == 1) //
331 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
332 r *= relaxation_factor();
333
334 u_min = std::min((Number(1.) - r) * u_min, (Number(1.) + r) * u_min);
335 u_max = std::max((Number(1.) + r) * u_max, (Number(1.) - r) * u_max);
336
337 return relaxed_bounds;
338 }
339
340
341 template <int dim, typename Number>
342 DEAL_II_ALWAYS_INLINE inline void
344 const unsigned int /*i*/,
345 const state_type &U_i,
346 const flux_contribution_type &flux_i)
347 {
348 U_i_ = U_i;
349 flux_i_ = flux_i;
350
351 /* Bounds: */
352
353 auto &[u_min, u_max] = bounds_;
354
355 u_min = Number(std::numeric_limits<ScalarNumber>::max());
356 u_max = Number(std::numeric_limits<ScalarNumber>::lowest());
357
358 /* Relaxation: */
359
360 u_relaxation_numerator_ = Number(0.);
361 u_relaxation_denominator_ = Number(0.);
362 }
363
364
365 template <int dim, typename Number>
366 DEAL_II_ALWAYS_INLINE inline void LimiterView<dim, Number>::accumulate(
367 const PrecomputedVectorView & /*pv*/,
368 const unsigned int * /*js*/,
369 const state_type &U_j,
370 const flux_contribution_type &flux_j,
371 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
372 const state_type &affine_shift)
373 {
374 /* Bounds: */
375 auto &[u_min, u_max] = bounds_;
376
377 const auto u_i = view_.state(U_i_);
378 const auto u_j = view_.state(U_j);
379
380 const auto U_ij_bar =
381 ScalarNumber(0.5) * (U_i_ + U_j) -
382 ScalarNumber(0.5) * contract(add(flux_j, -flux_i_), scaled_c_ij) +
383 affine_shift;
384
385 const auto u_ij_bar = view_.state(U_ij_bar);
386
387 /* Bounds: */
388
389 u_min = std::min(u_min, u_ij_bar);
390 u_max = std::max(u_max, u_ij_bar);
391
392 /* Relaxation: */
393
394 /* Use a uniform weight. */
395 const auto beta_ij = Number(1.);
396 u_relaxation_numerator_ += beta_ij * (u_i + u_j);
397 u_relaxation_denominator_ += std::abs(beta_ij);
398 }
399
400
401 template <int dim, typename Number>
402 DEAL_II_ALWAYS_INLINE inline auto
403 LimiterView<dim, Number>::bounds(const Number hd_i) const -> Bounds
404 {
405 const auto &[u_min, u_max] = bounds_;
406
407 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
408 auto &[u_min_relaxed, u_max_relaxed] = relaxed_bounds;
409
410 /* Apply a stricter window: */
411
412 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
413
414 const Number u_relaxation =
415 ScalarNumber(2. * relaxation_factor()) *
416 std::abs(u_relaxation_numerator_) /
417 (std::abs(u_relaxation_denominator_) + Number(eps));
418
419 u_min_relaxed = std::max(u_min_relaxed, u_min - u_relaxation);
420 u_max_relaxed = std::min(u_max_relaxed, u_max + u_relaxation);
421
422 return relaxed_bounds;
423 }
424 } // namespace ScalarConservation
425} // namespace ryujin
std::array< Number, n_precomputed_values > precomputed_type
Vectors::MultiComponentVectorView< ScalarNumber, n_precomputed_values, dealii::VectorizedArray< ScalarNumber >::size(), dealii::MemorySpace::Host, false > PrecomputedVectorView
typename get_value_type< Number >::type ScalarNumber
dealii::Tensor< 1, problem_dimension, Number > state_type
LimiterView(const View &view, const Limiter< ScalarNumber > &limiter)
Definition limiter.h:157
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition limiter.h:306
typename View::state_type state_type
Definition limiter.h:129
void reset(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition limiter.h:343
std::array< Number, n_bounds > Bounds
Definition limiter.h:151
typename View::PrecomputedVectorView PrecomputedVectorView
Definition limiter.h:135
ScalarNumber relaxation_factor() const
Definition limiter.h:174
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
HyperbolicSystemView< dim, Number > View
Definition limiter.h:123
static constexpr auto problem_dimension
Definition limiter.h:127
typename View::precomputed_type precomputed_type
Definition limiter.h:133
static constexpr unsigned int n_bounds
Definition limiter.h:146
Bounds bounds(const Number hd_i) const
Definition limiter.h:403
typename View::ScalarNumber ScalarNumber
Definition limiter.h:125
Bounds projection_bounds_from_state(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i) const
Definition limiter.h:295
typename View::flux_contribution_type flux_contribution_type
Definition limiter.h:131
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition limiter.h:318
void accumulate(const PrecomputedVectorView &pv, 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:366
Limiter(const HyperbolicSystem &hyperbolic_system, const std::string &subsection="/Limiter")
Definition limiter.h:53
DEAL_II_HOST_DEVICE_ALWAYS_INLINE dealii::Tensor< 1, problem_dim, T > contract(const FT &flux_ij, const TT &c_ij)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)