1#include "usmsrProblem.H"
2#include "ReducedUnsteadyMSR.H"
4#include "LRSensitivity.H"
18 explicit msr(
int argc,
char* argv[])
47 volScalarField& prec1;
48 volScalarField& prec2;
49 volScalarField& prec3;
50 volScalarField& prec4;
51 volScalarField& prec5;
52 volScalarField& prec6;
53 volScalarField& prec7;
54 volScalarField& prec8;
67 int NUproj = para->ITHACAdict->lookupOrDefault<
int>(
"NUproj", 2);
68 int NPproj = para->ITHACAdict->lookupOrDefault<
int>(
"NPproj", 2);
69 int NFluxproj = para->ITHACAdict->lookupOrDefault<
int>(
"NFluxproj", 2);
70 int NPrecproj1 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj1", 2);
71 int NPrecproj2 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj2", 2);
72 int NPrecproj3 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj3", 2);
73 int NPrecproj4 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj4", 2);
74 int NPrecproj5 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj5", 2);
75 int NPrecproj6 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj6", 2);
76 int NPrecproj7 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj7", 2);
77 int NPrecproj8 = para->ITHACAdict->lookupOrDefault<
int>(
"NPrecproj8", 2);
78 int NTproj = para->ITHACAdict->lookupOrDefault<
int>(
"NTproj", 2);
79 int NDecproj1 = para->ITHACAdict->lookupOrDefault<
int>(
"NDecproj1", 2);
80 int NDecproj2 = para->ITHACAdict->lookupOrDefault<
int>(
"NDecproj2", 2);
81 int NDecproj3 = para->ITHACAdict->lookupOrDefault<
int>(
"NDecproj3", 2);
82 int NCproj = para->ITHACAdict->lookupOrDefault<
int>(
"NCproj", 2);
84 double r1 = _beta1().value() / _betaTot().value();
85 double r2 = _beta2().value() / _betaTot().value();
86 double r3 = _beta3().value() / _betaTot().value();
87 double r4 = _beta4().value() / _betaTot().value();
88 double r5 = _beta5().value() / _betaTot().value();
89 double r6 = _beta6().value() / _betaTot().value();
90 double r7 = _beta7().value() / _betaTot().value();
91 double r8 = _beta8().value() / _betaTot().value();
97 List<scalar> mu_now(3);
99 for (
int i = 0; i <
mu.cols(); i++)
101 _nu().value() =
mu(0, i);
103 _betaTot().value() =
mu(1, i);
104 _beta1().value() = r1 *
mu(1, i);
105 _beta2().value() = r2 *
mu(1, i);
106 _beta3().value() = r3 *
mu(1, i);
107 _beta4().value() = r4 *
mu(1, i);
108 _beta5().value() = r5 *
mu(1, i);
109 _beta6().value() = r6 *
mu(1, i);
110 _beta7().value() = r7 *
mu(1, i);
111 _beta8().value() = r8 *
mu(1, i);
112 _decLam3().value() =
mu(2, i);
113 mu_now[0] =
mu(0, i);
114 mu_now[1] =
mu(1, i);
115 mu_now[2] =
mu(2, i);
128class Tmlocal :
public Tm
131 explicit Tmlocal(
int argc,
char* argv[],
int Nsampled)
133 Tm(argc, argv, Nsampled),
140class Ploct:
public Ptot_time
143 explicit Ploct(
int argc,
char* argv[],
int Nsampled)
145 Ptot_time(argc, argv, Nsampled),
149 volScalarField& powerDens;
152class Plocal:
public Ptot
155 explicit Plocal(
int argc,
char* argv[],
int Nsampled)
157 Ptot(argc, argv, Nsampled),
161 volScalarField& powerDens;
164class Tmloct:
public Tm_time
167 explicit Tmloct(
int argc,
char* argv[],
int Nsampled)
169 Tm_time(argc, argv, Nsampled),
176int main(
int argc,
char* argv[])
179 msr prova(argc, argv);
181 prova.inletIndex.resize(1, 2);
182 prova.inletIndex(0, 0) = 0;
183 prova.inletIndex(0, 1) = 0;
184 prova.inletIndexT.resize(4, 1);
185 prova.inletIndexT(0, 0) = 0;
186 prova.inletIndexT(1, 0) = 1;
187 prova.inletIndexT(2, 0) = 2;
188 prova.inletIndexT(3, 0) = 3;
192 prova.setParameters();
193 int central =
static_cast<int>(prova.Tnumber / 2);
194 double nu0 = 2.46E-06;
195 double betatot0 = 3.218E-05;
196 double dlam30 = 3.58e-04;
198 prova.mu_range(0, 0) = 1.1 * nu0;
199 prova.mu_range(0, 1) = 0.9 * nu0;
200 prova.mu_range(1, 0) = 1.1 * betatot0;
201 prova.mu_range(1, 1) = 0.9 * betatot0;
202 prova.mu_range(2, 0) = 1.1 * dlam30;
203 prova.mu_range(2, 1) = 0.9 * dlam30;
206 prova.mu(0, central) = nu0;
207 prova.mu(1, central) = betatot0;
208 prova.mu(2, central) = dlam30;
209 double tstart = std::time(0);
210 Eigen::MatrixXd arange(prova.Pnumber, 2);
211 Eigen::MatrixXd mu_f = prova.mu;
213 if (prova.offline ==
false)
215 for (
int i = 0; i < prova.Pnumber; i++)
217 arange(i, 0) = prova.mu.row(i).minCoeff();
218 arange(i, 1) = prova.mu.row(i).maxCoeff();
231 prova.offlineSolve();
232 prova._nu().value() = nu0;
233 prova.change_viscosity(nu0);
234 double tend = std::time(0);
235 double deltat_offline;
236 deltat_offline = difftime(tend, tstart);
239 if (prova.offline ==
false)
241 std::ofstream diff_t(
"./ITHACAoutput/t_offline=" + name(deltat_offline));
244 Info <<
"Offline stage time= " << deltat_offline << endl;
254 prova.msrgetModesEVD();
256 Eigen::VectorXi NPrec(8);
257 NPrec(0) = prova.NPrecproj1;
258 NPrec(1) = prova.NPrecproj2;
259 NPrec(2) = prova.NPrecproj3;
260 NPrec(3) = prova.NPrecproj4;
261 NPrec(4) = prova.NPrecproj5;
262 NPrec(5) = prova.NPrecproj6;
263 NPrec(6) = prova.NPrecproj7;
264 NPrec(7) = prova.NPrecproj8;
265 Eigen::VectorXi NDec(3);
266 NDec(0) = prova.NDecproj1;
267 NDec(1) = prova.NDecproj2;
268 NDec(2) = prova.NDecproj3;
269 prova.projectPPE(
"./Matrices", prova.NUproj, prova.NPproj, prova.NFluxproj,
270 NPrec, prova.NTproj, NDec, prova.NCproj);
272 double umedio = 2.46E-03;
274 Eigen::MatrixXd Twall(4, 2);
283 ridotto.finalTime = 10;
286 Eigen::MatrixXd vel_now(1, 1);
287 vel_now(0, 0) = umedio;
289 Eigen::MatrixXd temp_now = Twall;
291 Eigen::VectorXd mu_on(3);
292 ridotto.nu = mu_f(0, central);
293 ridotto.btot = mu_f(1, central);
294 ridotto.b1 = prova.r1 * ridotto.btot;
295 ridotto.b2 = prova.r2 * ridotto.btot;
296 ridotto.b3 = prova.r3 * ridotto.btot;
297 ridotto.b4 = prova.r4 * ridotto.btot;
298 ridotto.b5 = prova.r5 * ridotto.btot;
299 ridotto.b6 = prova.r6 * ridotto.btot;
300 ridotto.b7 = prova.r7 * ridotto.btot;
301 ridotto.b8 = prova.r8 * ridotto.btot;
302 ridotto.dl3 = mu_f(2, central);
305 mu_on(0) = ridotto.nu;
306 mu_on(1) = ridotto.btot;
307 mu_on(2) = ridotto.dl3;
308 ridotto.solveOnline(vel_now, temp_now, mu_on, ref_start);
309 ridotto.reconstructAP(
"./ITHACAoutput/Reconstruction/", 10);
316 Eigen::MatrixXd err(10, tf);
317 Eigen::MatrixXd TmFOM(prova.Tnumber, tf);
318 Eigen::MatrixXd PtotFOM(prova.Tnumber, tf);
319 Eigen::MatrixXd TmROM(prova.Tnumber, tf);
320 Eigen::MatrixXd PtotROM(prova.Tnumber, tf);
321 Info <<
"Computing errors on reference case..." << endl;
323 for (
int i = ref_start; i < ref_start + tf; i++)
329 ridotto.PREC1REC[c]);
331 ridotto.PREC4REC[c]);
333 ridotto.PREC8REC[c]);
340 ridotto.POWERDENSREC[c]);
346 Info <<
"End" << endl;
349 Info <<
"computing FOM output..." << endl;
355 for (
int i = 0; i < prova.Tfield.size(); i++)
357 tmpt = prova.Tfield[i].weightedAverage(prova._mesh().V()).value();
358 tmpp = fvc::domainIntegrate(prova.PowerDensfield[i]).value();
362 TmFOM(rig, col) = tmpt;
363 PtotFOM(rig, col) = tmpp;
366 else if (col == tf_fom)
377 Info <<
"End" << endl;
383 std::string folder = {
"./ITHACAoutput/ROMOutput/"};
385 for (
int i = 0; i < prova.Tnumber; i++)
387 ridotto.nu = mu_f(0, i);
388 ridotto.btot = mu_f(1, i);
389 ridotto.b1 = prova.r1 * ridotto.btot;
390 ridotto.b2 = prova.r2 * ridotto.btot;
391 ridotto.b3 = prova.r3 * ridotto.btot;
392 ridotto.b4 = prova.r4 * ridotto.btot;
393 ridotto.b5 = prova.r5 * ridotto.btot;
394 ridotto.b6 = prova.r6 * ridotto.btot;
395 ridotto.b7 = prova.r7 * ridotto.btot;
396 ridotto.b8 = prova.r8 * ridotto.btot;
397 ridotto.dl3 = mu_f(2, i);
398 mu_on(0) = ridotto.nu;
399 mu_on(1) = ridotto.btot;
400 mu_on(2) = ridotto.dl3;
401 folder.append(std::to_string(i));
402 ridotto.solveOnline(vel_now, temp_now, mu_on, ref_start);
403 ridotto.reconstructAP(folder, 50);
404 folder = {
"./ITHACAoutput/ROMOutput/"};
405 ridotto.clearFields();
409 Tmloct Tmedia(argc, argv, prova.Tnumber);
410 Ploct Ptotale(argc, argv, prova.Tnumber);
413 for (
int i = 0; i < tf; i++)
415 Tmedia.buildMO(folder, i);
416 Ptotale.buildMO(folder, i);
417 TmROM.col(c) = Tmedia.modelOutput;
418 PtotROM.col(c) = Ptotale.modelOutput;
429 folder = {
"./ITHACAoutput/Sensitivity/ModelOutput/"};
430 int Nparameters = prova.Pnumber;
431 std::vector<std::string> pdflist = {
"normal",
"normal",
"normal"};
432 int samplingPoints = 1000;
435 analisi.trainingRange = arange;
437 Eigen::MatrixXd Mp(Nparameters, 2);
439 Mp(0, 1) = nu0 * 0.1 / 3;
441 Mp(1, 1) = betatot0 * 0.1 / 3;
443 Mp(2, 1) = dlam30 * 0.1 / 3;
445 analisi.buildSamplingSet(pdflist, Mp);
451 tstart = std::time(0);
452 ridotto.clearFields();
454 for (
int j = 0; j < samplingPoints; j++)
456 ridotto.nu = analisi.MatX(j, 0);
457 ridotto.btot = analisi.MatX(j, 1);
458 ridotto.b1 = prova.r1 * ridotto.btot;
459 ridotto.b2 = prova.r2 * ridotto.btot;
460 ridotto.b3 = prova.r3 * ridotto.btot;
461 ridotto.b4 = prova.r4 * ridotto.btot;
462 ridotto.b5 = prova.r5 * ridotto.btot;
463 ridotto.b6 = prova.r6 * ridotto.btot;
464 ridotto.b7 = prova.r7 * ridotto.btot;
465 ridotto.b8 = prova.r8 * ridotto.btot;
466 ridotto.dl3 = analisi.MatX(j, 2);
467 mu_on(0) = ridotto.nu;
468 mu_on(1) = ridotto.btot;
469 mu_on(2) = ridotto.dl3;
470 folder.append(std::to_string(counter));
471 ridotto.solveOnline(vel_now, temp_now, mu_on, ref_start);
472 ridotto.reconstructAP(folder, 1000);
473 folder = {
"./ITHACAoutput/Sensitivity/ModelOutput/"};
474 ridotto.clearFields();
479 int Ntot = samplingPoints;
480 double deltat_online = difftime(tend, tstart);
482 std::ofstream diff_ton(
"./ITHACAoutput/t_onlineSA_" + name(Ntot) +
"=" + name(
485 Tmlocal TmediaSA(argc, argv, Ntot);
486 Plocal PtotSA(argc, argv, Ntot);
488 TmediaSA.buildMO(folder);
489 PtotSA.buildMO(folder);
491 analisi.M = autoPtr<FofM>(& TmediaSA);
493 analisi.load_output();
496 Info <<
"Mean value and variance of the output:" << endl;
497 Info << analisi.Ey <<
"\t" << analisi.Vy << endl;
498 Info <<
"-------------" << endl;
501 Info <<
"analisi regression coefficients:" << endl;
502 Info << analisi.betas << endl;
503 Info <<
"-------------" << endl;
505 analisi.assessQuality();
506 Info <<
"quality: " << analisi.QI << endl;
508 Eigen::MatrixXd saveyout(samplingPoints, 1);
509 Eigen::MatrixXd saveymodel(samplingPoints, 1);
510 saveyout.col(0) = analisi.y;
511 saveymodel.col(0) = analisi.ylin;
514 Eigen::MatrixXd coeffsT(4, 3);
516 coeffsT(0, 0) = analisi.Ey;
517 coeffsT(0, 1) = analisi.Vy;
518 coeffsT(0, 2) = analisi.QI;
519 coeffsT.row(1) = analisi.EX;
520 coeffsT.row(2) = analisi.VX;
521 coeffsT.row(3) = analisi.betas;
524 analisi.M = autoPtr<FofM>(& PtotSA);
527 analisi.load_output();
529 Info <<
"Mean value and variance of the output:" << endl;
530 Info << analisi.Ey <<
"\t" << analisi.Vy << endl;
531 Info <<
"-------------" << endl;
533 Info <<
"analisi regression coefficients:" << endl;
534 Info << analisi.betas << endl;
535 Info <<
"-------------" << endl;
536 analisi.assessQuality();
537 Info <<
"quality: " << analisi.QI << endl;
538 Eigen::MatrixXd saveyoutP(samplingPoints, 1);
539 Eigen::MatrixXd saveymodelP(samplingPoints, 1);
540 saveyoutP.col(0) = analisi.y;
541 saveymodelP.col(0) = analisi.ylin;
544 Eigen::MatrixXd coeffsP(4, 3);
546 coeffsP(0, 0) = analisi.Ey;
547 coeffsP(0, 1) = analisi.Vy;
548 coeffsP(0, 2) = analisi.QI;
549 coeffsP.row(1) = analisi.EX;
550 coeffsP.row(2) = analisi.VX;
551 coeffsP.row(3) = analisi.betas;
Header file of the ITHACAstream class, it contains the implementation of several methods for input ou...
Class for the definition of some general parameters, the parameters must be defined from the file ITH...
static ITHACAparameters * getInstance()
Gets an instance of ITHACAparameters, to be used if the instance is already existing.
autoPtr< volScalarField > _powerDens
List of pointers to power density field.
autoPtr< volScalarField > _powerDens
List of pointers to power density field.
autoPtr< volScalarField > _T
List of pointers to temperature field.
autoPtr< volScalarField > _T
List of pointers to temperature field.
void change_viscosity(double mu)
method to change the viscosity in UEqn
void readMSRfields()
Method to read all the fieds of the MSR problem in the offline folder, it also reads mu_samples matri...
void restart()
method to set all fields back to values in 0 folder
bool offline
Boolean variable, it is 1 if the Offline phase has already been computed, else 0.
Eigen::MatrixXd mu
Row matrix of parameters.
void truthSolve()
Perform a TruthSolve.
usmsrProblem()
Construct Null.
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.
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.