RBGen  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RBGenDriver_EpetraMV.cpp
1 
2 #include "RBGen_Params.h"
3 #include "RBGen_Utils.h"
4 #include "RBGen_EpetraMVFileIOFactory.h"
5 #include "RBGen_EpetraMVMethodFactory.h"
6 #include "RBGen_EpetraMVPreprocessorFactory.h"
7 #include "RBGen_EpetraCrsMatrixFileIOHandler.h"
8 #include "RBGen_PODMethod.hpp"
9 
10 #include "Epetra_Map.h"
11 #include "Epetra_LocalMap.h"
12 #include "Epetra_LAPACK.h"
13 #include "Epetra_MultiVector.h"
14 
15 #ifdef EPETRA_MPI
16 #include "Epetra_MpiComm.h"
17 #include "mpi.h"
18 #else
19 #include "Epetra_SerialComm.h"
20 #endif
21 
22 #include "Teuchos_Array.hpp"
23 #include "Teuchos_Time.hpp"
24 #include "Teuchos_TimeMonitor.hpp"
27 #include "Teuchos_Assert.hpp"
28 
29 int main( int argc, char* argv[] )
30 {
31 
32 #ifdef EPETRA_MPI
33  // Initialize MPI
34  MPI_Init(&argc,&argv);
35  Epetra_MpiComm Comm( MPI_COMM_WORLD );
36 #else
37  Epetra_SerialComm Comm;
38 #endif
39 
40  // Create command line processor
42  RBGen_CLP.recogniseAllOptions( false );
43  RBGen_CLP.throwExceptions( false );
44 
45  // Generate list of acceptable command line options
46  bool verbose = false;
47  std::string xml_file = "";
48  RBGen_CLP.setOption("verbose", "quiet", &verbose, "Print messages and results.");
49  RBGen_CLP.setOption("xml-file", &xml_file, "XML Input File");
50 
51  // Process command line.
53  parseReturn= RBGen_CLP.parse( argc, argv );
55  return 0;
56  }
58 #ifdef EPETRA_MPI
59  MPI_Finalize();
60 #endif
61  return -1; // Error!
62  }
63 
64  // Check to make sure an XML input file was provided
65  TEUCHOS_TEST_FOR_EXCEPTION(xml_file == "", std::invalid_argument, "ERROR: An XML file was not provided; use --xml-file to provide an XML input file for this RBGen driver.");
66 
68  //
69  // ---------------------------------------------------------------
70  // CREATE THE INITIAL PARAMETER LIST FROM THE INPUT XML FILE
71  // ---------------------------------------------------------------
72  //
74  if (verbose && Comm.MyPID() == 0)
75  {
76  std::cout<<"-------------------------------------------------------"<<std::endl;
77  std::cout<<"Input Parameter List: "<<std::endl;
78  std::cout<<"-------------------------------------------------------"<<std::endl;
79  BasisParams->print();
80  }
81  //
82  // ---------------------------------------------------------------
83  // CREATE THE FILE I/O HANDLER
84  // ---------------------------------------------------------------
85  //
86  // - First create the abstract factory for the file i/o handler.
87  //
88  RBGen::EpetraMVFileIOFactory fio_factory;
89  //
90  // - Then use the abstract factory to create the file i/o handler specified in the parameter list.
91  //
92  Teuchos::RCP<Teuchos::Time> timerFileIO = Teuchos::rcp( new Teuchos::Time("Create File I/O Handler") );
93  timersRBGen.push_back( timerFileIO );
94  //
98  {
99  Teuchos::TimeMonitor lcltimer( *timerFileIO );
100  mvFileIO = fio_factory.create( *BasisParams );
101  //
102  // Initialize file IO handlers
103  //
104  mvFileIO->Initialize( BasisParams );
105  opFileIO->Initialize( BasisParams );
106  }
107  if (verbose && Comm.MyPID() == 0)
108  {
109  std::cout<<"-------------------------------------------------------"<<std::endl;
110  std::cout<<"File I/O Handlers Generated"<<std::endl;
111  std::cout<<"-------------------------------------------------------"<<std::endl;
112  }
113  //
114  // ---------------------------------------------------------------
115  // READ IN THE DATA SET / SNAPSHOT SET & PREPROCESS
116  // ( this will be a separate abstract class type )
117  // ---------------------------------------------------------------
118  //
119  Teuchos::RCP<std::vector<std::string> > filenames = RBGen::genFileList( *BasisParams );
120  Teuchos::RCP<Teuchos::Time> timerSnapshotIn = Teuchos::rcp( new Teuchos::Time("Reading in Snapshot Set") );
121  timersRBGen.push_back( timerSnapshotIn );
122  //
124  {
125  Teuchos::TimeMonitor lcltimer( *timerSnapshotIn );
126  testMV = mvFileIO->Read( *filenames );
127  }
128 
129  RBGen::EpetraMVPreprocessorFactory preprocess_factory;
130 
131  Teuchos::RCP<Teuchos::Time> timerCreatePreprocessor = Teuchos::rcp( new Teuchos::Time("Create Preprocessor") );
132  timersRBGen.push_back( timerCreatePreprocessor );
134  {
135  Teuchos::TimeMonitor lcltimer( *timerCreatePreprocessor );
136  prep = preprocess_factory.create( *BasisParams );
137  //
138  // Initialize preprocessor.
139  //
140  prep->Initialize( BasisParams, mvFileIO );
141  }
142 
143  Teuchos::RCP<Teuchos::Time> timerPreprocess = Teuchos::rcp( new Teuchos::Time("Preprocess Snapshot Set") );
144  timersRBGen.push_back( timerPreprocess );
145  {
146  Teuchos::TimeMonitor lcltimer( *timerPreprocess );
147  prep->Preprocess( testMV );
148  }
149 
150  if (verbose && Comm.MyPID() == 0)
151  {
152  std::cout<<"-------------------------------------------------------"<<std::endl;
153  std::cout<<"Snapshot Set Imported and Preprocessed"<<std::endl;
154  std::cout<<"-------------------------------------------------------"<<std::endl;
155  }
156  //
157  // ---------------------------------------------------------------
158  // COMPUTE THE REDUCED BASIS
159  // ---------------------------------------------------------------
160  //
161  // - First create the abstract factory for the reduced basis methods.
162  //
163  RBGen::EpetraMVMethodFactory mthd_factory;
164  //
165  // - Then use the abstract factory to create the method specified in the parameter list.
166  //
167  Teuchos::RCP<Teuchos::Time> timerCreateMethod = Teuchos::rcp( new Teuchos::Time("Create Reduced Basis Method") );
168  timersRBGen.push_back( timerCreateMethod );
170  {
171  Teuchos::TimeMonitor lcltimer( *timerCreateMethod );
172  method = mthd_factory.create( *BasisParams );
173  //
174  // Initialize reduced basis method.
175  //
176  method->Initialize( BasisParams, testMV, opFileIO );
177  }
178  //
179  // - Call the computeBasis method on the reduced basis method object.
180  //
181  Teuchos::RCP<Teuchos::Time> timerComputeBasis = Teuchos::rcp( new Teuchos::Time("Reduced Basis Computation") );
182  timersRBGen.push_back( timerComputeBasis );
183  {
184  Teuchos::TimeMonitor lcltimer( *timerComputeBasis );
185  method->computeBasis();
186  }
187  //
188  // - Retrieve the computed basis from the method object.
189  //
190  Teuchos::RCP<const Epetra_MultiVector> basisMV = method->getBasis();
191  //
192  // Since we're using a POD method, we can dynamic cast to get the singular values.
193  //
194  Teuchos::RCP<RBGen::PODMethod<double> > pod_method = Teuchos::rcp_dynamic_cast<RBGen::PODMethod<double> >( method );
195  const std::vector<double> sv = pod_method->getSingularValues();
196  //
197  if (verbose && Comm.MyPID() == 0) {
198  std::cout<<"-------------------------------------------------------"<<std::endl;
199  std::cout<<"Computed Singular Values : "<<std::endl;
200  std::cout<<"-------------------------------------------------------"<<std::endl;
201  for (unsigned int i=0; i<sv.size(); ++i) { std::cout << sv[i] << std::endl; }
202  }
203 
204  if (Comm.MyPID() == 0) {
205  std::cout<<"-------------------------------------------------------"<<std::endl;
206  std::cout<<"RBGen Computation Time Breakdown (seconds) : "<<std::endl;
207  std::cout<<"-------------------------------------------------------"<<std::endl;
208  for (unsigned int i=0; i<timersRBGen.size(); ++i)
209  std::cout << std::left << std::setw(40) << timersRBGen[i]->name() << " : "
210  << std::setw(15) << timersRBGen[i]->totalElapsedTime() << std::endl;
211  std::cout<<"-------------------------------------------------------"<<std::endl;
212  }
213  //
214  // ---------------------------------------------------------------
215  // POSTPROCESS BASIS (not necessary right now)
216  // ---------------------------------------------------------------
217  //
218  //
219  // ---------------------------------------------------------------
220  // WRITE OUT THE REDUCED BASIS
221  // ---------------------------------------------------------------
222  //
223  if ( BasisParams->isSublist( "File IO" ) ) {
224  Teuchos::ParameterList fileio_params = BasisParams->sublist( "File IO" );
225  if ( fileio_params.isParameter( "Reduced Basis Output File" ) ) {
226  std::string outfile = Teuchos::getParameter<std::string>( fileio_params, "Reduced Basis Output File" );
227  mvFileIO->Write( basisMV, outfile );
228  }
229  }
230  //
231 #ifdef EPETRA_MPI
232  // Finalize MPI
233  MPI_Finalize();
234 #endif
235 
236  return 0;
237 }
238 
239 
Teuchos::RCP< Teuchos::ParameterList > createParams(const std::string &filename)
Create a Teuchos::ParameterList from an XML file.
void recogniseAllOptions(const bool &recogniseAllOptions)
Specialization of a PreprocessorFactor for Epetra_MultiVector datasets.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
FileIOHandler for reading EpetraCrsMatrix data from a file using EpetraExt.
Specialization of MethodFactory for Epetra_MultiVector datasets.
int MyPID() const
Abstract factory for creating a concrete FileIOFactory for reading an Epetra_MultiVector.
Teuchos::RCP< std::vector< std::string > > genFileList(const Teuchos::ParameterList &params)
Extract the filename list from a Teuchos::ParameterList.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
bool isSublist(const std::string &name) const
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
void throwExceptions(const bool &throwExceptions)
void push_back(const value_type &x)
size_type size() const
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")