13 #include "BasisWriter.h"
14 #include "utils/HDFDatabase.h"
15 #include "utils/HDFDatabaseMPIO.h"
18 #include "BasisGenerator.h"
19 #include "utils/Utilities.h"
25 BasisWriter::BasisWriter(
27 const std::string& base_file_name,
29 d_basis_generator(basis_generator),
32 db_format_(db_format),
36 CAROM_ASSERT(!base_file_name.empty());
39 MPI_Initialized(&mpi_init);
42 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
48 full_file_name = base_file_name;
49 snap_file_name = base_file_name +
"_snapshot";
52 if (db_format_ == Database::formats::HDF5)
57 else if (db_format_ == Database::formats::HDF5_MPIO)
63 CAROM_ERROR(
"BasisWriter only supports HDF5/HDF5_MPIO data format!\n");
69 delete d_snap_database;
75 CAROM_ASSERT(kind ==
"basis" || kind ==
"snapshot");
79 if (kind ==
"basis") {
80 d_database->
create(full_file_name, MPI_COMM_WORLD);
82 std::shared_ptr<const Matrix> basis = d_basis_generator.
getSpatialBasis();
84 CAROM_VERIFY(basis->distributed());
85 int num_rows = basis->numRows();
86 int nrows_infile = num_rows;
87 if (db_format_ == Database::formats::HDF5_MPIO)
88 MPI_Allreduce(MPI_IN_PLACE, &nrows_infile, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
89 sprintf(tmp,
"spatial_basis_num_rows");
91 int num_cols = basis->numColumns();
92 sprintf(tmp,
"spatial_basis_num_cols");
94 sprintf(tmp,
"spatial_basis");
95 d_database->
putDoubleArray(tmp, &basis->item(0, 0), num_rows*num_cols,
true);
100 CAROM_VERIFY(!tbasis->distributed());
101 num_rows = tbasis->numRows();
102 sprintf(tmp,
"temporal_basis_num_rows");
104 num_cols = tbasis->numColumns();
105 sprintf(tmp,
"temporal_basis_num_cols");
107 sprintf(tmp,
"temporal_basis");
108 d_database->
putDoubleArray(tmp, &tbasis->item(0, 0), num_rows*num_cols);
113 CAROM_VERIFY(!sv->distributed());
114 int sv_dim = sv->dim();
115 sprintf(tmp,
"singular_value_size");
117 sprintf(tmp,
"singular_value");
123 if (kind ==
"snapshot") {
124 d_snap_database->
create(snap_file_name, MPI_COMM_WORLD);
126 std::shared_ptr<const Matrix> snapshots =
129 CAROM_VERIFY(snapshots->distributed());
130 int num_rows = snapshots->numRows();
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();
137 sprintf(tmp,
"snapshot_matrix_num_cols");
139 sprintf(tmp,
"snapshot_matrix");
140 d_snap_database->
putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols,
143 d_snap_database->
close();
std::shared_ptr< const Matrix > getSnapshotMatrix()
Returns the snapshot matrix for the current time interval.
bool updateRightSV()
Check whether right basis vectors will be updated.
std::shared_ptr< const Matrix > getSpatialBasis()
Returns the basis vectors for the current time interval as a Matrix.
std::shared_ptr< const Vector > getSingularValues()
Returns the singular values for the current time interval as a Vector.
std::shared_ptr< const Matrix > getTemporalBasis()
Returns the temporal basis vectors for the current time interval as a Matrix.
void writeBasis(const std::string &kind="basis")
Write basis or state vectors generated by d_basis_generator.
~BasisWriter()
Destructor.
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL)
Creates a new database file with the supplied name.
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.
void putInteger(const std::string &key, int data)
Writes an integer associated with the supplied key to currently open database file.
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.