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  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "adaptiveSolver.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 (
36  const ODESystem& ode,
37  const dictionary& dict
38 )
39 :
40  safeScale_(dict.getOrDefault<scalar>("safeScale", 0.9)),
41  alphaInc_(dict.getOrDefault<scalar>("alphaIncrease", 0.2)),
42  alphaDec_(dict.getOrDefault<scalar>("alphaDecrease", 0.25)),
43  minScale_(dict.getOrDefault<scalar>("minScale", 0.2)),
44  maxScale_(dict.getOrDefault<scalar>("maxScale", 10)),
45  dydx0_(ode.nEqns()),
46  yTemp_(ode.nEqns())
47 {}
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
53 {
54  ODESolver::resizeField(dydx0_, n);
55  ODESolver::resizeField(yTemp_, n);
56 
57  return true;
58 }
59 
60 
62 (
63  const ODESystem& odes,
64  scalar& x,
65  scalarField& y,
66  scalar& dxTry
67 ) const
68 {
69  scalar dx = dxTry;
70  scalar err = 0.0;
71 
72  odes.derivatives(x, y, dydx0_);
73 
74  // Loop over solver and adjust step-size as necessary
75  // to achieve desired error
76  do
77  {
78  // Solve step and provide error estimate
79  err = solve(x, y, dydx0_, dx, yTemp_);
80 
81  // If error is large reduce dx
82  if (err > 1)
83  {
84  scalar scale = max(safeScale_*pow(err, -alphaDec_), minScale_);
85  dx *= scale;
86 
87  if (dx < VSMALL)
88  {
90  << "stepsize underflow"
91  << exit(FatalError);
92  }
93  }
94  } while (err > 1);
95 
96  // Update the state
97  x += dx;
98  y = yTemp_;
99 
100  // If the error is small increase the step-size
101  if (err > pow(maxScale_/safeScale_, -1.0/alphaInc_))
102  {
103  dxTry =
104  min(max(safeScale_*pow(err, -alphaInc_), minScale_), maxScale_)*dx;
105  }
106  else
107  {
108  dxTry = safeScale_*maxScale_*dx;
109  }
110 }
111 
112 
113 // ************************************************************************* //
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::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:123
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:453
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:52
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::adaptiveSolver::adaptiveSolver
adaptiveSolver(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
Definition: adaptiveSolver.C:35
y
scalar y
Definition: LISASMDCalcMethod1.H:14