ryujin 2.1.1 revision ee5cbcbf2346c1299c942d0e1f13b46449973c18
Loading...
Searching...
No Matches
initial_values.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
8#include "initial_values.h"
9
10#include <deal.II/numerics/vector_tools.h>
11#include <deal.II/numerics/vector_tools.templates.h>
12
13#include <random>
14
15namespace ryujin
16{
17 using namespace dealii;
18
19 template <typename Description, int dim, typename Number>
21 const MPIEnsemble &mpi_ensemble,
22 const OfflineData<dim, Number> &offline_data,
23 const HyperbolicSystem &hyperbolic_system,
24 const ParabolicSystem &parabolic_system,
25 const std::string &subsection)
26 : ParameterAcceptor(subsection)
27 , mpi_ensemble_(mpi_ensemble)
28 , offline_data_(&offline_data)
29 , hyperbolic_system_(&hyperbolic_system)
30 , parabolic_system_(&parabolic_system)
31 {
32 ParameterAcceptor::parse_parameters_call_back.connect(std::bind(
34 this));
35
36 configuration_ = "uniform";
37 add_parameter("configuration",
38 configuration_,
39 "The initial state configuration. Valid names are given by "
40 "any of the subsections defined below.");
41
42 initial_direction_[0] = 1.;
43 add_parameter(
44 "direction",
45 initial_direction_,
46 "Initial direction of initial configuration (Galilei transform)");
47
48 initial_position_[0] = 1.;
49 add_parameter(
50 "position",
51 initial_position_,
52 "Initial position of initial configuration (Galilei transform)");
53
54 perturbation_ = 0.;
55 add_parameter("perturbation",
56 perturbation_,
57 "Add a random perturbation of the specified magnitude to the "
58 "initial state.");
59
60 /*
61 * And finally populate the initial state list with all initial state
62 * configurations defined in the InitialStateLibrary namespace:
63 */
65 initial_state_list_,
66 *hyperbolic_system_,
67 *parabolic_system_,
68 subsection);
69 }
70
71 namespace
72 {
76 template <int dim>
77 inline DEAL_II_ALWAYS_INLINE dealii::Point<dim>
78 affine_transform(const dealii::Tensor<1, dim> initial_direction,
79 const dealii::Point<dim> initial_position,
80 const dealii::Point<dim> x)
81 {
82 auto direction = x - initial_position;
83
84 /* Roll third component of initial_direction onto xy-plane: */
85 if constexpr (dim == 3) {
86 auto n_x = initial_direction[0];
87 auto n_z = initial_direction[2];
88 const auto norm = std::sqrt(n_x * n_x + n_z * n_z);
89 n_x /= norm;
90 n_z /= norm;
91 auto new_direction = direction;
92 if (norm > 1.0e-14) {
93 new_direction[0] = n_x * direction[0] + n_z * direction[2];
94 new_direction[2] = -n_z * direction[0] + n_x * direction[2];
95 }
96 direction = new_direction;
97 }
98
99 /* Roll second component of initial_direction onto x-axis: */
100 if constexpr (dim >= 2) {
101 auto n_x = initial_direction[0];
102 auto n_y = initial_direction[1];
103 const auto norm = std::sqrt(n_x * n_x + n_y * n_y);
104 n_x /= norm;
105 n_y /= norm;
106 auto new_direction = direction;
107 if (norm > 1.0e-14) {
108 new_direction[0] = n_x * direction[0] + n_y * direction[1];
109 new_direction[1] = -n_y * direction[0] + n_x * direction[1];
110 }
111 direction = new_direction;
112 }
113
114 if constexpr (dim == 1) {
115 auto n = initial_direction[0];
116 const auto norm = std::abs(n);
117 n /= norm;
118 auto new_direction = direction;
119 if (norm > 1.0e-14)
120 new_direction[0] = n * direction[0];
121 direction = new_direction;
122 }
123
124 return Point<dim>() + direction;
125 }
126
127
131 template <int dim, typename Number>
132 inline DEAL_II_ALWAYS_INLINE dealii::Tensor<1, dim, Number>
133 affine_transform_vector(const dealii::Tensor<1, dim> initial_direction,
134 dealii::Tensor<1, dim, Number> direction)
135 {
136 if constexpr (dim == 1) {
137 auto n = initial_direction[0];
138 const auto norm = std::abs(n);
139 n /= norm;
140 auto new_direction = direction;
141 if (norm > 1.0e-14)
142 new_direction[0] = n * direction[0];
143 direction = new_direction;
144 }
145
146 if constexpr (dim >= 2) {
147 auto n_x = initial_direction[0];
148 auto n_y = initial_direction[1];
149 const auto norm = std::sqrt(n_x * n_x + n_y * n_y);
150 n_x /= norm;
151 n_y /= norm;
152 auto new_direction = direction;
153 if (norm > 1.0e-14) {
154 new_direction[0] = n_x * direction[0] - n_y * direction[1];
155 new_direction[1] = n_y * direction[0] + n_x * direction[1];
156 }
157 direction = new_direction;
158 }
159
160 if constexpr (dim == 3) {
161 auto n_x = initial_direction[0];
162 auto n_z = initial_direction[2];
163 const auto norm = std::sqrt(n_x * n_x + n_z * n_z);
164 n_x /= norm;
165 n_z /= norm;
166 auto new_direction = direction;
167 if (norm > 1.0e-14) {
168 new_direction[0] = n_x * direction[0] - n_z * direction[2];
169 new_direction[2] = n_z * direction[0] + n_x * direction[2];
170 }
171 direction = new_direction;
172 }
173
174 return direction;
175 }
176 } /* namespace */
177
178
179 template <typename Description, int dim, typename Number>
181 {
182 /* First, let's normalize the direction: */
183
184 AssertThrow(initial_direction_.norm() != 0.,
185 ExcMessage("Initial direction is set to the zero vector."));
186 initial_direction_ /= initial_direction_.norm();
187
188 /* Populate std::function object: */
189
190 {
191 bool initialized = false;
192 for (auto &it : initial_state_list_)
193 if (it->name() == configuration_) {
194 initial_state_ = [this, &it](const dealii::Point<dim> &point,
195 Number t) {
196 const auto transformed_point =
197 affine_transform(initial_direction_, initial_position_, point);
198 auto state = it->compute(transformed_point, t);
199 const auto view = hyperbolic_system_->template view<dim, Number>();
200 state =
201 view.apply_galilei_transform(state, [&](const auto &momentum) {
202 return affine_transform_vector(initial_direction_, momentum);
203 });
204 return state;
205 };
206
207 initial_precomputed_ = [this, &it](const dealii::Point<dim> &point) {
208 const auto transformed_point =
209 affine_transform(initial_direction_, initial_position_, point);
210 return it->initial_precomputations(transformed_point);
211 };
212
213 initialized = true;
214 break;
215 }
216
217 AssertThrow(
218 initialized,
219 ExcMessage(
220 "Could not find an initial state description with name \"" +
221 configuration_ + "\""));
222 }
223
224 /* Add a random perturbation to the original function object: */
225
226 if (perturbation_ != 0.) {
227 initial_state_ = [old_state = this->initial_state_,
228 perturbation = this->perturbation_](
229 const dealii::Point<dim> &point, Number t) {
230 auto state = old_state(point, t);
231
232 if (t > 0.)
233 return state;
234
235 static std::default_random_engine generator =
236 std::default_random_engine(std::random_device()());
237 static std::uniform_real_distribution<Number> distribution(-1., 1.);
238 static auto draw = std::bind(distribution, generator);
239 for (unsigned int i = 0; i < problem_dimension; ++i)
240 state[i] *= (Number(1.) + perturbation * draw());
241
242 return state;
243 };
244 }
245 }
246
247
248 template <typename Description, int dim, typename Number>
250 Number t) const -> HyperbolicVector
251 {
252#ifdef DEBUG_OUTPUT
253 std::cout << "InitialValues<dim, Number>::"
254 << "interpolate_hyperbolic_vector(t = " << t << ")" << std::endl;
255#endif
256
257 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
258 const auto &vector_partitioner =
259 offline_data_->hyperbolic_vector_partitioner();
260
262 U.reinit_with_vector_partitioner(vector_partitioner);
263
264 using ScalarHostVector = Vectors::ScalarHostVector<Number>;
265 ScalarHostVector temp;
266 temp.reinit(scalar_partitioner);
267
268 // FIXME: it is not particularly efficient to call
269 // VectorTools::interpolate for every component separately. If this
270 // gets too slow, we should consider writing out to a temporary (block)
271 // vector and then inserting into the MultiComponentVector.
272 const auto U_view = U.view();
273 const auto callable = [&](const auto &p) { return initial_state(p, t); };
274 for (unsigned int d = 0; d < problem_dimension; ++d) {
275 VectorTools::interpolate(offline_data_->discretization().mapping(),
276 offline_data_->dof_handler(),
277 to_function<dim, Number>(callable, d),
278 temp);
279 U_view.insert_component(temp, d);
280 }
281
282 U_view.update_ghost_values();
283
284 return U;
285 }
286
287
288 template <typename Description, int dim, typename Number>
291 {
292#ifdef DEBUG_OUTPUT
293 std::cout << "InitialValues<dim, Number>::"
294 << "interpolate_initial_precomputed_vector()" << std::endl;
295#endif
296
297 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
298
299 InitialPrecomputedVector precomputed;
300 precomputed.reinit_with_scalar_partitioner(scalar_partitioner);
301
302 if constexpr (n_initial_precomputed_values == 0)
303 return precomputed;
304
305 using ScalarHostVector = Vectors::ScalarHostVector<Number>;
306 ScalarHostVector temp;
307 temp.reinit(scalar_partitioner);
308
309 // FIXME: it is not particularly efficient to call
310 // VectorTools::interpolate for every component separately. If this
311 // gets too slow, we should consider writing out to a temporary (block)
312 // vector and then inserting into the MultiComponentVector.
313 const auto precomputed_view = precomputed.view();
314 const auto callable = [&](const auto &p) { return initial_precomputed(p); };
315 for (unsigned int d = 0; d < n_initial_precomputed_values; ++d) {
316 VectorTools::interpolate(offline_data_->dof_handler(),
317 to_function<dim, Number>(callable, d),
318 temp);
319 precomputed_view.insert_component(temp, d);
320 }
321
322 precomputed_view.update_ghost_values();
323 return precomputed;
324 }
325
326} /* namespace ryujin */
InitialValues(const MPIEnsemble &mpi_ensemble, const OfflineData< dim, Number > &offline_data, const HyperbolicSystem &hyperbolic_system, const ParabolicSystem &parabolic_system, const std::string &subsection="/InitialValues")
InitialPrecomputedVector interpolate_initial_precomputed_vector() const
typename View::InitialPrecomputedVector InitialPrecomputedVector
typename Description::HyperbolicSystem HyperbolicSystem
typename Description::ParabolicSystem ParabolicSystem
HyperbolicVector interpolate_hyperbolic_vector(Number t=0) const
typename View::HyperbolicVector HyperbolicVector
static void populate_initial_state_list(initial_state_list_type &initial_state_list, const HyperbolicSystem &h, const ParabolicSystem &p, const std::string &s)
dealii::LinearAlgebra::distributed::Vector< Number > ScalarHostVector