ryujin 2.1.1 revision 284c1ee75f38e11ba18808dd0d991d905cf7c040
limiter.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0
3// [LANL Copyright Statement]
4// Copyright (C) 2023 - 2024 by the ryujin authors
5// Copyright (C) 2023 - 2024 by Triad National Security, LLC
6//
7
8#pragma once
9
10#include "hyperbolic_system.h"
11
12#include <compile_time_options.h>
14#include <newton.h>
15
16namespace ryujin
17{
18 namespace ShallowWater
19 {
20 template <typename ScalarNumber = double>
21 class LimiterParameters : public dealii::ParameterAcceptor
22 {
23 public:
24 LimiterParameters(const std::string &subsection = "/Limiter")
25 : ParameterAcceptor(subsection)
26 {
27 iterations_ = 2;
28 add_parameter(
29 "iterations", iterations_, "Number of limiter iterations");
30
31 if constexpr (std::is_same<ScalarNumber, double>::value)
32 newton_tolerance_ = 1.e-10;
33 else
34 newton_tolerance_ = 1.e-4;
35 add_parameter("newton tolerance",
36 newton_tolerance_,
37 "Tolerance for the quadratic newton stopping criterion");
38
39 newton_max_iterations_ = 2;
40 add_parameter("newton max iterations",
41 newton_max_iterations_,
42 "Maximal number of quadratic newton iterations performed "
43 "during limiting");
44
45 relaxation_factor_ = ScalarNumber(1.);
46 add_parameter("relaxation factor",
47 relaxation_factor_,
48 "Factor for scaling the relaxation window with r_i = "
49 "factor * (m_i/|Omega|)^(1.5/d).");
50
51 limit_on_kinetic_energy_ = false;
52 add_parameter("limit on kinetic energy",
53 limit_on_kinetic_energy_,
54 "Limit on kinetic energy");
55
56 limit_on_square_velocity_ = true;
57 add_parameter("limit on square velocity",
58 limit_on_square_velocity_,
59 "Limit on square velocity");
60 }
61
62 ACCESSOR_READ_ONLY(iterations);
63 ACCESSOR_READ_ONLY(newton_tolerance);
64 ACCESSOR_READ_ONLY(newton_max_iterations);
65 ACCESSOR_READ_ONLY(relaxation_factor);
66
67 ACCESSOR_READ_ONLY(limit_on_kinetic_energy);
68 ACCESSOR_READ_ONLY(limit_on_square_velocity);
69
70 private:
71 unsigned int iterations_;
72 ScalarNumber newton_tolerance_;
73 unsigned int newton_max_iterations_;
74 ScalarNumber relaxation_factor_;
75
76 bool limit_on_kinetic_energy_;
77 bool limit_on_square_velocity_;
78 };
79
80
86 template <int dim, typename Number = double>
87 class Limiter
88 {
89 public:
94
98 using state_type = typename View::state_type;
99
103 static constexpr unsigned int n_precomputed_values =
105
110
115
120
140
144 static constexpr unsigned int n_bounds = 5;
145
149 using Bounds = std::array<Number, n_bounds>;
150
154 Limiter(const HyperbolicSystem &hyperbolic_system,
155 const Parameters &parameters,
157 &precomputed_values)
158 : hyperbolic_system(hyperbolic_system)
159 , parameters(parameters)
160 , precomputed_values(precomputed_values)
161 {
162 }
163
167 void reset(const unsigned int i,
168 const state_type &U_i,
169 const flux_contribution_type &flux_i);
170
175 void accumulate(const state_type &U_j,
176 const state_type &U_star_ij,
177 const state_type &U_star_ji,
178 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
179 const Number &beta_ij,
180 const state_type &affine_shift);
181
185 Bounds bounds(const Number hd_i) const;
186
187 //*}
190
197 std::tuple<Number, bool> limit(const Bounds &bounds,
198 const state_type &U,
199 const state_type &P,
200 const Number t_min = Number(0.),
201 const Number t_max = Number(1.));
202
203 //*}
208
215 static bool
216 is_in_invariant_domain(const HyperbolicSystem & /*hyperbolic_system*/,
217 const Bounds & /*bounds*/,
218 const state_type & /*U*/);
219
220 private:
222
224
225 const HyperbolicSystem &hyperbolic_system;
226 const Parameters &parameters;
227
229 &precomputed_values;
230
231 state_type U_i;
232
233 Bounds bounds_;
234
235 /* for relaxation */
236
237 Number h_relaxation_numerator;
238 Number kin_relaxation_numerator;
239 Number v2_relaxation_numerator;
240 Number relaxation_denominator;
241
243 };
244
245
246 /*
247 * -------------------------------------------------------------------------
248 * Inline definitions
249 * -------------------------------------------------------------------------
250 */
251
252
253 template <int dim, typename Number>
254 DEAL_II_ALWAYS_INLINE inline void
255 Limiter<dim, Number>::reset(unsigned int /*i*/,
256 const state_type &new_U_i,
257 const flux_contribution_type & /*new_flux_i*/)
258 {
259 U_i = new_U_i;
260
261 auto &[h_min, h_max, h_small, kin_max, v2_max] = bounds_;
262
263 h_min = Number(std::numeric_limits<ScalarNumber>::max());
264 h_max = Number(0.);
265 h_small = Number(0.);
266 kin_max = Number(0.);
267 v2_max = Number(0.);
268
269 h_relaxation_numerator = Number(0.);
270 kin_relaxation_numerator = Number(0.);
271 v2_relaxation_numerator = Number(0.);
272 relaxation_denominator = Number(0.);
273 }
274
275
276 template <int dim, typename Number>
277 DEAL_II_ALWAYS_INLINE inline void Limiter<dim, Number>::accumulate(
278 const state_type &U_j,
279 const state_type &U_star_ij,
280 const state_type &U_star_ji,
281 const dealii::Tensor<1, dim, Number> &scaled_c_ij,
282 const Number &beta_ij,
283 const state_type &affine_shift)
284 {
285 const auto view = hyperbolic_system.view<dim, Number>();
286
287 /* The bar states: */
288
289 const auto f_star_ij = view.f(U_star_ij);
290 const auto f_star_ji = view.f(U_star_ji);
291
292 auto U_ij_bar = ScalarNumber(0.5) *
293 (U_star_ij + U_star_ji +
294 contract(add(f_star_ij, -f_star_ji), scaled_c_ij));
295
296 U_ij_bar += affine_shift;
297
298 /* Bounds: */
299
300 auto &[h_min, h_max, h_small, kin_max, v2_max] = bounds_;
301
302 const auto h_bar_ij = view.water_depth(U_ij_bar);
303 h_min = std::min(h_min, h_bar_ij);
304 h_max = std::max(h_max, h_bar_ij);
305
306 const auto kin_bar_ij = view.kinetic_energy(U_ij_bar);
307 kin_max = std::max(kin_max, kin_bar_ij);
308
309 const auto v_bar_ij = view.momentum(U_ij_bar) *
310 view.inverse_water_depth_mollified(U_ij_bar);
311 const auto v2_bar_ij = v_bar_ij.norm_square();
312 v2_max = std::max(v2_max, v2_bar_ij);
313
314 /* Relaxation: */
315
316 relaxation_denominator += std::abs(beta_ij);
317
318 const auto h_i = view.water_depth(U_i);
319 const auto h_j = view.water_depth(U_j);
320 h_relaxation_numerator += beta_ij * (h_i + h_j);
321
322 const auto kin_i = view.kinetic_energy(U_i);
323 const auto kin_j = view.kinetic_energy(U_j);
324 kin_relaxation_numerator += beta_ij * (kin_i + kin_j);
325
326 const auto vel_i =
327 view.momentum(U_i) * view.inverse_water_depth_mollified(U_i);
328 const auto vel_j =
329 view.momentum(U_j) * view.inverse_water_depth_mollified(U_j);
330 v2_relaxation_numerator +=
331 beta_ij * (-vel_i.norm_square() + vel_j.norm_square());
332 }
333
334
335 template <int dim, typename Number>
336 DEAL_II_ALWAYS_INLINE inline auto
337 Limiter<dim, Number>::bounds(const Number hd_i) const -> Bounds
338 {
339 const auto view = hyperbolic_system.view<dim, Number>();
340
341 auto relaxed_bounds = bounds_;
342 auto &[h_min, h_max, h_small, kin_max, v2_max] = relaxed_bounds;
343
344 /* Use r_i = factor * (m_i / |Omega|) ^ (1.5 / d): */
345
346 Number r_i = std::sqrt(hd_i); // in 3D: ^ 3/6
347 if constexpr (dim == 2) //
348 r_i = dealii::Utilities::fixed_power<3>(std::sqrt(r_i)); // in 2D: ^ 3/4
349 else if constexpr (dim == 1) //
350 r_i = dealii::Utilities::fixed_power<3>(r_i); // in 1D: ^ 3/2
351 r_i *= parameters.relaxation_factor();
352
353 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
354
355 const Number h_relaxed = ScalarNumber(2.) *
356 std::abs(h_relaxation_numerator) /
357 (relaxation_denominator + Number(eps));
358
359 h_min = std::max((Number(1.) - r_i) * h_min, h_min - h_relaxed);
360 h_max = std::min((Number(1.) + r_i) * h_max, h_max + h_relaxed);
361
362 const Number kin_relaxed = ScalarNumber(2.) *
363 std::abs(kin_relaxation_numerator) /
364 (relaxation_denominator + Number(eps));
365
366 kin_max = std::min((Number(1.) + r_i) * kin_max, kin_max + kin_relaxed);
367
368 const Number v2_relaxed = ScalarNumber(2.) *
369 std::abs(v2_relaxation_numerator) /
370 (relaxation_denominator + Number(eps));
371
372 v2_max = std::min((Number(1.) + r_i) * v2_max, v2_max + v2_relaxed);
373
374 /* Use r_i = 0.2 * (m_i / |Omega|) ^ (1 / d): */
375
376 r_i = hd_i;
377 if constexpr (dim == 2)
378 r_i = std::sqrt(hd_i);
379 r_i *= view.dry_state_relaxation_factor();
380
381 h_small = view.reference_water_depth() * r_i;
382
383 return relaxed_bounds;
384 }
385
386
387 template <int dim, typename Number>
388 DEAL_II_ALWAYS_INLINE inline bool
390 const HyperbolicSystem & /*hyperbolic_system*/,
391 const Bounds & /*bounds*/,
392 const state_type & /*U*/)
393 {
394 AssertThrow(false, dealii::ExcNotImplemented());
395 __builtin_trap();
396 return true;
397 }
398
399
400 } // namespace ShallowWater
401} // namespace ryujin
static constexpr unsigned int n_precomputed_values
dealii::Tensor< 1, problem_dimension, Number > state_type
std::tuple< state_type, Number > flux_contribution_type
ACCESSOR_READ_ONLY(limit_on_square_velocity)
ACCESSOR_READ_ONLY(newton_max_iterations)
LimiterParameters(const std::string &subsection="/Limiter")
Definition: limiter.h:24
ACCESSOR_READ_ONLY(limit_on_kinetic_energy)
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.))
typename View::flux_contribution_type flux_contribution_type
Definition: limiter.h:109
std::array< Number, n_bounds > Bounds
Definition: limiter.h:149
static constexpr unsigned int n_precomputed_values
Definition: limiter.h:103
void accumulate(const state_type &U_j, const state_type &U_star_ij, const state_type &U_star_ji, const dealii::Tensor< 1, dim, Number > &scaled_c_ij, const Number &beta_ij, const state_type &affine_shift)
Definition: limiter.h:277
typename get_value_type< Number >::type ScalarNumber
Definition: limiter.h:114
Bounds bounds(const Number hd_i) const
Definition: limiter.h:337
void reset(const unsigned int i, const state_type &U_i, const flux_contribution_type &flux_i)
Definition: limiter.h:255
typename View::state_type state_type
Definition: limiter.h:98
Limiter(const HyperbolicSystem &hyperbolic_system, const Parameters &parameters, const MultiComponentVector< ScalarNumber, n_precomputed_values > &precomputed_values)
Definition: limiter.h:154
static bool is_in_invariant_domain(const HyperbolicSystem &, const Bounds &, const state_type &)
Definition: limiter.h:389
static constexpr unsigned int n_bounds
Definition: limiter.h:144
LimiterParameters< ScalarNumber > Parameters
Definition: limiter.h:119
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)