Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
postprocessing_velocities.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/utilities.h>
5#include <core/vector.h>
6
8
9#include <deal.II/base/index_set.h>
10#include <deal.II/base/point.h>
11
12#include <deal.II/fe/fe.h>
13
14#include <deal.II/lac/vector_operation.h>
15
16#include <array>
17#include <fstream>
18#include <type_traits>
19#include <vector>
20
21template <int dim, typename VectorType, typename DofsType>
23 DoFHandler<dim> &dof_handler)
30{
31 // Initialize average velocity solution vector shared pointer
32 get_av = std::make_shared<VectorType>();
33}
34
35template <int dim, typename VectorType, typename DofsType>
36void
38 const VectorType &local_evaluation_point,
39 const Parameters::PostProcessing<dim> &post_processing,
40 const double &current_time,
41 const double &time_step)
42{
43 const double epsilon = 1e-6;
44 const double initial_time =
46 dt = time_step;
47
48 // When averaging velocities begins
49 if (current_time >= (initial_time - epsilon))
50 {
52 {
54 real_initial_time = current_time;
55
56 // Store the first dt value in case dt varies.
57 dt_0 = dt;
58 }
59 // Calculate (u*dt) at each time step and accumulate the values
60 velocity_dt.equ(dt, local_evaluation_point);
62
63 // Get the inverse of the time since the beginning of the time averaging
66
67 // Calculate the average velocities.
69
70 this->calculate_reynolds_stresses(local_evaluation_point);
71 }
72}
73
74
75template <int dim, typename VectorType, typename DofsType>
76void
78{
79 // Use the inverse of the time since the beginning of the time averaging to
80 // reevaluate the average velocity field and Reynolds stress.
81
82 if (total_time_for_average > 1e-16)
83 {
85 // Calculate the average velocities.
90 }
91}
92
93// The Reynolds stresses are computed at every locally owned velocity support
94// point from the degrees of freedom of its velocity components, which are
95// gathered by gather_locally_owned_support_points(). The global indices of the
96// degrees of freedom are also the global indices of the vectors, including the
97// block vectors, whose blocks hold the velocity and the pressure degrees of
98// freedom numbered component-wise. The stresses are stored in the slots of the
99// solution: (<u'u'>, <v'v'>, <w'w'>, k) in the normal stress vector and
100// (<u'v'>, <v'w'>, <w'u'>, 0) in the shear stress vector, where k is only
101// stored at the pressure degrees of freedom that share a velocity support
102// point. In 2D, the v slot of the shear stress vector is not used.
103template <int dim, typename VectorType, typename DofsType>
104void
106 const VectorType &local_evaluation_point)
107{
108 for (const SupportPointDoFs &support_point : locally_owned_support_points)
109 {
110 const std::array<types::global_dof_index, dim> &velocity_dofs =
111 support_point.velocity;
112
113 // Velocity fluctuations u', v' and w'
114 std::array<double, dim> fluctuation;
115 for (int d = 0; d < dim; ++d)
116 fluctuation[d] = local_evaluation_point(velocity_dofs[d]) -
117 average_velocities(velocity_dofs[d]);
118
119 // u'u'*dt, v'v'*dt and w'w'*dt
120 double sum_normal_stresses = 0.;
121 for (int d = 0; d < dim; ++d)
122 {
123 const double normal_stress_dt = fluctuation[d] * fluctuation[d] * dt;
124 reynolds_normal_stress_dt(velocity_dofs[d]) = normal_stress_dt;
125 sum_normal_stresses += normal_stress_dt;
126 }
127
128 // u'v'*dt
129 reynolds_shear_stress_dt(velocity_dofs[0]) =
130 fluctuation[0] * fluctuation[1] * dt;
131
132 if constexpr (dim == 3)
133 {
134 // v'w'*dt
135 reynolds_shear_stress_dt(velocity_dofs[1]) =
136 fluctuation[1] * fluctuation[2] * dt;
137
138 // w'u'*dt
139 reynolds_shear_stress_dt(velocity_dofs[2]) =
140 fluctuation[2] * fluctuation[0] * dt;
141 }
142
143 // k*dt = 1/2(u'u'+v'v'+w'w')*dt (turbulent kinetic energy)
144 if (support_point.pressure != numbers::invalid_dof_index)
145 reynolds_normal_stress_dt(support_point.pressure) =
146 0.5 * sum_normal_stresses;
147 }
148
149 reynolds_normal_stress_dt.compress(VectorOperation::insert);
150 reynolds_shear_stress_dt.compress(VectorOperation::insert);
151
152 // Sum of all reynolds stresses during simulation.
155
156 // Calculate the reynolds stresses.
159}
160
161
162template <int dim, typename VectorType, typename DofsType>
163void
165 const DofsType &locally_owned_dofs,
166 const DofsType &locally_relevant_dofs,
167 const MPI_Comm &mpi_communicator)
168{
169 // Reinitialisation of the average velocity and reynolds stress vectors
170 // to get the right length.
171 velocity_dt.reinit(locally_owned_dofs, mpi_communicator);
172 sum_velocity_dt.reinit(locally_owned_dofs, mpi_communicator);
173 average_velocities.reinit(locally_owned_dofs, mpi_communicator);
174 get_av->reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
175
176 // Reinitialize independent components of stress tensor vectors.
177 reynolds_normal_stress_dt.reinit(locally_owned_dofs, mpi_communicator);
178 sum_reynolds_normal_stress_dt.reinit(locally_owned_dofs, mpi_communicator);
179 reynolds_normal_stresses.reinit(locally_owned_dofs, mpi_communicator);
180 get_rns.reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
181
182 reynolds_shear_stress_dt.reinit(locally_owned_dofs, mpi_communicator);
183 sum_reynolds_shear_stress_dt.reinit(locally_owned_dofs, mpi_communicator);
184 reynolds_shear_stresses.reinit(locally_owned_dofs, mpi_communicator);
185 get_rss.reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
186
187 // Initializing vector with locally_relevant_dofs because writing checkpoint
188 // needs to read ghost cells.
189 sum_velocity_dt_with_ghost_cells.reinit(locally_owned_dofs,
190 locally_relevant_dofs,
191 mpi_communicator);
192 sum_rns_dt_with_ghost_cells.reinit(locally_owned_dofs,
193 locally_relevant_dofs,
194 mpi_communicator);
195 sum_rss_dt_with_ghost_cells.reinit(locally_owned_dofs,
196 locally_relevant_dofs,
197 mpi_communicator);
198
199 // The Reynolds stresses are written using the global indices of the degrees
200 // of freedom, which requires vectors that span all of them.
201 AssertDimension(reynolds_normal_stress_dt.size(), dof_handler.n_dofs());
202
204}
205
206template <int dim, typename VectorType, typename DofsType>
207void
210{
211 // Tolerance used to identify two unit support points of the reference cell.
212 constexpr double support_point_tolerance = 1e-12;
213
214 Assert(!dof_handler.has_hp_capabilities(), ExcNotImplemented());
215
216 const FiniteElement<dim> &fe = dof_handler.get_fe();
217 AssertDimension(fe.n_components(), dim + 1);
218 Assert(fe.is_primitive(), ExcNotImplemented());
219
220 // All the velocity components share one base element, so the degrees of
221 // freedom of a velocity support point are the degrees of freedom of the dim
222 // components with the same index within this base element.
223 const unsigned int velocity_base = fe.component_to_base_index(0).first;
224 for (int d = 1; d < dim; ++d)
225 Assert(fe.component_to_base_index(d).first == velocity_base,
226 ExcInternalError());
227
228 const FiniteElement<dim> &velocity_fe = fe.base_element(velocity_base);
229 const FiniteElement<dim> &pressure_fe =
230 fe.base_element(fe.component_to_base_index(dim).first);
231 AssertThrow(velocity_fe.has_support_points() &&
232 pressure_fe.has_support_points(),
233 ExcMessage(
234 "The time-averaged Reynolds stresses require velocity and "
235 "pressure finite elements with support points."));
236
237 const unsigned int n_velocity_nodes = velocity_fe.n_dofs_per_cell();
238 const unsigned int n_pressure_nodes = pressure_fe.n_dofs_per_cell();
239 const std::vector<Point<dim>> &velocity_unit_support_points =
240 velocity_fe.get_unit_support_points();
241 const std::vector<Point<dim>> &pressure_unit_support_points =
242 pressure_fe.get_unit_support_points();
243
244 // Cell-local indices of the velocity components of every velocity node
245 std::vector<std::array<unsigned int, dim>> local_velocity_dofs(
246 n_velocity_nodes);
247 for (unsigned int i = 0; i < n_velocity_nodes; ++i)
248 for (int d = 0; d < dim; ++d)
249 local_velocity_dofs[i][d] = fe.component_to_system_index(d, i);
250
251 // Cell-local index of the pressure at every velocity node. Every pressure
252 // node is paired with the first velocity node that has the same unit support
253 // point and that is not paired yet. With equal-order elements, this pairs the
254 // nodes with the same index. With bubble functions, which all have their
255 // support point at the center of the cell, the Lagrange nodes are paired
256 // first, since they are numbered before the bubble functions.
257 std::vector<unsigned int> local_pressure_dofs(n_velocity_nodes,
258 numbers::invalid_unsigned_int);
259 for (unsigned int j = 0; j < n_pressure_nodes; ++j)
260 for (unsigned int i = 0; i < n_velocity_nodes; ++i)
261 if (local_pressure_dofs[i] == numbers::invalid_unsigned_int &&
262 velocity_unit_support_points[i].distance(
263 pressure_unit_support_points[j]) < support_point_tolerance)
264 {
265 local_pressure_dofs[i] = fe.component_to_system_index(dim, j);
266 break;
267 }
268
269 // Gather the global indices of every locally owned velocity support point
270 // once. A support point is identified by the degree of freedom of its first
271 // velocity component. All the degrees of freedom of a support point belong to
272 // the same mesh object, and therefore to the same process.
273 const IndexSet &locally_owned_dofs = dof_handler.locally_owned_dofs();
274 std::vector<bool> is_gathered(locally_owned_dofs.n_elements(), false);
275 std::vector<types::global_dof_index> dof_indices(fe.n_dofs_per_cell());
276
278 for (const auto &cell : dof_handler.active_cell_iterators())
279 {
280 if (!cell->is_locally_owned())
281 continue;
282
283 cell->get_dof_indices(dof_indices);
284 for (unsigned int i = 0; i < n_velocity_nodes; ++i)
285 {
286 const types::global_dof_index first_velocity_dof =
287 dof_indices[local_velocity_dofs[i][0]];
288 if (!locally_owned_dofs.is_element(first_velocity_dof))
289 continue;
290
291 const types::global_dof_index index_within_owned_dofs =
292 locally_owned_dofs.index_within_set(first_velocity_dof);
293 if (is_gathered[index_within_owned_dofs])
294 continue;
295 is_gathered[index_within_owned_dofs] = true;
296
297 SupportPointDoFs support_point;
298 for (int d = 0; d < dim; ++d)
299 {
300 support_point.velocity[d] =
301 dof_indices[local_velocity_dofs[i][d]];
302 Assert(locally_owned_dofs.is_element(support_point.velocity[d]),
303 ExcInternalError());
304 }
305
306 support_point.pressure =
307 (local_pressure_dofs[i] == numbers::invalid_unsigned_int) ?
308 numbers::invalid_dof_index :
309 dof_indices[local_pressure_dofs[i]];
310 Assert(support_point.pressure == numbers::invalid_dof_index ||
311 locally_owned_dofs.is_element(support_point.pressure),
312 ExcInternalError());
313
314 locally_owned_support_points.push_back(support_point);
315 }
316 }
317}
318
319template <int dim, typename VectorType, typename DofsType>
320void
333
334template <int dim, typename VectorType, typename DofsType>
335void
337{
338 if constexpr (std::is_same_v<VectorType,
339 LinearAlgebra::distributed::Vector<double>>)
340 {
341 // When the vector are deal.II vectors, the solution transfer expects a
342 // vector with ghost entries
349
353 }
354 else
355 {
356 // When the vectors are trilinos vectors, the solution transfer expects a
357 // locally_owned vector
363
364
365
369 }
370
371
372
374}
375
376template <int dim, typename VectorType, typename DofsType>
377std::vector<const VectorType *>
379{
383
384
385 std::vector<const VectorType *> av_set_transfer;
386 av_set_transfer.push_back(&sum_velocity_dt_with_ghost_cells);
387 av_set_transfer.push_back(&sum_rns_dt_with_ghost_cells);
388 av_set_transfer.push_back(&sum_rss_dt_with_ghost_cells);
389
390
391 std::string filename = prefix + ".averagevelocities";
392 std::ofstream output(filename.c_str());
393 output << "Average velocities" << std::endl;
394 output << "dt_0 " << dt_0 << std::endl;
395 output << "has_started_averaging_boolean " << has_started_averaging
396 << std::endl;
397 output << "Real_initial_time " << real_initial_time << std::endl;
398
399 return av_set_transfer;
400}
401
402template <int dim, typename VectorType, typename DofsType>
403std::vector<VectorType *>
405{
406 std::vector<VectorType *> sum_vectors;
407 sum_vectors.push_back(&sum_velocity_dt_with_ghost_cells);
408 sum_vectors.push_back(&sum_rns_dt_with_ghost_cells);
409 sum_vectors.push_back(&sum_rss_dt_with_ghost_cells);
410
411
412 std::string filename = prefix + ".averagevelocities";
413 check_file_exists(filename,
414 "checkpoint file of the time-averaged velocities, given by "
415 "'subsection restart' - 'set filename'");
416 std::ifstream input(filename.c_str());
417
418 std::string buffer;
419 std::getline(input, buffer);
420 input >> buffer >> dt_0;
421 input >> buffer >> has_started_averaging;
422 input >> buffer >> real_initial_time;
423
424 return sum_vectors;
425}
426
427template <int dim, typename VectorType, typename DofsType>
428void
441
443
445
446template class AverageVelocities<2,
448 std::vector<IndexSet>>;
449
450template class AverageVelocities<3,
452 std::vector<IndexSet>>;
453#ifndef LETHE_USE_LDV
454template class AverageVelocities<2,
455 LinearAlgebra::distributed::Vector<double>,
456 IndexSet>;
457
458template class AverageVelocities<3,
459 LinearAlgebra::distributed::Vector<double>,
460 IndexSet>;
461#endif
Calculates the time-averaged velocities and pressure and the independent components of the Reynolds...
VectorType sum_velocity_dt
Vector to store the sum of all the velocities multiplied by time step.
SolutionTransfer< dim, VectorType > solution_transfer_sum_reynolds_normal_stress_dt
Object used to transfer the average Reynolds normal stresses in case of mesh adaptation.
VectorType reynolds_normal_stress_dt
Vector to store the velocities multiplied by the time step to calculate the normal stresses.
VectorType sum_velocity_dt_with_ghost_cells
Ghosted vector to store the sum of all the velocities multiplied by time step.
VectorType average_velocities
Vector to store average velocities.
VectorType sum_rns_dt_with_ghost_cells
Ghosted vector to store sum of the velocities multiplied by the time step to calculate the normal str...
std::vector< const VectorType * > save(const std::string &prefix)
Save checkpoints to continuing averaging after restart.
VectorType velocity_dt
Vector to store the velocity multiplied by time step.
double inv_range_time
Inverse for total time for averaging.
std::shared_ptr< VectorType > get_av
Shared pointer to the getter vector for the average velocities.
SolutionTransfer< dim, VectorType > solution_transfer_sum_reynolds_shear_stress_dt
Object used to transfer the average Reynolds shear stresses in case of mesh adaptation.
void initialize_vectors(const DofsType &locally_owned_dofs, const DofsType &locally_relevant_dofs, const MPI_Comm &mpi_communicator)
Initialize all the important vectors for the average velocities and reynolds stresses calculation....
VectorType get_rns
Getter vector for the average Reynolds normal stresses.
VectorType sum_reynolds_shear_stress_dt
Vector to store the sum the velocities multiplied by the time step to calculate the shear stresses.
void post_mesh_adaptation()
Reestablish solution vectors after dynamic mesh adaptation.
VectorType get_rss
Getter vector for the average Reynolds shear stresses.
void calculate_reynolds_stresses(const VectorType &local_evaluation_point)
Calculate normal and other resolved time-averaged Reynold stresses and .
void gather_locally_owned_support_points()
Gather the degrees of freedom of every locally owned velocity support point, with the pressure degree...
VectorType sum_rss_dt_with_ghost_cells
Ghosted vector to store the sum the velocities multiplied by the time step to calculate the shear str...
std::vector< SupportPointDoFs > locally_owned_support_points
Degrees of freedom of the locally owned velocity support points.
const DoFHandler< dim > & dof_handler
DoFHandler of the velocity and pressure fields.
double dt_0
Store the first time step in case it varies.
void prepare_for_mesh_adaptation()
Prepare average velocity object for dynamic mesh adaptation.
void calculate_average_velocities(const VectorType &local_evaluation_point, const Parameters::PostProcessing< dim > &post_processing, const double &current_time, const double &time_step)
Calculate time-averaged velocities and pressure using vector with no ghost cells.
std::vector< VectorType * > read(const std::string &prefix)
Read checkpoints to continuing averaging after restart.
SolutionTransfer< dim, VectorType > solution_transfer_sum_velocity_dt
Object used to transfer the average velocities in case of mesh adaptation.
bool has_started_averaging
Track whether we are within the averaging time period.
AverageVelocities(DoFHandler< dim > &dof_handler)
Constructor that initializes the solution transfer objects for the velocity and the Reynolds stresses...
void zero_average_after_restart()
Reinitialize the values of sum_velocities_dt_with_ghost_cells vector to 0 and the has_started_averagi...
VectorType reynolds_shear_stress_dt
Vector to store the velocities multiplied by the time step to calculate the shear stresses.
double real_initial_time
Initial time of the simulation.
VectorType reynolds_shear_stresses
Vector with average average Reynolds shear stresses.
VectorType sum_reynolds_normal_stress_dt
Vector to store sum of the velocities multiplied by the time step to calculate the normal stresses.
double total_time_for_average
Total period of time for averaging.
VectorType reynolds_normal_stresses
Vector with average Reynolds normal stresses.
Postprocessing - Defines the parameters for the post-processing. In Lethe, post-processing implies th...
double initial_time_for_average_velocities
Set initial time to start calculations for velocities.
void check_file_exists(const std::string &file_name, const std::string &file_description)
Verify that a file that Lethe must read exists and can be opened, and raise an error stated in the us...
Definition utilities.cc:225
Global indices of the degrees of freedom located at one velocity support point.
types::global_dof_index pressure
Degree of freedom of the pressure at the same support point, or numbers::invalid_dof_index if the pre...
std::array< types::global_dof_index, dim > velocity
Degrees of freedom of the dim velocity components.
GlobalVectorType VectorType
dealii::TrilinosWrappers::MPI::BlockVector GlobalBlockVectorType
Definition vector.h:18