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  };
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 // ************************************************************************* //
label nEqns() const
Return the number of equations to solve.
Definition: ODESolverI.H:24
virtual void solve(scalar &x, scalarField &y, scalar &dxTry) const
Solve the ODE system as far as possible up to dxTry.
Definition: ODESolver.C:110
dictionary dict
scalarField & relTol()
Return access to the relative tolerance field.
Definition: ODESolverI.H:36
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:43
virtual bool resize()=0
Resize the ODE solver.
Definition: ODESolver.C:85
void operator=(const ODESolver &)=delete
No copy assignment.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
dimensionedScalar y0(const dimensionedScalar &ds)
An ODE solver for chemistry.
Definition: ode.H:48
stepState(const scalar dx)
Definition: ODESolver.H:134
scalar y
void resizeMatrix(scalarSquareMatrix &m) const
Definition: ODESolverI.H:56
const ODESystem & odes_
Reference to ODESystem.
Definition: ODESolver.H:62
TypeName("ODESolver")
Runtime type information.
label maxSteps_
The maximum number of sub-steps allowed for the integration step.
Definition: ODESolver.H:87
const label maxN_
Maximum size of the ODESystem.
Definition: ODESolver.H:67
labelList f(nPoints)
declareRunTimeSelectionTable(autoPtr, ODESolver, dictionary,(const ODESystem &ode, const dictionary &dict),(ode, dict))
virtual ~ODESolver()=default
Destructor.
scalarField relTol_
Relative convergence tolerance per step.
Definition: ODESolver.H:82
scalar normalizeError(const scalarField &y0, const scalarField &y, const scalarField &err) const
Return the nomalized scalar error.
Definition: ODESolver.C:36
Abstract base-class for ODE system solvers.
Definition: ODESolver.H:52
scalarField absTol_
Absolute convergence tolerance per step.
Definition: ODESolver.H:77
static autoPtr< ODESolver > New(const ODESystem &ode, const dictionary &dict)
Select null constructed.
Definition: ODESolverNew.C:27
scalarField & absTol()
Return access to the absolute tolerance field.
Definition: ODESolverI.H:30
label n
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:43
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
ODESolver(const ODESolver &)=delete
No copy construct.
label n_
Size of the ODESystem (adjustable)
Definition: ODESolver.H:72
Namespace for OpenFOAM.