1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM 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 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 Preconditioned Conjugate Gradient solver with run-time selectable
30 Hrvoje Jasak, Wikki Ltd. All rights reserved
32 \*---------------------------------------------------------------------------*/
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 defineTypeNameAndDebug(cgSolver, 0);
42 lduSolver::addsymMatrixConstructorToTable<cgSolver>
43 addcgSolverSymMatrixConstructorToTable_;
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 //- Construct from matrix and solver data stream
50 Foam::cgSolver::cgSolver
52 const word& fieldName,
53 const lduMatrix& matrix,
54 const FieldField<Field, scalar>& coupleBouCoeffs,
55 const FieldField<Field, scalar>& coupleIntCoeffs,
56 const lduInterfaceFieldPtrsList& interfaces,
57 const dictionary& dict
71 lduPreconditioner::New
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 Foam::lduSolverPerformance Foam::cgSolver::solve
92 // Prepare solver performance
93 lduSolverPerformance solverPerf(typeName, fieldName());
95 scalarField wA(x.size());
96 scalarField rA(x.size());
98 // Calculate initial residual
99 matrix_.Amul(wA, x, coupleBouCoeffs_, interfaces_, cmpt);
101 // Use rA as scratch space when calculating the normalisation factor
102 scalar normFactor = this->normFactor(x, b, wA, rA, cmpt);
104 if (lduMatrix::debug >= 2)
106 Info<< " Normalisation factor = " << normFactor << endl;
109 // Calculate residual
112 rA[i] = b[i] - wA[i];
115 solverPerf.initialResidual() = gSumMag(rA)/normFactor;
116 solverPerf.finalResidual() = solverPerf.initialResidual();
118 if (!stop(solverPerf))
120 scalar rho = matrix_.great_;
123 scalar alpha, beta, wApA;
125 scalarField pA(x.size(), 0);
131 // Execute preconditioning
132 preconPtr_->precondition(wA, rA, cmpt);
134 // Update search directions
135 rho = gSumProd(wA, rA);
141 pA[i] = wA[i] + beta*pA[i];
145 // Update preconditioned residual
146 matrix_.Amul(wA, pA, coupleBouCoeffs_, interfaces_, cmpt);
148 wApA = gSumProd(wA, pA);
151 // Check for singularity
152 if (solverPerf.checkSingularity(mag(wApA)/normFactor))
157 // Update solution and residual
167 rA[i] -= alpha*wA[i];
170 solverPerf.finalResidual() = gSumMag(rA)/normFactor;
171 solverPerf.nIterations()++;
172 } while (!stop(solverPerf));
179 // ************************************************************************* //