Loading...
Searching...
No Matches
PODTemplateH1.C
1#include "PODTemplateH1.H"
2
3namespace ITHACAPOD {
4
5template <typename T, typename G>
6PODTemplateH1<T,G>::PODTemplateH1(Parameters* myParameters, const word& myfield_name) :
7 PODTemplate<T>::PODTemplate(myParameters, myfield_name),
8 gradfield_name("grad"+field_name)
9{
10}
11
12template <typename T, typename G>
13Eigen::MatrixXd PODTemplateH1<T,G>::buildCovMatrix()
14{
15 precomputeGradients();
16
17 l_hilbertSp = "L2";
18 PODTemplate<T>::define_paths();
19 Eigen::MatrixXd covMatrix = PODTemplate<T>::buildCovMatrix();
20 l_hilbertSp = m_parameters->get_hilbertSpacePOD()[field_name];
21 PODTemplate<T>::define_paths();
22
23 m_parameters->set_hilbertSpacePOD(gradfield_name, "L2");
24 m_parameters->set_nModes(gradfield_name, l_nmodes);
25 PODTemplate<G> ithacaFVPODgrad(m_parameters,gradfield_name);
26 ithacaFVPODgrad.computeMeanField();
27 Eigen::MatrixXd covMatrixGrad = ithacaFVPODgrad.buildCovMatrix();
28
29 if (l_hilbertSp == "wH1")
30 {
31 weightH1 = covMatrix.trace()/covMatrixGrad.trace();
32 covMatrixGrad *= weightH1;
33 }
34 else
35 {
36 weightH1 = 1.0;
37 }
38 m_parameters->set_weightH1(weightH1);
39
40 covMatrix += covMatrixGrad;
41
42 if (Pstream::master())
43 {
44 mkDir(folder_covMatrix);
45 cnpy::save(covMatrix, folder_covMatrix + name_covMatrix+ ".npy");
46 }
47
48 double varyingEnergy = covMatrix.trace()/l_nSnapshot;
49 m_parameters->set_varyingEnergy( field_name + "_" + l_hilbertSp, varyingEnergy);
50 Info << "Total varying energy " << l_hilbertSp << " : " << varyingEnergy << endl;
51 Info << endl;
52
53 return covMatrix;
54}
55
56template <typename T, typename G>
57void PODTemplateH1<T,G>::precomputeGradients()
58{
59 T snapshotj = *f_field;
60 G gradSnapshotsj = fvc::grad(*f_field);
61 word local_file;
62 bool exist_precomputed_fields = true;
63
64 word pathProcessor("");
65 if (Pstream::parRun())
66 {
67 pathProcessor = "processor" + name(Pstream::myProcNo()) + "/";
68 }
69
70 local_file = runTime2.caseName() + pathProcessor + runTime2.times()[1].name()
71 + "/" + gradfield_name;
72 exist_precomputed_fields = exist_precomputed_fields && ITHACAutilities::check_file(local_file);
73 for (label j = 0; j < l_nSnapshot + l_nSnapshotSimulation - 1 ; j++)
74 {
75 // Read the j-th field
76 local_file = runTime2.caseName() + pathProcessor + runTime2.times()[l_startTime + j].name()
77 + "/" + gradfield_name;
78 exist_precomputed_fields = exist_precomputed_fields && ITHACAutilities::check_file(local_file);
79 }
80
81 if (!exist_precomputed_fields)
82 {
83 Info << "Evaluating gradient fields" << endl;
84 local_file = runTime2.caseName() + ".";
85 ITHACAstream::read_snapshot(snapshotj, "0", runTime2.caseName());
86 gradSnapshotsj = fvc::grad(snapshotj);
87 ITHACAstream::exportSolution(gradSnapshotsj,
88 "0",
89 local_file,
90 gradfield_name);
91 for (label j = 0; j < l_nSnapshot + l_nSnapshotSimulation - 1; j++)
92 {
93 label index = l_startTime + j;
94 ITHACAstream::read_snapshot(snapshotj, timeFolders[index].name(), runTime2.caseName());
95 gradSnapshotsj = fvc::grad(snapshotj);
96 // Write the j-th field
97 string idxTimeStr( runTime2.times()[ index ].name() );
98 fileName subfolder = idxTimeStr;
99 ITHACAstream::exportSolution(gradSnapshotsj,
100 subfolder,
101 local_file,
102 gradfield_name);
103 }
104 }
105}
106
107template <typename T, typename G>
108void PODTemplateH1<T,G>::changeEigenFolderUnitTest(const word& name) {
109
110 l_nmodes = l_nSnapshot -1;
111 word pathCentered("");
112 if (this->b_centeredOrNot && !(this->field_name=="U"))
113 {
114 pathCentered = "_centered";
115 }
116 this->folder_eigen = "./ITHACAoutput/AllEigenValues_" + name
117 + pathCentered + "_" + std::to_string(this->l_nmodes) + "snapshots/";
118
119 this->exist_eigenDecomposition = ITHACAutilities::check_file(this->folder_eigen + this->name_eigenValues+".npy")
120 && ITHACAutilities::check_file(this->folder_eigen + this->name_eigenVector+".npy");
121
122}
123
124}
namespace for the computation of the POD, it exploits bot the method of snapshots and SVD.
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
bool check_file(std::string fileName)
Function that returns true if a file exists.