Loading...
Searching...
No Matches
hyperReduction.H
Go to the documentation of this file.
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/>.
24Class
25 HyperReduction
26Description
27 Implementation of the hyper-reduction methods.
28SourceFiles
29 hyperReduction.C
30\*---------------------------------------------------------------------------*/
31#ifndef hyperReduction_H
32#define hyperReduction_H
33
34#include "fvCFD.H"
35#include "ITHACAPOD.H"
36#include "Foam2Eigen.H"
37#include "EigenFunctions.H"
38#include "ITHACAutilities.H"
39#include "fvMeshSubset.H"
40#include <set>
41#include "redsvd"
42
43
44
45// TODO uniform style for separating offline and onlie stages
46template <typename... SnapshotsLists>
48{
49public:
51
52 using SnapshotsListTuple = std::tuple<std::decay_t<SnapshotsLists>...>;
53 using FieldsTuple = std::tuple<typename std::decay_t<SnapshotsLists>::value_type ...>;
54 static constexpr auto n_fields = sizeof...(SnapshotsLists);
55
56 // TODO get vectorial dim from class template parameters
57 unsigned int vectorial_dim;
58 unsigned int sumFieldsDim;
59
60 template <std::size_t N>
61 using NthFieldListType = typename std::tuple_element<N, SnapshotsListTuple>::type;
62
63 template <std::size_t N>
65
66 //----------------------------------------------------------------------
77 label n_nodes,
78 Eigen::VectorXi initialSeeds,
79 word problemName,
80 SnapshotsLists &&...snapshotsLists);
81
82 //----------------------------------------------------------------------
93 label n_nodes,
94 unsigned int vectorial_dim,
95 label n_cells,
96 Eigen::VectorXi initialSeeds,
97 word problemName);
98
99 // ITHACA parameters dict
101
102 //TODO
104
105 //TODO
107
108 //TODO
110
112 label n_modes;
113
115 label n_nodes;
116
119
120 // Initial nodes
121 Eigen::VectorXi initialSeeds;
122
123 // nodes
124 Eigen::VectorXi nodes;
125
128
131
133 List<word> fieldNames;
134
136 List<unsigned int> fieldDims;
137
139 label n_cells;
140
143
145 autoPtr<IOList<label>> nodePoints;
146
148 autoPtr<IOList<labelList>> totalNodePoints;
149
151 autoPtr<IOList<label>> uniqueNodePoints;
152
155
158
160 List<label> localNodePoints;
161
162 // Outputs
164 Eigen::MatrixXd basisMatrix;
165
166 // Normalizing weights of SVD decomposition
167 Eigen::VectorXd normalizingWeights;
168
170 Eigen::MatrixXd renormalizedBasisMatrix;
171
172 // TODO
173 Eigen::MatrixXd pinvPU;
174
175 // TODO
176 Eigen::MatrixXd wPU;
177
178 // TODO
179 Eigen::VectorXd eigenValueseig;
180
182 Eigen::SparseMatrix<double> P;
183
184 // Mask field to submesh
185 Eigen::SparseMatrix<double> field2submesh;
186
187 // Mask submesh to nodes
188 Eigen::SparseMatrix<double> submesh2nodes;
189
190 // Indices submesh to nodes
191 Eigen::VectorXi submesh2nodesMask;
192
194 Eigen::VectorXd quadratureWeights;
195
197 autoPtr<fvMeshSubset> submesh;
198
200 autoPtr<volVectorField> submesh_field;
201
203 Eigen::MatrixXd MatrixOnline;
204
205 //----------------------------------------------------------------------
208 void offlineGappyDEIM(Eigen::MatrixXd& snapshotsModes, Eigen::VectorXd& normalizingWeights){
209 methodName = "GappyDEIM";
210 word name = "ITHACAoutput/" + problemName + "/" + methodName;
211 offlineGappyDEIM(snapshotsModes, normalizingWeights, name);
212 }
213
214 void offlineGappyDEIM(Eigen::MatrixXd& snapshotsModes, Eigen::VectorXd& normalizingWeights, word folderMethodName);
215
216 //----------------------------------------------------------------------
219 void offlineECP(Eigen::MatrixXd& snapshotsModes, Eigen::VectorXd& normalizingWeights){
220 methodName = "ECP";
221 word name = "ITHACAoutput/" + problemName + "/" + methodName;
222 offlineECP(snapshotsModes, normalizingWeights, name);
223 }
224
225 void offlineECP(Eigen::MatrixXd& snapshotsModes, Eigen::VectorXd& normalizingWeights, word folderMethodName);
226
227 //----------------------------------------------------------------------
230 void initSeeds(Eigen::VectorXd mp_not_mask, std::set<label> nodePointsSet);
231
232 //----------------------------------------------------------------------
235 void computeLS(Eigen::MatrixXd& J, Eigen::MatrixXd& JWhole, Eigen::VectorXd& b, Eigen::VectorXd& q);
236
237 //----------------------------------------------------------------------
243 void getModesSVD(SnapshotsListTuple &SnapshotsListTuple, Eigen::MatrixXd &modesSVD, Eigen::VectorXd &fieldWeights, bool saveModesFlag = false);
244
245 //----------------------------------------------------------------------
251 void getModesSVD(SnapshotsListTuple& snapshotsListTuple, Eigen::MatrixXd& modesSVD, Eigen::VectorXd& fieldWeights, Eigen::MatrixXd& modesSVDBoundary, Eigen::VectorXd& fieldWeightsBoundary, bool saveModesFlag);
252
253 //----------------------------------------------------------------------
259 void updateNodes(Eigen::SparseMatrix<double>& P, label& ind_max, Eigen::VectorXd& mp_not_mask);
260
261 //----------------------------------------------------------------------
267 template <typename SnapshotsList>
268 void stackSnapshots(SnapshotsList sList, Eigen::MatrixXd &snapshotsMatrix, Eigen::VectorXd &fieldWeights);
269
270 //----------------------------------------------------------------------
276 template <typename SnapshotsList>
277 void stackSnapshotsBoundary(SnapshotsList sList, List<Eigen::MatrixXd>& snapshotsMatrixBoundary, List<Eigen::VectorXd>& fieldWeightsBoundary);
278
279 //----------------------------------------------------------------------
285 template <typename SnapshotsList>
286 void saveModes(SnapshotsList sList, Eigen::MatrixXd& snapshotsMatrix, unsigned int &rowIndex, unsigned int &modeIndex, word folder);
287
288 //----------------------------------------------------------------------
294 template <typename SnapshotsList>
295 void saveModes(SnapshotsList sList, Eigen::MatrixXd& snapshotsMatrix, Eigen::MatrixXd& snapshotsMatrixBoundary, unsigned int &rowIndex, unsigned int& rowIndexBoundary, unsigned int &modeIndex, word folder);
296
297 //----------------------------------------------------------------------
303 template <typename SnapshotsList>
304 void stackNames(SnapshotsList sList){
305 fieldNames.append(sList[0].name());
306 }
307
308 //----------------------------------------------------------------------
314 template <typename SnapshotsList>
315 inline void stackDimensions(SnapshotsList sList){
317
318 //----------------------------------------------------------------------
324 template <typename SnapshotsList>
325 inline void sumDimensions(double sum, SnapshotsList sList){
327
328 //----------------------------------------------------------------------
334 void evaluatePinv(Eigen::SparseMatrix<double> &Projector, Eigen::MatrixXd &Modes, Eigen::VectorXd& fieldWeights);
335
336 //----------------------------------------------------------------------
342 void evaluateWPU(Eigen::SparseMatrix<double> &Projector, Eigen::MatrixXd &Modes, Eigen::VectorXd& fieldWeights, Eigen::VectorXd& quadratureWeights);
343
344 //----------------------------------------------------------------------
350 void generateSubmesh(label layers, const fvMesh &mesh);
351
352 //----------------------------------------------------------------------
360 List<label> global2local(List<label> &points, fvMeshSubset &submesh);
361
367 template <typename FieldType>
368 inline autoPtr<FieldType> interpolateField(const FieldType &field)
369 {
370 return autoPtr<FieldType>(new FieldType(submesh->interpolate(field)));
371 }
372
378 // TODO expand with sphericalTensor, symmTensor, tensor
379 template <typename Field>
380 inline constexpr unsigned int get_field_dim()
381 {
382 return std::is_same<Field, volScalarField>::value ? 1 : 3;
383 }
384
390 template <typename LastList>
391 inline constexpr unsigned int compute_vectorial_dim(LastList x) { return get_field_dim<typename std::decay_t<LastList>::value_type>(); }
392
398 template <typename List, typename... RemainingLists>
399 inline constexpr unsigned int compute_vectorial_dim(List &&head, RemainingLists &&...tail)
400 {
401 return get_field_dim<typename std::decay_t<List>::value_type>() + compute_vectorial_dim<RemainingLists...>(tail...);
402 }
403
409 static inline double s_optimality(Eigen::MatrixXd& A)
410 {
411 return std::pow(
412 std::sqrt((A.transpose()*A).determinant())/\
413 A.colwise().lpNorm<2>().prod(),
414 1.0/A.cols());
415 }
416
422 template <typename... FieldsArgs>
423 void eigen2fields(Eigen::VectorXd& eFields, FieldsArgs&&... oFields)
424 {
425 unsigned int cumulativeSize{0}, ith_field{0};
426
427 ([&](auto& oFieldsArg)
428 {
429 double ith_fieldSize = fieldDims[ith_field]*oFieldsArg.size();
430 Eigen::VectorXd vec = eFields.segment(cumulativeSize, ith_fieldSize);
431 oFieldsArg = Foam2Eigen::Eigen2field(oFieldsArg, vec);
432 cumulativeSize+=ith_fieldSize;
433 ith_field++;
434 }(oFields), ...);
435
436 }
437
443 void initReshapeMat(Eigen::SparseMatrix<double> &reshapeMat);
444
450 void createMasks(bool offlineStage = true);
451
457 void getSnapMatrix(Eigen::MatrixXd& snapMatrix, Eigen::VectorXd& fieldWeights);
458
464 void getSnapMatrix(Eigen::MatrixXd& snapMatrix, Eigen::VectorXd& fieldWeights, List<Eigen::MatrixXd>& snapMatrixBoundary, List<Eigen::VectorXd>& fieldWeightsBoundary);
465};
466
467#endif
Header file of the EigenFunctions class.
Header file of the Foam2Eigen class.
Header file of the ITHACAPOD class.
Foam::fvMesh & mesh
Definition createMesh.H:47
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.
Definition Foam2Eigen.C:517
Eigen::VectorXi submesh2nodesMask
static constexpr auto n_fields
typename NthFieldListType< N >::value_type NthFieldType
void stackSnapshotsBoundary(SnapshotsList sList, List< Eigen::MatrixXd > &snapshotsMatrixBoundary, List< Eigen::VectorXd > &fieldWeightsBoundary)
TODO.
SnapshotsListTuple snapshotsListTuple
The snapshots matrix containing the nonlinear function or operator.
label n_modes
The maximum number of modes to be considered.
void evaluateWPU(Eigen::SparseMatrix< double > &Projector, Eigen::MatrixXd &Modes, Eigen::VectorXd &fieldWeights, Eigen::VectorXd &quadratureWeights)
Compute the pseudo-inverse of the matrix M restricted with the projector P.
label n_nodes
The maximum number of modes to be considered.
word folderMethod
Folder for the selected HR method.
void stackNames(SnapshotsList sList)
TODO.
List< word > fieldNames
Names of the fields.
void generateSubmesh(label layers, const fvMesh &mesh)
Compute the submesh common to all fields in SnapshotsLists.
Eigen::VectorXi nodes
autoPtr< FieldType > interpolateField(const FieldType &field)
TODO.
autoPtr< fvMeshSubset > submesh
Submesh of the HyperReduction method.
autoPtr< IOList< label > > uniqueNodePoints
List of the unique indices of the nodes that define the submesh.
Eigen::MatrixXd renormalizedBasisMatrix
Renormalized basis of HR.
List< label > n_boundary_cells_list
typename std::tuple_element< N, SnapshotsListTuple >::type NthFieldListType
void offlineGappyDEIM(Eigen::MatrixXd &snapshotsModes, Eigen::VectorXd &normalizingWeights)
Methods implemented: 'GappyDEIM' from "DEIM, Chaturantabut, Saifon, and Danny C. Sorensen....
void updateNodes(Eigen::SparseMatrix< double > &P, label &ind_max, Eigen::VectorXd &mp_not_mask)
TODO.
unsigned int sumFieldsDim
void evaluatePinv(Eigen::SparseMatrix< double > &Projector, Eigen::MatrixXd &Modes, Eigen::VectorXd &fieldWeights)
Compute the pseudo-inverse of the matrix M restricted with the projector P.
Eigen::VectorXd quadratureWeights
Quadrature weights. Ordered in the same order of matrix P.
void sumDimensions(double sum, SnapshotsList sList)
TODO.
Eigen::MatrixXd MatrixOnline
Online Matrix.
List< label > global2local(List< label > &points, fvMeshSubset &submesh)
Get local indices in the submesh from indices in the global ones.
Eigen::SparseMatrix< double > submesh2nodes
void stackDimensions(SnapshotsList sList)
TODO.
constexpr unsigned int compute_vectorial_dim(LastList x)
TODO.
void getSnapMatrix(Eigen::MatrixXd &snapMatrix, Eigen::VectorXd &fieldWeights)
TODO.
Eigen::VectorXi initialSeeds
Eigen::MatrixXd pinvPU
List< label > localNodePoints
Indices of the local node points in the subMesh.
constexpr unsigned int get_field_dim()
TODO.
Eigen::VectorXd normalizingWeights
void offlineECP(Eigen::MatrixXd &snapshotsModes, Eigen::VectorXd &normalizingWeights)
Methods implemented: 'ECP' from "ECP, Hernandez, Joaquin Alberto, Manuel Alejandro Caicedo,...
word problemName
The name of the non-linear function e.g. HR_method/residual.
constexpr unsigned int compute_vectorial_dim(List &&head, RemainingLists &&...tail)
TODO.
void eigen2fields(Eigen::VectorXd &eFields, FieldsArgs &&... oFields)
TODO.
HyperReduction(label n_modes, label n_nodes, Eigen::VectorXi initialSeeds, word problemName, SnapshotsLists &&...snapshotsLists)
Construct HyperReduction class, interpolation-based.
ITHACAparameters * para
std::tuple< std::decay_t< SnapshotsLists >... > SnapshotsListTuple
std::tuple< typename std::decay_t< SnapshotsLists >::value_type ... > FieldsTuple
Eigen::VectorXd eigenValueseig
Eigen::SparseMatrix< double > P
The P matrix of the HyperReduction method. The nodes are ordered in the order of insertion during the...
unsigned int vectorial_dim
label n_cells
Int Number of Cells;.
autoPtr< IOList< label > > nodePoints
Nodes in the case of the a nonlinear function.
void initReshapeMat(Eigen::SparseMatrix< double > &reshapeMat)
TODO.
void initSeeds(Eigen::VectorXd mp_not_mask, std::set< label > nodePointsSet)
TODO.
autoPtr< IOList< labelList > > totalNodePoints
List of label lists of the nodes and corresponding surrounding nodes.
static double s_optimality(Eigen::MatrixXd &A)
TODO.
void getModesSVD(SnapshotsListTuple &SnapshotsListTuple, Eigen::MatrixXd &modesSVD, Eigen::VectorXd &fieldWeights, bool saveModesFlag=false)
TODO.
autoPtr< volVectorField > submesh_field
Submeshes.
label n_snapshots
The length of the snapshots lists.
Eigen::SparseMatrix< double > field2submesh
List< unsigned int > fieldDims
Dimensions of the fields.
void saveModes(SnapshotsList sList, Eigen::MatrixXd &snapshotsMatrix, unsigned int &rowIndex, unsigned int &modeIndex, word folder)
TODO.
word folderProblem
Folder for the HR problem.
void stackSnapshots(SnapshotsList sList, Eigen::MatrixXd &snapshotsMatrix, Eigen::VectorXd &fieldWeights)
TODO.
Eigen::MatrixXd basisMatrix
Orthonormal basis of HR.
label n_cellsSubfields
Int Number of Cells in submeshes;.
void createMasks(bool offlineStage=true)
TODO.
void computeLS(Eigen::MatrixXd &J, Eigen::MatrixXd &JWhole, Eigen::VectorXd &b, Eigen::VectorXd &q)
TODO.
Eigen::MatrixXd wPU
Class for the definition of some general parameters, the parameters must be defined from the file ITH...
Implementation of a container class derived from PtrList.
Definition Modes.H:69
volScalarField & A