Merge remote-tracking branch 'origin/BUGFIX/signInHerschelBuckley'
[foam-extend-3.0.git] / src / foam / matrices / blockLduMatrix / BlockLduMatrix / BlockLduMatrix.H
bloba5cd36466d7a7b588b8be6ac4e1fd62ec8407f6c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
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 Class
25     BlockLduMatrix
27 Description
28     BlockLduMatrix is a general matrix class in which the coefficients are
29     stored as three arrays, one for the upper triangle, one for the
30     lower triangle and a third for the diagonal.  Addressing object must
31     be supplied for the upper and lower triangles.
33 Author
34     Hrvoje Jasak, Wikki Ltd.  All rights reserved.
36 SourceFiles
37     BlockLduMatrix.C
38     BlockLduMatrixOperations.C
39     BlockLduMatrixUpdateInterfaces.C
40     BlockLduMatrixATmul.C
41     BlockLduMatrixHOps.C
42     BlockLduMatrixDecouple.C
43     BlockLduMatrixDecoupledHOps.C
45 \*---------------------------------------------------------------------------*/
47 #ifndef BlockLduMatrix_H
48 #define BlockLduMatrix_H
50 #include "coeffFields.H"
51 #include "FieldField.H"
52 #include "lduMesh.H"
53 #include "HashSet.H"
54 #include "BlockLduInterfaceFieldPtrsList.H"
55 #include "Map.H"
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 namespace Foam
62 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
64 template<class Type>
65 class BlockLduMatrix;
67 template<class Type>
68 Ostream& operator<<(Ostream&, const BlockLduMatrix<Type>&);
70 template<class Type>
71 class BlockConstraint;
74 /*---------------------------------------------------------------------------*\
75                         Class BlockLduMatrix Declaration
76 \*---------------------------------------------------------------------------*/
78 template<class Type>
79 class BlockLduMatrix
81     public refCount
83 public:
85     // Public data types
87         typedef CoeffField<Type> TypeCoeffField;
88         typedef Field<Type> TypeField;
89         typedef BlockConstraint<Type> ConstraintType;
92 private:
94     // Private data
96         // LDU mesh reference
97         const lduMesh& lduMesh_;
99         //- Eliminated equations
100         //  Consider adding solo cells on construction.  HJ, 26/Oct/2012
101         labelHashSet eliminatedEqns_;
104         // Block matrix elements
106             //- Diagonal coefficients
107             CoeffField<Type>* diagPtr_;
109             //- Upper triangle coefficients.  Also used for symmetric matrix
110             CoeffField<Type>* upperPtr_;
112             //- Lower triangle coefficients
113             CoeffField<Type> *lowerPtr_;
116         // Coupling
118             //- List of coupled interfaces
119             typename BlockLduInterfaceFieldPtrsList<Type>::Type interfaces_;
121             //- Coupled interface coefficients, upper
122             FieldField<CoeffField, Type> coupleUpper_;
124             //- Coupled interface coefficients, lower
125             FieldField<CoeffField, Type> coupleLower_;
128         // Constraints
130             //- Equation triangle map
131             mutable Map<ConstraintType> fixedEqns_;
134     // Private static data
136         //- Matrix constraint fill-in
137         //  Equals to the estimated fraction of fixed nodes in the matrix
138         static const label fixFillIn;
141     // Private member functions
143         // Decoupled versions of nmatrix operations
145             //- Sum off-diagonal coefficients and add to diagonal,
146             //  decoupled version
147             void decoupledSumDiag();
149             //- Sum negative off-diagonal coefficients and add to diagonal,
150             //  decoupled version
151             void decoupledNegSumDiag();
153             //- Check matrix for diagonal dominance, decoupled version
154             void decoupledCheck() const;
156             //- Relax matrix, decoupled version
157             void decoupledRelax
158             (
159                 const TypeField& x,
160                 TypeField& b,
161                 const scalar alpha
162             );
164             //- Matrix scaling with scalar field, decoupled version
165             void decoupledMultEqOp(const scalarField& sf);
167             //- Matrix multiplication without coupled interface update,
168             //  Decoupled version
169             void decoupledAmulCore
170             (
171                 TypeField& Ax,
172                 const TypeField& x
173             ) const;
175             //- Matrix transpose multiplication without coupled
176             //  interface update
177             //  Decoupled version
178             void decoupledTmulCore
179             (
180                 TypeField& Tx,
181                 const TypeField& x
182             ) const;
184             //- Return L-U vector-matrix multiplication in row-form,
185             //  Decoupled version
186             tmp<TypeField> decoupledH(const TypeField& x) const;
188             //- Return L-U  vector-matrix multiplication in off-diagonal form,
189             //  Decoupled version
190             tmp<TypeField> decoupledFaceH(const TypeField& x) const;
193 protected:
195     // Access to constraints
197         //- Return constraint map
198         const Map<ConstraintType>& fixedEqns() const
199         {
200             return fixedEqns_;
201         }
204         //- Return access constraint map
205         Map<ConstraintType>& fixedEqns()
206         {
207             return fixedEqns_;
208         }
211 public:
213     //- Runtime type information
214     TypeName("BlockLduMatrix");
217     // Constructors
219         //- Construct given addressing
220         explicit BlockLduMatrix(const lduMesh&);
222         //- Construct as copy
223         BlockLduMatrix(const BlockLduMatrix<Type>&);
225         //- Construct as copy or re-use as specified.
226         BlockLduMatrix(BlockLduMatrix<Type>&, bool reUse);
229     // Destructor
231         virtual ~BlockLduMatrix();
234     // Member functions
236         // Access to addressing
238             //- Return the LDU mesh from which the addressing is obtained
239             const lduMesh& mesh() const
240             {
241                 return lduMesh_;
242             }
244             //- Return eliminated equations
245             const labelHashSet& eliminatedEqns() const
246             {
247                 return eliminatedEqns_;
248             }
250             //- Return access to eliminated equations
251             labelHashSet& eliminatedEqns()
252             {
253                 return eliminatedEqns_;
254             }
256             //- Return the LDU addressing
257             const lduAddressing& lduAddr() const
258             {
259                 return lduMesh_.lduAddr();
260             }
262             //- Return the patch evaluation schedule
263             const lduSchedule& patchSchedule() const
264             {
265                 return lduAddr().patchSchedule();
266             }
269         // Access to coefficients
271             //- Return access to diagonal coefficients
272             TypeCoeffField& diag();
274             //- Return diagonal coefficients
275             const TypeCoeffField& diag() const;
277             //- Return access to upper coefficients
278             //  Also used for symmetric matrices
279             TypeCoeffField& upper();
281             //- Return upper coefficients
282             //  Also used for symmetric matrices
283             const TypeCoeffField& upper() const;
285             //- Return access to lower coefficients
286             TypeCoeffField& lower();
288             //- Return lower coefficients
289             const TypeCoeffField& lower() const;
291             //- Return access to coupled interface coefficients, upper
292             FieldField<CoeffField, Type>& coupleUpper()
293             {
294                 return coupleUpper_;
295             }
297             //- Return coupled interface coefficients, upper
298             const FieldField<CoeffField, Type>& coupleUpper() const
299             {
300                 return coupleUpper_;
301             }
303             //- Return access to coupled interface coefficients, lower
304             FieldField<CoeffField, Type>& coupleLower()
305             {
306                 return coupleLower_;
307             }
309             //- Return coupled interface coefficients, lower
310             const FieldField<CoeffField, Type>& coupleLower() const
311             {
312                 return coupleLower_;
313             }
315             //- Access to coupled interfaces
316             typename BlockLduInterfaceFieldPtrsList<Type>::Type& interfaces()
317             {
318                 return interfaces_;
319             }
322         // Matrix structure
324             //- Return true if there is a diagonal
325             bool thereIsDiag() const
326             {
327                 return (diagPtr_);
328             }
330             //- Return true if upper triangle is allocated
331             bool thereIsUpper() const
332             {
333                 return (upperPtr_);
334             }
336             //- Return true if lower triangle is allocated
337             bool thereIsLower() const
338             {
339                 return (lowerPtr_);
340             }
342             //- Return true if matrix is empty
343             bool empty() const;
345             //- Return true if matrix is diagonal-only
346             bool diagonal() const;
348             //- Return true if matrix is symmetric
349             bool symmetric() const;
351             //- Return true if matrix is asymmetric
352             bool asymmetric() const;
354             //- Return true if matrix is component-coupled
355             bool componentCoupled() const;
358         // Operations
360             //- Sum off-diagonal coefficients and add to diagonal
361             void sumDiag();
363             //- Sum negative off-diagonal coefficients and add to diagonal
364             void negSumDiag();
366             //- Check matrix for diagonal dominance
367             void check() const;
369             //- Relax matrix
370             void relax
371             (
372                 const TypeField& x,
373                 TypeField& b,
374                 const scalar alpha
375             );
377             //- Matrix multiplication
378             void Amul
379             (
380                 TypeField& Ax,
381                 const TypeField& x
382             ) const;
384             //- Matrix multiplication without coupled interface update
385             void AmulCore
386             (
387                 TypeField& Ax,
388                 const TypeField& x
389             ) const;
391             //- Matrix transpose multiplication
392             void Tmul
393             (
394                 TypeField& Ax,
395                 const TypeField& x
396             ) const;
398             //- Matrix transpose multiplication without coupled
399            //  interface update
400             void TmulCore
401             (
402                 TypeField& Ax,
403                 const TypeField& x
404             ) const;
407             //- Return decoupled b
408             void segregateB
409             (
410                 TypeField& sMul,
411                 const TypeField& x
412             ) const;
415         // Coupled interface functionality
417             //- Initialise the update of coupled interfaces
418             //  for Amul operations
419             void initInterfaces
420             (
421                 const FieldField<CoeffField, Type>& interfaceCoeffs,
422                 TypeField& result,
423                 const TypeField& psi,
424                 const bool switchToLhs = false
425             ) const;
427             //- Update coupled interfaces
428             void updateInterfaces
429             (
430                 const FieldField<CoeffField, Type>& interfaceCoeffs,
431                 TypeField& result,
432                 const TypeField& psi,
433                 const bool switchToLhs = false
434             ) const;
437         // Constraint manipulation
439             //- Set constrained value in a prescribed point
440             void setValue
441             (
442                 const label eqnIndex,
443                 const Type& value
444             );
447         // Residual calculation
449             //- Calculate residual
450             tmp<TypeField> residual
451             (
452                 const TypeField& x
453             ) const;
455             tmp<TypeField> residual
456             (
457                 const TypeField& x,
458                 const TypeField& b
459             ) const;
462         // H-operations
464             //- Return L-U vector-matrix multiplication in row-form
465             tmp<TypeField> H(const TypeField&) const;
467             //- Return L-U  vector-matrix multiplication in off-diagonal form
468             tmp<TypeField> faceH(const TypeField&) const;
471     // Member operators
473         void operator=(const BlockLduMatrix<Type>&);
475         void negate();
477         void operator+=(const BlockLduMatrix<Type>&);
478         void operator-=(const BlockLduMatrix<Type>&);
480         void operator*=(const scalarField&);
481         void operator*=(const scalar);
484     // Ostream operator
486         friend Ostream& operator<< <Type>
487         (
488             Ostream&,
489             const BlockLduMatrix<Type>&
490         );
494 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
496 } // End namespace Foam
498 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
500 #ifdef NoRepository
501 #   include "BlockLduMatrix.C"
502 #   include "BlockLduMatrixOperations.C"
503 #   include "BlockLduMatrixUpdateInterfaces.C"
504 #   include "BlockLduMatrixATmul.C"
505 #   include "BlockLduMatrixHOps.C"
506 #   include "BlockLduMatrixDecouple.C"
507 #   include "BlockLduMatrixDecoupledHOps.C"
508 #endif
510 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512 #endif
514 // ************************************************************************* //