diff --git a/src/OpenFOAM/matrices/Matrix/Matrix.C b/src/OpenFOAM/matrices/Matrix/Matrix.C index 808e00b86c..46508d8e3f 100644 --- a/src/OpenFOAM/matrices/Matrix/Matrix.C +++ b/src/OpenFOAM/matrices/Matrix/Matrix.C @@ -30,7 +30,7 @@ License template void Foam::Matrix::allocate() { - if (nRows_ && nCols_) + if (mRows_ && nCols_) { v_ = new Type[size()]; } @@ -42,14 +42,14 @@ void Foam::Matrix::allocate() template Foam::Matrix::Matrix(const label m, const label n) : - nRows_(m), + mRows_(m), nCols_(n), v_(NULL) { - if (nRows_ < 0 || nCols_ < 0) + if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction - << "Bad m, n " << nRows_ << ", " << nCols_ + << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } @@ -60,14 +60,14 @@ Foam::Matrix::Matrix(const label m, const label n) template Foam::Matrix::Matrix(const label m, const label n, const zero) : - nRows_(m), + mRows_(m), nCols_(n), v_(NULL) { - if (nRows_ < 0 || nCols_ < 0) + if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction - << "Bad m, n " << nRows_ << ", " << nCols_ + << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } @@ -85,16 +85,16 @@ Foam::Matrix::Matrix(const label m, const label n, const zero) template -Foam::Matrix::Matrix(const label m, const label n, const Type& a) +Foam::Matrix::Matrix(const label m, const label n, const Type& s) : - nRows_(m), + mRows_(m), nCols_(n), v_(NULL) { - if (nRows_ < 0 || nCols_ < 0) + if (mRows_ < 0 || nCols_ < 0) { FatalErrorInFunction - << "Bad m, n " << nRows_ << ", " << nCols_ + << "Incorrect m, n " << mRows_ << ", " << nCols_ << abort(FatalError); } @@ -105,45 +105,92 @@ Foam::Matrix::Matrix(const label m, const label n, const Type& a) const label mn = size(); for (label i=0; i -inline Foam::Matrix::Matrix(const mType::Block& block) +Foam::Matrix::Matrix(const Matrix& M) : - nRows_(block.m()), - nCols_(block.n()) -{ - allocate(); - - for (label i=0; i -Foam::Matrix::Matrix(const Matrix& a) -: - nRows_(a.nRows_), - nCols_(a.nCols_), + mRows_(M.mRows_), + nCols_(M.nCols_), v_(NULL) { - if (a.v_) + if (M.v_) { allocate(); const label mn = size(); for (label i=0; i +template +Foam::Matrix::Matrix(const Matrix& M) +: + mRows_(M.m()), + nCols_(M.n()), + v_(NULL) +{ + if (M.v()) + { + allocate(); + + const label mn = size(); + for (label i=0; i +template +inline Foam::Matrix::Matrix +( + const ConstMatrixBlock& Mb +) +: + mRows_(Mb.m()), + nCols_(Mb.n()) +{ + allocate(); + + for (label i=0; i +template +inline Foam::Matrix::Matrix +( + const MatrixBlock& Mb +) +: + mRows_(Mb.m()), + nCols_(Mb.n()) +{ + allocate(); + + for (label i=0; i::clear() v_ = NULL; } - nRows_ = 0; + mRows_ = 0; nCols_ = 0; } template -void Foam::Matrix::transfer(Matrix& a) +void Foam::Matrix::transfer(Matrix& M) { clear(); - nRows_ = a.nRows_; - a.nRows_ = 0; + mRows_ = M.mRows_; + M.mRows_ = 0; - nCols_ = a.nCols_; - a.nCols_ = 0; + nCols_ = M.nCols_; + M.nCols_ = 0; - v_ = a.v_; - a.v_ = NULL; + v_ = M.v_; + M.v_ = NULL; } @@ -198,7 +245,7 @@ void Foam::Matrix::setSize(const label m, const label n) { mType newMatrix(m, n, Zero); - label minM = min(m, nRows_); + label minM = min(m, mRows_); label minN = min(n, nCols_); for (label i=0; i::T() const } -template -Foam::Matrix::ConstBlock::operator Field() const -{ - if (nCols_ != 1) - { - FatalErrorInFunction - << "Number of columns " << nCols_ << " != 1" - << abort(FatalError); - } - - Field f(nRows_); - - forAll(f, i) - { - f[i] = operator()(i, 0); - } - - return f; -} - - -template -Foam::Matrix::Block::operator Field() const -{ - if (nCols_ != 1) - { - FatalErrorInFunction - << "Number of columns " << nCols_ << " != 1" - << abort(FatalError); - } - - Field f(nRows_); - - forAll(f, i) - { - f[i] = operator()(i, 0); - } - - return f; -} - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template -void Foam::Matrix::operator=(const Matrix& a) +void Foam::Matrix::operator=(const Matrix& M) { - if (this == &a) + if (this == &M) { FatalErrorInFunction << "Attempted assignment to self" << abort(FatalError); } - if (nRows_ != a.nRows_ || nCols_ != a.nCols_) + if (mRows_ != M.mRows_ || nCols_ != M.nCols_) { clear(); - nRows_ = a.nRows_; - nCols_ = a.nCols_; + mRows_ = M.mRows_; + nCols_ = M.nCols_; allocate(); } @@ -298,106 +303,55 @@ void Foam::Matrix::operator=(const Matrix& a) const label mn = size(); for (label i=0; i -void Foam::Matrix::operator=(const mType::Block& block) +template +void Foam::Matrix::operator= +( + const ConstMatrixBlock& Mb +) { - for (label i=0; i -void Foam::Matrix::Block::operator=(const Block& block) +template +void Foam::Matrix::operator= +( + const MatrixBlock& Mb +) { - if (this != &block) - { - if (nRows_ != block.m() || nCols_ != block.n()) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << block.m() << "x" << block.n() - << abort(FatalError); - } - - for (label i=0; i -void Foam::Matrix::Block::operator=(const ConstBlock& block) -{ - if (this != &block) - { - if (nRows_ != block.m() || nCols_ != block.n()) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << block.m() << "x" << block.n() - << abort(FatalError); - } - - for (label i=0; i -void Foam::Matrix::Block::operator=(const mType& block) -{ - if (nRows_ != block.m() || nCols_ != block.n()) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << block.m() << "x" << block.n() - << abort(FatalError); - } - - for (label i=0; i -void Foam::Matrix::operator=(const Type& t) +void Foam::Matrix::operator=(const Type& s) { if (v_) { const label mn = size(); for (label i=0; i::operator=(const zero) } -template -template -< - template class MSBlock, - class SubTensor, - Foam::direction BRowStart, - Foam::direction BColStart -> -void Foam::Matrix::Block::operator= -( - const MSBlock& block -) -{ - if (nRows_ != block.nRows || nCols_ != block.nCols) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << block.nRows << "x" << block.nCols - << abort(FatalError); - } - - for (direction i=0; i -template -< - template class VSBlock, - class SubVector, - Foam::direction BStart -> -void Foam::Matrix::Block::operator= -( - const VSBlock& block -) -{ - if (nRows_ != block.nComponents || nCols_ != 1) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << block.nComponents << "x" << 1 - << abort(FatalError); - } - - for (direction i=0; i -template -void Foam::Matrix::Block::operator= -( - const MatrixSpace& ms -) -{ - if (nRows_ != Nrows || nCols_ != Ncols) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << Nrows << "x" << Ncols - << abort(FatalError); - } - - for (label i=0; i -template -void Foam::Matrix::Block::operator= -( - const VectorSpace& ms -) -{ - if (nRows_ != Ncmpts || nCols_ != 1) - { - FatalErrorInFunction - << "Attempt to assign blocks of different sizes: " - << nRows_ << "x" << nCols_ << " != " - << Ncmpts << "x" << 1 - << abort(FatalError); - } - - for (direction i=0; i -void Foam::Matrix::Block::operator=(const Field& f) -{ - if (nRows_ != f.size() || nCols_ != 1) - { - FatalErrorInFunction - << "Error: cannot assign blocks of different size (left is " - << nRows_ << "x" << nCols_ << " != " - << f.size() << "x" << 1 - << abort(FatalError); - } - - forAll(f, i) - { - operator()(i, 0) = f[i]; - } -} - - // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template -const Type& Foam::max(const Matrix& a) +const Type& Foam::max(const Matrix& M) { - const label mn = a.size(); + const label mn = M.size(); if (mn) { label curMaxI = 0; - const Type* v = a.v(); + const Type* Mv = M.v(); for (label i=1; i v[curMaxI]) + if (Mv[i] > Mv[curMaxI]) { curMaxI = i; } } - return v[curMaxI]; + return Mv[curMaxI]; } else { @@ -574,30 +400,30 @@ const Type& Foam::max(const Matrix& a) << abort(FatalError); // Return in error to keep compiler happy - return a(0, 0); + return M(0, 0); } } template -const Type& Foam::min(const Matrix& a) +const Type& Foam::min(const Matrix& M) { - const label mn = a.size(); + const label mn = M.size(); if (mn) { label curMinI = 0; - const Type* v = a.v(); + const Type* Mv = M.v(); for (label i=1; i& a) << abort(FatalError); // Return in error to keep compiler happy - return a(0, 0); + return M(0, 0); } } @@ -614,187 +440,197 @@ const Type& Foam::min(const Matrix& a) // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // template -Form Foam::operator-(const Matrix& a) +Form Foam::operator-(const Matrix& M) { - Form na(a.m(), a.n()); + Form nM(M.m(), M.n()); - if (a.m() && a.n()) + if (M.m() && M.n()) { - Type* nav = na.v(); - const Type* av = a.v(); + Type* nMv = nM.v(); + const Type* Mv = M.v(); - const label mn = a.size(); + const label mn = M.size(); for (label i=0; i -Form Foam::operator+(const Matrix& a, const Matrix& b) +Form Foam::operator+(const Matrix& A, const Matrix& B) { - if (a.m() != b.m()) + if (A.m() != B.m()) { FatalErrorInFunction - << "Attempt to add matrices with different number of rows: " - << a.m() << ", " << b.m() + << "Attempt to add matrices with different numbers of rows: " + << A.m() << ", " << B.m() << abort(FatalError); } - if (a.n() != b.n()) + if (A.n() != B.n()) { FatalErrorInFunction - << "Attempt to add matrices with different number of columns: " - << a.n() << ", " << b.n() + << "Attempt to add matrices with different numbers of columns: " + << A.n() << ", " << B.n() << abort(FatalError); } - Form ab(a.m(), a.n()); + Form AB(A.m(), A.n()); - Type* abv = ab.v(); - const Type* av = a.v(); - const Type* bv = b.v(); + Type* ABv = AB.v(); + const Type* Av = A.v(); + const Type* Bv = B.v(); - const label mn = a.size(); + const label mn = A.size(); for (label i=0; i -Form Foam::operator-(const Matrix& a, const Matrix& b) +Form Foam::operator-(const Matrix& A, const Matrix& B) { - if (a.m() != b.m()) + if (A.m() != B.m()) { FatalErrorInFunction - << "Attempt to add matrices with different number of rows: " - << a.m() << ", " << b.m() + << "Attempt to add matrices with different numbers of rows: " + << A.m() << ", " << B.m() << abort(FatalError); } - if (a.n() != b.n()) + if (A.n() != B.n()) { FatalErrorInFunction - << "Attempt to add matrices with different number of columns: " - << a.n() << ", " << b.n() + << "Attempt to add matrices with different numbers of columns: " + << A.n() << ", " << B.n() << abort(FatalError); } - Form ab(a.m(), a.n()); + Form AB(A.m(), A.n()); - Type* abv = ab.v(); - const Type* av = a.v(); - const Type* bv = b.v(); + Type* ABv = AB.v(); + const Type* Av = A.v(); + const Type* Bv = B.v(); - const label mn = a.size(); + const label mn = A.size(); for (label i=0; i -Form Foam::operator*(const scalar s, const Matrix& a) +Form Foam::operator*(const scalar s, const Matrix& M) { - Form sa(a.m(), a.n()); + Form sM(M.m(), M.n()); - if (a.m() && a.n()) + if (M.m() && M.n()) { - Type* sav = sa.v(); - const Type* av = a.v(); + Type* sMv = sM.v(); + const Type* Mv = M.v(); - const label mn = a.size(); + const label mn = M.size(); for (label i=0; i -Form Foam::operator*(const Matrix& a, const scalar s) +Form Foam::operator*(const Matrix& M, const scalar s) { - Form sa(a.m(), a.n()); + Form sM(M.m(), M.n()); - if (a.m() && a.n()) + if (M.m() && M.n()) { - Type* sav = sa.v(); - const Type* av = a.v(); + Type* sMv = sM.v(); + const Type* Mv = M.v(); - const label mn = a.size(); + const label mn = M.size(); for (label i=0; i -Form Foam::operator/(const Matrix& a, const scalar s) +Form Foam::operator/(const Matrix& M, const scalar s) { - Form sa(a.m(), a.n()); + Form sM(M.m(), M.n()); - if (a.m() && a.n()) + if (M.m() && M.n()) { - Type* sav = sa.v(); - const Type* av = a.v(); + Type* sMv = sM.v(); + const Type* Mv = M.v(); - const label mn = a.size(); + const label mn = M.size(); for (label i=0; i -Form Foam::operator*(const Matrix& a, const Matrix& b) +template +typename Foam::typeOfInnerProduct::type +Foam::operator* +( + const Matrix& A, + const Matrix& B +) { - if (a.n() != b.m()) + if (A.n() != B.m()) { FatalErrorInFunction << "Attempt to multiply incompatible matrices:" << nl - << "Matrix A : " << a.m() << " rows, " << a.n() << " columns" << nl - << "Matrix B : " << b.m() << " rows, " << b.n() << " columns" << nl + << "Matrix A : " << A.m() << " x " << A.n() << nl + << "Matrix B : " << B.m() << " x " << B.n() << nl << "In order to multiply matrices, columns of A must equal " << "rows of B" << abort(FatalError); } - Form ab(a.m(), b.n(), scalar(0)); + typename typeOfInnerProduct::type AB + ( + A.m(), + B.n(), + Zero + ); - for (label i=0; i> Foam::operator* { FatalErrorInFunction << "Attempt to multiply incompatible matrix and field:" << nl - << "Matrix : " << M.m() << " rows, " << M.n() << " columns" << nl + << "Matrix : " << M.m() << " x " << M.n() << nl << "Field : " << f.size() << " rows" << nl << "In order to multiply a matrix M and field f, " "columns of M must equal rows of f" @@ -819,9 +655,9 @@ inline Foam::tmp> Foam::operator* tmp> tMf(new Field(f.size(), Zero)); Field& Mf = tMf.ref(); - for (label i=0; i, where the n x m matrix - dimensions are known and used for subscript bounds checking, etc. + A templated (m x n) matrix of objects of \. SourceFiles Matrix.C @@ -42,7 +41,6 @@ SourceFiles #include "label.H" #include "uLabel.H" #include "Field.H" -#include "MatrixSpace.H" #include "zero.H" #include "autoPtr.H" @@ -67,6 +65,12 @@ template Ostream& operator<< const Matrix& ); +template +class ConstMatrixBlock; + +template +class MatrixBlock; + /*---------------------------------------------------------------------------*\ Class Matrix Declaration @@ -78,7 +82,7 @@ class Matrix // Private data //- Number of rows and columns in Matrix. - label nRows_, nCols_; + label mRows_, nCols_; //- Row pointers Type* __restrict__ v_; @@ -102,157 +106,6 @@ public: inline static const mType& null(); - // Sub-Block Classes - - class ConstBlock - { - // Private data - - //- Const reference to the parent matrix - const mType& matrix_; - - // Block size - const label nRows_; - const label nCols_; - - // Block location in parent matrix - const label rowStart_; - const label colStart_; - - public: - - typedef typename mType::cmptType cmptType; - - // Constructors - - //- Construct block for matrix, size and location - inline ConstBlock - ( - const mType& matrix, - const label m, - const label n, - const label mStart, - const label nStart - ); - - - // Member Functions - - //- Return the number of rows in the block - inline label m() const; - - //- Return the number of columns in the block - inline label n() const; - - //- (i, j) const element access operator - inline const Type& operator() - ( - const label i, - const label j - ) const; - - //- Convert a column of a matrix to a Field - operator Field() const; - }; - - - class Block - { - // Private data - - //- Reference to the parent matrix - mType& matrix_; - - // Block size - const label nRows_; - const label nCols_; - - // Block location in parent matrix - const label rowStart_; - const label colStart_; - - public: - - typedef typename mType::cmptType cmptType; - - // Constructors - - //- Construct block for matrix, size and location - inline Block - ( - mType& matrix, - const label m, - const label n, - const label mStart, - const label nStart - ); - - - // Member Functions - - //- Return the number of rows in the block - inline label m() const; - - //- Return the number of columns in the block - inline label n() const; - - //- (i, j) const element access operator - inline const Type& operator() - ( - const label i, - const label j - ) const; - - //- (i, j) element access operator - inline Type& operator()(const label i, const label j); - - //- Convert a column of a matrix to a Field - operator Field() const; - - - // Member operators - - //- Assignment to a compatible matrix - void operator=(const mType&); - - //- Assignment to a compatible block - void operator=(const Block&); - - //- Assignment to a compatible const block - void operator=(const ConstBlock&); - - //- Assignment to a compatible MatrixSpace - template - void operator=(const MatrixSpace&); - - //- Assignment to a compatible MatrixSpace block - template - < - template class Block, - class SubTensor, - direction BRowStart, - direction BColStart - > - void operator=(const Block&); - - //- Assignment to a compatible VectorSpace (column-vector) - template - void operator=(const VectorSpace&); - - //- Assignment to a compatible VectorSpace (column-vector) block - template - < - template class Block, - class SubVector, - direction BStart - > - void operator=(const Block&); - - //- Assignment to a Field (column-vector) - void operator=(const Field&); - }; - - // Constructors //- Null constructor. @@ -272,8 +125,17 @@ public: //- Copy constructor. Matrix(const mType&); + //- Copy constructor from matrix of a different form + template + explicit Matrix(const Matrix&); + //- Construct from a block of another matrix - Matrix(const mType::Block&); + template + Matrix(const ConstMatrixBlock&); + + //- Construct from a block of another matrix + template + Matrix(const MatrixBlock&); //- Construct from Istream. Matrix(Istream&); @@ -308,7 +170,7 @@ public: // Block access - inline ConstBlock block + inline ConstMatrixBlock block ( const label m, const label n, @@ -317,19 +179,19 @@ public: ) const; template - inline ConstBlock block + inline ConstMatrixBlock block ( const label mStart, const label nStart ) const; - inline ConstBlock col + inline ConstMatrixBlock col ( const label m, const label rowStart ) const; - inline ConstBlock col + inline ConstMatrixBlock col ( const label m, const label mStart, @@ -337,7 +199,7 @@ public: ) const; - inline Block block + inline MatrixBlock block ( const label m, const label n, @@ -346,11 +208,19 @@ public: ); template - inline Block block(const label mStart, const label nStart); + inline MatrixBlock block + ( + const label mStart, + const label nStart + ); - inline Block col(const label m, const label rowStart); + inline MatrixBlock col + ( + const label m, + const label rowStart + ); - inline Block col + inline MatrixBlock col ( const label m, const label mStart, @@ -402,7 +272,12 @@ public: void operator=(const mType&); //- Assignment to a block of another matrix - void operator=(const mType::Block&); + template + void operator=(const ConstMatrixBlock&); + + //- Assignment to a block of another matrix + template + void operator=(const MatrixBlock&); //- Assignment of all entries to zero void operator=(const zero); @@ -475,11 +350,12 @@ Form operator/ const scalar ); -template -Form operator* +template +typename typeOfInnerProduct::type +operator* ( - const Matrix&, - const Matrix& + const Matrix& a, + const Matrix& b ); template diff --git a/src/OpenFOAM/matrices/Matrix/MatrixI.H b/src/OpenFOAM/matrices/Matrix/MatrixI.H index 73992f3285..73992aac77 100644 --- a/src/OpenFOAM/matrices/Matrix/MatrixI.H +++ b/src/OpenFOAM/matrices/Matrix/MatrixI.H @@ -23,12 +23,14 @@ License \*---------------------------------------------------------------------------*/ +#include "MatrixBlock.H" + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template inline Foam::Matrix::Matrix() : - nRows_(0), + mRows_(0), nCols_(0), v_(NULL) {} @@ -42,68 +44,6 @@ clone() const } -template -Foam::Matrix::ConstBlock::ConstBlock -( - const mType& matrix, - const label m, - const label n, - const label mStart, - const label nStart -) -: - matrix_(matrix), - nRows_(m), - nCols_(n), - rowStart_(mStart), - colStart_(nStart) -{ - #ifdef FULLDEBUG - if - ( - rowStart_ + nRows_ > matrix.m() - || colStart_ + nCols_ > matrix.n() - ) - { - FatalErrorInFunction - << "Block addresses outside matrix" - << abort(FatalError); - } - #endif -} - - -template -Foam::Matrix::Block::Block -( - mType& matrix, - const label m, - const label n, - const label mStart, - const label nStart -) -: - matrix_(matrix), - nRows_(m), - nCols_(n), - rowStart_(mStart), - colStart_(nStart) -{ - #ifdef FULLDEBUG - if - ( - rowStart_ + nRows_ > matrix.m() - || colStart_ + nCols_ > matrix.n() - ) - { - FatalErrorInFunction - << "Block addresses outside matrix" - << abort(FatalError); - } - #endif -} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -116,7 +56,7 @@ inline const Foam::Matrix& Foam::Matrix::null() template inline Foam::label Foam::Matrix::m() const { - return nRows_; + return mRows_; } @@ -130,7 +70,7 @@ inline Foam::label Foam::Matrix::n() const template inline Foam::label Foam::Matrix::size() const { - return nRows_*nCols_; + return mRows_*nCols_; } @@ -138,16 +78,16 @@ template inline void Foam::Matrix::checki(const label i) const { #ifdef FULLDEBUG - if (!nRows_ || !nCols_) + if (!mRows_ || !nCols_) { FatalErrorInFunction << "Attempt to access element from empty matrix" << abort(FatalError); } - else if (i<0 || i>=nRows_) + else if (i<0 || i>=mRows_) { FatalErrorInFunction - << "Index " << i << " out of range 0 ... " << nRows_-1 + << "Index " << i << " out of range 0 ... " << mRows_-1 << abort(FatalError); } #endif @@ -158,7 +98,7 @@ template inline void Foam::Matrix::checkj(const label j) const { #ifdef FULLDEBUG - if (!nRows_ || !nCols_) + if (!mRows_ || !nCols_) { FatalErrorInFunction << "Attempt to access element from empty matrix" @@ -174,34 +114,6 @@ inline void Foam::Matrix::checkj(const label j) const } -template -inline Foam::label Foam::Matrix::ConstBlock::m() const -{ - return nRows_; -} - - -template -inline Foam::label Foam::Matrix::ConstBlock::n() const -{ - return nCols_; -} - - -template -inline Foam::label Foam::Matrix::Block::m() const -{ - return nRows_; -} - - -template -inline Foam::label Foam::Matrix::Block::n() const -{ - return nCols_; -} - - template inline const Type* Foam::Matrix::v() const { @@ -217,7 +129,7 @@ inline Type* Foam::Matrix::v() template -inline typename Foam::Matrix::ConstBlock +inline Foam::ConstMatrixBlock> Foam::Matrix::block ( const label m, @@ -226,7 +138,7 @@ Foam::Matrix::block const label nStart ) const { - return ConstBlock + return ConstMatrixBlock ( *this, m, @@ -239,17 +151,17 @@ Foam::Matrix::block template template -inline typename Foam::Matrix::ConstBlock +inline Foam::ConstMatrixBlock> Foam::Matrix::block ( const label mStart, const label nStart ) const { - return ConstBlock + return ConstMatrixBlock ( *this, - VectorSpace::nRows, + VectorSpace::mRows, VectorSpace::nCols, mStart, nStart @@ -258,14 +170,14 @@ Foam::Matrix::block template -inline typename Foam::Matrix::ConstBlock +inline Foam::ConstMatrixBlock> Foam::Matrix::col ( const label m, const label mStart ) const { - return ConstBlock + return ConstMatrixBlock ( *this, m, @@ -277,7 +189,7 @@ Foam::Matrix::col template -inline typename Foam::Matrix::ConstBlock +inline Foam::ConstMatrixBlock> Foam::Matrix::col ( const label m, @@ -285,7 +197,7 @@ Foam::Matrix::col const label nStart ) const { - return ConstBlock + return ConstMatrixBlock ( *this, m, @@ -297,7 +209,7 @@ Foam::Matrix::col template -inline typename Foam::Matrix::Block +inline Foam::MatrixBlock> Foam::Matrix::block ( const label m, @@ -306,7 +218,7 @@ Foam::Matrix::block const label nStart ) { - return Block + return MatrixBlock ( *this, m, @@ -319,13 +231,13 @@ Foam::Matrix::block template template -inline typename Foam::Matrix::Block +inline Foam::MatrixBlock> Foam::Matrix::block(const label mStart, const label nStart) { - return Block + return MatrixBlock ( *this, - VectorSpace::nRows, + VectorSpace::mRows, VectorSpace::nCols, mStart, nStart @@ -334,10 +246,10 @@ Foam::Matrix::block(const label mStart, const label nStart) template -inline typename Foam::Matrix::Block +inline Foam::MatrixBlock> Foam::Matrix::col(const label m, const label mStart) { - return Block + return MatrixBlock ( *this, m, @@ -349,7 +261,7 @@ Foam::Matrix::col(const label m, const label mStart) template -inline typename Foam::Matrix::Block +inline Foam::MatrixBlock> Foam::Matrix::col ( const label m, @@ -357,7 +269,7 @@ Foam::Matrix::col const label nStart ) { - return Block + return MatrixBlock ( *this, m, @@ -370,84 +282,6 @@ Foam::Matrix::col // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -template -inline const Type& Foam::Matrix::ConstBlock::operator() -( - const label i, - const label j -) const -{ - #ifdef FULLDEBUG - if (i<0 || i>=nRows_) - { - FatalErrorInFunction - << "Index " << i << " out of range 0 ... " << nRows_-1 - << abort(FatalError); - } - if (j<0 || j>=nCols_) - { - FatalErrorInFunction - << "Index " << j << " out of range 0 ... " << nCols_-1 - << abort(FatalError); - } - #endif - - return matrix_(i + rowStart_, j + colStart_); -} - - -template -inline const Type& Foam::Matrix::Block::operator() -( - const label i, - const label j -) const -{ - #ifdef FULLDEBUG - if (i<0 || i>=nRows_) - { - FatalErrorInFunction - << "Index " << i << " out of range 0 ... " << nRows_-1 - << abort(FatalError); - } - if (j<0 || j>=nCols_) - { - FatalErrorInFunction - << "Index " << j << " out of range 0 ... " << nCols_-1 - << abort(FatalError); - } - #endif - - return matrix_(i + rowStart_, j + colStart_); -} - - -template -inline Type& Foam::Matrix::Block::operator() -( - const label i, - const label j -) -{ - #ifdef FULLDEBUG - if (i<0 || i>=nRows_) - { - FatalErrorInFunction - << "Index " << i << " out of range 0 ... " << nRows_-1 - << abort(FatalError); - } - if (j<0 || j>=nCols_) - { - FatalErrorInFunction - << "Index " << j << " out of range 0 ... " << nCols_-1 - << abort(FatalError); - } - #endif - - return matrix_(i + rowStart_, j + colStart_); -} - - template inline const Type& Foam::Matrix::operator() ( diff --git a/src/OpenFOAM/matrices/Matrix/MatrixIO.C b/src/OpenFOAM/matrices/Matrix/MatrixIO.C index 9be178c12f..51943ada59 100644 --- a/src/OpenFOAM/matrices/Matrix/MatrixIO.C +++ b/src/OpenFOAM/matrices/Matrix/MatrixIO.C @@ -34,7 +34,7 @@ License template Foam::Matrix::Matrix(Istream& is) : - nRows_(0), + mRows_(0), nCols_(0), v_(NULL) { @@ -59,10 +59,10 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix& M) if (firstToken.isLabel()) { - M.nRows_ = firstToken.labelToken(); + M.mRows_ = firstToken.labelToken(); M.nCols_ = readLabel(is); - label mn = M.nRows_*M.nCols_; + label mn = M.mRows_*M.nCols_; // Read list contents depending on data format if (is.format() == IOstream::ASCII || !contiguous()) @@ -151,7 +151,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix& M) template Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix& M) { - label mn = M.nRows_*M.nCols_; + label mn = M.mRows_*M.nCols_; os << M.m() << token::SPACE << M.n(); diff --git a/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.C b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.C new file mode 100644 index 0000000000..3aecbe8377 --- /dev/null +++ b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.C @@ -0,0 +1,342 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 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 . + +\*---------------------------------------------------------------------------*/ + +#include "MatrixBlock.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +Foam::ConstMatrixBlock::operator Field() const +{ + if (nCols_ != 1) + { + FatalErrorInFunction + << "Number of columns " << nCols_ << " != 1" + << abort(FatalError); + } + + Field f(mRows_); + + forAll(f, i) + { + f[i] = operator()(i, 0); + } + + return f; +} + + +template +Foam::MatrixBlock::operator Field() const +{ + if (nCols_ != 1) + { + FatalErrorInFunction + << "Number of columns " << nCols_ << " != 1" + << abort(FatalError); + } + + Field f(mRows_); + + forAll(f, i) + { + f[i] = operator()(i, 0); + } + + return f; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template +template +void Foam::MatrixBlock::operator= +( + const Matrix& Mb +) +{ + if (mRows_ != Mb.m() || nCols_ != Mb.n()) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.m() << "x" << Mb.n() + << abort(FatalError); + } + + for (label i=0; i +void Foam::MatrixBlock::operator= +( + const ConstMatrixBlock& Mb +) +{ + if (this != &Mb) + { + if (mRows_ != Mb.m() || nCols_ != Mb.n()) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.m() << "x" << Mb.n() + << abort(FatalError); + } + + for (label i=0; i +void Foam::MatrixBlock::operator= +( + const MatrixBlock& Mb +) +{ + if (this != &Mb) + { + if (mRows_ != Mb.m() || nCols_ != Mb.n()) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.m() << "x" << Mb.n() + << abort(FatalError); + } + + for (label i=0; i +template +void Foam::MatrixBlock::operator= +( + const ConstMatrixBlock& Mb +) +{ + if (this != &Mb) + { + if (mRows_ != Mb.m() || nCols_ != Mb.n()) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.m() << "x" << Mb.n() + << abort(FatalError); + } + + for (label i=0; i +template +void Foam::MatrixBlock::operator= +( + const MatrixBlock& Mb +) +{ + if (this != &Mb) + { + if (mRows_ != Mb.m() || nCols_ != Mb.n()) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.m() << "x" << Mb.n() + << abort(FatalError); + } + + for (label i=0; i +template +< + template class MSBlock, + class SubTensor, + Foam::direction BRowStart, + Foam::direction BColStart +> +void Foam::MatrixBlock::operator= +( + const MSBlock& Mb +) +{ + if (mRows_ != Mb.mRows || nCols_ != Mb.nCols) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.mRows << "x" << Mb.nCols + << abort(FatalError); + } + + for (direction i=0; i +template +< + template class VSBlock, + class SubVector, + Foam::direction BStart +> +void Foam::MatrixBlock::operator= +( + const VSBlock& Mb +) +{ + if (mRows_ != Mb.nComponents || nCols_ != 1) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Mb.nComponents << "x" << 1 + << abort(FatalError); + } + + for (direction i=0; i +template +void Foam::MatrixBlock::operator= +( + const MatrixSpace& ms +) +{ + if (mRows_ != Nrows || nCols_ != Ncols) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Nrows << "x" << Ncols + << abort(FatalError); + } + + for (label i=0; i +template +void Foam::MatrixBlock::operator= +( + const VectorSpace& ms +) +{ + if (mRows_ != Ncmpts || nCols_ != 1) + { + FatalErrorInFunction + << "Attempt to assign blocks of different sizes: " + << mRows_ << "x" << nCols_ << " != " + << Ncmpts << "x" << 1 + << abort(FatalError); + } + + for (direction i=0; i +void Foam::MatrixBlock::operator=(const Field& f) +{ + if (mRows_ != f.size() || nCols_ != 1) + { + FatalErrorInFunction + << "Error: cannot assign blocks of different size (left is " + << mRows_ << "x" << nCols_ << " != " + << f.size() << "x" << 1 + << abort(FatalError); + } + + forAll(f, i) + { + operator()(i, 0) = f[i]; + } +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.H b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.H new file mode 100644 index 0000000000..67ece198ee --- /dev/null +++ b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlock.H @@ -0,0 +1,240 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 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::MatrixBlock + +Description + A templated block of an (m x n) matrix of type \. + + Foam::ConstMatrixBlock: block of a const matrix + Foam::MatrixBlock: block of a non-const matrix + + The block may be assigned to a block of another matrix or to a VectorSpace + or MatrixSpace e.g. \c tensor. Conversion of a column block to a \c + Field is also provide. + +SourceFiles + MatrixBlock.C + MatrixBlockI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef MatrixBlock_H +#define MatrixBlock_H + +#include "Matrix.H" +#include "MatrixSpace.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class ConstMatrixBlock Declaration +\*---------------------------------------------------------------------------*/ + +template +class ConstMatrixBlock +{ + // Private data + + //- Const reference to the parent matrix + const MatrixType& matrix_; + + // Block size + const label mRows_; + const label nCols_; + + // Block location in parent matrix + const label rowStart_; + const label colStart_; + +public: + + typedef typename MatrixType::cmptType cmptType; + + // Constructors + + //- Construct block for matrix, size and location + inline ConstMatrixBlock + ( + const MatrixType& matrix, + const label m, + const label n, + const label mStart, + const label nStart + ); + + + // Member Functions + + //- Return the number of rows in the block + inline label m() const; + + //- Return the number of columns in the block + inline label n() const; + + //- (i, j) const element access operator + inline const cmptType& operator() + ( + const label i, + const label j + ) const; + + //- Convert a column of a matrix to a Field + operator Field() const; +}; + + +/*---------------------------------------------------------------------------*\ + Class MatrixBlock Declaration +\*---------------------------------------------------------------------------*/ + +template +class MatrixBlock +{ + // Private data + + //- Reference to the parent matrix + MatrixType& matrix_; + + // Block size + const label mRows_; + const label nCols_; + + // Block location in parent matrix + const label rowStart_; + const label colStart_; + +public: + + typedef typename MatrixType::cmptType cmptType; + + // Constructors + + //- Construct block for matrix, size and location + inline MatrixBlock + ( + MatrixType& matrix, + const label m, + const label n, + const label mStart, + const label nStart + ); + + + // Member Functions + + //- Return the number of rows in the block + inline label m() const; + + //- Return the number of columns in the block + inline label n() const; + + //- (i, j) const element access operator + inline const cmptType& operator() + ( + const label i, + const label j + ) const; + + //- (i, j) element access operator + inline cmptType& operator()(const label i, const label j); + + //- Convert a column of a matrix to a Field + operator Field() const; + + + // Member operators + + //- Assignment to a compatible matrix + template + void operator=(const Matrix&); + + //- Assignment to a compatible const block + void operator=(const ConstMatrixBlock&); + + //- Assignment to a compatible block + void operator=(const MatrixBlock&); + + //- Assignment to a compatible const block + template + void operator=(const ConstMatrixBlock&); + + //- Assignment to a compatible block + template + void operator=(const MatrixBlock&); + + //- Assignment to a compatible MatrixSpace + template + void operator=(const MatrixSpace&); + + //- Assignment to a compatible MatrixSpace block + template + < + template class Block, + class SubTensor, + direction BRowStart, + direction BColStart + > + void operator=(const Block&); + + //- Assignment to a compatible VectorSpace (column-vector) + template + void operator=(const VectorSpace&); + + //- Assignment to a compatible VectorSpace (column-vector) block + template + < + template class Block, + class SubVector, + direction BStart + > + void operator=(const Block&); + + //- Assignment to a Field (column-vector) + void operator=(const Field&); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "MatrixBlockI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "MatrixBlock.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/matrices/MatrixBlock/MatrixBlockI.H b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlockI.H new file mode 100644 index 0000000000..e764dea032 --- /dev/null +++ b/src/OpenFOAM/matrices/MatrixBlock/MatrixBlockI.H @@ -0,0 +1,203 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 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 . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::ConstMatrixBlock::ConstMatrixBlock +( + const MatrixType& matrix, + const label m, + const label n, + const label mStart, + const label nStart +) +: + matrix_(matrix), + mRows_(m), + nCols_(n), + rowStart_(mStart), + colStart_(nStart) +{ + #ifdef FULLDEBUG + if + ( + rowStart_ + mRows_ > matrix.m() + || colStart_ + nCols_ > matrix.n() + ) + { + FatalErrorInFunction + << "Block addresses outside matrix" + << abort(FatalError); + } + #endif +} + + +template +Foam::MatrixBlock::MatrixBlock +( + MatrixType& matrix, + const label m, + const label n, + const label mStart, + const label nStart +) +: + matrix_(matrix), + mRows_(m), + nCols_(n), + rowStart_(mStart), + colStart_(nStart) +{ + #ifdef FULLDEBUG + if + ( + rowStart_ + mRows_ > matrix.m() + || colStart_ + nCols_ > matrix.n() + ) + { + FatalErrorInFunction + << "Block addresses outside matrix" + << abort(FatalError); + } + #endif +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +inline Foam::label Foam::ConstMatrixBlock::m() const +{ + return mRows_; +} + + +template +inline Foam::label Foam::ConstMatrixBlock::n() const +{ + return nCols_; +} + + +template +inline Foam::label Foam::MatrixBlock::m() const +{ + return mRows_; +} + + +template +inline Foam::label Foam::MatrixBlock::n() const +{ + return nCols_; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template +inline const typename MatrixType::cmptType& +Foam::ConstMatrixBlock::operator() +( + const label i, + const label j +) const +{ + #ifdef FULLDEBUG + if (i<0 || i>=mRows_) + { + FatalErrorInFunction + << "Index " << i << " out of range 0 ... " << mRows_-1 + << abort(FatalError); + } + if (j<0 || j>=nCols_) + { + FatalErrorInFunction + << "Index " << j << " out of range 0 ... " << nCols_-1 + << abort(FatalError); + } + #endif + + return matrix_(i + rowStart_, j + colStart_); +} + + +template +inline const typename MatrixType::cmptType& +Foam::MatrixBlock::operator() +( + const label i, + const label j +) const +{ + #ifdef FULLDEBUG + if (i<0 || i>=mRows_) + { + FatalErrorInFunction + << "Index " << i << " out of range 0 ... " << mRows_-1 + << abort(FatalError); + } + if (j<0 || j>=nCols_) + { + FatalErrorInFunction + << "Index " << j << " out of range 0 ... " << nCols_-1 + << abort(FatalError); + } + #endif + + return matrix_(i + rowStart_, j + colStart_); +} + + +template +inline typename MatrixType::cmptType& +Foam::MatrixBlock::operator() +( + const label i, + const label j +) +{ + #ifdef FULLDEBUG + if (i<0 || i>=mRows_) + { + FatalErrorInFunction + << "Index " << i << " out of range 0 ... " << mRows_-1 + << abort(FatalError); + } + if (j<0 || j>=nCols_) + { + FatalErrorInFunction + << "Index " << j << " out of range 0 ... " << nCols_-1 + << abort(FatalError); + } + #endif + + return matrix_(i + rowStart_, j + colStart_); +} + + +// ************************************************************************* //