forked from LLNL/libROM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVDBasisGenerator.h
249 lines (225 loc) · 7.7 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
/******************************************************************************
*
* Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory
* Written by William Arrighi [email protected]
* CODE-686965
* All rights reserved.
*
* This file is part of libROM.
* For details, see https://computation.llnl.gov/librom
* Please also read README_BSD_NOTICE.
*
* Redistribution and use in source and binary forms, with or without
* modifications, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* o Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the disclaimer (as noted below) in
* the documentation and/or other materials provided with the
* distribution.
* o Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OR SUCH DAMAGE.
*
*****************************************************************************/
// 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"
#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 Constructor.
*
* @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 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 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(
const double* u_in,
double time,
double dt)
{
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);
if (d_basis_writer) {
d_basis_writer->writeBasis();
}
}
return d_svdsampler->takeSample(u_in, time);
}
/**
* @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*
getBasis()
{
return d_svdsampler->getBasis();
}
/**
* @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 Writer of basis vectors.
*/
BasisWriter* d_basis_writer;
/**
* @brief Pointer to the underlying sampling control object.
*/
boost::shared_ptr<SVDSampler> d_svdsampler;
private:
/**
* @brief Unimplemented copy constructor.
*/
SVDBasisGenerator(
const SVDBasisGenerator& other);
/**
* @brief Unimplemented assignment operator.
*/
SVDBasisGenerator&
operator = (
const SVDBasisGenerator& rhs);
};
}
#endif