Loading...
Searching...
No Matches
hyperReduction.H
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#include <unsupported/Eigen/NNLS>
43
44
45
46// TODO uniform style for separating offline and onlie stages
47template <typename... SnapshotsLists>
49{
50 public:
51 word methodName;
52
53 using SnapshotsListTuple = std::tuple<std::decay_t<SnapshotsLists>...>;
54 using FieldsTuple =
55 std::tuple<typename std::decay_t<SnapshotsLists>::value_type ...>;
56 static constexpr auto n_fields = sizeof...(SnapshotsLists);
57
58 // TODO get vectorial dim from class template parameters
59 unsigned int vectorial_dim;
60 unsigned int sumFieldsDim;
61
62 template <std::size_t N>
63 using NthFieldListType = typename
64 std::tuple_element<N, SnapshotsListTuple>::type;
65
66 template <std::size_t N>
67 using NthFieldType = typename NthFieldListType<N>::value_type;
68
69 //----------------------------------------------------------------------
80 label n_nodes,
81 Eigen::VectorXi initialSeeds,
82 word problemName,
83 SnapshotsLists &&...snapshotsLists);
84
85 //----------------------------------------------------------------------
97 label n_nodes,
98 unsigned int vectorial_dim,
99 label n_cells,
100 Eigen::VectorXi initialSeeds,
101 word problemName,
102 Eigen::VectorXd volumes = Eigen::VectorXd::Constant(1,0.0));
103
104 // ITHACA parameters dict
105 ITHACAparameters* para;
106
107 //TODO
108 label n_boundary_patches;
109
110 //TODO
111 List<label> n_boundary_cells_list;
112
113 //TODO
114 label n_boundary_cells;
115
117 label n_modes;
118
120 label n_nodes;
121
124
125 // Initial nodes
126 Eigen::VectorXi initialSeeds;
127
128 // nodes
129 Eigen::VectorXi nodes;
130
133
135 SnapshotsListTuple snapshotsListTuple;
136
138 List<word> fieldNames;
139
141 List<unsigned int> fieldDims;
142
144 label n_cells;
145
147 Eigen::VectorXd volumes;
148
151
153 autoPtr<IOList<label >> nodePoints;
154
156 autoPtr<IOList<labelList >> totalNodePoints;
157
159 autoPtr<IOList<label >> uniqueNodePoints;
160
163
166
168 List<label> localNodePoints;
169
170 // Outputs
172 Eigen::MatrixXd basisMatrix;
173
174 // Normalizing weights of SVD decomposition
175 Eigen::VectorXd normalizingWeights;
176
178 Eigen::MatrixXd renormalizedBasisMatrix;
179
180 // TODO
181 Eigen::MatrixXd pinvPU;
182
183 // TODO
184 Eigen::MatrixXd wPU;
185
186 // TODO
187 Eigen::VectorXd eigenValueseig;
188
190 Eigen::SparseMatrix<double> P;
191
192 // Mask field to submesh
193 Eigen::SparseMatrix<double> field2submesh;
194
195 // Mask submesh to nodes
196 Eigen::SparseMatrix<double> submesh2nodes;
197
198 // Indices submesh to nodes
199 Eigen::VectorXi submesh2nodesMask;
200
202 Eigen::VectorXd quadratureWeights;
203 List<Eigen::VectorXd> quadratureWeightsSubspaces;
204
206 autoPtr<fvMeshSubset> submesh;
207
209 autoPtr<volVectorField> submesh_field;
210
212 Eigen::MatrixXd MatrixOnline;
213
214 //----------------------------------------------------------------------
217 void offlineGappyDEIM(Eigen::MatrixXd& snapshotsModes,
218 Eigen::VectorXd& normalizingWeights)
219 {
220 methodName = "GappyDEIM";
221 word name = "ITHACAoutput/" + problemName + "/" + methodName;
222 offlineGappyDEIM(snapshotsModes, normalizingWeights, name);
223 }
224
225 void offlineGappyDEIM(Eigen::MatrixXd& snapshotsModes,
226 Eigen::VectorXd& normalizingWeights, word folderMethodName);
227
228 //----------------------------------------------------------------------
231 void offlineECP(Eigen::MatrixXd& snapshotsModes,
232 Eigen::VectorXd& normalizingWeights)
233 {
234 methodName = "ECP";
235 word name = "ITHACAoutput/" + problemName + "/" + methodName;
236 offlineECP(snapshotsModes, normalizingWeights, name);
237 }
238
239 void offlineECP(Eigen::MatrixXd& snapshotsModes,
240 Eigen::VectorXd& normalizingWeights, word folderMethodName)
241 {
242 List<Eigen::MatrixXd> listSnapshotsModes(1);
243 listSnapshotsModes[0] = snapshotsModes.leftCols(n_modes);
244 offlineECP(listSnapshotsModes, normalizingWeights, folderMethodName);
245 }
246
247 //----------------------------------------------------------------------
252 void offlineECP(List<Eigen::MatrixXd>& listSnapshotsModes,
253 Eigen::VectorXd& weights, word folderMethodName);
254
255
256 //----------------------------------------------------------------------
259 void initSeeds(Eigen::VectorXd mp_not_mask, std::set<label> nodePointsSet);
260
261 //----------------------------------------------------------------------
264 void computeLS(Eigen::MatrixXd& J, Eigen::MatrixXd& JWhole, Eigen::VectorXd& b,
265 Eigen::VectorXd& q, bool NNLS);
266
267 //----------------------------------------------------------------------
273 void getModesSVD(SnapshotsListTuple& SnapshotsListTuple,
274 Eigen::MatrixXd& modesSVD, Eigen::VectorXd& fieldWeights,
275 bool saveModesFlag = false);
276
277 //----------------------------------------------------------------------
283 void getModesSVD(SnapshotsListTuple& snapshotsListTuple,
284 Eigen::MatrixXd& modesSVD, Eigen::VectorXd& fieldWeights,
285 Eigen::MatrixXd& modesSVDBoundary, Eigen::VectorXd& fieldWeightsBoundary,
286 bool saveModesFlag);
287
288 //----------------------------------------------------------------------
294 void updateNodes(Eigen::SparseMatrix<double>& P, label& ind_max,
295 Eigen::VectorXd& mp_not_mask);
296
297 //----------------------------------------------------------------------
303 void updateNodesRemove(Eigen::SparseMatrix<double>& P, Eigen::VectorXd weights);
304
305 //----------------------------------------------------------------------
311 template <typename SnapshotsList>
312 void stackSnapshots(SnapshotsList sList, Eigen::MatrixXd& snapshotsMatrix,
313 Eigen::VectorXd& fieldWeights);
314
315 //----------------------------------------------------------------------
321 template <typename SnapshotsList>
322 void stackSnapshotsBoundary(SnapshotsList sList,
323 List<Eigen::MatrixXd>& snapshotsMatrixBoundary,
324 List<Eigen::VectorXd>& fieldWeightsBoundary);
325
326 //----------------------------------------------------------------------
332 template <typename SnapshotsList>
333 void saveModes(SnapshotsList sList, Eigen::MatrixXd& snapshotsMatrix,
334 unsigned int& rowIndex, unsigned int& modeIndex, word folder);
335
336 //----------------------------------------------------------------------
342 template <typename SnapshotsList>
343 void saveModes(SnapshotsList sList, Eigen::MatrixXd& snapshotsMatrix,
344 Eigen::MatrixXd& snapshotsMatrixBoundary, unsigned int& rowIndex,
345 unsigned int& rowIndexBoundary, unsigned int& modeIndex, word folder);
346
347 //----------------------------------------------------------------------
353 template <typename SnapshotsList>
354 void stackNames(SnapshotsList sList)
355 {
356 fieldNames.append(sList[0].name());
357 }
358
359 //----------------------------------------------------------------------
365 template <typename SnapshotsList>
366 inline void stackDimensions(SnapshotsList sList)
367 {
369 }
370
371 //----------------------------------------------------------------------
377 template <typename SnapshotsList>
378 inline void sumDimensions(double sum, SnapshotsList sList)
379 {
381 }
382
383 //----------------------------------------------------------------------
389 void evaluatePinv(Eigen::SparseMatrix<double>& Projector,
390 Eigen::MatrixXd& Modes, Eigen::VectorXd& fieldWeights);
391
392 //----------------------------------------------------------------------
398 void evaluateWPU(Eigen::SparseMatrix<double>& Projector,
399 Eigen::MatrixXd& Modes, Eigen::VectorXd& fieldWeights,
400 Eigen::VectorXd& quadratureWeights);
401
402 //----------------------------------------------------------------------
408 void generateSubmesh(label layers, const fvMesh& mesh);
409
410 //----------------------------------------------------------------------
418 List<label> global2local(List<label>& points, fvMeshSubset& submesh);
419
425 template <typename FieldType>
426 inline autoPtr<FieldType> interpolateField(const FieldType& field)
427 {
428 return autoPtr<FieldType>(new FieldType(submesh->interpolate(field)));
429 }
430
436 // TODO expand with sphericalTensor, symmTensor, tensor
437 template <typename Field>
438 inline constexpr unsigned int get_field_dim()
439 {
440 return std::is_same<Field, volScalarField>::value ? 1 : 3;
441 }
442
448 template <typename LastList>
449 inline constexpr unsigned int compute_vectorial_dim(LastList x)
450 {
452 }
453
459 template <typename List, typename... RemainingLists>
460 inline constexpr unsigned int compute_vectorial_dim(List &&head,
461 RemainingLists &&...tail)
462 {
465 }
466
472 static inline double s_optimality(Eigen::MatrixXd& A)
473 {
474 return std::pow(
475 std::sqrt((A.transpose() * A).determinant()) / \
476 A.colwise().lpNorm<2>().prod(),
477 1.0 / A.cols());
478 }
479
485 template <typename... FieldsArgs>
486 void eigen2fields(Eigen::VectorXd& eFields, FieldsArgs&&... oFields)
487 {
488 unsigned int cumulativeSize{0}, ith_field{0};
489 ([ & ](auto & oFieldsArg)
490 {
491 double ith_fieldSize = fieldDims[ith_field] * oFieldsArg.size();
492 Eigen::VectorXd vec = eFields.segment(cumulativeSize, ith_fieldSize);
493 oFieldsArg = Foam2Eigen::Eigen2field(oFieldsArg, vec);
494 cumulativeSize += ith_fieldSize;
495 ith_field++;
496 }
497 (oFields), ...);
498 }
499
505 void initReshapeMat(Eigen::SparseMatrix<double>& reshapeMat);
506
512 void createMasks(bool offlineStage = true);
513
519 void getSnapMatrix(Eigen::MatrixXd& snapMatrix, Eigen::VectorXd& fieldWeights);
520
526 void getSnapMatrix(Eigen::MatrixXd& snapMatrix, Eigen::VectorXd& fieldWeights,
527 List<Eigen::MatrixXd>& snapMatrixBoundary,
528 List<Eigen::VectorXd>& fieldWeightsBoundary);
529
535 Eigen::SparseMatrix<double> maskToOtherDim(int newDim);
536};
537
538#endif
Header file of the EigenFunctions class.
Header file of the Foam2Eigen class.
Header file of the ITHACAPOD class.
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:533
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.
autoPtr< FieldType > interpolateField(const FieldType &field)
TODO.
autoPtr< fvMeshSubset > submesh
Submesh of the HyperReduction method.
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....
void updateNodes(Eigen::SparseMatrix< double > &P, label &ind_max, Eigen::VectorXd &mp_not_mask)
TODO.
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.
autoPtr< IOList< labelList > > totalNodePoints
List of label lists of the nodes and corresponding surrounding nodes.
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.
void stackDimensions(SnapshotsList sList)
TODO.
constexpr unsigned int compute_vectorial_dim(LastList x)
TODO.
void getSnapMatrix(Eigen::MatrixXd &snapMatrix, Eigen::VectorXd &fieldWeights)
TODO.
List< label > localNodePoints
Indices of the local node points in the subMesh.
constexpr unsigned int get_field_dim()
TODO.
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.
void updateNodesRemove(Eigen::SparseMatrix< double > &P, Eigen::VectorXd weights)
Remove from the nodes the weights equal to 0 (used with NNLS).
Eigen::SparseMatrix< double > P
The P matrix of the HyperReduction method. The nodes are ordered in the order of insertion during the...
label n_cells
Int Number of Cells;.
void initReshapeMat(Eigen::SparseMatrix< double > &reshapeMat)
TODO.
void initSeeds(Eigen::VectorXd mp_not_mask, std::set< label > nodePointsSet)
TODO.
autoPtr< IOList< label > > nodePoints
Nodes in the case of the a nonlinear function.
Eigen::VectorXd volumes
Volume of each cell.
Eigen::SparseMatrix< double > maskToOtherDim(int newDim)
Compute the sparse mask matrix P with a vectorial dimension different from the snapshots (enlarged or...
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.
autoPtr< IOList< label > > uniqueNodePoints
List of the unique indices of the nodes that define the submesh.
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.
void computeLS(Eigen::MatrixXd &J, Eigen::MatrixXd &JWhole, Eigen::VectorXd &b, Eigen::VectorXd &q, bool NNLS)
TODO.
label n_cellsSubfields
Int Number of Cells in submeshes;.
void createMasks(bool offlineStage=true)
TODO.
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