Loading...
Searching...
No Matches
PODTemplate.H
1#ifndef PODTemplate_H
2#define PODTemplate_H
3
4#include <Time.H>
5#include <volFieldsFwd.H>
6#include <FieldField.H>
7#include "Foam2Eigen.H"
8
9#include "ITHACAstream.H"
10#include "ITHACAutilities.H"
11#include "StoredParameters.H"
12
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Wold-style-cast"
15#include <Eigen/Eigen>
16#include <Spectra/GenEigsSolver.h>
17#include <Spectra/SymEigsSolver.h>
18#pragma GCC diagnostic pop
19
20namespace ITHACAPOD
21{
22
23// Structure to store the indexes of the added block to the cov matrix
24// Triangular block
26{
27 Foam::label index_start;
28 Foam::label index_end;
29};
30
31// Structure to store the indexes of the added block to the cov matrix
32// Square block
34{
35 Foam::label index1_start;
36 Foam::label index1_end;
37 Foam::label index2_start;
38 Foam::label index2_end;
39};
40
41
42// Class that compute POD (spatial modes and temporal modes)
43template<typename T>
44class PODTemplate
45{
46 protected :
47 StoredParameters* m_parameters;
48 // Path where OpenFOAM simulation outputs are
49 Foam::word field_name;
50 // Type of data used in the POD
51 const Foam::fileName& casenameData;
52 // Number of snapshot, also number of time step
53 Foam::label l_nSnapshot;
54 // number of blocks to load snapshots
55 const Foam::label l_nBlocks;
56 // number of modes
57 Foam::label l_nmodes;
58 // Hilbert space used for the POD scalar product
59 // (and the associated norm to sort the modes)
60 Foam::word l_hilbertSp;
61 //Weight of the weighted H1 scalar product
62 double weightH1;
63 //Weight of the boundary conditions
64 const double weightBC;
65 //Patch for the boundary conditions
66 const Foam::word patchBC;
67 // start time of the POD decomposition
68 Foam::label l_startTime;
69 // end time of the POD decomposition
70 Foam::label l_endTime;
71
72 // Number of Simulation snapshot, also number of time step
73 Foam::label l_nSnapshotSimulation;
74 // end time of the Simulation
75 const Foam::label l_endTimeSimulation;
76
77 // Path to the directory where the snapshots are stored
78 // (usually casenameData, but can be different through 3rd arg in constructor)
79 fileName snapshotsPath;
80
81 // List linking indices and time for the snapshots data directories
82 Foam::instantList timeFolders;
83
84 // name and folder to save the covMatrix
85 Foam::fileName name_covMatrix;
86 Foam::word folder_covMatrix;
87 bool exist_covMatrix;
88
89 // name and folder to save eigenValues et eigenVectors
90 Foam::fileName name_eigenValues;
91 Foam::fileName name_eigenValuesNormalized;
92 Foam::fileName name_cumEigenValues;
93 Foam::fileName name_eigenVector;
94 Foam::word folder_eigen;
95 bool exist_eigenDecomposition;
96
97 // folder of the spatialModes
98 Foam::word folder_spatialModes;
99 bool exist_spatialModes;
100
101 // folder of the temporal Modes
102 Foam::word folder_temporalModes;
103 bool exist_temporalModes;
104
105 // folder of the temporal Modes
106 Foam::word folder_temporalModesSimulation;
107 bool exist_temporalModesSimulation;
108
109 // template of field readed, useful for the ITHACAstream::read_fields mlethod
110 T* f_field;
111
112 // if true, the temporal mean of snapshot is computed and subtract to snapshot fields
113 // else, temporal mean is not computed and snapshot fields are not modified
114 bool b_centeredOrNot;
115 T* f_meanField;
116 Foam::word folder_mean;
117 bool exist_noMean;
118
119 // Temporal modes energies
120 Eigen::VectorXd eigenValueseigLam;
121 Eigen::VectorXd lambda;
122
123 const Foam::word w_eigensolver;
124 const int i_precision;
125 const std::_Ios_Fmtflags ios_outytpe;
126
127 const Foam::Time runTime2;
128
129 Eigen::MatrixXi inletIndex;
130 bool lifting;
131 Foam::PtrList<T> liftfield;
132
133 public :
134 // Constructor
135 PODTemplate(Parameters* myParameters, const Foam::word& myfield_name, const word& mySnapshots_path = "default_path");
136
137 // Destructor
138 virtual ~PODTemplate();
139
140 void define_paths();
141
142 // Optionnal preliminary computation
143 // -------------------------------------------------------------------------------------------
144 // method to compute the temporal mean of the snapshots
145 // it modifies the private attribute f_meanField
146 // if centeredOrNot is true, the mean is computed
147 // else the mean is a zero field
148 void computeMeanField();
149
150 // in case the mean has been computed
151 // the mean field is appended to spatialModes
152 // param[inout] : spatialModes without mean field, spatialModes with mean field
153 void appendMeanfieldtoSpatialModes(Foam::PtrList<T>& spatialModes);
154 // -------------------------------------------------------------------------------------------
155
156 // if a covMatrix temporary file is found in the covMat folder
157 // it is loaded and the process continues where it stopped
158 void findTempFile(Eigen::MatrixXd* covMat, int* index1, int* index2);
159
160 // temporary covMatrix filename
161 Foam::word nameTempCovMatrix( int i, int j);
162
163 // save the temporary covMatrix file
164 void saveTempCovMatrix(Eigen::MatrixXd& covMatrix, int i, int j);
165
166 // delete the temporary covMatrix file
167 void deleteTempCovMatrix(int i, int j);
168
169 // delete previous [step-N] covMatrix temp file -> keep last N covMatrix temp file
170 void deletePreviousTempCovMatrix_N(int* valI, int* valJ, int i, int j, int N);
171
172 // First step : computation of covariance matrix
173 // -------------------------------------------------------------------------------------------
174 // method to compute the covariance matrix
175 // param[out] : the covariance matrix
176 virtual Eigen::MatrixXd buildCovMatrix();
177
178 // method to add a new triangular block to the covariance matrix (near the diagonal)
179 // used in buildCovMatrix method
180 // param[inout] : covMatrix, covariance matrix
181 // param[in] : snapshots sample of the field
182 // param[in] : indexes of the added block
183 void addCovMatrixTriCoeff(Eigen::MatrixXd& covMatrix,
184 Foam::PtrList<T>& snapshots,
185 indexTri& indTri);
186
187 // method to add a new square block to the covariance matrix
188 // used in buildCovMatrix method
189 // param[inout] : covMatrix, covariance matrix
190 // param[in] : snapshots1 and snapshots2, sample of the field
191 // param[in] : indexes of the added block
192 void addCovMatrixSquareCoeff(Eigen::MatrixXd& covMatrix,
193 Foam::PtrList<T>& snapshots1,
194 Foam::PtrList<T>& snapshots2,
195 indexSquare& indSquare);
196 // -------------------------------------------------------------------------------------------
197
198
199 // Second step : diagonalisation of the covariance matrix
200 // -------------------------------------------------------------------------------------------
201 // method to diagonalize the covariance matrix
202 // param[in] covMatrix : covariance matrix (compute in the buildCovMatrix method)
203 // param[out] eigenValueseig : eigen values of the diagonalisation
204 // param[out] eigenVectoreig : eigen vector of the diagonalisation
205 void diagonalisation(Eigen::MatrixXd& covMatrix,
206 Eigen::VectorXd& eigenValueseig, Eigen::MatrixXd& eigenVectoreig);
207 // -------------------------------------------------------------------------------------------
208
209
210 // Third step : computation of spatial modes (alse named topos)
211 // -------------------------------------------------------------------------------------------
212 // compute the spatial from the eigen values and vectors of the diagonalisation
213 // param[in] eigenValueseig and eigenVectoreig : eigen values and vectors (compute in the diagonalisation method)
214 // param[out] : spatialModes
215 Foam::PtrList<T> computeSpatialModes(Eigen::VectorXd& eigenValueseig,
216 Eigen::MatrixXd& eigenVectoreig);
217
218 // // get the modes on Eigen format from the eigen values and vectors and snapshots
219 // // param[in] : eigenValueseigLam and eigenVectoreig
220 // // param[out] : modesEig, the Eigen formatting modes
221 // // used in computeSpatialModes method
222 // List<Eigen::MatrixXd> computeModesEig(Eigen::VectorXd& eigenValueseigLam, Eigen::MatrixXd& eigenVectoreig);
223 // -------------------------------------------------------------------------------------------
224
225
226 // Forth step : computation of temporal modes (also named chronos)
227 // -------------------------------------------------------------------------------------------
228 // compute the temporals from the spatial modes and snapshots
229 // param[in] : spatial modes
230 // param[out] : temporals modes
231 Eigen::MatrixXd computeTemporalModes(Eigen::VectorXd& eigenValueseig,
232 Eigen::MatrixXd& eigenVectoreig);
233 // -------------------------------------------------------------------------------------------
234
235
236 // Function which execute all the steps implemented in the previous functions
237 // -------------------------------------------------------------------------------------------
238 // get the temporal and spatial modes
239 // param[out] : spatialModes
240 // param[out] : temporalModes
241 void getModes(Foam::PtrList<T>& spatialModes, Eigen::MatrixXd& temporalModes,
242 Eigen::MatrixXd& temporalModesSimulation, Eigen::MatrixXd& covMatrix);
243 // -------------------------------------------------------------------------------------------
244
245 // computation of test temporal modes
246 // -------------------------------------------------------------------------------------------
247 // compute the temporals from the spatial modes and snapshots
248 // param[in] : spatial modes
249 // param[out] : temporals modes
250 Eigen::MatrixXd computeSimulationTemporalModes(Foam::PtrList<T>& spatialModes);
251
252 // computation of test temporal modes energies
253 void compute_lambda(Eigen::MatrixXd& temporalModes);
254
255 const T& get_mean() const
256 {
257 return (*f_meanField);
258 }
259
260 void set_b_centeredOrNot(const Foam::label& input_b_centeredOrNot)
261 {
262 b_centeredOrNot = input_b_centeredOrNot;
263 define_paths();
264 }
265
266 //--------------------------------------------------------------------------
267 // Homogenize the snapshot matrix, it works with PtrList of volVectorField and volScalarField
268 //
269 // @param[in] Lfield The list of snapshots to be homogenized.
270 void lift(Foam::PtrList<T>& snapshots);
271 void lift(T& snapshot);
272
273
274 // Modify parameters for the snapshots. Useful if they are different from the OpenFOAM simulation
275 void set_snapFolderParams(label nSnapshot=-1, label nSnapshotSimulation=-1, label startTime=-1, label endTime=-1)
276 {
277 if (nSnapshot != -1) {l_nSnapshot = nSnapshot;}
278 if (nSnapshotSimulation != -1) {l_nSnapshotSimulation = nSnapshotSimulation;}
279 if (startTime != -1) {l_startTime = startTime;}
280 if (endTime != -1) {l_endTime = endTime;}
281 };
282
283};
284
285void computeLift(Foam::PtrList<Foam::volScalarField>& Lfield,
286 Foam::PtrList<Foam::volScalarField>& liftfield,
287 Foam::PtrList<Foam::volScalarField>& omfield, Eigen::MatrixXi inletIndex);
288void computeLift(Foam::PtrList<Foam::volVectorField>& Lfield,
289 Foam::PtrList<Foam::volVectorField>& liftfield,
290 Foam::PtrList<Foam::volVectorField>& omfield, Eigen::MatrixXi inletIndex);
291void computeLift(Foam::PtrList<Foam::volTensorField>& Lfield,
292 Foam::PtrList<Foam::volTensorField>& liftfield,
293 Foam::PtrList<Foam::volTensorField>& omfield, Eigen::MatrixXi inletIndex);
294
295}
296
297#endif
Header file of the Foam2Eigen class.
Header file of the ITHACAstream class, it contains the implementation of several methods for input ou...
Header file of the ITHACAutilities namespace.
Class that contains all parameters of the stochastic POD.
namespace for the computation of the POD, it exploits bot the method of snapshots and SVD.