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-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 \*---------------------------------------------------------------------------*/
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  scalar& dxTry
64 ) const
65 {
66  scalar dx = dxTry;
67  scalar err = 0.0;
68 
69  odes.derivatives(x, y, dydx0_);
70 
71  // Loop over solver and adjust step-size as necessary
72  // to achieve desired error
73  do
74  {
75  // Solve step and provide error estimate
76  err = solve(x, y, dydx0_, dx, yTemp_);
77 
78  // If error is large reduce dx
79  if (err > 1)
80  {
81  scalar scale = max(safeScale_*pow(err, -alphaDec_), minScale_);
82  dx *= scale;
83 
84  if (dx < vSmall)
85  {
87  << "stepsize underflow"
88  << exit(FatalError);
89  }
90  }
91  } while (err > 1);
92 
93  // Update the state
94  x += dx;
95  y = yTemp_;
96 
97  // If the error is small increase the step-size
98  if (err > pow(maxScale_/safeScale_, -1.0/alphaInc_))
99  {
100  dxTry =
101  min(max(safeScale_*pow(err, -alphaInc_), minScale_), maxScale_)*dx;
102  }
103  else
104  {
105  dxTry = safeScale_*maxScale_*dx;
106  }
107 }
108 
109 
110 // ************************************************************************* //
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.
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: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.
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 void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.