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 Foam_ODESolver_H
42 #define Foam_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 protected:
60 
61  // Protected Data
62 
63  //- Reference to ODESystem
64  const ODESystem& odes_;
65 
66  //- Maximum size of the ODESystem
67  const label maxN_;
68 
69  //- Size of the ODESystem (adjustable)
70  mutable label n_;
71 
72  //- Absolute convergence tolerance per step
74 
75  //- Relative convergence tolerance per step
77 
78  //- The maximum number of sub-steps allowed for the integration step
79  label maxSteps_;
80 
81 
82  // Protected Member Functions
83 
84  //- Return the nomalized scalar error
85  scalar normalizeError
86  (
87  const scalarField& y0,
88  const scalarField& y,
89  const scalarField& err
90  ) const;
91 
92  //- No copy construct
93  ODESolver(const ODESolver&) = delete;
94 
95  //- No copy assignment
96  void operator=(const ODESolver&) = delete;
97 
98 
99 public:
100 
101  friend class ODESystem;
102 
103  //- Runtime type information
104  TypeName("ODESolver");
105 
106  class stepState
107  {
108  public:
109 
110  const bool forward;
111  scalar dxTry;
112  scalar dxDid;
113  bool first;
114  bool last;
115  bool reject;
116  bool prevReject;
117 
118  stepState(const scalar dx)
119  :
120  forward(dx > 0 ? true : false),
121  dxTry(dx),
122  dxDid(0),
123  first(true),
124  last(false),
125  reject(false),
126  prevReject(false)
127  {}
128  };
131  // Declare run-time constructor selection table
132 
134  (
135  autoPtr,
136  ODESolver,
137  dictionary,
138  (const ODESystem& ode, const dictionary& dict),
139  (ode, dict)
140  );
141 
142 
143  // Constructors
144 
145  //- Construct for given ODESystem
146  ODESolver(const ODESystem& ode, const dictionary& dict);
147 
148  //- Construct for given ODESystem specifying tolerances
149  ODESolver
150  (
151  const ODESystem& ode,
152  const scalarField& absTol,
153  const scalarField& relTol
154  );
155 
156 
157  // Selectors
158 
159  //- Select null constructed
160  static autoPtr<ODESolver> New
161  (
162  const ODESystem& ode,
163  const dictionary& dict
164  );
165 
166 
167  //- Destructor
168  virtual ~ODESolver() = default;
169 
170 
171  // Member Functions
172 
173  //- The number of equations to solve
174  label nEqns() const noexcept { return n_; }
175 
176  //- Access to the absolute tolerance field
177  scalarField& absTol() noexcept { return absTol_; }
178 
179  //- Access to the relative tolerance field
180  scalarField& relTol() noexcept { return relTol_; }
181 
182  //- Resize the ODE solver
183  virtual bool resize() = 0;
184 
185  template<class Type>
186  static inline void resizeField(UList<Type>& f, const label n);
187 
188  template<class Type>
189  inline void resizeField(UList<Type>& f) const;
190 
191  inline void resizeMatrix(scalarSquareMatrix& m) const;
192 
193  //- Solve the ODE system as far as possible up to dxTry
194  // adjusting the step as necessary to provide a solution within
195  // the specified tolerance.
196  // Update the state and return an estimate for the next step in dxTry
197  virtual void solve
198  (
199  scalar& x,
200  scalarField& y,
201  scalar& dxTry
202  ) const;
203 
204  //- Solve the ODE system as far as possible up to dxTry
205  // adjusting the step as necessary to provide a solution within
206  // the specified tolerance.
207  // Update the state and return an estimate for the next step in dxTry
208  virtual void solve
209  (
210  scalar& x,
211  scalarField& y,
212  stepState& step
213  ) const;
214 
215  //- Solve the ODE system from xStart to xEnd, update the state
216  // and return an estimate for the next step in dxTry
217  virtual void solve
218  (
219  const scalar xStart,
220  const scalar xEnd,
221  scalarField& y,
222  scalar& dxEst
223  ) const;
224 };
225 
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 } // End namespace Foam
230 
231 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 
233 #include "ODESolverI.H"
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 #endif
238 
239 // ************************************************************************* //
label nEqns() const noexcept
The number of equations to solve.
Definition: ODESolver.H:199
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
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:133
scalar y
void resizeMatrix(scalarSquareMatrix &m) const
Definition: ODESolverI.H:39
const ODESystem & odes_
Reference to ODESystem.
Definition: ODESolver.H:61
TypeName("ODESolver")
Runtime type information.
label maxSteps_
The maximum number of sub-steps allowed for the integration step.
Definition: ODESolver.H:86
const label maxN_
Maximum size of the ODESystem.
Definition: ODESolver.H:66
const direction noexcept
Definition: Scalar.H:258
labelList f(nPoints)
declareRunTimeSelectionTable(autoPtr, ODESolver, dictionary,(const ODESystem &ode, const dictionary &dict),(ode, dict))
virtual ~ODESolver()=default
Destructor.
Basic run-time type information using word as the type&#39;s name. Used to enhance the standard RTTI to c...
scalarField & absTol() noexcept
Access to the absolute tolerance field.
Definition: ODESolver.H:204
scalarField relTol_
Relative convergence tolerance per step.
Definition: ODESolver.H:81
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:76
static autoPtr< ODESolver > New(const ODESystem &ode, const dictionary &dict)
Select null constructed.
Definition: ODESolverNew.C:27
scalarField & relTol() noexcept
Access to the relative tolerance field.
Definition: ODESolver.H:209
label n
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:24
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:71
SquareMatrix< scalar > scalarSquareMatrix
Namespace for OpenFOAM.