1 #include "RBGen_NetCDFFileIOHandler.h"
3 #include "Epetra_BLAS.h"
4 #include "Epetra_Export.h"
5 #include "Epetra_Import.h"
6 #include "Epetra_Map.h"
7 #include "Epetra_Vector.h"
8 #include "Epetra_MultiVector.h"
11 #include "Teuchos_Assert.hpp"
14 #include "Epetra_MpiComm.h"
16 #include "Epetra_SerialComm.h"
25 : isInit(false), num_nodes(0), num_nod_var(0), len_string(0), var_name(0)
31 for (
int i=0; i<num_nod_var; i++)
32 delete [] var_name[i];
33 if (var_name)
delete [] var_name;
47 if ( fileio_params.
isParameter(
"Data Input Path" ) ) {
48 in_path = Teuchos::getParameter<std::string>( fileio_params,
"Data Input Path" );
53 if ( fileio_params.
isParameter(
"Data Output Path" ) ) {
54 out_path = Teuchos::getParameter<std::string>( fileio_params,
"Data Output Path" );
69 int ncid=0, row_id=0, col_id=0, ss_id=0, num_nod_var_id=0;
70 int i, j, k, col_ptr=0;
71 int rows=0, num_ss=0, num_vars=0, status=0;
72 size_t rows_t=0, num_nod_var_t=0, start2[2],count2[2];
76 bool createSSIdx =
false;
77 std::vector< std::pair<int,int> > scaling_idx;
78 std::pair<int, int> idx_pair;
90 if ( comm.
MyPID() == 0 ) {
91 size_t rows0=0, cols0=0, cols_t=0, total_rows=0;
93 std::string temp_filename = in_path + filenames[0];
94 status = nc_open(temp_filename.c_str(),NC_NOWRITE,&ncid);
95 if (status != NC_NOERR) handle_error(status);
100 idx_pair.first = total_rows;
104 status = nc_inq_dimid(ncid,
"row",&row_id);
105 if (status != NC_NOERR) handle_error(status);
106 status = nc_inq_dimlen(ncid,row_id, &rows0);
107 if (status != NC_NOERR) handle_error(status);
111 status = nc_inq_dimid(ncid,
"col",&col_id);
112 if (status != NC_NOERR) handle_error(status);
113 status = nc_inq_dimlen(ncid,col_id, &cols0);
114 if (status != NC_NOERR) handle_error(status);
117 int len_string_id, num_nodes_id, name_nod_var_id;
118 size_t len_string_t, num_nodes_t;
121 status=nc_inq_dimid(ncid,
"len_string",&len_string_id);
122 if (status != NC_NOERR) handle_error(status);
123 status=nc_inq_dimlen(ncid,len_string_id,&len_string_t);
124 if (status != NC_NOERR) handle_error(status);
127 status=nc_inq_dimid(ncid,
"num_nodes",&num_nodes_id);
128 if (status != NC_NOERR) handle_error(status);
129 status=nc_inq_dimlen(ncid,num_nodes_id, &num_nodes_t);
130 if (status != NC_NOERR) handle_error(status);
133 status=nc_inq_dimid(ncid,
"num_nod_var",&num_nod_var_id);
134 if (status != NC_NOERR) handle_error(status);
135 status=nc_inq_dimlen(ncid,num_nod_var_id,&num_nod_var_t);
136 if (status != NC_NOERR) handle_error(status);
138 len_string = len_string_t;
139 num_nodes = num_nodes_t;
140 num_nod_var = num_nod_var_t;
143 status=nc_inq_varid(ncid,
"name_nod_var",&name_nod_var_id);
144 if (status != NC_NOERR) handle_error(status);
146 var_name =
new char*[ num_nod_var ];
147 for (i=0; i<num_nod_var; ++i)
148 var_name[i] =
new char[ len_string ];
150 for (i=0; i<num_nod_var; ++i) {
154 count2[1]=len_string;
156 status=nc_get_vara_text(ncid,name_nod_var_id,start2,count2,var_name[i]);
157 if (status != NC_NOERR) handle_error(status);
163 idx_pair.second = total_rows-1;
164 scaling_idx.push_back( idx_pair );
171 std::cout<<
"len_string = "<<len_string<<std::endl;
172 std::cout<<
"num_nodes = "<<num_nodes<<std::endl;
173 std::cout<<
"num_nod_var = "<<num_nod_var<<std::endl;
174 std::cout<<
"var_name = ";
175 for (i=0; i< num_nod_var; ++i)
176 std::cout<<var_name[i]<<
" ";
177 std::cout<<std::endl;
181 status = nc_close(ncid);
182 if (status != NC_NOERR) handle_error(status);
184 for (i=1; i<(int)filenames.size(); i++) {
185 std::string temp_filename = in_path + filenames[i];
186 status = nc_open(temp_filename.c_str(),NC_NOWRITE,&ncid);
187 if (status != NC_NOERR) handle_error(status);
192 idx_pair.first = total_rows;
196 status = nc_inq_dimid(ncid,
"row",&row_id);
197 if (status != NC_NOERR) handle_error(status);
198 status = nc_inq_dimlen(ncid,row_id, &rows_t);
199 if (status != NC_NOERR) handle_error(status);
202 status = nc_inq_dimid(ncid,
"col",&col_id);
203 if (status != NC_NOERR) handle_error(status);
204 status = nc_inq_dimlen(ncid,col_id, &cols_t);
205 if (status != NC_NOERR) handle_error(status);
208 status=nc_inq_dimid(ncid,
"num_nod_var",&num_nod_var_id);
209 if (status != NC_NOERR) handle_error(status);
210 status=nc_inq_dimlen(ncid,num_nod_var_id,&num_nod_var_t);
211 if (status != NC_NOERR) handle_error(status);
214 TEUCHOS_TEST_FOR_EXCEPTION(cols_t != cols0 || (
int)num_nod_var_t != num_nod_var, std::runtime_error,
"Data set in file "+temp_filename+
" is of inconsistent size!");
215 total_rows += rows_t;
220 idx_pair.second = total_rows-1;
221 scaling_idx.push_back( idx_pair );
224 status = nc_close(ncid);
225 if (status != NC_NOERR) handle_error(status);
232 std::cout<<
"Number of snapshots: "<< num_ss << std::endl;
233 std::cout<<
"Length of snapshot : "<< num_vars << std::endl;
242 for (i=0; i<(int)filenames.size(); i++) {
243 if ( comm.
MyPID() != 0 )
244 scaling_idx.push_back( idx_pair );
245 comm.
Broadcast( &scaling_idx[i].first, 1, 0 );
246 comm.
Broadcast( &scaling_idx[i].second, 1, 0 );
260 float *temp_vec_f = 0;
261 double *temp_vec_d = 0;
263 if ( comm.
MyPID() == 0 ) {
264 Proc0Map =
new Epetra_Map( num_vars, num_vars, 0, comm );
265 temp_vec_f =
new float [ num_vars ];
266 temp_vec_d =
new double [ num_vars ];
267 index =
new int[ num_vars ];
268 for ( i=0; i<num_vars; i++ ) { index[i] = i; }
270 Proc0Map =
new Epetra_Map( num_vars, 0, 0, comm );
289 for (i=0; i<(int)filenames.size(); i++) {
291 if ( comm.
MyPID() == 0 ) {
293 std::string temp_filename = in_path + filenames[i];
294 status = nc_open(temp_filename.c_str(),NC_NOWRITE,&ncid);
295 if (status != NC_NOERR) handle_error(status);
298 status = nc_inq_dimid(ncid,
"row",&row_id);
299 if (status != NC_NOERR) handle_error(status);
300 status = nc_inq_dimlen(ncid,row_id, &rows_t);
302 if (status != NC_NOERR) handle_error(status);
304 status = nc_inq_varid(ncid,
"snapshot",&ss_id);
305 if (status != NC_NOERR) handle_error(status);
312 for (j=0; j<rows; j++) {
316 col_newMV = (*newMV)( col_ptr );
320 if ( comm.
MyPID() == 0 ) {
325 status=nc_get_vara_float(ncid,ss_id,start2,count2,temp_vec_f);
326 for (k=0; k<num_vars; k++) {
327 temp_vec_d[k] = temp_vec_f[k];
337 col_newMV->Import(Proc0Vector, importer,
Add);
345 if ( comm.
MyPID() == 0 ) {
346 status = nc_close(ncid);
347 if (status != NC_NOERR) handle_error(status);
353 if ( index )
delete [] index;
354 if ( temp_vec_f )
delete [] temp_vec_f;
355 if ( temp_vec_d )
delete [] temp_vec_d;
372 int ncid, len_string_id, num_nodes_id, num_nod_var_id, row_id, col_id, ss_id, name_nod_var_id;
377 size_t start2[2],count2[2];
381 int num_vecs = MV->NumVectors();
382 int dim = MV->GlobalLength();
389 if ( comm.
MyPID() == 0 ) {
390 Proc0Map =
new Epetra_Map( dim, dim, 0, comm );
400 if ( comm.
MyPID() == 0 ) {
404 std::string temp_filename = out_path + filename;
405 status=nc_create(temp_filename.c_str(),NC_CLOBBER,&ncid);
406 if (status != NC_NOERR) handle_error(status);
408 status=nc_def_dim(ncid,
"len_string",(
long)len_string,&len_string_id);
409 if (status != NC_NOERR) handle_error(status);
411 status=nc_def_dim(ncid,
"num_nodes",(
long)num_nodes,&num_nodes_id);
412 if (status != NC_NOERR) handle_error(status);
414 status=nc_def_dim(ncid,
"num_nod_var",(
long)num_nod_var,&num_nod_var_id);
415 if (status != NC_NOERR) handle_error(status);
417 status=nc_def_dim(ncid,
"row",NC_UNLIMITED,&row_id);
418 if (status != NC_NOERR) handle_error(status);
420 status=nc_def_dim(ncid,
"col",(
long)dim,&col_id);
421 if (status != NC_NOERR) handle_error(status);
425 status=nc_def_var(ncid,
"snapshot",NC_FLOAT,2,ss_dimids,&ss_id);
426 if (status != NC_NOERR) handle_error(status);
428 ss_dimids[0]=num_nod_var_id;
429 ss_dimids[1]=len_string_id;
430 status=nc_def_var(ncid,
"name_nod_var",NC_CHAR,2,ss_dimids,&name_nod_var_id);
431 if (status != NC_NOERR) handle_error(status);
433 status=nc_enddef(ncid);
434 if (status != NC_NOERR) handle_error(status);
440 if ( comm.
MyPID() == 0 )
441 temp_vec =
new float[ dim ];
443 for (i=0; i<num_vecs; ++i) {
447 col_newMV = (*MV)( i );
449 Proc0Vector.Export(*col_newMV, exporter,
Insert);
451 if ( comm.
MyPID()==0 ) {
459 for (j=0; j<dim; ++j)
460 temp_vec[j] = Proc0Vector[j];
462 status=nc_put_vara_float(ncid,ss_id,start2,count2,temp_vec);
463 if (status != NC_NOERR) handle_error(status);
468 if ( comm.
MyPID() == 0 ) {
469 for(i=0; i<num_nod_var; ++i) {
473 count2[1] = strlen(var_name[i]);
477 status=nc_put_vara_text(ncid,name_nod_var_id,start2,count2,var_name[i]);
478 if (status != NC_NOERR) handle_error(status);
481 status=nc_close(ncid);
482 if (status != NC_NOERR) handle_error(status);
487 if (temp_vec)
delete [] temp_vec;
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
bool isParameter(const std::string &name) const
Teuchos::RCP< Epetra_MultiVector > Read(const std::vector< std::string > &filenames)
Method for reading multiple files and putting them into an Epetra_MultiVector.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
NetCDFFileIOHandler()
Default constructor.
void Write(const Teuchos::RCP< const Epetra_MultiVector > &MV, const std::string &filename)
Method for writing one Epetra_MultiVector into a file.
virtual ~NetCDFFileIOHandler()
Destructor.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
int Broadcast(double *MyVals, int Count, int Root) const
void Initialize(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Initialize file reader using.