13 #include "BasisReader.h"
14 #include "utils/HDFDatabase.h"
15 #include "utils/HDFDatabaseMPIO.h"
19 #include "utils/mpi_utils.h"
23 BasisReader::BasisReader(
24 const std::string& base_file_name,
30 base_file_name_(base_file_name),
33 CAROM_VERIFY(!base_file_name.empty());
34 CAROM_VERIFY(comm == MPI_COMM_NULL || comm == MPI_COMM_WORLD);
35 d_distributed = comm != MPI_COMM_NULL;
38 MPI_Initialized(&mpi_init);
40 if (mpi_init && d_distributed) {
41 MPI_Comm_rank(comm, &rank);
47 full_file_name = base_file_name;
50 if (d_format == Database::formats::HDF5)
54 else if (d_format == Database::formats::HDF5_MPIO)
62 CAROM_VERIFY(d_dim >= 0);
63 CAROM_VERIFY(d_global_dim > 0);
67 CAROM_ERROR(
"BasisWriter only supports HDF5/HDF5_MPIO data format!\n");
69 d_database->
open(full_file_name,
"r", comm);
78 std::unique_ptr<Matrix>
81 int num_rows =
getDim(
"basis");
84 Matrix* spatial_basis_vectors =
new Matrix(num_rows, num_cols, d_distributed);
87 &spatial_basis_vectors->
item(0, 0),
90 return std::unique_ptr<Matrix>(spatial_basis_vectors);
93 std::unique_ptr<Matrix>
100 std::unique_ptr<Matrix>
105 int num_rows =
getDim(
"basis");
109 CAROM_VERIFY(0 < start_col && start_col <= num_cols);
110 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
111 int num_cols_to_read = end_col - start_col + 1;
113 Matrix* spatial_basis_vectors =
new Matrix(num_rows, num_cols_to_read,
115 sprintf(tmp,
"spatial_basis");
117 &spatial_basis_vectors->
item(0, 0),
118 num_rows*num_cols_to_read,
123 return std::unique_ptr<Matrix>(spatial_basis_vectors);
126 std::unique_ptr<Matrix>
131 double total_energy = 0.0;
133 for (
int i = 0; i < sv->dim(); i++)
135 total_energy += sv->item(i);
138 int num_used_singular_values = 0;
139 for (
int i = 0; i < sv->dim(); i++)
141 energy += sv->item(i);
142 num_used_singular_values++;
143 if (energy / total_energy >= ef)
152 std::unique_ptr<Matrix>
155 int num_rows =
getDim(
"temporal_basis");
159 Matrix* temporal_basis_vectors =
new Matrix(num_rows, num_cols,
false);
160 sprintf(tmp,
"temporal_basis");
162 &temporal_basis_vectors->
item(0, 0),
163 num_rows*num_cols, d_distributed);
164 return std::unique_ptr<Matrix>(temporal_basis_vectors);
167 std::unique_ptr<Matrix>
174 std::unique_ptr<Matrix>
179 int num_rows =
getDim(
"temporal_basis");
183 CAROM_VERIFY(0 < start_col && start_col <= num_cols);
184 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
185 int num_cols_to_read = end_col - start_col + 1;
187 Matrix* temporal_basis_vectors =
new Matrix(num_rows, num_cols_to_read,
false);
188 sprintf(tmp,
"temporal_basis");
190 &temporal_basis_vectors->
item(0, 0),
191 num_rows*num_cols_to_read,
196 return std::unique_ptr<Matrix>(temporal_basis_vectors);
199 std::unique_ptr<Matrix>
204 double total_energy = 0.0;
206 for (
int i = 0; i < sv->dim(); i++)
208 total_energy += sv->item(i);
211 int num_used_singular_values = 0;
212 for (
int i = 0; i < sv->dim(); i++)
214 energy += sv->item(i);
215 num_used_singular_values++;
216 if (energy / total_energy >= ef)
225 std::unique_ptr<Vector>
230 sprintf(tmp,
"singular_value_size");
234 sprintf(tmp,
"singular_value");
236 &singular_values->
item(0),
239 return std::unique_ptr<Vector>(singular_values);
242 std::unique_ptr<Vector>
247 double total_energy = 0.0;
249 for (
int i = 0; i < sv->dim(); i++)
251 total_energy += sv->item(i);
254 int num_used_singular_values = 0;
255 for (
int i = 0; i < sv->dim(); i++)
257 energy += sv->item(i);
258 num_used_singular_values++;
259 if (energy / total_energy >= ef)
265 Vector* truncated_singular_values =
new Vector(num_used_singular_values,
267 for (
int i = 0; i < num_used_singular_values; i++)
269 truncated_singular_values->
item(i) = sv->item(i);
272 return std::unique_ptr<Vector>(truncated_singular_values);
277 const std::string kind)
279 CAROM_ASSERT((kind ==
"basis") ||
280 (kind ==
"snapshot") ||
281 (kind ==
"temporal_basis"));
286 if (kind ==
"basis") sprintf(tmp,
"spatial_basis_num_rows");
287 else if (kind ==
"snapshot") sprintf(tmp,
"snapshot_matrix_num_rows");
288 else if (kind ==
"temporal_basis") sprintf(tmp,
289 "temporal_basis_num_rows");
293 if ((kind !=
"temporal_basis") && (d_format == Database::formats::HDF5_MPIO))
299 CAROM_VERIFY(d_global_dim == num_rows);
308 const std::string kind)
310 CAROM_ASSERT((kind ==
"basis") ||
311 (kind ==
"snapshot") ||
312 (kind ==
"temporal_basis"));
317 if (kind ==
"basis") sprintf(tmp,
"spatial_basis_num_cols");
318 else if (kind ==
"snapshot") sprintf(tmp,
"snapshot_matrix_num_cols");
319 else if (kind ==
"temporal_basis") sprintf(tmp,
320 "temporal_basis_num_cols");
326 std::unique_ptr<Matrix>
329 int num_rows =
getDim(
"snapshot");
333 Matrix* snapshots =
new Matrix(num_rows, num_cols, d_distributed);
334 sprintf(tmp,
"snapshot_matrix");
336 &snapshots->
item(0, 0),
339 return std::unique_ptr<Matrix>(snapshots);
342 std::unique_ptr<Matrix>
349 std::unique_ptr<Matrix>
354 int num_rows =
getDim(
"snapshot");
357 CAROM_VERIFY(0 < start_col && start_col <= num_cols);
358 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
359 int num_cols_to_read = end_col - start_col + 1;
362 Matrix* snapshots =
new Matrix(num_rows, num_cols_to_read, d_distributed);
363 sprintf(tmp,
"snapshot_matrix");
365 &snapshots->
item(0, 0),
366 num_rows*num_cols_to_read,
371 return std::unique_ptr<Matrix>(snapshots);
std::unique_ptr< Matrix > getSnapshotMatrix()
Returns the snapshot matrix for the requested time.
std::unique_ptr< Matrix > getTemporalBasis()
Returns the temporal basis vectors for the requested time as a Matrix.
int getNumSamples(const std::string kind)
Returns the number of samples (columns) in file.
int getDim(const std::string kind)
Returns the dimension of the system on this processor.
std::unique_ptr< Matrix > getSpatialBasis()
Returns the spatial basis vectors as a Matrix.
std::unique_ptr< Vector > getSingularValues()
Returns the singular values for the requested time.
~BasisReader()
Destructor.
void getInteger(const std::string &key, int &data)
Reads an integer associated with the supplied key from the currently open database file.
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.
virtual void getDoubleArray(const std::string &key, double *data, int nelements, const bool distributed=false)=0
Reads an array of doubles associated with the supplied key from the currently open database file.
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL)
Opens an existing database file with the supplied name.
const double & item(int row, int col) const
Const Matrix member access. Matrix data is stored in row-major format.
const double & item(int i) const
Const Vector member access.
int get_global_offsets(const int local_dim, std::vector< int > &offsets, const MPI_Comm &comm)
Save integer offsets for each MPI rank under MPI communicator comm, where each rank as the size of lo...