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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "adaptiveSolver.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const ODESystem& ode,
36  const dictionary& dict
37 )
38 :
39  safeScale_(dict.lookupOrDefault<scalar>("safeScale", 0.9)),
40  alphaInc_(dict.lookupOrDefault<scalar>("alphaIncrease", 0.2)),
41  alphaDec_(dict.lookupOrDefault<scalar>("alphaDecrease", 0.25)),
42  minScale_(dict.lookupOrDefault<scalar>("minScale", 0.2)),
43  maxScale_(dict.lookupOrDefault<scalar>("maxScale", 10)),
44  dydx0_(ode.nEqns()),
45  yTemp_(ode.nEqns())
46 {}
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
52 {
53  ODESolver::resizeField(dydx0_, n);
54  ODESolver::resizeField(yTemp_, n);
55 
56  return true;
57 }
58 
59 
61 (
62  const ODESystem& odes,
63  scalar& x,
64  scalarField& y,
65  scalar& dxTry
66 ) const
67 {
68  scalar dx = dxTry;
69  scalar err = 0.0;
70 
71  odes.derivatives(x, y, dydx0_);
72 
73  // Loop over solver and adjust step-size as necessary
74  // to achieve desired error
75  do
76  {
77  // Solve step and provide error estimate
78  err = solve(x, y, dydx0_, dx, yTemp_);
79 
80  // If error is large reduce dx
81  if (err > 1)
82  {
83  scalar scale = max(safeScale_*pow(err, -alphaDec_), minScale_);
84  dx *= scale;
85 
86  if (dx < VSMALL)
87  {
89  << "stepsize underflow"
90  << exit(FatalError);
91  }
92  }
93  } while (err > 1);
94 
95  // Update the state
96  x += dx;
97  y = yTemp_;
98 
99  // If the error is small increase the step-size
100  if (err > pow(maxScale_/safeScale_, -1.0/alphaInc_))
101  {
102  dxTry =
103  min(max(safeScale_*pow(err, -alphaInc_), minScale_), maxScale_)*dx;
104  }
105  else
106  {
107  dxTry = safeScale_*maxScale_*dx;
108  }
109 }
110 
111 
112 // ************************************************************************* //
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::adaptiveSolver::solve
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.
n
label n
Definition: TABSMDCalcMethod2.H:31
adaptiveSolver.H
Foam::label
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:62
Foam::Field< scalar >
Foam::solve
SolverPerformance< Type > solve(faMatrix< Type > &, Istream &)
Solve returning the solution statistics given convergence tolerance.
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::ode
An ODE solver for chemistry.
Definition: ode.H:52
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::ODESolver::resizeField
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:50
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::ODESystem::derivatives
virtual void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.
Foam::ODESystem
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:49
Foam::adaptiveSolver::resize
bool resize(const label n)
Resize the ODE solver.
Definition: adaptiveSolver.C:51
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::adaptiveSolver::adaptiveSolver
adaptiveSolver(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: adaptiveSolver.C:34
y
scalar y
Definition: LISASMDCalcMethod1.H:14