PBiCCCG.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "PBiCCCG.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Type, class DType, class LUType>
35 (
36  const word& fieldName,
37  const LduMatrix<Type, DType, LUType>& matrix,
38  const dictionary& solverDict
39 )
40 :
42  (
43  fieldName,
44  matrix,
45  solverDict
46  )
47 {}
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
52 template<class Type, class DType, class LUType>
55 (
57 ) const
58 {
59  const word preconditionerName(this->controlDict_.getWord("preconditioner"));
60 
61  // --- Setup class containing solver performance data
62  SolverPerformance<Type> solverPerf
63  (
64  preconditionerName + typeName,
65  this->fieldName_
66  );
67 
68  label nIter = 0;
69 
70  label nCells = psi.size();
71 
72  Type* __restrict__ psiPtr = psi.begin();
73 
74  Field<Type> pA(nCells);
75  Type* __restrict__ pAPtr = pA.begin();
76 
77  Field<Type> pT(nCells, Zero);
78  Type* __restrict__ pTPtr = pT.begin();
79 
80  Field<Type> wA(nCells);
81  Type* __restrict__ wAPtr = wA.begin();
82 
83  Field<Type> wT(nCells);
84  Type* __restrict__ wTPtr = wT.begin();
85 
86  scalar wArT = 1e15; //this->matrix_.great_;
87  scalar wArTold = wArT;
88 
89  // --- Calculate A.psi and T.psi
90  this->matrix_.Amul(wA, psi);
91  this->matrix_.Tmul(wT, psi);
92 
93  // --- Calculate initial residual and transpose residual fields
94  Field<Type> rA(this->matrix_.source() - wA);
95  Field<Type> rT(this->matrix_.source() - wT);
96  Type* __restrict__ rAPtr = rA.begin();
97  Type* __restrict__ rTPtr = rT.begin();
98 
99  // --- Calculate normalisation factor
100  Type normFactor = this->normFactor(psi, wA, pA);
101 
102  if ((this->log_ >= 2) || (LduMatrix<Type, DType, LUType>::debug >= 2))
103  {
104  Info<< " Normalisation factor = " << normFactor << endl;
105  }
106 
107  // --- Calculate normalised residual norm
108  solverPerf.initialResidual() = cmptDivide(gSumCmptMag(rA), normFactor);
109  solverPerf.finalResidual() = solverPerf.initialResidual();
110 
111  // --- Check convergence, solve if not converged
112  if
113  (
114  this->minIter_ > 0
115  || !solverPerf.checkConvergence
116  (
117  this->tolerance_,
118  this->relTol_,
119  this->log_
120  )
121  )
122  {
123  // --- Select and construct the preconditioner
126  (
127  *this,
128  this->controlDict_
129  );
130 
131  // --- Solver iteration
132  do
133  {
134  // --- Store previous wArT
135  wArTold = wArT;
136 
137  // --- Precondition residuals
138  preconPtr->precondition(wA, rA);
139  preconPtr->preconditionT(wT, rT);
140 
141  // --- Update search directions:
142  wArT = gSumProd(wA, rT);
143 
144  if (nIter == 0)
145  {
146  for (label cell=0; cell<nCells; cell++)
147  {
148  pAPtr[cell] = wAPtr[cell];
149  pTPtr[cell] = wTPtr[cell];
150  }
151  }
152  else
153  {
154  scalar beta = wArT/wArTold;
155 
156  for (label cell=0; cell<nCells; cell++)
157  {
158  pAPtr[cell] = wAPtr[cell] + (beta* pAPtr[cell]);
159  pTPtr[cell] = wTPtr[cell] + (beta* pTPtr[cell]);
160  }
161  }
162 
163 
164  // --- Update preconditioned residuals
165  this->matrix_.Amul(wA, pA);
166  this->matrix_.Tmul(wT, pT);
167 
168  scalar wApT = gSumProd(wA, pT);
169 
170  // --- Test for singularity
171  if
172  (
173  solverPerf.checkSingularity
174  (
175  cmptDivide(pTraits<Type>::one*mag(wApT), normFactor)
176  )
177  )
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  cmptDivide(gSumCmptMag(rA), normFactor);
196 
197  } while
198  (
199  (
200  nIter++ < this->maxIter_
201  && !solverPerf.checkConvergence
202  (
203  this->tolerance_,
204  this->relTol_,
205  this->log_
206  )
207  )
208  || nIter < this->minIter_
209  );
210  }
211 
212  solverPerf.nIterations() =
214 
215  return solverPerf;
216 }
217 
218 
219 // ************************************************************************* //
Foam::PBiCCCG::solve
virtual SolverPerformance< Type > solve(Field< Type > &psi) const
Solve the matrix with this solver.
Definition: PBiCCCG.C:55
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:212
PBiCCCG.H
Foam::LduMatrix
LduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: LduMatrix.H:72
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::LduMatrix::solver
Abstract base-class for LduMatrix solvers.
Definition: LduMatrix.H:115
Foam::gSumCmptMag
Type gSumCmptMag(const UList< Type > &f, const label comm)
Definition: FieldFunctions.C:592
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
beta
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
Foam::gSumProd
scalarProduct< Type, Type >::type gSumProd(const UList< Type > &f1, const UList< Type > &f2, const label comm)
Definition: FieldFunctions.C:605
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::cmptDivide
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
psi
const volScalarField & psi
Definition: createFieldRefs.H:1
Foam::SolverPerformance
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
Definition: SolverPerformance.H:52
Foam::cell
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:54
Foam::PBiCCCG
Preconditioned bi-conjugate gradient solver for asymmetric lduMatrices using a run-time selectable pr...
Definition: PBiCCCG.H:53