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

PBiCG.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 "PBiCG.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033     defineTypeNameAndDebug(PBiCG, 0);
00034 
00035     lduMatrix::solver::addasymMatrixConstructorToTable<PBiCG>
00036         addPBiCGAsymMatrixConstructorToTable_;
00037 }
00038 
00039 
00040 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00041 
00042 Foam::PBiCG::PBiCG
00043 (
00044     const word& fieldName,
00045     const lduMatrix& matrix,
00046     const FieldField<Field, scalar>& interfaceBouCoeffs,
00047     const FieldField<Field, scalar>& interfaceIntCoeffs,
00048     const lduInterfaceFieldPtrsList& interfaces,
00049     const dictionary& solverControls
00050 )
00051 :
00052     lduMatrix::solver
00053     (
00054         fieldName,
00055         matrix,
00056         interfaceBouCoeffs,
00057         interfaceIntCoeffs,
00058         interfaces,
00059         solverControls
00060     )
00061 {}
00062 
00063 
00064 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00065 
00066 Foam::lduMatrix::solverPerformance Foam::PBiCG::solve
00067 (
00068     scalarField& psi,
00069     const scalarField& source,
00070     const direction cmpt
00071 ) const
00072 {
00073     // --- Setup class containing solver performance data
00074     lduMatrix::solverPerformance solverPerf
00075     (
00076         lduMatrix::preconditioner::getName(controlDict_) + typeName,
00077         fieldName_
00078     );
00079 
00080     register label nCells = psi.size();
00081 
00082     scalar* __restrict__ psiPtr = psi.begin();
00083 
00084     scalarField pA(nCells);
00085     scalar* __restrict__ pAPtr = pA.begin();
00086 
00087     scalarField pT(nCells, 0.0);
00088     scalar* __restrict__ pTPtr = pT.begin();
00089 
00090     scalarField wA(nCells);
00091     scalar* __restrict__ wAPtr = wA.begin();
00092 
00093     scalarField wT(nCells);
00094     scalar* __restrict__ wTPtr = wT.begin();
00095 
00096     scalar wArT = matrix_.great_;
00097     scalar wArTold = wArT;
00098 
00099     // --- Calculate A.psi and T.psi
00100     matrix_.Amul(wA, psi, interfaceBouCoeffs_, interfaces_, cmpt);
00101     matrix_.Tmul(wT, psi, interfaceIntCoeffs_, interfaces_, cmpt);
00102 
00103     // --- Calculate initial residual and transpose residual fields
00104     scalarField rA(source - wA);
00105     scalarField rT(source - wT);
00106     scalar* __restrict__ rAPtr = rA.begin();
00107     scalar* __restrict__ rTPtr = rT.begin();
00108 
00109     // --- Calculate normalisation factor
00110     scalar normFactor = this->normFactor(psi, source, wA, pA);
00111 
00112     if (lduMatrix::debug >= 2)
00113     {
00114         Info<< "   Normalisation factor = " << normFactor << endl;
00115     }
00116 
00117     // --- Calculate normalised residual norm
00118     solverPerf.initialResidual() = gSumMag(rA)/normFactor;
00119     solverPerf.finalResidual() = solverPerf.initialResidual();
00120 
00121     // --- Check convergence, solve if not converged
00122     if (!solverPerf.checkConvergence(tolerance_, relTol_))
00123     {
00124         // --- Select and construct the preconditioner
00125         autoPtr<lduMatrix::preconditioner> preconPtr =
00126         lduMatrix::preconditioner::New
00127         (
00128             *this,
00129             controlDict_
00130         );
00131 
00132         // --- Solver iteration
00133         do
00134         {
00135             // --- Store previous wArT
00136             wArTold = wArT;
00137 
00138             // --- Precondition residuals
00139             preconPtr->precondition(wA, rA, cmpt);
00140             preconPtr->preconditionT(wT, rT, cmpt);
00141 
00142             // --- Update search directions:
00143             wArT = gSumProd(wA, rT);
00144 
00145             if (solverPerf.nIterations() == 0)
00146             {
00147                 for (register label cell=0; cell<nCells; cell++)
00148                 {
00149                     pAPtr[cell] = wAPtr[cell];
00150                     pTPtr[cell] = wTPtr[cell];
00151                 }
00152             }
00153             else
00154             {
00155                 scalar beta = wArT/wArTold;
00156 
00157                 for (register label cell=0; cell<nCells; cell++)
00158                 {
00159                     pAPtr[cell] = wAPtr[cell] + beta*pAPtr[cell];
00160                     pTPtr[cell] = wTPtr[cell] + beta*pTPtr[cell];
00161                 }
00162             }
00163 
00164 
00165             // --- Update preconditioned residuals
00166             matrix_.Amul(wA, pA, interfaceBouCoeffs_, interfaces_, cmpt);
00167             matrix_.Tmul(wT, pT, interfaceIntCoeffs_, interfaces_, cmpt);
00168 
00169             scalar wApT = gSumProd(wA, pT);
00170 
00171 
00172             // --- Test for singularity
00173             if (solverPerf.checkSingularity(mag(wApT)/normFactor)) break;
00174 
00175 
00176             // --- Update solution and residual:
00177 
00178             scalar alpha = wArT/wApT;
00179 
00180             for (register label cell=0; cell<nCells; cell++)
00181             {
00182                 psiPtr[cell] += alpha*pAPtr[cell];
00183                 rAPtr[cell] -= alpha*wAPtr[cell];
00184                 rTPtr[cell] -= alpha*wTPtr[cell];
00185             }
00186 
00187             solverPerf.finalResidual() = gSumMag(rA)/normFactor;
00188 
00189         } while
00190         (
00191             solverPerf.nIterations()++ < maxIter_
00192         && !(solverPerf.checkConvergence(tolerance_, relTol_))
00193         );
00194     }
00195 
00196     return solverPerf;
00197 }
00198 
00199 
00200 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd