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  std::shared_ptr<Vector> state_offset = nullptr);
85 
92  DMD(std::string base_file_name);
93 
97  virtual ~DMD() { };
98 
102  virtual void setOffset(std::shared_ptr<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  std::unique_ptr<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  std::unique_ptr<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>(std::unique_ptr<DMD>& parametric_dmd,
240  const std::vector<Vector>& parameter_points,
241  std::vector<DMD*>& dmds,
242  const 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,
257  std::shared_ptr<Vector> state_offset = nullptr);
258 
270  DMD(std::vector<std::complex<double>> & eigs,
271  std::shared_ptr<Matrix> & phi_real,
272  std::shared_ptr<Matrix> & phi_imaginary, int k, double dt,
273  double t_offset, std::shared_ptr<Vector> & state_offset);
274 
278  DMD();
279 
283  DMD(const DMD& other);
284 
288  DMD&
290  const DMD& rhs);
291 
295  std::pair<std::shared_ptr<Matrix>,std::shared_ptr<Matrix>> phiMultEigs(
296  double t, int deg = 0);
297 
301  void constructDMD(const Matrix & f_snapshots,
302  int rank,
303  int num_procs,
304  const Matrix* W0,
305  double linearity_tol);
306 
310  virtual std::pair<Matrix*, Matrix*> computeDMDSnapshotPair(
311  const Matrix & snapshots);
312 
316  virtual void computePhi(DMDInternal dmd_internal_obj);
317 
321  virtual std::complex<double> computeEigExp(std::complex<double> eig, double t);
322 
326  virtual void addOffset(Vector & result, double t = 0.0, int deg = 0);
327 
331  std::unique_ptr<const Matrix>
332  createSnapshotMatrix(const std::vector<std::shared_ptr<Vector>> & snapshots);
333 
337  int d_rank;
338 
343 
347  int d_dim;
348 
352  double d_dt = -1.0;
353 
357  double d_t_offset;
358 
362  std::vector<std::shared_ptr<Vector>> d_snapshots;
363 
367  std::vector<double> d_sampled_times;
368 
372  std::shared_ptr<Vector> d_state_offset;
373 
377  bool d_trained;
378 
383 
388 
392  std::vector<double> d_sv;
393 
398 
402  int d_k;
403 
407  std::shared_ptr<Matrix> d_basis;
408 
412  bool d_alt_output_basis = false;
413 
417  std::shared_ptr<Matrix> d_A_tilde;
418 
422  std::shared_ptr<Matrix> d_phi_real;
423 
427  std::shared_ptr<Matrix> d_phi_imaginary;
428 
432  std::unique_ptr<Matrix> d_phi_real_squared_inverse;
433 
437  std::unique_ptr<Matrix> d_phi_imaginary_squared_inverse;
438 
442  std::shared_ptr<Vector> d_projected_init_real;
443 
447  std::shared_ptr<Vector> d_projected_init_imaginary;
448 
452  std::vector<std::complex<double>> d_eigs;
453 
454 };
455 
456 }
457 
458 #endif
Definition: DMD.h:71
void constructDMD(const Matrix &f_snapshots, int rank, int num_procs, const Matrix *W0, double linearity_tol)
Construct the DMD object.
Definition: DMD.cpp:256
std::vector< std::complex< double > > d_eigs
A vector holding the complex eigenvalues of the eigenmodes.
Definition: DMD.h:452
virtual void addOffset(Vector &result, double t=0.0, int deg=0)
Add the appropriate offset when predicting the solution.
Definition: DMD.cpp:636
virtual ~DMD()
Destroy the DMD object.
Definition: DMD.h:97
int getNumSamples() const
Returns the number of samples taken.
Definition: DMD.h:166
double d_energy_fraction
The energy fraction used to obtain the DMD modes.
Definition: DMD.h:397
int d_dim
The total dimension of the sample vector.
Definition: DMD.h:347
std::unique_ptr< Matrix > d_phi_real_squared_inverse
The real part of d_phi_squared_inverse.
Definition: DMD.h:432
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:180
bool d_init_projected
Whether the initial condition has been projected.
Definition: DMD.h:382
std::shared_ptr< Vector > d_projected_init_imaginary
The imaginary part of the projected initial condition.
Definition: DMD.h:447
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:140
virtual std::pair< Matrix *, Matrix * > computeDMDSnapshotPair(const Matrix &snapshots)
Returns a pair of pointers to the minus and plus snapshot matrices.
Definition: DMD.cpp:200
std::shared_ptr< Matrix > d_phi_imaginary
The imaginary part of d_phi.
Definition: DMD.h:427
virtual std::complex< double > computeEigExp(std::complex< double > eig, double t)
Compute the appropriate exponential function when predicting the solution.
Definition: DMD.cpp:645
void summary(std::string base_file_name)
Output the DMD record in CSV files.
Definition: DMD.cpp:905
std::pair< std::shared_ptr< Matrix >, std::shared_ptr< Matrix > > phiMultEigs(double t, int deg=0)
Internal function to multiply d_phi with the eigenvalues.
Definition: DMD.cpp:651
std::unique_ptr< const Matrix > getSnapshotMatrix()
Get the snapshot matrix contained within d_snapshots.
Definition: DMD.cpp:693
double d_dt
The time step size between samples.
Definition: DMD.h:352
virtual void save(std::string base_file_name)
Save the object state to a file.
Definition: DMD.cpp:816
std::vector< double > d_sv
std::vector holding the signular values.
Definition: DMD.h:392
double getTimeOffset() const
Get the time offset contained within d_t_offset.
Definition: DMD.cpp:687
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:514
std::unique_ptr< const Matrix > createSnapshotMatrix(const std::vector< std::shared_ptr< Vector >> &snapshots)
Get the snapshot matrix contained within d_snapshots.
Definition: DMD.cpp:699
int d_num_singular_vectors
The maximum number of singular vectors.
Definition: DMD.h:387
std::vector< double > d_sampled_times
The stored times of each sample.
Definition: DMD.h:367
double d_t_offset
The time offset of the first sample.
Definition: DMD.h:357
std::vector< std::shared_ptr< Vector > > d_snapshots
std::vector holding the snapshots.
Definition: DMD.h:362
DMD(const DMD &other)
Unimplemented copy constructor.
int d_rank
The rank of the process this object belongs to.
Definition: DMD.h:337
DMD()
Unimplemented default constructor.
DMD & operator=(const DMD &rhs)
Unimplemented assignment operator.
std::shared_ptr< Matrix > d_basis
The left singular vector basis.
Definition: DMD.h:407
std::unique_ptr< 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:609
int d_num_procs
The number of processors being run on.
Definition: DMD.h:342
std::shared_ptr< Vector > d_state_offset
State offset in snapshot.
Definition: DMD.h:372
bool d_alt_output_basis
Whether to use the alternative basis for output, i.e. phi = U^(+)*V*Omega^(-1)*X.
Definition: DMD.h:412
virtual void load(std::string base_file_name)
Load the object state from a file.
Definition: DMD.cpp:725
virtual void setOffset(std::shared_ptr< Vector > &offset_vector, int order)
Set the offset of a certain order.
Definition: DMD.cpp:132
std::shared_ptr< Matrix > d_A_tilde
A_tilde.
Definition: DMD.h:417
bool d_trained
Whether the DMD has been trained or not.
Definition: DMD.h:377
std::shared_ptr< Vector > d_projected_init_real
The real part of the projected initial condition.
Definition: DMD.h:442
virtual void computePhi(DMDInternal dmd_internal_obj)
Compute phi.
Definition: DMD.cpp:233
std::shared_ptr< Matrix > d_phi_real
The real part of d_phi.
Definition: DMD.h:422
std::unique_ptr< Matrix > d_phi_imaginary_squared_inverse
The imaginary part of d_phi_squared_inverse.
Definition: DMD.h:437
int d_k
The number of columns used after obtaining the SVD decomposition.
Definition: DMD.h:402
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