RBGen  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RBGen_MSPreprocessor.cpp
1 
2 
3 #include "RBGen_MSPreprocessor.h"
4 #include "Epetra_MultiVector.h"
5 
6 namespace RBGen {
7 
8  MSPreprocessor::MSPreprocessor(): isInitialized_(false), scale_(1.0)
9  {
10  // Insert the acceptable preprocessing types for this class
11  // Subtract off vectors given through an input file.
12  preproc_types_.push_back("Input File");
13  // Subtract off the arithmetic mean of the snapshot set.
14  preproc_types_.push_back("Arithmetic Mean");
15  }
16 
19  {
20  // Save the file i/o handler.
21  fileio_ = fileio;
22 
23  // Get the preprocessing method sublist.
24  const Teuchos::ParameterList& preproc_params = params->sublist( "Preprocessing Method" );
25 
26  // Get the preprocessing method.
27  std::string preprocMethod = Teuchos::getParameter< std::string >( preproc_params, "Method" );
28  // if (preprocMethod != "Modified Snapshot") THROW AN ERROR!!!
29 
30  // Get the preprocessing type.
31  preprocType_ = Teuchos::getParameter< std::string >( preproc_params, "Type" );
32 
33  // Check that this type is valid.
34  bool typeValid = false;
35  std::vector<std::string>::iterator p = preproc_types_.begin();
36  while ( p != preproc_types_.end() && !typeValid ) {
37  if ( *p == preprocType_ )
38  typeValid = true;
39  ++p;
40  }
41  // if (!typeValid) THROW AN ERROR!!!
42 
43  if (preprocType_ == "Input File") {
44  //
45  // Try to get the input file from the parameter list
46  //
47  try {
48  input_file_ = Teuchos::getParameter< std::string >( preproc_params, "Filename" );
49  }
50  catch (std::exception &e) {
51  std::cout<<"The modified snapshot input file has not been specified"<<std::endl;
52  }
53  }
54  //
55  // Check if there is a scaling factor on the steady state vector
56  //
57  scale_ = 1.0;
58  if ( preproc_params.isParameter("Scaling Factor") ) {
59  scale_ = Teuchos::getParameter< double >( preproc_params, "Scaling Factor" );
60  }
61 
62  /* try {
63  scalings_ = Teuchos::getParameter< std::vector< double > >( *params, "Snapshot Scaling" );
64  }
65  catch (std::exception &e) {
66  std::cout<<"The snapshot scaling vector has not been specified in the input!"<<std::endl;
67  }
68  //
69  // Try to get the snapshot scaling indices vector
70  //
71  try {
72  scaling_idx_ = Teuchos::getParameter< std::vector< std::pair<int,int> > >( *params, "Snapshot Scaling Indices" );
73  }
74  catch (std::exception &e) {
75  std::cout<<"The snapshot scaling indices have not been specified in the input!"<<std::endl;
76  }
77  */
78  if ( scalings_.size() != scaling_idx_.size() ) {
79  std::cout<<"The scaling vector is not the same size as the number of index pairs!"<< std::endl;
80  }
81  //
82  // We're initialized now.
83  //
84  isInitialized_ = true;
85  }
86 
88  {
89  if (isInitialized_) {
90 
91  if (preprocType_ == "Input File") {
92  //
93  // Get the steady state vector from the file.
94  //
95  std::vector< std::string > steady_file( 1, input_file_ );
96  msVector_ = fileio_->Read( steady_file );
97  Epetra_MultiVector* colMV = 0;
98  //
99  // Remove the scaled steady state vector.
100  //
101  for (int i=0; i<ss->NumVectors(); i++) {
102  colMV = new Epetra_MultiVector( View, *ss, i, 1 );
103  colMV->Update( -1.0*scale_, *msVector_, 1.0 );
104  delete colMV;
105  }
106  }
107  else if (preprocType_ == "Arithmetic Mean") {
108  //
109  // Compute the arithmetic mean from the input snapshots
110  //
111  msVector_ = Teuchos::rcp( new Epetra_MultiVector( ss->Map(), 1 ) );
112  Epetra_MultiVector* colMV = 0;
113  //
114  // Sum up all the multivectors; compute the average.
115  //
116  for (int i=0; i<ss->NumVectors(); i++) {
117  colMV = new Epetra_MultiVector( View, *ss, i, 1 );
118  msVector_->Update( 1.0, *colMV, 1.0 );
119  delete colMV;
120  }
121  msVector_->Scale( 1.0 / ss->NumVectors() );
122  //
123  // Remove the arithmetic mean from the snapshot set.
124  //
125  for (int i=0; i<ss->NumVectors(); i++) {
126  colMV = new Epetra_MultiVector( View, *ss, i, 1 );
127  colMV->Update( -1.0*scale_, *msVector_, 1.0 );
128  delete colMV;
129  }
130  }
131  }
132  else {
133  // Throw error here!
134  }
135  }
136 
137 } // end of RBGen namespace
138 
MSPreprocessor()
Default constructor.
void Initialize(const Teuchos::RCP< Teuchos::ParameterList > &params, const Teuchos::RCP< FileIOHandler< Epetra_MultiVector > > &fileio=Teuchos::null)
Initialize preprocessor.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void Preprocess(Teuchos::RCP< Epetra_MultiVector > &ss)
Preprocess the snapshot set passed in.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")