1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
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/>.
28 Finest AMG level container, using matrix and field references
31 Hrvoje Jasak, Wikki Ltd. All rights reserved
33 \*---------------------------------------------------------------------------*/
35 #include "fineAmgLevel.H"
36 #include "coarseAmgLevel.H"
38 #include "bicgStabSolver.H"
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 // Construct from components
43 Foam::fineAmgLevel::fineAmgLevel
45 const lduMatrix& matrix,
46 const FieldField<Field, scalar>& coupleBouCoeffs,
47 const FieldField<Field, scalar>& coupleIntCoeffs,
48 const lduInterfaceFieldPtrsList& interfaceFields,
49 const dictionary& dict,
50 const word& policyType,
51 const label groupSize,
52 const label minCoarseEqns,
53 const word& smootherType
57 coupleBouCoeffs_(coupleBouCoeffs),
58 coupleIntCoeffs_(coupleIntCoeffs),
59 interfaceFields_(interfaceFields),
63 amgPolicy::New(policyType, matrix_, groupSize, minCoarseEqns)
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 Foam::scalarField& Foam::fineAmgLevel::x()
83 FatalErrorIn("scalarField& Foam::fineAmgLevel::x()")
84 << "x is not available."
88 return const_cast<scalarField&>(scalarField::null());
92 Foam::scalarField& Foam::fineAmgLevel::b()
94 FatalErrorIn("scalarField& Foam::fineAmgLevel::b()")
95 << "b is not available."
99 return const_cast<scalarField&>(scalarField::null());
103 void Foam::fineAmgLevel::residual
105 const scalarField& x,
106 const scalarField& b,
107 const direction cmpt,
123 res[i] = b[i] - res[i];
128 void Foam::fineAmgLevel::restrictResidual
130 const scalarField& x,
131 const scalarField& b,
132 const direction cmpt,
133 scalarField& xBuffer,
134 scalarField& coarseRes,
140 // Calculate residual
141 residual(x, b, cmpt, xBuffer);
144 // Here x != 0. It is assumed that the buffer will contain the residual
145 // if no pre-sweeps have been done. HJ, 4/Sep/2006
146 policyPtr_->restrictResidual(xBuffer, coarseRes);
150 void Foam::fineAmgLevel::prolongateCorrection
153 const scalarField& coarseX
156 policyPtr_->prolongateCorrection(x, coarseX);
160 void Foam::fineAmgLevel::smooth
163 const scalarField& b,
164 const direction cmpt,
168 smootherPtr_->smooth(x, b, cmpt, nSweeps);
172 void Foam::fineAmgLevel::solve
175 const scalarField& b,
176 const direction cmpt,
177 const scalar tolerance,
181 Info << "Fine level direct solution" << endl;
183 lduSolverPerformance coarseSolverPerf;
185 dictionary fineLevelDict;
186 fineLevelDict.add("minIter", 1);
187 fineLevelDict.add("maxIter", 1000);
188 fineLevelDict.add("tolerance", tolerance);
189 fineLevelDict.add("relTol", relTol);
191 // Avoid issues with round-off on strict tolerance setup
193 x = b/matrix_.diag();
195 if (matrix_.symmetric())
197 fineLevelDict.add("preconditioner", "Cholesky");
199 coarseSolverPerf = cgSolver
211 fineLevelDict.add("preconditioner", "ILU0");
213 coarseSolverPerf = bicgStabSolver
224 if (lduMatrix::debug >= 2)
226 coarseSolverPerf.print();
231 void Foam::fineAmgLevel::scaleX
234 const scalarField& b,
235 const direction cmpt,
240 scalarField& Ax = xBuffer;
251 scalar scalingFactorNum = 0.0;
252 scalar scalingFactorDenom = 0.0;
256 scalingFactorNum += x[i]*b[i];
257 scalingFactorDenom += x[i]*Ax[i];
260 vector scalingVector(scalingFactorNum, scalingFactorDenom, 0);
261 reduce(scalingVector, sumOp<vector>());
266 mag(scalingVector[0]) > GREAT
267 || mag(scalingVector[1]) > GREAT
268 || scalingVector[0]*scalingVector[1] <= 0
269 || mag(scalingVector[0]) < mag(scalingVector[1])
272 // Factor = 1.0, no scaling
274 else if (mag(scalingVector[0]) > 2*mag(scalingVector[1]))
282 x *= scalingVector[0]/stabilise(scalingVector[1], SMALL);
287 Foam::autoPtr<Foam::amgLevel> Foam::fineAmgLevel::makeNextLevel() const
289 if (policyPtr_->coarsen())
291 return autoPtr<Foam::amgLevel>
295 policyPtr_->restrictMatrix
303 policyPtr_->groupSize(),
304 policyPtr_->minCoarseEqns(),
311 // Final level: cannot coarsen
312 return autoPtr<Foam::amgLevel>();
317 // ************************************************************************* //