21 using namespace dealii;
32 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
void atomic_store(T *ptr,
const T value)
34#if KOKKOS_VERSION >= 40000 || defined(KOKKOS_ENABLE_IMPL_DESUL_ATOMICS)
35 Kokkos::atomic_store(ptr, value);
37 Kokkos::atomic_exchange(ptr, value);
42 template <
typename HyperbolicDescription,
int dim,
typename Number>
48 const std::string &subsection )
50 problem_dimension, n_precomputed_values, subsection)
51 , indicator_(hyperbolic_system, subsection +
"/indicator")
52 , limiter_(hyperbolic_system, subsection +
"/limiter")
53 , wave_speed_estimator_(hyperbolic_system,
54 subsection +
"/wave speed estimator")
55 , mpi_ensemble_(mpi_ensemble)
56 , offline_data_(&offline_data)
57 , hyperbolic_system_(&hyperbolic_system)
58 , initial_values_(&initial_values)
63 template <
typename HyperbolicDescription,
int dim,
typename Number>
68 <<
"HyperbolicModule<HyperbolicDescription, dim, Number>::prepare()"
72 const auto limiter_view = limiter_.template view<dim, Number>();
73 AssertThrow(limiter_view.iterations() <= 2,
75 "The number of limiter iterations must be between [0,2]"));
79 [[maybe_unused]]
const auto &hyperbolic_system = *hyperbolic_system_;
80 AssertDimension(hyperbolic_system.state_names(dim).size(),
82 AssertDimension(hyperbolic_system.primitive_names(dim).size(),
84 AssertDimension(hyperbolic_system.precomputed_names(dim).size(),
85 n_precomputed_values);
86 AssertDimension(hyperbolic_system.initial_precomputed_names(dim).size(),
87 n_initial_precomputed_values);
91 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
94 alpha_.reinit_with_scalar_partitioner(
97 bounds_.reinit_with_scalar_partitioner(scalar_partitioner, n_bounds);
98 r_.reinit_with_vector_partitioner(
99 offline_data_->hyperbolic_vector_partitioner(), problem_dimension);
103 boundary_states_.reinit(offline_data_->boundary_indices().size() *
109 const auto &sparsity_simd = offline_data_->sparsity_pattern_simd();
110 dij_matrix_.reinit(sparsity_simd);
111 lij_matrix_.reinit(sparsity_simd);
112 lij_matrix_next_.reinit(sparsity_simd);
113 pij_matrix_.reinit(sparsity_simd);
117 initial_precomputed_ =
118 initial_values_->interpolate_initial_precomputed_vector();
127 bounds_.template move_to_memory_space<MemorySpace>();
128 r_.template move_to_memory_space<MemorySpace>();
130 dij_matrix_.template move_to_memory_space<MemorySpace>();
131 lij_matrix_.template move_to_memory_space<MemorySpace>();
132 lij_matrix_next_.template move_to_memory_space<MemorySpace>();
133 pij_matrix_.template move_to_memory_space<MemorySpace>();
136 initial_precomputed_.template copy_to_memory_space<MemorySpace>();
156 template <
typename HyperbolicDescription,
int dim,
typename Number>
162 std::cout <<
"HyperbolicModule<dim, Number>::reinit_state_vector()"
166 auto &[U, Q, U_dynamic, Q_dynamic] = state_vector;
167 U.reinit_with_vector_partitioner(
168 offline_data_->hyperbolic_vector_partitioner(), problem_dimension);
169 Q.reinit_with_vector_partitioner(
170 offline_data_->precomputed_vector_partitioner(), n_precomputed_values);
174 constexpr auto nan = std::numeric_limits<Number>::signaling_NaN();
175 for (
auto *vector : {&U, &Q}) {
176 auto &data = vector->deal_ii_vector();
177 std::fill_n(data.begin(), data.locally_owned_size(), nan);
190 template <
typename HyperbolicDescription,
int dim,
typename Number>
196 std::cout <<
"HyperbolicModule<HyperbolicDescription, dim, "
197 "Number>::prepare_state_vector()"
203 move_to_memory_space<MemorySpace>(
206 auto &[U, Q, U_dynamic, Q_dynamic] = state_vector;
209 "time step [H] 1 - update boundary values, precompute values");
215 apply_boundary_conditions<MemorySpace>(U, t);
217 U.template deal_ii_vector<MemorySpace>().update_ghost_values();
223 hyperbolic_system_->template fill_precomputed_values<MemorySpace>(
224 *offline_data_, state_vector);
226 Q.template view<n_precomputed_values, MemorySpace>().update_ghost_values();
230 template <
typename HyperbolicDescription,
int dim,
typename Number>
236 std::cout <<
"HyperbolicModule<HyperbolicDescription, dim, "
237 "Number>::fill_precomputed_values()"
243 move_to_memory_space<MemorySpace>(
246 hyperbolic_system_->template fill_precomputed_values<MemorySpace>(
247 *offline_data_, state_vector,
false);
249 state_vector.
precomputed.template view<n_precomputed_values, MemorySpace>()
250 .update_ghost_values();
254 template <
typename HyperbolicDescription,
int dim,
typename Number>
255 template <
typename MemorySpace>
258 const Number t)
const
260 constexpr auto n_comp = problem_dimension;
262 const auto &boundary_indices = offline_data_->boundary_indices();
263 const auto n_boundary_indices =
264 static_cast<unsigned int>(boundary_indices.size());
269 const auto U_view = std::as_const(U).template view<n_comp, MemorySpace>();
270 const auto *indices = boundary_indices.template view<MemorySpace>();
271 auto *states = boundary_states_.template view<MemorySpace>();
273 const auto body = [=](
auto ,
unsigned int k) {
274 const auto U_i = U_view.read_tensor(indices[k]);
275 for (
unsigned int d = 0; d < n_comp; ++d)
276 states[k * n_comp + d] = U_i[d];
279 loop<MemorySpace, Number>(
"hyperbolic_kernel_01a",
289 auto *states = [&]() {
291 ComputingTimer::Scope scope(
292 "time step [X] _ - memory space transfers");
293 return boundary_states_.template view<dealii::MemorySpace::Host>();
295 return boundary_states_.template view<dealii::MemorySpace::Host>();
298 const auto &boundary_map = offline_data_->boundary_map();
299 const auto &boundary_slots = offline_data_->boundary_slots();
300 const auto view = hyperbolic_system_->template view<dim, Number>();
303 for (std::size_t e = 0; e < boundary_map.size(); ++e) {
304 const auto &[i, normal, normal_mass, boundary_mass, id, position] =
321 const auto k = boundary_slots[e];
324 for (
unsigned int d = 0; d < n_comp; ++d)
325 U_i[d] = states[k * n_comp + d];
328 auto get_dirichlet = [position = position, t = t,
this]() {
329 return initial_values_->initial_state(position, t);
332 U_i = view.apply_boundary_conditions(
id, U_i, normal, get_dirichlet);
334 for (
unsigned int d = 0; d < n_comp; ++d)
335 states[k * n_comp + d] = U_i[d];
342 const auto U_view = U.template view<n_comp, MemorySpace>();
343 const auto *indices = boundary_indices.template view<MemorySpace>();
344 const auto *states = [&]() {
346 ComputingTimer::Scope scope(
347 "time step [X] _ - memory space transfers");
348 return std::as_const(boundary_states_).template view<MemorySpace>();
350 return std::as_const(boundary_states_).template view<MemorySpace>();
353 const auto body = [=](
auto ,
unsigned int k) {
355 for (
unsigned int d = 0; d < n_comp; ++d)
356 U_i[d] = states[k * n_comp + d];
357 U_view.write_tensor(U_i, indices[k]);
360 loop<MemorySpace, Number>(
"hyperbolic_kernel_01a",
382 template <
typename T>
383 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
bool
384 all_below_diagonal(
unsigned int i,
const unsigned int *js)
386 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
394 constexpr auto simd_length = T::size();
396 bool all_below_diagonal =
true;
397 for (
unsigned int k = 0; k < simd_length; ++k)
398 if (js[k] >= i + k) {
399 all_below_diagonal =
false;
402 return all_below_diagonal;
408 template <
typename HyperbolicDescription,
int dim,
typename Number>
417 Number tau_max )
const
419 switch (stage_state_vectors.size()) {
421 return forward_euler_step<0>(old_state_vector,
429 return forward_euler_step<1>(old_state_vector,
437 return forward_euler_step<2>(old_state_vector,
445 return forward_euler_step<3>(old_state_vector,
453 return forward_euler_step<4>(old_state_vector,
461 AssertThrow(
false, dealii::ExcMessage(
"Unsupported number of stages."));
467 template <
typename HyperbolicDescription,
int dim,
typename Number>
468 template <
int stages>
472 StageVectors stage_state_vectors,
473 StageWeights stage_weights,
477 Number tau_max)
const
479 Assert(stage_state_vectors.size() == stages &&
480 stage_weights.size() == stages,
481 dealii::ExcMessage(
"The number of stage state vectors and stage "
482 "weights must match the number of stages."));
485 std::cout <<
"HyperbolicModule<HyperbolicDescription, dim, "
486 "Number>::forward_euler_step()"
492 copy_to_memory_space<MemorySpace>(
494 for (
int s = 0; s < stages; ++s)
495 copy_to_memory_space<MemorySpace>(stage_state_vectors.begin()[s].get(),
500 auto &[old_U, old_Q, old_U_dynamic, old_Q_dynamic] = old_state_vector;
501 auto &new_U = new_state_vector.
state;
510 constexpr bool equilibrated_states =
515 const unsigned int n_internal = offline_data_->n_locally_internal();
516 const unsigned int n_owned = offline_data_->n_locally_owned();
520 const auto sparsity_simd_view =
521 offline_data_->sparsity_pattern_simd().template view<MemorySpace>();
523 const auto mass_matrix_view =
524 offline_data_->mass_matrix().template view<MemorySpace>();
525 const auto lumped_mass_matrix_view =
526 offline_data_->lumped_mass_matrix().template view<1, MemorySpace>();
527 const auto lumped_mass_matrix_inverse_view =
528 offline_data_->lumped_mass_matrix_inverse()
529 .template view<1, MemorySpace>();
531 const auto cij_matrix_view =
532 offline_data_->cij_matrix().template view<MemorySpace>();
538 const bool have_discontinuous_ansatz =
539 offline_data_->discretization().have_discontinuous_ansatz();
540 using MatrixReadView =
541 decltype(offline_data_->mass_matrix().template view<MemorySpace>());
542 const auto mass_matrix_inverse_view =
543 have_discontinuous_ansatz
544 ? offline_data_->mass_matrix_inverse().template view<MemorySpace>()
546 const auto incidence_matrix_view =
547 have_discontinuous_ansatz
548 ? offline_data_->incidence_matrix().template view<MemorySpace>()
551 const auto *coupling_boundary_pairs =
552 offline_data_->coupling_boundary_pairs().template view<MemorySpace>();
553 const auto n_coupling_boundary_pairs =
554 offline_data_->coupling_boundary_pairs().size();
556 const Number measure_of_omega_inverse =
557 Number(1.) / offline_data_->measure_of_omega();
561 const auto dij_matrix_view = dij_matrix_.template view<MemorySpace>();
562 const auto lij_matrix_view = lij_matrix_.template view<MemorySpace>();
563 const auto lij_matrix_next_view =
564 lij_matrix_next_.template view<MemorySpace>();
565 const auto pij_matrix_view = pij_matrix_.template view<MemorySpace>();
569 const auto initial_precomputed_view =
571 .template view<n_initial_precomputed_values, MemorySpace>();
573 const auto old_U_view =
574 old_U.template view<problem_dimension, MemorySpace>();
575 const auto old_precomputed_view =
576 old_Q.template view<n_precomputed_values, MemorySpace>();
583 using HyperbolicVectorView = std::remove_const_t<
decltype(old_U_view)>;
584 using PrecomputedVectorView =
585 std::remove_const_t<
decltype(old_precomputed_view)>;
587 Kokkos::Array<HyperbolicVectorView, stages> stage_U_view;
588 Kokkos::Array<PrecomputedVectorView, stages> stage_precomputed_view;
589 Kokkos::Array<Number, stages> stage_weight;
590 for (
int s = 0; s < stages; ++s) {
591 const auto &[U_s, Q_s, U_dynamic_s, Q_dynamic_s] =
592 stage_state_vectors.begin()[s].get();
593 stage_U_view[s] = U_s.template view<problem_dimension, MemorySpace>();
594 stage_precomputed_view[s] =
595 Q_s.template view<n_precomputed_values, MemorySpace>();
596 stage_weight[s] = stage_weights.begin()[s];
599 const auto new_U_view =
600 new_U.template view<problem_dimension, MemorySpace>();
602 const auto alpha_view = alpha_.template view<1, MemorySpace>();
603 const auto bounds_view = bounds_.template view<n_bounds, MemorySpace>();
604 const auto r_view = r_.template view<problem_dimension, MemorySpace>();
613 const Number cfl = this->cfl_;
615 const auto hyperbolic_system_views =
616 make_select_view<dim, Number, MemorySpace>(*hyperbolic_system_);
618 const auto indicator_views =
619 make_select_view<dim, Number, MemorySpace>(indicator_);
621 const auto limiter_views =
622 make_select_view<dim, Number, MemorySpace>(limiter_);
623 const auto n_limiter_iterations =
624 limiter_.template view<dim, Number>().iterations();
626 const auto wave_speed_estimator_views =
627 make_select_view<dim, Number, MemorySpace>(wave_speed_estimator_);
635 const auto scoped_name = [&step_no](
const auto &name,
636 const bool advance =
true) {
637 advance || step_no--;
638 return "time step [H] " + std::to_string(++step_no) +
" - " + name;
641 const auto loop_name = [&step_no]() {
642 return "hyperbolic_kernel_" + Utilities::int_to_string(step_no, 2);
646 Mirrored<int> restart_needed(
"hyperbolic_module_restart_needed",
648 *restart_needed.view() = 0;
676 ComputingTimer::Scope scope(scoped_name(
"compute d_ij, and alpha_i"));
678 const auto body = [=](
auto sentinel,
unsigned int i) {
679 using T =
decltype(sentinel);
681 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
683 const auto wave_speed_estimator_view =
684 wave_speed_estimator_views.template view<T>();
686 auto indicator_view = indicator_views.template view<T>();
689 const unsigned int row_length = sparsity_simd_view.row_length(i);
693 const auto view = hyperbolic_system_views.template view<T>();
695 const auto U_i = old_U_view.template read_tensor<T>(i);
696 const auto flux_i = view.flux_contribution(
697 old_precomputed_view, initial_precomputed_view, i, U_i);
699 indicator_view.reset(old_precomputed_view, i, U_i, flux_i);
701 const unsigned int *js = sparsity_simd_view.columns(i);
702 for (
unsigned int col_idx = 0; col_idx < row_length;
703 ++col_idx, js += stride_size) {
705 const auto U_j = old_U_view.template read_tensor<T>(js);
706 const auto flux_j = view.flux_contribution(
707 old_precomputed_view, initial_precomputed_view, js, U_j);
709 const auto c_ij = cij_matrix_view.template read_tensor<T>(i, col_idx);
711 indicator_view.accumulate(
712 old_precomputed_view, js, U_j, flux_j, c_ij);
719 if (all_below_diagonal<T>(i, js))
722 const auto norm = c_ij.norm();
723 const auto n_ij = c_ij / norm;
724 const auto lambda_max = wave_speed_estimator_view.compute(
725 old_precomputed_view, i, js, U_i, U_j, flux_i, flux_j, n_ij);
726 const auto d_ij = norm * lambda_max;
728 dij_matrix_view.write_entry(d_ij, i, col_idx,
true);
731 const auto mass = lumped_mass_matrix_view.template read_entry<T>(i);
732 const auto hd_i = mass * measure_of_omega_inverse;
733 alpha_view.template write_entry<T>(indicator_view.alpha(hd_i), i);
736 loop<MemorySpace, Number>(loop_name(), body, 0, n_internal, n_owned);
738 alpha_view.update_ghost_values();
748 ComputingTimer::Scope scope(
749 scoped_name(
"compute bdry d_ij, diag d_ii, and tau_max"));
765 const auto body_boundary = [=](
auto,
const unsigned int k) {
766 const auto &[i, col_idx, j] = coupling_boundary_pairs[k];
768 const auto wave_speed_estimator_view =
769 wave_speed_estimator_views.template view<Number>();
782 const auto view = hyperbolic_system_views.template view<Number>();
784 const auto U_i = old_U_view.read_tensor(i);
785 const auto U_j = old_U_view.read_tensor(j);
786 const auto flux_i = view.flux_contribution(
787 old_precomputed_view, initial_precomputed_view, i, U_i);
788 const auto flux_j = view.flux_contribution(
789 old_precomputed_view, initial_precomputed_view, j, U_j);
791 const auto c_ji = cij_matrix_view.read_transposed_tensor(i, col_idx);
792 Assert(c_ji.norm() > 1.e-12, ExcInternalError());
793 const auto norm_ji = c_ji.norm();
794 const auto n_ji = c_ji / norm_ji;
796 const auto d_ij = dij_matrix_view.read_entry(i, col_idx);
798 const auto lambda_max = wave_speed_estimator_view.compute(
799 old_precomputed_view, j, &i, U_j, U_i, flux_j, flux_i, n_ji);
800 const auto d_ji = norm_ji * lambda_max;
802 dij_matrix_view.write_entry(std::max(d_ij, d_ji), i, col_idx);
805 loop<MemorySpace, Number>(loop_name() +
'a',
809 n_coupling_boundary_pairs);
812 const auto body = [=](
auto,
unsigned int i) -> Number {
814#ifdef DEBUG_SYMMETRY_CHECK
815 const auto view = hyperbolic_system_views.template view<Number>();
816 const auto wave_speed_estimator_view =
817 wave_speed_estimator_views.template view<Number>();
821 const unsigned int row_length = sparsity_simd_view.row_length(i);
823 return std::numeric_limits<Number>::max();
825 Number d_sum = Number(0.);
828 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
829 const unsigned int *js = sparsity_simd_view.columns(i);
830 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
831 const auto j = *(js + col_idx * stride_size);
835 const auto d_ji = dij_matrix_view.read_transposed_entry(i, col_idx);
837#ifdef DEBUG_SYMMETRY_CHECK
840 const auto U_i = old_U_view.read_tensor(i);
841 const auto U_j = old_U_view.read_tensor(j);
842 const auto flux_i = view.flux_contribution(
843 old_precomputed_view, initial_precomputed_view, i, U_i);
844 const auto flux_j = view.flux_contribution(
845 old_precomputed_view, initial_precomputed_view, j, U_j);
847 const auto c_ij = cij_matrix_view.read_tensor(i, col_idx);
848 Assert(c_ij.norm() > 1.e-12, ExcInternalError());
849 const auto norm_ij = c_ij.norm();
850 const auto n_ij = c_ij / norm_ij;
852 const auto lambda_max = wave_speed_estimator_view.compute(
853 old_precomputed_view, i, &j, U_i, U_j, flux_i, flux_j, n_ij);
854 const auto d_ij = norm_ij * lambda_max;
856 Assert(d_ij <= d_ji + 1.0e-12,
857 dealii::ExcMessage(
"d_ij not symmetrized correctly on "
858 "boundary degrees of freedom."));
861 dij_matrix_view.write_entry(d_ji, i, col_idx);
864 d_sum -= dij_matrix_view.read_entry(i, col_idx);
873 std::min(d_sum, Number(-1.e6) * std::numeric_limits<Number>::min());
876 dij_matrix_view.write_entry(d_sum, i, 0);
878 const Number mass = lumped_mass_matrix_view.read_entry(i);
879 return cfl * mass / (Number(-2.) * d_sum);
882 reduction_loop<MemorySpace>(
883 loop_name() +
'b', body, Kokkos::Min<Number>(tau_max), 0, n_owned);
887 ComputingTimer::Scope scope(
"time step [X] _ - synchronization barriers");
893 tau_max = Utilities::MPI::min(
894 tau_max, mpi_ensemble_.synchronization_communicator());
897 !std::isnan(tau_max) && !std::isinf(tau_max) && tau_max > 0.,
899 "I'm sorry, Dave. I'm afraid I can't do that.\nWe crashed."));
901 tau = (tau == Number(0.) ? tau_max : tau);
904 std::cout <<
" computed tau_max = " << tau_max <<
" (CFL = " << cfl
906 std::cout <<
" step with tau = " << tau << std::endl;
910 *restart_needed.view() =
911 (tau > this->acceptable_tau_max_ratio_ * tau_max);
914 if (*restart_needed.view() && this->id_violation_strategy_ ==
919 std::cout <<
" signalling restart (suggested_tau_max = "
920 << tau_max <<
")" << std::endl;
923 throw Restart{tau_max};
928 int *restart_needed_view = restart_needed.view<MemorySpace>();
932 dij_matrix_view.update_ghost_rows();
942 ComputingTimer::Scope scope(
943 scoped_name(
"l.-o. update, compute bounds, r_i, and p_ij"));
945 const Number weight =
946 -std::accumulate(stage_weights.begin(), stage_weights.end(), -1.);
948 const auto body = [=](
auto sentinel,
949 auto have_discontinuous_ansatz,
950 const unsigned int i) {
951 using T =
decltype(sentinel);
953 const auto view = hyperbolic_system_views.template view<T>();
955 using View =
decltype(view);
956 using flux_contribution_type =
typename View::flux_contribution_type;
957 using state_type =
typename View::state_type;
959 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
961 auto limiter_view = limiter_views.template view<T>();
964 const unsigned int row_length = sparsity_simd_view.row_length(i);
968 const auto U_i = old_U_view.template read_tensor<T>(i);
971 const auto alpha_i = alpha_view.template read_entry<T>(i);
972 const auto m_i = lumped_mass_matrix_view.template read_entry<T>(i);
974 lumped_mass_matrix_inverse_view.template read_entry<T>(i);
976 const auto flux_i = view.flux_contribution(
977 old_precomputed_view, initial_precomputed_view, i, U_i);
979 Kokkos::Array<flux_contribution_type, stages> flux_iHs;
980 [[maybe_unused]] state_type S_iH;
982 for (
int s = 0; s < stages; ++s) {
983 const auto U_iHs = stage_U_view[s].template read_tensor<T>(i);
984 flux_iHs[s] = view.flux_contribution(
985 stage_precomputed_view[s], initial_precomputed_view, i, U_iHs);
987 if constexpr (View::have_source_terms) {
988 S_iH += stage_weight[s] *
989 view.nodal_source(stage_precomputed_view[s], i, U_iHs, tau);
993 [[maybe_unused]] state_type S_i;
996 if constexpr (View::have_source_terms) {
997 S_i = view.nodal_source(old_precomputed_view, i, U_i, tau);
998 S_iH += weight * S_i;
999 U_i_new += tau * S_i;
1003 limiter_view.reset(old_precomputed_view, i, U_i, flux_i);
1005 [[maybe_unused]] state_type affine_shift;
1007 const unsigned int *js = sparsity_simd_view.columns(i);
1008 if constexpr (equilibrated_states) {
1009 for (
unsigned int col_idx = 0; col_idx < row_length;
1010 ++col_idx, js += stride_size) {
1012 const auto U_j = old_U_view.template read_tensor<T>(js);
1013 const auto flux_j = view.flux_contribution(
1014 old_precomputed_view, initial_precomputed_view, js, U_j);
1017 dij_matrix_view.template read_entry<T>(i, col_idx);
1019 cij_matrix_view.template read_tensor<T>(i, col_idx);
1021 const auto B_ij = view.affine_shift(flux_i, flux_j, c_ij, d_ij);
1022 affine_shift += B_ij;
1025 affine_shift *= tau * m_i_inv;
1028 if constexpr (View::have_source_terms) {
1029 affine_shift += tau * S_i;
1032 js = sparsity_simd_view.columns(i);
1033 for (
unsigned int col_idx = 0; col_idx < row_length;
1034 ++col_idx, js += stride_size) {
1036 const auto U_j = old_U_view.template read_tensor<T>(js);
1038 const auto alpha_j = alpha_view.template read_entry<T>(js);
1040 const auto d_ij = dij_matrix_view.template read_entry<T>(i, col_idx);
1041 auto factor = (alpha_i + alpha_j) * Number(.5);
1043 if constexpr (have_discontinuous_ansatz) {
1044 const auto incidence_ij =
1045 incidence_matrix_view.template read_entry<T>(i, col_idx);
1046 factor = std::max(factor, incidence_ij);
1049 const auto d_ijH = d_ij * factor;
1051#ifdef DEBUG_SYMMETRY_CHECK
1059 dij_matrix_view.template read_transposed_entry<T>(i, col_idx);
1060 Assert(std::max(std::abs(d_ij - d_ji), T(1.0e-12)) == T(1.0e-12),
1062 "d_ij not symmetrized correctly over MPI ranks"));
1065 const auto c_ij = cij_matrix_view.template read_tensor<T>(i, col_idx);
1066 constexpr auto eps = std::numeric_limits<Number>::epsilon();
1069 ryujin::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
1070 std::abs(d_ij), T(eps * eps), T(0.), T(1.) / d_ij);
1072 const auto scaled_c_ij = c_ij * scale;
1074 const auto flux_j = view.flux_contribution(
1075 old_precomputed_view, initial_precomputed_view, js, U_j);
1077 const auto m_ij = mass_matrix_view.template read_entry<T>(i, col_idx);
1083 const auto flux_ij = view.flux_divergence(flux_i, flux_j, c_ij);
1084 U_i_new += tau * m_i_inv * flux_ij;
1085 auto P_ij = -flux_ij;
1087 if constexpr (equilibrated_states) {
1088 const auto &[U_star_ij, U_star_ji] =
1089 view.equilibrated_states(flux_i, flux_j);
1091 U_i_new += tau * m_i_inv * d_ij * (U_star_ji - U_star_ij);
1092 F_iH += d_ijH * (U_star_ji - U_star_ij);
1093 P_ij += (d_ijH - d_ij) * (U_star_ji - U_star_ij);
1095 U_i_new += tau * m_i_inv * d_ij * (U_j - U_i);
1096 F_iH += d_ijH * (U_j - U_i);
1097 P_ij += (d_ijH - d_ij) * (U_j - U_i);
1100 limiter_view.accumulate(
1101 old_precomputed_view, js, U_j, flux_j, scaled_c_ij, affine_shift);
1103 if constexpr (View::have_source_terms) {
1104 F_iH -= m_ij * S_iH;
1112 if constexpr (View::have_high_order_flux) {
1113 const auto high_order_flux_ij =
1114 view.high_order_flux_divergence(flux_i, flux_j, c_ij);
1115 F_iH += weight * high_order_flux_ij;
1116 P_ij += weight * high_order_flux_ij;
1118 F_iH += weight * flux_ij;
1119 P_ij += weight * flux_ij;
1122 if constexpr (View::have_source_terms) {
1124 view.nodal_source(old_precomputed_view, js, U_j, tau);
1125 F_iH += weight * m_ij * S_j;
1126 P_ij += weight * m_ij * S_j;
1129 for (
int s = 0; s < stages; ++s) {
1130 const auto U_jHs = stage_U_view[s].template read_tensor<T>(js);
1131 const auto flux_jHs = view.flux_contribution(
1132 stage_precomputed_view[s], initial_precomputed_view, js, U_jHs);
1134 if constexpr (View::have_high_order_flux) {
1135 const auto high_order_flux_ij =
1136 view.high_order_flux_divergence(flux_iHs[s], flux_jHs, c_ij);
1137 F_iH += stage_weight[s] * high_order_flux_ij;
1138 P_ij += stage_weight[s] * high_order_flux_ij;
1140 const auto flux_ij =
1141 view.flux_divergence(flux_iHs[s], flux_jHs, c_ij);
1142 F_iH += stage_weight[s] * flux_ij;
1143 P_ij += stage_weight[s] * flux_ij;
1146 if constexpr (View::have_source_terms) {
1148 view.nodal_source(stage_precomputed_view[s], js, U_jHs, tau);
1149 F_iH += stage_weight[s] * m_ij * S_js;
1150 P_ij += stage_weight[s] * m_ij * S_js;
1154 pij_matrix_view.template write_tensor<T>(P_ij, i, col_idx,
true);
1157#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
1158 if (!view.is_admissible(U_i_new)) {
1159 atomic_store(restart_needed_view, 1);
1163 new_U_view.template write_tensor<T>(U_i_new, i);
1164 r_view.template write_tensor<T>(F_iH, i);
1166 const auto hd_i = m_i * measure_of_omega_inverse;
1167 const auto relaxed_bounds = limiter_view.bounds(hd_i);
1168 bounds_view.template write_tensor<T>(relaxed_bounds, i);
1177 if (have_discontinuous_ansatz) {
1178 loop<MemorySpace, Number>(
1179 loop_name(), body, 0, n_internal, n_owned, std::true_type{});
1181 loop<MemorySpace, Number>(
1182 loop_name(), body, 0, n_internal, n_owned, std::false_type{});
1185 r_view.update_ghost_values();
1186 if (have_discontinuous_ansatz) {
1192 bounds_view.update_ghost_values();
1202 if (n_limiter_iterations != 0) {
1203 ComputingTimer::Scope scope(scoped_name(
"compute p_ij, and l_ij"));
1205 const auto body = [=](
auto sentinel,
1206 auto have_discontinuous_ansatz,
1207 const unsigned int i) {
1208 using T =
decltype(sentinel);
1210 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
1212 auto limiter_view = limiter_views.template view<T>();
1215 const unsigned int row_length = sparsity_simd_view.row_length(i);
1216 if (row_length == 1)
1220 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(i);
1227 if constexpr (have_discontinuous_ansatz) {
1229 const unsigned int *js = sparsity_simd_view.columns(i) + stride_size;
1230 for (
unsigned int col_idx = 1; col_idx < row_length;
1231 ++col_idx, js += stride_size) {
1232 local_bounds = limiter_view.combine_bounds(
1234 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(
1237 bounds_view.template write_tensor<T>(local_bounds, i);
1240 [[maybe_unused]] T m_i;
1241 if constexpr (have_discontinuous_ansatz)
1242 m_i = lumped_mass_matrix_view.template read_entry<T>(i);
1244 const auto m_i_inv =
1245 lumped_mass_matrix_inverse_view.template read_entry<T>(i);
1247 const auto U_i_new = new_U_view.template read_tensor<T>(i);
1249 const auto F_iH = r_view.template read_tensor<T>(i);
1251 const auto lambda_inv = Number(row_length - 1);
1252 const auto factor = tau * m_i_inv * lambda_inv;
1262 auto P_ij = pij_matrix_view.template read_tensor<T>(i, 1);
1265 const unsigned int *js = sparsity_simd_view.columns(i) + stride_size;
1266 for (
unsigned int col_idx = 1; col_idx < row_length;
1267 ++col_idx, js += stride_size) {
1269 const auto P_ij_next = pij_matrix_view.template read_tensor<T>(
1270 i, col_idx + 1 < row_length ? col_idx + 1 : col_idx);
1271 const auto F_jH = r_view.template read_tensor<T>(js);
1277 const auto kronecker_ij = col_idx == 0 ? T(1.) : T(0.);
1279 if constexpr (have_discontinuous_ansatz) {
1282 const auto m_j = lumped_mass_matrix_view.template read_entry<T>(js);
1283 const auto m_ij_inv =
1284 mass_matrix_inverse_view.template read_entry<T>(i, col_idx);
1285 const auto b_ij = m_i * m_ij_inv - kronecker_ij;
1286 const auto b_ji = m_j * m_ij_inv - kronecker_ij;
1288 P_ij += b_ij * F_jH - b_ji * F_iH;
1293 const auto m_j_inv =
1294 lumped_mass_matrix_inverse_view.template read_entry<T>(js);
1296 mass_matrix_view.template read_entry<T>(i, col_idx);
1297 const auto b_ij = kronecker_ij - m_ij * m_j_inv;
1298 const auto b_ji = kronecker_ij - m_ij * m_i_inv;
1300 P_ij += b_ij * F_jH - b_ji * F_iH;
1304 pij_matrix_view.template write_tensor<T>(P_ij, i, col_idx);
1310 const auto &[l_ij, success] =
1311 limiter_view.limit(local_bounds, U_i_new, P_ij);
1312 lij_matrix_view.template write_entry<T>(l_ij, i, col_idx,
true);
1323 atomic_store(restart_needed_view, 1);
1335 if (have_discontinuous_ansatz) {
1336 loop<MemorySpace, Number>(
1337 loop_name(), body, 0, n_internal, n_owned, std::true_type{});
1339 loop<MemorySpace, Number>(
1340 loop_name(), body, 0, n_internal, n_owned, std::false_type{});
1343 lij_matrix_view.update_ghost_rows();
1356 for (
unsigned int pass = 0; pass < n_limiter_iterations; ++pass) {
1357 bool last_round = (pass + 1 == n_limiter_iterations);
1359 std::string additional_step = (last_round ?
"" :
", next l_ij");
1360 ComputingTimer::Scope scope(
1361 scoped_name(
"symmetrize l_ij, h.-o. update" + additional_step));
1363 const auto lij_view = (n_limiter_iterations == 2 && last_round)
1364 ? lij_matrix_next_view
1367 const auto body = [=](
auto sentinel,
const unsigned int i) {
1368 using T =
decltype(sentinel);
1370 auto limiter_view = limiter_views.template view<T>();
1373 const unsigned int row_length = sparsity_simd_view.row_length(i);
1374 if (row_length == 1)
1377 auto U_i_new = new_U_view.template read_tensor<T>(i);
1379 const Number lambda = Number(1.) / Number(row_length - 1);
1382 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1385 std::min(lij_view.template read_entry<T>(i, col_idx),
1386 lij_view.template read_transposed_entry<T>(i, col_idx));
1388 const auto p_ij = pij_matrix_view.template read_tensor<T>(i, col_idx);
1390 U_i_new += l_ij * lambda * p_ij;
1393#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
1394 const auto view = hyperbolic_system_views.template view<T>();
1395 if (!view.is_admissible(U_i_new)) {
1396 atomic_store(restart_needed_view, 1);
1400 new_U_view.template write_tensor<T>(U_i_new, i);
1406 const auto local_bounds =
1407 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(i);
1409 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1411 const auto old_l_ij =
1412 std::min(lij_view.template read_entry<T>(i, col_idx),
1413 lij_view.template read_transposed_entry<T>(i, col_idx));
1415 const auto new_p_ij =
1416 (T(1.) - old_l_ij) *
1417 pij_matrix_view.template read_tensor<T>(i, col_idx);
1419 const auto &[new_l_ij, success] =
1420 limiter_view.limit(local_bounds, U_i_new, new_p_ij);
1432#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
1434 atomic_store(restart_needed_view, 1);
1443 const auto entry = (T(1.) - old_l_ij) * new_l_ij;
1444 lij_matrix_next_view.write_entry(entry, i, col_idx,
true);
1448 loop<MemorySpace, Number>(loop_name(), body, 0, n_internal, n_owned);
1451 lij_matrix_next_view.update_ghost_rows();
1465 ComputingTimer::Scope scope(
"time step [X] _ - synchronization barriers");
1476 int &restart_flag = *restart_needed.view();
1477 restart_flag = Utilities::MPI::logical_or(
1478 restart_flag != 0, mpi_ensemble_.synchronization_communicator());
1481 if (*restart_needed.view()) {
1482 switch (this->id_violation_strategy_) {
1484 this->n_warnings_++;
1486 std::cout <<
" raised warning, CFL/IDP violation encountered "
1491 this->n_restarts_++;
1494 std::cout <<
" signalling restart (suggested_tau_max = "
1495 << tau_max <<
")" << std::endl;
1497 throw Restart{tau_max};
static dealii::Timer & timer(const std::string §ion)
typename Interface::HyperbolicModule< dim, Number >::StageWeights StageWeights
HyperbolicModule(const MPIEnsemble &mpi_ensemble, const OfflineData< dim, Number > &offline_data, const HyperbolicSystem &hyperbolic_system, const InitialValues< HyperbolicDescription, dim, Number > &initial_values, const std::string &subsection="/HyperbolicModule")
typename HyperbolicDescription::HyperbolicSystem HyperbolicSystem
typename Interface::HyperbolicModule< dim, Number >::StageVectors StageVectors
void prepare_state_vector(StateVector< Number > &state_vector, Number t) const override
void reinit_state_vector(StateVector< Number > &state_vector) const override
Number forward_euler_step(const StateVector< Number > &old_state_vector, StageVectors stage_state_vectors, StageWeights stage_weights, StateVector< Number > &new_state_vector, Number t, Number tau, Number tau_max=std::numeric_limits< Number >::max()) const override
void fill_precomputed_values(StateVector< Number > &state_vector) const override
std::conditional_t< have_separate_memory_spaces, dealii::MemorySpace::Default, dealii::MemorySpace::Host > selected_memory_space_t
constexpr bool have_separate_memory_spaces
void debug_poison_invalid_values(StateVector< Number > &state_vector, const OfflineData &offline_data)
std::vector< ScalarVector< Number > > dynamic_state
MultiComponentVector< Number > state
MultiComponentVector< Number > precomputed