Trapezoid.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2013-2018 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 "Trapezoid.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(Trapezoid, 0);
34  addToRunTimeSelectionTable(ODESolver, Trapezoid, dictionary);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 :
42  ODESolver(ode, dict),
43  adaptiveSolver(ode, dict),
44  err_(n_)
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
51 {
52  if (ODESolver::resize())
53  {
55 
56  resizeField(err_);
57 
58  return true;
59  }
60  else
61  {
62  return false;
63  }
64 }
65 
66 
67 Foam::scalar Foam::Trapezoid::solve
68 (
69  const scalar x0,
70  const scalarField& y0,
71  const scalarField& dydx0,
72  const scalar dx,
73  scalarField& y
74 ) const
75 {
76  // Predict the state using 1st-order Trapezoid method
77  forAll(y, i)
78  {
79  y[i] = y0[i] + dx*dydx0[i];
80  }
81 
82  // Evaluate the system for the predicted state
83  odes_.derivatives(x0 + dx, y, err_);
84 
85  // Update the state as the average between the prediction and the correction
86  // and estimate the error from the difference
87  forAll(y, i)
88  {
89  y[i] = y0[i] + 0.5*dx*(dydx0[i] + err_[i]);
90  err_[i] = 0.5*dx*(err_[i] - dydx0[i]);
91  }
92 
93  return normalizeError(y0, y, err_);
94 }
95 
96 
98 (
99  scalar& x,
100  scalarField& y,
101  scalar& dxTry
102 ) const
103 {
104  adaptiveSolver::solve(odes_, x, y, dxTry);
105 }
106 
107 
108 // ************************************************************************* //
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:434
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
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: Trapezoid.C:68
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
An ODE solver for chemistry.
Definition: ode.H:50
Macros for easy insertion into run-time selection tables.
bool resize(const label n)
Resize the ODE solver.
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
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:48
Trapezoid(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: Trapezoid.C:40
label n_
Size of the ODESystem (adjustable)
Definition: ODESolver.H:64
virtual bool resize()
Resize the ODE solver.
Definition: Trapezoid.C:50
virtual void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.
Namespace for OpenFOAM.