/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . Class Foam::fvMatrix Description A special matrix type and solver, designed for finite volume solutions of scalar equations. Face addressing is used to make all matrix assembly and solution loops vectorise. SourceFiles fvMatrix.C fvMatrixSolve.C fvScalarMatrix.C \*---------------------------------------------------------------------------*/ #ifndef fvMatrix_H #define fvMatrix_H #include "volFields.H" #include "surfaceFields.H" #include "lduMatrix.H" #include "tmp.H" #include "autoPtr.H" #include "dimensionedTypes.H" #include "zero.H" #include "className.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { // Forward declaration of friend functions and operators template class fvMatrix; template tmp> operator& ( const fvMatrix&, const VolInternalField& ); template tmp> operator& ( const fvMatrix&, const tmp>& ); template tmp> operator& ( const tmp>&, const VolInternalField& ); template tmp> operator& ( const tmp>&, const tmp>& ); template Ostream& operator<<(Ostream&, const fvMatrix&); template class UIndirectList; /*---------------------------------------------------------------------------*\ Class fvMatrix Declaration \*---------------------------------------------------------------------------*/ template class fvMatrix : public tmp>::refCount, public lduMatrix { // Private Data //- Const reference to VolField // Converted into a non-const reference at the point of solution. const VolField& psi_; //- Dimension set dimensionSet dimensions_; //- Source term Field source_; //- Boundary scalar field containing pseudo-matrix coeffs // for internal cells Field> internalCoeffs_; //- Boundary scalar field containing pseudo-matrix coeffs // for boundary cells Field> boundaryCoeffs_; //- Face flux field for non-orthogonal correction mutable SurfaceField* faceFluxCorrectionPtr_; protected: //- Declare friendship with the fvSolver class friend class fvSolver; // Protected Member Functions //- Add patch contribution to internal field template void addToInternalField ( const labelUList& addr, const Field& pf, Field& intf ) const; template void addToInternalField ( const labelUList& addr, const tmp>& tpf, Field& intf ) const; //- Subtract patch contribution from internal field template void subtractFromInternalField ( const labelUList& addr, const Field& pf, Field& intf ) const; template void subtractFromInternalField ( const labelUList& addr, const tmp>& tpf, Field& intf ) const; // Matrix completion functionality void addBoundaryDiag ( scalarField& diag, const direction cmpt ) const; void addCmptAvBoundaryDiag(scalarField& diag) const; void addBoundarySource ( Field& source, const bool couples=true ) const; // Matrix manipulation functionality //- Set solution in the given cell to the specified value void setValue ( const label celli, const Type& value ); //- Set solution in the given cell to the specified value void setValue ( const label celli, const Type& value, const scalar fraction, const scalarField& ddtDiag ); public: //- Solver class returned by the solver function // used for systems in which it is useful to cache the solver for reuse // e.g. if the solver is potentially expensive to construct (AMG) and can // be used several times (PISO) class fvSolver { fvMatrix& fvMat_; autoPtr solver_; public: // Constructors fvSolver(fvMatrix& fvMat, autoPtr sol) : fvMat_(fvMat), solver_(sol) {} // Member Functions //- Solve returning the solution statistics. // Use the given solver controls SolverPerformance solve(const dictionary&); //- Solve returning the solution statistics. // Solver controls read from fvSolution SolverPerformance solve(); }; ClassName("fvMatrix"); // Constructors //- Construct given a field to solve for fvMatrix ( const VolField&, const dimensionSet& ); //- Copy constructor fvMatrix(const fvMatrix&); //- Copy constructor of tmp> deleting argument fvMatrix(const tmp>&); //- Construct from Istream given field to solve for fvMatrix(const VolField&, Istream&); //- Clone tmp> clone() const; //- Destructor virtual ~fvMatrix(); // Member Functions // Access VolField& psi() { return const_cast&> ( psi_ ); } const VolField& psi() const { return psi_; } const fvMesh& mesh() const { return psi_.mesh(); } const dimensionSet& dimensions() const { return dimensions_; } Field& source() { return source_; } const Field& source() const { return source_; } //- fvBoundary scalar field containing pseudo-matrix coeffs // for internal cells Field>& internalCoeffs() { return internalCoeffs_; } //- fvBoundary scalar field containing pseudo-matrix coeffs // for boundary cells Field>& boundaryCoeffs() { return boundaryCoeffs_; } //- Return pointer to face-flux non-orthogonal correction field SurfaceField*& faceFluxCorrectionPtr() { return faceFluxCorrectionPtr_; } // Operations //- Set solution in given cells to the specified values // and eliminate the corresponding equations from the matrix. template class ListType> void setValues ( const labelUList& cells, const ListType& values ); //- Set solution in given cells to the specified values // and eliminate the corresponding equations from the matrix. template class ListType> void setValues ( const labelUList& cells, const ListType& values, const scalarList& fractions, const bool hasDdt = true ); //- Set reference level for solution void setReference ( const label celli, const Type& value, const bool forceReference = false ); //- Set reference level for a component of the solution // on a given patch face void setComponentReference ( const label patchi, const label facei, const direction cmpt, const scalar value ); //- Return the relaxation factor for this equation scalar relaxationFactor() const; //- Relax matrix (for steady-state solution). // alpha = 1 : diagonally equal // alpha < 1 : diagonally dominant // alpha = 0 : do nothing // Note: Requires positive diagonal. void relax(const scalar alpha); //- Relax matrix (for steady-state solution). // alpha is read from controlDict void relax(); //- Manipulate based on a boundary field void boundaryManipulate ( typename VolField::Boundary& values ); //- Construct and return the solver // Use the given solver controls autoPtr solver(const dictionary&); //- Construct and return the solver // Solver controls read from fvSolution autoPtr solver(); //- Solve segregated or coupled returning the solution statistics. // Use the given solver controls SolverPerformance solve(const dictionary&); //- Solve segregated returning the solution statistics. // Use the given solver controls SolverPerformance solveSegregated(const dictionary&); //- Solve coupled returning the solution statistics. // Use the given solver controls SolverPerformance solveCoupled(const dictionary&); //- Solve segregated or coupled returning the solution statistics. // Solver controls read from fvSolution SolverPerformance solve(const word& name); //- Solve returning the solution statistics. // Solver controls read from fvSolution SolverPerformance solve(); //- Return the matrix residual tmp> residual() const; //- Return the matrix scalar diagonal tmp D() const; //- Return the matrix Type diagonal tmp> DD() const; //- Return the central coefficient tmp A() const; //- Return the explicit source tmp> Su() const; //- Return the implicit source tmp Sp() const; //- Return the H operation source tmp> H() const; //- Return H(1) tmp H1() const; //- Return the face-flux field from the matrix tmp> flux() const; // Member Operators void operator=(const fvMatrix&); void operator=(const tmp>&); void negate(); void operator+=(const fvMatrix&); void operator+=(const tmp>&); void operator-=(const fvMatrix&); void operator-=(const tmp>&); void operator+= ( const VolInternalField& ); //- Assign-add an expression template> void operator+=(const Expression& e); void operator+= ( const tmp>& ); void operator+= ( const tmp>& ); void operator-= ( const VolInternalField& ); //- Assign-subtract an expression template> void operator-=(const Expression& e); void operator-= ( const tmp>& ); void operator-= ( const tmp>& ); void operator+=(const dimensioned&); void operator-=(const dimensioned&); void operator+=(const zero&); void operator-=(const zero&); void operator*=(const volInternalScalarField&); //- Assign-multiply by an expression template> void operator*=(const Expression& e); void operator*=(const tmp&); void operator*=(const tmp&); void operator*=(const dimensioned&); void operator/=(const volInternalScalarField&); //- Assign-divide by an expression template> void operator/=(const Expression& e); void operator/=(const tmp&); void operator/=(const tmp&); void operator/=(const dimensioned&); // Friend operators friend tmp> operator& ( const fvMatrix&, const VolInternalField& ); friend tmp> operator& ( const fvMatrix&, const tmp>& ); friend tmp> operator& ( const tmp>&, const VolInternalField& ); friend tmp> operator& ( const tmp>&, const tmp>& ); // Ostream operator friend Ostream& operator<< ( Ostream&, const fvMatrix& ); }; namespace expression { template struct Expressionable> : public notExpressionable {}; } // * * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * // template void checkMethod ( const fvMatrix&, const fvMatrix&, const char* ); template void checkMethod ( const fvMatrix&, const VolInternalField&, const char* ); template void checkMethod ( const fvMatrix&, const dimensioned& dt, const char* ); template> void checkMethod ( const fvMatrix&, const Expression&, const char* ); //- Solve returning the solution statistics given convergence tolerance // Use the given solver controls template SolverPerformance solve(fvMatrix&, const word&); //- Solve returning the solution statistics given convergence tolerance, // deleting temporary matrix after solution. // Use the given solver controls template SolverPerformance solve ( const tmp>&, const word& ); //- Solve returning the solution statistics given convergence tolerance // Solver controls read fvSolution template SolverPerformance solve(fvMatrix&); //- Solve returning the solution statistics given convergence tolerance, // deleting temporary matrix after solution. // Solver controls read fvSolution template SolverPerformance solve(const tmp>&); //- Return the correction form of the given matrix // by subtracting the matrix multiplied by the current field template tmp> correction(const fvMatrix&); //- Return the correction form of the given temporary matrix // by subtracting the matrix multiplied by the current field template tmp> correction(const tmp>&); // * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * // template tmp> operator== ( const fvMatrix&, const fvMatrix& ); template tmp> operator== ( const tmp>&, const fvMatrix& ); template tmp> operator== ( const fvMatrix&, const tmp>& ); template tmp> operator== ( const tmp>&, const tmp>& ); template tmp> operator== ( const fvMatrix&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator== ( const fvMatrix&, const Expression& e ); template tmp> operator== ( const fvMatrix&, const tmp>& ); template tmp> operator== ( const fvMatrix&, const tmp>& ); template tmp> operator== ( const tmp>&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator== ( const tmp>&, const Expression& e ); template tmp> operator== ( const tmp>&, const tmp>& ); template tmp> operator== ( const tmp>&, const tmp>& ); template tmp> operator== ( const fvMatrix&, const dimensioned& ); template tmp> operator== ( const tmp>&, const dimensioned& ); template tmp> operator== ( const fvMatrix&, const zero& ); template tmp> operator== ( const tmp>&, const zero& ); template tmp> operator- ( const fvMatrix& ); template tmp> operator- ( const tmp>& ); template tmp> operator+ ( const fvMatrix&, const fvMatrix& ); template tmp> operator+ ( const tmp>&, const fvMatrix& ); template tmp> operator+ ( const fvMatrix&, const tmp>& ); template tmp> operator+ ( const tmp>&, const tmp>& ); template tmp> operator+ ( const fvMatrix&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator+ ( const fvMatrix&, const Expression& e ); template tmp> operator+ ( const fvMatrix&, const tmp>& ); template tmp> operator+ ( const fvMatrix&, const tmp>& ); template tmp> operator+ ( const tmp>&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator+ ( const tmp>&, const Expression& e ); template tmp> operator+ ( const tmp>&, const tmp>& ); template tmp> operator+ ( const tmp>&, const tmp>& ); template tmp> operator+ ( const VolInternalField&, const fvMatrix& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator+ ( const Expression& e, const fvMatrix& ); template tmp> operator+ ( const tmp>&, const fvMatrix& ); template tmp> operator+ ( const tmp>&, const fvMatrix& ); template tmp> operator+ ( const VolInternalField&, const tmp>& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator+ ( const Expression& e, const tmp>& ); template tmp> operator+ ( const tmp>&, const tmp>& ); template tmp> operator+ ( const tmp>&, const tmp>& ); template tmp> operator+ ( const fvMatrix&, const dimensioned& ); template tmp> operator+ ( const tmp>&, const dimensioned& ); template tmp> operator+ ( const dimensioned&, const fvMatrix& ); template tmp> operator+ ( const dimensioned&, const tmp>& ); template tmp> operator- ( const fvMatrix&, const fvMatrix& ); template tmp> operator- ( const tmp>&, const fvMatrix& ); template tmp> operator- ( const fvMatrix&, const tmp>& ); template tmp> operator- ( const tmp>&, const tmp>& ); template tmp> operator- ( const fvMatrix&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator- ( const fvMatrix&, const Expression& e ); template tmp> operator- ( const fvMatrix&, const tmp>& ); template tmp> operator- ( const fvMatrix&, const tmp>& ); template tmp> operator- ( const tmp>&, const VolInternalField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator- ( const tmp>&, const Expression& e ); template tmp> operator- ( const tmp>&, const tmp>& ); template tmp> operator- ( const tmp>&, const tmp>& ); template tmp> operator- ( const VolInternalField&, const fvMatrix& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator- ( const Expression& e, const fvMatrix& ); template tmp> operator- ( const tmp>&, const fvMatrix& ); template tmp> operator- ( const tmp>&, const fvMatrix& ); template tmp> operator- ( const VolInternalField&, const tmp>& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator- ( const Expression& e, const tmp>& ); template tmp> operator- ( const tmp>&, const tmp>& ); template tmp> operator- ( const tmp>&, const tmp>& ); template tmp> operator- ( const fvMatrix&, const dimensioned& ); template tmp> operator- ( const tmp>&, const dimensioned& ); template tmp> operator- ( const dimensioned&, const fvMatrix& ); template tmp> operator- ( const dimensioned&, const tmp>& ); template tmp> operator* ( const volInternalScalarField&, const fvMatrix& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator* ( const Expression& e, const fvMatrix& ); template tmp> operator* ( const tmp&, const fvMatrix& ); template tmp> operator* ( const tmp&, const fvMatrix& ); template tmp> operator* ( const volInternalScalarField&, const tmp>& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator* ( const Expression& e, const tmp>& ); template tmp> operator* ( const tmp&, const tmp>& ); template tmp> operator* ( const tmp&, const tmp>& ); template tmp> operator* ( const dimensioned&, const fvMatrix& ); template tmp> operator* ( const dimensioned&, const tmp>& ); template tmp> operator/ ( const fvMatrix&, const volInternalScalarField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator/ ( const fvMatrix&, const Expression& e ); template tmp> operator/ ( const fvMatrix&, const tmp& ); template tmp> operator/ ( const fvMatrix&, const tmp& ); template tmp> operator/ ( const tmp>&, const volInternalScalarField& ); template < class Type, class Expression, class = EnableIfExpression > tmp> operator/ ( const tmp>&, const Expression& e ); template tmp> operator/ ( const tmp>&, const tmp& ); template tmp> operator/ ( const tmp>&, const tmp& ); template tmp> operator/ ( const fvMatrix&, const dimensioned& ); template tmp> operator/ ( const tmp>&, const dimensioned& ); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository #include "fvMatrix.C" #endif // Specialisation for scalars #include "fvScalarMatrix.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif // ************************************************************************* //