KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
butcher_tableau.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Eduard Gomez
11 //
12 //
13 
14 #pragma once
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "containers/array_1d.h"
22 
23 namespace Kratos
24 {
27 
28 
32 
33 
37 
38 
42 
43 
47 
48 
64 template<typename Derived, unsigned int TOrder, unsigned int TSubstepCount>
66 {
67 
68 public:
71 
72  using VectorType = std::vector<double>;
73 
74  /* Using the following constructs allows us to multiply parts of vectors with parts of matrices
75  * while avoiding BOOST's size checks. This is useful to skip multiplications by zero, since
76  * for all explicit runge-kutta methods a_ij = 0 for i>j
77  */
78 
79  using RowType = std::vector<double>;
80  using MatrixType = std::vector<RowType>;
81 
82  static constexpr unsigned int Order() {return TOrder;}
83  static constexpr unsigned int SubstepCount() {return TSubstepCount; }
84 
88 
90  virtual ~ButcherTableau() = default;
91 
95 
96 
100 
117  std::tuple<RowType::const_iterator, RowType::const_iterator> GetMatrixRow(const unsigned int SubStepIndex) const
118  {
119  return {GetMatrixRowBegin(SubStepIndex), GetMatrixRowEnd(SubStepIndex)};
120  }
121 
122  RowType::const_iterator GetMatrixRowBegin(const unsigned int SubStepIndex) const
123  {
124  KRATOS_DEBUG_ERROR_IF(SubStepIndex == 0) << "Provided substep is 0. This must be greater than or equal to 1." << std::endl;
125  return mA[SubStepIndex - 1].begin();
126  }
127 
128  RowType::const_iterator GetMatrixRowEnd(const unsigned int SubStepIndex) const
129  {
130  KRATOS_DEBUG_ERROR_IF(SubStepIndex == 0) << "Provided substep is 0. This must be greater than or equal to 1." << std::endl;
131  return mA[SubStepIndex - 1].begin() + SubStepIndex;
132  }
133 
134  constexpr const VectorType& GetWeights() const
135  {
136  return mB;
137  }
138 
139  constexpr double GetIntegrationTheta(const unsigned int SubStepIndex) const
140  {
141  KRATOS_DEBUG_ERROR_IF(SubStepIndex == 0) << "Provided substep is 0. This must be greater than or equal to 1." << std::endl;
142  return mC[SubStepIndex - 1];
143  }
144 
148 
149  static std::string Name()
150  {
151  return Derived::Name();
152  }
153 
154  virtual std::string Info() const = 0;
155 
156 protected:
159 
160 
164 
165  const MatrixType mA = Derived::GenerateRKMatrix(); // Runge-Kutta matrix
166  const VectorType mB = Derived::GenerateWeights(); // Weights vector
167  const VectorType mC = Derived::GenerateThetasVector(); // Nodes vector
168 
172 
173 
177 
181 
182 
186 
187 
191 
192 private:
193 
197 
198 
202 
203 
207 
208 
212 
213 
217 
218 
222 
223 
227 };
228 
230 
233 
234 
235 
236 class ButcherTableauForwardEuler : public ButcherTableau<ButcherTableauForwardEuler, 1, 1>
237 {
238 public:
240 
242  {
243  return {};
244  }
245 
247  {
248  return VectorType {1.0};
249  }
250 
252  {
253  return VectorType {0.0};
254  }
255 
256  static std::string Name()
257  {
258  return "butcher_tableau_forward_euler";
259  }
260 
261  std::string Info() const override
262  {
263  return "ButcherTableauForwardEuler";
264  }
265 };
266 
267 
268 class ButcherTableauMidPointMethod : public ButcherTableau<ButcherTableauMidPointMethod, 2, 2>
269 {
270 public:
272 
274  {
275  MatrixType A(1, RowType(1));
276  A[0][0] = 0.5;
277  return A;
278  }
279 
281  {
282  return VectorType {0.0, 1.0};
283  }
284 
286  {
287  return VectorType {0.0, 0.5};
288  }
289 
290  static std::string Name()
291  {
292  return "butcher_tableau_midpoint_method";
293  }
294 
295  std::string Info() const override
296  {
297  return "ButcherTableauMidPointMethod";
298  }
299 };
300 
307 class ButcherTableauRK3TVD : public ButcherTableau<ButcherTableauRK3TVD, 3, 3>
308 {
309 public:
311 
313  {
314  MatrixType A(2, RowType(2));
315  A[0][0] = 1;
316  A[1][0] = 0.25;
317  A[0][1] = 0.0;
318  A[1][1] = 0.25;
319  return A;
320  }
321 
323  {
324  return VectorType {1.0 / 6.0,
325  1.0 / 6.0,
326  2.0 / 3.0};
327  }
328 
330  {
331  return VectorType {0.0,
332  1.0,
333  0.5};
334  }
335 
336  static std::string Name()
337  {
338  return "butcher_tableau_RK3TVD";
339  }
340 
341  std::string Info() const override
342  {
343  return "ButcherTableauRK3TVD";
344  }
345 };
346 
347 
348 class ButcherTableauRK4 : public ButcherTableau<ButcherTableauRK4, 4, 4>
349 {
350 public:
353  {
354  MatrixType A(3, RowType(3,0.0));
355  A[0][0] = 0.5;
356  A[1][1] = 0.5;
357  A[2][2] = 1.0;
358  return A;
359  }
360 
362  {
363  return VectorType {1.0 / 6.0,
364  1.0 / 3.0,
365  1.0 / 3.0,
366  1.0 / 6.0};
367  }
368 
370  {
371  return VectorType {0.0,
372  0.5,
373  0.5,
374  1.0};
375  }
376 
377  static std::string Name()
378  {
379  return "butcher_tableau_RK4";
380  }
381 
382  std::string Info() const override
383  {
384  return "ButcherTableauRK4";
385  }
386 };
387 
389 
390 } /* namespace Kratos.*/
Definition: butcher_tableau.h:237
static const BaseType::MatrixType GenerateRKMatrix()
Definition: butcher_tableau.h:241
ButcherTableau< ButcherTableauForwardEuler, 1, 1 > BaseType
Definition: butcher_tableau.h:239
static const BaseType::VectorType GenerateThetasVector()
Definition: butcher_tableau.h:251
static const BaseType::VectorType GenerateWeights()
Definition: butcher_tableau.h:246
static std::string Name()
Definition: butcher_tableau.h:256
std::string Info() const override
Definition: butcher_tableau.h:261
Butcher tableau for Runge-Kutta method.
Definition: butcher_tableau.h:66
virtual std::string Info() const =0
std::vector< double > VectorType
Definition: butcher_tableau.h:72
constexpr double GetIntegrationTheta(const unsigned int SubStepIndex) const
Definition: butcher_tableau.h:139
const VectorType mC
Definition: butcher_tableau.h:167
RowType::const_iterator GetMatrixRowEnd(const unsigned int SubStepIndex) const
Definition: butcher_tableau.h:128
virtual ~ButcherTableau()=default
Destructor.
std::vector< RowType > MatrixType
Definition: butcher_tableau.h:80
std::tuple< RowType::const_iterator, RowType::const_iterator > GetMatrixRow(const unsigned int SubStepIndex) const
Definition: butcher_tableau.h:117
const VectorType mB
Definition: butcher_tableau.h:166
std::vector< double > RowType
Definition: butcher_tableau.h:79
static constexpr unsigned int SubstepCount()
Definition: butcher_tableau.h:83
static constexpr unsigned int Order()
Definition: butcher_tableau.h:82
constexpr const VectorType & GetWeights() const
Definition: butcher_tableau.h:134
RowType::const_iterator GetMatrixRowBegin(const unsigned int SubStepIndex) const
Definition: butcher_tableau.h:122
static std::string Name()
Definition: butcher_tableau.h:149
const MatrixType mA
Definition: butcher_tableau.h:165
Definition: butcher_tableau.h:269
static const BaseType::VectorType GenerateThetasVector()
Definition: butcher_tableau.h:285
static const BaseType::VectorType GenerateWeights()
Definition: butcher_tableau.h:280
std::string Info() const override
Definition: butcher_tableau.h:295
ButcherTableau< ButcherTableauMidPointMethod, 2, 2 > BaseType
Definition: butcher_tableau.h:271
static const BaseType::MatrixType GenerateRKMatrix()
Definition: butcher_tableau.h:273
static std::string Name()
Definition: butcher_tableau.h:290
Explicit total variation diminishing 3rd order Runge-Kutta.
Definition: butcher_tableau.h:308
static const BaseType::VectorType GenerateThetasVector()
Definition: butcher_tableau.h:329
std::string Info() const override
Definition: butcher_tableau.h:341
static std::string Name()
Definition: butcher_tableau.h:336
ButcherTableau< ButcherTableauRK3TVD, 3, 3 > BaseType
Definition: butcher_tableau.h:310
static const BaseType::VectorType GenerateWeights()
Definition: butcher_tableau.h:322
static const BaseType::MatrixType GenerateRKMatrix()
Definition: butcher_tableau.h:312
Definition: butcher_tableau.h:349
static const BaseType::MatrixType GenerateRKMatrix()
Definition: butcher_tableau.h:352
static const BaseType::VectorType GenerateWeights()
Definition: butcher_tableau.h:361
ButcherTableau< ButcherTableauRK4, 4, 4 > BaseType
Definition: butcher_tableau.h:351
std::string Info() const override
Definition: butcher_tableau.h:382
static const BaseType::VectorType GenerateThetasVector()
Definition: butcher_tableau.h:369
static std::string Name()
Definition: butcher_tableau.h:377
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
A
Definition: sensitivityMatrix.py:70