Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_RKButcherTableau.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_RKButcherTableau_hpp
10 #define Tempus_RKButcherTableau_hpp
11 
12 // disable clang warnings
13 #ifdef __clang__
14 #pragma clang system_header
15 #endif
16 
19 
20 #include "Tempus_config.hpp"
22 
23 namespace Tempus {
24 
42 template <class Scalar>
44  : virtual public Teuchos::Describable,
45  virtual public Teuchos::VerboseObject<RKButcherTableau<Scalar> > {
46  public:
47  RKButcherTableau(std::string stepperType,
51  const int order, const int orderMin, const int orderMax,
54  bool checkC = true)
55  : description_(stepperType)
56  {
57  const int numStages = A.numRows();
61  TEUCHOS_ASSERT(order > 0);
62  A_ = A;
63  b_ = b;
64  c_ = c;
65  order_ = order;
68  this->set_isImplicit();
69  this->set_isDIRK();
70 
71  // Consistency check on b
73  Scalar sumb = ST::zero();
74  for (size_t i = 0; i < this->numStages(); i++) sumb += b_(i);
76  std::abs(ST::one() - sumb) > 1.0e-08, std::runtime_error,
77  "Error - Butcher Tableau b fails to satisfy Sum(b_i) = 1.\n"
78  << " Sum(b_i) = " << sumb << "\n");
79 
80  // Consistency check on c
81  if (checkC) {
82  for (size_t i = 0; i < this->numStages(); i++) {
83  Scalar sumai = ST::zero();
84  for (size_t j = 0; j < this->numStages(); j++) sumai += A_(i, j);
85  bool failed = false;
86  if (std::abs(sumai) > 1.0e-08)
87  failed = (std::abs((sumai - c_(i)) / sumai) > 1.0e-08);
88  else
89  failed = (std::abs(c_(i)) > 1.0e-08);
90 
92  failed, std::runtime_error,
93  "Error - Butcher Tableau fails to satisfy c_i = Sum_j(a_ij).\n"
94  << " Stage i = " << i << "\n"
95  << " c_i = " << c_(i) << "\n"
96  << " Sum_j(a_ij) = " << sumai << "\n"
97  << " This may be OK as some valid tableaus do not satisfy\n"
98  << " this condition. If so, construct this RKButcherTableau\n"
99  << " with checkC = false.\n");
100  }
101  }
102 
103  if (bstar.length() > 0) {
105  isEmbedded_ = true;
106  }
107  else {
108  isEmbedded_ = false;
109  }
110  bstar_ = bstar;
111  }
112 
114  virtual std::size_t numStages() const { return A_.numRows(); }
117  {
118  return A_;
119  }
122  {
123  return b_;
124  }
127  {
128  return bstar_;
129  }
132  {
133  return c_;
134  }
136  virtual int order() const { return order_; }
138  virtual int orderMin() const { return orderMin_; }
140  virtual int orderMax() const { return orderMax_; }
142  virtual bool isImplicit() const { return isImplicit_; }
144  virtual bool isDIRK() const { return isDIRK_; }
146  virtual bool isEmbedded() const { return isEmbedded_; }
148  virtual bool isTVD() const { return isTVD_; }
150  virtual Scalar getTVDCoeff() const { return tvdCoeff_; }
152  virtual void setTVDCoeff(const Scalar a) { tvdCoeff_ = a; }
153  virtual void setTVD(bool a) { isTVD_ = a; }
154  virtual void setEmbedded(bool a) { isEmbedded_ = a; }
155 
156  /* \brief Redefined from Teuchos::Describable */
158  virtual std::string description() const { return description_; }
159 
160  virtual void describe(Teuchos::FancyOStream& out,
161  const Teuchos::EVerbosityLevel verbLevel) const
162  {
163  out.setOutputToRootOnly(0);
164 
165  if (verbLevel != Teuchos::VERB_NONE) {
166  out << this->description() << std::endl;
167  out << "number of Stages = " << this->numStages() << std::endl;
168  out << "A = " << printMat(this->A()) << std::endl;
169  out << "b = " << printMat(this->b()) << std::endl;
170  out << "c = " << printMat(this->c()) << std::endl;
171  out << "bstar = " << printMat(this->bstar()) << std::endl;
172  out << "order = " << this->order() << std::endl;
173  out << "orderMin = " << this->orderMin() << std::endl;
174  out << "orderMax = " << this->orderMax() << std::endl;
175  out << "isImplicit = " << this->isImplicit() << std::endl;
176  out << "isDIRK = " << this->isDIRK() << std::endl;
177  out << "isEmbedded = " << this->isEmbedded() << std::endl;
178  if (this->isTVD())
179  out << "TVD Coeff = " << this->getTVDCoeff() << std::endl;
180  }
181  }
183 
184  bool operator==(const RKButcherTableau& t) const
185  {
186  const Scalar relTol = 1.0e-15;
187  if (A_->numRows() != t.A_->numRows() || A_->numCols() != t.A_->numCols()) {
188  return false;
189  }
190  else {
191  int i, j;
192  for (i = 0; i < A_->numRows(); i++) {
193  for (j = 0; j < A_->numCols(); j++) {
194  if (std::abs((t.A_(i, j) - A_(i, j)) / A_(i, j)) > relTol)
195  return false;
196  }
197  }
198  }
199 
200  if (b_->length() != t.b_->length() || b_->length() != t.c_->length() ||
201  b_->length() != t.bstar_->length()) {
202  return false;
203  }
204  else {
205  int i;
206  for (i = 0; i < A_->numRows(); i++) {
207  if (std::abs((t.b_(i) - b_(i)) / b_(i)) > relTol) return false;
208  if (std::abs((t.c_(i) - c_(i)) / c_(i)) > relTol) return false;
209  if (std::abs((t.bstar_(i) - bstar_(i)) / bstar_(i)) > relTol)
210  return false;
211  }
212  }
213  return true;
214  }
215 
216  bool operator!=(const RKButcherTableau& t) const { return !((*this) == t); }
217 
218  private:
220 
221  protected:
223  {
224  isImplicit_ = false;
225  for (size_t i = 0; i < this->numStages(); i++)
226  for (size_t j = i; j < this->numStages(); j++)
227  if (A_(i, j) != 0.0) isImplicit_ = true;
228  }
229 
231  void set_isDIRK()
232  {
233  isDIRK_ = true;
234  bool nonZero = false;
235  for (size_t i = 0; i < this->numStages(); i++) {
236  if (A_(i, i) != 0.0) nonZero = true;
237  for (size_t j = i + 1; j < this->numStages(); j++)
238  if (A_(i, j) != 0.0) isDIRK_ = false;
239  }
240  if (nonZero == false) isDIRK_ = false;
241  }
242 
243  std::string description_;
244 
248  int order_;
252  bool isDIRK_;
254  bool isTVD_ = false;
256  Scalar tvdCoeff_ = 0;
257 };
258 
259 } // namespace Tempus
260 
261 #endif // Tempus_RKButcherTableau_hpp
RKButcherTableau(std::string stepperType, const Teuchos::SerialDenseMatrix< int, Scalar > &A, const Teuchos::SerialDenseVector< int, Scalar > &b, const Teuchos::SerialDenseVector< int, Scalar > &c, const int order, const int orderMin, const int orderMax, const Teuchos::SerialDenseVector< int, Scalar > &bstar=Teuchos::SerialDenseVector< int, Scalar >(), bool checkC=true)
bool operator!=(const RKButcherTableau &t) const
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void setTVDCoeff(const Scalar a)
set TVD coefficient of RK method
virtual bool isTVD() const
Return true if the RK method is TVD.
Teuchos::SerialDenseVector< int, Scalar > bstar_
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual bool isImplicit() const
Return true if the RK method is implicit.
virtual bool isEmbedded() const
Return true if the RK method has embedded capabilities.
virtual bool isDIRK() const
Return true if the RK method is Diagonally Implicit.
virtual int orderMax() const
Return the maximum order.
virtual const Teuchos::SerialDenseVector< int, Scalar > & c() const
Return the vector of stage positions.
virtual Scalar getTVDCoeff() const
Return TVD coefficient of RK method.
void set_isDIRK()
DIRK is defined as if a_ij = 0 for j&gt;i and a_ii != 0 for at least one i.
virtual const Teuchos::SerialDenseVector< int, Scalar > & b() const
Return the vector of quadrature weights.
virtual std::string description() const
OrdinalType length() const
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
OrdinalType numCols() const
virtual const Teuchos::SerialDenseMatrix< int, Scalar > & A() const
Return the matrix coefficients.
virtual std::size_t numStages() const
Return the number of stages.
Teuchos::SerialDenseVector< int, Scalar > c_
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::SerialDenseMatrix< int, Scalar > A_
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
virtual const Teuchos::SerialDenseVector< int, Scalar > & bstar() const
Return the vector of quadrature weights for embedded methods.
virtual int order() const
Return the order.
Teuchos::SerialDenseVector< int, Scalar > b_
virtual int orderMin() const
Return the minimum order.
bool operator==(const RKButcherTableau &t) const
OrdinalType numRows() const