-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathBasisGenerator.h
More file actions
390 lines (344 loc) · 10 KB
/
Copy pathBasisGenerator.h
File metadata and controls
390 lines (344 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/******************************************************************************
*
* Copyright (c) 2013-2024, Lawrence Livermore National Security, LLC
* and other libROM project developers. See the top-level COPYRIGHT
* file for details.
*
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
*
*****************************************************************************/
// Description: The wrapper class for an SVD algorithm and
// sampler. This class controls all aspects of basis
// vector generation.
#ifndef included_BasisGenerator_h
#define included_BasisGenerator_h
#include "BasisWriter.h"
#include "BasisReader.h"
#include "Options.h"
#include "svd/SVD.h"
#include "mpi.h"
#include <cmath>
/* Use C++11 built-in shared pointers if available; else fallback to Boost. */
#if __cplusplus >= 201103L
#include <memory>
#else
#include <boost/shared_ptr.hpp>
#endif
#include <string.h>
namespace CAROM {
class BasisWriter;
class BasisReader;
class Matrix;
/**
* Class BasisGenerator defines the interface for the generation of basis
* vectors via the SVD method. This class wraps the SVD algorithm and sampler
* and controls all aspects of basis vector generation.
*/
class BasisGenerator
{
public:
/**
* @brief Constructor.
*
* @param[in] options The struct containing the options for this basis
* generator.
* @param[in] incremental Whether to conduct static or incremental SVD
* @param[in] basis_file_name The base part of the name of the file
* containing the basis vectors. Each process
* will append its process ID to this base
* name.
* @param[in] file_format The format of the file containing the basis
* vectors.
*/
BasisGenerator(
Options options,
bool incremental,
const std::string& basis_file_name = "",
Database::formats file_format = Database::formats::HDF5);
/**
* @brief Destructor.
*/
~BasisGenerator();
/**
* @brief Returns true if it is time for the next svd sample.
*
* @pre time >= 0.0
*
* @param[in] time Time of interest.
*
* @return True if it is time for the next sample to be taken.
*/
bool
isNextSample(
double time);
/**
* @brief Check whether right basis vectors will be updated.
*
* * @return True if the right basis vectors will be updated.
*/
bool
updateRightSV()
{
return d_update_right_SV;
}
/**
* @brief Sample the new state, u_in, at the given time.
*
* @pre u_in != 0
* @pre time >= 0.0
*
* @param[in] u_in The state at the specified time.
* @param[in] add_without_increase If true, the addLinearlyDependent is
* invoked. This only applies to incremental
* SVD.
*
* @return True if the sampling was successful.
*/
bool
takeSample(
double* u_in,
bool add_without_increase = false);
/**
* @brief Signal that the final sample has been taken.
*
* @param[in] kind A string equal to "basis" or "snapshot", representing
* which one will be written.
*/
void
endSamples(const std::string& kind = "basis")
{
if (d_basis_writer) {
d_basis_writer->writeBasis(kind);
}
}
/**
* @brief Write current snapshot matrix.
*/
void
writeSnapshot()
{
if (d_basis_writer) {
d_basis_writer->writeBasis("snapshot");
}
}
/**
* @brief Load previously saved sample (basis or state)
* within a column range.
*
* @param[in] base_file_name The base part of the name of the files
* holding the basis/snapshot vectors.
* @param[in] kind A string equal to "basis" or "snapshot", representing
* which kind of data to load.
* @param[in] col_min The first basis/snapshot vector to read.
* @param[in] col_max The last basis/snapshot vector to read.
* @param[in] db_format Format of the file to read.
*/
void
loadSampleRange(const std::string& base_file_name,
const std::string& kind = "basis",
int col_min = 0,
int col_max = 1e9,
Database::formats db_format = Database::formats::HDF5);
/**
* @brief Load previously saved sample (basis or state).
*
* @param[in] base_file_name The base part of the name of the files
* holding the basis/snapshot vectors.
* @param[in] kind A string equal to "basis" or "snapshot", representing
* which kind of data to load.
* @param[in] cutoff The maximum number of basis/snapshot vectors to read.
* @param[in] db_format Format of the file to read.
*/
void
loadSamples(const std::string& base_file_name,
const std::string& kind = "basis",
int cut_off = 1e9,
Database::formats db_format = Database::formats::HDF5);
/**
* @brief Computes next time an svd sample is needed.
*
* @pre u_in != 0
* @pre rhs_in != 0
* @pre time >= 0.0
*
* @param[in] u_in The state at the specified time.
* @param[in] rhs_in The right hand side at the specified time.
* @param[in] time The simulation time for the state.
*/
double
computeNextSampleTime(
double* u_in,
double* rhs_in,
double time);
/**
* @brief Returns the basis vectors for the current time interval as a
* Matrix.
*
* @return The basis vectors for the current time interval.
*/
std::shared_ptr<const Matrix>
getSpatialBasis()
{
return d_svd->getSpatialBasis();
}
/**
* @brief Returns the temporal basis vectors for the current time interval as a
* Matrix.
*
* @return The temporal basis vectors for the current time interval.
*/
std::shared_ptr<const Matrix>
getTemporalBasis()
{
return d_svd->getTemporalBasis();
}
/**
* @brief Returns the singular values for the current time interval as a
* Vector.
*
* @return The singular values for the current time interval.
*/
std::shared_ptr<const Vector>
getSingularValues()
{
return d_svd->getSingularValues();
}
/**
* @brief Returns the snapshot matrix for the current time interval.
*
* @return The snapshot matrix for the current time interval.
*/
std::shared_ptr<const Matrix>
getSnapshotMatrix()
{
return d_svd->getSnapshotMatrix();
}
/**
* @brief Returns the number of samples taken.
*
* @return The number of samples taken.
*/
int getNumSamples() const
{
return d_svd->getNumSamples();
}
/**
* @brief Prints the summary of recommended numbers of basis vectors.
*
* @param[in] energyFractionThreshold Energy Fraction threshold
* (energy fraction = 1.0 - energyFractionThreshold).
* @param[in] cutoff Number of basis vectors selected.
* @param[in] cutoffOutputPath Path of the summary file.
* @param[in] first_sv First singular vector in the calculaton of energy.
*/
void finalSummary(
const double energyFractionThreshold,
int & cutoff,
const std::string & cutoffOutputPath = "",
const int first_sv = 0, const bool squareSV = true);
protected:
/**
* @brief Writer of basis vectors.
*/
BasisWriter* d_basis_writer;
/**
* @brief Reader of basis vectors.
*/
BasisReader* d_basis_reader;
/**
* @brief Whether to write snapshots instead of bases.
*/
bool d_write_snapshots;
/**
* @brief Pointer to the abstract SVD algorithm object.
*/
#if __cplusplus >= 201103L
std::shared_ptr<SVD> d_svd;
#else
boost::shared_ptr<SVD> d_svd;
#endif
private:
/**
* @brief Unimplemented default constructor.
*/
BasisGenerator();
/**
* @brief Unimplemented copy constructor.
*/
BasisGenerator(
const BasisGenerator& other);
/**
* @brief Unimplemented assignment operator.
*/
BasisGenerator&
operator = (
const BasisGenerator& rhs);
/**
* @brief Resets sample time step.
*
* @param[in] new_dt New value of sample time step.
*/
void
resetDt(
double new_dt);
/**
* @brief Returns the dimension of the system on this processor.
*
* @return The dimension of the system on this processor.
*/
int
getDim()
{
return d_dim;
}
/**
* @brief If using incremental or static SVD
*/
bool d_incremental;
/**
* @brief if true, isNextSample returns always true
*/
bool d_update_right_SV;
/**
* @brief Sampling control tolerance.
*
* Limits error in projection of solution into the reduced order space.
*/
double d_tol;
/**
* @brief Maximum time between samples.
*/
double d_max_time_between_samples;
/**
* @brief Minimum sampling time step scale factor.
*/
double d_min_sampling_time_step_scale;
/**
* @brief Sampling time step scale factor to apply to algorithm.
*/
double d_sampling_time_step_scale;
/**
* @brief Maximum sampling time step scale factor.
*/
double d_max_sampling_time_step_scale;
/**
* @brief Current time step.
*/
double d_dt;
/**
* @brief Next time at which a sample should be taken.
*/
double d_next_sample_time;
/**
* @brief The number of processors being run on.
*/
int d_num_procs;
/**
* @brief Dimension of the system on this processor.
*
* Equivalent to d_svd->getDim().
*/
const int d_dim;
};
}
#endif