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) 2020 - 2026 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 EulerAEOS
20 {
21 template <int dim, typename Number = double>
22 class LimiterView;
23
56 template <typename ScalarNumber = double>
57 class Limiter : public dealii::ParameterAcceptor
58 {
59 public:
64
69 template <int dim, typename Number = double>
71
73
77
81 Limiter(const HyperbolicSystem &hyperbolic_system,
82 const std::string &subsection = "/Limiter")
83 : ParameterAcceptor(subsection)
84 , hyperbolic_system_(&hyperbolic_system)
85 {
86 iterations_ = 2;
87 add_parameter(
88 "iterations", iterations_, "Number of limiter iterations");
89
90 if constexpr (std::is_same_v<ScalarNumber, double>)
91 newton_tolerance_ = 1.e-10;
92 else
93 newton_tolerance_ = 1.e-4;
94 add_parameter("newton tolerance",
95 newton_tolerance_,
96 "Tolerance for the quadratic newton stopping criterion");
97
98 newton_max_iterations_ = 2;
99 add_parameter("newton max iterations",
100 newton_max_iterations_,
101 "Maximal number of quadratic newton iterations performed "
102 "during limiting");
103
104 relaxation_factor_ = ScalarNumber(1.);
105 add_parameter("relaxation factor",
106 relaxation_factor_,
107 "Factor for scaling the relaxation window with r_i = "
108 "factor * (m_i/|Omega|)^(1.5/d).");
109 }
110
116 template <int dim, typename Number>
117 auto view() const
118 {
119 return View<dim, Number>{
120 hyperbolic_system_->template view<dim, Number>(), *this};
121 }
122
123 private:
125
129
130 unsigned int iterations_;
131 ScalarNumber newton_tolerance_;
132 unsigned int newton_max_iterations_;
133 ScalarNumber relaxation_factor_;
134
136
140
141 dealii::ObserverPointer<const HyperbolicSystem> hyperbolic_system_;
142
144
145 template <int, typename>
146 friend class LimiterView;
147 };
148
149
158 template <int dim, typename Number>
160 {
161 public:
166
168
170
172
173 using state_type = typename View::state_type;
174
176
178
180
182
189 static constexpr unsigned int n_bounds = 4;
190
194 using Bounds = std::array<Number, n_bounds>;
195
200 LimiterView(const View &view, const Limiter<ScalarNumber> &limiter)
201 : view_(view)
202 , limiter_(limiter)
203 {
204 }
205
209 unsigned int iterations() const
210 {
211 return limiter_.iterations_;
212 }
213
218 {
219 return limiter_.newton_tolerance_;
220 }
221
225 unsigned int newton_max_iterations() const
226 {
227 return limiter_.newton_max_iterations_;
228 }
229
234 {
235 return limiter_.relaxation_factor_;
236 }
237
243 const unsigned int i,
244 const state_type &U_i) const;
245
251 Bounds combine_bounds(const Bounds &bounds_left,
252 const Bounds &bounds_right) const;
253
262 Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const;
263
265
284
288 void reset(const PrecomputedVectorView &pv,
289 const unsigned int i,
290 const state_type &U_i,
291 const flux_contribution_type &flux_i);
292
297 void accumulate(const PrecomputedVectorView &pv,
298 const unsigned int *js,
299 const state_type &U_j,
300 const flux_contribution_type &flux_j,
301 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
302 const state_type &affine_shift);
303
307 Bounds bounds(const Number hd_i) const;
308
310
314
330 std::tuple<Number, bool> limit(const Bounds &bounds,
331 const state_type &U,
332 const state_type &P,
333 const Number t_min = Number(0.),
334 const Number t_max = Number(1.)) const;
335
336 private:
338
342
343 const View view_;
344 const Limiter<ScalarNumber> &limiter_;
345
346 state_type U_i_;
348
349 Bounds bounds_;
350
351 Number rho_relaxation_numerator_;
352 Number rho_relaxation_denominator_;
353 Number s_interp_max_;
354
356 };
357
358
359 /*
360 * -------------------------------------------------------------------------
361 * Inline definitions
362 * -------------------------------------------------------------------------
363 */
364
365
366 template <int dim, typename Number>
367 DEAL_II_ALWAYS_INLINE inline auto
369 const PrecomputedVectorView &pv,
370 const unsigned int i,
371 const state_type &U_i) const -> Bounds
372 {
373 const auto rho_i = view_.density(U_i);
374 const auto &[p_i, gamma_min_i, s_i, eta_i] =
375 pv.template read_tensor<Number, precomputed_type>(i);
376
377 return {/*rho_min*/ rho_i,
378 /*rho_max*/ rho_i,
379 /*s_min*/ s_i,
380 /*gamma_min*/ gamma_min_i};
381 }
382
383
384 template <int dim, typename Number>
385 DEAL_II_ALWAYS_INLINE inline auto LimiterView<dim, Number>::combine_bounds(
386 const Bounds &bounds_left, const Bounds &bounds_right) const -> Bounds
387 {
388 const auto &[rho_min_l, rho_max_l, s_min_l, gamma_min_l] = bounds_left;
389 const auto &[rho_min_r, rho_max_r, s_min_r, gamma_min_r] = bounds_right;
390
391 return {std::min(rho_min_l, rho_min_r),
392 std::max(rho_max_l, rho_max_r),
393 std::min(s_min_l, s_min_r),
394 std::min(gamma_min_l, gamma_min_r)};
395 }
396
397
398 template <int dim, typename Number>
399 DEAL_II_ALWAYS_INLINE inline auto
401 const Number &hd) const
402 -> Bounds
403 {
404 const auto &[rho_min, rho_max, s_min, gamma_min] = bounds;
405
406 auto relaxed_bounds = bounds;
407 auto &[rho_min_relaxed, rho_max_relaxed, s_min_relaxed, g_m_relaxed] =
408 relaxed_bounds;
409
410 /* Use r = factor * (m_i / |Omega|) ^ (1.5 / d): */
411
412 Number r = std::sqrt(hd); // in 3D: ^ 3/6
413 if constexpr (dim == 2) //
414 r = dealii::Utilities::fixed_power<3>(std::sqrt(r)); // in 2D: ^ 3/4
415 else if constexpr (dim == 1) //
416 r = dealii::Utilities::fixed_power<3>(r); // in 1D: ^ 3/2
417 r *= relaxation_factor();
418
419 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
420 rho_min_relaxed *= std::max(Number(1.) - r, Number(eps));
421 rho_max_relaxed *= (Number(1.) + r);
422 s_min_relaxed *= std::max(Number(1.) - r, Number(eps));
423
424 /*
425 * If we have a maximum compressibility constant, b, the maximum
426 * bound for rho changes. See @cite ryujin-2023-4 for how to define
427 * rho_max.
428 */
429
430 const auto numerator = (gamma_min + Number(1.)) * rho_max;
431 const auto covolume_b = view_.eos_covolume_constant();
432 const auto denominator =
433 gamma_min - Number(1.) + ScalarNumber(2.) * covolume_b * rho_max;
434 const auto rho_compressibility_bound = numerator / denominator;
435
436 rho_max_relaxed = std::min(rho_compressibility_bound, rho_max_relaxed);
437
438 return relaxed_bounds;
439 }
440
441
442 template <int dim, typename Number>
443 DEAL_II_ALWAYS_INLINE inline void
445 const unsigned int i,
446 const state_type &U_i,
447 const flux_contribution_type &flux_i)
448 {
449 U_i_ = U_i;
450 flux_i_ = flux_i;
451
452 /* Bounds: */
453
454 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
455
456 rho_min = Number(std::numeric_limits<ScalarNumber>::max());
457 rho_max = Number(0.);
458 s_min = Number(std::numeric_limits<ScalarNumber>::max());
459
460 const auto &[p_i, gamma_min_i, s_i, eta_i] =
461 pv.template read_tensor<Number, precomputed_type>(i);
462
463 gamma_min = gamma_min_i;
464
465 /* Relaxation: */
466
467 rho_relaxation_numerator_ = Number(0.);
468 rho_relaxation_denominator_ = Number(0.);
469 s_interp_max_ = Number(0.);
470 }
471
472
473 template <int dim, typename Number>
474 DEAL_II_ALWAYS_INLINE inline void LimiterView<dim, Number>::accumulate(
475 const PrecomputedVectorView &pv,
476 const unsigned int *js,
477 const state_type &U_j,
478 const flux_contribution_type &flux_j,
479 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
480 const state_type &affine_shift)
481 {
482 // TODO: Currently we only apply the affine_shift to U_ij_bar (which
483 // then enters all bounds), but we do not modify s_interp and
484 // rho_relaxation. When actually adding a source term to the Euler
485 // equations verify that this does the right thing.
486 Assert(std::max(affine_shift.norm(), Number(0.)) == Number(0.),
487 dealii::ExcNotImplemented());
488
489 /* Bounds: */
490 auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
491
492 const auto rho_i = view_.density(U_i_);
493 const auto rho_j = view_.density(U_j);
494
495 /* bar state shifted by an affine shift: */
496 const auto U_ij_bar =
497 ScalarNumber(0.5) * (U_i_ + U_j) -
498 ScalarNumber(0.5) * contract(add(flux_j, -flux_i_), scaled_c_ij) +
499 affine_shift;
500
501 const auto rho_ij_bar = view_.density(U_ij_bar);
502
503 /* Density bounds: */
504
505 rho_min = std::min(rho_min, rho_ij_bar);
506 rho_max = std::max(rho_max, rho_ij_bar);
507
508 /* Density relaxation: */
509
510 /* Use a uniform weight. */
511 const auto beta_ij = Number(1.);
512 rho_relaxation_numerator_ += beta_ij * (rho_i + rho_j);
513 rho_relaxation_denominator_ += std::abs(beta_ij);
514
515 /* Surrogate entropy bounds and relaxation: */
516
517 if (view_.compute_strict_bounds()) {
518 /*
519 * Compute strict bounds precisely as outlined in @cite ryujin-2023-4
520 *
521 * This means, we compute
522 * - the surrogate entropy at dof j with the gamma_min of index i,
523 * - the surrogate entropy of the bar state U_ij_bar
524 * - an interpolated surrogate entropy at (U_i + U_j) / 2 for
525 * bounds relaxation:
526 */
527
528 const auto s_j = view_.surrogate_specific_entropy(U_j, gamma_min);
529
530 const auto s_ij_bar =
531 view_.surrogate_specific_entropy(U_ij_bar, gamma_min);
532
533 const Number s_interp = view_.surrogate_specific_entropy(
534 (U_i_ + U_j) * ScalarNumber(.5), gamma_min);
535
536 s_min = std::min(s_min, s_j);
537 s_min = std::min(s_min, s_ij_bar);
538 s_interp_max_ = std::max(s_interp_max_, s_interp);
539
540 } else {
541 /*
542 * Compute a cheaper bound solely relying on the diagonal s_j
543 * (computed with gamma_min_j) and the surrogate entropy s_ij_bar
544 * of the bar state. We use the s_ij_bar for computing the bounds
545 * relaxation as well.
546 */
547 const auto [p_j, gamma_min_j, s_j, eta_j] =
548 pv.template read_tensor<Number, precomputed_type>(js);
549
550 const auto s_ij_bar =
551 view_.surrogate_specific_entropy(U_ij_bar, gamma_min);
552
553 s_min = std::min(s_min, s_j);
554 s_min = std::min(s_min, s_ij_bar);
555 s_interp_max_ = std::max(s_interp_max_, s_ij_bar);
556 }
557 }
558
559
560 template <int dim, typename Number>
561 DEAL_II_ALWAYS_INLINE inline auto
562 LimiterView<dim, Number>::bounds(const Number hd_i) const -> Bounds
563 {
564 const auto &[rho_min, rho_max, s_min, gamma_min] = bounds_;
565
566 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
567 auto &[rho_min_relaxed, rho_max_relaxed, s_min_relaxed, g_m_relaxed] =
568 relaxed_bounds;
569
570 /* Apply a stricter window: */
571
572 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
573
574 const auto rho_relaxation =
575 ScalarNumber(2. * relaxation_factor()) *
576 std::abs(rho_relaxation_numerator_) /
577 (std::abs(rho_relaxation_denominator_) + Number(eps));
578
579 const auto entropy_relaxation =
580 relaxation_factor() * (s_interp_max_ - s_min);
581
582 rho_min_relaxed = std::max(rho_min_relaxed, rho_min - rho_relaxation);
583 rho_max_relaxed = std::min(rho_max_relaxed, rho_max + rho_relaxation);
584 s_min_relaxed = std::max(s_min_relaxed, s_min - entropy_relaxation);
585
586 return relaxed_bounds;
587 }
588 } // namespace EulerAEOS
589} // namespace ryujin
dealii::Tensor< 1, problem_dimension, Number > state_type
Vectors::MultiComponentVectorView< ScalarNumber, n_precomputed_values, dealii::VectorizedArray< ScalarNumber >::size(), dealii::MemorySpace::Host, false > PrecomputedVectorView
typename get_value_type< Number >::type ScalarNumber
static constexpr unsigned int problem_dimension
std::array< Number, n_precomputed_values > precomputed_type
std::array< Number, n_bounds > Bounds
Definition limiter.h:194
typename View::ScalarNumber ScalarNumber
Definition limiter.h:169
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:167
typename View::state_type state_type
Definition limiter.h:173
typename View::flux_contribution_type flux_contribution_type
Definition limiter.h:175
Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
Definition limiter.h:385
unsigned int iterations() const
Definition limiter.h:209
static constexpr unsigned int n_bounds
Definition limiter.h:189
Bounds bounds(const Number hd_i) const
Definition limiter.h:562
ScalarNumber newton_tolerance() const
Definition limiter.h:217
static constexpr auto problem_dimension
Definition limiter.h:171
ScalarNumber relaxation_factor() const
Definition limiter.h:233
typename View::PrecomputedVectorView PrecomputedVectorView
Definition limiter.h:179
typename View::precomputed_type precomputed_type
Definition limiter.h:177
unsigned int newton_max_iterations() const
Definition limiter.h:225
Bounds projection_bounds_from_state(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i) const
Definition limiter.h:368
Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
Definition limiter.h:400
void reset(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition limiter.h:444
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:474
LimiterView(const View &view, const Limiter< ScalarNumber > &limiter)
Definition limiter.h:200
Limiter(const HyperbolicSystem &hyperbolic_system, const std::string &subsection="/Limiter")
Definition limiter.h:81
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)