ryujin 2.5.0 revision c64172211cf21344cb9b9b0f3a1266aa6400d392
Loading...
Searching...
No Matches
hyperbolic_module.template.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
9#include <ryujin/base/gpu.h>
10#include <ryujin/base/loop.h>
12#include <ryujin/base/simd.h>
15
16#include <numeric>
17#include <utility>
18
19namespace ryujin
20{
21 using namespace dealii;
22
23 namespace
24 {
31 template <typename T>
32 DEAL_II_HOST_DEVICE_ALWAYS_INLINE void atomic_store(T *ptr, const T value)
33 {
34#if KOKKOS_VERSION >= 40000 || defined(KOKKOS_ENABLE_IMPL_DESUL_ATOMICS)
35 Kokkos::atomic_store(ptr, value);
36#else
37 Kokkos::atomic_exchange(ptr, value);
38#endif
39 }
40 } // namespace
41
42 template <typename HyperbolicDescription, int dim, typename Number>
44 const MPIEnsemble &mpi_ensemble,
45 const OfflineData<dim, Number> &offline_data,
46 const HyperbolicSystem &hyperbolic_system,
48 const std::string &subsection /*= "HyperbolicModule"*/)
49 : Interface::HyperbolicModule<dim, Number>(
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)
59 {
60 }
61
62
63 template <typename HyperbolicDescription, int dim, typename Number>
65 {
66#ifdef DEBUG_OUTPUT
67 std::cout
68 << "HyperbolicModule<HyperbolicDescription, dim, Number>::prepare()"
69 << std::endl;
70#endif
71
72 const auto limiter_view = limiter_.template view<dim, Number>();
73 AssertThrow(limiter_view.iterations() <= 2,
74 dealii::ExcMessage(
75 "The number of limiter iterations must be between [0,2]"));
76
77 /* Ensure dimensions match: */
78
79 [[maybe_unused]] const auto &hyperbolic_system = *hyperbolic_system_;
80 AssertDimension(hyperbolic_system.state_names(dim).size(),
81 problem_dimension);
82 AssertDimension(hyperbolic_system.primitive_names(dim).size(),
83 problem_dimension);
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);
88
89 /* Initialize vectors: */
90
91 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
92
93 /* The alpha vector is also read on the host memory space: */
94 alpha_.reinit_with_scalar_partitioner(
95 scalar_partitioner, 1, TransferPolicy::implicit_transfers);
96
97 bounds_.reinit_with_scalar_partitioner(scalar_partitioner, n_bounds);
98 r_.reinit_with_vector_partitioner(
99 offline_data_->hyperbolic_vector_partitioner(), problem_dimension);
100
101 /* Initialize the compact buffer used for updating boundary values: */
102
103 boundary_states_.reinit(offline_data_->boundary_indices().size() *
104 problem_dimension,
106
107 /* Initialize matrices: */
108
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);
114
115 /* Set up initial precomputed vector: */
116
117 initial_precomputed_ =
118 initial_values_->interpolate_initial_precomputed_vector();
119
120 /*
121 * Move all temporary data structures to the correct memory space:
122 */
123
124 if constexpr (have_separate_memory_spaces) {
125 using MemorySpace = selected_memory_space_t;
126
127 bounds_.template move_to_memory_space<MemorySpace>();
128 r_.template move_to_memory_space<MemorySpace>();
129
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>();
134
135 /* The initial_precomputed vector is also read on the host memory space:*/
136 initial_precomputed_.template copy_to_memory_space<MemorySpace>();
137 }
138 }
139
140
141 /*
142 * -------------------------------------------------------------------------
143 * Step 0: Reinitialize vector
144 * -------------------------------------------------------------------------
145 */
146
147
156 template <typename HyperbolicDescription, int dim, typename Number>
157 void
159 StateVector<Number> &state_vector) const
160 {
161#ifdef DEBUG_OUTPUT
162 std::cout << "HyperbolicModule<dim, Number>::reinit_state_vector()"
163 << std::endl;
164#endif
165
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);
171
172#ifdef DEBUG
173 /* Poison all vectors: */
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);
178 }
179#endif
180 }
181
182
183 /*
184 * -------------------------------------------------------------------------
185 * Step 1: Apply boundary conditions and precompute values
186 * -------------------------------------------------------------------------
187 */
188
189
190 template <typename HyperbolicDescription, int dim, typename Number>
191 void
193 StateVector<Number> &state_vector, Number t) const
194 {
195#ifdef DEBUG_OUTPUT
196 std::cout << "HyperbolicModule<HyperbolicDescription, dim, "
197 "Number>::prepare_state_vector()"
198 << std::endl;
199#endif
200
201 using MemorySpace = selected_memory_space_t;
202
203 move_to_memory_space<MemorySpace>(
205
206 auto &[U, Q, U_dynamic, Q_dynamic] = state_vector;
207
209 "time step [H] 1 - update boundary values, precompute values");
210
211 /*
212 * Update boundary values and distribute the result over all MPI ranks.
213 */
214
215 apply_boundary_conditions<MemorySpace>(U, t);
216
217 U.template deal_ii_vector<MemorySpace>().update_ghost_values();
218
219 /*
220 * Compute and populate precomputed values.
221 */
222
223 hyperbolic_system_->template fill_precomputed_values<MemorySpace>(
224 *offline_data_, state_vector);
225
226 Q.template view<n_precomputed_values, MemorySpace>().update_ghost_values();
227 }
228
229
230 template <typename HyperbolicDescription, int dim, typename Number>
231 void
233 StateVector<Number> &state_vector) const
234 {
235#ifdef DEBUG_OUTPUT
236 std::cout << "HyperbolicModule<HyperbolicDescription, dim, "
237 "Number>::fill_precomputed_values()"
238 << std::endl;
239#endif
240
241 using MemorySpace = selected_memory_space_t;
242
243 move_to_memory_space<MemorySpace>(
245
246 hyperbolic_system_->template fill_precomputed_values<MemorySpace>(
247 *offline_data_, state_vector, /*skip_constrained_dofs*/ false);
248
249 state_vector.precomputed.template view<n_precomputed_values, MemorySpace>()
250 .update_ghost_values();
251 }
252
253
254 template <typename HyperbolicDescription, int dim, typename Number>
255 template <typename MemorySpace>
258 const Number t) const
259 {
260 constexpr auto n_comp = problem_dimension;
261
262 const auto &boundary_indices = offline_data_->boundary_indices();
263 const auto n_boundary_indices =
264 static_cast<unsigned int>(boundary_indices.size());
265
266 /* Gather all boundary states into a mirrored buffer: */
267
268 {
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>();
272
273 const auto body = [=](auto /*sentinel*/, 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];
277 };
278
279 loop<MemorySpace, Number>("hyperbolic_kernel_01a",
280 body,
281 0,
282 /*no vectorization*/ 0,
283 n_boundary_indices);
284 }
285
286 /* Apply boundary conditions on the host memory space: */
287
288 {
289 auto *states = [&]() {
290 if constexpr (have_separate_memory_spaces) {
291 ComputingTimer::Scope scope(
292 "time step [X] _ - memory space transfers");
293 return boundary_states_.template view<dealii::MemorySpace::Host>();
294 }
295 return boundary_states_.template view<dealii::MemorySpace::Host>();
296 }();
297
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>();
301
302 /* FIXME: not thread parallel... */
303 for (std::size_t e = 0; e < boundary_map.size(); ++e) {
304 const auto &[i, normal, normal_mass, boundary_mass, id, position] =
305 boundary_map[e];
306
307 /*
308 * Relay the task of applying appropriate boundary conditions to the
309 * Problem Description.
310 */
311
312 if (id == Boundary::do_nothing)
313 continue;
314
315 /*
316 * Note: The boundary map can contain more than one entry for the
317 * same degree of freedom. All such entries share the same position
318 * in the buffer so that boundary conditions compose in the same way
319 * as they would when operating on the state vector directly.
320 */
321 const auto k = boundary_slots[e];
322
323 state_type U_i;
324 for (unsigned int d = 0; d < n_comp; ++d)
325 U_i[d] = states[k * n_comp + d];
326
327 /* Use a lambda to avoid computing unnecessary state values */
328 auto get_dirichlet = [position = position, t = t, this]() {
329 return initial_values_->initial_state(position, t);
330 };
331
332 U_i = view.apply_boundary_conditions(id, U_i, normal, get_dirichlet);
333
334 for (unsigned int d = 0; d < n_comp; ++d)
335 states[k * n_comp + d] = U_i[d];
336 }
337 }
338
339 /* Write back the updated boundary states: */
340
341 {
342 const auto U_view = U.template view<n_comp, MemorySpace>();
343 const auto *indices = boundary_indices.template view<MemorySpace>();
344 const auto *states = [&]() {
345 if constexpr (have_separate_memory_spaces) {
346 ComputingTimer::Scope scope(
347 "time step [X] _ - memory space transfers");
348 return std::as_const(boundary_states_).template view<MemorySpace>();
349 }
350 return std::as_const(boundary_states_).template view<MemorySpace>();
351 }();
352
353 const auto body = [=](auto /*sentinel*/, unsigned int k) {
354 state_type U_i;
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]);
358 };
359
360 loop<MemorySpace, Number>("hyperbolic_kernel_01a",
361 body,
362 0,
363 /*no vectorization*/ 0,
364 n_boundary_indices);
365 }
366 }
367
368
369 /*
370 * -------------------------------------------------------------------------
371 * Step 2 - 7: Perform an explicit Euler step
372 * -------------------------------------------------------------------------
373 */
374
375
376 namespace
377 {
382 template <typename T>
383 DEAL_II_HOST_DEVICE_ALWAYS_INLINE bool
384 all_below_diagonal(unsigned int i, const unsigned int *js)
385 {
386 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
387 /* Non-vectorized sequential access. */
388 const auto j = *js;
389 return j < i;
390
391 } else {
392 /* Vectorized fast access. index must be divisible by simd_length */
393
394 constexpr auto simd_length = T::size();
395
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;
400 break;
401 }
402 return all_below_diagonal;
403 }
404 }
405 } // namespace
406
407
408 template <typename HyperbolicDescription, int dim, typename Number>
409 Number
411 const StateVector<Number> &old_state_vector,
412 StageVectors stage_state_vectors,
413 StageWeights stage_weights,
414 StateVector<Number> &new_state_vector,
415 Number t,
416 Number tau,
417 Number tau_max /*= std::numeric_limits<Number>::max()*/) const
418 {
419 switch (stage_state_vectors.size()) {
420 case 0:
421 return forward_euler_step<0>(old_state_vector,
422 stage_state_vectors,
423 stage_weights,
424 new_state_vector,
425 t,
426 tau,
427 tau_max);
428 case 1:
429 return forward_euler_step<1>(old_state_vector,
430 stage_state_vectors,
431 stage_weights,
432 new_state_vector,
433 t,
434 tau,
435 tau_max);
436 case 2:
437 return forward_euler_step<2>(old_state_vector,
438 stage_state_vectors,
439 stage_weights,
440 new_state_vector,
441 t,
442 tau,
443 tau_max);
444 case 3:
445 return forward_euler_step<3>(old_state_vector,
446 stage_state_vectors,
447 stage_weights,
448 new_state_vector,
449 t,
450 tau,
451 tau_max);
452 case 4:
453 return forward_euler_step<4>(old_state_vector,
454 stage_state_vectors,
455 stage_weights,
456 new_state_vector,
457 t,
458 tau,
459 tau_max);
460 default:
461 AssertThrow(false, dealii::ExcMessage("Unsupported number of stages."));
462 __builtin_trap();
463 }
464 }
465
466
467 template <typename HyperbolicDescription, int dim, typename Number>
468 template <int stages>
469 Number
471 const StateVector<Number> &old_state_vector,
472 StageVectors stage_state_vectors,
473 StageWeights stage_weights,
474 StateVector<Number> &new_state_vector,
475 Number /*t*/,
476 Number tau,
477 Number tau_max) const
478 {
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."));
483
484#ifdef DEBUG_OUTPUT
485 std::cout << "HyperbolicModule<HyperbolicDescription, dim, "
486 "Number>::forward_euler_step()"
487 << std::endl;
488#endif
489
490 using MemorySpace = selected_memory_space_t;
491
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(),
498 move_to_memory_space<MemorySpace>(new_state_vector, StateVectorPart::state);
499
500 auto &[old_U, old_Q, old_U_dynamic, old_Q_dynamic] = old_state_vector;
501 auto &new_U = new_state_vector.state;
502
503 /*
504 * Taking a view<>() might imply implicit memory space transfers. Let's
505 * account for them in our computing timers.
506 */
508 ComputingTimer::timer("time step [X] _ - memory space transfers").start();
509
510 constexpr bool equilibrated_states =
512
513 /* Index ranges for the iteration over the sparsity pattern : */
514
515 const unsigned int n_internal = offline_data_->n_locally_internal();
516 const unsigned int n_owned = offline_data_->n_locally_owned();
517
518 /* Sparsity pattern, matrices, boundary information: */
519
520 const auto sparsity_simd_view =
521 offline_data_->sparsity_pattern_simd().template view<MemorySpace>();
522
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>();
530
531 const auto cij_matrix_view =
532 offline_data_->cij_matrix().template view<MemorySpace>();
533
534 /*
535 * The mass_matrix_inverse and incidence_matrix objects are only
536 * initialized (and accessed) for a discontinuous ansatz:
537 */
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>()
545 : MatrixReadView{};
546 const auto incidence_matrix_view =
547 have_discontinuous_ansatz
548 ? offline_data_->incidence_matrix().template view<MemorySpace>()
549 : MatrixReadView{};
550
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();
555
556 const Number measure_of_omega_inverse =
557 Number(1.) / offline_data_->measure_of_omega();
558
559 /* Temporary matrices: */
560
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>();
566
567 /* Vectors: */
568
569 const auto initial_precomputed_view =
570 initial_precomputed_
571 .template view<n_initial_precomputed_values, MemorySpace>();
572
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>();
577
578 /*
579 * FIXME GPU: std::array::operator[] is not device capable.
580 * Use a Kokkos::Array for wrapping for the time being:
581 */
582
583 using HyperbolicVectorView = std::remove_const_t<decltype(old_U_view)>;
584 using PrecomputedVectorView =
585 std::remove_const_t<decltype(old_precomputed_view)>;
586
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];
597 }
598
599 const auto new_U_view =
600 new_U.template view<problem_dimension, MemorySpace>();
601
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>();
605
607 ComputingTimer::timer("time step [X] _ - memory space transfers").stop();
608
609 /*
610 * Create a local copy of cfl_ so that we do not capture "this" in the
611 * compute kernels
612 */
613 const Number cfl = this->cfl_;
614
615 const auto hyperbolic_system_views =
616 make_select_view<dim, Number, MemorySpace>(*hyperbolic_system_);
617
618 const auto indicator_views =
619 make_select_view<dim, Number, MemorySpace>(indicator_);
620
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();
625
626 const auto wave_speed_estimator_views =
627 make_select_view<dim, Number, MemorySpace>(wave_speed_estimator_);
628
629 /*
630 * Lambdas for creating the computing timer and loop strings:
631 */
632
633 int step_no = 1;
634
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;
639 };
640
641 const auto loop_name = [&step_no]() {
642 return "hyperbolic_kernel_" + Utilities::int_to_string(step_no, 2);
643 };
644
645 /* A flag signalling that a restart is necessary. */
646 Mirrored<int> restart_needed("hyperbolic_module_restart_needed",
648 *restart_needed.view() = 0;
649
650 /*
651 * -------------------------------------------------------------------------
652 * Step 2: Compute off-diagonal d_ij, and alpha_i
653 *
654 * The computation of the d_ij is quite costly. So we do a trick to
655 * save a bit of computational resources. Instead of computing all d_ij
656 * entries for a row of a given local index i, we only compute d_ij for
657 * which j > i,
658 *
659 * llllrr
660 * l .xxxxx
661 * l ..xxxx
662 * l ...xxx
663 * l ....xx
664 * r ......
665 * r ......
666 *
667 * and symmetrize in Step 2.
668 *
669 * MM: We could save a bit more computational resources by only
670 * computing entries for which *IN A GLOBAL* enumeration j > i. But
671 * the index translation, subsequent symmetrization, and exchange
672 * sounds a bit too expensive...
673 * -------------------------------------------------------------------------
674 */
675 {
676 ComputingTimer::Scope scope(scoped_name("compute d_ij, and alpha_i"));
677
678 const auto body = [=](auto sentinel, unsigned int i) {
679 using T = decltype(sentinel);
680
681 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
682
683 const auto wave_speed_estimator_view =
684 wave_speed_estimator_views.template view<T>();
685
686 auto indicator_view = indicator_views.template view<T>();
687
688 /* Skip constrained degrees of freedom: */
689 const unsigned int row_length = sparsity_simd_view.row_length(i);
690 if (row_length == 1)
691 return;
692
693 const auto view = hyperbolic_system_views.template view<T>();
694
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);
698
699 indicator_view.reset(old_precomputed_view, i, U_i, flux_i);
700
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) {
704
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);
708
709 const auto c_ij = cij_matrix_view.template read_tensor<T>(i, col_idx);
710
711 indicator_view.accumulate(
712 old_precomputed_view, js, U_j, flux_j, c_ij);
713
714 /* Skip diagonal. */
715 if (col_idx == 0)
716 continue;
717
718 /* Only iterate over the upper triangular portion of d_ij */
719 if (all_below_diagonal<T>(i, js))
720 continue;
721
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;
727
728 dij_matrix_view.write_entry(d_ij, i, col_idx, true);
729 }
730
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);
734 };
735
736 loop<MemorySpace, Number>(loop_name(), body, 0, n_internal, n_owned);
737
738 alpha_view.update_ghost_values();
739 }
740
741 /*
742 * -------------------------------------------------------------------------
743 * Step 3: Compute diagonal of d_ij, and maximal time-step size.
744 * -------------------------------------------------------------------------
745 */
746
747 {
748 ComputingTimer::Scope scope(
749 scoped_name("compute bdry d_ij, diag d_ii, and tau_max"));
750
751 /*
752 * Complete d_ij at boundary:
753 *
754 * Here, for continuous finite elements the assumption c_ij = -c_ji
755 * no longer holds true. This implies that d_ij != d_ji. We thus need
756 * to compute the lower-triangular part of d_ij, where i and j are
757 * boundary degrees of freedom as well.
758 */
759
760 /*
761 * Note: we need this dance of iterating over an integer and then
762 * accessing the element to make Apple's OpenMP implementation
763 * happy.
764 */
765 const auto body_boundary = [=](auto, const unsigned int k) {
766 const auto &[i, col_idx, j] = coupling_boundary_pairs[k];
767
768 const auto wave_speed_estimator_view =
769 wave_speed_estimator_views.template view<Number>();
770
771 /*
772 * Only work on index pairs "i < j" that point to the upper
773 * triangular portion of the d_ij matrix. For all of these index
774 * pairs we compute the corresponding d_ji entry and fix up the
775 * d_ij entry (from step 2) by taking the maximum. Note that we
776 * actually do not store anything in the d_ji entry itself because
777 * we symmetrize the matrix later on anyway.
778 */
779 if (j < i)
780 return;
781
782 const auto view = hyperbolic_system_views.template view<Number>();
783
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);
790
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;
795
796 const auto d_ij = dij_matrix_view.read_entry(i, col_idx);
797
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;
801
802 dij_matrix_view.write_entry(std::max(d_ij, d_ji), i, col_idx);
803 };
804
805 loop<MemorySpace, Number>(loop_name() + 'a',
806 body_boundary,
807 0,
808 /*no vectorization*/ 0,
809 n_coupling_boundary_pairs);
810
811 /* Symmetrize d_ij and compute the maximal time-step size: */
812 const auto body = [=](auto, unsigned int i) -> Number {
813
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>();
818#endif
819
820 /* Skip constrained degrees of freedom: */
821 const unsigned int row_length = sparsity_simd_view.row_length(i);
822 if (row_length == 1)
823 return std::numeric_limits<Number>::max();
824
825 Number d_sum = Number(0.);
826
827 /* skip diagonal: */
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);
832
833 // fill lower triangular part of dij_matrix missing from step 1
834 if (j < i) {
835 const auto d_ji = dij_matrix_view.read_transposed_entry(i, col_idx);
836
837#ifdef DEBUG_SYMMETRY_CHECK
838 /* Verify that d_ji == std::max(d_ij, d_ji): */
839
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);
846
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;
851
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;
855
856 Assert(d_ij <= d_ji + 1.0e-12,
857 dealii::ExcMessage("d_ij not symmetrized correctly on "
858 "boundary degrees of freedom."));
859#endif
860
861 dij_matrix_view.write_entry(d_ji, i, col_idx);
862 }
863
864 d_sum -= dij_matrix_view.read_entry(i, col_idx);
865 }
866
867 /*
868 * Make sure that we do not accidentally divide by zero. (Yes, this
869 * can happen for some (admittedly, rather esoteric) scalar
870 * conservation equations...).
871 */
872 d_sum =
873 std::min(d_sum, Number(-1.e6) * std::numeric_limits<Number>::min());
874
875 /* write diagonal element */
876 dij_matrix_view.write_entry(d_sum, i, 0);
877
878 const Number mass = lumped_mass_matrix_view.read_entry(i);
879 return cfl * mass / (Number(-2.) * d_sum);
880 };
881
882 reduction_loop<MemorySpace>(
883 loop_name() + 'b', body, Kokkos::Min<Number>(tau_max), 0, n_owned);
884 }
885
886 {
887 ComputingTimer::Scope scope("time step [X] _ - synchronization barriers");
888
889 /*
890 * MPI Barrier: Synchronize the maximal time-step size. This has to
891 * happen either over the global, or the local subrange communicator:
892 */
893 tau_max = Utilities::MPI::min(
894 tau_max, mpi_ensemble_.synchronization_communicator());
895
896 AssertThrow(
897 !std::isnan(tau_max) && !std::isinf(tau_max) && tau_max > 0.,
898 ExcMessage(
899 "I'm sorry, Dave. I'm afraid I can't do that.\nWe crashed."));
900
901 tau = (tau == Number(0.) ? tau_max : tau);
902
903#ifdef DEBUG_OUTPUT
904 std::cout << " computed tau_max = " << tau_max << " (CFL = " << cfl
905 << ")" << std::endl;
906 std::cout << " step with tau = " << tau << std::endl;
907#endif
908
909 /* We need to signal a restart if the enforced tau is too wacky: */
910 *restart_needed.view() =
911 (tau > this->acceptable_tau_max_ratio_ * tau_max);
912
913 /* Don't bother with computing the update step, signal a restart: */
914 if (*restart_needed.view() && this->id_violation_strategy_ ==
916 this->n_restarts_++;
917 /* Suggest a restart with tau_max: */
918#ifdef DEBUG_OUTPUT
919 std::cout << " signalling restart (suggested_tau_max = "
920 << tau_max << ")" << std::endl;
921#endif
922
923 throw Restart{tau_max};
924 }
925 }
926
927 /* moves the "boolean" to device memory space: */
928 int *restart_needed_view = restart_needed.view<MemorySpace>();
929
930#ifdef DEBUG
931 /* Exchange d_ij so that we can check for symmetry: */
932 dij_matrix_view.update_ghost_rows();
933#endif
934
935 /*
936 * -------------------------------------------------------------------------
937 * Step 4: Low-order update, also compute limiter bounds, R_i
938 * -------------------------------------------------------------------------
939 */
940
941 {
942 ComputingTimer::Scope scope(
943 scoped_name("l.-o. update, compute bounds, r_i, and p_ij"));
944
945 const Number weight =
946 -std::accumulate(stage_weights.begin(), stage_weights.end(), -1.);
947
948 const auto body = [=](auto sentinel,
949 auto have_discontinuous_ansatz,
950 const unsigned int i) {
951 using T = decltype(sentinel);
952
953 const auto view = hyperbolic_system_views.template view<T>();
954
955 using View = decltype(view);
956 using flux_contribution_type = typename View::flux_contribution_type;
957 using state_type = typename View::state_type;
958
959 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
960
961 auto limiter_view = limiter_views.template view<T>();
962
963 /* Skip constrained degrees of freedom: */
964 const unsigned int row_length = sparsity_simd_view.row_length(i);
965 if (row_length == 1)
966 return;
967
968 const auto U_i = old_U_view.template read_tensor<T>(i);
969 auto U_i_new = U_i;
970
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);
973 const auto m_i_inv =
974 lumped_mass_matrix_inverse_view.template read_entry<T>(i);
975
976 const auto flux_i = view.flux_contribution(
977 old_precomputed_view, initial_precomputed_view, i, U_i);
978
979 Kokkos::Array<flux_contribution_type, stages> flux_iHs;
980 [[maybe_unused]] state_type S_iH;
981
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);
986
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);
990 }
991 }
992
993 [[maybe_unused]] state_type S_i;
994 state_type F_iH;
995
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 * /* m_i_inv * m_i */ S_i;
1000 F_iH += m_i * S_iH;
1001 }
1002
1003 limiter_view.reset(old_precomputed_view, i, U_i, flux_i);
1004
1005 [[maybe_unused]] state_type affine_shift;
1006
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) {
1011
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);
1015
1016 const auto d_ij =
1017 dij_matrix_view.template read_entry<T>(i, col_idx);
1018 const auto c_ij =
1019 cij_matrix_view.template read_tensor<T>(i, col_idx);
1020
1021 const auto B_ij = view.affine_shift(flux_i, flux_j, c_ij, d_ij);
1022 affine_shift += B_ij;
1023 }
1024
1025 affine_shift *= tau * m_i_inv;
1026 }
1027
1028 if constexpr (View::have_source_terms) {
1029 affine_shift += tau * /* m_i_inv * m_i */ S_i;
1030 }
1031
1032 js = sparsity_simd_view.columns(i);
1033 for (unsigned int col_idx = 0; col_idx < row_length;
1034 ++col_idx, js += stride_size) {
1035
1036 const auto U_j = old_U_view.template read_tensor<T>(js);
1037
1038 const auto alpha_j = alpha_view.template read_entry<T>(js);
1039
1040 const auto d_ij = dij_matrix_view.template read_entry<T>(i, col_idx);
1041 auto factor = (alpha_i + alpha_j) * Number(.5);
1042
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);
1047 }
1048
1049 const auto d_ijH = d_ij * factor;
1050
1051#ifdef DEBUG_SYMMETRY_CHECK
1052 /*
1053 * Verify that all local chunks of the d_ij matrix have been
1054 * computed consistently over all MPI ranks. For that we import
1055 * all ghost rows from neighboring MPI ranks and simply check
1056 * that the (local) values of d_ij and d_ji match.
1057 */
1058 const auto d_ji =
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),
1061 dealii::ExcMessage(
1062 "d_ij not symmetrized correctly over MPI ranks"));
1063#endif
1064
1065 const auto c_ij = cij_matrix_view.template read_tensor<T>(i, col_idx);
1066 constexpr auto eps = std::numeric_limits<Number>::epsilon();
1067
1068 const auto scale =
1069 ryujin::compare_and_apply_mask<dealii::SIMDComparison::less_than>(
1070 std::abs(d_ij), T(eps * eps), T(0.), T(1.) / d_ij);
1071
1072 const auto scaled_c_ij = c_ij * scale;
1073
1074 const auto flux_j = view.flux_contribution(
1075 old_precomputed_view, initial_precomputed_view, js, U_j);
1076
1077 const auto m_ij = mass_matrix_view.template read_entry<T>(i, col_idx);
1078
1079 /*
1080 * Compute low-order flux and limiter bounds:
1081 */
1082
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;
1086
1087 if constexpr (equilibrated_states) {
1088 const auto &[U_star_ij, U_star_ji] =
1089 view.equilibrated_states(flux_i, flux_j);
1090
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);
1094 } else {
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);
1098 }
1099
1100 limiter_view.accumulate(
1101 old_precomputed_view, js, U_j, flux_j, scaled_c_ij, affine_shift);
1102
1103 if constexpr (View::have_source_terms) {
1104 F_iH -= m_ij * S_iH;
1105 P_ij -= m_ij * /*sic!*/ S_i;
1106 }
1107
1108 /*
1109 * Compute high-order fluxes and source terms:
1110 */
1111
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;
1117 } else {
1118 F_iH += weight * flux_ij;
1119 P_ij += weight * flux_ij;
1120 }
1121
1122 if constexpr (View::have_source_terms) {
1123 const auto S_j =
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;
1127 }
1128
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);
1133
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;
1139 } else {
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;
1144 }
1145
1146 if constexpr (View::have_source_terms) {
1147 const auto S_js =
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;
1151 }
1152 }
1153
1154 pij_matrix_view.template write_tensor<T>(P_ij, i, col_idx, true);
1155 }
1156
1157#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
1158 if (!view.is_admissible(U_i_new)) {
1159 atomic_store(restart_needed_view, 1);
1160 }
1161#endif
1162
1163 new_U_view.template write_tensor<T>(U_i_new, i);
1164 r_view.template write_tensor<T>(F_iH, i);
1165
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);
1169 };
1170
1171 /*
1172 * Chain through a compile time integral constant std::true_type for
1173 * a discontinuous ansatz and std::false_type otherwise. We use the
1174 * (constexpr) integral constant later on to avoid branching when
1175 * computing d_ijH.
1176 */
1177 if (have_discontinuous_ansatz) {
1178 loop<MemorySpace, Number>(
1179 loop_name(), body, 0, n_internal, n_owned, std::true_type{});
1180 } else {
1181 loop<MemorySpace, Number>(
1182 loop_name(), body, 0, n_internal, n_owned, std::false_type{});
1183 }
1184
1185 r_view.update_ghost_values();
1186 if (have_discontinuous_ansatz) {
1187 /*
1188 * In case we extend bounds over the stencil, we have to ensure
1189 * that ghost ranges are properly communicated over all MPI
1190 * ranks.
1191 */
1192 bounds_view.update_ghost_values();
1193 }
1194 }
1195
1196 /*
1197 * -------------------------------------------------------------------------
1198 * Step 5: Compute second part of P_ij, and l_ij (first round):
1199 * -------------------------------------------------------------------------
1200 */
1201
1202 if (n_limiter_iterations != 0) {
1203 ComputingTimer::Scope scope(scoped_name("compute p_ij, and l_ij"));
1204
1205 const auto body = [=](auto sentinel,
1206 auto have_discontinuous_ansatz,
1207 const unsigned int i) {
1208 using T = decltype(sentinel);
1209
1210 const unsigned int stride_size = sparsity_simd_view.stride_of_row(i);
1211
1212 auto limiter_view = limiter_views.template view<T>();
1213
1214 /* Skip constrained degrees of freedom: */
1215 const unsigned int row_length = sparsity_simd_view.row_length(i);
1216 if (row_length == 1)
1217 return;
1218
1219 auto local_bounds =
1220 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(i);
1221
1222 /*
1223 * In case of a discontinuous finite element ansatz we need to
1224 * extend bounds over the stencil. We do this by looping over the
1225 * stencil once and taking the minimum/maximum:
1226 */
1227 if constexpr (have_discontinuous_ansatz) {
1228 /* Skip diagonal. */
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(
1233 local_bounds,
1234 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(
1235 js));
1236 }
1237 bounds_view.template write_tensor<T>(local_bounds, i);
1238 }
1239
1240 [[maybe_unused]] T m_i;
1241 if constexpr (have_discontinuous_ansatz)
1242 m_i = lumped_mass_matrix_view.template read_entry<T>(i);
1243
1244 const auto m_i_inv =
1245 lumped_mass_matrix_inverse_view.template read_entry<T>(i);
1246
1247 const auto U_i_new = new_U_view.template read_tensor<T>(i);
1248
1249 const auto F_iH = r_view.template read_tensor<T>(i);
1250
1251 const auto lambda_inv = Number(row_length - 1);
1252 const auto factor = tau * m_i_inv * lambda_inv;
1253
1254 /*
1255 * Note: We "software-pipeline" the read access into the p_ij
1256 * matrix by one column index. This ensures that the P_ij entry of
1257 * the next column index is loaded *before* we store the updated
1258 * entry for the current column index. Otherwise we run into
1259 * aliasing issues that force the (cuda) compiler to serialize the
1260 * final store of P_ij and the read of the next entry.
1261 */
1262 auto P_ij = pij_matrix_view.template read_tensor<T>(i, 1);
1263
1264 /* Skip diagonal. */
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) {
1268
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);
1272
1273 /*
1274 * Mass matrix correction:
1275 */
1276
1277 const auto kronecker_ij = col_idx == 0 ? T(1.) : T(0.);
1278
1279 if constexpr (have_discontinuous_ansatz) {
1280 /* Use full consistent mass matrix inverse: */
1281
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;
1287
1288 P_ij += b_ij * F_jH - b_ji * F_iH;
1289
1290 } else {
1291 /* Use Neumann series expansion: */
1292
1293 const auto m_j_inv =
1294 lumped_mass_matrix_inverse_view.template read_entry<T>(js);
1295 const auto m_ij =
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;
1299
1300 P_ij += b_ij * F_jH - b_ji * F_iH;
1301 }
1302
1303 P_ij *= factor;
1304 pij_matrix_view.template write_tensor<T>(P_ij, i, col_idx);
1305
1306 /*
1307 * Compute limiter coefficients:
1308 */
1309
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);
1313
1314 /*
1315 * If the success is set to false then the low-order update
1316 * resulted in a state outside of the limiter bounds. This can
1317 * happen if we compute with an aggressive CFL number. We
1318 * signal this condition by setting the restart_needed flag and
1319 * defer further action to the chosen IDViolationStrategy and the
1320 * policy set in the TimeIntegrator.
1321 */
1322 if (!success)
1323 atomic_store(restart_needed_view, 1);
1324
1325 P_ij = P_ij_next;
1326 }
1327 };
1328
1329 /*
1330 * Chain through a compile time integral constant std::true_type for
1331 * a discontinuous ansatz and std::false_type otherwise. We use the
1332 * (constexpr) integral constant later on to avoid branching when
1333 * computing d_ijH.
1334 */
1335 if (have_discontinuous_ansatz) {
1336 loop<MemorySpace, Number>(
1337 loop_name(), body, 0, n_internal, n_owned, std::true_type{});
1338 } else {
1339 loop<MemorySpace, Number>(
1340 loop_name(), body, 0, n_internal, n_owned, std::false_type{});
1341 }
1342
1343 lij_matrix_view.update_ghost_rows();
1344 }
1345
1346 /*
1347 * -------------------------------------------------------------------------
1348 * Step 6, 7: Perform high-order update:
1349 *
1350 * Symmetrize l_ij
1351 * High-order update: += l_ij * lambda * P_ij
1352 * Compute next l_ij
1353 * -------------------------------------------------------------------------
1354 */
1355
1356 for (unsigned int pass = 0; pass < n_limiter_iterations; ++pass) {
1357 bool last_round = (pass + 1 == n_limiter_iterations);
1358
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));
1362
1363 const auto lij_view = (n_limiter_iterations == 2 && last_round)
1364 ? lij_matrix_next_view
1365 : lij_matrix_view;
1366
1367 const auto body = [=](auto sentinel, const unsigned int i) {
1368 using T = decltype(sentinel);
1369
1370 auto limiter_view = limiter_views.template view<T>();
1371
1372 /* Skip constrained degrees of freedom: */
1373 const unsigned int row_length = sparsity_simd_view.row_length(i);
1374 if (row_length == 1)
1375 return;
1376
1377 auto U_i_new = new_U_view.template read_tensor<T>(i);
1378
1379 const Number lambda = Number(1.) / Number(row_length - 1);
1380
1381 /* Skip diagonal. */
1382 for (unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1383
1384 const auto l_ij =
1385 std::min(lij_view.template read_entry<T>(i, col_idx),
1386 lij_view.template read_transposed_entry<T>(i, col_idx));
1387
1388 const auto p_ij = pij_matrix_view.template read_tensor<T>(i, col_idx);
1389
1390 U_i_new += l_ij * lambda * p_ij;
1391 }
1392
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);
1397 }
1398#endif
1399
1400 new_U_view.template write_tensor<T>(U_i_new, i);
1401
1402 /* Skip computating l_ij and updating p_ij in the last round */
1403 if (last_round)
1404 return;
1405
1406 const auto local_bounds =
1407 bounds_view.template read_tensor<T, std::array<T, n_bounds>>(i);
1408 /* Skip diagonal. */
1409 for (unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1410
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));
1414
1415 const auto new_p_ij =
1416 (T(1.) - old_l_ij) *
1417 pij_matrix_view.template read_tensor<T>(i, col_idx);
1418
1419 const auto &[new_l_ij, success] =
1420 limiter_view.limit(local_bounds, U_i_new, new_p_ij);
1421
1422 /*
1423 * This is the second pass of the limiter. Under rare
1424 * circumstances the previous high-order update might be
1425 * slightly out of bounds due to roundoff errors. This happens
1426 * for example in flat regions or in stagnation points at a
1427 * (slip boundary) point. The limiter should ensure that we do
1428 * not further manipulate the state in this case. We thus only
1429 * signal a restart condition if the `EXPENSIVE_BOUNDS_CHECK` debug
1430 * macro is defined.
1431 */
1432#ifdef DEBUG_EXPENSIVE_BOUNDS_CHECK
1433 if (!success)
1434 atomic_store(restart_needed_view, 1);
1435#endif
1436
1437 /*
1438 * Shortcut: We omit updating the p_ij and q_ij matrices and
1439 * simply write (1 - l_ij^(1)) * l_ij^(2) into the l_ij matrix.
1440 *
1441 * This approach only works for at most two limiting steps.
1442 */
1443 const auto entry = (T(1.) - old_l_ij) * new_l_ij;
1444 lij_matrix_next_view.write_entry(entry, i, col_idx, true);
1445 }
1446 };
1447
1448 loop<MemorySpace, Number>(loop_name(), body, 0, n_internal, n_owned);
1449
1450 if (!last_round) {
1451 lij_matrix_next_view.update_ghost_rows();
1452 }
1453 } /* limiter_iter_ */
1454
1455 /*
1456 * Pass through the dynamic state
1457 */
1458 new_state_vector.dynamic_state = old_U_dynamic;
1459
1460 /*
1461 * Do we have to restart?
1462 */
1463
1464 {
1465 ComputingTimer::Scope scope("time step [X] _ - synchronization barriers");
1466
1467 /*
1468 * Synchronize whether we have to restart the time step. Even though
1469 * the restart condition itself only affects the local ensemble we
1470 * nevertheless need to synchronize the flag in case we perform
1471 * synchronized global time steps. (Otherwise different ensembles
1472 * might end up with a different time step.)
1473 *
1474 * The host view reads the flag back from the selected memory space.
1475 */
1476 int &restart_flag = *restart_needed.view();
1477 restart_flag = Utilities::MPI::logical_or(
1478 restart_flag != 0, mpi_ensemble_.synchronization_communicator());
1479 }
1480
1481 if (*restart_needed.view()) {
1482 switch (this->id_violation_strategy_) {
1484 this->n_warnings_++;
1485#ifdef DEBUG_OUTPUT
1486 std::cout << " raised warning, CFL/IDP violation encountered "
1487 << std::endl;
1488#endif
1489 break;
1491 this->n_restarts_++;
1492 /* Suggest a restart with tau_max: */
1493#ifdef DEBUG_OUTPUT
1494 std::cout << " signalling restart (suggested_tau_max = "
1495 << tau_max << ")" << std::endl;
1496#endif
1497 throw Restart{tau_max};
1498 }
1499 }
1500
1501 /* Poison all values that are left invalid after the update step: */
1502 debug_poison_invalid_values(new_state_vector, *offline_data_);
1503
1504 /* Return the time step size tau: */
1505 return tau;
1506 }
1507
1508} // namespace ryujin
static dealii::Timer & timer(const std::string &section)
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
Definition gpu.h:65
constexpr bool have_separate_memory_spaces
Definition gpu.h:29
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