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

GAMGSolverSolve.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 "GAMGSolver.H"
00028 #include "ICCG.H"
00029 #include "BICCG.H"
00030 #include "SubField.H"
00031 
00032 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00033 
00034 Foam::lduMatrix::solverPerformance Foam::GAMGSolver::solve
00035 (
00036     scalarField& psi,
00037     const scalarField& source,
00038     const direction cmpt
00039 ) const
00040 {
00041     // Setup class containing solver performance data
00042     lduMatrix::solverPerformance solverPerf(typeName, fieldName_);
00043 
00044     // Calculate A.psi used to calculate the initial residual
00045     scalarField Apsi(psi.size());
00046     matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
00047 
00048     // Create the storage for the finestCorrection which may be used as a
00049     // temporary in normFactor
00050     scalarField finestCorrection(psi.size());
00051 
00052     // Calculate normalisation factor
00053     scalar normFactor = this->normFactor(psi, source, Apsi, finestCorrection);
00054 
00055     if (debug >= 2)
00056     {
00057         Pout<< "   Normalisation factor = " << normFactor << endl;
00058     }
00059 
00060     // Calculate initial finest-grid residual field
00061     scalarField finestResidual(source - Apsi);
00062 
00063     // Calculate normalised residual for convergence test
00064     solverPerf.initialResidual() = gSumMag(finestResidual)/normFactor;
00065     solverPerf.finalResidual() = solverPerf.initialResidual();
00066 
00067 
00068     // Check convergence, solve if not converged
00069     if (!solverPerf.checkConvergence(tolerance_, relTol_))
00070     {
00071         // Create coarse grid correction fields
00072         PtrList<scalarField> coarseCorrFields;
00073 
00074         // Create coarse grid sources
00075         PtrList<scalarField> coarseSources;
00076 
00077         // Create the smoothers for all levels
00078         PtrList<lduMatrix::smoother> smoothers;
00079 
00080         // Initialise the above data structures
00081         initVcycle(coarseCorrFields, coarseSources, smoothers);
00082 
00083         do
00084         {
00085             Vcycle
00086             (
00087                 smoothers,
00088                 psi,
00089                 source,
00090                 Apsi,
00091                 finestCorrection,
00092                 finestResidual,
00093                 coarseCorrFields,
00094                 coarseSources,
00095                 cmpt
00096             );
00097 
00098             // Calculate finest level residual field
00099             matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
00100             finestResidual = source;
00101             finestResidual -= Apsi;
00102 
00103             solverPerf.finalResidual() = gSumMag(finestResidual)/normFactor;
00104 
00105             if (debug >= 2)
00106             {
00107                 solverPerf.print();
00108             }
00109         } while
00110         (
00111             ++solverPerf.nIterations() < maxIter_
00112          && !(solverPerf.checkConvergence(tolerance_, relTol_))
00113         );
00114     }
00115 
00116     return solverPerf;
00117 }
00118 
00119 
00120 void Foam::GAMGSolver::Vcycle
00121 (
00122     const PtrList<lduMatrix::smoother>& smoothers,
00123     scalarField& psi,
00124     const scalarField& source,
00125     scalarField& Apsi,
00126     scalarField& finestCorrection,
00127     scalarField& finestResidual,
00128     PtrList<scalarField>& coarseCorrFields,
00129     PtrList<scalarField>& coarseSources,
00130     const direction cmpt
00131 ) const
00132 {
00133     //debug = 2;
00134 
00135     const label coarsestLevel = matrixLevels_.size() - 1;
00136 
00137     // Restrict finest grid residual for the next level up
00138     agglomeration_.restrictField(coarseSources[0], finestResidual, 0);
00139 
00140     if (debug >= 2 && nPreSweeps_)
00141     {
00142         Pout<< "Pre-smoothing scaling factors: ";
00143     }
00144 
00145 
00146     // Residual restriction (going to coarser levels)
00147     for (label leveli = 0; leveli < coarsestLevel; leveli++)
00148     {
00149         // If the optional pre-smoothing sweeps are selected
00150         // smooth the coarse-grid field for the restriced source
00151         if (nPreSweeps_)
00152         {
00153             coarseCorrFields[leveli] = 0.0;
00154 
00155             smoothers[leveli + 1].smooth
00156             (
00157                 coarseCorrFields[leveli],
00158                 coarseSources[leveli],
00159                 cmpt,
00160                 nPreSweeps_ + leveli
00161             );
00162 
00163             scalarField::subField ACf
00164             (
00165                 Apsi,
00166                 coarseCorrFields[leveli].size()
00167             );
00168 
00169             // Scale coarse-grid correction field
00170             // but not on the coarsest level because it evaluates to 1
00171             if (scaleCorrection_ && leveli < coarsestLevel - 1)
00172             {
00173                 scalar sf = scalingFactor
00174                 (
00175                     const_cast<scalarField&>(ACf.operator const scalarField&()),
00176                     matrixLevels_[leveli],
00177                     coarseCorrFields[leveli],
00178                     interfaceLevelsBouCoeffs_[leveli],
00179                     interfaceLevels_[leveli],
00180                     coarseSources[leveli],
00181                     cmpt
00182                 );
00183 
00184                 if (debug >= 2)
00185                 {
00186                     Pout<< sf << " ";
00187                 }
00188 
00189                 coarseCorrFields[leveli] *= sf;
00190             }
00191 
00192             // Correct the residual with the new solution
00193             matrixLevels_[leveli].Amul
00194             (
00195                 const_cast<scalarField&>(ACf.operator const scalarField&()),
00196                 coarseCorrFields[leveli],
00197                 interfaceLevelsBouCoeffs_[leveli],
00198                 interfaceLevels_[leveli],
00199                 cmpt
00200             );
00201 
00202             coarseSources[leveli] -= ACf;
00203         }
00204 
00205         // Residual is equal to source
00206         agglomeration_.restrictField
00207         (
00208             coarseSources[leveli + 1],
00209             coarseSources[leveli],
00210             leveli + 1
00211         );
00212     }
00213 
00214     if (debug >= 2 && nPreSweeps_)
00215     {
00216         Pout<< endl;
00217     }
00218 
00219 
00220     // Solve Coarsest level with either an iterative or direct solver
00221     solveCoarsestLevel
00222     (
00223         coarseCorrFields[coarsestLevel],
00224         coarseSources[coarsestLevel]
00225     );
00226 
00227 
00228     if (debug >= 2)
00229     {
00230         Pout<< "Post-smoothing scaling factors: ";
00231     }
00232 
00233     // Smoothing and prolongation of the coarse correction fields
00234     // (going to finer levels)
00235     for (label leveli = coarsestLevel - 1; leveli >= 0; leveli--)
00236     {
00237         // Create a field for the pre-smoothed correction field
00238         // as a sub-field of the finestCorrection which is not
00239         // currently being used
00240         scalarField::subField preSmoothedCoarseCorrField
00241         (
00242             finestCorrection,
00243             coarseCorrFields[leveli].size()
00244         );
00245 
00246         // Only store the preSmoothedCoarseCorrField is pre-smoothing is used
00247         if (nPreSweeps_)
00248         {
00249             preSmoothedCoarseCorrField.assign(coarseCorrFields[leveli]);
00250         }
00251 
00252         agglomeration_.prolongField
00253         (
00254             coarseCorrFields[leveli],
00255             coarseCorrFields[leveli + 1],
00256             leveli + 1
00257         );
00258 
00259         // Scale coarse-grid correction field
00260         // but not on the coarsest level because it evaluates to 1
00261         if (scaleCorrection_ && leveli < coarsestLevel - 1)
00262         {
00263             // Create A.psi for this coarse level as a sub-field of Apsi
00264             scalarField::subField ACf
00265             (
00266                 Apsi,
00267                 coarseCorrFields[leveli].size()
00268             );
00269 
00270             scalar sf = scalingFactor
00271             (
00272                 const_cast<scalarField&>(ACf.operator const scalarField&()),
00273                 matrixLevels_[leveli],
00274                 coarseCorrFields[leveli],
00275                 interfaceLevelsBouCoeffs_[leveli],
00276                 interfaceLevels_[leveli],
00277                 coarseSources[leveli],
00278                 cmpt
00279             );
00280 
00281 
00282             if (debug >= 2)
00283             {
00284                 Pout<< sf << " ";
00285             }
00286 
00287             coarseCorrFields[leveli] *= sf;
00288         }
00289 
00290         // Only add the preSmoothedCoarseCorrField is pre-smoothing is used
00291         if (nPreSweeps_)
00292         {
00293             coarseCorrFields[leveli] += preSmoothedCoarseCorrField;
00294         }
00295 
00296         smoothers[leveli + 1].smooth
00297         (
00298             coarseCorrFields[leveli],
00299             coarseSources[leveli],
00300             cmpt,
00301             nPostSweeps_ + leveli
00302         );
00303     }
00304 
00305     // Prolong the finest level correction
00306     agglomeration_.prolongField
00307     (
00308         finestCorrection,
00309         coarseCorrFields[0],
00310         0
00311     );
00312 
00313     if (scaleCorrection_)
00314     {
00315         // Calculate finest level scaling factor
00316         scalar fsf = scalingFactor
00317         (
00318             Apsi,
00319             matrix_,
00320             finestCorrection,
00321             interfaceBouCoeffs_,
00322             interfaces_,
00323             finestResidual,
00324             cmpt
00325         );
00326 
00327         if (debug >= 2)
00328         {
00329             Pout<< fsf << endl;
00330         }
00331 
00332         forAll(psi, i)
00333         {
00334             psi[i] += fsf*finestCorrection[i];
00335         }
00336     }
00337     else
00338     {
00339         forAll(psi, i)
00340         {
00341             psi[i] += finestCorrection[i];
00342         }
00343     }
00344 
00345     smoothers[0].smooth
00346     (
00347         psi,
00348         source,
00349         cmpt,
00350         nFinestSweeps_
00351     );
00352 }
00353 
00354 
00355 void Foam::GAMGSolver::initVcycle
00356 (
00357     PtrList<scalarField>& coarseCorrFields,
00358     PtrList<scalarField>& coarseSources,
00359     PtrList<lduMatrix::smoother>& smoothers
00360 ) const
00361 {
00362     coarseCorrFields.setSize(matrixLevels_.size());
00363     coarseSources.setSize(matrixLevels_.size());
00364     smoothers.setSize(matrixLevels_.size() + 1);
00365 
00366     // Create the smoother for the finest level
00367     smoothers.set
00368     (
00369         0,
00370         lduMatrix::smoother::New
00371         (
00372             fieldName_,
00373             matrix_,
00374             interfaceBouCoeffs_,
00375             interfaceIntCoeffs_,
00376             interfaces_,
00377             controlDict_
00378         )
00379     );
00380 
00381     forAll (matrixLevels_, leveli)
00382     {
00383         coarseCorrFields.set
00384         (
00385             leveli,
00386             new scalarField
00387             (
00388                 agglomeration_.meshLevel(leveli + 1).lduAddr().size()
00389             )
00390         );
00391 
00392         coarseSources.set
00393         (
00394             leveli,
00395             new scalarField
00396             (
00397                 agglomeration_.meshLevel(leveli + 1).lduAddr().size()
00398             )
00399         );
00400 
00401         smoothers.set
00402         (
00403             leveli + 1,
00404             lduMatrix::smoother::New
00405             (
00406                 fieldName_,
00407                 matrixLevels_[leveli],
00408                 interfaceLevelsBouCoeffs_[leveli],
00409                 interfaceLevelsIntCoeffs_[leveli],
00410                 interfaceLevels_[leveli],
00411                 controlDict_
00412             )
00413         );
00414     }
00415 }
00416 
00417 
00418 void Foam::GAMGSolver::solveCoarsestLevel
00419 (
00420     scalarField& coarsestCorrField,
00421     const scalarField& coarsestSource
00422 ) const
00423 {
00424     if (directSolveCoarsest_)
00425     {
00426         coarsestCorrField = coarsestSource;
00427         coarsestLUMatrixPtr_->solve(coarsestCorrField);
00428     }
00429     else
00430     {
00431         const label coarsestLevel = matrixLevels_.size() - 1;
00432         coarsestCorrField = 0;
00433         lduMatrix::solverPerformance coarseSolverPerf;
00434 
00435         if (matrixLevels_[coarsestLevel].asymmetric())
00436         {
00437             coarseSolverPerf = BICCG
00438             (
00439                 "coarsestLevelCorr",
00440                 matrixLevels_[coarsestLevel],
00441                 interfaceLevelsBouCoeffs_[coarsestLevel],
00442                 interfaceLevelsIntCoeffs_[coarsestLevel],
00443                 interfaceLevels_[coarsestLevel],
00444                 tolerance_,
00445                 relTol_
00446             ).solve
00447             (
00448                 coarsestCorrField,
00449                 coarsestSource
00450             );
00451         }
00452         else
00453         {
00454             coarseSolverPerf = ICCG
00455             (
00456                 "coarsestLevelCorr",
00457                 matrixLevels_[coarsestLevel],
00458                 interfaceLevelsBouCoeffs_[coarsestLevel],
00459                 interfaceLevelsIntCoeffs_[coarsestLevel],
00460                 interfaceLevels_[coarsestLevel],
00461                 tolerance_,
00462                 relTol_
00463             ).solve
00464             (
00465                 coarsestCorrField,
00466                 coarsestSource
00467             );
00468         }
00469 
00470         if (debug >= 2)
00471         {
00472             coarseSolverPerf.print();
00473         }
00474     }
00475 }
00476 
00477 
00478 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd