Rosenbrock34.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2013-2016 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 "Rosenbrock34.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(Rosenbrock34, 0);
34  addToRunTimeSelectionTable(ODESolver, Rosenbrock34, dictionary);
35 
36 const scalar
37  // Constants by Shampine
38  // More accurate than the L-Stable coefficients for small step-size
39  // but less stable for large step-size
40  Rosenbrock34::a21 = 2,
41  Rosenbrock34::a31 = 48.0/25.0,
42  Rosenbrock34::a32 = 6.0/25.0,
43 
44  Rosenbrock34::c21 = -8,
45  Rosenbrock34::c31 = 372.0/25.0,
46  Rosenbrock34::c32 = 12.0/5.0,
47 
48  Rosenbrock34::c41 = -112.0/125.0,
49  Rosenbrock34::c42 = -54.0/125.0,
50  Rosenbrock34::c43 = -2.0/5.0,
51 
52  Rosenbrock34::b1 = 19.0/9.0,
53  Rosenbrock34::b2 = 1.0/2.0,
54  Rosenbrock34::b3 = 25.0/108.0,
55  Rosenbrock34::b4 = 125.0/108.0,
56 
57  Rosenbrock34::e1 = 34.0/108.0,
58  Rosenbrock34::e2 = 7.0/36.0,
59  Rosenbrock34::e3 = 0,
60  Rosenbrock34::e4 = 125.0/108.0,
61 
62  Rosenbrock34::gamma = 1.0/2.0,
63  Rosenbrock34::c2 = 1,
64  Rosenbrock34::c3 = 3.0/5.0,
65 
66  Rosenbrock34::d1 = 1.0/2.0,
67  Rosenbrock34::d2 = -3.0/2.0,
68  Rosenbrock34::d3 = 605.0/250.0,
69  Rosenbrock34::d4 = 29.0/250.0;
70 
71  /*
72  // L-Stable constants from Hairer et. al.
73  Rosenbrock34::a21 = 2,
74  Rosenbrock34::a31 = 1.867943637803922,
75  Rosenbrock34::a32 = 0.2344449711399156,
76 
77  Rosenbrock34::c21 = -7.137615036412310,
78  Rosenbrock34::c31 = 2.580708087951457,
79  Rosenbrock34::c32 = 0.6515950076447975,
80  Rosenbrock34::c41 = -2.137148994382534,
81  Rosenbrock34::c42 = -0.3214669691237626,
82  Rosenbrock34::c43 = -0.6949742501781779,
83 
84  Rosenbrock34::b1 = 2.255570073418735,
85  Rosenbrock34::b2 = 0.2870493262186792,
86  Rosenbrock34::b3 = 0.435317943184018,
87  Rosenbrock34::b4 = 1.093502252409163,
88 
89  Rosenbrock34::e1 = -0.2815431932141155,
90  Rosenbrock34::e2 = -0.0727619912493892,
91  Rosenbrock34::e3 = -0.1082196201495311,
92  Rosenbrock34::e4 = -1.093502252409163,
93 
94  Rosenbrock34::gamma = 0.57282,
95  Rosenbrock34::c2 = 1.14564,
96  Rosenbrock34::c3 = 0.65521686381559,
97 
98  Rosenbrock34::d1 = 0.57282,
99  Rosenbrock34::d2 = -1.769193891319233,
100  Rosenbrock34::d3 = 0.7592633437920482,
101  Rosenbrock34::d4 = -0.1049021087100450;
102  */
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
107 
109 :
110  ODESolver(ode, dict),
111  adaptiveSolver(ode, dict),
112  k1_(n_),
113  k2_(n_),
114  k3_(n_),
115  k4_(n_),
116  err_(n_),
117  dydx_(n_),
118  dfdx_(n_),
119  dfdy_(n_, n_),
120  a_(n_, n_),
121  pivotIndices_(n_)
122 {}
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
128 {
129  if (ODESolver::resize())
130  {
132 
133  resizeField(k1_);
134  resizeField(k2_);
135  resizeField(k3_);
136  resizeField(k4_);
137  resizeField(err_);
138  resizeField(dydx_);
139  resizeField(dfdx_);
140  resizeMatrix(dfdy_);
141  resizeMatrix(a_);
142  resizeField(pivotIndices_);
143 
144  return true;
145  }
146  else
147  {
148  return false;
149  }
150 }
151 
152 
153 Foam::scalar Foam::Rosenbrock34::solve
154 (
155  const scalar x0,
156  const scalarField& y0,
157  const scalarField& dydx0,
158  const scalar dx,
159  scalarField& y
160 ) const
161 {
162  odes_.jacobian(x0, y0, dfdx_, dfdy_);
163 
164  for (label i=0; i<n_; i++)
165  {
166  for (label j=0; j<n_; j++)
167  {
168  a_(i, j) = -dfdy_(i, j);
169  }
170 
171  a_(i, i) += 1.0/(gamma*dx);
172  }
173 
174  LUDecompose(a_, pivotIndices_);
175 
176  // Calculate k1:
177  forAll(k1_, i)
178  {
179  k1_[i] = dydx0[i] + dx*d1*dfdx_[i];
180  }
181 
182  LUBacksubstitute(a_, pivotIndices_, k1_);
183 
184  // Calculate k2:
185  forAll(y, i)
186  {
187  y[i] = y0[i] + a21*k1_[i];
188  }
189 
190  odes_.derivatives(x0 + c2*dx, y, dydx_);
191 
192  forAll(k2_, i)
193  {
194  k2_[i] = dydx_[i] + dx*d2*dfdx_[i] + c21*k1_[i]/dx;
195  }
196 
197  LUBacksubstitute(a_, pivotIndices_, k2_);
198 
199  // Calculate k3:
200  forAll(y, i)
201  {
202  y[i] = y0[i] + a31*k1_[i] + a32*k2_[i];
203  }
204 
205  odes_.derivatives(x0 + c3*dx, y, dydx_);
206 
207  forAll(k3_, i)
208  {
209  k3_[i] = dydx_[i] + dx*d3*dfdx_[i] + (c31*k1_[i] + c32*k2_[i])/dx;
210  }
211 
212  LUBacksubstitute(a_, pivotIndices_, k3_);
213 
214  // Calculate k4:
215  forAll(k4_, i)
216  {
217  k4_[i] = dydx_[i] + dx*d4*dfdx_[i]
218  + (c41*k1_[i] + c42*k2_[i] + c43*k3_[i])/dx;
219  }
220 
221  LUBacksubstitute(a_, pivotIndices_, k4_);
222 
223  // Calculate error and update state:
224  forAll(y, i)
225  {
226  y[i] = y0[i] + b1*k1_[i] + b2*k2_[i] + b3*k3_[i] + b4*k4_[i];
227  err_[i] = e1*k1_[i] + e2*k2_[i] + e4*k4_[i];
228  }
229 
230  return normalizeError(y0, y, err_);
231 }
232 
233 
235 (
236  scalar& x,
237  scalarField& y,
238  scalar& dxTry
239 ) const
240 {
241  adaptiveSolver::solve(odes_, x, y, dxTry);
242 }
243 
244 
245 // ************************************************************************* //
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:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
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:137
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
virtual bool resize()
Resize the ODE solver.
Definition: Rosenbrock34.C:127
An ODE solver for chemistry.
Definition: ode.H:50
Macros for easy insertion into run-time selection tables.
virtual void jacobian(const scalar x, const scalarField &y, scalarField &dfdx, scalarSquareMatrix &dfdy) const =0
Calculate the Jacobian of the system.
bool resize(const label n)
Resize the ODE solver.
void resizeMatrix(scalarSquareMatrix &m) const
Definition: ODESolverI.H:61
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: Rosenbrock34.C:154
defineTypeNameAndDebug(combustionModel, 0)
Rosenbrock34(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: Rosenbrock34.C:108
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
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.
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution.