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

KRR4.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 "KRR4.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::KRR4, 0);
00033 
00034 namespace Foam
00035 {
00036     addToRunTimeSelectionTable(ODESolver, KRR4, ODE);
00037 
00038 const scalar
00039     KRR4::safety = 0.9, KRR4::grow = 1.5, KRR4::pgrow = -0.25,
00040     KRR4::shrink = 0.5, KRR4::pshrink = (-1.0/3.0), KRR4::errcon = 0.1296;
00041 
00042 const scalar
00043     KRR4::gamma = 1.0/2.0,
00044     KRR4::a21 = 2.0, KRR4::a31 = 48.0/25.0, KRR4::a32 = 6.0/25.0,
00045     KRR4::c21 = -8.0, KRR4::c31 = 372.0/25.0, KRR4::c32 = 12.0/5.0,
00046     KRR4::c41 = -112.0/125.0, KRR4::c42 = -54.0/125.0, KRR4::c43 = -2.0/5.0,
00047     KRR4::b1 = 19.0/9.0, KRR4::b2 = 1.0/2.0, KRR4::b3 = 25.0/108.0,
00048     KRR4::b4 = 125.0/108.0,
00049     KRR4::e1 = 17.0/54.0, KRR4::e2 = 7.0/36.0, KRR4::e3 = 0.0,
00050     KRR4::e4 = 125.0/108.0,
00051     KRR4::c1X = 1.0/2.0, KRR4::c2X = -3.0/2.0, KRR4::c3X = 121.0/50.0,
00052     KRR4::c4X = 29.0/250.0,
00053     KRR4::a2X = 1.0, KRR4::a3X = 3.0/5.0;
00054 };
00055 
00056 
00057 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00058 
00059 Foam::KRR4::KRR4(const ODE& ode)
00060 :
00061     ODESolver(ode),
00062     yTemp_(n_),
00063     dydxTemp_(n_),
00064     g1_(n_),
00065     g2_(n_),
00066     g3_(n_),
00067     g4_(n_),
00068     yErr_(n_),
00069     dfdx_(n_),
00070     dfdy_(n_, n_),
00071     a_(n_, n_),
00072     pivotIndices_(n_)
00073 {}
00074 
00075 
00076 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00077 
00078 void Foam::KRR4::solve
00079 (
00080     const ODE& ode,
00081     scalar& x,
00082     scalarField& y,
00083     scalarField& dydx,
00084     const scalar eps,
00085     const scalarField& yScale,
00086     const scalar hTry,
00087     scalar& hDid,
00088     scalar& hNext
00089 ) const
00090 {
00091     scalar xTemp = x;
00092     yTemp_ = y;
00093     dydxTemp_ = dydx;
00094 
00095     ode.jacobian(xTemp, yTemp_, dfdx_, dfdy_);
00096 
00097     scalar h = hTry;
00098 
00099     for (register label jtry=0; jtry<maxtry; jtry++)
00100     {
00101         for (register label i=0; i<n_; i++)
00102         {
00103             for (register label j=0; j<n_; j++)
00104             {
00105                 a_[i][j] = -dfdy_[i][j];
00106             }
00107 
00108             a_[i][i] += 1.0/(gamma*h);
00109         }
00110 
00111         LUDecompose(a_, pivotIndices_);
00112 
00113         for (register label i=0; i<n_; i++)
00114         {
00115             g1_[i] = dydxTemp_[i] + h*c1X*dfdx_[i];
00116         }
00117 
00118         LUBacksubstitute(a_, pivotIndices_, g1_);
00119 
00120         for (register label i=0; i<n_; i++)
00121         {
00122             y[i] = yTemp_[i] + a21*g1_[i];
00123         }
00124 
00125         x = xTemp + a2X*h;
00126         ode.derivatives(x, y, dydx_);
00127 
00128         for (register label i=0; i<n_; i++)
00129         {
00130             g2_[i] = dydx_[i] + h*c2X*dfdx_[i] + c21*g1_[i]/h;
00131         }
00132 
00133         LUBacksubstitute(a_, pivotIndices_, g2_);
00134 
00135         for (register label i=0; i<n_; i++)
00136         {
00137             y[i] = yTemp_[i] + a31*g1_[i] + a32*g2_[i];
00138         }
00139 
00140         x = xTemp + a3X*h;
00141         ode.derivatives(x, y, dydx_);
00142 
00143         for (register label i=0; i<n_; i++)
00144         {
00145             g3_[i] = dydx[i] + h*c3X*dfdx_[i] + (c31*g1_[i] + c32*g2_[i])/h;
00146         }
00147 
00148         LUBacksubstitute(a_, pivotIndices_, g3_);
00149 
00150         for (register label i=0; i<n_; i++)
00151         {
00152             g4_[i] = dydx_[i] + h*c4X*dfdx_[i]
00153                 + (c41*g1_[i] + c42*g2_[i] + c43*g3_[i])/h;
00154         }
00155 
00156         LUBacksubstitute(a_, pivotIndices_, g4_);
00157 
00158         for (register label i=0; i<n_; i++)
00159         {
00160             y[i] = yTemp_[i] + b1*g1_[i] + b2*g2_[i] + b3*g3_[i] + b4*g4_[i];
00161             yErr_[i] = e1*g1_[i] + e2*g2_[i] + e3*g3_[i] + e4*g4_[i];
00162         }
00163 
00164         x = xTemp + h;
00165 
00166         if (x == xTemp)
00167         {
00168             FatalErrorIn("ODES::KRR4")
00169                 << "stepsize not significant"
00170                 << exit(FatalError);
00171         }
00172 
00173         scalar maxErr = 0.0;
00174         for (register label i=0; i<n_; i++)
00175         {
00176             maxErr = max(maxErr, mag(yErr_[i]/yScale[i]));
00177         }
00178         maxErr /= eps;
00179 
00180         if (maxErr <= 1.0)
00181         {
00182             hDid = h;
00183             hNext = (maxErr > errcon ? safety*h*pow(maxErr, pgrow) : grow*h);
00184             return;
00185         }
00186         else
00187         {
00188             hNext = safety*h*pow(maxErr, pshrink);
00189             h = (h >= 0.0 ? max(hNext, shrink*h) : min(hNext, shrink*h));
00190         }
00191     }
00192 
00193     FatalErrorIn("ODES::KRR4")
00194         << "exceeded maxtry"
00195         << exit(FatalError);
00196 }
00197 
00198 
00199 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd