Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_StepperRKBase.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_StepperRKBase_hpp
10 #define Tempus_StepperRKBase_hpp
11 
12 #include "Thyra_VectorBase.hpp"
13 
14 #include "Tempus_config.hpp"
15 #include "Tempus_Stepper.hpp"
19 
20 
21 namespace Tempus {
22 
29 template<class Scalar>
30 class StepperRKBase : virtual public Tempus::Stepper<Scalar>
31 {
32 
33 public:
34 
36  { return tableau_; }
37 
38  virtual Scalar getOrder() const{return getTableau()->order();}
39  virtual Scalar getOrderMin() const{return getTableau()->orderMin();}
40  virtual Scalar getOrderMax() const{return getTableau()->orderMax();}
41  virtual int getNumberOfStages() const {return getTableau()->numStages();}
42 
43  virtual int getStageNumber() const { return stageNumber_; }
44  virtual void setStageNumber(int s) { stageNumber_ = s; }
45 
46  virtual void setUseEmbedded(bool a) { useEmbedded_ = a; }
47  virtual bool getUseEmbedded() const { return useEmbedded_; }
48 
50  {
51  if (appAction == Teuchos::null) {
52  // Create default appAction
55  } else {
56  stepperRKAppAction_ = appAction;
57  }
58  this->isInitialized_ = false;
59  }
60 
62  { return stepperRKAppAction_; }
63 
66  {
67  if (pl != Teuchos::null) {
69  this->setStepperValues(pl);
70  if (pl->isParameter("Use Embedded"))
71  this->setUseEmbedded(pl->get<bool>("Use Embedded"));
72  }
73  }
74 
77  {
78  TEUCHOS_TEST_FOR_EXCEPTION(pl == Teuchos::null,std::runtime_error,
79  "Error parsing general tableau. ParameterList is null.\n");
80 
82 
83  Teuchos::RCP<Teuchos::ParameterList> tableauPL = sublist(pl,"Tableau",true);
84  std::size_t numStages = 0;
85  int order = tableauPL->get<int>("order");
90 
91  // read in the A matrix
92  {
93  std::vector<std::string> A_row_tokens;
94  Tempus::StringTokenizer(A_row_tokens, tableauPL->get<std::string>("A"),
95  ";",true);
96 
97  // this is the only place where numStages is set
98  numStages = A_row_tokens.size();
99 
100  // allocate the matrix
101  A.shape(Teuchos::as<int>(numStages),Teuchos::as<int>(numStages));
102 
103  // fill the rows
104  for(std::size_t row=0;row<numStages;row++) {
105  // parse the row (tokenize on space)
106  std::vector<std::string> tokens;
107  Tempus::StringTokenizer(tokens,A_row_tokens[row]," ",true);
108 
109  std::vector<double> values;
110  Tempus::TokensToDoubles(values,tokens);
111 
112  TEUCHOS_TEST_FOR_EXCEPTION(values.size()!=numStages,std::runtime_error,
113  "Error parsing A matrix, wrong number of stages in row "
114  << row << ".\n");
115 
116  for(std::size_t col=0;col<numStages;col++)
117  A(row,col) = values[col];
118  }
119  }
120 
121  // size b and c vectors
122  b.size(Teuchos::as<int>(numStages));
123  c.size(Teuchos::as<int>(numStages));
124 
125  // read in the b vector
126  {
127  std::vector<std::string> tokens;
128  Tempus::StringTokenizer(tokens,tableauPL->get<std::string>("b")," ",true);
129  std::vector<double> values;
130  Tempus::TokensToDoubles(values,tokens);
131 
132  TEUCHOS_TEST_FOR_EXCEPTION(values.size()!=numStages,std::runtime_error,
133  "Error parsing b vector, wrong number of stages.\n");
134 
135  for(std::size_t i=0;i<numStages;i++)
136  b(i) = values[i];
137  }
138 
139  // read in the c vector
140  {
141  std::vector<std::string> tokens;
142  Tempus::StringTokenizer(tokens,tableauPL->get<std::string>("c")," ",true);
143  std::vector<double> values;
144  Tempus::TokensToDoubles(values,tokens);
145 
146  TEUCHOS_TEST_FOR_EXCEPTION(values.size()!=numStages,std::runtime_error,
147  "Error parsing c vector, wrong number of stages.\n");
148 
149  for(std::size_t i=0;i<numStages;i++)
150  c(i) = values[i];
151  }
152 
153  if (tableauPL->isParameter("bstar") &&
154  tableauPL->get<std::string>("bstar") != "") {
155  bstar.size(Teuchos::as<int>(numStages));
156  // read in the bstar vector
157  {
158  std::vector<std::string> tokens;
160  tokens, tableauPL->get<std::string>("bstar"), " ", true);
161  std::vector<double> values;
162  Tempus::TokensToDoubles(values,tokens);
163 
164  TEUCHOS_TEST_FOR_EXCEPTION(values.size()!=numStages,std::runtime_error,
165  "Error parsing bstar vector, wrong number of stages.\n"
166  " Number of RK stages = " << numStages << "\n"
167  " Number of bstar values = " << values.size() << "\n");
168 
169  for(std::size_t i=0;i<numStages;i++)
170  bstar(i) = values[i];
171  }
172  tableau = rcp(new RKButcherTableau<Scalar>(
173  "From ParameterList",A,b,c,order,order,order,bstar));
174  } else {
175  tableau = rcp(new RKButcherTableau<Scalar>(
176  "From ParameterList",A,b,c,order,order,order));
177  }
178  return tableau;
179  }
180 
181 
182 protected:
183 
185 
186  // For Embedded RK
192 
196 
197 };
198 
199 } // namespace Tempus
200 
201 #endif // Tempus_StepperRKBase_hpp
Teuchos::RCP< Thyra::VectorBase< Scalar > > sc
virtual void setAppAction(Teuchos::RCP< StepperRKAppAction< Scalar > > appAction)
virtual Teuchos::RCP< StepperRKAppAction< Scalar > > getAppAction() const
Teuchos::RCP< RKButcherTableau< Scalar > > tableau_
T & get(const std::string &name, T def_value)
virtual Scalar getOrderMin() const
virtual void setUseEmbedded(bool a)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void TokensToDoubles(std::vector< double > &values, const std::vector< std::string > &tokens)
Turn a vector of tokens into a vector of doubles.
virtual void setStageNumber(int s)
virtual Scalar getOrder() const
virtual void setStepperRKValues(Teuchos::RCP< Teuchos::ParameterList > pl)
Set StepperRK member data from the ParameterList.
bool isInitialized_
True if stepper&#39;s member data is initialized.
Teuchos::RCP< Thyra::VectorBase< Scalar > > ee_
Thyra Base interface for time steppers.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Application Action for StepperRKBase.
virtual int getNumberOfStages() const
int stageNumber_
The current Runge-Kutta stage number, {0,...,s-1}. -1 indicates outside stage loop.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
virtual int getStageNumber() const
virtual Teuchos::RCP< RKButcherTableau< Scalar > > createTableau(Teuchos::RCP< Teuchos::ParameterList > pl)
void setStepperValues(const Teuchos::RCP< Teuchos::ParameterList > pl)
Set Stepper member data from ParameterList.
Teuchos::RCP< Thyra::VectorBase< Scalar > > abs_u0
Teuchos::RCP< Thyra::VectorBase< Scalar > > abs_u
int size(OrdinalType length_in)
virtual Scalar getOrderMax() const
void StringTokenizer(std::vector< std::string > &tokens, const std::string &str, const std::string delimiters, bool trim)
Tokenize a string, put tokens in a vector.
Teuchos::RCP< StepperRKAppAction< Scalar > > stepperRKAppAction_
virtual bool getUseEmbedded() const
virtual Teuchos::RCP< const RKButcherTableau< Scalar > > getTableau() const
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const