Rosenbrock23.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2013-2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "Rosenbrock23.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(Rosenbrock23, 0);
37  addToRunTimeSelectionTable(ODESolver, Rosenbrock23, dictionary);
38 
39 const scalar
40  Rosenbrock23::a21 = 1,
41  Rosenbrock23::a31 = 1,
42  Rosenbrock23::a32 = 0,
43 
44  Rosenbrock23::c21 = -1.0156171083877702091975600115545,
45  Rosenbrock23::c31 = 4.0759956452537699824805835358067,
46  Rosenbrock23::c32 = 9.2076794298330791242156818474003,
47 
48  Rosenbrock23::b1 = 1,
49  Rosenbrock23::b2 = 6.1697947043828245592553615689730,
50  Rosenbrock23::b3 = -0.4277225654321857332623837380651,
51 
52  Rosenbrock23::e1 = 0.5,
53  Rosenbrock23::e2 = -2.9079558716805469821718236208017,
54  Rosenbrock23::e3 = 0.2235406989781156962736090927619,
55 
56  Rosenbrock23::gamma = 0.43586652150845899941601945119356,
57  Rosenbrock23::c2 = 0.43586652150845899941601945119356,
58 
59  Rosenbrock23::d1 = 0.43586652150845899941601945119356,
60  Rosenbrock23::d2 = 0.24291996454816804366592249683314,
61  Rosenbrock23::d3 = 2.1851380027664058511513169485832;
62 }
63 
64 
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
66 
68 :
69  ODESolver(ode, dict),
71  k1_(n_),
72  k2_(n_),
73  k3_(n_),
74  err_(n_),
75  dydx_(n_),
76  dfdx_(n_),
77  dfdy_(n_, n_),
78  a_(n_, n_),
79  pivotIndices_(n_)
80 {}
81 
82 
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
84 
86 {
87  if (ODESolver::resize())
88  {
90 
91  resizeField(k1_);
92  resizeField(k2_);
93  resizeField(k3_);
94  resizeField(err_);
95  resizeField(dydx_);
96  resizeField(dfdx_);
97  resizeMatrix(dfdy_);
98  resizeMatrix(a_);
99  resizeField(pivotIndices_);
100 
101  return true;
102  }
103 
104  return false;
105 }
106 
107 
108 Foam::scalar Foam::Rosenbrock23::solve
109 (
110  const scalar x0,
111  const scalarField& y0,
112  const scalarField& dydx0,
113  const scalar dx,
114  scalarField& y
115 ) const
116 {
117  odes_.jacobian(x0, y0, dfdx_, dfdy_);
118 
119  for (label i=0; i<n_; i++)
120  {
121  for (label j=0; j<n_; j++)
122  {
123  a_(i, j) = -dfdy_(i, j);
124  }
125 
126  a_(i, i) += 1.0/(gamma*dx);
127  }
128 
129  LUDecompose(a_, pivotIndices_);
130 
131  // Calculate k1:
132  forAll(k1_, i)
133  {
134  k1_[i] = dydx0[i] + dx*d1*dfdx_[i];
135  }
136 
137  LUBacksubstitute(a_, pivotIndices_, k1_);
138 
139  // Calculate k2:
140  forAll(y, i)
141  {
142  y[i] = y0[i] + a21*k1_[i];
143  }
144 
145  odes_.derivatives(x0 + c2*dx, y, dydx_);
146 
147  forAll(k2_, i)
148  {
149  k2_[i] = dydx_[i] + dx*d2*dfdx_[i] + c21*k1_[i]/dx;
150  }
151 
152  LUBacksubstitute(a_, pivotIndices_, k2_);
153 
154  // Calculate k3:
155  forAll(k3_, i)
156  {
157  k3_[i] = dydx_[i] + dx*d3*dfdx_[i]
158  + (c31*k1_[i] + c32*k2_[i])/dx;
159  }
160 
161  LUBacksubstitute(a_, pivotIndices_, k3_);
162 
163  // Calculate error and update state:
164  forAll(y, i)
165  {
166  y[i] = y0[i] + b1*k1_[i] + b2*k2_[i] + b3*k3_[i];
167  err_[i] = e1*k1_[i] + e2*k2_[i] + e3*k3_[i];
168  }
169 
170  return normalizeError(y0, y, err_);
171 }
172 
173 
175 (
176  scalar& x,
177  scalarField& y,
178  scalar& dxTry
179 ) const
180 {
181  adaptiveSolver::solve(odes_, x, y, dxTry);
182 }
183 
184 
185 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::ODESolver
Abstract base-class for ODE system solvers.
Definition: ODESolver.H:56
Foam::Rosenbrock23::solve
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: Rosenbrock23.C:109
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Rosenbrock23::Rosenbrock23
Rosenbrock23(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: Rosenbrock23.C:67
Foam::adaptiveSolver::solve
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.
Rosenbrock23.H
Foam::Field< scalar >
Foam::y0
dimensionedScalar y0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:281
Foam::ode
An ODE solver for chemistry.
Definition: ode.H:52
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
Foam::LUBacksubstitute
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
Definition: scalarMatricesTemplates.C:120
Foam::LUDecompose
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
Definition: scalarMatrices.C:34
Foam::Rosenbrock23::resize
virtual bool resize()
Resize the ODE solver.
Definition: Rosenbrock23.C:85
Foam::ODESystem
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:49
Foam::adaptiveSolver::resize
bool resize(const label n)
Resize the ODE solver.
Definition: adaptiveSolver.C:52
gamma
const scalar gamma
Definition: EEqn.H:9
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::ODESolver::resize
virtual bool resize()=0
Resize the ODE solver.
Definition: ODESolver.C:92
Foam::adaptiveSolver
Definition: adaptiveSolver.H:53
y
scalar y
Definition: LISASMDCalcMethod1.H:14