8#include <compile_time_options.h>
19 namespace ScalarConservation
22 typename Number = double,
23 typename MemorySpace = dealii::MemorySpace::Host>
31 template <
typename ScalarNumber =
double>
32 class Limiter :
public dealii::ParameterAcceptor
58 const std::string &subsection =
"/Limiter")
59 : ParameterAcceptor(subsection)
60 , parameters_(
"scalar_conservation_limiter_parameters",
62 , hyperbolic_system_(&hyperbolic_system)
65 auto ¶meters = *parameters_.view();
67 parameters.iterations = 2;
68 add_parameter(
"iterations",
69 parameters.iterations,
70 "Number of limiter iterations");
72 parameters.relaxation_factor = 1.;
73 add_parameter(
"relaxation factor",
74 parameters.relaxation_factor,
75 "Factor for scaling the relaxation window with r_i = "
76 "factor * (m_i/|Omega|)^(1.5/d).");
79 ParameterAcceptor::parse_parameters_call_back.connect(
80 [
this] { parameters_.view(); });
92 typename MemorySpace = dealii::MemorySpace::Host>
96 hyperbolic_system_->template view<dim, Number, MemorySpace>(),
115 dealii::ObserverPointer<const HyperbolicSystem> hyperbolic_system_;
117 template <
int,
typename,
typename>
132 template <
int dim,
typename Number,
typename MemorySpace>
137 std::is_same_v<MemorySpace, dealii::MemorySpace::Host> ||
138 std::is_same_v<MemorySpace, dealii::MemorySpace::Default>,
139 "Unexpected memory space");
170 using Bounds = std::array<Number, n_bounds>;
178 , parameters_(limiter.parameters_.template view<MemorySpace>())
185 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
unsigned int iterations()
const
187 return parameters_->iterations;
202 DEAL_II_HOST_DEVICE
Bounds
204 const unsigned int i,
213 const Bounds &bounds_left,
const Bounds &bounds_right)
const;
221 const Number &hd)
const;
248 const unsigned int i,
256 DEAL_II_HOST_DEVICE
void
258 const unsigned int *js,
261 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
267 DEAL_II_HOST_DEVICE
Bounds bounds(
const Number hd_i)
const;
281 DEAL_II_HOST_DEVICE std::tuple<Number, bool>
285 const Number t_min = Number(0.),
286 const Number t_max = Number(1.))
const;
303 Number u_relaxation_numerator_;
304 Number u_relaxation_denominator_;
316 template <
int dim,
typename Number,
typename MemorySpace>
317 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
auto
323 const auto u_i = view_.state(U_i);
328 template <
int dim,
typename Number,
typename MemorySpace>
329 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
auto
333 const auto &[u_min_l, u_max_l] = bounds_left;
334 const auto &[u_min_r, u_max_r] = bounds_right;
336 return {std::min(u_min_l, u_min_r), std::max(u_max_l, u_max_r)};
340 template <
int dim,
typename Number,
typename MemorySpace>
341 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
auto
345 auto relaxed_bounds = bounds;
346 auto &[u_min, u_max] = relaxed_bounds;
350 Number r = std::sqrt(hd);
351 if constexpr (dim == 2)
352 r = ryujin::fixed_power<3>(std::sqrt(r));
353 else if constexpr (dim == 1)
354 r = ryujin::fixed_power<3>(r);
355 r *= relaxation_factor();
357 u_min = std::min((Number(1.) - r) * u_min, (Number(1.) + r) * u_min);
358 u_max = std::max((Number(1.) + r) * u_max, (Number(1.) - r) * u_max);
360 return relaxed_bounds;
364 template <
int dim,
typename Number,
typename MemorySpace>
365 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
void
377 auto &[u_min, u_max] = bounds_;
379 u_min = Number(std::numeric_limits<ScalarNumber>::max());
380 u_max = Number(std::numeric_limits<ScalarNumber>::lowest());
384 u_relaxation_numerator_ = Number(0.);
385 u_relaxation_denominator_ = Number(0.);
389 template <
int dim,
typename Number,
typename MemorySpace>
390 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
void
393 const unsigned int * ,
396 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
400 auto &[u_min, u_max] = bounds_;
402 const auto u_i = view_.state(U_i_);
403 const auto u_j = view_.state(U_j);
405 const auto U_ij_bar =
410 const auto u_ij_bar = view_.state(U_ij_bar);
414 u_min = std::min(u_min, u_ij_bar);
415 u_max = std::max(u_max, u_ij_bar);
420 const auto beta_ij = Number(1.);
421 u_relaxation_numerator_ += beta_ij * (u_i + u_j);
422 u_relaxation_denominator_ += std::abs(beta_ij);
426 template <
int dim,
typename Number,
typename MemorySpace>
427 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
auto
431 const auto &[u_min, u_max] = bounds_;
433 auto relaxed_bounds = fully_relax_bounds(bounds_, hd_i);
434 auto &[u_min_relaxed, u_max_relaxed] = relaxed_bounds;
438 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
440 const Number u_relaxation =
442 std::abs(u_relaxation_numerator_) /
443 (std::abs(u_relaxation_denominator_) + Number(eps));
445 u_min_relaxed = std::max(u_min_relaxed, u_min - u_relaxation);
446 u_max_relaxed = std::min(u_max_relaxed, u_max + u_relaxation);
448 return relaxed_bounds;
452 template <
int dim,
typename Number,
typename MemorySpace>
453 DEAL_II_HOST_DEVICE_ALWAYS_INLINE std::tuple<Number, bool>
459 const Number t_max )
const
464 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
467 const auto &u_U = view_.state(U);
468 const auto &u_P = view_.state(P);
470 const auto &u_min = std::get<0>(bounds);
471 const auto &u_max = std::get<1>(bounds);
479 const auto test_max = std::max(
480 Number(0.), std::min(u_U - relax * u_max, relax * u_U - u_max));
481 const auto test_min = std::max(
482 Number(0.), std::min(u_min - relax * u_U, relax * u_min - u_U));
483 if (!(test_max == Number(0.) && test_min == Number(0.))) {
485 std::cout << std::fixed << std::setprecision(16);
486 std::cout <<
"Bounds violation: low-order state (critical)!"
487 <<
"\n\t\tu min: " << u_min
489 <<
"\n\t\tu: " << u_U
491 <<
"\n\t\tu max: " << u_max <<
"\n"
497 const auto regularization =
498 Number(100. * std::numeric_limits<ScalarNumber>::min());
500 const Number denominator =
502 std::max(regularization, std::abs(u_P) + eps * u_max);
504 t_r = ryujin::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
513 (u_max - u_U) * denominator,
516 t_r = ryujin::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
525 (u_U - u_min) * denominator,
535 t_r = std::min(t_r, t_max);
536 t_r = std::max(t_r, t_min);
538#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
544 const auto u_new = view_.state(U + t_r * P);
545 const auto test_new_max = std::max(
546 Number(0.), std::min(u_new - relax * u_max, relax * u_new - u_max));
547 const auto test_new_min = std::max(
548 Number(0.), std::min(u_min - relax * u_new, relax * u_min - u_new));
549 if (!(test_new_max == Number(0.) && test_new_min == Number(0.))) {
551 std::cout << std::fixed << std::setprecision(16);
552 std::cout <<
"Bounds violation: high-order state!"
553 <<
"\n\t\tu min: " << u_min
555 <<
"\n\t\tu: " << u_new
557 <<
"\n\t\tu max: " << u_max <<
"\n"
564 return {t_r, success};
dealii::Tensor< 1, problem_dimension, Number > state_type
flux_type flux_contribution_type
typename get_value_type< Number >::type ScalarNumber
VectorView< n_precomputed_values > PrecomputedVectorView
typename View::PrecomputedVectorView PrecomputedVectorView
DEAL_II_HOST_DEVICE 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)
DEAL_II_HOST_DEVICE Bounds bounds(const Number hd_i) const
typename View::state_type state_type
DEAL_II_HOST_DEVICE Bounds fully_relax_bounds(const Bounds &bounds, const Number &hd) const
typename View::ScalarNumber ScalarNumber
DEAL_II_HOST_DEVICE Bounds projection_bounds_from_state(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i) const
static constexpr unsigned int n_bounds
std::array< Number, n_bounds > Bounds
DEAL_II_HOST_DEVICE Bounds combine_bounds(const Bounds &bounds_left, const Bounds &bounds_right) const
typename View::flux_contribution_type flux_contribution_type
DEAL_II_HOST_DEVICE_ALWAYS_INLINE unsigned int iterations() const
DEAL_II_HOST_DEVICE void reset(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
HyperbolicSystemView< dim, Number, MemorySpace > View
DEAL_II_HOST_DEVICE_ALWAYS_INLINE ScalarNumber relaxation_factor() const
LimiterView(const View &view, const Limiter< ScalarNumber > &limiter)
DEAL_II_HOST_DEVICE 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
Limiter(const HyperbolicSystem &hyperbolic_system, const std::string &subsection="/Limiter")
@ implicit_transfers_host_resident
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)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number positive_part(const Number number)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number negative_part(const Number number)