libROM  v1.0
Data-driven physical simulation library
BasisWriter.cpp
1 
11 // Description: A class that writes basis vectors to a file.
12 
13 #include "BasisWriter.h"
14 #include "utils/HDFDatabase.h"
15 #include "utils/HDFDatabaseMPIO.h"
16 #include "Matrix.h"
17 #include "Vector.h"
18 #include "BasisGenerator.h"
19 #include "utils/Utilities.h"
20 
21 #include "mpi.h"
22 
23 namespace CAROM {
24 
25 BasisWriter::BasisWriter(
26  BasisGenerator* basis_generator,
27  const std::string& base_file_name,
28  Database::formats db_format) :
29  d_basis_generator(basis_generator),
30  full_file_name(""),
31  snap_file_name(""),
32  db_format_(db_format),
33  d_database(NULL),
34  d_snap_database(NULL)
35 {
36  CAROM_ASSERT(basis_generator != 0);
37  CAROM_ASSERT(!base_file_name.empty());
38 
39  int mpi_init;
40  MPI_Initialized(&mpi_init);
41  int rank;
42  if (mpi_init) {
43  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
44  }
45  else {
46  rank = 0;
47  }
48 
49  full_file_name = base_file_name;
50  snap_file_name = base_file_name + "_snapshot";
51 
52  // create and open snapshot/basis database
53  if (db_format_ == Database::formats::HDF5)
54  {
55  d_snap_database = new HDFDatabase();
56  d_database = new HDFDatabase();
57  }
58  else if (db_format_ == Database::formats::HDF5_MPIO)
59  {
60  d_snap_database = new HDFDatabaseMPIO();
61  d_database = new HDFDatabaseMPIO();
62  }
63  else
64  CAROM_ERROR("BasisWriter only supports HDF5/HDF5_MPIO data format!\n");
65 }
66 
68 {
69  delete d_database;
70  delete d_snap_database;
71 }
72 
73 void
74 BasisWriter::writeBasis(const std::string& kind)
75 {
76  CAROM_ASSERT(kind == "basis" || kind == "snapshot");
77 
78  char tmp[100];
79 
80  if (kind == "basis") {
81  d_database->create(full_file_name, MPI_COMM_WORLD);
82 
83  const Matrix* basis = d_basis_generator->getSpatialBasis();
84  /* spatial basis is always distributed */
85  CAROM_VERIFY(basis->distributed());
86  int num_rows = basis->numRows();
87  int nrows_infile = num_rows;
88  if (db_format_ == Database::formats::HDF5_MPIO)
89  MPI_Allreduce(MPI_IN_PLACE, &nrows_infile, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
90  sprintf(tmp, "spatial_basis_num_rows");
91  d_database->putInteger(tmp, nrows_infile);
92  int num_cols = basis->numColumns();
93  sprintf(tmp, "spatial_basis_num_cols");
94  d_database->putInteger(tmp, num_cols);
95  sprintf(tmp, "spatial_basis");
96  d_database->putDoubleArray(tmp, &basis->item(0, 0), num_rows*num_cols, true);
97 
98  if(d_basis_generator->updateRightSV()) {
99  const Matrix* tbasis = d_basis_generator->getTemporalBasis();
100  /* temporal basis is always not distributed */
101  CAROM_VERIFY(!tbasis->distributed());
102  num_rows = tbasis->numRows();
103  sprintf(tmp, "temporal_basis_num_rows");
104  d_database->putInteger(tmp, num_rows);
105  num_cols = tbasis->numColumns();
106  sprintf(tmp, "temporal_basis_num_cols");
107  d_database->putInteger(tmp, num_cols);
108  sprintf(tmp, "temporal_basis");
109  d_database->putDoubleArray(tmp, &tbasis->item(0, 0), num_rows*num_cols);
110  }
111 
112  const Vector* sv = d_basis_generator->getSingularValues();
113  /* singular values are always not distributed */
114  CAROM_VERIFY(!sv->distributed());
115  int sv_dim = sv->dim();
116  sprintf(tmp, "singular_value_size");
117  d_database->putInteger(tmp, sv_dim);
118  sprintf(tmp, "singular_value");
119  d_database->putDoubleArray(tmp, &sv->item(0), sv_dim);
120 
121  d_database->close();
122  }
123 
124  if (kind == "snapshot") {
125  d_snap_database->create(snap_file_name, MPI_COMM_WORLD);
126 
127  const Matrix* snapshots = d_basis_generator->getSnapshotMatrix();
128  /* snapshot matrix is always distributed */
129  CAROM_VERIFY(snapshots->distributed());
130  int num_rows = snapshots->numRows(); // d_dim
131  int nrows_infile = num_rows;
132  if (db_format_ == Database::formats::HDF5_MPIO)
133  MPI_Allreduce(MPI_IN_PLACE, &nrows_infile, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
134  sprintf(tmp, "snapshot_matrix_num_rows");
135  d_snap_database->putInteger(tmp, nrows_infile);
136  int num_cols = snapshots->numColumns(); // d_num_samples
137  sprintf(tmp, "snapshot_matrix_num_cols");
138  d_snap_database->putInteger(tmp, num_cols);
139  sprintf(tmp, "snapshot_matrix");
140  d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols,
141  true);
142 
143  d_snap_database->close();
144  }
145 
146 }
147 
148 }
const Matrix * getSnapshotMatrix()
Returns the snapshot matrix for the current time interval.
bool updateRightSV()
Check whether right basis vectors will be updated.
const Matrix * getTemporalBasis()
Returns the temporal basis vectors for the current time interval as a Matrix.
const Matrix * getSpatialBasis()
Returns the basis vectors for the current time interval as a Matrix.
const Vector * getSingularValues()
Returns the singular values for the current time interval as a Vector.
void writeBasis(const std::string &kind="basis")
Write basis or state vectors generated by d_basis_generator.
Definition: BasisWriter.cpp:74
~BasisWriter()
Destructor.
Definition: BasisWriter.cpp:67
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL)
Creates a new database file with the supplied name.
Definition: Database.cpp:35
virtual bool close()=0
Closes the currently open database file.
formats
Implemented database file formats. Add to this enum each time a new database format is implemented.
Definition: Database.h:312
void putInteger(const std::string &key, int data)
Writes an integer associated with the supplied key to currently open database file.
Definition: Database.h:95
virtual void putDoubleArray(const std::string &key, const double *const data, int nelements, const bool distributed=false)=0
Writes an array of doubles associated with the supplied key to the currently open database file.
bool distributed() const
Returns true if the Matrix is distributed.
Definition: Matrix.h:177
int numColumns() const
Returns the number of columns in the Matrix. This method will return the same value from each process...
Definition: Matrix.h:220
const double & item(int row, int col) const
Const Matrix member access. Matrix data is stored in row-major format.
Definition: Matrix.h:1007
int numRows() const
Returns the number of rows of the Matrix on this processor.
Definition: Matrix.h:194
bool distributed() const
Returns true if the Vector is distributed.
Definition: Vector.h:255
const double & item(int i) const
Const Vector member access.
Definition: Vector.h:656
int dim() const
Returns the dimension of the Vector on this processor.
Definition: Vector.h:266