RBGen  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RBGen_EpetraCrsMatrixFileIOHandler.cpp
1 
2 #include "RBGen_EpetraCrsMatrixFileIOHandler.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_CrsMatrix.h"
10 
11 #include "EpetraExt_readEpetraLinearSystem.h"
12 #include "EpetraExt_RowMatrixOut.h"
13 
14 #ifdef EPETRA_MPI
15 #include "Epetra_MpiComm.h"
16 #else
17 #include "Epetra_SerialComm.h"
18 #endif
19 
20 #include "Teuchos_Assert.hpp"
21 
22 namespace RBGen {
23 
25  : isInit(false)
26  {
27  }
28 
30  {
31  // Get the "File I/O" sublist.
32  Teuchos::ParameterList& fileio_params = params->sublist( "File IO" );
33 
34  // Get the input path.
35  in_path = "";
36  if ( fileio_params.isParameter( "Data Input Path" ) ) {
37  in_path = Teuchos::getParameter<std::string>( fileio_params, "Data Input Path" );
38  }
39 
40  // Get the output path.
41  out_path = "";
42  if ( fileio_params.isParameter( "Data Output Path" ) ) {
43  out_path = Teuchos::getParameter<std::string>( fileio_params, "Data Output Path" );
44  }
45 
46  // This file i/o handler is now initialized.
47  isInit = true;
48  }
49 
50  Teuchos::RCP<Epetra_Operator> EpetraCrsMatrixFileIOHandler::Read( const std::vector<std::string>& filenames )
51  {
52 
54 
55  if (isInit) {
56 
57 #ifdef EPETRA_MPI
58  Epetra_MpiComm comm( MPI_COMM_WORLD );
59 #else
60  Epetra_SerialComm comm;
61 #endif
62  // This reader is only equipped to read in one matrix file.
63  TEUCHOS_TEST_FOR_EXCEPTION(filenames.size() > 1, std::invalid_argument, "File I/O handler cannot read more than one file!");
64 
65  // Open the data file
66  std::string temp_filename = in_path + filenames[0];
67 
68  // Create a null pointer to the Epetra_Map
70 
71  // Read in the matrix from file
72  EpetraExt::readEpetraLinearSystem( temp_filename, comm, &newMTX, &Map );
73 
74  }
75  else {
76  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "File I/O handler is not initialized!");
77  }
78  // Return.
79  return newMTX;
80  }
81 
82  void EpetraCrsMatrixFileIOHandler::Write( const Teuchos::RCP<const Epetra_Operator>& MTX, const std::string& filename )
83  {
84  if (isInit) {
85 
86  std::string temp_filename = out_path + filename;
87  EpetraExt::RowMatrixToMatrixMarketFile( temp_filename.c_str(), *(Teuchos::rcp_dynamic_cast<const Epetra_CrsMatrix>(MTX)) );
88 
89  }
90  else {
91  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "File I/O handler is not initialized!");
92  }
93  }
94 
95 } // namespace RBGen
96 
97 
void Initialize(const Teuchos::RCP< Teuchos::ParameterList > &params)
Initialize file reader using.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
bool isParameter(const std::string &name) const
void Write(const Teuchos::RCP< const Epetra_Operator > &MTX, const std::string &filename)
Method for writing one Epetra_CrsMatrix into a file using the same type as was.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Teuchos::RCP< Epetra_Operator > Read(const std::vector< std::string > &filenames)
Method for reading a file and constructing an Epetra_CrsMatrix.