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 
52 bool Foam::adaptiveSolver::resize(const label n)
53 {
54  ODESolver::resizeField(dydx0_, 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  scalar scale = safeScale_*pow(err, -alphaInc_);
104  dxTry = clamp(scale, minScale_, maxScale_)*dx;
105  }
106  else
107  {
108  dxTry = safeScale_*maxScale_*dx;
109  }
110 }
111 
112 
113 // ************************************************************************* //
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.
dictionary dict
Abstract base class for the systems of ordinary differential equations.
Definition: ODESystem.H:43
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
An ODE solver for chemistry.
Definition: ode.H:48
Macros for easy insertion into run-time selection tables.
adaptiveSolver(const ODESystem &ode, const dictionary &dict)
Construct from ODESystem.
scalar y
bool resize(const label n)
Resize the ODE solver.
SolverPerformance< Type > solve(faMatrix< Type > &, const dictionary &solverControls)
Solve returning the solution statistics given convergence tolerance.
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
label n
static void resizeField(UList< Type > &f, const label n)
Definition: ODESolverI.H:43
virtual void derivatives(const scalar x, const scalarField &y, scalarField &dydx) const =0
Calculate the derivatives in dydx.
dimensionSet clamp(const dimensionSet &a, const dimensionSet &range)
Definition: dimensionSet.C:271