RBGen  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RBGen_MatrixMarketFileIOHandler.cpp
1 
2 #include "RBGen_MatrixMarketFileIOHandler.h"
3 #include "RBGen_ConfigDefs.h"
4 
5 #include "Epetra_BLAS.h"
6 #include "Epetra_Export.h"
7 #include "Epetra_Import.h"
8 #include "Epetra_Map.h"
9 #include "Epetra_MultiVector.h"
10 
11 #include "EpetraExt_mmio.h"
12 #include "EpetraExt_MultiVectorIn.h"
13 #include "EpetraExt_MultiVectorOut.h"
14 
15 #include "Teuchos_Utils.hpp"
16 #include "Teuchos_Assert.hpp"
17 
18 #ifdef EPETRA_MPI
19 #include "Epetra_MpiComm.h"
20 #else
21 #include "Epetra_SerialComm.h"
22 #endif
23 
24 
25 namespace RBGen {
26 
28  : num_nodes(0), isInit(false)
29  {
30  }
31 
33  {
34  // Get the "File I/O" sublist.
35  Teuchos::ParameterList& fileio_params = params->sublist( "File IO" );
36 
37  // Get the input path.
38  in_path = "";
39  if ( fileio_params.isParameter( "Data Input Path" ) ) {
40  in_path = Teuchos::getParameter<std::string>( fileio_params, "Data Input Path" );
41  }
42 
43  // Get the output path.
44  out_path = "";
45  if ( fileio_params.isParameter( "Data Output Path" ) ) {
46  out_path = Teuchos::getParameter<std::string>( fileio_params, "Data Output Path" );
47  }
48 
49  // This file i/o handler is now initialized.
50  isInit = true;
51  }
52 
53  Teuchos::RCP<Epetra_MultiVector> MatrixMarketFileIOHandler::Read( const std::vector<std::string>& filenames )
54  {
55 
57 
58  if (isInit) {
59 
60 #ifdef EPETRA_MPI
61  Epetra_MpiComm comm( MPI_COMM_WORLD );
62 #else
63  Epetra_SerialComm comm;
64 #endif
65 
66  int i, rows = 0;
67  int num_vecs = 0;
68  int num_files = filenames.size();
69  std::vector<int> cols(num_files,0);
70  FILE * handle = 0;
71  for (i=0; i<num_files; ++i) {
72  int info = 0, rows_i = 0;
73 
74  // Open the data file
75  std::string temp_filename = in_path + filenames[i];
76  handle = fopen(temp_filename.c_str(), "r");
77  TEUCHOS_TEST_FOR_EXCEPTION(handle==0, std::invalid_argument, "File named '"+temp_filename+"' does not exist or is not readable!");
78 
79  // Get the array dimensions
80  info = EpetraExt::mm_read_mtx_array_size( handle, &rows_i, &cols[i] );
81  TEUCHOS_TEST_FOR_EXCEPTION(info!=0, std::runtime_error, "Error reading file with name '"+temp_filename+"'!");
82 
83  if (i==0) {
84  rows = rows_i; // Get the number of rows from the first file
85  }
86  else {
87  // Check to make sure the number of rows is the same.
88  TEUCHOS_TEST_FOR_EXCEPTION(rows_i!=rows, std::logic_error, "Error reading file '"+temp_filename+"', does not have same number of rows!");
89  }
90  // Add the number of columns up.
91  num_vecs += cols[i];
92 
93  // Close the data file
94  fclose( handle );
95  }
96 
97  // Create the map and full multivector.
98  Epetra_Map Map( rows, 0, comm );
99  newMV = Teuchos::rcp( new Epetra_MultiVector( Map, num_vecs ) );
100 
101  // Create a pointer to a multivector
102  int col_ptr = 0;
103  Epetra_MultiVector* fileMV = 0;
104 
105  for ( i=0; i<num_files; i++ ) {
106  //
107  // Read in Epetra_MultiVector from file.
108  //
109  std::string curr_filename = in_path + filenames[i];
110  int info = EpetraExt::MatrixMarketFileToMultiVector( curr_filename.c_str(), Map, fileMV );
111  TEUCHOS_TEST_FOR_EXCEPTION(info!=0, std::runtime_error, "Error reading file with name '"+curr_filename+"'!");
112  //
113  // Get a view of the multivector columns.
114  //
115  Epetra_MultiVector subMV( View, *newMV, col_ptr, cols[i] );
116  //
117  // Put the multivector read in from the file into this subview.
118  //
119  subMV.Update( 1.0, *fileMV, 0.0 );
120  //
121  // Update the column pointer
122  //
123  col_ptr += cols[i];
124  //
125  // Clean up the multivector
126  //
127  if (fileMV) { delete fileMV; fileMV=0; }
128  }
129 
130  }
131  else {
132  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "File I/O handler is not initialized!");
133  }
134  // Return.
135  return newMV;
136  }
137 
138  void MatrixMarketFileIOHandler::Write( const Teuchos::RCP<const Epetra_MultiVector>& MV, const std::string& filename )
139  {
140  if (isInit) {
141 
142  std::string temp_filename = out_path + filename;
143  EpetraExt::MultiVectorToMatrixMarketFile( temp_filename.c_str(), *MV );
144 
145  }
146  else {
147  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "File I/O handler is not initialized!");
148  }
149  }
150 
151 } // namespace RBGen
152 
153 
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void Initialize(const Teuchos::RCP< Teuchos::ParameterList > &params)
Initialize file reader using.
Teuchos::RCP< Epetra_MultiVector > Read(const std::vector< std::string > &filenames)
Method for reading multiple files and putting them into an Epetra_MultiVector.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void Write(const Teuchos::RCP< const Epetra_MultiVector > &MV, const std::string &filename)
Method for writing one Epetra_MultiVector into a file.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")