Rosenbrock12.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 "Rosenbrock12.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(Rosenbrock12, 0);
37  addToRunTimeSelectionTable(ODESolver, Rosenbrock12, dictionary);
38 
39 const scalar
40  Rosenbrock12::gamma = 1 + 1.0/std::sqrt(2.0),
41  Rosenbrock12::a21 = 1.0/gamma,
42  Rosenbrock12::c2 = 1.0,
43  Rosenbrock12::c21 = -2.0/gamma,
44  Rosenbrock12::b1 = (3.0/2.0)/gamma,
45  Rosenbrock12::b2 = (1.0/2.0)/gamma,
46  Rosenbrock12::e1 = b1 - 1.0/gamma,
47  Rosenbrock12::e2 = b2,
48  Rosenbrock12::d1 = gamma,
49  Rosenbrock12::d2 = -gamma;
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 :
57  ODESolver(ode, dict),
59  k1_(n_),
60  k2_(n_),
61  err_(n_),
62  dydx_(n_),
63  dfdx_(n_),
64  dfdy_(n_, n_),
65  a_(n_, n_),
66  pivotIndices_(n_)
67 {}
68 
69 
70 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
71 
73 {
74  if (ODESolver::resize())
75  {
77 
78  resizeField(k1_);
79  resizeField(k2_);
80  resizeField(err_);
81  resizeField(dydx_);
82  resizeField(dfdx_);
83  resizeMatrix(dfdy_);
84  resizeMatrix(a_);
85  resizeField(pivotIndices_);
86 
87  return true;
88  }
89 
90  return false;
91 }
92 
93 
94 Foam::scalar Foam::Rosenbrock12::solve
95 (
96  const scalar x0,
97  const scalarField& y0,
98  const scalarField& dydx0,
99  const scalar dx,
100  scalarField& y
101 ) const
102 {
103  odes_.jacobian(x0, y0, dfdx_, dfdy_);
104 
105  for (label i=0; i<n_; i++)
106  {
107  for (label j=0; j<n_; j++)
108  {
109  a_(i, j) = -dfdy_(i, j);
110  }
111 
112  a_(i, i) += 1.0/(gamma*dx);
113  }
114 
115  LUDecompose(a_, pivotIndices_);
116 
117  // Calculate k1:
118  forAll(k1_, i)
119  {
120  k1_[i] = dydx0[i] + dx*d1*dfdx_[i];
121  }
122 
123  LUBacksubstitute(a_, pivotIndices_, k1_);
124 
125  // Calculate k2:
126  forAll(y, i)
127  {
128  y[i] = y0[i] + a21*k1_[i];
129  }
130 
131  odes_.derivatives(x0 + c2*dx, y, dydx_);
132 
133  forAll(k2_, i)
134  {
135  k2_[i] = dydx_[i] + dx*d2*dfdx_[i] + c21*k1_[i]/dx;
136  }
137 
138  LUBacksubstitute(a_, pivotIndices_, k2_);
139 
140  // Calculate error and update state:
141  forAll(y, i)
142  {
143  y[i] = y0[i] + b1*k1_[i] + b2*k2_[i];
144  err_[i] = e1*k1_[i] + e2*k2_[i];
145  }
146 
147  return normalizeError(y0, y, err_);
148 }
149 
150 
152 (
153  scalar& x,
154  scalarField& y,
155  scalar& dxTry
156 ) const
157 {
158  adaptiveSolver::solve(odes_, x, y, dxTry);
159 }
160 
161 
162 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::ODESolver
Abstract base-class for ODE system solvers.
Definition: ODESolver.H:56
Foam::Rosenbrock12::Rosenbrock12
Rosenbrock12(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: Rosenbrock12.C:55
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
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.
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:123
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::ODESystem
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:49
Foam::Rosenbrock12::resize
virtual bool resize()
Resize the ODE solver.
Definition: Rosenbrock12.C:72
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
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
Rosenbrock12.H
Foam::Rosenbrock12::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: Rosenbrock12.C:95
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