PBiCG.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "PBiCG.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32  defineTypeNameAndDebug(PBiCG, 0);
33 
34  lduMatrix::solver::addasymMatrixConstructorToTable<PBiCG>
36 }
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
41 Foam::PBiCG::PBiCG
42 (
43  const word& fieldName,
44  const lduMatrix& matrix,
45  const FieldField<Field, scalar>& interfaceBouCoeffs,
46  const FieldField<Field, scalar>& interfaceIntCoeffs,
47  const lduInterfaceFieldPtrsList& interfaces,
48  const dictionary& solverControls
49 )
50 :
52  (
53  fieldName,
54  matrix,
55  interfaceBouCoeffs,
56  interfaceIntCoeffs,
57  interfaces,
58  solverControls
59  )
60 {}
61 
62 
63 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 
66 (
67  scalarField& psi,
68  const scalarField& source,
69  const direction cmpt
70 ) const
71 {
72  // --- Setup class containing solver performance data
73  solverPerformance solverPerf
74  (
75  lduMatrix::preconditioner::getName(controlDict_) + typeName,
76  fieldName_
77  );
78 
79  label nCells = psi.size();
80 
81  scalar* __restrict__ psiPtr = psi.begin();
82 
83  scalarField pA(nCells);
84  scalar* __restrict__ pAPtr = pA.begin();
85 
86  scalarField pT(nCells, 0.0);
87  scalar* __restrict__ pTPtr = pT.begin();
88 
89  scalarField wA(nCells);
90  scalar* __restrict__ wAPtr = wA.begin();
91 
92  scalarField wT(nCells);
93  scalar* __restrict__ wTPtr = wT.begin();
94 
95  scalar wArT = solverPerf.great_;
96  scalar wArTold = wArT;
97 
98  // --- Calculate A.psi and T.psi
99  matrix_.Amul(wA, psi, interfaceBouCoeffs_, interfaces_, cmpt);
100  matrix_.Tmul(wT, psi, interfaceIntCoeffs_, interfaces_, cmpt);
101 
102  // --- Calculate initial residual and transpose residual fields
103  scalarField rA(source - wA);
104  scalarField rT(source - wT);
105  scalar* __restrict__ rAPtr = rA.begin();
106  scalar* __restrict__ rTPtr = rT.begin();
107 
108  // --- Calculate normalisation factor
109  scalar normFactor = this->normFactor(psi, source, wA, pA);
110 
111  if (lduMatrix::debug >= 2)
112  {
113  Info<< " Normalisation factor = " << normFactor << endl;
114  }
115 
116  // --- Calculate normalised residual norm
117  solverPerf.initialResidual() =
118  gSumMag(rA, matrix().mesh().comm())
119  /normFactor;
120  solverPerf.finalResidual() = solverPerf.initialResidual();
121 
122  // --- Check convergence, solve if not converged
123  if
124  (
125  minIter_ > 0
126  || !solverPerf.checkConvergence(tolerance_, relTol_)
127  )
128  {
129  // --- Select and construct the preconditioner
132  (
133  *this,
134  controlDict_
135  );
136 
137  // --- Solver iteration
138  do
139  {
140  // --- Store previous wArT
141  wArTold = wArT;
142 
143  // --- Precondition residuals
144  preconPtr->precondition(wA, rA, cmpt);
145  preconPtr->preconditionT(wT, rT, cmpt);
146 
147  // --- Update search directions:
148  wArT = gSumProd(wA, rT, matrix().mesh().comm());
149 
150  if (solverPerf.nIterations() == 0)
151  {
152  for (label cell=0; cell<nCells; cell++)
153  {
154  pAPtr[cell] = wAPtr[cell];
155  pTPtr[cell] = wTPtr[cell];
156  }
157  }
158  else
159  {
160  scalar beta = wArT/wArTold;
161 
162  for (label cell=0; cell<nCells; cell++)
163  {
164  pAPtr[cell] = wAPtr[cell] + beta*pAPtr[cell];
165  pTPtr[cell] = wTPtr[cell] + beta*pTPtr[cell];
166  }
167  }
168 
169 
170  // --- Update preconditioned residuals
171  matrix_.Amul(wA, pA, interfaceBouCoeffs_, interfaces_, cmpt);
172  matrix_.Tmul(wT, pT, interfaceIntCoeffs_, interfaces_, cmpt);
173 
174  scalar wApT = gSumProd(wA, pT, matrix().mesh().comm());
175 
176  // --- Test for singularity
177  if (solverPerf.checkSingularity(mag(wApT)/normFactor))
178  {
179  break;
180  }
181 
182 
183  // --- Update solution and residual:
184 
185  scalar alpha = wArT/wApT;
186 
187  for (label cell=0; cell<nCells; cell++)
188  {
189  psiPtr[cell] += alpha*pAPtr[cell];
190  rAPtr[cell] -= alpha*wAPtr[cell];
191  rTPtr[cell] -= alpha*wTPtr[cell];
192  }
193 
194  solverPerf.finalResidual() =
195  gSumMag(rA, matrix().mesh().comm())
196  /normFactor;
197  } while
198  (
199  (
200  solverPerf.nIterations()++ < maxIter_
201  && !solverPerf.checkConvergence(tolerance_, relTol_)
202  )
203  || solverPerf.nIterations() < minIter_
204  );
205  }
206 
207  return solverPerf;
208 }
209 
210 
211 // ************************************************************************* //
scalar gSumMag(const FieldField< Field, Type > &f)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
uint8_t direction
Definition: direction.H:46
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const Type & finalResidual() const
Return final residual.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const lduMesh & mesh() const
Return the LDU mesh from which the addressing is obtained.
Definition: lduMatrix.H:541
bool checkSingularity(const Type &residual)
Singularity test.
Generic field type.
Definition: FieldField.H:51
static const scalar great_
Large Type for the use in solvers.
bool checkConvergence(const Type &tolerance, const Type &relTolerance)
Check, store and return convergence.
A class for handling words, derived from string.
Definition: word.H:59
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics...
static autoPtr< preconditioner > New(const solver &sol, const dictionary &solverControls)
Return a new preconditioner.
Abstract base-class for lduMatrix solvers.
Definition: lduMatrix.H:93
label nIterations() const
Return number of iterations.
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
lduMatrix::solver::addasymMatrixConstructorToTable< PBiCG > addPBiCGAsymMatrixConstructorToTable_
Definition: PBiCG.C:35
scalar gSumProd(const UList< Type > &f1, const UList< Type > &f2, const label comm)
virtual solverPerformance solve(scalarField &psi, const scalarField &source, const direction cmpt=0) const
Solve the matrix with this solver.
Definition: PBiCG.C:66
defineTypeNameAndDebug(combustionModel, 0)
lduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: lduMatrix.H:79
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:56
const Type & initialResidual() const
Return initial residual.
messageStream Info
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
dimensioned< scalar > mag(const dimensioned< Type > &)
static word getName(const dictionary &)
Find the preconditioner name (directly or from a sub-dictionary)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Namespace for OpenFOAM.