libROM  v1.0
Data-driven physical simulation library
DMD.h
1 
11 // Description: Computes the DMD algorithm on the given snapshot matrix. The
12 // implemented dynamic mode decomposition algorithm is derived from
13 // Tu et. al's paper "On Dynamic Mode Decomposition: Theory and
14 // Applications": https://arxiv.org/abs/1312.0041
15 // This algorithm also works in the case that the first sample does
16 // not start from t = 0.0 by incorporating a time offset.
17 
18 #ifndef included_DMD_h
19 #define included_DMD_h
20 
21 #include "ParametricDMD.h"
22 #include <vector>
23 #include <complex>
24 
25 namespace CAROM {
26 
27 class Matrix;
28 class Vector;
29 class ComplexEigenPair;
30 
35 {
40 
45 
50 
55 
60 
65 };
66 
70 class DMD
71 {
72 public:
73 
83  DMD(int dim, double dt, bool alt_output_basis = false,
84  Vector* state_offset = NULL);
85 
92  DMD(std::string base_file_name);
93 
97  virtual ~DMD();
98 
102  virtual void setOffset(Vector* offset_vector, int order);
103 
114  virtual void takeSample(double* u_in, double t);
115 
124  virtual void train(double energy_fraction, const Matrix* W0 = NULL,
125  double linearity_tol = 0.0);
126 
135  virtual void train(int k, const Matrix* W0 = NULL, double linearity_tol = 0.0);
136 
145  void projectInitialCondition(const Vector* init, double t_offset = -1.0);
146 
154  Vector* predict(double t, int deg = 0);
155 
159  double getTimeOffset() const;
160 
166  int getNumSamples() const
167  {
168  return d_snapshots.size();
169  }
170 
171  int getDimension() const
172  {
173  return d_k;
174  }
175 
179  const Matrix* getSnapshotMatrix();
180 
187  virtual void load(std::string base_file_name);
188 
195  void load(const char* base_file_name);
196 
203  virtual void save(std::string base_file_name);
204 
211  void save(const char* base_file_name);
212 
216  void summary(std::string base_file_name);
217 
218 protected:
239  friend void getParametricDMD<DMD>(DMD*& parametric_dmd,
240  std::vector<Vector*>& parameter_points,
241  std::vector<DMD*>& dmds,
242  Vector* desired_point,
243  std::string rbf,
244  std::string interp_method,
245  double closest_rbf_val,
246  bool reorthogonalize_W);
247 
256  DMD(int dim, bool alt_output_basis = false, Vector* state_offset = NULL);
257 
269  DMD(std::vector<std::complex<double>> eigs, Matrix* phi_real,
270  Matrix* phi_imaginary, int k,
271  double dt, double t_offset, Vector* state_offset);
272 
276  DMD();
277 
281  DMD(const DMD& other);
282 
286  DMD&
288  const DMD& rhs);
289 
293  std::pair<Matrix*, Matrix*> phiMultEigs(double t, int deg = 0);
294 
298  void constructDMD(const Matrix* f_snapshots,
299  int rank,
300  int num_procs,
301  const Matrix* W0,
302  double linearity_tol);
303 
307  virtual std::pair<Matrix*, Matrix*> computeDMDSnapshotPair(
308  const Matrix* snapshots);
309 
313  virtual void computePhi(DMDInternal dmd_internal_obj);
314 
318  virtual std::complex<double> computeEigExp(std::complex<double> eig, double t);
319 
323  virtual void addOffset(Vector*& result, double t = 0.0, int deg = 0);
324 
328  const Matrix* createSnapshotMatrix(std::vector<Vector*> snapshots);
329 
333  int d_rank;
334 
339 
343  int d_dim;
344 
348  double d_dt = -1.0;
349 
353  double d_t_offset;
354 
358  std::vector<Vector*> d_snapshots;
359 
363  std::vector<Vector*> d_sampled_times;
364 
369 
373  bool d_trained;
374 
379 
384 
388  std::vector<double> d_sv;
389 
394 
398  int d_k;
399 
403  Matrix* d_basis = NULL;
404 
408  bool d_alt_output_basis = false;
409 
413  Matrix* d_A_tilde = NULL;
414 
419 
424 
429 
434 
439 
444 
448  std::vector<std::complex<double>> d_eigs;
449 
450 };
451 
452 }
453 
454 #endif
Definition: DMD.h:71
std::pair< Matrix *, Matrix * > phiMultEigs(double t, int deg=0)
Internal function to multiply d_phi with the eigenvalues.
Definition: DMD.cpp:689
Matrix * d_phi_real
The real part of d_phi.
Definition: DMD.h:418
std::vector< std::complex< double > > d_eigs
A vector holding the complex eigenvalues of the eigenmodes.
Definition: DMD.h:448
Matrix * d_phi_real_squared_inverse
The real part of d_phi_squared_inverse.
Definition: DMD.h:428
int getNumSamples() const
Returns the number of samples taken.
Definition: DMD.h:166
virtual ~DMD()
Destroy the DMD object.
Definition: DMD.cpp:130
double d_energy_fraction
The energy fraction used to obtain the DMD modes.
Definition: DMD.h:393
int d_dim
The total dimension of the sample vector.
Definition: DMD.h:343
Vector * predict(double t, int deg=0)
Predict state given a time. Uses the projected initial condition of the training dataset (the first c...
Definition: DMD.cpp:645
virtual void train(double energy_fraction, const Matrix *W0=NULL, double linearity_tol=0.0)
Train the DMD model with energy fraction criterion.
Definition: DMD.cpp:203
bool d_init_projected
Whether the initial condition has been projected.
Definition: DMD.h:378
Matrix * d_A_tilde
A_tilde.
Definition: DMD.h:413
virtual void takeSample(double *u_in, double t)
Sample the new state, u_in. Any samples in d_snapshots taken at the same or later time will be erased...
Definition: DMD.cpp:159
virtual void addOffset(Vector *&result, double t=0.0, int deg=0)
Add the appropriate offset when predicting the solution.
Definition: DMD.cpp:674
virtual std::complex< double > computeEigExp(std::complex< double > eig, double t)
Compute the appropriate exponential function when predicting the solution.
Definition: DMD.cpp:683
const Matrix * createSnapshotMatrix(std::vector< Vector * > snapshots)
Get the snapshot matrix contained within d_snapshots.
Definition: DMD.cpp:734
void constructDMD(const Matrix *f_snapshots, int rank, int num_procs, const Matrix *W0, double linearity_tol)
Construct the DMD object.
Definition: DMD.cpp:286
void projectInitialCondition(const Vector *init, double t_offset=-1.0)
Project new initial condition using d_phi. Calculate pinv(phi) x init, or more precisely,...
Definition: DMD.cpp:541
void summary(std::string base_file_name)
Output the DMD record in CSV files.
Definition: DMD.cpp:939
virtual std::pair< Matrix *, Matrix * > computeDMDSnapshotPair(const Matrix *snapshots)
Returns a pair of pointers to the minus and plus snapshot matrices.
Definition: DMD.cpp:227
double d_dt
The time step size between samples.
Definition: DMD.h:348
Vector * d_state_offset
State offset in snapshot.
Definition: DMD.h:368
virtual void save(std::string base_file_name)
Save the object state to a file.
Definition: DMD.cpp:850
const Matrix * getSnapshotMatrix()
Get the snapshot matrix contained within d_snapshots.
Definition: DMD.cpp:728
std::vector< double > d_sv
std::vector holding the signular values.
Definition: DMD.h:388
virtual void setOffset(Vector *offset_vector, int order)
Set the offset of a certain order.
Definition: DMD.cpp:151
std::vector< Vector * > d_sampled_times
The stored times of each sample.
Definition: DMD.h:363
double getTimeOffset() const
Get the time offset contained within d_t_offset.
Definition: DMD.cpp:722
int d_num_singular_vectors
The maximum number of singular vectors.
Definition: DMD.h:383
Matrix * d_phi_imaginary
The imaginary part of d_phi.
Definition: DMD.h:423
double d_t_offset
The time offset of the first sample.
Definition: DMD.h:353
Vector * d_projected_init_real
The real part of the projected initial condition.
Definition: DMD.h:438
DMD(const DMD &other)
Unimplemented copy constructor.
int d_rank
The rank of the process this object belongs to.
Definition: DMD.h:333
std::vector< Vector * > d_snapshots
std::vector holding the snapshots.
Definition: DMD.h:358
DMD()
Unimplemented default constructor.
DMD & operator=(const DMD &rhs)
Unimplemented assignment operator.
Matrix * d_basis
The left singular vector basis.
Definition: DMD.h:403
int d_num_procs
The number of processors being run on.
Definition: DMD.h:338
bool d_alt_output_basis
Whether to use the alternative basis for output, i.e. phi = U^(+)*V*Omega^(-1)*X.
Definition: DMD.h:408
virtual void load(std::string base_file_name)
Load the object state from a file.
Definition: DMD.cpp:759
Matrix * d_phi_imaginary_squared_inverse
The imaginary part of d_phi_squared_inverse.
Definition: DMD.h:433
Vector * d_projected_init_imaginary
The imaginary part of the projected initial condition.
Definition: DMD.h:443
bool d_trained
Whether the DMD has been trained or not.
Definition: DMD.h:373
virtual void computePhi(DMDInternal dmd_internal_obj)
Compute phi.
Definition: DMD.cpp:260
int d_k
The number of columns used after obtaining the SVD decomposition.
Definition: DMD.h:398
Matrix * snapshots_in
The snapshot vectors of input.
Definition: DMD.h:39
ComplexEigenPair * eigenpair
The resultant DMD eigenvalues and eigenvectors.
Definition: DMD.h:64
Matrix * snapshots_out
The snapshot vectors of output.
Definition: DMD.h:44
Matrix * basis
The left singular vector basis.
Definition: DMD.h:49
Matrix * basis_right
The right singular vector basis.
Definition: DMD.h:54
Matrix * S_inv
The inverse of singular values.
Definition: DMD.h:59