forked from LLNL/libROM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVDBasisGenerator.h
274 lines (243 loc) · 7.69 KB
/
SVDBasisGenerator.h
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
/******************************************************************************
*
* Copyright (c) 2013-2019, 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 abstract wrapper class for an abstract SVD algorithm and
// sampler. This class provides interfaces to each so that an
// application only needs to instantiate one concrete
// implementation of this class to control all aspects of basis
// vector generation.
#ifndef included_SVDBasisGenerator_h
#define included_SVDBasisGenerator_h
#include "BasisWriter.h"
#include "SVDSampler.h"
/* 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 Matrix;
/**
* Class SVDBasisGenerator is an abstract base class defining the interface for
* the generation of basis vectors via the svd method. This class wraps the
* abstract SVD algorithm and sampler and provides interfaces to each so
* that an application only needs to instantiate one concrete implementation of
* this class to control all aspects of basis vector generation.
*/
class SVDBasisGenerator
{
public:
/**
* @brief Destructor.
*/
virtual
~SVDBasisGenerator();
/**
* @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)
{
CAROM_ASSERT(time >= 0.0);
return d_svdsampler->isNextSample(time);
}
/**
* @brief Returns true if it needs to update right basis vectors.
*/
bool
updateRightSV() {return d_svdsampler->isUpdateRightSV(); };
/**
* @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] time The simulation time for the state.
* @param[in] dt The current simulation dt.
*
* @return True if the sampling was successful.
*/
bool
takeSample(
double* u_in,
double time,
double dt,
bool add_without_increase = false)
{
CAROM_ASSERT(u_in != 0);
CAROM_ASSERT(time >= 0);
// Check that u_in is not non-zero.
Vector u_vec(u_in, d_svdsampler->getDim(), true);
if (u_vec.norm() == 0.0) {
return false;
}
if (getNumBasisTimeIntervals() > 0 &&
d_svdsampler->isNewTimeInterval()) {
d_svdsampler->resetDt(dt);
// YC: commenting this out unless someone think this is necessary
// we call writeBasis in "endSamples()" function below.
// I think that one call is enough.
// I will remove completely if no one has other opinion.
//if (d_basis_writer) {
// d_basis_writer->writeBasis();
//}
}
return d_svdsampler->takeSample(u_in, time, add_without_increase);
}
/**
* @brief Signal that the final sample has been taken.
*/
void
endSamples()
{
if (d_basis_writer) {
d_basis_writer->writeBasis();
}
}
/**
* @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)
{
CAROM_ASSERT(u_in != 0);
CAROM_ASSERT(rhs_in != 0);
CAROM_ASSERT(time >= 0);
return d_svdsampler->computeNextSampleTime(u_in, rhs_in, time);
}
/**
* @brief Returns the basis vectors for the current time interval as a
* Matrix.
*
* @return The basis vectors for the current time interval.
*/
const Matrix*
getSpatialBasis()
{
return d_svdsampler->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.
*/
const Matrix*
getTemporalBasis()
{
return d_svdsampler->getTemporalBasis();
}
/**
* @brief Returns the singular values for the current time interval as a
* Matrix.
*
* @return The singular values for the current time interval.
*/
const Matrix*
getSingularValues()
{
return d_svdsampler->getSingularValues();
}
/**
* @brief Returns the number of time intervals on which different sets of
* basis vectors are defined.
*
* @return The number of time intervals on which there are basis vectors.
*/
int
getNumBasisTimeIntervals() const
{
return d_svdsampler->getNumBasisTimeIntervals();
}
/**
* @brief Returns the start time for the requested time interval.
*
* @pre 0 <= which_interval
* @pre which_interval < getNumBasisTimeIntervals()
*
* @param[in] which_interval Time interval whose start time is needed.
*
* @return The start time for the requested time interval.
*/
double
getBasisIntervalStartTime(
int which_interval) const
{
CAROM_ASSERT(0 <= which_interval);
CAROM_ASSERT(which_interval < getNumBasisTimeIntervals());
return d_svdsampler->getBasisIntervalStartTime(which_interval);
}
protected:
/**
* @brief Constructor.
*
* Although all member functions are implemented by delegation to either
* d_basis_writer or d_svdsampler, this class is still abstract. In this
* context it is not yet known which SVDSampler to instantiate. Hence an
* instance of this class may not be constructed.
*
* @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.
*/
SVDBasisGenerator(
const std::string& basis_file_name,
Database::formats file_format = Database::HDF5);
/**
* @brief Writer of basis vectors.
*/
BasisWriter* d_basis_writer;
/**
* @brief Pointer to the underlying sampling control object.
*/
#if __cplusplus >= 201103L
std::shared_ptr<SVDSampler> d_svdsampler;
#else
boost::shared_ptr<SVDSampler> d_svdsampler;
#endif
private:
/**
* @brief Unimplemented copy constructor.
*/
SVDBasisGenerator(
const SVDBasisGenerator& other);
/**
* @brief Unimplemented assignment operator.
*/
SVDBasisGenerator&
operator = (
const SVDBasisGenerator& rhs);
};
}
#endif