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