All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
adaptiveSolver.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) 2011-2019 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 "adaptiveSolver.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
32 (
33  const ODESystem& ode,
34  const dictionary& dict
35 )
36 :
37  safeScale_(dict.lookupOrDefault<scalar>("safeScale", 0.9)),
38  alphaInc_(dict.lookupOrDefault<scalar>("alphaIncrease", 0.2)),
39  alphaDec_(dict.lookupOrDefault<scalar>("alphaDecrease", 0.25)),
40  minScale_(dict.lookupOrDefault<scalar>("minScale", 0.2)),
41  maxScale_(dict.lookupOrDefault<scalar>("maxScale", 10)),
42  dydx0_(ode.nEqns()),
43  yTemp_(ode.nEqns())
44 {}
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
50 {
51  ODESolver::resizeField(dydx0_, n);
52  ODESolver::resizeField(yTemp_, n);
53 
54  return true;
55 }
56 
57 
59 (
60  const ODESystem& odes,
61  scalar& x,
62  scalarField& y,
63  const label li,
64  scalar& dxTry
65 ) const
66 {
67  scalar dx = dxTry;
68  scalar err = 0.0;
69 
70  odes.derivatives(x, y, li, dydx0_);
71 
72  // Loop over solver and adjust step-size as necessary
73  // to achieve desired error
74  do
75  {
76  // Solve step and provide error estimate
77  err = solve(x, y, li, dydx0_, dx, yTemp_);
78 
79  // If error is large reduce dx
80  if (err > 1)
81  {
82  scalar scale = max(safeScale_*pow(err, -alphaDec_), minScale_);
83  dx *= scale;
84 
85  if (dx < vSmall)
86  {
88  << "stepsize underflow"
89  << exit(FatalError);
90  }
91  }
92  } while (err > 1);
93 
94  // Update the state
95  x += dx;
96  y = yTemp_;
97 
98  // If the error is small increase the step-size
99  if (err > pow(maxScale_/safeScale_, -1.0/alphaInc_))
100  {
101  dxTry =
102  min(max(safeScale_*pow(err, -alphaInc_), minScale_), maxScale_)*dx;
103  }
104  else
105  {
106  dxTry = safeScale_*maxScale_*dx;
107  }
108 }
109 
110 
111 // ************************************************************************* //
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
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:46
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual void derivatives(const scalar x, const scalarField &y, const label li, scalarField &dydx) const =0
Calculate the derivatives in dydx.
Macros for easy insertion into run-time selection tables.
adaptiveSolver(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
bool resize(const label n)
Resize the ODE solver.
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
label n
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:48
virtual label nEqns() const =0
Return the number of equations in the system.
virtual scalar solve(const scalar x0, const scalarField &y0, const label li, const scalarField &dydx0, const scalar dx, scalarField &y) const =0
Solve a single step dx and return the error.