10#include <compile_time_options.h>
20#include <deal.II/base/parameter_acceptor.h>
21#include <deal.II/base/tensor.h>
27 namespace ShallowWater
29 template <
int dim,
typename Number>
30 class HyperbolicSystemView;
47 static inline const std::string
problem_name =
"Shallow water equations";
58 template <
int dim,
typename Number =
double>
67 template <
int dim,
typename Number>
81 template <
int dim,
typename ScalarNumber>
86 const bool skip_constrained_dofs =
true)
const;
95 double manning_friction_coefficient_;
97 double reference_water_depth_;
98 double dry_state_relaxation_small_;
99 double dry_state_relaxation_large_;
107 template <
int dim,
typename Number>
132 template <
int dim,
typename Number>
154 using state_type = dealii::Tensor<1, problem_dimension, Number>;
160 dealii::Tensor<1, problem_dimension, dealii::Tensor<1, dim, Number>>;
172 []() -> std::array<std::string, problem_dimension> {
173 if constexpr (dim == 1)
175 else if constexpr (dim == 2)
176 return {
"h",
"m_1",
"m_2"};
177 else if constexpr (dim == 3)
178 return {
"h",
"m_1",
"m_2",
"m_3"};
187 []() -> std::array<std::string, problem_dimension> {
188 if constexpr (dim == 1)
190 else if constexpr (dim == 2)
191 return {
"h",
"v_1",
"v_2"};
192 else if constexpr (dim == 3)
193 return {
"h",
"v_1",
"v_2",
"v_3"};
211 std::array<std::string, n_precomputed_values>{
"eta_m",
"h_star"};
222 std::array<Number, n_initial_precomputed_values>;
228 std::array<std::string, n_initial_precomputed_values>{
"bathymetry"};
234 StateVector<ScalarNumber, problem_dimension, n_precomputed_values>;
255 dealii::VectorizedArray<ScalarNumber>::size(),
256 dealii::MemorySpace::Host,
274 dealii::VectorizedArray<ScalarNumber>::size(),
275 dealii::MemorySpace::Host,
289 : hyperbolic_system_(hyperbolic_system)
301 return hyperbolic_system_.gravity_;
307 return hyperbolic_system_.manning_friction_coefficient_;
312 return hyperbolic_system_.reference_water_depth_;
318 return hyperbolic_system_.dry_state_relaxation_small_;
324 return hyperbolic_system_.dry_state_relaxation_large_;
434 template <
int component>
438 const dealii::Tensor<1, dim, Number> &normal)
const;
443 template <
typename Lambda>
447 const dealii::Tensor<1, dim, Number> &normal,
448 const Lambda &get_dirichlet_data)
const;
484 const Number &Z_left,
485 const Number &Z_right)
const;
492 std::array<state_type, 2>
519 const unsigned int i,
525 const unsigned int *js,
536 const dealii::Tensor<1, dim, Number> &c_ij)
const;
551 const dealii::Tensor<1, dim, Number> &c_ij)
const;
560 const dealii::Tensor<1, dim, Number> &c_ij,
561 const Number &d_ij)
const;
578 const Number &h_star,
582 const unsigned int i,
587 const unsigned int *js,
607 template <
typename ST>
621 template <
typename ST>
640 template <
typename Lambda>
642 const Lambda &lambda)
const;
665 : ParameterAcceptor(subsection)
668 add_parameter(
"gravity", gravity_,
"Gravitational constant [m/s^2]");
670 manning_friction_coefficient_ = 0.;
671 add_parameter(
"manning friction coefficient",
672 manning_friction_coefficient_,
673 "Roughness coefficient for friction source");
675 reference_water_depth_ = 1.;
676 add_parameter(
"reference water depth",
677 reference_water_depth_,
678 "Problem specific water depth reference");
680 dry_state_relaxation_small_ = 1.e2;
681 add_parameter(
"dry state relaxation small",
682 dry_state_relaxation_small_,
683 "Problem specific dry-state relaxation parameter");
685 dry_state_relaxation_large_ = 1.e4;
686 add_parameter(
"dry state relaxation large",
687 dry_state_relaxation_large_,
688 "Problem specific dry-state relaxation parameter");
692 template <
int dim,
typename ScalarNumber>
697 const bool skip_constrained_dofs)
const
701 const auto sparsity_simd_view =
703 using VA = dealii::VectorizedArray<ScalarNumber>;
705 const auto U_view = std::get<0>(state_vector).view();
706 const auto precomputed_view = std::get<1>(state_vector).view();
708 const auto body = [&](
auto sentinel,
unsigned int i) {
709 using T =
decltype(sentinel);
713 const unsigned int row_length = sparsity_simd_view.row_length(i);
714 if (skip_constrained_dofs && row_length == 1)
717 const auto U_i = U_view.template read_tensor<T>(i);
718 const auto view = this->view<dim, T>();
719 const auto eta_m =
view.mathematical_entropy(U_i);
720 const auto h_sharp =
view.water_depth_sharp(U_i);
721 const auto h_star =
ryujin::pow(h_sharp, ScalarNumber(4. / 3.));
723 const precomputed_type prec_i{eta_m, h_star};
725 precomputed_view.template write_tensor<T>(prec_i, i);
728 cpu_simd_loop<ScalarNumber>(
"time_step_1", body, 0, n_internal, n_owned);
732 template <
int dim,
typename Number>
733 DEAL_II_ALWAYS_INLINE
inline Number
740 template <
int dim,
typename Number>
741 DEAL_II_ALWAYS_INLINE
inline Number
745 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
747 const Number h_cutoff_mollified =
748 reference_water_depth() * dry_state_relaxation_large() * Number(eps);
750 const Number h = water_depth(U);
752 const Number h_max = std::max(h, h_cutoff_mollified);
753 const Number denom = h * h + h_max * h_max;
758 template <
int dim,
typename Number>
759 DEAL_II_ALWAYS_INLINE
inline Number
763 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
765 const Number h_cutoff_small =
766 reference_water_depth() * dry_state_relaxation_small() * Number(eps);
768 const Number h = water_depth(U);
769 const Number h_max = std::max(h, h_cutoff_small);
774 template <
int dim,
typename Number>
775 DEAL_II_ALWAYS_INLINE
inline Number
783 template <
int dim,
typename Number>
784 DEAL_II_ALWAYS_INLINE
inline Number
786 const Number &h)
const
789 constexpr ScalarNumber eps = std::numeric_limits<ScalarNumber>::epsilon();
791 const Number h_cutoff_large =
792 reference_water_depth() * dry_state_relaxation_large() * Number(eps);
794 return dealii::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
795 std::abs(h), h_cutoff_large, Number(0.), h);
799 template <
int dim,
typename Number>
800 DEAL_II_ALWAYS_INLINE
inline dealii::Tensor<1, dim, Number>
803 dealii::Tensor<1, dim, Number> result;
805 for (
unsigned int i = 0; i < dim; ++i)
806 result[i] = U[1 + i];
811 template <
int dim,
typename Number>
812 DEAL_II_ALWAYS_INLINE
inline Number
815 const auto h = water_depth(U);
816 const auto vel = momentum(U) * inverse_water_depth_sharp(U);
823 template <
int dim,
typename Number>
824 DEAL_II_ALWAYS_INLINE
inline Number
827 const Number h_sqd = U[0] * U[0];
834 template <
int dim,
typename Number>
835 DEAL_II_ALWAYS_INLINE
inline Number
839 return std::sqrt(gravity() * U[0]);
843 template <
int dim,
typename Number>
844 DEAL_II_ALWAYS_INLINE
inline Number
848 const auto p = pressure(U);
849 const auto k_e = kinetic_energy(U);
854 template <
int dim,
typename Number>
855 DEAL_II_ALWAYS_INLINE
inline auto
872 const Number &h = U[0];
873 const auto vel = momentum(U) * inverse_water_depth_sharp(U);
876 result[0] = gravity() * h -
ScalarNumber(0.5) * vel.norm_square();
879 for (
unsigned int i = 0; i < dim; ++i) {
880 result[1 + i] = vel[i];
887 template <
int dim,
typename Number>
888 DEAL_II_ALWAYS_INLINE
inline bool
891 const auto h = filter_dry_water_depth(water_depth(U));
893 constexpr auto gte = dealii::SIMDComparison::greater_than_or_equal;
894 const auto test = dealii::compare_and_apply_mask<gte>(
895 h, Number(0.), Number(0.), Number(-1.));
898 if (!(test == Number(0.))) {
899 std::cout << std::fixed << std::setprecision(16);
900 std::cout <<
"Bounds violation: Negative state [h] detected!\n";
901 std::cout <<
"\t\th: " << h <<
"\n" << std::endl;
906 return (test == Number(0.));
910 template <
int dim,
typename Number>
911 template <
int component>
912 DEAL_II_ALWAYS_INLINE
inline auto
916 const dealii::Tensor<1, dim, Number> &normal)
const ->
state_type
919 static_assert(component == 1 || component == 2,
920 "component has to be 1 or 2");
924 const auto m = momentum(U);
925 const auto a = speed_of_sound(U);
926 const auto vn = m * normal * inverse_water_depth_sharp(U);
928 const auto m_bar = momentum(U_bar);
929 const auto a_bar = speed_of_sound(U_bar);
930 const auto vn_bar = m_bar * normal * inverse_water_depth_sharp(U_bar);
934 const auto R_1 = component == 1 ? vn_bar -
ScalarNumber(2.) * a_bar
939 const auto R_2 = component == 2 ? vn_bar +
ScalarNumber(2.) * a_bar
942 const auto vperp = m * inverse_water_depth_sharp(U) - vn * normal;
947 ryujin::fixed_power<2>((R_2 - R_1) /
ScalarNumber(4.)) / gravity();
951 for (
unsigned int d = 0; d < dim; ++d) {
952 U_new[1 + d] = h_new * (vn_new * normal + vperp)[d];
959 template <
int dim,
typename Number>
960 template <
typename Lambda>
961 DEAL_II_ALWAYS_INLINE
inline auto
963 const dealii::types::boundary_id
id,
965 const dealii::Tensor<1, dim, Number> &normal,
966 const Lambda &get_dirichlet_data)
const ->
state_type
971 result = get_dirichlet_data();
975 auto m_dirichlet = momentum(get_dirichlet_data());
976 for (
unsigned int k = 0; k < dim; ++k)
977 result[k + 1] = m_dirichlet[k];
981 const auto U_dirichlet = get_dirichlet_data();
982 const auto h_inv_sharp_dirichlet =
983 inverse_water_depth_sharp(U_dirichlet);
984 const auto v_dirichlet = momentum(U_dirichlet) * h_inv_sharp_dirichlet;
985 const auto h = water_depth_sharp(result);
986 for (
unsigned int k = 0; k < dim; ++k)
987 result[k + 1] = h * v_dirichlet[k];
990 auto m = momentum(U);
991 m -= 1. * (m * normal) * normal;
992 for (
unsigned int k = 0; k < dim; ++k)
993 result[k + 1] = m[k];
996 for (
unsigned int k = 0; k < dim; ++k)
997 result[k + 1] = Number(0.);
1012 const auto m = momentum(U);
1013 const auto h_inverse = inverse_water_depth_sharp(U);
1014 const auto a = speed_of_sound(U);
1015 const auto vn = m * normal * h_inverse;
1019 result = get_dirichlet_data();
1023 if (vn >= -a && vn <= 0.) {
1024 const auto U_dirichlet = get_dirichlet_data();
1025 result = prescribe_riemann_characteristic<2>(U_dirichlet, U, normal);
1029 if (vn > 0. && vn <= a) {
1030 const auto U_dirichlet = get_dirichlet_data();
1031 result = prescribe_riemann_characteristic<1>(U, U_dirichlet, normal);
1037 AssertThrow(
false, dealii::ExcNotImplemented());
1044 template <
int dim,
typename Number>
1045 DEAL_II_ALWAYS_INLINE
inline auto
1048 const auto h_inverse = inverse_water_depth_sharp(U);
1049 const auto m = momentum(U);
1050 const auto p = pressure(U);
1054 result[0] = (m * h_inverse) * U[0];
1055 for (
unsigned int i = 0; i < dim; ++i) {
1056 result[1 + i] = (m * h_inverse) * m[i];
1057 result[1 + i][i] += p;
1063 template <
int dim,
typename Number>
1064 DEAL_II_ALWAYS_INLINE
inline auto
1067 const auto h_inverse = inverse_water_depth_sharp(U);
1068 const auto m = momentum(U);
1072 result[0] = (m * h_inverse) * U[0];
1073 for (
unsigned int i = 0; i < dim; ++i) {
1074 result[1 + i] = (m * h_inverse) * m[i];
1080 template <
int dim,
typename Number>
1081 DEAL_II_ALWAYS_INLINE
inline auto
1083 const Number &Z_left,
1084 const Number &Z_right)
const
1087 const Number Z_max = std::max(Z_left, Z_right);
1088 const Number h = water_depth(U);
1089 const Number H_star = std::max(Number(0.), h + Z_left - Z_max);
1091 return U * H_star * inverse_water_depth_mollified(U);
1095 template <
int dim,
typename Number>
1096 DEAL_II_ALWAYS_INLINE
inline auto
1101 const auto &[U_i, Z_i] = flux_i;
1102 const auto &[U_j, Z_j] = flux_j;
1104 const auto U_star_ij = star_state(U_i, Z_i, Z_j);
1105 const auto U_star_ji = star_state(U_j, Z_j, Z_i);
1107 return {U_star_ij, U_star_ji};
1111 template <
int dim,
typename Number>
1112 DEAL_II_ALWAYS_INLINE
inline auto
1116 const unsigned int i,
1119 const auto Z_i = piv.template read_tensor<Number>(i)[0];
1124 template <
int dim,
typename Number>
1125 DEAL_II_ALWAYS_INLINE
inline auto
1129 const unsigned int *js,
1132 const auto Z_j = piv.template read_tensor<Number>(js)[0];
1137 template <
int dim,
typename Number>
1138 DEAL_II_ALWAYS_INLINE
inline auto
1142 const dealii::Tensor<1, dim, Number> &c_ij)
const ->
state_type
1144 const auto &[U_i, Z_i] = flux_i;
1145 const auto &[U_star_ij, U_star_ji] = equilibrated_states(flux_i, flux_j);
1147 const auto H_i = water_depth(U_i);
1148 const auto H_star_ij = water_depth(U_star_ij);
1149 const auto H_star_ji = water_depth(U_star_ji);
1151 const auto g_i = g(U_star_ij);
1152 const auto g_j = g(U_star_ji);
1154 auto result = -
add(g_i, g_j);
1157 (
ScalarNumber(0.5) * (H_star_ji * H_star_ji - H_star_ij * H_star_ij) +
1161 for (
unsigned int i = 0; i < dim; ++i) {
1162 result[1 + i][i] -= factor;
1169 template <
int dim,
typename Number>
1170 DEAL_II_ALWAYS_INLINE
inline auto
1174 const dealii::Tensor<1, dim, Number> &c_ij)
const ->
state_type
1176 const auto &[U_i, Z_i] = flux_i;
1177 const auto &[U_j, Z_j] = flux_j;
1179 const auto H_i = water_depth(U_i);
1180 const auto H_j = water_depth(U_j);
1182 const auto g_i = g(U_i);
1183 const auto g_j = g(U_j);
1185 auto result = -
add(g_i, g_j);
1187 const auto factor = gravity() * H_i * (H_j + Z_j - Z_i);
1188 for (
unsigned int i = 0; i < dim; ++i) {
1189 result[1 + i][i] -= factor;
1196 template <
int dim,
typename Number>
1197 DEAL_II_ALWAYS_INLINE
inline auto
1201 const dealii::Tensor<1, dim, Number> &c_ij,
1204 const auto &[U_i, Z_i] = flux_i;
1205 const auto &[U_j, Z_j] = flux_j;
1206 const auto U_star_ij = star_state(U_i, Z_i, Z_j);
1208 const auto h_inverse = inverse_water_depth_sharp(U_i);
1209 const auto m = momentum(U_i);
1210 const auto factor =
ScalarNumber(2.) * (d_ij + h_inverse * (m * c_ij));
1212 return -factor * (U_star_ij - U_i);
1216 template <
int dim,
typename Number>
1217 DEAL_II_ALWAYS_INLINE
inline auto
1224 const auto g = gravity();
1225 const auto n = manning_friction_coefficient();
1227 const auto h_inverse = inverse_water_depth_mollified(U);
1229 const auto m = momentum(U);
1230 const auto v_norm = (m * h_inverse).norm();
1231 const auto factor =
ScalarNumber(2.) * g * n * n * v_norm;
1233 const auto denominator = h_star + std::max(h_star, tau * factor);
1234 const auto denominator_inverse =
ScalarNumber(1.) / denominator;
1236 for (
unsigned int d = 0; d < dim; ++d)
1237 result[d + 1] = -factor * denominator_inverse * m[d];
1243 template <
int dim,
typename Number>
1244 DEAL_II_ALWAYS_INLINE
inline auto
1247 const unsigned int i,
1251 const auto &[eta_m, h_star] =
1252 pv.template read_tensor<Number, precomputed_type>(i);
1254 return manning_friction(U_i, h_star, tau);
1258 template <
int dim,
typename Number>
1259 DEAL_II_ALWAYS_INLINE
inline auto
1262 const unsigned int *js,
1266 const auto &[eta_m, h_star] =
1267 pv.template read_tensor<Number, precomputed_type>(js);
1269 return manning_friction(U_j, h_star, tau);
1273 template <
int dim,
typename Number>
1274 template <
typename ST>
1275 DEAL_II_ALWAYS_INLINE
inline auto
1279 using T =
typename ST::value_type;
1280 static_assert(std::is_same_v<Number, T>,
"template mismatch");
1282 constexpr auto dim2 = ST::dimension - 1;
1283 static_assert(dim >= dim2,
1284 "the space dimension of the argument state must not be "
1285 "larger than the one of the target state");
1288 result[0] = state[0];
1289 for (
unsigned int i = 1; i < dim2 + 1; ++i)
1290 result[i] = state[i];
1295 template <
int dim,
typename Number>
1296 template <
typename ST>
1297 DEAL_II_ALWAYS_INLINE
inline auto
1301 const auto primitive_state = expand_state(initial_state);
1302 return from_primitive_state(primitive_state);
1306 template <
int dim,
typename Number>
1307 DEAL_II_ALWAYS_INLINE
inline auto
1311 const auto &h = primitive_state[0];
1313 auto state = primitive_state;
1315 for (
unsigned int i = 1; i < dim + 1; ++i)
1322 template <
int dim,
typename Number>
1323 DEAL_II_ALWAYS_INLINE
inline auto
1327 const auto h_inverse = inverse_water_depth_sharp(state);
1329 auto primitive_state = state;
1331 for (
unsigned int i = 1; i < dim + 1; ++i)
1332 primitive_state[i] *= h_inverse;
1334 return primitive_state;
1338 template <
int dim,
typename Number>
1339 template <
typename Lambda>
1340 DEAL_II_ALWAYS_INLINE
inline auto
1344 auto result = state;
1345 auto M = lambda(momentum(state));
1346 for (
unsigned int d = 0; d < dim; ++d)
1347 result[1 + d] = M[d];
dealii::Tensor< 1, problem_dimension, Number > state_type
typename get_value_type< Number >::type ScalarNumber
const auto & n_locally_owned() const
const auto & sparsity_pattern_simd() const
const auto & n_locally_internal() const
dealii::Tensor< 1, problem_dimension, dealii::Tensor< 1, dim, Number > > flux_type
Number pressure(const state_type &U) const
flux_contribution_type flux_contribution(const PrecomputedVectorView &pv, const InitialPrecomputedVectorView &piv, const unsigned int i, const state_type &U_i) const
state_type flux_divergence(const flux_contribution_type &flux_i, const flux_contribution_type &flux_j, const dealii::Tensor< 1, dim, Number > &c_ij) const
DEAL_II_ALWAYS_INLINE ScalarNumber gravity() const
bool is_admissible(const state_type &U) const
static const auto precomputed_names
static const auto initial_precomputed_names
state_type mathematical_entropy_derivative(const state_type &U) const
typename get_value_type< Number >::type ScalarNumber
static Number water_depth(const state_type &U)
DEAL_II_ALWAYS_INLINE ScalarNumber dry_state_relaxation_large() const
static constexpr unsigned int n_initial_precomputed_values
state_type high_order_flux_divergence(const flux_contribution_type &flux_i, const flux_contribution_type &flux_j, const dealii::Tensor< 1, dim, Number > &c_ij) const
DEAL_II_ALWAYS_INLINE ScalarNumber dry_state_relaxation_small() const
Number inverse_water_depth_mollified(const state_type &U) const
static const auto component_names
Number water_depth_sharp(const state_type &U) const
Vectors::StateVector< ScalarNumber, problem_dimension, n_precomputed_values > StateVector
static constexpr unsigned int n_precomputed_values
static constexpr bool have_high_order_flux
Number inverse_water_depth_sharp(const state_type &U) const
Number kinetic_energy(const state_type &U) const
Number filter_dry_water_depth(const Number &h) const
static constexpr bool have_source_terms
state_type from_primitive_state(const state_type &primitive_state) const
state_type manning_friction(const state_type &U, const Number &h_star, const ScalarNumber tau) const
state_type expand_state(const ST &state) const
std::array< Number, n_initial_precomputed_values > initial_precomputed_type
DEAL_II_ALWAYS_INLINE ScalarNumber manning_friction_coefficient() const
Number speed_of_sound(const state_type &U) const
state_type star_state(const state_type &U, const Number &Z_left, const Number &Z_right) const
dealii::Tensor< 1, problem_dimension, Number > state_type
static dealii::Tensor< 1, dim, Number > momentum(const state_type &U)
state_type affine_shift(const flux_contribution_type &flux_i, const flux_contribution_type &flux_j, const dealii::Tensor< 1, dim, Number > &c_ij, const Number &d_ij) const
std::array< Number, n_precomputed_values > precomputed_type
state_type nodal_source(const PrecomputedVectorView &pv, const unsigned int i, const state_type &U_i, const ScalarNumber tau) const
state_type to_primitive_state(const state_type &state) const
state_type from_initial_state(const ST &initial_state) const
static const auto primitive_component_names
static constexpr unsigned int problem_dimension
flux_type g(const state_type &U) const
Number mathematical_entropy(const state_type &U) const
HyperbolicSystemView(const HyperbolicSystem &hyperbolic_system)
state_type apply_boundary_conditions(const dealii::types::boundary_id id, const state_type &U, const dealii::Tensor< 1, dim, Number > &normal, const Lambda &get_dirichlet_data) const
std::array< state_type, 2 > equilibrated_states(const flux_contribution_type &, const flux_contribution_type &) const
std::tuple< state_type, Number > flux_contribution_type
flux_type f(const state_type &U) const
state_type prescribe_riemann_characteristic(const state_type &U, const state_type &U_bar, const dealii::Tensor< 1, dim, Number > &normal) const
state_type apply_galilei_transform(const state_type &state, const Lambda &lambda) const
DEAL_II_ALWAYS_INLINE ScalarNumber reference_water_depth() const
void fill_precomputed_values(const OfflineData< dim, ScalarNumber > &offline_data, typename HyperbolicSystemView< dim, ScalarNumber >::StateVector &state_vector, const bool skip_constrained_dofs=true) const
HyperbolicSystem(const std::string &subsection="/HyperbolicSystem")
static const std::string problem_name
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 T pow(const T x, const T b)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number positive_part(const Number number)