HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
DMDROMObject Class Reference

ROM object of type DMD (see libROM) More...

#include <rom_object_dmd.h>

Inheritance diagram for DMDROMObject:
ROMObject

Public Member Functions

 DMDROMObject (const int, const double, const int, const int, const int, const int, const int a_var_idx=-1)
 
virtual ~DMDROMObject ()
 
virtual void projectInitialSolution (CAROM::Vector &a_U)
 
virtual void takeSample (const CAROM::Vector &, const double)
 
virtual void train ()
 
virtual const CAROM::Vector *const predict (const double a_t) const
 
virtual void save (const std::string &a_fname_root) const
 
virtual void load (const std::string &a_fname_root)
 
- Public Member Functions inherited from ROMObject
virtual ~ROMObject ()
 

Protected Attributes

std::vector< CAROM::DMD * > m_dmd
 
std::vector< bool > m_dmd_is_trained
 
std::vector< Intervalm_intervals
 
int m_rank
 
int m_nproc
 
int m_vec_size
 
double m_dt
 
double m_t_final
 
int m_rdim
 
int m_num_window_samples
 
int m_tic
 
int m_curr_win
 
int m_sim_idx
 
int m_var_idx
 
bool m_write_snapshot_mat
 
std::string m_dirname
 

Detailed Description

ROM object of type DMD (see libROM)

ROM object of type DMD (see libROM) DMD-type ROM (see libROM for details)

DMD-type ROM (see libROM for details). DMD stands for "Dynamic Mode Decomposition".

Definition at line 37 of file rom_object_dmd.h.

Constructor & Destructor Documentation

◆ DMDROMObject()

DMDROMObject ( const int  a_vec_size,
const double  a_dt,
const int  a_rdim,
const int  a_rank,
const int  a_nproc,
const int  a_sim_idx,
const int  a_var_idx = -1 
)

Constructor

Constructor This function will also look into the file librom.inp for DMD-specific options. Rank 0 reads in the inputs and broadcasts them to all the processors.

The format of librom.inp is as follows:

begin
    <keyword>   <value>
    ...
    <keyword>   <value>
end

where the list of keywords and their type (that this function will look for) are:

Keyword name Type Variable Default value --—
dmd_num_win_samples int DMDROMObject::m_num_window_samples INT_MAX
dmd_dirname string DMDROMObject::m_dirname "DMD"
dmd_write_snapshot_mat bool DMDROMObject::m_write_snapshot_mat false

Note: other keywords in this file may be read by other functions.

