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: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_StepperRKBase_hpp
11 #define Tempus_StepperRKBase_hpp
12 
13 #include "Thyra_VectorBase.hpp"
14 
15 #include "Tempus_config.hpp"
16 #include "Tempus_Stepper.hpp"
20 #include "Tempus_Stepper_ErrorNorm.hpp"
21 
22 namespace Tempus {
23 
30 template <class Scalar>
31 class StepperRKBase : virtual public Tempus::Stepper<Scalar> {
32  public:
34  {
35  return tableau_;
36  }
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)
47  {
48  useEmbedded_ = a;
49  this->setEmbeddedMemory();
50  this->isInitialized_ = false;
51  }
52 
53  virtual bool getUseEmbedded() const { return useEmbedded_; }
54 
56  &errCalculator = Teuchos::null)
57  {
58  if (errCalculator != Teuchos::null) {
59  stepperErrorNormCalculator_ = errCalculator;
60  }
61  else {
62  auto er = Teuchos::rcp(new Stepper_ErrorNorm<Scalar>());
64  }
65  }
66 
68  {
69  if (appAction == Teuchos::null) {
70  // Create default appAction
73  }
74  else {
75  stepperRKAppAction_ = appAction;
76  }
77  this->isInitialized_ = false;
78  }
79 
81  {
82  return stepperRKAppAction_;
83  }
84 
87  {
88  if (pl != Teuchos::null) {
90  this->setStepperValues(pl);
91  if (pl->isParameter("Use Embedded"))
92  this->setUseEmbedded(pl->get<bool>("Use Embedded"));
93  }
94  }
95 
98  {
100  pl == Teuchos::null, std::runtime_error,
101  "Error parsing general tableau. ParameterList is null.\n");
102 
104 
106  sublist(pl, "Tableau", true);
107  std::size_t numStages = 0;
108  int order = tableauPL->get<int>("order");
113 
114  // read in the A matrix
115  {
116  std::vector<std::string> A_row_tokens;
117  Tempus::StringTokenizer(A_row_tokens, tableauPL->get<std::string>("A"),
118  ";", true);
119 
120  // this is the only place where numStages is set
121  numStages = A_row_tokens.size();
122 
123  // allocate the matrix
124  A.shape(Teuchos::as<int>(numStages), Teuchos::as<int>(numStages));
125 
126  // fill the rows
127  for (std::size_t row = 0; row < numStages; row++) {
128  // parse the row (tokenize on space)
129  std::vector<std::string> tokens;
130  Tempus::StringTokenizer(tokens, A_row_tokens[row], " ", true);
131 
132  std::vector<double> values;
133  Tempus::TokensToDoubles(values, tokens);
134 
136  values.size() != numStages, std::runtime_error,
137  "Error parsing A matrix, wrong number of stages in row "
138  << row << ".\n");
139 
140  for (std::size_t col = 0; col < numStages; col++)
141  A(row, col) = values[col];
142  }
143  }
144 
145  // size b and c vectors
146  b.size(Teuchos::as<int>(numStages));
147  c.size(Teuchos::as<int>(numStages));
148 
149  // read in the b vector
150  {
151  std::vector<std::string> tokens;
152  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("b"), " ",
153  true);
154  std::vector<double> values;
155  Tempus::TokensToDoubles(values, tokens);
156 
158  values.size() != numStages, std::runtime_error,
159  "Error parsing b vector, wrong number of stages.\n");
160 
161  for (std::size_t i = 0; i < numStages; i++) b(i) = values[i];
162  }
163 
164  // read in the c vector
165  {
166  std::vector<std::string> tokens;
167  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("c"), " ",
168  true);
169  std::vector<double> values;
170  Tempus::TokensToDoubles(values, tokens);
171 
173  values.size() != numStages, std::runtime_error,
174  "Error parsing c vector, wrong number of stages.\n");
175 
176  for (std::size_t i = 0; i < numStages; i++) c(i) = values[i];
177  }
178 
179  if (tableauPL->isParameter("bstar") &&
180  tableauPL->get<std::string>("bstar") != "") {
181  bstar.size(Teuchos::as<int>(numStages));
182  // read in the bstar vector
183  {
184  std::vector<std::string> tokens;
185  Tempus::StringTokenizer(tokens, tableauPL->get<std::string>("bstar"),
186  " ", true);
187  std::vector<double> values;
188  Tempus::TokensToDoubles(values, tokens);
189 
191  values.size() != numStages, std::runtime_error,
192  "Error parsing bstar vector, wrong number of stages.\n"
193  << " Number of RK stages = " << numStages
194  << "\n Number of bstar values = "
195  << values.size() << "\n");
196 
197  for (std::size_t i = 0; i < numStages; i++) bstar(i) = values[i];
198  }
199  tableau = rcp(new RKButcherTableau<Scalar>("From ParameterList", A, b, c,
200  order, order, order, bstar));
201  }
202  else {
203  tableau = rcp(new RKButcherTableau<Scalar>("From ParameterList", A, b, c,
204  order, order, order));
205  }
206  return tableau;
207  }
208 
209  protected:
210  virtual void setEmbeddedMemory() {}
211 
213 
214  // For Embedded RK
220 
222 
227 };
228 
229 } // namespace Tempus
230 
231 #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