libROM  v1.0
Data-driven physical simulation library
DMDc.h
1 
11 // Description: Computes the DMDc algorithm on the given snapshot matrix and control matrix. The
12 // implemented dynamic mode decomposition with control algorithm is derived from
13 // Proctor et. al's paper "Dynamic mode decomposition with control":
14 // https://arxiv.org/abs/1409.6358
15 
16 #ifndef included_DMDc_h
17 #define included_DMDc_h
18 
19 #include "ParametricDMDc.h"
20 #include <vector>
21 #include <complex>
22 
23 namespace CAROM {
24 
25 class Matrix;
26 class Vector;
27 class ComplexEigenPair;
28 
32 class DMDc
33 {
34 public:
35 
44  DMDc(int dim, int dim_c, double dt,
45  std::shared_ptr<Vector> state_offset = nullptr);
46 
53  DMDc(std::string base_file_name);
54 
58  virtual ~DMDc() {};
59 
63  virtual void setOffset(std::shared_ptr<Vector> & offset_vector);
64 
77  virtual void takeSample(double* u_in, double t, double* f_in,
78  bool last_step = false);
79 
91  virtual void train(double energy_fraction, const Matrix* B = NULL);
92 
104  virtual void train(int k, const Matrix* B = NULL);
105 
115  void project(const Vector & init, const Matrix & controls,
116  double t_offset = -1.0);
117 
124  std::unique_ptr<Vector> predict(double t);
125 
129  double getTimeOffset() const;
130 
136  int getNumSamples() const
137  {
138  return d_snapshots.size();
139  }
140 
141  int getDimension() const
142  {
143  return d_k;
144  }
145 
149  std::unique_ptr<const Matrix> getSnapshotMatrix();
150 
157  virtual void load(std::string base_file_name);
158 
165  void load(const char* base_file_name);
166 
173  virtual void save(std::string base_file_name);
174 
181  void save(const char* base_file_name);
182 
186  void summary(std::string base_file_name);
187 
188 protected:
213  friend void getParametricDMDc<DMDc>(std::unique_ptr<DMDc>& parametric_dmdc,
214  const std::vector<Vector>& parameter_points,
215  std::vector<DMDc*>& dmdcs,
216  std::vector<std::shared_ptr<Matrix>> & controls,
217  std::shared_ptr<Matrix> & controls_interpolated,
218  const Vector & desired_point,
219  std::string rbf,
220  std::string interp_method,
221  double closest_rbf_val,
222  bool reorthogonalize_W);
223 
231  DMDc(int dim, int dim_c, std::shared_ptr<Vector> state_offset = nullptr);
232 
248  DMDc(std::vector<std::complex<double>> & eigs,
249  std::shared_ptr<Matrix> & phi_real,
250  std::shared_ptr<Matrix> & phi_imaginary,
251  std::shared_ptr<Matrix> & B_tilde,
252  int k, double dt, double t_offset,
253  std::shared_ptr<Vector> & state_offset,
254  std::shared_ptr<Matrix> & basis);
255 
259  DMDc();
260 
264  DMDc(const DMDc& other);
265 
269  DMDc&
271  const DMDc& rhs);
272 
276  std::pair<std::shared_ptr<Matrix>,std::shared_ptr<Matrix>>
277  phiMultEigs(double t);
278 
282  void constructDMDc(const Matrix & f_snapshots,
283  const Matrix & f_controls,
284  int rank,
285  int num_procs,
286  const Matrix* B);
287 
291  virtual std::pair<Matrix*, Matrix*> computeDMDcSnapshotPair(
292  const Matrix & snapshots, const Matrix & controls, const Matrix* B);
293 
297  virtual std::complex<double> computeEigExp(std::complex<double> eig, double t);
298 
302  virtual void addOffset(Vector & result);
303 
307  std::unique_ptr<const Matrix>
308  createSnapshotMatrix(const std::vector<std::shared_ptr<Vector>> & snapshots);
309 
313  int d_rank;
314 
319 
323  int d_dim;
324 
328  int d_dim_c;
329 
333  double d_dt = -1.0;
334 
338  double d_t_offset;
339 
343  std::vector<std::shared_ptr<Vector>> d_snapshots;
344 
348  std::vector<std::shared_ptr<Vector>> d_controls;
349 
353  std::vector<double> d_sampled_times;
354 
358  std::shared_ptr<Vector> d_state_offset;
359 
363  bool d_trained;
364 
369 
374 
378  std::vector<double> d_sv;
379 
384 
388  int d_k;
389 
393  std::shared_ptr<Matrix> d_basis;
394 
398  std::shared_ptr<Matrix> d_A_tilde;
399 
403  std::shared_ptr<Matrix> d_B_tilde;
404 
408  std::shared_ptr<Matrix> d_phi_real;
409 
413  std::shared_ptr<Matrix> d_phi_imaginary;
414 
418  std::shared_ptr<Matrix> d_phi_real_squared_inverse;
419 
423  std::shared_ptr<Matrix> d_phi_imaginary_squared_inverse;
424 
428  std::shared_ptr<Vector> d_projected_init_real;
429 
433  std::shared_ptr<Vector> d_projected_init_imaginary;
434 
438  std::unique_ptr<Matrix> d_projected_controls_real;
439 
443  std::unique_ptr<Matrix> d_projected_controls_imaginary;
444 
448  std::vector<std::complex<double>> d_eigs;
449 
450 };
451 
452 }
453 
454 #endif
DMDc(const DMDc &other)
Unimplemented copy constructor.
std::vector< double > d_sv
std::vector holding the signular values.
Definition: DMDc.h:378
std::vector< std::shared_ptr< Vector > > d_controls
std::vector holding the controls.
Definition: DMDc.h:348
std::shared_ptr< Vector > d_projected_init_real
The real part of the projected initial condition.
Definition: DMDc.h:428
double d_dt
The time step size between samples.
Definition: DMDc.h:333
std::shared_ptr< Matrix > d_phi_real_squared_inverse
The real part of d_phi_squared_inverse.
Definition: DMDc.h:418
virtual void setOffset(std::shared_ptr< Vector > &offset_vector)
Set the state offset.
Definition: DMDc.cpp:137
int d_num_procs
The number of processors being run on.
Definition: DMDc.h:318
std::unique_ptr< Matrix > d_projected_controls_real
The real part of the projected controls.
Definition: DMDc.h:438
virtual void addOffset(Vector &result)
Add the state offset when predicting the solution.
Definition: DMDc.cpp:732
std::unique_ptr< Matrix > d_projected_controls_imaginary
The imaginary part of the projected controls.
Definition: DMDc.h:443
int d_num_singular_vectors
The maximum number of singular vectors.
Definition: DMDc.h:373
void project(const Vector &init, const Matrix &controls, double t_offset=-1.0)
Project U using d_phi, where U is the initial condition and the controls. Calculate pinv(phi) x U,...
Definition: DMDc.cpp:562
std::shared_ptr< Matrix > d_A_tilde
A_tilde.
Definition: DMDc.h:398
bool d_trained
Whether the DMDc has been trained or not.
Definition: DMDc.h:363
std::shared_ptr< Vector > d_projected_init_imaginary
The imaginary part of the projected initial condition.
Definition: DMDc.h:433
std::vector< std::shared_ptr< Vector > > d_snapshots
std::vector holding the snapshots.
Definition: DMDc.h:343
std::vector< std::complex< double > > d_eigs
A vector holding the complex eigenvalues of the eigenmodes.
Definition: DMDc.h:448
std::unique_ptr< Vector > predict(double t)
Predict state given a time. Uses the projected initial condition of the training dataset (the first c...
Definition: DMDc.cpp:681
double d_t_offset
The time offset of the first sample.
Definition: DMDc.h:338
virtual std::pair< Matrix *, Matrix * > computeDMDcSnapshotPair(const Matrix &snapshots, const Matrix &controls, const Matrix *B)
Returns a pair of pointers to the minus and plus snapshot matrices.
Definition: DMDc.cpp:213
void constructDMDc(const Matrix &f_snapshots, const Matrix &f_controls, int rank, int num_procs, const Matrix *B)
Construct the DMDc object.
Definition: DMDc.cpp:265
std::unique_ptr< const Matrix > getSnapshotMatrix()
Get the snapshot matrix contained within d_snapshots.
Definition: DMDc.cpp:785
virtual void save(std::string base_file_name)
Save the object state to a file.
Definition: DMDc.cpp:912
DMDc & operator=(const DMDc &rhs)
Unimplemented assignment operator.
virtual std::complex< double > computeEigExp(std::complex< double > eig, double t)
Compute the appropriate exponential function when predicting the solution.
Definition: DMDc.cpp:741
int d_k
The number of columns used after obtaining the SVD decomposition.
Definition: DMDc.h:388
bool d_init_projected
Whether the initial condition has been projected.
Definition: DMDc.h:368
void summary(std::string base_file_name)
Output the DMDc record in CSV files.
Definition: DMDc.cpp:1007
std::unique_ptr< const Matrix > createSnapshotMatrix(const std::vector< std::shared_ptr< Vector >> &snapshots)
Get the snapshot matrix contained within d_snapshots.
Definition: DMDc.cpp:791
std::vector< double > d_sampled_times
The stored times of each sample.
Definition: DMDc.h:353
std::shared_ptr< Matrix > d_phi_real
The real part of d_phi.
Definition: DMDc.h:408
std::shared_ptr< Matrix > d_basis
The left singular vector basis.
Definition: DMDc.h:393
int d_dim_c
The total dimension of the control vector.
Definition: DMDc.h:328
std::shared_ptr< Vector > d_state_offset
State offset in snapshot.
Definition: DMDc.h:358
DMDc()
Unimplemented default constructor.
int getNumSamples() const
Returns the number of samples taken.
Definition: DMDc.h:136
int d_rank
The rank of the process this object belongs to.
Definition: DMDc.h:313
double d_energy_fraction
The energy fraction used to obtain the DMDc modes.
Definition: DMDc.h:383
double getTimeOffset() const
Get the time offset contained within d_t_offset.
Definition: DMDc.cpp:779
std::pair< std::shared_ptr< Matrix >, std::shared_ptr< Matrix > > phiMultEigs(double t)
Internal function to multiply d_phi with the eigenvalues.
Definition: DMDc.cpp:747
int d_dim
The total dimension of the sample vector.
Definition: DMDc.h:323
std::shared_ptr< Matrix > d_B_tilde
B_tilde.
Definition: DMDc.h:403
std::shared_ptr< Matrix > d_phi_imaginary
The imaginary part of d_phi.
Definition: DMDc.h:413
std::shared_ptr< Matrix > d_phi_imaginary_squared_inverse
The imaginary part of d_phi_squared_inverse.
Definition: DMDc.h:423
virtual ~DMDc()
Destroy the DMDc object.
Definition: DMDc.h:58
virtual void takeSample(double *u_in, double t, double *f_in, bool last_step=false)
Sample the new state, u_in. Any samples in d_snapshots taken at the same or later time will be erased...
Definition: DMDc.cpp:142
virtual void load(std::string base_file_name)
Load the object state from a file.
Definition: DMDc.cpp:817
virtual void train(double energy_fraction, const Matrix *B=NULL)
Train the DMDc model with energy fraction criterion. The control matrix B may be available and used i...
Definition: DMDc.cpp:189