OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

SIBS.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "SIBS.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::SIBS, 0);
00033 
00034 namespace Foam
00035 {
00036     addToRunTimeSelectionTable(ODESolver, SIBS, ODE);
00037 
00038     const label SIBS::nSeq_[iMaxX_] = {2, 6, 10, 14, 22, 34, 50, 70};
00039 
00040     const scalar
00041         SIBS::safe1 = 0.25, SIBS::safe2 = 0.7,
00042         SIBS::redMax = 1.0e-5, SIBS::redMin = 0.0, SIBS::scaleMX = 0.1;
00043 };
00044 
00045 
00046 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00047 
00048 Foam::SIBS::SIBS(const ODE& ode)
00049 :
00050     ODESolver(ode),
00051     a_(iMaxX_),
00052     alpha_(kMaxX_, kMaxX_),
00053     d_p_(n_, kMaxX_),
00054     x_p_(kMaxX_),
00055     err_(kMaxX_),
00056 
00057     yTemp_(n_),
00058     ySeq_(n_),
00059     yErr_(n_),
00060     dfdx_(n_),
00061     dfdy_(n_, n_),
00062     first_(1),
00063     epsOld_(-1.0)
00064 {}
00065 
00066 
00067 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00068 
00069 void Foam::SIBS::solve
00070 (
00071     const ODE& ode,
00072     scalar& x,
00073     scalarField& y,
00074     scalarField& dydx,
00075     const scalar eps,
00076     const scalarField& yScale,
00077     const scalar hTry,
00078     scalar& hDid,
00079     scalar& hNext
00080 ) const
00081 {
00082     bool exitflag = false;
00083 
00084     if (eps != epsOld_)
00085     {
00086         hNext = xNew_ = -GREAT;
00087         scalar eps1 = safe1*eps;
00088         a_[0] = nSeq_[0] + 1;
00089 
00090         for (register label k=0; k<kMaxX_; k++)
00091         {
00092             a_[k + 1] = a_[k] + nSeq_[k + 1];
00093         }
00094 
00095         for (register label iq = 1; iq<kMaxX_; iq++)
00096         {
00097             for (register label k=0; k<iq; k++)
00098             {
00099                 alpha_[k][iq] =
00100                     pow(eps1, (a_[k + 1] - a_[iq + 1])
00101                    /((a_[iq + 1] - a_[0] + 1.0)*(2*k + 3)));
00102             }
00103         }
00104 
00105         epsOld_ = eps;
00106         a_[0] += n_;
00107 
00108         for (register label k=0; k<kMaxX_; k++)
00109         {
00110             a_[k + 1] = a_[k] + nSeq_[k + 1];
00111         }
00112 
00113         for (kOpt_ = 1; kOpt_<kMaxX_ - 1; kOpt_++)
00114         {
00115             if (a_[kOpt_ + 1] > a_[kOpt_]*alpha_[kOpt_ - 1][kOpt_])
00116             {
00117                 break;
00118             }
00119         }
00120 
00121         kMax_ = kOpt_;
00122     }
00123 
00124     label k=0;
00125     scalar h = hTry;
00126     yTemp_ = y;
00127 
00128     ode.jacobian(x, y, dfdx_, dfdy_);
00129 
00130     if (x != xNew_ || h != hNext)
00131     {
00132         first_ = 1;
00133         kOpt_ = kMax_;
00134     }
00135 
00136     label km=0;
00137     label reduct=0;
00138     scalar maxErr = SMALL;
00139 
00140     for (;;)
00141     {
00142         scalar red = 1.0;
00143 
00144         for (k=0; k <= kMax_; k++)
00145         {
00146             xNew_ = x + h;
00147 
00148             if (xNew_ == x)
00149             {
00150                 FatalErrorIn("ODES::SIBS")
00151                     << "step size underflow"
00152                     << exit(FatalError);
00153             }
00154 
00155             SIMPR(ode, x, yTemp_, dydx, dfdx_, dfdy_, h, nSeq_[k], ySeq_);
00156             scalar xest = sqr(h/nSeq_[k]);
00157 
00158             polyExtrapolate(k, xest, ySeq_, y, yErr_, x_p_, d_p_);
00159 
00160             if (k != 0)
00161             {
00162                 maxErr = SMALL;
00163                 for (register label i=0; i<n_; i++)
00164                 {
00165                     maxErr = max(maxErr, mag(yErr_[i]/yScale[i]));
00166                 }
00167                 maxErr /= eps;
00168                 km = k - 1;
00169                 err_[km] = pow(maxErr/safe1, 1.0/(2*km + 3));
00170             }
00171 
00172             if (k != 0 && (k >= kOpt_ - 1 || first_))
00173             {
00174                 if (maxErr < 1.0)
00175                 {
00176                     exitflag = true;
00177                     break;
00178                 }
00179 
00180                 if (k == kMax_ || k == kOpt_ + 1)
00181                 {
00182                     red = safe2/err_[km];
00183                     break;
00184                 }
00185                 else if (k == kOpt_ && alpha_[kOpt_ - 1][kOpt_] < err_[km])
00186                 {
00187                     red = 1.0/err_[km];
00188                     break;
00189                 }
00190                 else if (kOpt_ == kMax_ && alpha_[km][kMax_ - 1] < err_[km])
00191                 {
00192                     red = alpha_[km][kMax_ - 1]*safe2/err_[km];
00193                     break;
00194                 }
00195                 else if (alpha_[km][kOpt_] < err_[km])
00196                 {
00197                     red = alpha_[km][kOpt_ - 1]/err_[km];
00198                     break;
00199                 }
00200             }
00201         }
00202 
00203         if (exitflag)
00204         {
00205             break;
00206         }
00207 
00208         red = min(red, redMin);
00209         red = max(red, redMax);
00210         h *= red;
00211         reduct = 1;
00212     }
00213 
00214     x = xNew_;
00215     hDid = h;
00216     first_=0;
00217     scalar wrkmin = GREAT;
00218     scalar scale = 1.0;
00219 
00220     for (register label kk=0; kk<=km; kk++)
00221     {
00222         scalar fact = max(err_[kk], scaleMX);
00223         scalar work = fact*a_[kk + 1];
00224         if (work < wrkmin)
00225         {
00226             scale = fact;
00227             wrkmin = work;
00228             kOpt_ = kk + 1;
00229         }
00230     }
00231 
00232     hNext = h/scale;
00233 
00234     if (kOpt_ >= k && kOpt_ != kMax_ && !reduct)
00235     {
00236         scalar fact = max(scale/alpha_[kOpt_ - 1][kOpt_], scaleMX);
00237         if (a_[kOpt_ + 1]*fact <= wrkmin)
00238         {
00239             hNext = h/fact;
00240             kOpt_++;
00241         }
00242     }
00243 }
00244 
00245 
00246 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd