ryujin 2.1.1 revision 0348cbb53a3e4b1da2a4c037e81f88f2d21ce219
selected_components_extractor.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2024 by the ryujin authors
4//
5
6#pragma once
7
8#include "state_vector.h"
9
10namespace ryujin
11{
12 template <typename Description, int dim, typename Number>
15
16 using View =
17 typename Description::template HyperbolicSystemView<dim, Number>;
18
20 using InitialPrecomputedVector = typename View::InitialPrecomputedVector;
22
24
25 static void check(const std::vector<std::string> &selected)
26 {
27 const auto search = [&](const auto entry, const auto &names) {
28 const auto pos = std::find(std::begin(names), std::end(names), entry);
29 return pos != std::end(names);
30 };
31
32 for (const auto &entry : selected) {
33 const auto found = search(entry, View::component_names) ||
34 search(entry, View::primitive_component_names) ||
35 search(entry, View::precomputed_names) ||
36 search(entry, View::initial_precomputed_names) ||
37 (entry == "alpha");
38 AssertThrow(
39 found,
40 dealii::ExcMessage(
41 "Invalid component name: \"" + entry +
42 "\" is not a valid conserved/primitive/precomputed/initial "
43 "component name."));
44 }
45 }
46
47 static std::vector<ScalarVector>
48 extract(const HyperbolicSystem &hyperbolic_system,
49 const StateVector &state_vector,
50 const InitialPrecomputedVector &initial_precomputed,
51 const ScalarVector &alpha,
52 const std::vector<std::string> &selected)
53 {
54 /*
55 * Match the selected_components strings against conserved,
56 * primitive, and initial component names and record an index pair
57 * matching return vector position and component index:
58 */
59
60 std::vector<std::tuple<std::size_t, std::size_t>> conserved_indices;
61 std::vector<std::tuple<std::size_t, std::size_t>> primitive_indices;
62 std::vector<std::tuple<std::size_t, std::size_t>> precomputed_indices;
63 std::vector<std::tuple<std::size_t, std::size_t>> initial_indices;
64 std::vector<std::size_t> alpha_indices;
65
66 for (std::size_t i = 0; const auto &entry : selected) {
67 const auto search = [&](const auto &names, auto &indices) {
68 const auto pos = std::find(std::begin(names), std::end(names), entry);
69 if (pos != std::end(names)) {
70 const auto index = std::distance(std::begin(names), pos);
71 indices.push_back({i++, index});
72 return true;
73 }
74 return false;
75 };
76
77 if (search(View::component_names, conserved_indices))
78 ;
79 else if (search(View::primitive_component_names, primitive_indices))
80 ;
81 else if (search(View::precomputed_names, precomputed_indices))
82 ;
83 else if (search(View::initial_precomputed_names, initial_indices))
84 ;
85 else if (entry == "alpha")
86 alpha_indices.push_back(i++);
87 else
88 AssertThrow(false, dealii::ExcInternalError());
89 }
90
91 std::vector<ScalarVector> extracted_components(selected.size());
92 const auto &scalar_partitioner = alpha.get_partitioner();
93 for (auto &it : extracted_components)
94 it.reinit(scalar_partitioner);
95
96 for (const auto &[i, k] : conserved_indices) {
97 const auto &U = std::get<0>(state_vector);
98 U.extract_component(extracted_components[i], k);
99 }
100
101 if (!primitive_indices.empty()) {
102 const auto &U = std::get<0>(state_vector);
103 const unsigned int n_owned = scalar_partitioner->locally_owned_size();
104 const auto view = hyperbolic_system.template view<dim, Number>();
105 for (unsigned int i = 0; i < n_owned; ++i) {
106 const auto U_i = U.get_tensor(i);
107 const auto PU_i = view.to_primitive_state(U_i);
108 for (const auto &[j, k] : primitive_indices)
109 extracted_components[j].local_element(i) = PU_i[k];
110 }
111 }
112
113 for (const auto &[i, k] : precomputed_indices) {
114 const auto &prec = std::get<1>(state_vector);
115 prec.extract_component(extracted_components[i], k);
116 }
117
118 for (const auto &[i, k] : initial_indices) {
119 initial_precomputed.extract_component(extracted_components[i], k);
120 }
121
122 for (const auto &i : alpha_indices)
123 extracted_components[i] = alpha;
124
125 return extracted_components;
126 }
127 };
128} // namespace ryujin
dealii::LinearAlgebra::distributed::Vector< Number > ScalarVector
Definition: state_vector.h:31
std::tuple< MultiComponentVector< Number, problem_dim >, MultiComponentVector< Number, prec_dim >, BlockVector< Number > > StateVector
Definition: state_vector.h:51
Euler::HyperbolicSystem HyperbolicSystem
Definition: description.h:32
typename Description::template HyperbolicSystemView< dim, Number > View
typename Description::HyperbolicSystem HyperbolicSystem
Vectors::ScalarVector< Number > ScalarVector
typename View::InitialPrecomputedVector InitialPrecomputedVector
static std::vector< ScalarVector > extract(const HyperbolicSystem &hyperbolic_system, const StateVector &state_vector, const InitialPrecomputedVector &initial_precomputed, const ScalarVector &alpha, const std::vector< std::string > &selected)
static void check(const std::vector< std::string > &selected)