Loading...
Searching...
No Matches
offline.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8 * In real Time Highly Advanced Computational Applications for Finite Volumes
9 * Copyright (C) 2017 by the ITHACA-FV authors
10-------------------------------------------------------------------------------
11License
12 This file is part of ITHACA-FV
13
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
19 ITHACA-FV is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public License
25 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
26
27Description
28 Example of NS-Stokes Reduction Problem
29
30\*---------------------------------------------------------------------------*/
31#include "steadyNS.H"
32#include "UnsteadyNSTurb.H"
33#include <math.h>
34
35#include "ITHACAutilities.H"
36#include "reductionProblem.H"
37
38#include "ReducedSteadyNS.H"
40#include "ReducedUnsteadyNS.H"
41
42#include <Eigen/Dense>
43#include <Eigen/Core>
44#include "forces.H"
45#include "IOmanip.H"
46#include "forces.H"
47#include "forceCoeffs.H"
48#include "fvCFD.H"
49#include "fvOptions.H"
50#include "simpleControl.H"
51#include "Foam2Eigen.H"
52#include <chrono>
53#include "ITHACAstream.H"
54#include "ITHACAPOD.H"
55#include "EigenFunctions.H"
56#include <chrono>
57#include <Eigen/SVD>
58#include <Eigen/SparseLU>
59
60#include <stdio.h>
61#include <sys/types.h>
62#include <dirent.h>
63#include <algorithm>
64#include <fstream>
65#include <string>
66#include <stdexcept>
67#include <sstream>
68#include <vector>
69#include <cstdio>
70#include <typeinfo>
71#include <iostream>
72#include <cassert>
73#include <zlib.h>
74
75
77{
78 public:
79 explicit tutorial22(int argc, char* argv[])
80 :
81 UnsteadyNSTurb(argc, argv),
82 U(_U()),
83 p(_p()),
84 nut(_nut())
85 {}
86
87 // Relevant Fields
88 volVectorField& U;
89 volScalarField& p;
90 volScalarField& nut;
91
92 void offlineSolve(std::string offlinepath)
93 {
94 Vector<double> inl(0, 0, 0);
95 List<scalar> mu_now(1);
96 mu_now[0] = 1e-05;
97
98 if ((offline) && (ITHACAutilities::check_folder(offlinepath) == true))
99 {
100 ITHACAstream::read_fields(Ufield, U, offlinepath);
101 ITHACAstream::read_fields(Pfield, p, offlinepath);
103 }
104 else
105 {
106 truthSolve(mu_now, offlinepath);
107 }
108 }
109
110 Eigen::MatrixXd vectorTensorMult(Eigen::VectorXd g, Eigen::Tensor<double, 3> c,
111 Eigen::VectorXd a)
112 {
113 int prodDim = c.dimension(0);
114 Eigen::MatrixXd prod;
115 prod.resize(prodDim, 1);
116
117 for (int i = 0; i < prodDim; i++)
118 {
119 prod(i, 0) = g.transpose() *
120 Eigen::SliceFromTensor(c, 0, i) * a;
121 }
122
123 return prod;
124 }
125};
126
127
128void supremizer_approach(tutorial22& example);
129void poisson_approach(tutorial22& example);
130
131/*---------------------------------------------------------------------------*\
132 Starting the MAIN
133\*---------------------------------------------------------------------------*/
134
135int main(int argc, char* argv[])
136{
137 if (argc == 1)
138 {
139 std::cout << "Pass 'supremizer' or 'poisson' as first arguments."
140 << std::endl;
141 exit(0);
142 }
143
144 // process arguments removing "offline" or "online" keywords
145 int argc_proc = argc - 1;
146 char* argv_proc[argc_proc];
147 argv_proc[0] = argv[0];
148
149 if (argc > 2)
150 {
151 std::copy(argv + 2, argv + argc, argv_proc + 1);
152 }
153
154 argc--;
155 tutorial22 example(argc, argv);
156
157 if (std::strcmp(argv[1], "supremizer") == 0)
158 {
159 // perform the offline stage, extracting the modes from the snapshots' dataset corresponding to parOffline
160 supremizer_approach(example);
161 }
162 else if (std::strcmp(argv[1], "poisson") == 0)
163 {
164 poisson_approach(example);
165 }
166 else
167 {
168 std::cout << "Pass supremizer, poisson" << std::endl;
169 }
170
171 exit(0);
172}
173
175
176{
177 word filename("./par");
178 Eigen::VectorXd par;
179 example.inletIndex.resize(1, 2);
180 example.inletIndex << 0, 0;
181 example.inletIndexT.resize(1, 1);
182 example.inletIndexT << 1;
184 example._runTime());
185 int NmodesU = para->ITHACAdict->lookupOrDefault<int>("NmodesU", 5);
186 int NmodesP = para->ITHACAdict->lookupOrDefault<int>("NmodesP", 5);
187 int NmodesSUP = para->ITHACAdict->lookupOrDefault<int>("NmodesSUP", 5);
188 int NmodesNUT = para->ITHACAdict->lookupOrDefault<int>("NmodesNUT", 5);
189 int NmodesProject = para->ITHACAdict->lookupOrDefault<int>("NmodesProject", 5);
190 int NmodesMatrixRec = para->ITHACAdict->lookupOrDefault<int>("NmodesMatrixRec",
191 5);
192 double penaltyFactor =
193 para->ITHACAdict->lookupOrDefault<double>("penaltyFactor", 5);
194 double U_BC = para->ITHACAdict->lookupOrDefault<double>("U_BC", 0.001);
195 double romStartTime = para->ITHACAdict->lookupOrDefault<double>("romStartTime",
196 0);
197 double romEndTime = para->ITHACAdict->lookupOrDefault<double>("romEndTime", 3);
198 double romTimeStep = para->ITHACAdict->lookupOrDefault<double>("romTimeStep",
199 0.001);
200 double e = para->ITHACAdict->lookupOrDefault<double>("RBFradius", 1);
201 example.startTime = 79.992;
202 example.finalTime = 99.996;
203 example.timeStep = 0.0002;
204 example.writeEvery = 0.004;
205 example.offlineSolve("./ITHACAoutput/Offline/");
206 example.solvesupremizer();
207 ITHACAPOD::getModes(example.nutFields, example.nutModes, example._nut().name(),
208 example.podex, 0, 0, NmodesProject);
209 ITHACAPOD::getModes(example.Ufield, example.Umodes, example._U().name(),
210 example.podex, 0, 0, NmodesProject);
211 ITHACAPOD::getModes(example.Pfield, example.Pmodes, example._p().name(),
212 example.podex, 0, 0, NmodesProject);
213 ITHACAPOD::getModes(example.supfield, example.supmodes, example._U().name(),
214 example.podex,
215 example.supex, 1, NmodesProject);
216 example.projectSUP("./Matrices", NmodesU, NmodesP, NmodesSUP, NmodesNUT);
217 Eigen::MatrixXd coeefs = ITHACAutilities::getCoeffs(example.Ufield,
218 example.L_U_SUPmodes);
219 Eigen::MatrixXd coeefsNut = ITHACAutilities::getCoeffs(example.nutFields,
220 example.nutModes);
221 Eigen::MatrixXd coeefsP = ITHACAutilities::getCoeffs(example.Pfield,
222 example.Pmodes);
223 cnpy::save(coeefs, "./ITHACAoutput/Matrices/coeefs.npy");
224 cnpy::save(coeefsNut, "./ITHACAoutput/Matrices/coeefsNut.npy");
225 cnpy::save(coeefsP, "./ITHACAoutput/Matrices/coeefsP.npy");
226}
227
228
230{
231 word filename("./par");
232 Eigen::VectorXd par;
233 example.inletIndex.resize(1, 2);
234 example.inletIndex << 0, 0;
235 example.inletIndexT.resize(1, 1);
236 example.inletIndexT << 1;
238 example._runTime());
239 int NmodesU = para->ITHACAdict->lookupOrDefault<int>("NmodesU", 5);
240 int NmodesP = para->ITHACAdict->lookupOrDefault<int>("NmodesP", 5);
241 int NmodesSUP = para->ITHACAdict->lookupOrDefault<int>("NmodesSUP", 5);
242 int NmodesNUT = para->ITHACAdict->lookupOrDefault<int>("NmodesNUT", 5);
243 int NmodesProject = para->ITHACAdict->lookupOrDefault<int>("NmodesProject", 5);
244 int NmodesMatrixRec = para->ITHACAdict->lookupOrDefault<int>("NmodesMatrixRec",
245 5);
246 double penaltyFactor =
247 para->ITHACAdict->lookupOrDefault<double>("penaltyFactor", 5);
248 double U_BC = para->ITHACAdict->lookupOrDefault<double>("U_BC", 0.001);
249 double romStartTime = para->ITHACAdict->lookupOrDefault<double>("romStartTime",
250 0);
251 double romEndTime = para->ITHACAdict->lookupOrDefault<double>("romEndTime", 3);
252 double romTimeStep = para->ITHACAdict->lookupOrDefault<double>("romTimeStep",
253 0.001);
254 double e = para->ITHACAdict->lookupOrDefault<double>("RBFradius", 1);
255 example.startTime = 79.992;
256 example.finalTime = 99.996;
257 example.timeStep = 0.0002;
258 example.writeEvery = 0.004;
259 example.offlineSolve("./ITHACAoutput/Offline");
260 example.solvesupremizer();
261 //modNut = ITHACAPOD::getModes(example.nutFields, example.nutModes, example._nut().name(),
262 // example.podex, 0, 0, NmodesProject);
263 ITHACAPOD::getModes(example.Ufield, example.Umodes, example._U().name(),
264 example.podex, 0, 0, NmodesProject);
265 ITHACAPOD::getModes(example.Pfield, example.Pmodes, example._p().name(),
266 example.podex, 0, 0, NmodesProject);
267 ITHACAPOD::getModes(example.supfield, example.supmodes, example._U().name(),
268 example.podex,
269 example.supex, 1, NmodesProject);
270 example.projectPPE("./Matrices", NmodesU, NmodesP, NmodesSUP, NmodesNUT);
271 Eigen::MatrixXd coeefs = ITHACAutilities::getCoeffs(example.Ufield,
272 example.L_U_SUPmodes);
273 Eigen::MatrixXd coeefsNut = ITHACAutilities::getCoeffs(example.nutFields,
274 example.nutModes);
275 Eigen::MatrixXd coeefsP = ITHACAutilities::getCoeffs(example.Pfield,
276 example.Pmodes);
277 cnpy::save(coeefs, "./ITHACAoutput/Matrices/coeefs.npy");
278 cnpy::save(coeefsNut, "./ITHACAoutput/Matrices/coeefsNut.npy");
279 cnpy::save(coeefsP, "./ITHACAoutput/Matrices/coeefsP.npy");
280}
281
Header file of the EigenFunctions class.
Header file of the Foam2Eigen class.
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 ITHACAutilities namespace.
ITHACAparameters * para
Definition NLsolve.H:40
Header file of the reducedSteadyNS class.
Header file of the ReducedUnsteadyNSTurb class.
Header file of the reducedUnsteadyNS class.
Header file of the UnsteadyNSTurb 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.
void projectSUP(fileName folder, label NUmodes, label NPmodes, label NSUPmodes, label nNutModes, bool rbfInterp=true)
Project using a supremizer approach.
UnsteadyNSTurb()
Construct Null.
autoPtr< volScalarField > _nut
Eddy viscosity field.
volScalarModes nutModes
List of POD modes for eddy viscosity.
double e
RBF functions radius.
PtrList< volScalarField > nutFields
List of snapshots for the solution for eddy viscosity.
void projectPPE(fileName folder, label NUmodes, label NPmodes, label NSUPmodes, label nNutModes, bool rbfInterp=true)
Project using the Poisson Equation for pressure.
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)
Eigen::MatrixXi inletIndexT
bool offline
Boolean variable, it is 1 if the Offline phase has already been computed, else 0.
Eigen::MatrixXi inletIndex
Matrix that contains informations about the inlet boundaries.
bool podex
Boolean variable, it is 1 if the POD has already been computed, else 0.
void truthSolve()
Perform a TruthSolve.
bool supex
Boolean variable to check the existence of the supremizer modes.
Definition steadyNS.H:265
PtrList< volScalarField > Pfield
List of pointers used to form the pressure snapshots matrix.
Definition steadyNS.H:86
volVectorModes supmodes
List of pointers used to form the supremizer modes.
Definition steadyNS.H:104
void solvesupremizer(word type="snapshots")
solve the supremizer either with the use of the pressure snaphots or the pressure modes
Definition steadyNS.C:137
autoPtr< Time > _runTime
Time.
Definition steadyNS.H:299
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
autoPtr< fvMesh > _mesh
Mesh.
Definition steadyNS.H:290
volVectorModes L_U_SUPmodes
List of pointers containing the total number of lift, supremizer and velocity modes.
Definition steadyNS.H:119
PtrList< volVectorField > supfield
List of pointers used to form the supremizer snapshots matrix.
Definition steadyNS.H:92
autoPtr< volVectorField > _U
Velocity field.
Definition steadyNS.H:272
volScalarModes Pmodes
List of pointers used to form the pressure modes.
Definition steadyNS.H:98
autoPtr< volScalarField > _p
Pressure field.
Definition steadyNS.H:269
volScalarField & nut
Definition offline.C:90
void offlineSolve(std::string offlinepath)
Definition offline.C:92
volScalarField & p
Definition offline.C:89
tutorial22(int argc, char *argv[])
Definition offline.C:79
Eigen::MatrixXd vectorTensorMult(Eigen::VectorXd g, Eigen::Tensor< double, 3 > c, Eigen::VectorXd a)
Definition offline.C:110
volVectorField & U
Definition offline.C:88
Matrix< VectorType, Dynamic, Dynamic > SliceFromTensor(Eigen::Tensor< VectorType, 3 > &tensor, label dim, label index1)
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:90
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.
bool check_folder(word folder)
Checks if a folder exists.
void save(const Eigen::Matrix< T, -1, dim > &mat, const std::string fname)
void supremizer_approach(tutorial22 &example)
Definition offline.C:174
int main(int argc, char *argv[])
Definition offline.C:135
void poisson_approach(tutorial22 &example)
Definition offline.C:229
Header file of the reductionProblem class.
dimensionedVector & g
label i
Definition pEqn.H:46
Header file of the steadyNS class.