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