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 #include "Tempus_Stepper_ErrorNorm.hpp"
20 
21 namespace Tempus {
22 
29 template <class Scalar>
30 class StepperRKBase : virtual public Tempus::Stepper<Scalar> {
31  public:
33  {
34  return tableau_;
35  }
36 
37  virtual Scalar getOrder() const { return getTableau()->order(); }
38  virtual Scalar getOrderMin() const { return getTableau()->orderMin(); }
39  virtual Scalar getOrderMax() const { return getTableau()->orderMax(); }
40  virtual int getNumberOfStages() const { return getTableau()->numStages(); }
41 
42  virtual int getStageNumber() const { return stageNumber_; }
43  virtual void setStageNumber(int s) { stageNumber_ = s; }
44 
45  virtual void setUseEmbedded(bool a)
46  {
47  useEmbedded_ = a;
48  this->setEmbeddedMemory();
49  this->isInitialized_ = false;
50  }
51 
52  virtual bool getUseEmbedded() const { return useEmbedded_; }
53 
55  &errCalculator = Teuchos::null)
56  {
57  if (errCalculator != Teuchos::null) {
58  stepperErrorNormCalculator_ = errCalculator;
59  }
60  else {
61  auto er = Teuchos::rcp(new Stepper_ErrorNorm<Scalar>());
63  }
64  }
65 
67  {
68  if (appAction == Teuchos::null) {
69  // Create default appAction
72  }
73  else {
74  stepperRKAppAction_ = appAction;
75  }
76  this->isInitialized_ = false;
77  }
78 
80  {
81  return stepperRKAppAction_;
82  }
83 
86  {
87  if (pl != Teuchos::null) {
89  this->setStepperValues(pl);
90  if (pl->isParameter("Use Embedded"))
91  this->setUseEmbedded(pl->get<bool>("Use Embedded"));
92  }
93  }
94 
97  {
99  pl == Teuchos::null, std::runtime_error,
100  "Error parsing general tableau. ParameterList is null.\n");
101 
103 
105  sublist(pl, "Tableau", true);
106  std::size_t numStages = 0;
107  int order = tableauPL->get<int>("order");
112 
113  // read in the A matrix
114  {
115  std::vector<std::string> A_row_tokens;
116  Tempus::StringTokenizer(A_row_tokens, tableauPL->get<std::string>("A"),
117  ";", true);
118 
119  // this is the only place where numStages is set
120  numStages = A_row_tokens.size();
121 
122  // allocate the matrix
123  A.shape(Teuchos::as<int>(numStages), Teuchos::as<int>(numStages));
124 
125  // fill the rows
126  for (std::size_t row = 0; row < numStages; row++) {
127  // parse the row (tokenize on space)
128  std::vector<std::string> tokens;
129  Tempus::StringTokenizer(tokens, A_row_tokens[row], " ", true);
130 
131  std::vector<double> values;
132  Tempus::TokensToDoubles(values, tokens);
133 
135  values.size() != numStages, std::runtime_error,
136  "Error parsing A matrix, wrong number of stages in row "
137  << row << ".\n");
138 
139  for (std::size_t col = 0; col < numStages; col++)
140  A(row, col) = values[col];
141  }
142  }
143 
144  // size b and c vectors
145  b.size(Teuchos::as<int>(numStages));
146  c.size(Teuchos::as<int>(numStages));
147 
148  // read in the b vector
149  {
150  std::vector<std::string> tokens;
151  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("b"), " ",
152  true);
153  std::vector<double> values;
154  Tempus::TokensToDoubles(values, tokens);
155 
157  values.size() != numStages, std::runtime_error,
158  "Error parsing b vector, wrong number of stages.\n");
159 
160  for (std::size_t i = 0; i < numStages; i++) b(i) = values[i];
161  }
162 
163  // read in the c vector
164  {
165  std::vector<std::string> tokens;
166  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("c"), " ",
167  true);
168  std::vector<double> values;
169  Tempus::TokensToDoubles(values, tokens);
170 
172  values.size() != numStages, std::runtime_error,
173  "Error parsing c vector, wrong number of stages.\n");
174 
175  for (std::size_t i = 0; i < numStages; i++) c(i) = values[i];
176  }
177 
178  if (tableauPL->isParameter("bstar") &&
179  tableauPL->get<std::string>("bstar") != "") {
180  bstar.size(Teuchos::as<int>(numStages));
181  // read in the bstar vector
182  {
183  std::vector<std::string> tokens;
184  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("bstar"),
185  " ", true);
186  std::vector<double> values;
187  Tempus::TokensToDoubles(values, tokens);
188 
190  values.size() != numStages, std::runtime_error,
191  "Error parsing bstar vector, wrong number of stages.\n"
192  << " Number of RK stages = " << numStages
193  << "\n Number of bstar values = "
194  << values.size() << "\n");
195 
196  for (std::size_t i = 0; i < numStages; i++) bstar(i) = values[i];
197  }
198  tableau = rcp(new RKButcherTableau<Scalar>("From ParameterList", A, b, c,
199  order, order, order, bstar));
200  }
201  else {
202  tableau = rcp(new RKButcherTableau<Scalar>("From ParameterList", A, b, c,
203  order, order, order));
204  }
205  return tableau;
206  }
207 
208  protected:
209  virtual void setEmbeddedMemory() {}
210 
212 
213  // For Embedded RK
219 
221 
226 };
227 
228 } // namespace Tempus
229 
230 #endif // Tempus_StepperRKBase_hpp
Teuchos::RCP< Thyra::VectorBase< Scalar > > sc
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
virtual void setAppAction(Teuchos::RCP< StepperRKAppAction< Scalar >> appAction)
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
Stepper_ErrorNorm provides error norm calcualtions for variable time stepping.
int size(OrdinalType length_in)
virtual void setErrorNorm(const Teuchos::RCP< Stepper_ErrorNorm< Scalar >> &errCalculator=Teuchos::null)
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_
Teuchos::RCP< Stepper_ErrorNorm< Scalar > > stepperErrorNormCalculator_
virtual bool getUseEmbedded() const
virtual Teuchos::RCP< const RKButcherTableau< Scalar > > getTableau() const
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const