Loading...
Searching...
No Matches
Moving_Cylinder.C
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12License
13 This file is part of ITHACA-FV
14 ITHACA-FV is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18 ITHACA-FV is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
24Description
25 Example of an unsteady NS Reduction Problem
26SourceFiles
27 22fsi.C
28\*---------------------------------------------------------------------------*/
29
30#include "fsiBasic.H"
31#include "ITHACAPOD.H"
32//#include "ReducedSimpleSteadyNS.H"
33#include "ITHACAstream.H"
34#include "dynamicFvMesh.H"
35#include "ReducedProblem.H"
36#include "ReducedFsi.H"
37#include <chrono>
38#include<math.h>
39#include<iomanip>
40#include "pointConstraints.H"
41#include "mathematicalConstants.H"
42//#include "zoneMotion.H"
43
44
45class tutorial22: public fsiBasic
46{
47 public:
48 tutorial22(int argc, char* argv[])
49 : fsiBasic(argc, argv),
50 U(_U()),
51 p(_p()),
53 {
54 //point0 = meshPtr().points();
55 }
57 volVectorField& U;
59 volScalarField& p;
60 pointVectorField& pd;
62 //vectorField point0;
63 void offlineSolve(word folder = "./ITHACAoutput/Offline/")
64 {
65 if (offline )
66 {
67 ITHACAstream::readMiddleFields(Ufield, U, folder);
70 //ITHACAstream::readMiddleFields(Efield, E, folder);
71 //mu_samples = ITHACAstream::readMatrix("./parsOff_mat.txt");
72 }
73 else if (!offline)
74 {
75 for (label i = 0; i < mu.rows(); i++)
76 {
77 //std::clock_t startOff;
78 //startOff = std::clock();
79 //folderN = i;
80 Info << "Current mu = " << mu(i, 0) << endl;
81 //change_viscosity(mu(i, 0));
82 //change_stiffness(mu(i,0));
83 //meshPtr().movePoints(Mesh0->points());
84 word param_name = "damping";
85 updateStiffnessAndRebuildSolver(mu(i, 0), param_name);
86 //exit(0);
87 //assignIF(_U(), Uinl);
88 //folder = folder + "/" + name(i+1);
89 startTime = 0;
90 finalTime = 30;
91 timeStep = 0.003; //0.0025; //4e-03; // ok dt=0.001
92 writeEvery = 1e-01;
93 truthSolve(i, folder);
94 word localFolder = folder + "../" + "/DataFromFoam_" + name(i + 1);
95 prepareFoamData(localFolder);
96 restart();
98 fomforcex.clear();
99 fomforcey.clear();
100 //centerofmassx.clear();
101 centerofmassy.clear();
102 //centerofmassz.clear();
103 //this->Ufield.last().checkIn(this->meshes[i]);
104 //this->Ufield.last().checkIn();
105 // ITHACAPOD::getModes(this->Ufield, this->Umodes, this->_U().name(),
106 // this->podex, 0, 0, 10);
107 // exit(0);
108 }
109 }
110 }
111};
112
113
114
115
116/*------------------------------------------------------------------------------------------*\
117 Starting the MAIN
118\*------------------------------------------------------------------------------------------*/
119int main(int argc, char* argv[])
120{
121 // Construct the tutorial object
122 tutorial22 example(argc, argv);
123 //std::clock_t startOff;
124 //double durationOff;
125 // Read some parameters from file
126 ITHACAparameters* para = ITHACAparameters::getInstance(example.meshPtr(),
127 example._runTime());
128 //Eigen::MatrixXd parOff;
129 // Read the par file where the parameters are stored
130 std::ifstream exFileOff("./parsOff_mat.txt");
131
132 if (exFileOff)
133 {
134 example.mu = ITHACAstream::readMatrix("./parsOff_mat.txt");
135 }
136
137 int NmodesUout = para->ITHACAdict->lookupOrDefault<int>("NmodesUout", 40);
138 int NmodesPout = para->ITHACAdict->lookupOrDefault<int>("NmodesPout", 40);
139 int NmodesDout = para->ITHACAdict->lookupOrDefault<int>("NmodesDout", 40);
140 int NmodesUproj = para->ITHACAdict->lookupOrDefault<int>("NmodesUproj", 15);
141 int NmodesPproj = para->ITHACAdict->lookupOrDefault<int>("NmodesPproj", 5);
142 int NmodesDproj = para->ITHACAdict->lookupOrDefault<int>("NmodesDproj", 1);
143 // //Perform the offline solve
144 //startOff= std::clock();
145 //Info << example.Mesh0->checkMesh(true) << endl;
146 //exit(0);
147 //std::cerr << "debug point 3" << endl;
148 example.offlineSolve();
149 //std::cerr << "debug point 4" << endl;
150 //example.meshPtr().movePoints( example.Mesh0->points());
151
152 // bool meshesDiffer = false;
153 // Info << example.Mesh0->points()[50] << endl;
154 // dynamicFvMesh& newmesh = example.meshPtr();
155 // //Info << example.Ufield[0].mesh().points()[0] << endl;
156 // pointField newPoints = newmesh.points()+ example.Dfield[50];
157 // newmesh.movePoints(newPoints );
158
159 // Info << newPoints[50] << endl;
160
161 // forAll(newPoints, i)
162 // {
163 // if (newmesh.points()[i] != example.Mesh0->points()[i]) // or a tolerance like 1e-10
164 // {
165 // meshesDiffer = true;
166 // break;
167 // }
168 // }
169 // if (meshesDiffer)
170 // Info << "Meshes differ in point locations." << endl;
171 // else
172 // Info << "Meshes are geometrically identical." << endl;
173 // //Info << "The Offline phase duration is equal to " << durationOff << endl;
174 // exit(0);
175
176 //example.meshPtr().movePoints(example.Dfield[0]);
177 if (example.podex == 0 )
178 {
179 ITHACAPOD::getModes(example.Ufield, example.Umodes, example._U().name(),
180 example.podex, 0, 0, NmodesUout);
181 //ITHACAPOD::getModes(UfieldEnrich, online.Umodes, online._U().name(),
182 // example.podex, 0, 0, NmodesUout);
183 ITHACAPOD::getModes(example.Pfield, example.Pmodes, example._p().name(),
184 example.podex, 0, 0, NmodesPout);
185 ITHACAPOD::getModes(example.Dfield, example.Dmodes,
186 example._pointDisplacement().name(),
187 example.podex, 0, 0, NmodesDout);
188 }
189 else
190 {
191 ITHACAstream::read_fields(example.Umodes, example._U(),
192 "./ITHACAoutput/POD/");
193 ITHACAstream::read_fields(example.Pmodes, example._p(),
194 "./ITHACAoutput/POD/");
195 ITHACAstream::read_fields(example.Dmodes, example._pointDisplacement(),
196 "./ITHACAoutput/POD/");
197 }
198
199 example.loadCentreOfMassY();
200 //Info << example.Dfield.size() << endl;
202 example.coeffL2 = ITHACAutilities::getCoeffs(example.Dfield,
203 example.Dmodes,
204 NmodesDproj, false);
205 //exit(0);
206 Eigen::MatrixXd parsOn;
207 std::ifstream exFileOn("./parsOn_mat.txt");
208
209 if (exFileOn)
210 {
211 parsOn = ITHACAstream::readMatrix("./parsOn_mat.txt");
212 }
213
215 word test_folder = "./ITHACAoutput/TestingOff/";
216
218 if (!ITHACAutilities::check_folder(test_folder))
219 {
220 tutorial22 testkOff(argc, argv);
221 testkOff.mu = parsOn;
222 //Perform the offline solve
223 testkOff.offline = false;
224
225 for (label k = 0; k < parsOn.rows(); k++)
226 {
227 //Info << "Current mu = " << parsOn(k, 0) << endl;
228 //example.meshPtr().movePoints( example.Mesh0->points());
229 word param_name = "damping";
230 testkOff.updateStiffnessAndRebuildSolver(parsOn(k, 0), param_name);
231 testkOff.startTime = 0;
232 testkOff.finalTime = 30;
233 testkOff.timeStep = 0.003; //0.0025; //4e-03; // ok dt=0.001
234 testkOff.writeEvery = 1e-01;
235 testkOff.truthSolve(k, test_folder);
236 word localFolder = test_folder + "/DataFromFoam_" + name(k + 1);
237 testkOff.prepareFoamData(localFolder);
238 testkOff.restart();
239 testkOff.Ufield.clear();
240 testkOff.Pfield.clear();
241 testkOff.Dfield.clear();
242 testkOff.fomforcex.clear();
243 testkOff.fomforcey.clear();
244 //testkOff.centerofmassx.clear();
245 testkOff.centerofmassy.clear();
246 //testkOff.centerofmassz.clear();
247 }
248
249 ITHACAutilities::createSymLink("./0", test_folder);
250 ITHACAutilities::createSymLink("./system", test_folder);
251 ITHACAutilities::createSymLink("./constant", test_folder);
252 testkOff.offline = true;
253 }
254
255 /*-----------------------------------------------------------*\
256 Starting the online part
257 \*------------------------------------------------------------*/
259 ReducedFsi reduced(example);
260
261 //reducedBasicFsi reduced(example)
262 // //exit(0);
263 // reduced.startTime = example.startTime;
264 // reduced.finalTime = example.finalTime;
265 // reduced.timeStep = example.timeStep;
266 // reduced.writeEvery = example.writeEvery;
267 //Perform the online solutions
268 //example.restart();
269 // std::clock_t startOn;
270 // double durationOn;
271 // startOn = std::clock();
272 if (exFileOn)
273 {
274 parsOn = ITHACAstream::readMatrix("./parsOn_mat.txt");
275 }
276
277 word folder = "./ITHACAoutput/Online/";
278 ITHACAutilities::createSymLink("./0", folder);
279 ITHACAutilities::createSymLink("./system", folder);
280 ITHACAutilities::createSymLink("./constant", folder);
281
282 for (label i = 0; i < parsOn.rows(); i++)
283 {
284 // Info << "Current mu = "
285 // << parsOn(i,0) << endl;
286 //example.meshPtr().movePoints( example.Mesh0->points());
287 word param_name = "damping";
288 example.updateStiffnessAndRebuildSolver(parsOn(i, 0), param_name);
289 reduced.startTime = 0; //example.startTime;
290 reduced.finalTime = 30; //example.finalTime;;
291 reduced.timeStep = 0.003; //example.timeStep;
292 reduced.writeEvery = 1e-1; //example.writeEvery;
294 reduced.solveOnline_Pimple(NmodesUproj,
295 NmodesPproj,
296 NmodesDproj,
297 folder);
298 word localFolder = folder + name(i + 1);
299
300 for (int k = 0; k < reduced.UredFields.size(); ++k)
301 {
302 ITHACAstream::exportSolution(reduced.UredFields[k], name(k + 1),
303 localFolder);
304 ITHACAstream::exportSolution(reduced.PredFields[k], name(k + 1),
305 localFolder);
306 ITHACAstream::exportSolution(reduced.Dfield[k], name(k + 1),
307 localFolder);
308 ITHACAstream::writePoints(reduced.ListOfpoints[k], localFolder,
309 name(k + 1) + "/polyMesh/");
310 }
311
312 word DataRom = folder + "../" + "/DataFromRom_" + name(i + 1);
313 reduced.prepareRomData(DataRom);
315 example.restart();
316 reduced.UredFields.clear();
317 reduced.PredFields.clear();
318 reduced.Dfield.clear();
319 reduced.romforcey.clear();
320 reduced.romforcex.clear();
321 reduced.centerofmassy.clear();
322 }
323
324 //durationOn = (std::clock() - startOn);
325 //Info << "The Online phase duration is equal to " << durationOn << endl;
326 return 0;
327 Eigen::MatrixXd errL2U = ITHACAutilities::errorL2Rel(example.Ufield,
328 reduced.UredFields);
329 Info <<
330 "======================= errL2U completed================================" <<
331 "\n";
332 Eigen::MatrixXd errL2P = ITHACAutilities::errorL2Rel(example.Pfield,
333 reduced.PredFields);
334 cnpy::save(errL2U, "./ITHACAoutput/DataFromRom/errL2U_" + name(
335 NmodesUproj) + "_" + name(NmodesPproj) + ".npy");
336 cnpy::save(errL2P, "./ITHACAoutput/DataFromRom/errL2P_" + name(
337 NmodesUproj) + "_" + name(NmodesPproj) + ".npy");
338 return 0;
339}
Header file of the ITHACAPOD class.
Header file of the ITHACAstream class, it contains the implementation of several methods for input ou...
Header file of the reducedProblem class.
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.
double e
RBF functions radius.
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).
Implementation of a parametrized full order unsteady NS problem and preparation of the the reduced ...
Definition fsiBasic.H:76
void updateStiffnessAndRebuildSolver(scalar &newMu, word param_name="stiffness")
Definition fsiBasic.C:524
autoPtr< pointVectorField > _pointDisplacement
pointDisplacement field
Definition fsiBasic.H:115
List< scalar > fomforcey
List to save lift and drag forces.
Definition fsiBasic.H:117
void prepareFoamData(const word &outputPath)
Prepare data such forces and centre of mass.
Definition fsiBasic.C:461
PtrList< pointVectorField > Dfield
List of pointers used to form the displacement snapshots matrix.
Definition fsiBasic.H:108
void restart()
method to set all fields back to values in 0 folder
Definition fsiBasic.C:376
fsiBasic()
Construct Null.
Definition fsiBasic.C:33
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.
PtrList< volScalarField > Pfield
List of pointers used to form the pressure snapshots matrix.
Definition steadyNS.H:86
autoPtr< volVectorField > _U
Velocity field.
Definition steadyNS.H:272
void offlineSolve(word folder="./ITHACAoutput/Offline/")
Initial coordinates of the grid points.
volVectorField & U
Velocity field.
Definition offline.C:88
volScalarField & p
Pressure field.
Definition offline.C:89
Header file of the fsiBasic class.
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 writePoints(pointField points, fileName folder, fileName subfolder)
Write points of a mesh to a file.
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
void readMiddleFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, GeometricField< Type, PatchField, GeoMesh > &field, fileName casename)
Funtion to read a list of volVectorField from name of the field including all the intermediate snapsh...
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.
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.