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,
29 base_file_name_(base_file_name),
32 CAROM_ASSERT(!base_file_name.empty());
35 MPI_Initialized(&mpi_init);
38 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
44 full_file_name = base_file_name;
47 if (d_format == Database::formats::HDF5)
51 else if (d_format == Database::formats::HDF5_MPIO)
59 CAROM_VERIFY(d_dim >= 0);
60 CAROM_VERIFY(d_global_dim > 0);
64 CAROM_ERROR(
"BasisWriter only supports HDF5/HDF5_MPIO data format!\n");
66 d_database->
open(full_file_name,
"r", MPI_COMM_WORLD);
78 int num_rows =
getDim(
"basis");
81 Matrix* spatial_basis_vectors =
new Matrix(num_rows, num_cols,
true);
84 &spatial_basis_vectors->
item(0, 0),
87 return spatial_basis_vectors;
102 int num_rows =
getDim(
"basis");
106 CAROM_VERIFY(0 < start_col <= num_cols);
107 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
108 int num_cols_to_read = end_col - start_col + 1;
110 Matrix* spatial_basis_vectors =
new Matrix(num_rows, num_cols_to_read,
true);
111 sprintf(tmp,
"spatial_basis");
113 &spatial_basis_vectors->
item(0, 0),
114 num_rows*num_cols_to_read,
119 return spatial_basis_vectors;
127 double total_energy = 0.0;
129 for (
int i = 0; i < sv->
dim(); i++)
131 total_energy += sv->
item(i);
134 int num_used_singular_values = 0;
135 for (
int i = 0; i < sv->
dim(); i++)
137 energy += sv->
item(i);
138 num_used_singular_values++;
139 if (energy / total_energy >= ef)
152 int num_rows =
getDim(
"temporal_basis");
156 Matrix* temporal_basis_vectors =
new Matrix(num_rows, num_cols,
false);
157 sprintf(tmp,
"temporal_basis");
159 &temporal_basis_vectors->
item(0, 0),
161 return temporal_basis_vectors;
176 int num_rows =
getDim(
"temporal_basis");
180 CAROM_VERIFY(0 < start_col <= num_cols);
181 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
182 int num_cols_to_read = end_col - start_col + 1;
184 Matrix* temporal_basis_vectors =
new Matrix(num_rows, num_cols_to_read,
false);
185 sprintf(tmp,
"temporal_basis");
187 &temporal_basis_vectors->
item(0, 0),
188 num_rows*num_cols_to_read,
192 return temporal_basis_vectors;
200 double total_energy = 0.0;
202 for (
int i = 0; i < sv->
dim(); i++)
204 total_energy += sv->
item(i);
207 int num_used_singular_values = 0;
208 for (
int i = 0; i < sv->
dim(); i++)
210 energy += sv->
item(i);
211 num_used_singular_values++;
212 if (energy / total_energy >= ef)
227 sprintf(tmp,
"singular_value_size");
231 sprintf(tmp,
"singular_value");
233 &singular_values->
item(0),
235 return singular_values;
243 double total_energy = 0.0;
245 for (
int i = 0; i < sv->
dim(); i++)
247 total_energy += sv->
item(i);
250 int num_used_singular_values = 0;
251 for (
int i = 0; i < sv->
dim(); i++)
253 energy += sv->
item(i);
254 num_used_singular_values++;
255 if (energy / total_energy >= ef)
261 Vector* truncated_singular_values =
new Vector(num_used_singular_values,
263 for (
int i = 0; i < num_used_singular_values; i++)
265 truncated_singular_values->
item(i) = sv->
item(i);
269 return truncated_singular_values;
274 const std::string kind)
276 CAROM_ASSERT((kind ==
"basis") ||
277 (kind ==
"snapshot") ||
278 (kind ==
"temporal_basis"));
283 if (kind ==
"basis") sprintf(tmp,
"spatial_basis_num_rows");
284 else if (kind ==
"snapshot") sprintf(tmp,
"snapshot_matrix_num_rows");
285 else if (kind ==
"temporal_basis") sprintf(tmp,
286 "temporal_basis_num_rows");
290 if ((kind !=
"temporal_basis") && (d_format == Database::formats::HDF5_MPIO))
296 CAROM_VERIFY(d_global_dim == num_rows);
305 const std::string kind)
307 CAROM_ASSERT((kind ==
"basis") ||
308 (kind ==
"snapshot") ||
309 (kind ==
"temporal_basis"));
314 if (kind ==
"basis") sprintf(tmp,
"spatial_basis_num_cols");
315 else if (kind ==
"snapshot") sprintf(tmp,
"snapshot_matrix_num_cols");
316 else if (kind ==
"temporal_basis") sprintf(tmp,
317 "temporal_basis_num_cols");
326 int num_rows =
getDim(
"snapshot");
330 Matrix* snapshots =
new Matrix(num_rows, num_cols,
true);
331 sprintf(tmp,
"snapshot_matrix");
333 &snapshots->
item(0, 0),
351 int num_rows =
getDim(
"snapshot");
354 CAROM_VERIFY(0 < start_col <= num_cols);
355 CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
356 int num_cols_to_read = end_col - start_col + 1;
359 Matrix* snapshots =
new Matrix(num_rows, num_cols_to_read,
true);
360 sprintf(tmp,
"snapshot_matrix");
362 &snapshots->
item(0, 0),
363 num_rows*num_cols_to_read,
Vector * getSingularValues()
Returns the singular values for the requested time.
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.
Matrix * getSnapshotMatrix()
Returns the snapshot matrix for the requested time.
Matrix * getSpatialBasis()
Returns the spatial basis vectors as a Matrix.
Matrix * getTemporalBasis()
Returns the temporal basis vectors for the requested time as a Matrix.
~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 dim() const
Returns the dimension of the Vector on this processor.
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...