rodas23.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 "rodas23.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(rodas23, 0);
34  addToRunTimeSelectionTable(ODESolver, rodas23, dictionary);
35 
36 const scalar
37  rodas23::c3 = 1,
38  rodas23::d1 = 1.0/2.0,
39  rodas23::d2 = 3.0/2.0,
40  rodas23::a31 = 2,
41  rodas23::a41 = 2,
42  rodas23::c21 = 4,
43  rodas23::c31 = 1,
44  rodas23::c32 = -1,
45  rodas23::c41 = 1,
46  rodas23::c42 = -1,
47  rodas23::c43 = -8.0/3.0,
48  rodas23::gamma = 1.0/2.0;
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
55 :
56  ODESolver(ode, dict),
57  adaptiveSolver(ode, dict),
58  k1_(n_),
59  k2_(n_),
60  k3_(n_),
61  dy_(n_),
62  err_(n_),
63  dydx_(n_),
64  dfdx_(n_),
65  dfdy_(n_, n_),
66  a_(n_, n_),
67  pivotIndices_(n_)
68 {}
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
74 {
75  if (ODESolver::resize())
76  {
78 
79  resizeField(k1_);
80  resizeField(k2_);
81  resizeField(k3_);
82  resizeField(dy_);
83  resizeField(err_);
84  resizeField(dydx_);
85  resizeField(dfdx_);
86  resizeMatrix(dfdy_);
87  resizeMatrix(a_);
88  resizeField(pivotIndices_);
89 
90  return true;
91  }
92  else
93  {
94  return false;
95  }
96 }
97 
98 
99 Foam::scalar Foam::rodas23::solve
100 (
101  const scalar x0,
102  const scalarField& y0,
103  const scalarField& dydx0,
104  const scalar dx,
105  scalarField& y
106 ) const
107 {
108  odes_.jacobian(x0, y0, dfdx_, dfdy_);
109 
110  for (label i=0; i<n_; i++)
111  {
112  for (label j=0; j<n_; j++)
113  {
114  a_(i, j) = -dfdy_(i, j);
115  }
116 
117  a_(i, i) += 1.0/(gamma*dx);
118  }
119 
120  LUDecompose(a_, pivotIndices_);
121 
122  // Calculate k1:
123  forAll(k1_, i)
124  {
125  k1_[i] = dydx0[i] + dx*d1*dfdx_[i];
126  }
127 
128  LUBacksubstitute(a_, pivotIndices_, k1_);
129 
130  // Calculate k2:
131  forAll(k2_, i)
132  {
133  k2_[i] = dydx0[i] + dx*d2*dfdx_[i] + c21*k1_[i]/dx;
134  }
135 
136  LUBacksubstitute(a_, pivotIndices_, k2_);
137 
138  // Calculate k3:
139  forAll(y, i)
140  {
141  dy_[i] = a31*k1_[i];
142  y[i] = y0[i] + dy_[i];
143  }
144 
145  odes_.derivatives(x0 + dx, y, dydx_);
146 
147  forAll(k3_, i)
148  {
149  k3_[i] = dydx_[i] + (c31*k1_[i] + c32*k2_[i])/dx;
150  }
151 
152  LUBacksubstitute(a_, pivotIndices_, k3_);
153 
154  // Calculate new state and error
155  forAll(y, i)
156  {
157  dy_[i] += k3_[i];
158  y[i] = y0[i] + dy_[i];
159  }
160 
161  odes_.derivatives(x0 + dx, y, dydx_);
162 
163  forAll(err_, i)
164  {
165  err_[i] = dydx_[i] + (c41*k1_[i] + c42*k2_[i] + c43*k3_[i])/dx;
166  }
167 
168  LUBacksubstitute(a_, pivotIndices_, err_);
169 
170  forAll(y, i)
171  {
172  y[i] = y0[i] + dy_[i] + err_[i];
173  }
174 
175  return normalizeError(y0, y, err_);
176 }
177 
178 
180 (
181  scalar& x,
182  scalarField& y,
183  scalar& dxTry
184 ) const
185 {
186  adaptiveSolver::solve(odes_, x, y, dxTry);
187 }
188 
189 
190 // ************************************************************************* //
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
rodas23(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: rodas23.C:54
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.
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 bool resize()
Resize the ODE solver.
Definition: rodas23.C:73
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
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.
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: rodas23.C:100
Namespace for OpenFOAM.
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution.