Merge /u/wyldckat/foam-extend32/ branch master into master
[foam-extend-3.2.git] / src / coupledMatrix / coupledLduSolver / iterativeSolver / coupledIterativeSolver.C
blob9b18dacb9971531b45824fa66483e1f85ef3f216
1 /*---------------------------------------------------------------------------*\
2   =========                 |
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 -------------------------------------------------------------------------------
8 License
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/>.
24 Description
25     Virtual base class for coupled iterative solvers
27 Author
28     Hrvoje Jasak, Wikki Ltd.  All rights reserved.
30 \*---------------------------------------------------------------------------*/
32 #include "coupledIterativeSolver.H"
34 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
36 Foam::coupledIterativeSolver::coupledIterativeSolver
38     const word& fieldName,
39     const coupledLduMatrix& matrix,
40     const PtrList<FieldField<Field, scalar> >& bouCoeffs,
41     const PtrList<FieldField<Field, scalar> >& intCoeffs,
42     const lduInterfaceFieldPtrsListList& interfaces,
43     const dictionary& solverData
46     coupledLduSolver
47     (
48         fieldName,
49         matrix,
50         bouCoeffs,
51         intCoeffs,
52         interfaces
53     ),
54     dict_(solverData),
55     tolerance_(1e-6),
56     relTolerance_(0),
57     minIter_(0),
58     maxIter_(1000)
60     readControls();
64 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
66 const Foam::dictionary& Foam::coupledIterativeSolver::dict() const
68     return dict_;
72 void Foam::coupledIterativeSolver::readControls()
74     dict().readIfPresent("minIter", minIter_);
75     dict().readIfPresent("maxIter", maxIter_);
76     dict().readIfPresent("tolerance", tolerance_);
77     dict().readIfPresent("relTol", relTolerance_);
81 Foam::scalar Foam::coupledIterativeSolver::normFactor
83     const FieldField<Field, scalar>& x,
84     const FieldField<Field, scalar>& b,
85     const FieldField<Field, scalar>& Ax,
86     FieldField<Field, scalar>& tmpField,
87     const direction cmpt
88 ) const
90     typedef FieldField<Field, scalar> scalarFieldField;
92     // Calculate reference value of x
93     scalar xRef = gAverage(x);
95     scalarFieldField pA(x.size());
97     forAll (x, rowI)
98     {
99         pA.set(rowI, new scalarField(x[rowI].size(), xRef));
100     }
102     // Calculate A.xRefField
103     matrix_.Amul(tmpField, pA, bouCoeffs_, interfaces_, cmpt);
105     // Calculate the normalisation factor
106     return gSum(mag(Ax - tmpField) + mag(b - tmpField)) + lduMatrix::small_;
110 Foam::scalar Foam::coupledIterativeSolver::normFactor
112     const FieldField<Field, scalar>& x,
113     const FieldField<Field, scalar>& b,
114     const direction cmpt
115 ) const
117     typedef FieldField<Field, scalar> scalarFieldField;
119     scalarFieldField wA(x.size());
120     scalarFieldField tmpField(x.size());
122     forAll (x, rowI)
123     {
124         wA.set(rowI, new scalarField(x[rowI].size(), 0));
125         tmpField.set(rowI, new scalarField(x[rowI].size()));
126     }
128     // Calculate A.x
129     matrix_.Amul(wA, x, bouCoeffs_, interfaces_, cmpt);
131     return normFactor(x, b, wA, tmpField, cmpt);
135 bool Foam::coupledIterativeSolver::stop
137     coupledSolverPerformance& solverPerf
138 ) const
140     if (solverPerf.nIterations() < minIter_)
141     {
142         return false;
143     }
145     if
146     (
147         solverPerf.nIterations() >= maxIter_
148      || solverPerf.checkConvergence(tolerance_, relTolerance_)
149     )
150     {
151         return true;
152     }
153     else
154     {
155         return false;
156     }
160 // ************************************************************************* //