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 <newton.h>
14#include <observer_pointer.h>
15#include <simd.h>
16
17namespace ryujin
18{
19 namespace EulerBarotropic
20 {
21 template <int dim, typename Number = double>
22 class LimiterView;
23
29 template <typename ScalarNumber = double>
30 class Limiter : public dealii::ParameterAcceptor
31 {
32 public:
37
42 template <int dim, typename Number = double>
44
46
50
54 Limiter(const HyperbolicSystem &hyperbolic_system,
55 const std::string &subsection = "/Limiter")
56 : ParameterAcceptor(subsection)
57 , hyperbolic_system_(&hyperbolic_system)
58 {
59 iterations_ = 2;
60 add_parameter(
61 "iterations", iterations_, "Number of limiter iterations");
62
63 relaxation_factor_ = ScalarNumber(1.);
64 add_parameter("relaxation factor",
65 relaxation_factor_,
66 "Factor for scaling the relaxation window with r_i = "
67 "factor * (m_i/|Omega|)^(1.5/d).");
68 }
69
75 template <int dim, typename Number>
76 auto view() const
77 {
78 return View<dim, Number>{
79 hyperbolic_system_->template view<dim, Number>(), *this};
80 }
81
82 private:
84
88
89 unsigned int iterations_;
90 ScalarNumber relaxation_factor_;
91
93
97
98 dealii::ObserverPointer<const HyperbolicSystem> hyperbolic_system_;
99
101
102 template <int, typename>
103 friend class LimiterView;
104 };
105
106
115 template <int dim, typename Number>
117 {
118 public:
123
125
127
129
130 using state_type = typename View::state_type;
131
133
135
137
139
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
203 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
204
206
225
229 void reset(const PrecomputedVectorView &pv,
230 const unsigned int i,
231 const state_type &U_i,
232 const flux_contribution_type &flux_i);
233
238 void accumulate(const PrecomputedVectorView &pv,
239 const unsigned int *js,
240 const state_type &U_j,
241 const flux_contribution_type &flux_j,
242 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
243 const state_type &affine_shift);
244
248 Bounds bounds(const Number hd_i) const;
249
251
255
271 std::tuple<Number, bool> limit(const Bounds &bounds,
272 const state_type &U,
273 const state_type &P,
274 const Number t_min = Number(0.),
275 const Number t_max = Number(1.)) const;
276
277 private:
279
283
284 const View view_;
285 const Limiter<ScalarNumber> &limiter_;
286
287 state_type U_i_;
289
290 Bounds bounds_;
291
292 Number rho_relaxation_numerator_;
293 Number rho_relaxation_denominator_;
294
296 };
297
298
299 /*
300 * -------------------------------------------------------------------------
301 * Inline definitions
302 * -------------------------------------------------------------------------
303 */
304
305
306 template <int dim, typename Number>
307 DEAL_II_ALWAYS_INLINE inline auto
309 const PrecomputedVectorView & /*pv*/,
310 const unsigned int /*i*/,
311 const state_type &U_i) const -> Bounds
312 {
313 const auto rho_i = view_.density(U_i);
314 return {/*rho_min*/ rho_i, /*rho_max*/ rho_i};
315 }
316
317
318 template <int dim, typename Number>
319 DEAL_II_ALWAYS_INLINE inline auto LimiterView<dim, Number>::combine_bounds(
320 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
321 {
322 const auto &[rho_min_l, rho_max_l] = bounds_left;
323 const auto &[rho_min_r, rho_max_r] = bounds_right;
324
325 return {std::min(rho_min_l, rho_min_r), std::max(rho_max_l, rho_max_r)};
326 }
327
328
329 template <int dim, typename Number>
330 DEAL_II_ALWAYS_INLINE inline auto
332 const Number &hd) const
333 -> Bounds
334 {
335 auto relaxed_bounds = bounds;
336 auto &[rho_min_relaxed, rho_max_relaxed] = relaxed_bounds;
337
338 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
339
340 Number r = std::sqrt(hd); // in 3D: ^ 3/6
341 if constexpr (dim == 2) //
342 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
343 else if constexpr (dim == 1) //
344 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
345 r *= relaxation_factor();
346
347 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
348 rho_min_relaxed *= std::max(Number(1.) - r, Number(eps));
349 rho_max_relaxed *= (Number(1.) + r);
350
351 return relaxed_bounds;
352 }
353
354
355 template <int dim, typename Number>
356 DEAL_II_ALWAYS_INLINE inline void
358 const unsigned int /*i*/,
359 const state_type &U_i,
360 const flux_contribution_type &flux_i)
361 {
362 U_i_ = U_i;
363 flux_i_ = flux_i;
364
365 /* Bounds: */
366
367 auto &[rho_min, rho_max] = bounds_;
368
369 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
370 rho_max = Number(0.);
371
372 /* Relaxation: */
373
374 rho_relaxation_numerator_ = Number(0.);
375 rho_relaxation_denominator_ = Number(0.);
376 }
377
378
379 template <int dim, typename Number>
380 DEAL_II_ALWAYS_INLINE inline void LimiterView<dim, Number>::accumulate(
381 const PrecomputedVectorView & /*pv*/,
382 const unsigned int * /*js*/,
383 const state_type &U_j,
384 const flux_contribution_type &flux_j,
385 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
386 const state_type &affine_shift)
387 {
388 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
389 // then enters all bounds), but we do not modify s_interp and
390 // rho_relaxation. When actually adding a source term to the Euler
391 // equations verify that this does the right thing.
392 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
393 dealii::ExcNotImplemented());
394
395 /* Bounds: */
396 auto &[rho_min, rho_max] = bounds_;
397
398 const auto rho_i = view_.density(U_i_);
399 const auto rho_j = view_.density(U_j);
400
401 /* bar state shifted by an affine shift: */
402 const auto U_ij_bar =
403 ScalarNumber(0.5) * (U_i_ + U_j) -
404 ScalarNumber(0.5) * contract(add(flux_j, -flux_i_), scaled_c_ij) +
405 affine_shift;
406
407 const auto rho_ij_bar = view_.density(U_ij_bar);
408
409 /* Density bounds: */
410
411 rho_min = std::min(rho_min, rho_ij_bar);
412 rho_max = std::max(rho_max, rho_ij_bar);
413
414 /* Density relaxation: */
415
416 /* Use a uniform weight. */
417 const auto beta_ij = Number(1.);
418 rho_relaxation_numerator_ += beta_ij * (rho_i + rho_j);
419 rho_relaxation_denominator_ += std::abs(beta_ij);
420 }
421
422
423 template <int dim, typename Number>
424 DEAL_II_ALWAYS_INLINE inline auto
425 LimiterView<dim, Number>::bounds(const Number hd_i) const -> Bounds
426 {
427 const auto &[rho_min, rho_max] = bounds_;
428
429 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
430 auto &[rho_min_relaxed, rho_max_relaxed] = relaxed_bounds;
431
432 /* Apply a stricter window: */
433
434 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
435
436 const auto rho_relaxation =
437 ScalarNumber(2. * relaxation_factor()) *
438 std::abs(rho_relaxation_numerator_) /
439 (std::abs(rho_relaxation_denominator_) + Number(eps));
440
441 rho_min_relaxed = std::max(rho_min_relaxed, rho_min - rho_relaxation);
442 rho_max_relaxed = std::min(rho_max_relaxed, rho_max + rho_relaxation);
443
444 return relaxed_bounds;
445 }
446 } // namespace EulerBarotropic
447} // namespace ryujin
std::array< Number, n_precomputed_values > precomputed_type
dealii::Tensor< 1, problem_dimension, Number > state_type
static constexpr unsigned int problem_dimension
typename get_value_type< Number >::type ScalarNumber
Vectors::MultiComponentVectorView< ScalarNumber, n_precomputed_values, dealii::VectorizedArray< ScalarNumber >::size(), dealii::MemorySpace::Host, false > PrecomputedVectorView
typename View::state_type state_type
Definition limiter.h:130
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
typename View::ScalarNumber ScalarNumber
Definition limiter.h:126
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:319
static constexpr auto problem_dimension
Definition limiter.h:128
HyperbolicSystemView< dim, Number > View
Definition limiter.h:124
typename View::precomputed_type precomputed_type
Definition limiter.h:134
Bounds projection_bounds_from_state(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i) const
Definition limiter.h:308
ScalarNumber relaxation_factor() const
Definition limiter.h:174
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition limiter.h:331
static constexpr unsigned int n_bounds
Definition limiter.h:146
Bounds bounds(const Number hd_i) const
Definition limiter.h:425
unsigned int iterations() const
Definition limiter.h:166
typename View::flux_contribution_type flux_contribution_type
Definition limiter.h:132
std::array< Number, n_bounds > Bounds
Definition limiter.h:151
void reset(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition limiter.h:357
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:380
typename View::PrecomputedVectorView PrecomputedVectorView
Definition limiter.h:136
Limiter(const HyperbolicSystem &hyperbolic_system, const std::string &subsection="/Limiter")
Definition limiter.h:54
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)