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

smoothSolver.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 "smoothSolver.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033     defineTypeNameAndDebug(smoothSolver, 0);
00034 
00035     lduMatrix::solver::addsymMatrixConstructorToTable<smoothSolver>
00036         addsmoothSolverSymMatrixConstructorToTable_;
00037 
00038     lduMatrix::solver::addasymMatrixConstructorToTable<smoothSolver>
00039         addsmoothSolverAsymMatrixConstructorToTable_;
00040 }
00041 
00042 
00043 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00044 
00045 Foam::smoothSolver::smoothSolver
00046 (
00047     const word& fieldName,
00048     const lduMatrix& matrix,
00049     const FieldField<Field, scalar>& interfaceBouCoeffs,
00050     const FieldField<Field, scalar>& interfaceIntCoeffs,
00051     const lduInterfaceFieldPtrsList& interfaces,
00052     const dictionary& solverControls
00053 )
00054 :
00055     lduMatrix::solver
00056     (
00057         fieldName,
00058         matrix,
00059         interfaceBouCoeffs,
00060         interfaceIntCoeffs,
00061         interfaces,
00062         solverControls
00063     )
00064 {
00065     readControls();
00066 }
00067 
00068 
00069 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00070 
00071 void Foam::smoothSolver::readControls()
00072 {
00073     lduMatrix::solver::readControls();
00074     nSweeps_ = controlDict_.lookupOrDefault<label>("nSweeps", 1);
00075 }
00076 
00077 
00078 Foam::lduMatrix::solverPerformance Foam::smoothSolver::solve
00079 (
00080     scalarField& psi,
00081     const scalarField& source,
00082     const direction cmpt
00083 ) const
00084 {
00085     // Setup class containing solver performance data
00086     lduMatrix::solverPerformance solverPerf(typeName, fieldName_);
00087 
00088     // If the nSweeps_ is negative do a fixed number of sweeps
00089     if (nSweeps_ < 0)
00090     {
00091         autoPtr<lduMatrix::smoother> smootherPtr = lduMatrix::smoother::New
00092         (
00093             fieldName_,
00094             matrix_,
00095             interfaceBouCoeffs_,
00096             interfaceIntCoeffs_,
00097             interfaces_,
00098             controlDict_
00099         );
00100 
00101         smootherPtr->smooth
00102         (
00103             psi,
00104             source,
00105             cmpt,
00106             -nSweeps_
00107         );
00108 
00109         solverPerf.nIterations() -= nSweeps_;
00110     }
00111     else
00112     {
00113         scalar normFactor = 0;
00114 
00115         {
00116             scalarField Apsi(psi.size());
00117             scalarField temp(psi.size());
00118 
00119             // Calculate A.psi
00120             matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
00121 
00122             // Calculate normalisation factor
00123             normFactor = this->normFactor(psi, source, Apsi, temp);
00124 
00125             // Calculate residual magnitude
00126             solverPerf.initialResidual() = gSumMag(source - Apsi)/normFactor;
00127             solverPerf.finalResidual() = solverPerf.initialResidual();
00128         }
00129 
00130         if (lduMatrix::debug >= 2)
00131         {
00132             Info<< "   Normalisation factor = " << normFactor << endl;
00133         }
00134 
00135 
00136         // Check convergence, solve if not converged
00137         if (!solverPerf.checkConvergence(tolerance_, relTol_))
00138         {
00139             autoPtr<lduMatrix::smoother> smootherPtr = lduMatrix::smoother::New
00140             (
00141                 fieldName_,
00142                 matrix_,
00143                 interfaceBouCoeffs_,
00144                 interfaceIntCoeffs_,
00145                 interfaces_,
00146                 controlDict_
00147             );
00148 
00149             // Smoothing loop
00150             do
00151             {
00152                 smootherPtr->smooth
00153                 (
00154                     psi,
00155                     source,
00156                     cmpt,
00157                     nSweeps_
00158                 );
00159 
00160                 // Calculate the residual to check convergence
00161                 solverPerf.finalResidual() = gSumMag
00162                 (
00163                     matrix_.residual
00164                     (
00165                         psi,
00166                         source,
00167                         interfaceBouCoeffs_,
00168                         interfaces_,
00169                         cmpt
00170                     )
00171                 )/normFactor;
00172             } while
00173             (
00174                 (solverPerf.nIterations() += nSweeps_) < maxIter_
00175              && !(solverPerf.checkConvergence(tolerance_, relTol_))
00176             );
00177         }
00178     }
00179 
00180     return solverPerf;
00181 }
00182 
00183 
00184 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd