Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
postprocessing_scalar.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 <fstream>
10
11template <int dim>
12AverageScalar<dim>::AverageScalar(const DoFHandler<dim> &dof_handler)
16{}
17
18template <int dim>
19void
21 const GlobalVectorType &local_evaluation_point,
22 const Parameters::PostProcessing<dim> &post_processing,
23 const double current_time,
24 const double time_step)
25{
26 const double epsilon = 1e-6;
27 const double initial_time =
29 dt = time_step;
30
31 // When averaging scalar begins
32 if (current_time >= (initial_time - epsilon))
33 {
35 {
36 real_initial_time = current_time;
37
38 // Store the first dt value in case dt varies.
39 dt_0 = dt;
40
42 }
43
44 // Calculate (scalar*dt) at each time step and accumulates the values
45 scalar_dt.equ(dt, local_evaluation_point);
46
48
49 // Get the inverse of the time since the beginning of the time averaging
52
53 // Calculate the average scalars.
55 }
56}
57
58template <int dim>
59void
60AverageScalar<dim>::initialize_vectors(const IndexSet &locally_owned_dofs,
61 const IndexSet &locally_relevant_dofs,
62 const MPI_Comm &mpi_communicator)
63{
64 // Reinitialisation of the average scalars to get the right length.
65 scalar_dt.reinit(locally_owned_dofs, mpi_communicator);
66 sum_scalar_dt.reinit(locally_owned_dofs, mpi_communicator);
67 average_scalar.reinit(locally_owned_dofs, mpi_communicator);
68
69 average_scalar_with_ghost_cells.reinit(locally_owned_dofs,
70 locally_relevant_dofs,
71 mpi_communicator);
72
73 sum_scalar_dt_with_ghost_cells.reinit(locally_owned_dofs,
74 locally_relevant_dofs,
75 mpi_communicator);
76}
77
78template <int dim>
79void
81{
82 if (total_time_for_average > 1e-16)
83 {
85 // Calculate the average scalar.
87 }
88}
89
90template <int dim>
91void
98
99template <int dim>
100void
109
110template <int dim>
111std::vector<const GlobalVectorType *>
112AverageScalar<dim>::save(const std::string &prefix)
113{
115
116 std::vector<const GlobalVectorType *> avg_scalar_set_transfer;
117 avg_scalar_set_transfer.push_back(&sum_scalar_dt_with_ghost_cells);
118
119 std::string filename = prefix + ".averagescalar";
120 std::ofstream output(filename.c_str());
121 output << "Average scalar" << std::endl;
122 output << "dt_0 " << dt_0 << std::endl;
123 output << "has_started_averaging_boolean " << has_started_averaging
124 << std::endl;
125 output << "Real_initial_time " << real_initial_time << std::endl;
126
127 return avg_scalar_set_transfer;
128}
129
130template <int dim>
131std::vector<GlobalVectorType *>
132AverageScalar<dim>::read(const std::string &prefix)
133{
134 std::vector<GlobalVectorType *> sum_vectors;
135 sum_vectors.push_back(&sum_scalar_dt_with_ghost_cells);
136
137 std::string filename = prefix + ".averagescalar";
138 check_file_exists(filename,
139 "checkpoint file of the time-averaged scalar, given by "
140 "'subsection restart' - 'set filename'");
141 std::ifstream input(filename.c_str());
142
143 std::string buffer;
144 std::getline(input, buffer);
145 input >> buffer >> dt_0;
146 input >> buffer >> has_started_averaging;
147 input >> buffer >> real_initial_time;
148
149 return sum_vectors;
150}
151
152template <int dim>
153void
161
162template class AverageScalar<2>;
163template class AverageScalar<3>;
Calculates the time-averaged scalar field.
SolutionTransfer< dim, GlobalVectorType > solution_transfer_sum_scalar_dt
Object to transfer the averaged scalar in case of mesh adaptation.
std::vector< const GlobalVectorType * > save(const std::string &prefix)
Save checkpoints to continue the averaging after the restart.
GlobalVectorType average_scalar_with_ghost_cells
Time averaged scalar containing the locally owned and locally relevant dofs for all processes.
void zero_average_after_restart()
Reinitialize the values of sum_scalar_dt_with_ghost_cells vector to 0 and the has_started_averaging f...
GlobalVectorType average_scalar
Time averaged scalar.
double inv_range_time
Inverse of the total time for averaging.
void prepare_for_mesh_adaptation()
Prepare average scalar object for dynamic mesh adaptation.
GlobalVectorType scalar_dt
Vector to store the scalar field multiplied by time step.
bool has_started_averaging
Track whether we are within the averaging time period.
GlobalVectorType sum_scalar_dt_with_ghost_cells
Ghosted vector to store the sum of all the scalar multiplied by time step.
GlobalVectorType sum_scalar_dt
Vector to store the sum of all the scalar multiplied by time step.
double dt_0
First time step is stored in dt_0 and added to total_time_for_average to ensure the first average cal...
double total_time_for_average
Vector to store the sum of all the scalar field multiplied by time step.
void calculate_average_scalar(const GlobalVectorType &local_evaluation_point, const Parameters::PostProcessing< dim > &post_processing, const double current_time, const double time_step)
Calculate time-averaged scalar field using vector with no ghost cells.
AverageScalar(const DoFHandler< dim > &dof_handler)
Constructor that initializes the solution transfer objects for the scalar field.
void initialize_vectors(const IndexSet &locally_owned_dofs, const IndexSet &locally_relevant_dofs, const MPI_Comm &mpi_communicator)
Calculate time-averaged scalar field using vector with no ghost cells.
void post_mesh_adaptation()
Reestablish solution vectors after dynamic mesh adaptation.
double real_initial_time
Difference between the simulation starting time and the initial averaging time.
std::vector< GlobalVectorType * > read(const std::string &prefix)
Read checkpoints to continuing averaging after restart.
double dt
Time step.
void update_average_scalar()
Prepare average scalar object for dynamic mesh adaptation.
Postprocessing - Defines the parameters for the post-processing. In Lethe, post-processing implies th...
double initial_time_for_average_temp_and_hf
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
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17