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