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

PCG.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 "PCG.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033     defineTypeNameAndDebug(PCG, 0);
00034 
00035     lduMatrix::solver::addsymMatrixConstructorToTable<PCG>
00036         addPCGSymMatrixConstructorToTable_;
00037 }
00038 
00039 
00040 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00041 
00042 Foam::PCG::PCG
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::PCG::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 wA(nCells);
00088     scalar* __restrict__ wAPtr = wA.begin();
00089 
00090     scalar wArA = matrix_.great_;
00091     scalar wArAold = wArA;
00092 
00093     // --- Calculate A.psi
00094     matrix_.Amul(wA, psi, interfaceBouCoeffs_, interfaces_, cmpt);
00095 
00096     // --- Calculate initial residual field
00097     scalarField rA(source - wA);
00098     scalar* __restrict__ rAPtr = rA.begin();
00099 
00100     // --- Calculate normalisation factor
00101     scalar normFactor = this->normFactor(psi, source, wA, pA);
00102 
00103     if (lduMatrix::debug >= 2)
00104     {
00105         Info<< "   Normalisation factor = " << normFactor << endl;
00106     }
00107 
00108     // --- Calculate normalised residual norm
00109     solverPerf.initialResidual() = gSumMag(rA)/normFactor;
00110     solverPerf.finalResidual() = solverPerf.initialResidual();
00111 
00112     // --- Check convergence, solve if not converged
00113     if (!solverPerf.checkConvergence(tolerance_, relTol_))
00114     {
00115         // --- Select and construct the preconditioner
00116         autoPtr<lduMatrix::preconditioner> preconPtr =
00117         lduMatrix::preconditioner::New
00118         (
00119             *this,
00120             controlDict_
00121         );
00122 
00123         // --- Solver iteration
00124         do
00125         {
00126             // --- Store previous wArA
00127             wArAold = wArA;
00128 
00129             // --- Precondition residual
00130             preconPtr->precondition(wA, rA, cmpt);
00131 
00132             // --- Update search directions:
00133             wArA = gSumProd(wA, rA);
00134 
00135             if (solverPerf.nIterations() == 0)
00136             {
00137                 for (register label cell=0; cell<nCells; cell++)
00138                 {
00139                     pAPtr[cell] = wAPtr[cell];
00140                 }
00141             }
00142             else
00143             {
00144                 scalar beta = wArA/wArAold;
00145 
00146                 for (register label cell=0; cell<nCells; cell++)
00147                 {
00148                     pAPtr[cell] = wAPtr[cell] + beta*pAPtr[cell];
00149                 }
00150             }
00151 
00152 
00153             // --- Update preconditioned residual
00154             matrix_.Amul(wA, pA, interfaceBouCoeffs_, interfaces_, cmpt);
00155 
00156             scalar wApA = gSumProd(wA, pA);
00157 
00158 
00159             // --- Test for singularity
00160             if (solverPerf.checkSingularity(mag(wApA)/normFactor)) break;
00161 
00162 
00163             // --- Update solution and residual:
00164 
00165             scalar alpha = wArA/wApA;
00166 
00167             for (register label cell=0; cell<nCells; cell++)
00168             {
00169                 psiPtr[cell] += alpha*pAPtr[cell];
00170                 rAPtr[cell] -= alpha*wAPtr[cell];
00171             }
00172 
00173             solverPerf.finalResidual() = gSumMag(rA)/normFactor;
00174 
00175         } while
00176         (
00177             solverPerf.nIterations()++ < maxIter_
00178         && !(solverPerf.checkConvergence(tolerance_, relTol_))
00179         );
00180     }
00181 
00182     return solverPerf;
00183 }
00184 
00185 
00186 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd