Loading...
Searching...
No Matches
pod.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_POD_HPP__
26#define __BITPIT_POD_HPP__
27
28#if BITPIT_ENABLE_MPI
29# include <mpi.h>
30#endif
31#include <string>
32#include <vector>
33#include <unordered_map>
34
35#include "pod_kernel.hpp"
36
37namespace bitpit {
38
39class PODKernel;
40
41class POD : public VTKBaseStreamer {
42
43public:
44
49 enum class MemoryMode {
52 };
53
58 enum class RunMode {
59 RESTORE ,
60 COMPUTE
61 };
62
67 enum class WriteMode {
68 DUMP ,
69 DEBUG ,
70 NONE
71 };
72
77 enum class ReconstructionMode {
80 };
81
86 enum class ErrorMode {
87 COMBINED ,
88 SINGLE ,
89 NONE
90 };
91
96 enum class MeshType {
97 UNDEFINED ,
99 };
100
101public:
102# if BITPIT_ENABLE_MPI
103 POD(MPI_Comm comm = MPI_COMM_WORLD);
104# else
105 POD();
106# endif
107
108 ~POD();
109
114 POD(POD&& other) = default;
115
116 void clear();
117
118 void setDirectory(const std::string &directory);
119 const std::string & getDirectory();
120 void setName(const std::string &name);
121 const std::string & getName();
122 void addSnapshot(const std::string &directory, const std::string &name);
123 void addSnapshot(const pod::SnapshotFile &file);
124 void setSnapshots(const std::vector<pod::SnapshotFile> &database);
125 void readSnapshot(const pod::SnapshotFile &snap, pod::PODField &fieldr);
126 void removeLeave1outSnapshot(const std::string &directory, const std::string &name);
129 void addReconstructionSnapshot(const std::string &directory, const std::string &name);
131 void setModeCount(std::size_t nmodes);
132 std::size_t getModeCount();
133 void setEnergyLevel(double energy);
134 double getEnergyLevel();
135 void setErrorThreshold(double threshold);
136 double getErrorThreshold();
137 void setTargetErrorFields(std::vector<std::string> &namesf, std::vector<std::array<std::string,3>> &namevf);
138
139 void setMeshType(MeshType type);
140 void setMesh(const std::string &directory, const std::string &name);
141 void setMesh(const pod::SnapshotFile &file);
142 void setMesh(std::unique_ptr<VolumeKernel> &&mesh);
144 void setStaticMesh(bool flag);
145 void setUseMean(bool flag);
146
147 void setMemoryMode(MemoryMode mode);
149 void setRunMode(RunMode mode);
151 void setWriteMode(WriteMode mode);
155 void setErrorMode(ErrorMode mode);
157 void setExpert(bool mode = true);
158
159 void setSensorMask(const PiercedStorage<bool> & mask, VolumeKernel * mesh = nullptr);
160
161 std::size_t getSnapshotCount();
162 std::vector<std::string> getScalarNames();
163 std::vector<std::array<std::string,3>> getVectorNames();
164 std::vector<std::string> getFieldsNames();
165
166 const VolumeKernel* getMesh();
167 const pod::PODMode & getMean();
168 const std::vector<pod::PODMode> & getModes();
169 std::vector<std::vector<double> > getReconstructionCoeffs();
170 const std::unordered_set<long int> & getListActiveIDs();
171 std::size_t getListIDInternalCount();
172 std::unique_ptr<PODKernel> & getKernel();
173
174 void run();
175 void dump();
176 void restore();
177 void leave1out();
178
179 void evalDecomposition();
180 void evalReconstruction();
181
182 void evalMeanMesh();
183 void fillListActiveIDs(const PiercedStorage<bool> &bfield);
184 void evalCorrelation();
185 void evalModes();
186 void evalEigen();
188 void computeMapper(const VolumeKernel * mesh);
189 void adaptionPrepare(const std::vector<adaption::Info> & info);
190 void adaptionAlter(const std::vector<adaption::Info> & info);
191 void adaptionCleanUp(const std::vector<adaption::Info> & info);
192
193 void dumpDecomposition();
195
196 void reconstructFields(pod::PODField &field, pod::PODField &recon);
197 void reconstructFields(const std::vector<std::vector<double>> &coeffMatrix, pod::PODField &recon);
199 std::map<std::string, std::size_t> targetFields,
200 const std::unordered_set<long> *targetCells);
201
202 std::vector<std::vector<double>> projectField(pod::PODField &field);
203
204 void dumpField(const std::string &name, const pod::PODField &field) const;
205
206 std::vector<double> fieldsl2norm(pod::PODField &snap);
207 std::vector<double> fieldsMax(pod::PODField &snap);
208
209 void write(const pod::PODField &snap, std::string file_name) const;
210 void write(int mode_index, std::string file_name);
211
212private:
213 std::unique_ptr<PODKernel> m_podkernel;
214 MeshType m_meshType;
215 bool m_staticMesh;
216 bool m_useMean;
217 std::string m_directory;
218 std::string m_name;
219 std::vector<pod::SnapshotFile> m_database;
220 std::vector<pod::SnapshotFile> m_reconstructionDatabase;
221 std::vector<pod::SnapshotFile> m_leave1outOffDatabase;
222 std::size_t m_nSnapshots;
223 std::size_t m_nReconstructionSnapshots;
224 std::size_t m_nScalarFields;
225 std::size_t m_nVectorFields;
226 std::size_t m_nFields;
227 std::vector<std::string> m_nameScalarFields;
228 std::vector<std::array<std::string,3>> m_nameVectorFields;
229 std::map<std::string, std::size_t> m_nameTargetErrorFields;
230 bool m_toUpdate;
231 PiercedStorage<bool> m_filter;
232 PiercedStorage<bool> m_sensorMask;
233 pod::PODMode m_mean;
234 pod::PODField m_errorMap;
235 std::vector<pod::PODMode> m_modes;
236 std::size_t m_nModes;
237 double m_energyLevel;
238 double m_errorThreshold;
240 std::vector<std::vector<double>> m_correlationMatrices;
241 std::vector<std::vector<double>> m_minimizationMatrices;
242 std::vector<std::vector<double>> m_lambda;
243 std::vector<std::vector<std::vector<double>>> m_podCoeffs;
244 std::vector<std::vector<double>> m_reconstructionCoeffs;
246 std::unordered_set<long int> m_listActiveIDs;
247 std::vector<std::size_t> m_listActiveIDsLeave1out;
248 std::size_t m_sizeInternal;
250#if BITPIT_ENABLE_MPI
251 MPI_Comm m_communicator;
252#endif
253 int m_rank;
254 int m_nProcs;
256 //pod options
257 MemoryMode m_memoryMode;
258 RunMode m_runMode;
259 WriteMode m_writeMode;
260 ReconstructionMode m_reconstructionMode;
261 ErrorMode m_errorMode;
262 bool m_expert;
266 std::vector<std::size_t> _m_nr;
268 const static int ARCHIVE_VERSION = 0;
269
270 const double ENERGY_CHECK_TOLERANCE = 1.0e-12;
272 void _evalMeanMesh();
273 void checkModeCount(double *alambda, std::size_t ifield);
274 void _evalModes();
275 void initCorrelation();
276 void evalCorrelationTerm(int i, pod::PODField &snapi, int j, pod::PODField &snapj);
277 void initErrorMaps();
278 void buildErrorMaps(pod::PODField &snap, pod::PODField &recon);
279 void evalMinimizationMatrices();
280 void initMinimization();
281 void solveMinimization(std::vector<std::vector<double>> &rhs);
282
283 void dumpMode(std::size_t ir);
284
285 void readMode(std::size_t ir);
286
287 double getCellVolume(long id);
288 double getRawCellVolume(long rawIndex);
289
290 void diff(pod::PODField * _a, const pod::PODMode &b);
291 void sum(pod::PODField * _a, const pod::PODMode &b);
292
293#if BITPIT_ENABLE_MPI
294 void initializeCommunicator(MPI_Comm communicator);
295 MPI_Comm getCommunicator() const;
296 bool isCommunicatorSet() const;
297 void freeCommunicator();
298#endif
299
300 void evalReconstructionCoeffs(pod::PODField &snapi);
301 void evalReconstructionCoeffs(PiercedStorage<double> &fields,
302 const std::vector<std::size_t> &scalarIds, const std::vector<std::size_t> &podscalarIds,
303 const std::vector<std::array<std::size_t, 3>> &vectorIds, const std::vector<std::size_t> &podvectorIds);
304 void buildFields(const std::vector<std::vector<double>> &reconstructionCoeffs, pod::PODField &recon);
305 void buildFields(const std::vector<std::vector<double>> &reconstructionCoeffs, PiercedStorage<double> &fields,
306 const std::vector<std::size_t> &scalarIds, const std::vector<std::size_t> &podscalarIds,
307 const std::vector<std::array<std::size_t, 3>> &vectorIds, const std::vector<std::size_t> &podvectorIds,
308 const std::unordered_set<long> *targetCells = nullptr);
309
310 void _computeMapper(const VolumeKernel * mesh);
311 void _adaptionAlter(const std::vector<adaption::Info> & info);
312
313 void diff(PiercedStorage<double> &fields, const pod::PODMode &mode,
314 const std::vector<std::size_t> &scalarIds, const std::vector<std::size_t> &podscalarIds,
315 const std::vector<std::array<std::size_t, 3>> &vectorIds, const std::vector<std::size_t> &podvectorIds,
316 const std::unordered_set<long> *targetCells = nullptr);
317 void sum(PiercedStorage<double> &fields, const pod::PODMode &mode,
318 const std::vector<std::size_t> &scalarIds, const std::vector<std::size_t> &podscalarIds,
319 const std::vector<std::array<std::size_t, 3>> &vectorIds, const std::vector<std::size_t> &podvectorIds,
320 const std::unordered_set<long> *targetCells = nullptr);
321
322};
323
324}
325
326#endif
The POD (Proper Orthogonal Decomposition) class provides an interface for defining POD object.
Definition pod.hpp:41
void setErrorThreshold(double threshold)
Definition pod.cpp:346
void computeMapper(const VolumeKernel *mesh)
Definition pod.cpp:2939
std::size_t getModeCount()
Definition pod.cpp:315
void adaptionAlter(const std::vector< adaption::Info > &info)
Definition pod.cpp:2952
void fillListActiveIDs(const PiercedStorage< bool > &bfield)
Definition pod.cpp:1316
void clear()
Definition pod.cpp:125
void run()
Definition pod.cpp:836
std::vector< std::array< std::string, 3 > > getVectorNames()
Definition pod.cpp:738
std::size_t getSnapshotCount()
Definition pod.cpp:718
void evalReconstruction()
Definition pod.cpp:1449
void evalCorrelation()
Definition pod.cpp:1345
void unsetLeave1outSnapshots()
Definition pod.cpp:262
const std::string & getDirectory()
Definition pod.cpp:197
void dumpField(const std::string &name, const pod::PODField &field) const
Definition pod.cpp:2433
const std::vector< pod::PODMode > & getModes()
Definition pod.cpp:790
void setRunMode(RunMode mode)
Definition pod.cpp:542
const VolumeKernel * getMesh()
Definition pod.cpp:767
std::vector< double > fieldsMax(pod::PODField &snap)
Definition pod.cpp:2618
std::unique_ptr< PODKernel > & getKernel()
Definition pod.cpp:824
std::vector< std::string > getScalarNames()
Definition pod.cpp:728
const std::unordered_set< long int > & getListActiveIDs()
Definition pod.cpp:808
void setTargetErrorFields(std::vector< std::string > &namesf, std::vector< std::array< std::string, 3 > > &namevf)
Definition pod.cpp:367
void setMeshType(MeshType type)
Definition pod.cpp:390
void setStaticMesh(bool flag)
Definition pod.cpp:483
void setSensorMask(const PiercedStorage< bool > &mask, VolumeKernel *mesh=nullptr)
Definition pod.cpp:672
WriteMode
Output Write Mode of the POD object. It defines the amount of information written by the POD object.
Definition pod.hpp:67
std::vector< double > fieldsl2norm(pod::PODField &snap)
Definition pod.cpp:2577
void setMemoryMode(MemoryMode mode)
Definition pod.cpp:505
void write(const pod::PODField &snap, std::string file_name) const
Definition pod.cpp:3080
double getErrorThreshold()
Definition pod.cpp:356
void setDirectory(const std::string &directory)
Definition pod.cpp:180
void adaptionPrepare(const std::vector< adaption::Info > &info)
Definition pod.cpp:2974
void adaptionCleanUp(const std::vector< adaption::Info > &info)
Definition pod.cpp:2993
double getEnergyLevel()
Definition pod.cpp:336
const std::string & getName()
Definition pod.cpp:170
RunMode getRunMode()
Definition pod.cpp:566
POD(MPI_Comm comm=MPI_COMM_WORLD)
Definition pod.cpp:67
void restoreDecomposition()
Definition pod.cpp:1051
void readSnapshot(const pod::SnapshotFile &snap, pod::PODField &fieldr)
Definition pod.cpp:2150
void leave1out()
Definition pod.cpp:1121
ReconstructionMode getReconstructionMode()
Definition pod.cpp:623
void dump()
Definition pod.cpp:855
std::vector< std::string > getFieldsNames()
Definition pod.cpp:749
void reconstructFields(pod::PODField &field, pod::PODField &recon)
Definition pod.cpp:1631
void evalErrorBoundingBox()
Definition pod.cpp:1503
MemoryMode getMemoryMode()
Definition pod.cpp:530
void setExpert(bool mode=true)
Definition pod.cpp:660
void setUseMean(bool flag)
Definition pod.cpp:493
void addReconstructionSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:282
void removeLeave1outSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:243
void evalModes()
Definition pod.cpp:2084
std::vector< std::vector< double > > projectField(pod::PODField &field)
Definition pod.cpp:1764
MeshType
Type of the Mesh used to compute the POD basis.
Definition pod.hpp:96
ReconstructionMode
Mode of Reconstruction of fields by using the POD basis.
Definition pod.hpp:77
MeshType getMeshType()
Definition pod.cpp:470
const pod::PODMode & getMean()
Definition pod.cpp:780
MemoryMode
Memory Mode of the POD object. It defines the use of the memory resources.
Definition pod.hpp:49
void setSnapshots(const std::vector< pod::SnapshotFile > &database)
Definition pod.cpp:230
void setWriteMode(WriteMode mode)
Definition pod.cpp:576
ErrorMode getErrorMode()
Definition pod.cpp:648
std::size_t getListIDInternalCount()
Definition pod.cpp:816
POD(POD &&other)=default
void setReconstructionMode(ReconstructionMode mode)
Definition pod.cpp:598
ErrorMode
Mode of Error evaluation of a reconstructed fields by the POD basis.
Definition pod.hpp:86
void dumpDecomposition()
Definition pod.cpp:904
void restore()
Definition pod.cpp:864
void setEnergyLevel(double energy)
Definition pod.cpp:326
void setModeCount(std::size_t nmodes)
Definition pod.cpp:304
void setName(const std::string &name)
Definition pod.cpp:160
WriteMode getWriteMode()
Definition pod.cpp:587
void setErrorMode(ErrorMode mode)
Definition pod.cpp:635
void evalDecomposition()
Definition pod.cpp:873
void setMesh(const std::string &directory, const std::string &name)
Definition pod.cpp:443
void evalMeanMesh()
Definition pod.cpp:1200
void evalEigen()
Definition pod.cpp:1973
std::vector< std::vector< double > > getReconstructionCoeffs()
Definition pod.cpp:800
void addSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:208
RunMode
Run Mode of the POD object. It defines if the POD basis has to be computed or restored.
Definition pod.hpp:58
Metafunction for generating a pierced storage.
The base class to be used to derive VTK streamers form.
Definition VTK.hpp:209
The VolumeKernel class provides an interface for defining volume patches.
The PODfield structure is used to store the fields inside POD classes.
The PODMode structure is used to store the modes inside pod classes.
The SnapFile structure is used to store the file names inside POD classes.
--- layout: doxygen_footer ---