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/>.
25 coupledGaussSeidelPrecon
28 GaussSeidel preconditioning
31 Hrvoje Jasak, Wikki Ltd. All rights reserved.
33 \*---------------------------------------------------------------------------*/
35 #include "coupledGaussSeidelPrecon.H"
36 #include "addToRunTimeSelectionTable.H"
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 defineTypeNameAndDebug(coupledGaussSeidelPrecon, 0);
44 addToRunTimeSelectionTable
47 coupledGaussSeidelPrecon,
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 void Foam::coupledGaussSeidelPrecon::forwardSweep
57 const lduMatrix& matrix,
62 const scalarField& diag = matrix.diag();
63 const scalarField& lower = matrix.lower();
64 const scalarField& upper = matrix.upper();
66 const labelList& upperAddr = matrix.lduAddr().upperAddr();
67 const labelList& ownStartAddr = matrix.lduAddr().ownerStartAddr();
69 const label nRows = x.size();
72 for (register label rowI = 0; rowI < nRows; rowI++)
74 // lRow is equal to rowI
75 scalar& curX = x[rowI];
77 // Grab the accumulated neighbour side
80 // Start and end of this row
81 fStart = ownStartAddr[rowI];
82 fEnd = ownStartAddr[rowI + 1];
84 // Accumulate the owner product side
85 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
87 curX -= upper[curCoeff]*x[upperAddr[curCoeff]];
93 // Distribute the neighbour side using current x
94 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
96 bPrime[upperAddr[curCoeff]] -= lower[curCoeff]*curX;
102 void Foam::coupledGaussSeidelPrecon::reverseSweep
104 const lduMatrix& matrix,
109 const scalarField& diag = matrix.diag();
110 const scalarField& lower = matrix.lower();
111 const scalarField& upper = matrix.upper();
113 const labelList& upperAddr = matrix.lduAddr().upperAddr();
114 const labelList& ownStartAddr = matrix.lduAddr().ownerStartAddr();
116 const label nRows = x.size();
119 for (register label rowI = nRows - 1; rowI >= 0; rowI--)
121 // lRow is equal to rowI
122 scalar& curX = x[rowI];
124 // Grab the accumulated neighbour side
127 // Start and end of this row
128 fStart = ownStartAddr[rowI];
129 fEnd = ownStartAddr[rowI + 1];
131 // Accumulate the owner product side
132 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
134 curX -= upper[curCoeff]*x[upperAddr[curCoeff]];
140 // Distribute the neighbour side using current x
141 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
143 bPrime[upperAddr[curCoeff]] -= lower[curCoeff]*curX;
149 void Foam::coupledGaussSeidelPrecon::forwardSweepTranspose
151 const lduMatrix& matrix,
156 const scalarField& diag = matrix.diag();
157 const scalarField& lower = matrix.lower();
158 const scalarField& upper = matrix.upper();
160 const labelList& upperAddr = matrix.lduAddr().upperAddr();
161 const labelList& ownStartAddr = matrix.lduAddr().ownerStartAddr();
163 const label nRows = x.size();
166 for (register label rowI = 0; rowI < nRows; rowI++)
168 // lRow is equal to rowI
169 scalar& curX = x[rowI];
171 // Grab the accumulated neighbour side
174 // Start and end of this row
175 fStart = ownStartAddr[rowI];
176 fEnd = ownStartAddr[rowI + 1];
178 // Accumulate the owner product side
179 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
181 // Transpose multiplication. HJ, 19/Jan/2009
182 curX -= lower[curCoeff]*x[upperAddr[curCoeff]];
188 // Distribute the neighbour side using current x
189 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
191 // Transpose multiplication. HJ, 19/Jan/2009
192 bPrime[upperAddr[curCoeff]] -= upper[curCoeff]*curX;
198 void Foam::coupledGaussSeidelPrecon::reverseSweepTranspose
200 const lduMatrix& matrix,
205 const scalarField& diag = matrix.diag();
206 const scalarField& lower = matrix.lower();
207 const scalarField& upper = matrix.upper();
209 const labelList& upperAddr = matrix.lduAddr().upperAddr();
210 const labelList& ownStartAddr = matrix.lduAddr().ownerStartAddr();
212 const label nRows = x.size();
215 for (register label rowI = nRows - 1; rowI >= 0; rowI--)
217 // lRow is equal to rowI
218 scalar& curX = x[rowI];
220 // Grab the accumulated neighbour side
223 // Start and end of this row
224 fStart = ownStartAddr[rowI];
225 fEnd = ownStartAddr[rowI + 1];
227 // Accumulate the owner product side
228 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
230 // Transpose multiplication. HJ, 19/Jan/2009
231 curX -= lower[curCoeff]*x[upperAddr[curCoeff]];
237 // Distribute the neighbour side using current x
238 for (register label curCoeff = fStart; curCoeff < fEnd; curCoeff++)
240 // Transpose multiplication. HJ, 19/Jan/2009
241 bPrime[upperAddr[curCoeff]] -= upper[curCoeff]*curX;
247 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
249 Foam::coupledGaussSeidelPrecon::coupledGaussSeidelPrecon
251 const coupledLduMatrix& matrix,
252 const PtrList<FieldField<Field, scalar> >& bouCoeffs,
253 const PtrList<FieldField<Field, scalar> >& intCoeffs,
254 const lduInterfaceFieldPtrsListList& interfaces
264 mBouCoeffs_(bouCoeffs),
265 bPrime_(matrix.size())
267 // Invert boundary coefficients
268 forAll (mBouCoeffs_, rowI)
270 mBouCoeffs_[rowI] *= -1;
273 // Hook bPrime components
274 forAll (matrix_, rowI)
276 bPrime_.set(rowI, new scalarField(matrix_[rowI].lduAddr().size(), 0));
281 Foam::coupledGaussSeidelPrecon::coupledGaussSeidelPrecon
283 const coupledLduMatrix& matrix,
284 const PtrList<FieldField<Field, scalar> >& bouCoeffs,
285 const PtrList<FieldField<Field, scalar> >& intCoeffs,
286 const lduInterfaceFieldPtrsListList& interfaces,
287 const dictionary& dict
297 mBouCoeffs_(bouCoeffs),
298 bPrime_(matrix.size())
300 // Invert boundary coefficients
301 forAll (mBouCoeffs_, rowI)
303 mBouCoeffs_[rowI] *= -1;
306 // Hook bPrime components
307 forAll (matrix_, rowI)
309 bPrime_.set(rowI, new scalarField(matrix_[rowI].lduAddr().size(), 0));
314 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
316 void Foam::coupledGaussSeidelPrecon::precondition
318 FieldField<Field, scalar>& x,
319 const FieldField<Field, scalar>& b,
323 // Execute preconditioning
324 if (matrix_.diagonal())
326 forAll (matrix_, rowI)
328 x[rowI] = b[rowI]/matrix_[rowI].diag();
331 else if (matrix_.symmetric() || matrix_.asymmetric())
335 // Parallel boundary update
337 matrix_.initMatrixInterfaces
344 true // switch to lhs
347 matrix_.updateMatrixInterfaces
354 true // switch to lhs
359 forAll (matrix_, rowI)
361 forwardSweep(matrix_[rowI], x[rowI], bPrime_[rowI]);
365 forAllReverse (matrix_, rowI)
367 reverseSweep(matrix_[rowI], x[rowI], bPrime_[rowI]);
373 void Foam::coupledGaussSeidelPrecon::preconditionT
375 FieldField<Field, scalar>& x,
376 const FieldField<Field, scalar>& b,
380 // Execute preconditioning
381 if (matrix_.diagonal())
383 forAll (matrix_, rowI)
385 x[rowI] = b[rowI]/matrix_[rowI].diag();
388 else if (matrix_.symmetric() || matrix_.asymmetric())
392 // Parallel boundary update
394 matrix_.initMatrixInterfaces
403 matrix_.updateMatrixInterfaces
414 forAll (matrix_, rowI)
416 forwardSweepTranspose(matrix_[rowI], x[rowI], bPrime_[rowI]);
419 // Parallel boundary update
421 matrix_.initMatrixInterfaces
430 matrix_.updateMatrixInterfaces
441 forAllReverse (matrix_, rowI)
443 reverseSweepTranspose(matrix_[rowI], x[rowI], bPrime_[rowI]);
449 // ************************************************************************* //