9#include <deal.II/base/index_set.h>
10#include <deal.II/base/point.h>
12#include <deal.II/fe/fe.h>
14#include <deal.II/lac/vector_operation.h>
21template <
int dim,
typename VectorType,
typename DofsType>
32 get_av = std::make_shared<VectorType>();
35template <
int dim,
typename VectorType,
typename DofsType>
40 const double ¤t_time,
41 const double &time_step)
43 const double epsilon = 1e-6;
44 const double initial_time =
49 if (current_time >= (initial_time - epsilon))
75template <
int dim,
typename VectorType,
typename DofsType>
103template <
int dim,
typename VectorType,
typename DofsType>
110 const std::array<types::global_dof_index, dim> &velocity_dofs =
111 support_point.velocity;
114 std::array<double, dim> fluctuation;
115 for (
int d = 0; d < dim; ++d)
116 fluctuation[d] = local_evaluation_point(velocity_dofs[d]) -
120 double sum_normal_stresses = 0.;
121 for (
int d = 0; d < dim; ++d)
123 const double normal_stress_dt = fluctuation[d] * fluctuation[d] *
dt;
125 sum_normal_stresses += normal_stress_dt;
130 fluctuation[0] * fluctuation[1] *
dt;
132 if constexpr (dim == 3)
136 fluctuation[1] * fluctuation[2] *
dt;
140 fluctuation[2] * fluctuation[0] *
dt;
144 if (support_point.pressure != numbers::invalid_dof_index)
146 0.5 * sum_normal_stresses;
162template <
int dim,
typename VectorType,
typename DofsType>
165 const DofsType &locally_owned_dofs,
166 const DofsType &locally_relevant_dofs,
167 const MPI_Comm &mpi_communicator)
171 velocity_dt.reinit(locally_owned_dofs, mpi_communicator);
174 get_av->reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
180 get_rns.reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
185 get_rss.reinit(locally_owned_dofs, locally_relevant_dofs, mpi_communicator);
190 locally_relevant_dofs,
193 locally_relevant_dofs,
196 locally_relevant_dofs,
206template <
int dim,
typename VectorType,
typename DofsType>
212 constexpr double support_point_tolerance = 1e-12;
214 Assert(!
dof_handler.has_hp_capabilities(), ExcNotImplemented());
216 const FiniteElement<dim> &fe =
dof_handler.get_fe();
217 AssertDimension(fe.n_components(), dim + 1);
218 Assert(fe.is_primitive(), ExcNotImplemented());
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,
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(),
234 "The time-averaged Reynolds stresses require velocity and "
235 "pressure finite elements with support points."));
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();
245 std::vector<std::array<unsigned int, dim>> local_velocity_dofs(
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);
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)
265 local_pressure_dofs[i] = fe.component_to_system_index(dim, j);
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());
278 for (
const auto &cell :
dof_handler.active_cell_iterators())
280 if (!cell->is_locally_owned())
283 cell->get_dof_indices(dof_indices);
284 for (
unsigned int i = 0; i < n_velocity_nodes; ++i)
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))
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])
295 is_gathered[index_within_owned_dofs] =
true;
298 for (
int d = 0; d < dim; ++d)
301 dof_indices[local_velocity_dofs[i][d]];
302 Assert(locally_owned_dofs.is_element(support_point.
velocity[d]),
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),
319template <
int dim,
typename VectorType,
typename DofsType>
329 .prepare_for_coarsening_and_refinement(
get_rns);
331 .prepare_for_coarsening_and_refinement(
get_rss);
334template <
int dim,
typename VectorType,
typename DofsType>
339 LinearAlgebra::distributed::Vector<double>>)
376template <
int dim,
typename VectorType,
typename DofsType>
377std::vector<const VectorType *>
385 std::vector<const VectorType *> av_set_transfer;
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;
399 return av_set_transfer;
402template <
int dim,
typename VectorType,
typename DofsType>
403std::vector<VectorType *>
406 std::vector<VectorType *> sum_vectors;
412 std::string filename = prefix +
".averagevelocities";
414 "checkpoint file of the time-averaged velocities, given by "
415 "'subsection restart' - 'set filename'");
416 std::ifstream input(filename.c_str());
419 std::getline(input, buffer);
420 input >> buffer >>
dt_0;
427template <
int dim,
typename VectorType,
typename DofsType>
448 std::vector<IndexSet>>;
452 std::vector<IndexSet>>;
455 LinearAlgebra::distributed::Vector<double>,
459 LinearAlgebra::distributed::Vector<double>,
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...
void update_average_velocities()
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 ¤t_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...
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