OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

lduMatrix.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::lduMatrix
00027 
00028 Description
00029     lduMatrix is a general matrix class in which the coefficients are
00030     stored as three arrays, one for the upper triangle, one for the
00031     lower triangle and a third for the diagonal.
00032 
00033     Addressing arrays must be supplied for the upper and lower triangles.
00034 
00035     It might be better if this class were organised as a hierachy starting
00036     from an empty matrix, then deriving diagonal, symmetric and asymmetric
00037     matrices.
00038 
00039 SourceFiles
00040     lduMatrixATmul.C
00041     lduMatrix.C
00042     lduMatrixTemplates.C
00043     lduMatrixOperations.C
00044     lduMatrixSolver.C
00045     lduMatrixPreconditioner.C
00046     lduMatrixTests.C
00047     lduMatrixUpdateMatrixInterfaces.C
00048 
00049 \*---------------------------------------------------------------------------*/
00050 
00051 #ifndef lduMatrix_H
00052 #define lduMatrix_H
00053 
00054 #include "lduMesh.H"
00055 #include "primitiveFieldsFwd.H"
00056 #include "FieldField.H"
00057 #include "lduInterfaceFieldPtrsList.H"
00058 #include "typeInfo.H"
00059 #include "autoPtr.H"
00060 #include "runTimeSelectionTables.H"
00061 
00062 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00063 
00064 namespace Foam
00065 {
00066 
00067 // Forward declaration of friend functions and operators
00068 
00069 class lduMatrix;
00070 Ostream& operator<<(Ostream&, const lduMatrix&);
00071 
00072 
00073 /*---------------------------------------------------------------------------*\
00074                            Class lduMatrix Declaration
00075 \*---------------------------------------------------------------------------*/
00076 
00077 class lduMatrix
00078 {
00079     // private data
00080 
00081         //- LDU mesh reference
00082         const lduMesh& lduMesh_;
00083 
00084         //- Coefficients (not including interfaces)
00085         scalarField *lowerPtr_, *diagPtr_, *upperPtr_;
00086 
00087 
00088 public:
00089 
00090     //- Class returned by the solver, containing performance statistics
00091     class solverPerformance
00092     {
00093         word   solverName_;
00094         word   fieldName_;
00095         scalar initialResidual_;
00096         scalar finalResidual_;
00097         label  noIterations_;
00098         bool   converged_;
00099         bool   singular_;
00100 
00101 
00102     public:
00103 
00104         // Constructors
00105 
00106             solverPerformance()
00107             :
00108                 initialResidual_(0),
00109                 finalResidual_(0),
00110                 noIterations_(0),
00111                 converged_(false),
00112                 singular_(false)
00113             {}
00114 
00115 
00116             solverPerformance
00117             (
00118                 const word&  solverName,
00119                 const word&  fieldName,
00120                 const scalar iRes = 0,
00121                 const scalar fRes = 0,
00122                 const label  nIter = 0,
00123                 const bool   converged = false,
00124                 const bool   singular = false
00125             )
00126             :
00127                 solverName_(solverName),
00128                 fieldName_(fieldName),
00129                 initialResidual_(iRes),
00130                 finalResidual_(fRes),
00131                 noIterations_(nIter),
00132                 converged_(converged),
00133                 singular_(singular)
00134             {}
00135 
00136 
00137         // Member functions
00138 
00139             //- Return solver name
00140             const word& solverName() const
00141             {
00142                 return solverName_;
00143             }
00144 
00145             //- Return initial residual
00146             scalar initialResidual() const
00147             {
00148                 return initialResidual_;
00149             }
00150 
00151             //- Return initial residual
00152             scalar& initialResidual()
00153             {
00154                 return initialResidual_;
00155             }
00156 
00157 
00158             //- Return final residual
00159             scalar finalResidual() const
00160             {
00161                 return finalResidual_;
00162             }
00163 
00164             //- Return final residual
00165             scalar& finalResidual()
00166             {
00167                 return finalResidual_;
00168             }
00169 
00170 
00171             //- Return number of iterations
00172             label nIterations() const
00173             {
00174                 return noIterations_;
00175             }
00176 
00177             //- Return number of iterations
00178             label& nIterations()
00179             {
00180                 return noIterations_;
00181             }
00182 
00183 
00184             //- Has the solver converged?
00185             bool converged() const
00186             {
00187                 return converged_;
00188             }
00189 
00190             //- Is the matrix singular?
00191             bool singular() const
00192             {
00193                 return singular_;
00194             }
00195 
00196             //- Convergence test
00197             bool checkConvergence
00198             (
00199                 const scalar tolerance,
00200                 const scalar relTolerance
00201             );
00202 
00203             //- Singularity test
00204             bool checkSingularity(const scalar residual);
00205 
00206             //- Print summary of solver performance
00207             void print() const;
00208     };
00209 
00210 
00211     //- Abstract base-class for lduMatrix solvers
00212     class solver
00213     {
00214     protected:
00215 
00216         // Protected data
00217 
00218             word fieldName_;
00219             const lduMatrix& matrix_;
00220             const FieldField<Field, scalar>& interfaceBouCoeffs_;
00221             const FieldField<Field, scalar>& interfaceIntCoeffs_;
00222             lduInterfaceFieldPtrsList interfaces_;
00223 
00224             //- dictionary of controls
00225             dictionary controlDict_;
00226 
00227             //- Maximum number of iterations in the solver
00228             label maxIter_;
00229 
00230             //- Final convergence tolerance
00231             scalar tolerance_;
00232 
00233             //- Convergence tolerance relative to the initial
00234             scalar relTol_;
00235 
00236 
00237         // Protected Member Functions
00238 
00239             //- Read the control parameters from the controlDict_
00240             virtual void readControls();
00241 
00242 
00243     public:
00244 
00245         //- Runtime type information
00246         virtual const word& type() const = 0;
00247 
00248 
00249         // Declare run-time constructor selection tables
00250 
00251             declareRunTimeSelectionTable
00252             (
00253                 autoPtr,
00254                 solver,
00255                 symMatrix,
00256                 (
00257                     const word& fieldName,
00258                     const lduMatrix& matrix,
00259                     const FieldField<Field, scalar>& interfaceBouCoeffs,
00260                     const FieldField<Field, scalar>& interfaceIntCoeffs,
00261                     const lduInterfaceFieldPtrsList& interfaces,
00262                     const dictionary& solverControls
00263                 ),
00264                 (
00265                     fieldName,
00266                     matrix,
00267                     interfaceBouCoeffs,
00268                     interfaceIntCoeffs,
00269                     interfaces,
00270                     solverControls
00271                 )
00272             );
00273 
00274             declareRunTimeSelectionTable
00275             (
00276                 autoPtr,
00277                 solver,
00278                 asymMatrix,
00279                 (
00280                     const word& fieldName,
00281                     const lduMatrix& matrix,
00282                     const FieldField<Field, scalar>& interfaceBouCoeffs,
00283                     const FieldField<Field, scalar>& interfaceIntCoeffs,
00284                     const lduInterfaceFieldPtrsList& interfaces,
00285                     const dictionary& solverControls
00286                 ),
00287                 (
00288                     fieldName,
00289                     matrix,
00290                     interfaceBouCoeffs,
00291                     interfaceIntCoeffs,
00292                     interfaces,
00293                     solverControls
00294                 )
00295             );
00296 
00297 
00298         // Constructors
00299 
00300             solver
00301             (
00302                 const word& fieldName,
00303                 const lduMatrix& matrix,
00304                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00305                 const FieldField<Field, scalar>& interfaceIntCoeffs,
00306                 const lduInterfaceFieldPtrsList& interfaces,
00307                 const dictionary& solverControls
00308             );
00309 
00310         // Selectors
00311 
00312             //- Return a new solver
00313             static autoPtr<solver> New
00314             (
00315                 const word& fieldName,
00316                 const lduMatrix& matrix,
00317                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00318                 const FieldField<Field, scalar>& interfaceIntCoeffs,
00319                 const lduInterfaceFieldPtrsList& interfaces,
00320                 const dictionary& solverControls
00321             );
00322 
00323 
00324 
00325         // Destructor
00326 
00327             virtual ~solver()
00328             {}
00329 
00330 
00331         // Member functions
00332 
00333             // Access
00334 
00335                 const word& fieldName() const
00336                 {
00337                     return fieldName_;
00338                 }
00339 
00340                 const lduMatrix& matrix() const
00341                 {
00342                     return matrix_;
00343                 }
00344 
00345                  const FieldField<Field, scalar>& interfaceBouCoeffs() const
00346                  {
00347                      return interfaceBouCoeffs_;
00348                  }
00349 
00350                  const FieldField<Field, scalar>& interfaceIntCoeffs() const
00351                  {
00352                      return interfaceIntCoeffs_;
00353                  }
00354 
00355                  const lduInterfaceFieldPtrsList& interfaces() const
00356                  {
00357                      return interfaces_;
00358                  }
00359 
00360 
00361             //- Read and reset the solver parameters from the given stream
00362             virtual void read(const dictionary&);
00363 
00364             virtual solverPerformance solve
00365             (
00366                 scalarField& psi,
00367                 const scalarField& source,
00368                 const direction cmpt=0
00369             ) const = 0;
00370 
00371             //- Return the matrix norm used to normalise the residual for the
00372             //  stopping criterion
00373             scalar normFactor
00374             (
00375                 const scalarField& psi,
00376                 const scalarField& source,
00377                 const scalarField& Apsi,
00378                 scalarField& tmpField
00379             ) const;
00380     };
00381 
00382 
00383     //- Abstract base-class for lduMatrix smoothers
00384     class smoother
00385     {
00386     protected:
00387 
00388         // Protected data
00389 
00390             word fieldName_;
00391             const lduMatrix& matrix_;
00392             const FieldField<Field, scalar>& interfaceBouCoeffs_;
00393             const FieldField<Field, scalar>& interfaceIntCoeffs_;
00394             const lduInterfaceFieldPtrsList& interfaces_;
00395 
00396 
00397     public:
00398 
00399         //- Find the smoother name (directly or from a sub-dictionary)
00400         static word getName(const dictionary&);
00401 
00402         //- Runtime type information
00403         virtual const word& type() const = 0;
00404 
00405 
00406         // Declare run-time constructor selection tables
00407 
00408             declareRunTimeSelectionTable
00409             (
00410                 autoPtr,
00411                 smoother,
00412                 symMatrix,
00413                 (
00414                     const word& fieldName,
00415                     const lduMatrix& matrix,
00416                     const FieldField<Field, scalar>& interfaceBouCoeffs,
00417                     const FieldField<Field, scalar>& interfaceIntCoeffs,
00418                     const lduInterfaceFieldPtrsList& interfaces
00419                 ),
00420                 (
00421                     fieldName,
00422                     matrix,
00423                     interfaceBouCoeffs,
00424                     interfaceIntCoeffs,
00425                     interfaces
00426                 )
00427             );
00428 
00429             declareRunTimeSelectionTable
00430             (
00431                 autoPtr,
00432                 smoother,
00433                 asymMatrix,
00434                 (
00435                     const word& fieldName,
00436                     const lduMatrix& matrix,
00437                     const FieldField<Field, scalar>& interfaceBouCoeffs,
00438                     const FieldField<Field, scalar>& interfaceIntCoeffs,
00439                     const lduInterfaceFieldPtrsList& interfaces
00440                 ),
00441                 (
00442                     fieldName,
00443                     matrix,
00444                     interfaceBouCoeffs,
00445                     interfaceIntCoeffs,
00446                     interfaces
00447                 )
00448             );
00449 
00450 
00451         // Constructors
00452 
00453             smoother
00454             (
00455                 const word& fieldName,
00456                 const lduMatrix& matrix,
00457                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00458                 const FieldField<Field, scalar>& interfaceIntCoeffs,
00459                 const lduInterfaceFieldPtrsList& interfaces
00460             );
00461 
00462 
00463         // Selectors
00464 
00465             //- Return a new smoother
00466             static autoPtr<smoother> New
00467             (
00468                 const word& fieldName,
00469                 const lduMatrix& matrix,
00470                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00471                 const FieldField<Field, scalar>& interfaceIntCoeffs,
00472                 const lduInterfaceFieldPtrsList& interfaces,
00473                 const dictionary& solverControls
00474             );
00475 
00476 
00477         // Destructor
00478 
00479             virtual ~smoother()
00480             {}
00481 
00482 
00483         // Member functions
00484 
00485             // Access
00486 
00487                 const word& fieldName() const
00488                 {
00489                     return fieldName_;
00490                 }
00491 
00492                 const lduMatrix& matrix() const
00493                 {
00494                     return matrix_;
00495                 }
00496 
00497                  const FieldField<Field, scalar>& interfaceBouCoeffs() const
00498                  {
00499                      return interfaceBouCoeffs_;
00500                  }
00501 
00502                  const FieldField<Field, scalar>& interfaceIntCoeffs() const
00503                  {
00504                      return interfaceIntCoeffs_;
00505                  }
00506 
00507                  const lduInterfaceFieldPtrsList& interfaces() const
00508                  {
00509                      return interfaces_;
00510                  }
00511 
00512 
00513             //- Smooth the solution for a given number of sweeps
00514             virtual void smooth
00515             (
00516                 scalarField& psi,
00517                 const scalarField& source,
00518                 const direction cmpt,
00519                 const label nSweeps
00520             ) const = 0;
00521     };
00522 
00523 
00524     //- Abstract base-class for lduMatrix preconditioners
00525     class preconditioner
00526     {
00527     protected:
00528 
00529         // Protected data
00530 
00531             //- Reference to the base-solver this preconditioner is used with
00532             const solver& solver_;
00533 
00534 
00535     public:
00536 
00537         //- Find the preconditioner name (directly or from a sub-dictionary)
00538         static word getName(const dictionary&);
00539 
00540         //- Runtime type information
00541         virtual const word& type() const = 0;
00542 
00543 
00544         // Declare run-time constructor selection tables
00545 
00546             declareRunTimeSelectionTable
00547             (
00548                 autoPtr,
00549                 preconditioner,
00550                 symMatrix,
00551                 (
00552                     const solver& sol,
00553                     const dictionary& solverControls
00554                 ),
00555                 (sol, solverControls)
00556             );
00557 
00558             declareRunTimeSelectionTable
00559             (
00560                 autoPtr,
00561                 preconditioner,
00562                 asymMatrix,
00563                 (
00564                     const solver& sol,
00565                     const dictionary& solverControls
00566                 ),
00567                 (sol, solverControls)
00568             );
00569 
00570 
00571         // Constructors
00572 
00573             preconditioner
00574             (
00575                 const solver& sol
00576             )
00577             :
00578                 solver_(sol)
00579             {}
00580 
00581 
00582         // Selectors
00583 
00584             //- Return a new preconditioner
00585             static autoPtr<preconditioner> New
00586             (
00587                 const solver& sol,
00588                 const dictionary& solverControls
00589             );
00590 
00591 
00592         // Destructor
00593 
00594             virtual ~preconditioner()
00595             {}
00596 
00597 
00598         // Member functions
00599 
00600             //- Read and reset the preconditioner parameters
00601             //  from the given stream
00602             virtual void read(const dictionary&)
00603             {}
00604 
00605             //- Return wA the preconditioned form of residual rA
00606             virtual void precondition
00607             (
00608                 scalarField& wA,
00609                 const scalarField& rA,
00610                 const direction cmpt=0
00611             ) const = 0;
00612 
00613             //- Return wT the transpose-matrix preconditioned form of
00614             //  residual rT.
00615             //  This is only required for preconditioning asymmetric matrices.
00616             virtual void preconditionT
00617             (
00618                 scalarField& wT,
00619                 const scalarField& rT,
00620                 const direction cmpt=0
00621             ) const
00622             {
00623                 notImplemented
00624                 (
00625                     type() +"::preconditionT"
00626                     "(scalarField& wT, const scalarField& rT, "
00627                     "const direction cmpt)"
00628                 );
00629             }
00630     };
00631 
00632 
00633     // Static data
00634 
00635         // Declare name of the class and its debug switch
00636         ClassName("lduMatrix");
00637 
00638         //- Large scalar for the use in solvers
00639         static const scalar great_;
00640 
00641         //- Small scalar for the use in solvers
00642         static const scalar small_;
00643 
00644 
00645     // Constructors
00646 
00647         //- Construct given an LDU addressed mesh.
00648         //  The coefficients are initially empty for subsequent setting.
00649         lduMatrix(const lduMesh&);
00650 
00651         //- Construct as copy
00652         lduMatrix(const lduMatrix&);
00653 
00654         //- Construct as copy or re-use as specified.
00655         lduMatrix(lduMatrix&, bool reUse);
00656 
00657         //- Construct given an LDU addressed mesh and an Istream
00658         //  from which the coefficients are read
00659         lduMatrix(const lduMesh&, Istream&);
00660 
00661 
00662     // Destructor
00663 
00664         ~lduMatrix();
00665 
00666 
00667     // Member functions
00668 
00669         // Access to addressing
00670 
00671             //- Return the LDU mesh from which the addressing is obtained
00672             const lduMesh& mesh() const
00673             {
00674                 return lduMesh_;
00675             }
00676 
00677             //- Return the LDU addressing
00678             const lduAddressing& lduAddr() const
00679             {
00680                 return lduMesh_.lduAddr();
00681             }
00682 
00683             //- Return the patch evaluation schedule
00684             const lduSchedule& patchSchedule() const
00685             {
00686                 return lduAddr().patchSchedule();
00687             }
00688 
00689 
00690         // Access to coefficients
00691 
00692             scalarField& lower();
00693             scalarField& diag();
00694             scalarField& upper();
00695 
00696             const scalarField& lower() const;
00697             const scalarField& diag() const;
00698             const scalarField& upper() const;
00699 
00700             bool hasDiag() const
00701             {
00702                 return (diagPtr_);
00703             }
00704 
00705             bool hasUpper() const
00706             {
00707                 return (upperPtr_);
00708             }
00709 
00710             bool hasLower() const
00711             {
00712                 return (lowerPtr_);
00713             }
00714 
00715             bool diagonal() const
00716             {
00717                 return (diagPtr_ && !lowerPtr_ && !upperPtr_);
00718             }
00719 
00720             bool symmetric() const
00721             {
00722                 return (diagPtr_ && (!lowerPtr_ && upperPtr_));
00723             }
00724 
00725             bool asymmetric() const
00726             {
00727                 return (diagPtr_ && lowerPtr_ && upperPtr_);
00728             }
00729 
00730 
00731         // operations
00732 
00733             void sumDiag();
00734             void negSumDiag();
00735 
00736             void sumMagOffDiag(scalarField& sumOff) const;
00737 
00738             //- Matrix multiplication with updated interfaces.
00739             void Amul
00740             (
00741                 scalarField&,
00742                 const tmp<scalarField>&,
00743                 const FieldField<Field, scalar>&,
00744                 const lduInterfaceFieldPtrsList&,
00745                 const direction cmpt
00746             ) const;
00747 
00748             //- Matrix transpose multiplication with updated interfaces.
00749             void Tmul
00750             (
00751                 scalarField&,
00752                 const tmp<scalarField>&,
00753                 const FieldField<Field, scalar>&,
00754                 const lduInterfaceFieldPtrsList&,
00755                 const direction cmpt
00756             ) const;
00757 
00758 
00759             //- Sum the coefficients on each row of the matrix
00760             void sumA
00761             (
00762                 scalarField&,
00763                 const FieldField<Field, scalar>&,
00764                 const lduInterfaceFieldPtrsList&
00765             ) const;
00766 
00767 
00768             void residual
00769             (
00770                 scalarField& rA,
00771                 const scalarField& psi,
00772                 const scalarField& source,
00773                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00774                 const lduInterfaceFieldPtrsList& interfaces,
00775                 const direction cmpt
00776             ) const;
00777 
00778             tmp<scalarField> residual
00779             (
00780                 const scalarField& psi,
00781                 const scalarField& source,
00782                 const FieldField<Field, scalar>& interfaceBouCoeffs,
00783                 const lduInterfaceFieldPtrsList& interfaces,
00784                 const direction cmpt
00785             ) const;
00786 
00787 
00788             //- Initialise the update of interfaced interfaces
00789             //  for matrix operations
00790             void initMatrixInterfaces
00791             (
00792                 const FieldField<Field, scalar>& interfaceCoeffs,
00793                 const lduInterfaceFieldPtrsList& interfaces,
00794                 const scalarField& psiif,
00795                 scalarField& result,
00796                 const direction cmpt
00797             ) const;
00798 
00799             //- Update interfaced interfaces for matrix operations
00800             void updateMatrixInterfaces
00801             (
00802                 const FieldField<Field, scalar>& interfaceCoeffs,
00803                 const lduInterfaceFieldPtrsList& interfaces,
00804                 const scalarField& psiif,
00805                 scalarField& result,
00806                 const direction cmpt
00807             ) const;
00808 
00809 
00810             template<class Type>
00811             tmp<Field<Type> > H(const Field<Type>&) const;
00812 
00813             template<class Type>
00814             tmp<Field<Type> > H(const tmp<Field<Type> >&) const;
00815 
00816             tmp<scalarField> H1() const;
00817 
00818             template<class Type>
00819             tmp<Field<Type> > faceH(const Field<Type>&) const;
00820 
00821             template<class Type>
00822             tmp<Field<Type> > faceH(const tmp<Field<Type> >&) const;
00823 
00824 
00825     // Member operators
00826 
00827         void operator=(const lduMatrix&);
00828 
00829         void negate();
00830 
00831         void operator+=(const lduMatrix&);
00832         void operator-=(const lduMatrix&);
00833 
00834         void operator*=(const scalarField&);
00835         void operator*=(scalar);
00836 
00837 
00838     // Ostream operator
00839 
00840         friend Ostream& operator<<(Ostream&, const lduMatrix&);
00841 };
00842 
00843 
00844 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00845 
00846 } // End namespace Foam
00847 
00848 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00849 
00850 #ifdef NoRepository
00851 #   include "lduMatrixTemplates.C"
00852 #endif
00853 
00854 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00855 
00856 #endif
00857 
00858 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd