ODESolver.H
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) 2011-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 Class
28  Foam::ODESolver
29 
30 Group
31  grpODESolvers
32 
33 Description
34  Abstract base-class for ODE system solvers
35 
36 SourceFiles
37  ODESolver.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef ODESolver_H
42 #define ODESolver_H
43 
44 #include "ODESystem.H"
45 #include "typeInfo.H"
46 #include "autoPtr.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class ODESolver Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class ODESolver
58 {
59 
60 protected:
61 
62  // Protected data
63 
64  //- Reference to ODESystem
65  const ODESystem& odes_;
66 
67  //- Maximum size of the ODESystem
68  const label maxN_;
69 
70  //- Size of the ODESystem (adjustable)
71  mutable label n_;
72 
73  //- Absolute convergence tolerance per step
75 
76  //- Relative convergence tolerance per step
78 
79  //- The maximum number of sub-steps allowed for the integration step
80  label maxSteps_;
81 
82 
83  // Protected Member Functions
84 
85  //- Return the nomalized scalar error
86  scalar normalizeError
87  (
88  const scalarField& y0,
89  const scalarField& y,
90  const scalarField& err
91  ) const;
92 
93  //- No copy construct
94  ODESolver(const ODESolver&) = delete;
95 
96  //- No copy assignment
97  void operator=(const ODESolver&) = delete;
98 
99 
100 public:
101 
102  friend class ODESystem;
103 
104  //- Runtime type information
105  TypeName("ODESolver");
106 
107  class stepState
108  {
109  public:
110 
111  const bool forward;
112  scalar dxTry;
113  scalar dxDid;
114  bool first;
115  bool last;
116  bool reject;
117  bool prevReject;
118 
119  stepState(const scalar dx)
120  :
121  forward(dx > 0 ? true : false),
122  dxTry(dx),
123  dxDid(0),
124  first(true),
125  last(false),
126  reject(false),
127  prevReject(false)
128  {}
129  };
130 
131 
132  // Declare run-time constructor selection table
133 
135  (
136  autoPtr,
137  ODESolver,
138  dictionary,
139  (const ODESystem& ode, const dictionary& dict),
140  (ode, dict)
141  );
142 
143 
144  // Constructors
145 
146  //- Construct for given ODESystem
147  ODESolver(const ODESystem& ode, const dictionary& dict);
148 
149  //- Construct for given ODESystem specifying tolerances
150  ODESolver
151  (
152  const ODESystem& ode,
153  const scalarField& absTol,
154  const scalarField& relTol
155  );
156 
157 
158  // Selectors
159 
160  //- Select null constructed
161  static autoPtr<ODESolver> New
162  (
163  const ODESystem& ode,
164  const dictionary& dict
165  );
166 
167 
168  //- Destructor
169  virtual ~ODESolver() = default;
170 
171 
172  // Member Functions
173 
174  //- Return the number of equations to solve
175  inline label nEqns() const;
176 
177  //- Return access to the absolute tolerance field
178  inline scalarField& absTol();
179 
180  //- Return access to the relative tolerance field
181  inline scalarField& relTol();
182 
183  //- Resize the ODE solver
184  virtual bool resize() = 0;
185 
186  template<class Type>
187  static inline void resizeField(UList<Type>& f, const label n);
188 
189  template<class Type>
190  inline void resizeField(UList<Type>& f) const;
191 
192  inline void resizeMatrix(scalarSquareMatrix& m) const;
193 
194  //- Solve the ODE system as far as possible up to dxTry
195  // adjusting the step as necessary to provide a solution within
196  // the specified tolerance.
197  // Update the state and return an estimate for the next step in dxTry
198  virtual void solve
199  (
200  scalar& x,
201  scalarField& y,
202  scalar& dxTry
203  ) const;
204 
205  //- Solve the ODE system as far as possible up to dxTry
206  // adjusting the step as necessary to provide a solution within
207  // the specified tolerance.
208  // Update the state and return an estimate for the next step in dxTry
209  virtual void solve
210  (
211  scalar& x,
212  scalarField& y,
213  stepState& step
214  ) const;
215 
216  //- Solve the ODE system from xStart to xEnd, update the state
217  // and return an estimate for the next step in dxTry
218  virtual void solve
219  (
220  const scalar xStart,
221  const scalar xEnd,
222  scalarField& y,
223  scalar& dxEst
224  ) const;
225 };
226 
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 } // End namespace Foam
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 #include "ODESolverI.H"
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 #endif
239 
240 // ************************************************************************* //
Foam::ODESolver::normalizeError
scalar normalizeError(const scalarField &y0, const scalarField &y, const scalarField &err) const
Return the nomalized scalar error.
Definition: ODESolver.C:43
Foam::ODESolver::stepState
Definition: ODESolver.H:106
Foam::ODESolver::stepState::reject
bool reject
Definition: ODESolver.H:115
Foam::ODESolver
Abstract base-class for ODE system solvers.
Definition: ODESolver.H:56
typeInfo.H
Foam::ODESolver::New
static autoPtr< ODESolver > New(const ODESystem &ode, const dictionary &dict)
Select null constructed.
Definition: ODESolverNew.C:34
Foam::ODESolver::odes_
const ODESystem & odes_
Reference to ODESystem.
Definition: ODESolver.H:64
ODESystem.H
Foam::ODESolver::operator=
void operator=(const ODESolver &)=delete
No copy assignment.
Foam::ODESolver::stepState::forward
const bool forward
Definition: ODESolver.H:110
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::ODESolver::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, ODESolver, dictionary,(const ODESystem &ode, const dictionary &dict),(ode, dict))
Foam::ODESolver::relTol
scalarField & relTol()
Return access to the relative tolerance field.
Definition: ODESolverI.H:43
Foam::Field< scalar >
ODESolverI.H
Foam::y0
dimensionedScalar y0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:281
Foam::ODESolver::~ODESolver
virtual ~ODESolver()=default
Destructor.
Foam::ode
An ODE solver for chemistry.
Definition: ode.H:52
Foam::ODESolver::maxSteps_
label maxSteps_
The maximum number of sub-steps allowed for the integration step.
Definition: ODESolver.H:79
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::ODESolver::stepState::dxTry
scalar dxTry
Definition: ODESolver.H:111
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::ODESolver::absTol
scalarField & absTol()
Return access to the absolute tolerance field.
Definition: ODESolverI.H:37
Foam::ODESolver::resizeField
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:50
Foam::ODESolver::stepState::first
bool first
Definition: ODESolver.H:113
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ODESolver::ODESolver
ODESolver(const ODESolver &)=delete
No copy construct.
Foam::ODESolver::TypeName
TypeName("ODESolver")
Runtime type information.
Foam::ODESolver::absTol_
scalarField absTol_
Absolute convergence tolerance per step.
Definition: ODESolver.H:73
Foam::ODESolver::n_
label n_
Size of the ODESystem (adjustable)
Definition: ODESolver.H:70
Foam::ODESolver::stepState::last
bool last
Definition: ODESolver.H:114
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::SquareMatrix< scalar >
Foam::ODESolver::resizeMatrix
void resizeMatrix(scalarSquareMatrix &m) const
Definition: ODESolverI.H:63
f
labelList f(nPoints)
Foam::ODESystem
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:49
Foam::ODESolver::solve
virtual void solve(scalar &x, scalarField &y, scalar &dxTry) const
Solve the ODE system as far as possible up to dxTry.
Definition: ODESolver.C:117
Foam::UList< Type >
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::ODESolver::relTol_
scalarField relTol_
Relative convergence tolerance per step.
Definition: ODESolver.H:76
Foam::ODESolver::stepState::dxDid
scalar dxDid
Definition: ODESolver.H:112
Foam::ODESolver::resize
virtual bool resize()=0
Resize the ODE solver.
Definition: ODESolver.C:92
Foam::ODESolver::stepState::stepState
stepState(const scalar dx)
Definition: ODESolver.H:118
Foam::ODESolver::nEqns
label nEqns() const
Return the number of equations to solve.
Definition: ODESolverI.H:31
Foam::ODESolver::stepState::prevReject
bool prevReject
Definition: ODESolver.H:116
Foam::ODESolver::maxN_
const label maxN_
Maximum size of the ODESystem.
Definition: ODESolver.H:67
y
scalar y
Definition: LISASMDCalcMethod1.H:14
autoPtr.H