32#include "simpleControl.H"
33#include "simpleControl.H"
34#include "fvMeshSubset.H"
48 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
49 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
51 for (
auto i = 0;
i <
S.size();
i++)
53 S[
i] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1,
54 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
62 Eigen::VectorXd ret(nodesList.size());
63 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
64 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
66 for (
auto i = 0;
i < nodesList.size();
i++)
68 ret[
i] = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1,
69 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
89 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
90 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
92 for (
auto i = 0;
i <
S.size();
i++)
94 S[
i][0] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1,
95 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
96 S[
i][1] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 0.5,
97 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
98 S[
i][2] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1.5,
99 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0., 2));
107 Eigen::VectorXd ret(nodesList.size()*3);
108 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
109 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
111 for (
auto i = 0;
i < nodesList.size();
i++)
113 ret(
i) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1,
114 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
115 ret(
i + nodesList.size()) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 0.5,
116 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
117 ret(
i + 2 * nodesList.size()) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1.5,
118 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0., 2));
136 static std::pair<volVectorField, volScalarField>
evaluate_expression(volVectorField& V, volScalarField&
S, Eigen::MatrixXd mu)
138 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
139 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
141 for (
auto i = 0;
i <
S.size();
i++)
143 V[
i][0] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1,
144 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
145 V[
i][1] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 0.5,
146 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
147 V[
i][2] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1.5,
148 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0., 2));
149 S[
i] = std::exp( - 2 * std::pow(
xPos[
i] - mu(0) - 1,
150 2) - 2 * std::pow(
yPos[
i] - mu(1) - 0.5, 2));
153 return std::make_pair(V,
S);
158 Eigen::VectorXd ret(nodesList.size()*4);
159 volScalarField
yPos =
S.mesh().C().component(vector::Y).ref();
160 volScalarField
xPos =
S.mesh().C().component(vector::X).ref();
162 for (
auto i = 0;
i < nodesList.size();
i++)
164 ret(
i) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1,
165 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
166 ret(
i + nodesList.size()) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 0.5,
167 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
168 ret(
i + 2 * nodesList.size()) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1.5,
169 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0., 2));
170 ret(
i + 3 * nodesList.size()) = std::exp( - 2 * std::pow(
xPos[nodesList[
i]] - mu(0) - 1,
171 2) - 2 * std::pow(
yPos[nodesList[
i]] - mu(1) - 0.5, 2));
186 int n_modes = para->
ITHACAdict->lookupOrDefault<
int>(
"Modes", 15);
187 int n_nodes = para->
ITHACAdict->lookupOrDefault<
int>(
"Nodes", 15);
190 word methodName = para->
ITHACAdict->lookupOrDefault<word>(
"HyperReduction",
"GappyDEIM");
194 PtrList<volScalarField> Sp;
207 dimensionedScalar(
"zero", dimensionSet(0, 0, -1, 1, 0, 0, 0), 0)
210 Eigen::MatrixXd pars;
214 for (
int i = 0;
i < 100;
i++)
217 Sp.append((
S).clone());
222 Eigen::MatrixXd parTest;
226 Eigen::VectorXi initSeeds;
229 if (methodName==
"GappyDEIM")
232 Eigen::MatrixXd snapshotsModes;
233 Eigen::VectorXd normalizingWeights;
238 unsigned int layers{2};
242 PtrList<volScalarField> testTrueFields;
243 PtrList<volScalarField> testRecFields;
244 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
253 testRecFields.append(S2.clone());
254 testTrueFields.append(
S.clone());
262 Info <<
"GappyDEIM max relative error: " << err.maxCoeff() << endl;
263 Info <<
"GappyDEIM mean relative error: " << err.array().mean() << endl;
265 else if (methodName==
"ECP")
269 bool saveOFModes{
true};
270 Eigen::MatrixXd snapshotsModes;
271 Eigen::VectorXd normalizingWeights;
273 c.
offlineECP(snapshotsModes, normalizingWeights);
276 unsigned int layers{2};
280 Eigen::VectorXd results(parTest.rows());
281 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
287 double trueIntegral = (normalizingWeights.cwiseInverse().asDiagonal()*ff).array().sum();
288 double testIntegral = (c.
wPU*f).array().sum();
292 results(idTest) = trueIntegral-testIntegral;
294 Info <<
"ECP max error: " << results.cwiseAbs().maxCoeff() << endl;
295 Info <<
"ECP mean error: " << results.cwiseAbs().mean() << endl;
300 int n_modes = para->
ITHACAdict->lookupOrDefault<
int>(
"Modes", 15);
301 int n_nodes = para->
ITHACAdict->lookupOrDefault<
int>(
"Nodes", 15);
303 word methodName = para->
ITHACAdict->lookupOrDefault<word>(
"HyperReduction",
"GappyDEIM");
307 PtrList<volVectorField> Sp;
320 dimensionedVector(
"zero", dimensionSet(0, 0, -1, 1, 0, 0, 0), vector(0, 0, 0))
324 Eigen::MatrixXd pars;
327 for (
int i = 0;
i < 100;
i++)
330 Sp.append((
S).clone());
335 Eigen::MatrixXd parTest;
339 Eigen::VectorXi initSeeds;
342 if (methodName==
"GappyDEIM")
345 Eigen::MatrixXd snapshotsModes;
346 Eigen::VectorXd normalizingWeights;
351 unsigned int layers{2};
355 PtrList<volVectorField> testTrueFields;
356 PtrList<volVectorField> testRecFields;
357 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
366 testRecFields.append(S2.clone());
367 testTrueFields.append(
S.clone());
375 Info <<
"GappyDEIM max relative error: " << err.maxCoeff() << endl;
376 Info <<
"GappyDEIM mean relative error: " << err.array().mean() << endl;
378 else if (methodName==
"ECP")
382 bool saveOFModes{
true};
383 Eigen::MatrixXd snapshotsModes;
384 Eigen::VectorXd normalizingWeights;
386 c.
offlineECP(snapshotsModes, normalizingWeights);
389 unsigned int layers{2};
393 Eigen::VectorXd results(parTest.rows());
394 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
401 double trueIntegral = (normalizingWeights.cwiseInverse().asDiagonal()*ff).array().sum();
402 double testIntegral = (c.
wPU*f).array().sum();
406 results(idTest) = trueIntegral-testIntegral;
408 Info <<
"ECP max error: " << results.cwiseAbs().maxCoeff() << endl;
409 Info <<
"ECP mean error: " << results.cwiseAbs().mean() << endl;
414 int n_modes = para->
ITHACAdict->lookupOrDefault<
int>(
"Modes", 15);
415 int n_nodes = para->
ITHACAdict->lookupOrDefault<
int>(
"Nodes", 15);
417 word methodName = para->
ITHACAdict->lookupOrDefault<word>(
"HyperReduction",
"GappyDEIM");
421 PtrList<volVectorField> Vp;
422 PtrList<volScalarField> Sp;
435 dimensionedVector(
"zero", dimensionSet(0, 0, -1, 1, 0, 0, 0), vector(0, 0, 0))
449 dimensionedScalar(
"zero", dimensionSet(0, 0, -1, 1, 0, 0, 0), 0)
453 Eigen::MatrixXd pars;
456 for (
int i = 0;
i < 100;
i++)
459 Vp.append((V).clone());
460 Sp.append((
S).clone());
466 Eigen::MatrixXd parTest;
470 Eigen::VectorXi initSeeds;
473 if (methodName==
"GappyDEIM")
476 Eigen::MatrixXd snapshotsModes;
477 Eigen::VectorXd normalizingWeights;
482 unsigned int layers{2};
487 PtrList<volVectorField> testTrueFieldsV;
488 PtrList<volVectorField> testRecFieldsV;
489 PtrList<volScalarField> testTrueFieldsS;
490 PtrList<volScalarField> testRecFieldsS;
491 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
494 auto theta = c.
onlineCoeffs(sfield(), parTest.row(idTest));
496 Eigen::VectorXd recvec = aprfield.head(3*
S.size());
497 Eigen::VectorXd recsca = aprfield.tail(
S.size());
504 testRecFieldsV.append(V2.clone());
505 testTrueFieldsV.append(V.clone());
510 testRecFieldsS.append(S2.clone());
511 testTrueFieldsS.append(
S.clone());
519 Info <<
"GappyDEIM vector field max relative error: " << errV.maxCoeff() << endl;
520 Info <<
"GappyDEIM vector field mean relative error: " << errV.array().mean() << endl;
523 Info <<
"GappyDEIM scalar max relative error: " << errS.maxCoeff() << endl;
524 Info <<
"GappyDEIM scalar mean relative error: " << errS.array().mean() << endl;
526 else if (methodName==
"ECP")
530 bool saveOFModes{
true};
531 Eigen::MatrixXd snapshotsModes;
532 Eigen::VectorXd normalizingWeights;
534 c.
offlineECP(snapshotsModes, normalizingWeights);
537 unsigned int layers{2};
542 Eigen::VectorXd results(parTest.rows());
543 for (
unsigned int idTest = 0; idTest < parTest.rows(); idTest++)
550 Eigen::VectorXd ff(ffV.rows() + ffS.rows());
551 ff.head(ffV.rows()) = ffV;
552 ff.tail(ffS.rows()) = ffS;
554 double trueIntegral = (normalizingWeights.cwiseInverse().asDiagonal()*ff).array().sum();
555 double testIntegral = (c.
wPU*f).array().sum();
559 results(idTest) = trueIntegral-testIntegral;
561 Info <<
"ECP max error: " << results.cwiseAbs().maxCoeff() << endl;
562 Info <<
"ECP mean error: " << results.cwiseAbs().mean() << endl;
566int main(
int argc,
char* argv[])
569 argList::addOption(
"test",
"scalar",
"Perform scalar test");
570 argList::addOption(
"test",
"vector",
"Perform vector test");
571 argList::addOption(
"test",
"vs",
"Perform (vector, scalar) test");
573 HashTable<string> validParOptions;
591#include "setRootCase.H"
592 Foam::Time
runTime(Foam::Time::controlDictName, args);
597 Foam::fvMesh::defaultRegion,
600 Foam::IOobject::MUST_READ
605 if (args.get(
"test").match(
"scalar"))
607 Info <<
"Init scalar testcase\n";
610 else if (args.get(
"test").match(
"vector"))
612 Info <<
"Init vector testcase\n";
615 else if (args.get(
"test").match(
"vs"))
617 Info <<
"Init vector scalar testcase\n";
622 Info <<
"Pass '-test scalar', '-test vector', '-test vs'" << endl;
int main(int argc, char *argv[])
void test_vector(ITHACAparameters *para, Foam::fvMesh &mesh, Foam::Time &runTime)
void test_vector_scalar(ITHACAparameters *para, Foam::fvMesh &mesh, Foam::Time &runTime)
void test_scalar(ITHACAparameters *para, Foam::fvMesh &mesh, Foam::Time &runTime)
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.
static GeometricField< scalar, PatchField, GeoMesh > Eigen2field(GeometricField< scalar, PatchField, GeoMesh > &field, Eigen::VectorXd &eigen_vector, bool correctBC=true)
Convert a vector in Eigen format into an OpenFOAM scalar GeometricField.
static Eigen::VectorXd field2Eigen(GeometricField< tensor, PatchField, GeoMesh > &field)
Convert a vector OpenFOAM field into an Eigen Vector.
Eigen::VectorXd onlineCoeffs(volScalarField &S, Eigen::MatrixXd mu)
static volScalarField evaluate_expression(volScalarField &S, Eigen::MatrixXd mu)
static Eigen::VectorXd evaluate_expression(volScalarField &S, Eigen::MatrixXd mu, List< label > nodesList)
static volVectorField evaluate_expression(volVectorField &S, Eigen::MatrixXd mu)
Eigen::VectorXd onlineCoeffs(volVectorField &S, Eigen::MatrixXd mu)
static Eigen::VectorXd evaluate_expression(volVectorField &S, Eigen::MatrixXd mu, List< label > nodesList)
Eigen::VectorXd onlineCoeffs(volScalarField &S, Eigen::MatrixXd mu)
static Eigen::VectorXd evaluate_expression(volScalarField &S, Eigen::MatrixXd mu, List< label > nodesList)
static std::pair< volVectorField, volScalarField > evaluate_expression(volVectorField &V, volScalarField &S, Eigen::MatrixXd mu)
SnapshotsListTuple snapshotsListTuple
The snapshots matrix containing the nonlinear function or operator.
void generateSubmesh(label layers, const fvMesh &mesh)
Compute the submesh common to all fields in SnapshotsLists.
autoPtr< FieldType > interpolateField(const FieldType &field)
TODO.
Eigen::MatrixXd renormalizedBasisMatrix
Renormalized basis of HR.
void offlineGappyDEIM(Eigen::MatrixXd &snapshotsModes, Eigen::VectorXd &normalizingWeights)
Methods implemented: 'GappyDEIM' from "DEIM, Chaturantabut, Saifon, and Danny C. Sorensen....
List< label > localNodePoints
void offlineECP(Eigen::MatrixXd &snapshotsModes, Eigen::VectorXd &normalizingWeights)
Methods implemented: 'ECP' from "ECP, Hernandez, Joaquin Alberto, Manuel Alejandro Caicedo,...
HyperReduction(label n_modes, label n_nodes, Eigen::VectorXi initialSeeds, word problemName, SnapshotsLists &&...snapshotsLists)
Construct HyperReduction class, interpolation-based.
void getModesSVD(SnapshotsListTuple &SnapshotsListTuple, Eigen::MatrixXd &modesSVD, Eigen::VectorXd &fieldWeights, bool saveModesFlag=false)
TODO.
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.
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
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.
Eigen::Matrix< T, -1, dim > load(Eigen::Matrix< T, -1, dim > &mat, const std::string fname, std::string order="rowMajor")
simpleControl simple(mesh)
volScalarField S(IOobject("S", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), T.mesh(), dimensionedScalar("zero", dimensionSet(0, 0, -1, 1, 0, 0, 0), 0))