Loading...
Searching...
No Matches
10UnsteadyBBEnclosed.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2Copyright (C) 2017 by the ITHACA-FV authors
3
4License
5 This file is part of ITHACA-FV
6
7 ITHACA-FV is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ITHACA-FV is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
19
20Description
21 Example of Boussinesq approximation for two way coupling NS-momentum equation
22 and heat transport equation for enclosed flows.
23SourceFiles
24 10UnsteadyBBEnclosed.C
25\*---------------------------------------------------------------------------*/
26
27#include "UnsteadyBB.H"
28#include "ITHACAPOD.H"
29#include "ReducedUnsteadyBB.H"
30#include "ITHACAstream.H"
31#include <chrono>
32#include <math.h>
33#include <iomanip>
34
36{
37 public:
38 explicit tutorial10(int argc, char* argv[])
39 :
40 UnsteadyBB(argc, argv),
41 U(_U()),
42 p(_p()),
43 p_rgh(_p_rgh()),
44 T(_T())
45 {}
46
47 // Fields To Perform
48 volVectorField& U;
49 volScalarField& p;
50 volScalarField& p_rgh;
51 volScalarField& T;
52
53 void offlineSolve(Eigen::MatrixXd par_BC)
54 {
55 List<scalar> mu_now(1);
56
57 if (offline)
58 {
59 ITHACAstream::read_fields(Ufield, U, "./ITHACAoutput/Offline/");
60 ITHACAstream::read_fields(Tfield, T, "./ITHACAoutput/Offline/");
61 }
62 else
63 {
64 for (label k = 0; k < par_BC.rows(); k++)
65 {
66 for (label j = 0; j < par_BC.cols(); j++)
67 {
68 for (label i = 0; i < mu.cols(); i++)
69 {
70 mu_now[0] = mu(0, i);
71 }
72
73 assignBC(T, inletIndexT(j, 0), par_BC(k, j));
74 }
75
76 truthSolve(mu_now);
77 }
78 }
79 }
80
81
82 void onlineSolveFull(Eigen::MatrixXd par_BC, label para_set_BC, fileName folder)
83 {
85 {
86 }
87 else
88 {
89 mkDir(folder);
91 label i = para_set_BC;
92
93 for (label j = 0; j < par_BC.cols(); j++)
94 {
95 assignBC(T, inletIndexT(j, 0), par_BC(i, j));
96 }
97
98 truthSolve(folder);
99 }
100 }
101
102 void onlineSolveRead(fileName folder)
103 {
105 {
108 }
109 else
110 {
111 }
112 }
113
114
115 // Method to compute the lifting function for temperature
117 {
118 for (label k = 0; k < inletIndexT.rows(); k++)
119 {
120 Time& runTime = _runTime();
121 fvMesh& mesh = _mesh();
122 volScalarField T = _T();
123 simpleControl simple(mesh);
124 volScalarField& alphat = _alphat();
125 dimensionedScalar& Pr = _Pr();
126 dimensionedScalar& Prt = _Prt();
127 label BCind = inletIndexT(k, 0);
128 volScalarField Tlift("Tlift" + name(k), T);
129 instantList Times = runTime.times();
130 runTime.setTime(Times[1], 1);
131 Info << "Solving a lifting Problem" << endl;
132 scalar t1 = 1;
133 scalar t0 = 0;
134
135 for (label j = 0; j < T.boundaryField().size(); j++)
136 {
137 assignIF(Tlift, t0);
138
139 if (j == BCind)
140 {
141 assignBC(Tlift, j, t1);
142 }
143 else if (T.boundaryField()[BCind].type() == "fixedValue")
144 {
145 assignBC(Tlift, j, t0);
146 }
147 else
148 {
149 }
150 }
151
152 while (simple.correctNonOrthogonal())
153 {
154 alphat = turbulence->nut() / Prt;
155 alphat.correctBoundaryConditions();
156 volScalarField alphaEff("alphaEff", turbulence->nu() / Pr + alphat);
157 fvScalarMatrix TEqn
158 (
159 fvm::laplacian(alphaEff, Tlift)
160 );
161 TEqn.solve();
162 Info << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
163 << " ClockTime = " << runTime.elapsedClockTime() << " s"
164 << nl << endl;
165 }
166
167 Tlift.write();
168 liftfieldT.append((Tlift).clone());
169 }
170 }
171};
172
173
174/*---------------------------------------------------------------------------*\
175 Starting the MAIN
176\*---------------------------------------------------------------------------*/
177int main(int argc, char* argv[])
178{
179 // Construct the tutorial object
180 tutorial10 example(argc, argv);
181 // the offline samples for the boundary conditions
182 word par_offline_BC("./par_offline_BC");
183 Eigen::MatrixXd par_off_BC = ITHACAstream::readMatrix(par_offline_BC);
184 // the samples which will be used for setting the boundary condition in the online stage
185 word par_online_BC("./par_online_BC");
186 Eigen::MatrixXd par_on_BC = ITHACAstream::readMatrix(par_online_BC);
187 // Read some parameters from file
189 example._runTime());
190 int NmodesUproj = para->ITHACAdict->lookupOrDefault<int>("NmodesUproj", 5);
191 int NmodesPproj = para->ITHACAdict->lookupOrDefault<int>("NmodesPproj", 5);
192 int NmodesTproj = para->ITHACAdict->lookupOrDefault<int>("NmodesTproj", 5);
193 int NmodesSUPproj = para->ITHACAdict->lookupOrDefault<int>("NmodesSUPproj", 5);
194 int NmodesOut = para->ITHACAdict->lookupOrDefault<int>("NmodesOut", 15);
196 example.Pnumber = 1;
198 example.Tnumber = 1;
200 example.setParameters();
202 example.mu_range(0, 0) = 0.00001;
203 example.mu_range(0, 1) = 0.00001;
204 // Generate equispaced samples inside the parameter range
205 example.genEquiPar();
207 example.inletIndexT.resize(2, 1);
208 example.inletIndexT << 1, 2;
210 example.startTime = 0.0;
211 example.finalTime = 10.0;
212 example.timeStep = 0.005;
213 example.writeEvery = 0.01;
214 // Perform the Offline Solve;
215 example.offlineSolve(par_off_BC);
216 // Search the lift function for the temperature
217 example.liftSolveT();
218 // Create homogeneous basis functions for temperature
219 example.computeLiftT(example.Tfield, example.liftfieldT, example.Tomfield);
220 // Perform a POD decomposition for velocity temperature and pressure fields
221 ITHACAPOD::getModes(example.Ufield, example.Umodes, example._U().name(),
222 example.podex, 0, 0,
223 NmodesOut);
224 ITHACAPOD::getModes(example.Tomfield, example.Tmodes, example._T().name(),
225 example.podex, 0, 0,
226 NmodesOut);
227 // Create a list with number of modes for which the projection needs to be performed
228 Eigen::MatrixXd List_of_modes(NmodesOut - 5, 1);
229
230 for (int i = 0; i < List_of_modes.rows(); i++)
231 {
232 List_of_modes(i, 0) = i + 1;
233 }
234
235 // Export with number of modes for which the projection needs to be performed
236 ITHACAstream::exportMatrix(List_of_modes, "List_of_modes", "eigen",
237 "./ITHACAoutput/l2error");
238 // Create locally the temperature modes
239 PtrList<volScalarField> TLmodes;
240
241 for (label k = 0; k < example.liftfieldT.size(); k++)
242 {
243 TLmodes.append((example.liftfieldT[k]).clone());
244 }
245
246 for (label k = 0; k < List_of_modes.size(); k++)
247 {
248 TLmodes.append((example.Tmodes[k]).clone());
249 }
250
251 // Perform the projection for all number of modes in list List_of_modes
252 Eigen::MatrixXd L2errorProjMatrixU(example.Ufield.size(), List_of_modes.rows());
253 Eigen::MatrixXd L2errorProjMatrixT(example.Tfield.size(), List_of_modes.rows());
254
255 // Calculate the coefficients and L2 error and store the error in a matrix for each number of modes
256 for (int i = 0; i < List_of_modes.rows(); i++)
257 {
258 Eigen::MatrixXd coeffU = ITHACAutilities::getCoeffs(example.Ufield,
259 example.Umodes,
260 List_of_modes(i, 0) + example.liftfield.size() + NmodesSUPproj);
261 Eigen::MatrixXd coeffT = ITHACAutilities::getCoeffs(example.Tfield, TLmodes,
262 List_of_modes(i, 0) + example.liftfieldT.size());
263 PtrList<volVectorField> rec_fieldU = ITHACAutilities::reconstructFromCoeff(
264 example.Umodes, coeffU, List_of_modes(i, 0));
265 PtrList<volScalarField> rec_fieldT = ITHACAutilities::reconstructFromCoeff(
266 TLmodes, coeffT, List_of_modes(i, 0) + example.liftfieldT.size());
267 Eigen::MatrixXd L2errorProjU = ITHACAutilities::errorL2Rel(example.Ufield,
268 rec_fieldU);
269 Eigen::MatrixXd L2errorProjT = ITHACAutilities::errorL2Rel(example.Tfield,
270 rec_fieldT);
271 L2errorProjMatrixU.col(i) = L2errorProjU;
272 L2errorProjMatrixT.col(i) = L2errorProjT;
273 }
274
275 // Export the matrix containing the error
276 ITHACAstream::exportMatrix(L2errorProjMatrixU, "L2errorProjMatrixU", "eigen",
277 "./ITHACAoutput/l2error");
278 ITHACAstream::exportMatrix(L2errorProjMatrixT, "L2errorProjMatrixT", "eigen",
279 "./ITHACAoutput/l2error");
280 // Get reduced matrices
281 example.projectSUP("./Matrices", NmodesUproj, NmodesPproj, NmodesTproj,
282 NmodesSUPproj);
283 // Resize the modes for projection
284 example.Tmodes.resize(NmodesTproj);
285 example.Umodes.resize(NmodesUproj);
286 // Online part
287 ReducedUnsteadyBB reduced(example);
288 // Set values of the online solve
289 reduced.nu = 0.00001;
290 reduced.Pr = 0.71;
291 reduced.tstart = 0.0;
292 reduced.finalTime = 10;
293 reduced.dt = 0.005;
294 // No parametrization of velocity on boundary
295 Eigen::MatrixXd vel_now_BC(0, 0);
296
297 // Set the online temperature BC and solve reduced model
298 for (label k = 0; k < (par_on_BC.rows()); k++)
299 {
300 Eigen::MatrixXd temp_now_BC(2, 1);
301 temp_now_BC(0, 0) = par_on_BC(k, 0);
302 temp_now_BC(1, 0) = par_on_BC(k, 1);
303 reduced.solveOnline_sup(temp_now_BC, vel_now_BC, k, par_on_BC.rows());
304 reduced.reconstruct_sup("./ITHACAoutput/ReconstructionSUP", 2);
305 }
306
307 // Performing full order simulation for second parameter set - temp_BC
308 tutorial10 HFonline2(argc, argv);
309 HFonline2.Pnumber = 1;
310 HFonline2.Tnumber = 1;
311 HFonline2.setParameters();
312 HFonline2.mu_range(0, 0) = 0.00001;
313 HFonline2.mu_range(0, 1) = 0.00001;
314 HFonline2.genEquiPar();
315 HFonline2.inletIndexT.resize(2, 1);
316 HFonline2.inletIndexT << 1, 2;
317 HFonline2.startTime = 0.0;
318 HFonline2.finalTime = 10;
319 HFonline2.timeStep = 0.005;
320 HFonline2.writeEvery = 0.01;
321 // Reconstruct the online solution
322 HFonline2.onlineSolveFull(par_on_BC, 1,
323 "./ITHACAoutput/HFonline2");
324 // Performing full order simulation for third parameter set - temp_BC
325 tutorial10 HFonline3(argc, argv);
326 HFonline3.Pnumber = 1;
327 HFonline3.Tnumber = 1;
328 HFonline3.setParameters();
329 HFonline3.mu_range(0, 0) = 0.00001;
330 HFonline3.mu_range(0, 1) = 0.00001;
331 HFonline3.genEquiPar();
332 HFonline3.inletIndexT.resize(2, 1);
333 HFonline3.inletIndexT << 1, 2;
334 HFonline3.startTime = 0.0;
335 HFonline3.finalTime = 10.0;
336 HFonline3.timeStep = 0.005;
337 HFonline3.writeEvery = 0.01;
338 // Reconstruct the online solution
339 HFonline3.onlineSolveFull(par_on_BC, 2,
340 "./ITHACAoutput/HFonline3");
341 // Reading in the high-fidelity solutions for the parameter set
342 // for which the offline solve has been performed
343 example.onlineSolveRead("./ITHACAoutput/Offline/");
344 // Reading in the high-fidelity solutions for the second parameter set
345 example.onlineSolveRead("./ITHACAoutput/HFonline2/");
346 // Reading in the high-fidelity solutions for the second parameter set
347 example.onlineSolveRead("./ITHACAoutput/HFonline3/");
348 // Calculate error between online- and corresponding full order solution
349 Eigen::MatrixXd L2errorMatrixU = ITHACAutilities::errorL2Rel(
350 example.Ufield_on, reduced.UREC);
351 Eigen::MatrixXd L2errorMatrixT = ITHACAutilities::errorL2Rel(
352 example.Tfield_on, reduced.TREC);
353 //Export the matrix containing the error
354 ITHACAstream::exportMatrix(L2errorMatrixU, "L2errorMatrixU", "eigen",
355 "./ITHACAoutput/l2error");
356 ITHACAstream::exportMatrix(L2errorMatrixT, "L2errorMatrixT", "eigen",
357 "./ITHACAoutput/l2error");
358 exit(0);
359}
360
int main(int argc, char *argv[])
Header file of the ITHACAPOD class.
Foam::fvMesh & mesh
Definition createMesh.H:47
Foam::Time & runTime
Definition createTime.H:33
Header file of the ITHACAstream class, it contains the implementation of several methods for input ou...
Header file of the ReducedUnsteadyBB class.
volScalarField alphaEff("alphaEff", turbulence->nu()/Pr+alphat)
Header file of the UnsteadyBB class.
Class for the definition of some general parameters, the parameters must be defined from the file ITH...
IOdictionary * ITHACAdict
Dictionary for input objects from file.
static ITHACAparameters * getInstance()
Gets an instance of ITHACAparameters, to be used if the instance is already existing.
Class where it is implemented a reduced problem for the unsteady Navier-stokes problem.
void reconstruct_sup(fileName folder="./ITHACAOutput/online_rec", int printevery=1)
Method to reconstruct a solution from an online solve with a supremizer stabilisation technique.
scalar Pr
DimensionedScalar Pr;.
Eigen::MatrixXd solveOnline_sup(Eigen::MatrixXd &temp_now_BC, Eigen::MatrixXd &vel_now_BC, int NParaSet=0, int startSnap=0)
Method to perform an online solve using a supremizer stabilisation method.
PtrList< volScalarField > TREC
Reconstructed temperature field.
Implementation of a parametrized full order unsteady Boussinesq problem and preparation of the the ...
Definition UnsteadyBB.H:60
autoPtr< volScalarField > _p_rgh
Shifted Pressure field.
Definition UnsteadyBB.H:131
autoPtr< volScalarField > _T
Temperature field.
Definition UnsteadyBB.H:134
autoPtr< dimensionedScalar > _Prt
dimensionedScalar Prt;
Definition UnsteadyBB.H:146
autoPtr< dimensionedScalar > _Pr
dimensionedScalar Pr;
Definition UnsteadyBB.H:143
PtrList< volScalarField > Tomfield
List of pointers used to form the homogeneous velocity snapshots.
Definition UnsteadyBB.H:97
PtrList< volVectorField > Ufield_on
List of pointers used to form the temperature snapshots matrix.
Definition UnsteadyBB.H:91
void projectSUP(fileName folder, label NUmodes, label NPmodes, label NTmodes, label NSUPmodes)
Project using a supremizer approach.
Definition UnsteadyBB.C:488
autoPtr< fvMesh > _mesh
Mesh.
Definition UnsteadyBB.H:128
PtrList< volScalarField > Tmodes
List of pointers used to form the temperature modes.
Definition UnsteadyBB.H:76
autoPtr< volScalarField > _alphat
dimensionedScalar alphat;
Definition UnsteadyBB.H:149
PtrList< volScalarField > Tfield_on
List of pointers used to form the temperature snapshots matrix.
Definition UnsteadyBB.H:85
PtrList< volScalarField > liftfieldT
List of pointers used to form the list of lifting functions.
Definition UnsteadyBB.H:94
PtrList< volScalarField > Tfield
List of pointers used to form the temperature snapshots matrix.
Definition UnsteadyBB.H:82
scalar startTime
Start Time (initial time to start storing the snapshots)
scalar writeEvery
Time step of the writing procedure.
scalar timeStep
Time step of the simulation.
scalar finalTime
Final time (final time of the simulation and consequently of the acquisition of the snapshots)
scalar nu
Reduced viscosity in case of parametrized viscosity.
PtrList< volVectorField > UREC
Recontructed velocity field.
scalar finalTime
Scalar to store the final time if the online simulation.
scalar tstart
Scalar to store the initial time if the online simulation.
double dt
Scalar to store the time increment.
Eigen::MatrixXi inletIndexT
label Pnumber
Number of parameters.
void assignBC(volVectorField &s, label BC_ind, Vector< double > &value)
Assign Boundary Condition to a volVectorField.
void assignIF(T &s, G &value)
Assign internal field condition.
label Tnumber
Dimension of the training set (used only when gerating parameters without input)
bool offline
Boolean variable, it is 1 if the Offline phase has already been computed, else 0.
void computeLiftT(T &Lfield, T &liftfield, T &omfield)
Virtual function to compute the lifting function.
Eigen::MatrixXd mu
Row matrix of parameters.
Eigen::MatrixXd mu_range
Range of the parameter spaces.
void setParameters()
Set Parameters Problems.
bool podex
Boolean variable, it is 1 if the POD has already been computed, else 0.
void truthSolve()
Perform a TruthSolve.
void genEquiPar()
Generate Equidistributed Numbers.
autoPtr< Time > _runTime
Time.
Definition steadyNS.H:290
volVectorModes Umodes
List of pointers used to form the velocity modes.
Definition steadyNS.H:101
PtrList< volVectorField > Ufield
List of pointers used to form the velocity snapshots matrix.
Definition steadyNS.H:89
PtrList< volVectorField > liftfield
List of pointers used to form the list of lifting functions.
Definition steadyNS.H:110
autoPtr< volVectorField > _U
Velocity field.
Definition steadyNS.H:263
autoPtr< volScalarField > _p
Pressure field.
Definition steadyNS.H:260
volScalarField & p
void onlineSolveRead(fileName folder)
tutorial10(int argc, char *argv[])
volScalarField & T
volScalarField & p_rgh
void onlineSolveFull(Eigen::MatrixXd par_BC, label para_set_BC, fileName folder)
void offlineSolve(Eigen::MatrixXd par_BC)
volVectorField & U
autoPtr< incompressible::turbulenceModel > turbulence
Turbulence model.
Definition unsteadyNS.H:76
fvScalarMatrix & TEqn
Definition TEqn.H:15
volScalarField & alphat
dimensionedScalar & Prt
dimensionedScalar & Pr
void getModes(PtrList< GeometricField< Type, PatchField, GeoMesh > > &snapshots, PtrList< GeometricField< Type, PatchField, GeoMesh > > &modes, word fieldName, bool podex, bool supex, bool sup, label nmodes, bool correctBC)
Computes the bases or reads them for a field.
Definition ITHACAPOD.C:93
void exportMatrix(Eigen::Matrix< T, -1, dim > &matrix, word Name, word type, word folder)
Export the reduced matrices in numpy (type=python), matlab (type=matlab) and txt (type=eigen) format ...
List< Eigen::MatrixXd > readMatrix(word folder, word mat_name)
Read a three dimensional matrix from a txt file in Eigen format.
void read_fields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, word Name, fileName casename, int first_snap, int n_snap)
Function to read a list of fields from the name of the field and casename.
Eigen::VectorXd getCoeffs(GeometricField< Type, PatchField, GeoMesh > &snapshot, PtrList< GeometricField< Type, PatchField, GeoMesh > > &modes, label Nmodes, bool consider_volumes)
Projects a snapshot on a basis function and gets the coefficients of the projection.
PtrList< GeometricField< Type, PatchField, GeoMesh > > reconstructFromCoeff(PtrList< GeometricField< Type, PatchField, GeoMesh > > &modes, Eigen::MatrixXd &coeff_matrix, label Nmodes)
Exact reconstruction using a certain number of modes for a list of fields and the projection coeffici...
double errorL2Rel(GeometricField< T, fvPatchField, volMesh > &field1, GeometricField< T, fvPatchField, volMesh > &field2, List< label > *labels)
Computes the relative error between two geometric Fields in L2 norm.
bool check_folder(word folder)
Checks if a folder exists.
void createSymLink(word folder)
Creates symbolic links to 0, system and constant.
simpleControl simple(mesh)
label i
Definition pEqn.H:46