Merge remote-tracking branch 'origin/BUGFIX/signInHerschelBuckley'
[foam-extend-3.0.git] / src / foam / matrices / blockLduMatrix / BlockLduSolvers / BlockAmgSolver / BlockAmgSolver.C
blobfd2551c3f16e8c8146a67a4c1ce469b8f5a0f38c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Algebraic Multigrid solver with run-time selection of coarsening and cycle
27 Author
28     Klas Jareteg, 2013-04-15
30 \*---------------------------------------------------------------------------*/
32 #include "BlockAmgSolver.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
39 //- Construct from matrix and solver data stream
40 template<class Type>
41 Foam::BlockAmgSolver<Type>::BlockAmgSolver
43     const word& fieldName,
44     const BlockLduMatrix<Type>& matrix,
45     const dictionary& dict
48     BlockIterativeSolver<Type>
49     (
50         fieldName,
51         matrix,
52         dict
53     ),
54     amg_
55     (
56         matrix,
57         dict
58     )
62 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
64 template<class Type>
65 typename Foam::BlockSolverPerformance<Type>
66 Foam::BlockAmgSolver<Type>::solve
68     Field<Type>& x,
69     const Field<Type>& b
72     // Prepare solver performance
73     BlockSolverPerformance<Type> solverPerf
74     (
75         typeName,
76         this->fieldName()
77     );
79     // Create local references to avoid the spread this-> ugliness
80     const BlockLduMatrix<Type>& matrix = this->matrix_;
82     scalar norm = this->normFactor(x, b);
84     Field<Type> wA(x.size());
86     // Calculate residual.  Note: sign of residual swapped for efficiency
87     matrix.Amul(wA, x);
88     wA -= b;
90     solverPerf.initialResidual() = gSum(cmptMag(wA))/norm;
91     solverPerf.finalResidual() = solverPerf.initialResidual();
93     if (!solverPerf.checkConvergence(this->tolerance(), this->relTolerance()))
94     {
95         do
96         {
97             amg_.cycle(x, b);
99             // Re-calculate residual.  Note: sign of residual swapped
100             // for efficiency
101             matrix.Amul(wA, x);
102             wA -= b;
104             solverPerf.finalResidual() = gSum(cmptMag(wA))/norm;
105             solverPerf.nIterations()++;
107         } while (!this->stop(solverPerf));
108     }
110     return solverPerf;
114 // ************************************************************************* //