Parameters
a_vec_sizevector size
a_dttime step size
a_rdimlatent space dimension
a_rankMPI rank of this process
a_nprocNumber of MPI processes
a_sim_idxSimulation index (for ensemble simulations
a_var_idxVector component index

Definition at line 35 of file DMDROMObject.cpp.

42 {
43  m_rank = a_rank;
44  m_nproc = a_nproc;
45 
46  m_dmd.clear();
47  m_dmd_is_trained.clear();
48  m_intervals.clear();
49  m_vec_size = a_vec_size;
50  m_dt = a_dt;
51  m_t_final = -1;
52  m_rdim = a_rdim;
53 
54  m_sim_idx = a_sim_idx;
55  m_var_idx = a_var_idx;
56 
57  m_num_window_samples = INT_MAX;
58 
59  char dirname_c_str[_MAX_STRING_SIZE_] = "DMD";
60  char write_snapshot_mat[_MAX_STRING_SIZE_] = "false";
61 
62  if (!m_rank) {
63 
64  FILE *in;
65  in = fopen(_LIBROM_INP_FNAME_,"r");
66 
67  if (in) {
68 
69  int ferr;
70  char word[_MAX_STRING_SIZE_];
71  ferr = fscanf(in,"%s", word); if (ferr != 1) return;
72 
73  if (std::string(word) == "begin") {
74  while (std::string(word) != "end") {
75  ferr = fscanf(in,"%s",word); if (ferr != 1) return;
76  if (std::string(word) == "dmd_num_win_samples") {
77  ferr = fscanf(in,"%d", &m_num_window_samples); if (ferr != 1) return;
78  } else if (std::string(word) == "dmd_dirname") {
79  ferr = fscanf(in,"%s", dirname_c_str); if (ferr != 1) return;
80  } else if (std::string(word) == "dmd_write_snapshot_mat") {
81  ferr = fscanf(in,"%s", write_snapshot_mat); if (ferr != 1) return;
82  }
83  if (ferr != 1) return;
84  }
85  } else {
86  fprintf( stderr, "Error: Illegal format in file \"%s\". Word read is: %s\n",
87  _LIBROM_INP_FNAME_, word);
88  return;
89  }
90 
91  fclose(in);
92 
93  }
94 
95  /* print useful stuff to screen */
96  printf("DMDROMObject details:\n");
97  printf(" number of samples per window: %d\n", m_num_window_samples);
98  printf(" directory name for DMD onjects: %s\n", dirname_c_str);
99  printf(" write snapshot matrix to file: %s\n", write_snapshot_mat);
100  if (m_sim_idx >= 0) {
101  printf(" simulation domain: %d\n", m_sim_idx);
102  }
103  if (m_var_idx >= 0) {
104  printf(" Vector component: %d\n", m_var_idx);
105  }
106  }
107 
108 #ifndef serial
109  MPI_Bcast(&m_num_window_samples,1,MPI_INT,0,MPI_COMM_WORLD);
110  MPI_Bcast(dirname_c_str,_MAX_STRING_SIZE_,MPI_CHAR,0,MPI_COMM_WORLD);
111  MPI_Bcast(write_snapshot_mat,_MAX_STRING_SIZE_,MPI_CHAR,0,MPI_COMM_WORLD);
112 #endif
113 
114  m_dirname = std::string( dirname_c_str );
115  m_write_snapshot_mat = (std::string(write_snapshot_mat) == "true");
116 
117  if (m_num_window_samples <= m_rdim) {
118  printf("ERROR:DMDROMObject::DMDROMObject() - m_num_window_samples <= m_rdim!!");
119  }
120 
121  m_tic = 0;
122  m_curr_win = 0;
123 }
std::vector< CAROM::DMD * > m_dmd
#define _LIBROM_INP_FNAME_
Definition: rom_object.h:15
int m_num_window_samples
std::vector< Interval > m_intervals
bool m_write_snapshot_mat
#define _MAX_STRING_SIZE_
Definition: basic.h:14
std::vector< bool > m_dmd_is_trained
std::string m_dirname

◆ ~DMDROMObject()

virtual ~DMDROMObject ( )
inlinevirtual

Destructor

Definition at line 51 of file rom_object_dmd.h.

52  {
53  for (int i = 0; i < m_dmd.size(); i++) delete m_dmd[i];
54  m_dmd.clear();
55  m_intervals.clear();
56  m_dmd_is_trained.clear();
57  }
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals
std::vector< bool > m_dmd_is_trained

Member Function Documentation

◆ projectInitialSolution()

virtual void projectInitialSolution ( CAROM::Vector &  a_U)
inlinevirtual

Project initial solution for prediction

Parameters
a_Usolution vector

Implements ROMObject.

Definition at line 60 of file rom_object_dmd.h.

61  {
62  if (m_dmd.size() == 0) {
63  if (!m_rank) {
64  printf("ERROR in DMDROMObject::projectInitialSolution() - m_dmd is a vector of size 0.\n");
65  }
66  return;
67  }
68 
69  m_dmd[0]->projectInitialCondition( &a_U );
70  for (int i = 1; i < m_dmd.size(); i++) {
71  m_dmd[i]->projectInitialCondition( m_dmd[i-1]->predict(m_intervals[i].first) );
72  }
73  return;
74  }
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals
virtual const CAROM::Vector *const predict(const double a_t) const

◆ takeSample()

void takeSample ( const CAROM::Vector &  a_U,
const double  a_time 
)
virtual

take a sample (solution snapshot)

Parameters
a_Usolution vector
a_timesample time

Implements ROMObject.

Definition at line 126 of file DMDROMObject.cpp.

128 {
129  if (m_tic == 0) {
130 
131  m_dmd.push_back( new CAROM::DMD(m_vec_size, m_dt) );
132  m_dmd_is_trained.push_back(false);
133  m_intervals.push_back( Interval(a_time, m_t_final) );
134 
135  if (!m_rank) {
136  printf( "DMDROMObject::takeSample() - creating new DMD object for sim. domain %d, var %d, t=%f (total: %d).\n",
137  m_sim_idx, m_var_idx, m_intervals[m_curr_win].first, m_dmd.size());
138  }
139  m_dmd[m_curr_win]->takeSample( a_U.getData(), a_time );
140 
141  } else {
142 
143  m_dmd[m_curr_win]->takeSample( a_U.getData(), a_time );
144 
145  if (m_tic%m_num_window_samples == 0) {
146 
147  m_intervals[m_curr_win].second = a_time;
148  int ncol = m_dmd[m_curr_win]->getSnapshotMatrix()->numColumns();
149  if (!m_rank) {
150  printf( "DMDROMObject::train() - training DMD object %d for sim. domain %d, var %d with %d samples.\n",
151  m_curr_win, m_sim_idx, m_var_idx, ncol );
152  }
153 
154  if (m_write_snapshot_mat) {
155  char idx_string[_MAX_STRING_SIZE_];
156  sprintf(idx_string, "%04d", m_curr_win);
157  std::string fname_root(m_dirname + "/snapshot_mat_"+std::string(idx_string));
158  m_dmd[m_curr_win]->getSnapshotMatrix()->write(fname_root);
159  }
160  m_dmd[m_curr_win]->train(m_rdim);
162 
163  m_curr_win++;
164 
165  m_dmd.push_back( new CAROM::DMD(m_vec_size, m_dt) );
166  m_dmd_is_trained.push_back(false);
167  m_intervals.push_back( Interval(a_time, m_t_final) );
168  m_dmd[m_curr_win]->takeSample( a_U.getData(), a_time );
169  if (!m_rank) {
170  printf("DMDROMObject::takeSample() - creating new DMD object for sim. domain %d, var %d, t=%f (total: %d).\n",
171  m_sim_idx, m_var_idx, m_intervals[m_curr_win].first, m_dmd.size());
172  }
173  }
174 
175  }
176 
177  m_tic++;
178  return;
179 }
std::pair< double, double > Interval
std::vector< CAROM::DMD * > m_dmd
int m_num_window_samples
std::vector< Interval > m_intervals
bool m_write_snapshot_mat
#define _MAX_STRING_SIZE_
Definition: basic.h:14
std::vector< bool > m_dmd_is_trained
std::string m_dirname

◆ train()

void train ( )
virtual

train the DMD object

train the DMD objects

Implements ROMObject.

Definition at line 182 of file DMDROMObject.cpp.

183 {
184  /* make sure the number of columns for the last DMD isn't less than m_rdim */
185  {
186  int last_win = m_dmd.size() - 1;
187  int num_columns( m_dmd[last_win]->getSnapshotMatrix()->numColumns() );
188  if (num_columns <= m_rdim) {
189  m_dmd.pop_back();
190  m_intervals.pop_back();
191  m_intervals[m_intervals.size()-1].second = m_t_final;
192  if (!m_rank) {
193  printf("DMDROMObject::train() - last window DMD for sim. domain %d, var %d has %d sample(s) only; deleted it ",
194  m_sim_idx,
195  m_var_idx,
196  num_columns );
197  printf("(total: %d).\n", m_dmd.size());
198  }
199  }
200  }
201 
202  if (m_dmd.size() > 0) {
203  for (int i = 0; i < m_dmd.size(); i++) {
204  if (!m_dmd_is_trained[i]) {
205  int ncol = m_dmd[i]->getSnapshotMatrix()->numColumns();
206  if (!m_rank) {
207  printf( "DMDROMObject::train() - training DMD object %d for sim. domain %d, var %d with %d samples.\n",
208  m_curr_win, m_sim_idx, m_var_idx, ncol );
209  }
210  if (m_write_snapshot_mat) {
211  char idx_string[_MAX_STRING_SIZE_];
212  sprintf(idx_string, "%04d", i);
213  std::string fname_root(m_dirname + "/snapshot_mat_"+std::string(idx_string));
214  m_dmd[i]->getSnapshotMatrix()->write(fname_root);
215  }
216  m_dmd[i]->train(m_rdim);
217  }
218  }
219  } else {
220  printf("ERROR in DMDROMObject::train(): m_dmd is of size zero!");
221  }
222 
223  return;
224 }
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals
bool m_write_snapshot_mat
#define _MAX_STRING_SIZE_
Definition: basic.h:14
std::vector< bool > m_dmd_is_trained
std::string m_dirname

◆ predict()

virtual const CAROM::Vector* const predict ( const double  a_t) const
inlinevirtual

compute prediction at given time

Parameters
a_ttime at which to predict solution

Implements ROMObject.

Definition at line 84 of file rom_object_dmd.h.

85  {
86  for (int i = 0; i < m_dmd.size(); i++) {
87  if ( (a_t >= m_intervals[i].first)
88  && ( (a_t < m_intervals[i].second) || (m_intervals[i].second < 0) ) ){
89  return m_dmd[i]->predict(a_t);
90  }
91  }
92  printf("ERROR in DMDROMObject::predict(): m_dmd is of size zero or interval not found!");
93  return nullptr;
94  }
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals

◆ save()

void save ( const std::string &  a_fname_root) const
virtual

save DMD object to file

Save DMD objects to file: the DMD object files will be saved in the subdirectory with the name DMDROMObject::m_dirname. They are in a format that libROM can read from.

Note: If the subdirectory DMDROMObject::m_dirname does not exist, the DMD objects will not be written (even though the screen output will claim they were written)!. The code may not report any error, or one may see HDF5 file writing errors.

Parameters
a_fname_rootFilename root

Implements ROMObject.

Definition at line 235 of file DMDROMObject.cpp.

236 {
237  std::string fname_root = m_dirname + "/";
238  std::string summary_fname_root = m_dirname + "/";
239  std::string header_fname = m_dirname + "/";
240  if (a_fname_root == "") {
241  fname_root += "dmdobj_";
242  summary_fname_root += "dmd_summary_";
243  header_fname += "dmd_header.dat";
244  } else {
245  fname_root += (a_fname_root+"_dmdobj_");
246  summary_fname_root += (a_fname_root+"_dmd_summary_");
247  header_fname += (a_fname_root+"_dmd_header.dat");
248  }
249 
250  if (!m_rank) {
251  FILE* out;
252  out = fopen(header_fname.c_str(), "w");
253  fprintf(out, "%d\n", m_dmd.size());
254  for (int i = 0; i < m_dmd.size(); i++) {
255  fprintf(out, "%1.16e %1.16e\n", m_intervals[i].first, m_intervals[i].second);
256  }
257  fclose(out);
258  }
259 
260  for (int i = 0; i < m_dmd.size(); i++) {
261  char idx_string[_MAX_STRING_SIZE_];
262  sprintf(idx_string, "%04d", i);
263  std::string fname = fname_root + std::string(idx_string);
264  std::string summary_fname = summary_fname_root + std::string(idx_string);
265  if (!m_rank) {
266  printf( " Saving DMD object and summary (%s, %s).\n",
267  fname.c_str(), summary_fname.c_str() );
268  }
269  m_dmd[i]->save(fname);
270  //m_dmd[i]->summary(summary_fname);
271  }
272 
273  return;
274 }
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals
#define _MAX_STRING_SIZE_
Definition: basic.h:14
std::string m_dirname

◆ load()

void load ( const std::string &  a_fname_root)
virtual

load DMD object from file

Load DMD objects from file: the DMD object files must be in the subdirectory with the name DMDROMObject::m_dirname. They must be in a format that libROM can read from. The number of objects and their parameters must correspond to the simulation being run for which this object is defined.

Parameters
a_fname_rootFilename root

Implements ROMObject.

Definition at line 281 of file DMDROMObject.cpp.

282 {
283  std::string fname_root = m_dirname + "/";
284  std::string header_fname = m_dirname + "/";
285  if (a_fname_root == "") {
286  fname_root += "dmdobj_";
287  header_fname += "dmd_header.dat";
288  } else {
289  fname_root += (a_fname_root+"_dmdobj_");
290  header_fname += (a_fname_root+"_dmd_header.dat");
291  }
292 
293  int num_dmds = 0;
294  std::vector<double> intervals_start(0), intervals_end(0);
295  if (!m_rank) {
296  FILE* in;
297  in = fopen(header_fname.c_str(), "r");
298  fscanf(in, "%d\n", &num_dmds);
299  intervals_start.resize(num_dmds);
300  intervals_end.resize(num_dmds);
301  for (int i = 0; i < num_dmds; i++) {
302  fscanf(in, "%lf", &intervals_start[i]);
303  fscanf(in, "%lf", &intervals_end[i]);
304  }
305  fclose(in);
306  }
307 #ifndef serial
308  MPI_Bcast(&num_dmds,1,MPI_INT,0,MPI_COMM_WORLD);
309  if (m_rank) {
310  intervals_start.resize(num_dmds);
311  intervals_end.resize(num_dmds);
312  }
313  MPI_Bcast(intervals_start.data(),num_dmds,MPI_DOUBLE,0,MPI_COMM_WORLD);
314  MPI_Bcast(intervals_end.data(),num_dmds,MPI_DOUBLE,0,MPI_COMM_WORLD);
315 #endif
316 
317  for (int i = 0; i < num_dmds; i++) {
318 
319  m_intervals.push_back( Interval(intervals_start[i],intervals_end[i]) );
320 
321  char idx_string[_MAX_STRING_SIZE_];
322  sprintf(idx_string, "%04d", i);
323  std::string fname = fname_root + std::string(idx_string);
324  if (!m_rank) {
325  printf( " Loading DMD object (%s), time window=[%1.2e,%1.2e].\n",
326  fname.c_str(),
327  m_intervals[i].first, m_intervals[i].second );
328  }
329  m_dmd.push_back( new CAROM::DMD(fname) );
330  }
331 
332  return;
333 }
std::pair< double, double > Interval
std::vector< CAROM::DMD * > m_dmd
std::vector< Interval > m_intervals
#define _MAX_STRING_SIZE_
Definition: basic.h:14
std::string m_dirname

Field Documentation

◆ m_dmd

std::vector<CAROM::DMD*> m_dmd
protected

Vector of DMD objects

Definition at line 104 of file rom_object_dmd.h.

◆ m_dmd_is_trained

std::vector<bool> m_dmd_is_trained
protected

Flag to indicate if DMD is trained

Definition at line 105 of file rom_object_dmd.h.

◆ m_intervals

std::vector<Interval> m_intervals
protected

Time intervals for each DMD object

Definition at line 106 of file rom_object_dmd.h.

◆ m_rank

int m_rank
protected

MPI rank

Definition at line 108 of file rom_object_dmd.h.

◆ m_nproc

int m_nproc
protected

Number of MPI ranks

Definition at line 109 of file rom_object_dmd.h.

◆ m_vec_size

int m_vec_size
protected

Local size of solution vector

Definition at line 111 of file rom_object_dmd.h.

◆ m_dt

double m_dt
protected

Time step size

Definition at line 112 of file rom_object_dmd.h.

◆ m_t_final

double m_t_final
protected

Final time

Definition at line 113 of file rom_object_dmd.h.

◆ m_rdim

int m_rdim
protected

Latent space dimension

Definition at line 114 of file rom_object_dmd.h.

◆ m_num_window_samples

int m_num_window_samples
protected

Number of samples per DMD for time-windowing

Definition at line 116 of file rom_object_dmd.h.

◆ m_tic

int m_tic
protected

private ticker to count number of samples taken

Definition at line 118 of file rom_object_dmd.h.

◆ m_curr_win

int m_curr_win
protected

private index for current window

Definition at line 119 of file rom_object_dmd.h.

◆ m_sim_idx

int m_sim_idx
protected

simulation index of this object for ensemble simulations

Definition at line 121 of file rom_object_dmd.h.

◆ m_var_idx

int m_var_idx
protected

component index of this object if component-wise ROMs are being used

Definition at line 122 of file rom_object_dmd.h.

◆ m_write_snapshot_mat

bool m_write_snapshot_mat
protected

Write snapshot matrix to file or not

Definition at line 124 of file rom_object_dmd.h.

◆ m_dirname

std::string m_dirname
protected

Subdirectory where DMD objects are written to or read from

Definition at line 126 of file rom_object_dmd.h.


The documentation for this class was generated from the following files: