adaptiveSolver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 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  const ODESystem& odes,
52  scalar& x,
53  scalarField& y,
54  scalar& dxTry
55 ) const
56 {
57  scalar dx = dxTry;
58  scalar err = 0.0;
59 
60  odes.derivatives(x, y, dydx0_);
61 
62  // Loop over solver and adjust step-size as necessary
63  // to achieve desired error
64  do
65  {
66  // Solve step and provide error estimate
67  err = solve(x, y, dydx0_, dx, yTemp_);
68 
69  // If error is large reduce dx
70  if (err > 1)
71  {
72  scalar scale = max(safeScale_*pow(err, -alphaDec_), minScale_);
73  dx *= scale;
74 
75  if (dx < VSMALL)
76  {
78  << "stepsize underflow"
79  << exit(FatalError);
80  }
81  }
82  } while (err > 1);
83 
84  // Update the state
85  x += dx;
86  y = yTemp_;
87 
88  // If the error is small increase the step-size
89  if (err > pow(maxScale_/safeScale_, -1.0/alphaInc_))
90  {
91  dxTry =
92  min(max(safeScale_*pow(err, -alphaInc_), minScale_), maxScale_)*dx;
93  }
94  else
95  {
96  dxTry = safeScale_*maxScale_*dx;
97  }
98 }
99 
100 
101 // ************************************************************************* //
virtual scalar solve(const scalar x0, const scalarField &y0, const scalarField &dydx0, const scalar dx, scalarField &y) const =0
Solve a single step dx and return the error.
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:137
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Macros for easy insertion into run-time selection tables.
adaptiveSolver(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
rhoEqn solve()
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
virtual label nEqns() const =0
Return the number of equations in the system.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
virtual void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.