1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 Bisection root Based on Numerical Recipes in C++, Section 9.1, page 358.
30 Function is provided as a template parameter function object, evaluated
31 using operator()(const scalar x)
34 Hrvoje Jasak, Wikki Ltd. All rights reserved.
36 \*----------------------------------------------------------------------------*/
38 #include "BisectionRoot.H"
40 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
43 const Foam::label Foam::BisectionRoot<Func>::maxIter = 60;
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 Foam::BisectionRoot<Func>::BisectionRoot(const Func& f, const scalar eps)
55 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
58 Foam::scalar Foam::BisectionRoot<Func>::root
64 scalar f, fMid, dx, rtb, xMid;
73 "Foam::scalar Foam::BisectionRoot<Func>::root\n"
78 ) << "Root is not bracketed. f(x0) = " << f << " f(x1) = " << fMid
82 // Orient the search such that f > 0 lies at x + dx
94 for (label nIter = 0; nIter < maxIter; nIter++)
106 if (mag(dx) < eps_ || mag(fMid) < SMALL)
114 "Foam::scalar Foam::BisectionRoot<Func>::root\n"
116 " const scalar x0,\n"
119 ) << "Maximum number of iterations exceeded" << abort(FatalError);
121 // Dummy return to keep compiler happy
126 // ************************************************************************* //