All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RKF45.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2013-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "RKF45.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(RKF45, 0);
34  addToRunTimeSelectionTable(ODESolver, RKF45, dictionary);
35 
36 const scalar
37  RKF45::c2 = 1.0/4.0,
38  RKF45::c3 = 3.0/8.0,
39  RKF45::c4 = 12.0/13.0,
40  RKF45::c5 = 1.0,
41  RKF45::c6 = 1.0/2.0,
42 
43  RKF45::a21 = 1.0/4.0,
44  RKF45::a31 = 3.0/32.0,
45  RKF45::a32 = 9.0/32.0,
46  RKF45::a41 = 1932.0/2197.0,
47  RKF45::a42 = -7200.0/2197.0,
48  RKF45::a43 = 7296.0/2197.0,
49  RKF45::a51 = 439.0/216.0,
50  RKF45::a52 = -8.0,
51  RKF45::a53 = 3680.0/513.0,
52  RKF45::a54 = -845.0/4104.0,
53  RKF45::a61 = -8.0/27.0,
54  RKF45::a62 = 2.0,
55  RKF45::a63 = -3544.0/2565.0,
56  RKF45::a64 = 1859.0/4104.0,
57  RKF45::a65 = -11.0/40.0,
58 
59  RKF45::b1 = 16.0/135.0,
60  RKF45::b3 = 6656.0/12825.0,
61  RKF45::b4 = 28561.0/56430.0,
62  RKF45::b5 = -9.0/50.0,
63  RKF45::b6 = 2.0/55.0,
64 
65  RKF45::e1 = 25.0/216.0 - RKF45::b1,
66  RKF45::e3 = 1408.0/2565.0 - RKF45::b3,
67  RKF45::e4 = 2197.0/4104.0 - RKF45::b4,
68  RKF45::e5 = -1.0/5.0 - RKF45::b5,
69  RKF45::e6 = -RKF45::b6;
70 }
71 
72 
73 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
74 
76 :
77  ODESolver(ode, dict),
78  adaptiveSolver(ode, dict),
79  yTemp_(n_),
80  k2_(n_),
81  k3_(n_),
82  k4_(n_),
83  k5_(n_),
84  k6_(n_),
85  err_(n_)
86 {}
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
92 {
93  if (ODESolver::resize())
94  {
96 
97  resizeField(yTemp_);
98  resizeField(k2_);
99  resizeField(k3_);
100  resizeField(k4_);
101  resizeField(k5_);
102  resizeField(k6_);
103  resizeField(err_);
104 
105  return true;
106  }
107  else
108  {
109  return false;
110  }
111 }
112 
113 
114 Foam::scalar Foam::RKF45::solve
115 (
116  const scalar x0,
117  const scalarField& y0,
118  const scalarField& dydx0,
119  const scalar dx,
120  scalarField& y
121 ) const
122 {
123  forAll(yTemp_, i)
124  {
125  yTemp_[i] = y0[i] + a21*dx*dydx0[i];
126  }
127 
128  odes_.derivatives(x0 + c2*dx, yTemp_, k2_);
129 
130  forAll(yTemp_, i)
131  {
132  yTemp_[i] = y0[i] + dx*(a31*dydx0[i] + a32*k2_[i]);
133  }
134 
135  odes_.derivatives(x0 + c3*dx, yTemp_, k3_);
136 
137  forAll(yTemp_, i)
138  {
139  yTemp_[i] = y0[i] + dx*(a41*dydx0[i] + a42*k2_[i] + a43*k3_[i]);
140  }
141 
142  odes_.derivatives(x0 + c4*dx, yTemp_, k4_);
143 
144  forAll(yTemp_, i)
145  {
146  yTemp_[i] = y0[i]
147  + dx*(a51*dydx0[i] + a52*k2_[i] + a53*k3_[i] + a54*k4_[i]);
148  }
149 
150  odes_.derivatives(x0 + c5*dx, yTemp_, k5_);
151 
152  forAll(yTemp_, i)
153  {
154  yTemp_[i] = y0[i]
155  + dx
156  *(a61*dydx0[i] + a62*k2_[i] + a63*k3_[i] + a64*k4_[i] + a65*k5_[i]);
157  }
158 
159  odes_.derivatives(x0 + c6*dx, yTemp_, k6_);
160 
161  // Calculate the 5th-order solution
162  forAll(y, i)
163  {
164  y[i] = y0[i]
165  + dx
166  *(b1*dydx0[i] + b3*k3_[i] + b4*k4_[i] + b5*k5_[i] + b6*k6_[i]);
167  }
168 
169  // Calculate the error estimate from the difference between the
170  // 4th-order and 5th-order solutions
171  forAll(err_, i)
172  {
173  err_[i] =
174  dx
175  *(e1*dydx0[i] + e3*k3_[i] + e4*k4_[i] + e5*k5_[i] + e6*k6_[i]);
176  }
177 
178  return normalizeError(y0, y, err_);
179 }
180 
181 
183 (
184  scalar& x,
185  scalarField& y,
186  scalar& dxTry
187 ) const
188 {
189  adaptiveSolver::solve(odes_, x, y, dxTry);
190 }
191 
192 
193 // ************************************************************************* //
virtual scalar solve(const scalar x0, const scalarField &y0, const scalarField &dydx0, const scalar dx, scalarField &y) const =0
Solve a single step dx and return the error.
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:46
virtual bool resize()=0
Resize the ODE solver.
Definition: ODESolver.C:89
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
RKF45(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: RKF45.C:75
An ODE solver for chemistry.
Definition: ode.H:50
Macros for easy insertion into run-time selection tables.
bool resize(const label n)
Resize the ODE solver.
const ODESystem & odes_
Reference to ODESystem.
Definition: ODESolver.H:58
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
virtual scalar solve(const scalar x0, const scalarField &y0, const scalarField &dydx0, const scalar dx, scalarField &y) const
Solve a single step dx and return the error.
Definition: RKF45.C:115
defineTypeNameAndDebug(combustionModel, 0)
scalar normalizeError(const scalarField &y0, const scalarField &y, const scalarField &err) const
Return the nomalized scalar error.
Definition: ODESolver.C:40
Abstract base-class for ODE system solvers.
Definition: ODESolver.H:50
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:48
virtual bool resize()
Resize the ODE solver.
Definition: RKF45.C:91
label n_
Size of the ODESystem (adjustable)
Definition: ODESolver.H:64
virtual void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.
Namespace for OpenFOAM.