Made Matrix generic base-class for SquareMatrix and RectangularMatrix by introducing

the type it will become as a template argument.

Brought everything into line with this change.
This commit is contained in:
henry 2008-09-28 22:50:57 +01:00
parent b722041fff
commit 318b71e206
32 changed files with 791 additions and 524 deletions

View File

@ -34,7 +34,7 @@ Description
#define ODE_H
#include "scalarField.H"
#include "Matrix.H"
#include "scalarMatrices.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -80,7 +80,7 @@ public:
const scalar x,
const scalarField& y,
scalarField& dfdx,
Matrix<scalar>& dfdy
scalarSquareMatrix& dfdy
) const = 0;
};

View File

@ -25,7 +25,6 @@ License
\*---------------------------------------------------------------------------*/
#include "KRR4.H"
#include "simpleMatrix.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -36,11 +35,11 @@ namespace Foam
{
addToRunTimeSelectionTable(ODESolver, KRR4, ODE);
const scalar
const scalar
KRR4::safety = 0.9, KRR4::grow = 1.5, KRR4::pgrow = -0.25,
KRR4::shrink = 0.5, KRR4::pshrink = (-1.0/3.0), KRR4::errcon = 0.1296;
const scalar
const scalar
KRR4::gamma = 1.0/2.0,
KRR4::a21 = 2.0, KRR4::a31 = 48.0/25.0, KRR4::a32 = 6.0/25.0,
KRR4::c21 = -8.0, KRR4::c31 = 372.0/25.0, KRR4::c32 = 12.0/5.0,
@ -81,8 +80,8 @@ void Foam::KRR4::solve
const ODE& ode,
scalar& x,
scalarField& y,
scalarField& dydx,
const scalar eps,
scalarField& dydx,
const scalar eps,
const scalarField& yScale,
const scalar hTry,
scalar& hDid,
@ -109,14 +108,14 @@ void Foam::KRR4::solve
a_[i][i] += 1.0/(gamma*h);
}
simpleMatrix<scalar>::LUDecompose(a_, pivotIndices_);
LUDecompose(a_, pivotIndices_);
for (register label i=0; i<n_; i++)
{
g1_[i] = dydxTemp_[i] + h*c1X*dfdx_[i];
}
simpleMatrix<scalar>::LUBacksubstitute(a_, pivotIndices_, g1_);
LUBacksubstitute(a_, pivotIndices_, g1_);
for (register label i=0; i<n_; i++)
{
@ -131,7 +130,7 @@ void Foam::KRR4::solve
g2_[i] = dydx_[i] + h*c2X*dfdx_[i] + c21*g1_[i]/h;
}
simpleMatrix<scalar>::LUBacksubstitute(a_, pivotIndices_, g2_);
LUBacksubstitute(a_, pivotIndices_, g2_);
for (register label i=0; i<n_; i++)
{
@ -146,7 +145,7 @@ void Foam::KRR4::solve
g3_[i] = dydx[i] + h*c3X*dfdx_[i] + (c31*g1_[i] + c32*g2_[i])/h;
}
simpleMatrix<scalar>::LUBacksubstitute(a_, pivotIndices_, g3_);
LUBacksubstitute(a_, pivotIndices_, g3_);
for (register label i=0; i<n_; i++)
{
@ -154,7 +153,7 @@ void Foam::KRR4::solve
+ (c41*g1_[i] + c42*g2_[i] + c43*g3_[i])/h;
}
simpleMatrix<scalar>::LUBacksubstitute(a_, pivotIndices_, g4_);
LUBacksubstitute(a_, pivotIndices_, g4_);
for (register label i=0; i<n_; i++)
{

View File

@ -62,15 +62,15 @@ class KRR4
mutable scalarField g4_;
mutable scalarField yErr_;
mutable scalarField dfdx_;
mutable Matrix<scalar> dfdy_;
mutable Matrix<scalar> a_;
mutable scalarSquareMatrix dfdy_;
mutable scalarSquareMatrix a_;
mutable labelList pivotIndices_;
static const int maxtry = 40;
static const scalar safety, grow, pgrow, shrink, pshrink, errcon;
static const scalar
static const scalar
gamma,
a21, a31, a32,
c21, c31, c32, c41, c42, c43,

View File

@ -60,8 +60,8 @@ class SIBS
static const scalar safe1, safe2, redMax, redMin, scaleMX;
mutable scalarField a_;
mutable Matrix<scalar> alpha_;
mutable Matrix<scalar> d_p_;
mutable scalarSquareMatrix alpha_;
mutable scalarSquareMatrix d_p_;
mutable scalarField x_p_;
mutable scalarField err_;
@ -69,7 +69,7 @@ class SIBS
mutable scalarField ySeq_;
mutable scalarField yErr_;
mutable scalarField dfdx_;
mutable Matrix<scalar> dfdy_;
mutable scalarSquareMatrix dfdy_;
mutable label first_, kMax_, kOpt_;
mutable scalar epsOld_, xNew_;
@ -82,7 +82,7 @@ void SIMPR
const scalarField& y,
const scalarField& dydx,
const scalarField& dfdx,
const Matrix<scalar>& dfdy,
const scalarSquareMatrix& dfdy,
const scalar deltaX,
const label nSteps,
scalarField& yEnd
@ -97,7 +97,7 @@ void polyExtrapolate
scalarField& yz,
scalarField& dy,
scalarField& x_p,
Matrix<scalar>& d_p
scalarSquareMatrix& d_p
) const;

View File

@ -25,7 +25,6 @@ License
\*---------------------------------------------------------------------------*/
#include "SIBS.H"
#include "simpleMatrix.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -36,7 +35,7 @@ void Foam::SIBS::SIMPR
const scalarField& y,
const scalarField& dydx,
const scalarField& dfdx,
const Matrix<scalar>& dfdy,
const scalarSquareMatrix& dfdy,
const scalar deltaX,
const label nSteps,
scalarField& yEnd
@ -44,7 +43,7 @@ void Foam::SIBS::SIMPR
{
scalar h = deltaX/nSteps;
Matrix<scalar> a(n_, n_);
scalarSquareMatrix a(n_);
for (register label i=0; i<n_; i++)
{
for (register label j=0; j<n_; j++)
@ -55,14 +54,14 @@ void Foam::SIBS::SIMPR
}
labelList pivotIndices(n_);
simpleMatrix<scalar>::LUDecompose(a, pivotIndices);
LUDecompose(a, pivotIndices);
for (register label i=0; i<n_; i++)
{
yEnd[i] = h*(dydx[i] + h*dfdx[i]);
}
simpleMatrix<scalar>::LUBacksubstitute(a, pivotIndices, yEnd);
LUBacksubstitute(a, pivotIndices, yEnd);
scalarField del(yEnd);
scalarField ytemp(n_);
@ -83,7 +82,7 @@ void Foam::SIBS::SIMPR
yEnd[i] = h*yEnd[i] - del[i];
}
simpleMatrix<scalar>::LUBacksubstitute(a, pivotIndices, yEnd);
LUBacksubstitute(a, pivotIndices, yEnd);
for (register label i=0; i<n_; i++)
{
@ -99,7 +98,7 @@ void Foam::SIBS::SIMPR
yEnd[i] = h*yEnd[i] - del[i];
}
simpleMatrix<scalar>::LUBacksubstitute(a, pivotIndices, yEnd);
LUBacksubstitute(a, pivotIndices, yEnd);
for (register label i=0; i<n_; i++)
{

View File

@ -36,7 +36,7 @@ void Foam::SIBS::polyExtrapolate
scalarField& yz,
scalarField& dy,
scalarField& x,
Matrix<scalar>& d
scalarSquareMatrix& d
) const
{
label n = yz.size();

View File

@ -177,9 +177,9 @@ dimensionedTypes/dimensionedTensor/dimensionedTensor.C
matrices/solution/solution.C
scalarMatrix = matrices/scalarMatrix
$(scalarMatrix)/scalarMatrix.C
$(scalarMatrix)/SVD/SVD.C
scalarMatrices = matrices/scalarMatrices
$(scalarMatrices)/scalarMatrices.C
$(scalarMatrices)/SVD/SVD.C
LUscalarMatrix = matrices/LUscalarMatrix
$(LUscalarMatrix)/LUscalarMatrix.C

View File

@ -22,8 +22,6 @@ License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
\*---------------------------------------------------------------------------*/
#include "Pstream.H"

View File

@ -29,7 +29,8 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
Foam::DiagonalMatrix<Type>::DiagonalMatrix(const Matrix<Type>& a)
template<class Form>
Foam::DiagonalMatrix<Type>::DiagonalMatrix(const Matrix<Form, Type>& a)
:
List<Type>(min(a.n(), a.m()))
{

View File

@ -46,7 +46,7 @@ namespace Foam
// * * * * * * * * * * * * Class Forward declaration * * * * * * * * * * * //
template<class Type> class Matrix;
template<class Form, class Type> class Matrix;
/*---------------------------------------------------------------------------*\
Class DiagonalMatrix Declaration
@ -62,7 +62,8 @@ public:
// Constructors
//- Construct from diagonal component of a Matrix
DiagonalMatrix<Type>(const Matrix<Type>&);
template<class Form>
DiagonalMatrix<Type>(const Matrix<Form, Type>&);
//- Construct empty from size
DiagonalMatrix<Type>(const label size);

View File

@ -31,9 +31,9 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::LUscalarMatrix::LUscalarMatrix(const Matrix<scalar>& matrix)
Foam::LUscalarMatrix::LUscalarMatrix(const scalarSquareMatrix& matrix)
:
scalarMatrix(matrix),
scalarSquareMatrix(matrix),
pivotIndices_(n())
{
LUDecompose(*this, pivotIndices_);
@ -101,7 +101,7 @@ Foam::LUscalarMatrix::LUscalarMatrix
nCells += lduMatrices[i].size();
}
Matrix<scalar> m(nCells, nCells, 0.0);
scalarSquareMatrix m(nCells, 0.0);
transfer(m);
convert(lduMatrices);
}
@ -109,7 +109,7 @@ Foam::LUscalarMatrix::LUscalarMatrix
else
{
label nCells = ldum.lduAddr().size();
Matrix<scalar> m(nCells, nCells, 0.0);
scalarSquareMatrix m(nCells, 0.0);
transfer(m);
convert(ldum, interfaceCoeffs, interfaces);
}

View File

@ -36,7 +36,7 @@ SourceFiles
#ifndef LUscalarMatrix_H
#define LUscalarMatrix_H
#include "scalarMatrix.H"
#include "scalarMatrices.H"
#include "labelList.H"
#include "FieldField.H"
#include "lduInterfaceFieldPtrsList.H"
@ -55,7 +55,7 @@ class procLduMatrix;
class LUscalarMatrix
:
public scalarMatrix
public scalarSquareMatrix
{
// Private data
@ -78,7 +78,7 @@ class LUscalarMatrix
void convert(const PtrList<procLduMatrix>& lduMatrices);
//- Print the ratio of the mag-sum of the off-diagonal coefficients
//- Print the ratio of the mag-sum of the off-diagonal coefficients
// to the mag-diagonal
void printDiagonalDominance() const;
@ -87,8 +87,8 @@ public:
// Constructors
//- Construct from Matrix<scalar> and perform LU decomposition
LUscalarMatrix(const Matrix<scalar>&);
//- Construct from scalarSquareMatrix and perform LU decomposition
LUscalarMatrix(const scalarSquareMatrix&);
//- Construct from lduMatrix and perform LU decomposition
LUscalarMatrix

View File

@ -28,8 +28,8 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template<class Type>
void Foam::Matrix<Type>::allocate()
template<class Form, class Type>
void Foam::Matrix<Form, Type>::allocate()
{
if (n_ && m_)
{
@ -46,8 +46,8 @@ void Foam::Matrix<Type>::allocate()
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class Type>
Foam::Matrix<Type>::~Matrix()
template<class Form, class Type>
Foam::Matrix<Form, Type>::~Matrix()
{
if (v_)
{
@ -59,16 +59,16 @@ Foam::Matrix<Type>::~Matrix()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
const Foam::Matrix<Type>& Foam::Matrix<Type>::null()
template<class Form, class Type>
const Foam::Matrix<Form, Type>& Foam::Matrix<Form, Type>::null()
{
Matrix<Type>* nullPtr = reinterpret_cast<Matrix<Type>*>(NULL);
Matrix<Form, Type>* nullPtr = reinterpret_cast<Matrix<Form, Type>*>(NULL);
return *nullPtr;
}
template<class Type>
Foam::Matrix<Type>::Matrix(const label n, const label m)
template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
:
n_(n),
m_(m),
@ -76,7 +76,7 @@ Foam::Matrix<Type>::Matrix(const label n, const label m)
{
if (n_ < 0 || m_ < 0)
{
FatalErrorIn("Matrix<Type>::Matrix(const label n, const label m)")
FatalErrorIn("Matrix<Form, Type>::Matrix(const label n, const label m)")
<< "bad n, m " << n_ << ", " << m_
<< abort(FatalError);
}
@ -85,8 +85,8 @@ Foam::Matrix<Type>::Matrix(const label n, const label m)
}
template<class Type>
Foam::Matrix<Type>::Matrix(const label n, const label m, const Type& a)
template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
:
n_(n),
m_(m),
@ -96,7 +96,7 @@ Foam::Matrix<Type>::Matrix(const label n, const label m, const Type& a)
{
FatalErrorIn
(
"Matrix<Type>::Matrix(const label n, const label m, const T&)"
"Matrix<Form, Type>::Matrix(const label n, const label m, const T&)"
) << "bad n, m " << n_ << ", " << m_
<< abort(FatalError);
}
@ -117,8 +117,8 @@ Foam::Matrix<Type>::Matrix(const label n, const label m, const Type& a)
}
template<class Type>
Foam::Matrix<Type>::Matrix(const Matrix<Type>& a)
template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
:
n_(a.n_),
m_(a.m_),
@ -139,8 +139,8 @@ Foam::Matrix<Type>::Matrix(const Matrix<Type>& a)
}
template<class Type>
void Foam::Matrix<Type>::clear()
template<class Form, class Type>
void Foam::Matrix<Form, Type>::clear()
{
if (v_)
{
@ -153,8 +153,8 @@ void Foam::Matrix<Type>::clear()
}
template<class Type>
void Foam::Matrix<Type>::transfer(Matrix<Type>& a)
template<class Form, class Type>
void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
{
clear();
@ -169,13 +169,11 @@ void Foam::Matrix<Type>::transfer(Matrix<Type>& a)
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
Foam::Matrix<Type> Foam::Matrix<Type>::T() const
template<class Form, class Type>
Form Foam::Matrix<Form, Type>::T() const
{
const Matrix<Type>& A = *this;
Matrix<Type> At(m(), n());
const Matrix<Form, Type>& A = *this;
Form At(m(), n());
for (register label i=0; i<n(); i++)
{
@ -189,8 +187,10 @@ Foam::Matrix<Type> Foam::Matrix<Type>::T() const
}
template<class Type>
void Foam::Matrix<Type>::operator=(const Type& t)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Form, class Type>
void Foam::Matrix<Form, Type>::operator=(const Type& t)
{
if (v_)
{
@ -206,12 +206,12 @@ void Foam::Matrix<Type>::operator=(const Type& t)
// Assignment operator. Takes linear time.
template<class Type>
void Foam::Matrix<Type>::operator=(const Matrix<Type>& a)
template<class Form, class Type>
void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
{
if (this == &a)
{
FatalErrorIn("Matrix<Type>::operator=(const Matrix<Type>&)")
FatalErrorIn("Matrix<Form, Type>::operator=(const Matrix<Form, Type>&)")
<< "attempted assignment to self"
<< abort(FatalError);
}
@ -240,8 +240,8 @@ void Foam::Matrix<Type>::operator=(const Matrix<Type>& a)
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class Type>
const Type& Foam::max(const Matrix<Type>& a)
template<class Form, class Type>
const Type& Foam::max(const Matrix<Form, Type>& a)
{
label nm = a.n_*a.m_;
@ -262,7 +262,7 @@ const Type& Foam::max(const Matrix<Type>& a)
}
else
{
FatalErrorIn("max(const Matrix<Type>&)")
FatalErrorIn("max(const Matrix<Form, Type>&)")
<< "matrix is empty"
<< abort(FatalError);
@ -272,8 +272,8 @@ const Type& Foam::max(const Matrix<Type>& a)
}
template<class Type>
const Type& Foam::min(const Matrix<Type>& a)
template<class Form, class Type>
const Type& Foam::min(const Matrix<Form, Type>& a)
{
label nm = a.n_*a.m_;
@ -294,7 +294,7 @@ const Type& Foam::min(const Matrix<Type>& a)
}
else
{
FatalErrorIn("min(const Matrix<Type>&)")
FatalErrorIn("min(const Matrix<Form, Type>&)")
<< "matrix is empty"
<< abort(FatalError);
@ -306,10 +306,10 @@ const Type& Foam::min(const Matrix<Type>& a)
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
template<class Type>
Foam::Matrix<Type> Foam::operator-(const Matrix<Type>& a)
template<class Form, class Type>
Form Foam::operator-(const Matrix<Form, Type>& a)
{
Matrix<Type> na(a.n_, a.m_);
Form na(a.n_, a.m_);
if (a.n_ && a.m_)
{
@ -327,14 +327,14 @@ Foam::Matrix<Type> Foam::operator-(const Matrix<Type>& a)
}
template<class Type>
Foam::Matrix<Type> Foam::operator+(const Matrix<Type>& a, const Matrix<Type>& b)
template<class Form, class Type>
Form Foam::operator+(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
{
if (a.n_ != b.n_)
{
FatalErrorIn
(
"Matrix<Type>::operator+(const Matrix<Type>&, const Matrix<Type>&)"
"Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
) << "attempted add matrices with different number of rows: "
<< a.n_ << ", " << b.n_
<< abort(FatalError);
@ -344,13 +344,13 @@ Foam::Matrix<Type> Foam::operator+(const Matrix<Type>& a, const Matrix<Type>& b)
{
FatalErrorIn
(
"Matrix<Type>::operator+(const Matrix<Type>&, const Matrix<Type>&)"
"Matrix<Form, Type>::operator+(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
) << "attempted add matrices with different number of columns: "
<< a.m_ << ", " << b.m_
<< abort(FatalError);
}
Matrix<Type> ab(a.n_, a.m_);
Form ab(a.n_, a.m_);
Type* abv = ab.v_[0];
const Type* av = a.v_[0];
@ -366,14 +366,14 @@ Foam::Matrix<Type> Foam::operator+(const Matrix<Type>& a, const Matrix<Type>& b)
}
template<class Type>
Foam::Matrix<Type> Foam::operator-(const Matrix<Type>& a, const Matrix<Type>& b)
template<class Form, class Type>
Form Foam::operator-(const Matrix<Form, Type>& a, const Matrix<Form, Type>& b)
{
if (a.n_ != b.n_)
{
FatalErrorIn
(
"Matrix<Type>::operator-(const Matrix<Type>&, const Matrix<Type>&)"
"Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
) << "attempted add matrices with different number of rows: "
<< a.n_ << ", " << b.n_
<< abort(FatalError);
@ -383,13 +383,13 @@ Foam::Matrix<Type> Foam::operator-(const Matrix<Type>& a, const Matrix<Type>& b)
{
FatalErrorIn
(
"Matrix<Type>::operator-(const Matrix<Type>&, const Matrix<Type>&)"
"Matrix<Form, Type>::operator-(const Matrix<Form, Type>&, const Matrix<Form, Type>&)"
) << "attempted add matrices with different number of columns: "
<< a.m_ << ", " << b.m_
<< abort(FatalError);
}
Matrix<Type> ab(a.n_, a.m_);
Form ab(a.n_, a.m_);
Type* abv = ab.v_[0];
const Type* av = a.v_[0];
@ -405,17 +405,17 @@ Foam::Matrix<Type> Foam::operator-(const Matrix<Type>& a, const Matrix<Type>& b)
}
template<class Type>
Foam::Matrix<Type> Foam::operator*(const scalar s, const Matrix<Type>& a)
template<class Form, class Type>
Form Foam::operator*(const scalar s, const Matrix<Form, Type>& a)
{
Matrix<Type> sa(a.n_, a.m_);
Form sa(a.n_, a.m_);
if (a.n_ && a.m_)
{
Type* sav = sa.v_[0];
const Type* av = a.v_[0];
label nm = a.n_*a.m_;;
label nm = a.n_*a.m_;
for (register label i=0; i<nm; i++)
{
sav[i] = s*av[i];

View File

@ -51,37 +51,26 @@ namespace Foam
// Forward declaration of friend functions and operators
template<class Type> class Matrix;
template<class Form, class Type> class Matrix;
template<class Type> const Type& max(const Matrix<Type>&);
template<class Type> const Type& min(const Matrix<Type>&);
template<class Type> Matrix<Type> operator-(const Matrix<Type>&);
template<class Type> Matrix<Type> operator+
template<class Form, class Type> Istream& operator>>
(
const Matrix<Type>&,
const Matrix<Type>&
);
template<class Type> Matrix<Type> operator-
(
const Matrix<Type>&,
const Matrix<Type>&
);
template<class Type> Matrix<Type> operator*
(
const scalar,
const Matrix<Type>&
Istream&,
Matrix<Form, Type>&
);
template<class Type> Istream& operator>>(Istream&, Matrix<Type>&);
template<class Type> Ostream& operator<<(Ostream&, const Matrix<Type>&);
template<class Form, class Type> Ostream& operator<<
(
Ostream&,
const Matrix<Form, Type>&
);
/*---------------------------------------------------------------------------*\
Class Matrix Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
template<class Form, class Type>
class Matrix
{
// Private data
@ -112,13 +101,13 @@ public:
Matrix(const label n, const label m, const Type&);
//- Copy constructor.
Matrix(const Matrix<Type>&);
Matrix(const Matrix<Form, Type>&);
//- Construct from Istream.
Matrix(Istream&);
//- Clone
inline autoPtr<Matrix<Type> > clone() const;
inline autoPtr<Matrix<Form, Type> > clone() const;
// Destructor
@ -129,7 +118,7 @@ public:
// Member functions
//- Return a null Matrix
static const Matrix<Type>& null();
static const Matrix<Form, Type>& null();
// Access
@ -160,7 +149,11 @@ public:
//- Transfer the contents of the argument Matrix into this Matrix
// and annull the argument Matrix.
void transfer(Matrix<Type>&);
void transfer(Matrix<Form, Type>&);
//- Return the transpose of the matrix
Form T() const;
// Member operators
@ -171,52 +164,49 @@ public:
//- Return subscript-checked element of constant Matrix.
inline const Type* operator[](const label) const;
//- Return the transpose of the matrix
Matrix<Type> T() const;
//- Assignment operator. Takes linear time.
void operator=(const Matrix<Type>&);
void operator=(const Matrix<Form, Type>&);
//- Assignment of all entries to the given value
void operator=(const Type&);
// Friend functions
friend const Type& max<Type>(const Matrix<Type>&);
friend const Type& min<Type>(const Matrix<Type>&);
// Friend operators
friend Matrix<Type> operator-<Type>(const Matrix<Type>&);
friend Matrix<Type> operator+<Type>
(
const Matrix<Type>&,
const Matrix<Type>&
);
friend Matrix<Type> operator-<Type>
(
const Matrix<Type>&,
const Matrix<Type>&
);
friend Matrix<Type> operator*<Type>
(
const scalar,
const Matrix<Type>&
);
// IOstream operators
//- Read Matrix from Istream, discarding contents of existing Matrix.
friend Istream& operator>> <Type>(Istream&, Matrix<Type>&);
friend Istream& operator>> <Form, Type>(Istream&, Matrix<Form, Type>&);
// Write Matrix to Ostream.
friend Ostream& operator<< <Type>(Ostream&, const Matrix<Type>&);
friend Ostream& operator<< <Form, Type>(Ostream&, const Matrix<Form, Type>&);
};
// Global functions and operators
template<class Form, class Type> const Type& max(const Matrix<Form, Type>&);
template<class Form, class Type> const Type& min(const Matrix<Form, Type>&);
template<class Form, class Type> Form operator-(const Matrix<Form, Type>&);
template<class Form, class Type> Form operator+
(
const Matrix<Form, Type>&,
const Matrix<Form, Type>&
);
template<class Form, class Type> Form operator-
(
const Matrix<Form, Type>&,
const Matrix<Form, Type>&
);
template<class Form, class Type> Form operator*
(
const scalar,
const Matrix<Form, Type>&
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -26,8 +26,8 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::Matrix<Type>::Matrix()
template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix()
:
n_(0),
m_(0),
@ -35,67 +35,67 @@ inline Foam::Matrix<Type>::Matrix()
{}
template<class Type>
inline Foam::autoPtr<Foam::Matrix<Type> > Foam::Matrix<Type>::clone() const
template<class Form, class Type>
inline Foam::autoPtr<Foam::Matrix<Form, Type> > Foam::Matrix<Form, Type>::clone() const
{
return autoPtr<Matrix<Type> >(new Matrix<Type>(*this));
return autoPtr<Matrix<Form, Type> >(new Matrix<Form, Type>(*this));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
//- Return the number of rows
template<class Type>
inline Foam::label Foam::Matrix<Type>::n() const
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::n() const
{
return n_;
}
template<class Type>
inline Foam::label Foam::Matrix<Type>::m() const
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::m() const
{
return m_;
}
template<class Type>
inline Foam::label Foam::Matrix<Type>::size() const
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::size() const
{
return n_*m_;
}
template<class Type>
inline void Foam::Matrix<Type>::checki(const label i) const
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checki(const label i) const
{
if (!n_)
{
FatalErrorIn("Matrix<Type>::checki(const label)")
FatalErrorIn("Matrix<Form, Type>::checki(const label)")
<< "attempt to access element from zero sized row"
<< abort(FatalError);
}
else if (i<0 || i>=n_)
{
FatalErrorIn("Matrix<Type>::checki(const label)")
FatalErrorIn("Matrix<Form, Type>::checki(const label)")
<< "index " << i << " out of range 0 ... " << n_-1
<< abort(FatalError);
}
}
template<class Type>
inline void Foam::Matrix<Type>::checkj(const label j) const
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checkj(const label j) const
{
if (!m_)
{
FatalErrorIn("Matrix<Type>::checkj(const label)")
FatalErrorIn("Matrix<Form, Type>::checkj(const label)")
<< "attempt to access element from zero sized column"
<< abort(FatalError);
}
else if (j<0 || j>=m_)
{
FatalErrorIn("Matrix<Type>::checkj(const label)")
FatalErrorIn("Matrix<Form, Type>::checkj(const label)")
<< "index " << j << " out of range 0 ... " << m_-1
<< abort(FatalError);
}
@ -104,8 +104,8 @@ inline void Foam::Matrix<Type>::checkj(const label j) const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
inline Type* Foam::Matrix<Type>::operator[](const label i)
template<class Form, class Type>
inline Type* Foam::Matrix<Form, Type>::operator[](const label i)
{
# ifdef FULLDEBUG
checki(i);
@ -114,8 +114,8 @@ inline Type* Foam::Matrix<Type>::operator[](const label i)
}
template<class Type>
inline const Type* Foam::Matrix<Type>::operator[](const label i) const
template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::operator[](const label i) const
{
# ifdef FULLDEBUG
checki(i);

View File

@ -32,8 +32,8 @@ License
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
template<class Type>
Foam::Matrix<Type>::Matrix(Istream& is)
template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(Istream& is)
:
n_(0),
m_(0),
@ -43,17 +43,17 @@ Foam::Matrix<Type>::Matrix(Istream& is)
}
template<class Type>
Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
template<class Form, class Type>
Foam::Istream& Foam::operator>>(Istream& is, Matrix<Form, Type>& M)
{
// Anull matrix
M.clear();
is.fatalCheck("operator>>(Istream&, Matrix<Type>&)");
is.fatalCheck("operator>>(Istream&, Matrix<Form, Type>&)");
token firstToken(is);
is.fatalCheck("operator>>(Istream&, Matrix<Type>&) : reading first token");
is.fatalCheck("operator>>(Istream&, Matrix<Form, Type>&) : reading first token");
if (firstToken.isLabel())
{
@ -88,7 +88,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
is.fatalCheck
(
"operator>>(Istream&, Matrix<Type>&) : "
"operator>>(Istream&, Matrix<Form, Type>&) : "
"reading entry"
);
}
@ -103,7 +103,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
is.fatalCheck
(
"operator>>(Istream&, Matrix<Type>&) : "
"operator>>(Istream&, Matrix<Form, Type>&) : "
"reading the single entry"
);
@ -128,7 +128,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
is.fatalCheck
(
"operator>>(Istream&, Matrix<Type>&) : "
"operator>>(Istream&, Matrix<Form, Type>&) : "
"reading the binary block"
);
}
@ -136,7 +136,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
}
else
{
FatalIOErrorIn("operator>>(Istream&, Matrix<Type>&)", is)
FatalIOErrorIn("operator>>(Istream&, Matrix<Form, Type>&)", is)
<< "incorrect first token, expected <int>, found "
<< firstToken.info()
<< exit(FatalIOError);
@ -146,8 +146,8 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Type>& M)
}
template<class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Type>& M)
template<class Form, class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
{
label nm = M.n_*M.m_;

View File

@ -0,0 +1,92 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::RectangularMatrix
Description
A templated 2D rectangular matrix of objects of \<T\>, where the n x n matrix
dimension is known and used for subscript bounds checking, etc.
SourceFiles
RectangularMatrixI.H
RectangularMatrix.C
\*---------------------------------------------------------------------------*/
#ifndef RectangularMatrix_H
#define RectangularMatrix_H
#include "Matrix.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class Matrix Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class RectangularMatrix
:
public Matrix<RectangularMatrix<Type>, Type>
{
public:
// Constructors
//- Null constructor.
inline RectangularMatrix();
//- Construct given number of rows and columns,
inline RectangularMatrix(const label m, const label n);
//- Construct with given number of rows and columns
// and value for all elements.
inline RectangularMatrix(const label m, const label n, const Type&);
//- Construct from Istream.
inline RectangularMatrix(Istream&);
//- Clone
inline autoPtr<RectangularMatrix<Type> > clone() const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
# include "RectangularMatrixI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,70 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix()
:
Matrix<RectangularMatrix<Type>, Type>()
{}
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix
(
const label m,
const label n
)
:
Matrix<RectangularMatrix<Type>, Type>(m, n)
{}
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix
(
const label m,
const label n,
const Type& t
)
:
Matrix<RectangularMatrix<Type>, Type>(m, n, t)
{}
template<class Type>
inline Foam::RectangularMatrix<Type>::RectangularMatrix(Istream& is)
:
Matrix<RectangularMatrix<Type>, Type>(is)
{}
template<class Type>
inline Foam::autoPtr<Foam::RectangularMatrix<Type> >
Foam::RectangularMatrix<Type>::clone() const
{
return autoPtr<RectangularMatrix<Type> >(new RectangularMatrix<Type>(*this));
}
// ************************************************************************* //

View File

@ -0,0 +1,96 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::SquareMatrix
Description
A templated 2D square matrix of objects of \<T\>, where the n x n matrix
dimension is known and used for subscript bounds checking, etc.
SourceFiles
SquareMatrixI.H
SquareMatrix.C
\*---------------------------------------------------------------------------*/
#ifndef SquareMatrix_H
#define SquareMatrix_H
#include "Matrix.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class Matrix Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class SquareMatrix
:
public Matrix<SquareMatrix<Type>, Type>
{
public:
// Constructors
//- Null constructor.
inline SquareMatrix();
//- Construct given number of rows/columns.
inline SquareMatrix(const label n);
//- Construct given number of rows and columns,
// It checks that m == n.
inline SquareMatrix(const label m, const label n);
//- Construct with given number of rows/columns
// and value for all elements.
inline SquareMatrix(const label n, const Type&);
//- Construct from Istream.
inline SquareMatrix(Istream&);
//- Clone
inline autoPtr<SquareMatrix<Type> > clone() const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
# include "SquareMatrixI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,75 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix()
:
Matrix<SquareMatrix<Type>, Type>()
{}
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix(const label n)
:
Matrix<SquareMatrix<Type>, Type>(n, n)
{}
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix(const label m, const label n)
:
Matrix<SquareMatrix<Type>, Type>(m, n)
{
if (m != n)
{
FatalErrorIn
(
"SquareMatrix<Type>::SquareMatrix(const label m, const label n)"
) << "m != n for constructing a square matrix" << exit(FatalError);
}
}
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix(const label n, const Type& t)
:
Matrix<SquareMatrix<Type>, Type>(n, t)
{}
template<class Type>
inline Foam::SquareMatrix<Type>::SquareMatrix(Istream& is)
:
Matrix<SquareMatrix<Type>, Type>(is)
{}
template<class Type>
inline Foam::autoPtr<Foam::SquareMatrix<Type> >
Foam::SquareMatrix<Type>::clone() const
{
return autoPtr<SquareMatrix<Type> >(new SquareMatrix<Type>(*this));
}
// ************************************************************************* //

View File

@ -26,12 +26,12 @@ License
#include "SVD.H"
#include "scalarList.H"
#include "scalarMatrix.H"
#include "scalarMatrices.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * //
Foam::SVD::SVD(const Matrix<scalar>& A, const scalar minCondition)
Foam::SVD::SVD(const scalarRectangularMatrix& A, const scalar minCondition)
:
U_(A),
V_(A.m(), A.m()),
@ -294,8 +294,11 @@ Foam::SVD::SVD(const Matrix<scalar>& A, const scalar minCondition)
}
if (its == 34)
{
WarningIn("SVD::SVD(Matrix<scalar>& A)")
<< "no convergence in 35 SVD iterations"
WarningIn
(
"SVD::SVD"
"(scalarRectangularMatrix& A, const scalar minCondition)"
) << "no convergence in 35 SVD iterations"
<< endl;
}
@ -375,7 +378,7 @@ Foam::SVD::SVD(const Matrix<scalar>& A, const scalar minCondition)
multiply(VSinvUt_, V_, inv(S_), U_.T());
// test SVD
/*Matrix<scalar> SVDA(A.n(), A.m());
/*scalarRectangularMatrix SVDA(A.n(), A.m());
multiply(SVDA, U_, S_, transpose(V_));
scalar maxDiff = 0;
scalar diff = 0;

View File

@ -23,12 +23,13 @@ License
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
SVD
Foam::SVD
Description
Singular value decomposition of a rectangular matrix.
SourceFiles
SVDI.H
SVD.C
\*---------------------------------------------------------------------------*/
@ -36,8 +37,7 @@ SourceFiles
#ifndef SVD_H
#define SVD_H
#include "DiagonalMatrix.H"
#include "Matrix.H"
#include "scalarMatrices.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -56,16 +56,16 @@ class SVD
// Private data
//- Rectangular matrix with the same dimensions as the input
Matrix<scalar> U_;
scalarRectangularMatrix U_;
//- square matrix V
Matrix<scalar> V_;
scalarRectangularMatrix V_;
//- The singular values
DiagonalMatrix<scalar> S_;
//- The matrix product V S^(-1) U^T
Matrix<scalar> VSinvUt_;
scalarRectangularMatrix VSinvUt_;
//- The number of zero singular values
label nZeros_;
@ -88,22 +88,22 @@ public:
// Constructors
//- Construct from a rectangular Matrix
SVD(const Matrix<scalar>& A, const scalar minCondition = 0);
SVD(const scalarRectangularMatrix& A, const scalar minCondition = 0);
// Access functions
//- Return U
inline const Matrix<scalar>& U() const;
inline const scalarRectangularMatrix& U() const;
//- Return the square matrix V
inline const Matrix<scalar>& V() const;
inline const scalarRectangularMatrix& V() const;
//- Return the singular values
inline const DiagonalMatrix<scalar>& S() const;
inline const scalarDiagonalMatrix& S() const;
//- Return VSinvUt (the pseudo inverse)
inline const Matrix<scalar>& VSinvUt() const;
inline const scalarRectangularMatrix& VSinvUt() const;
//- Return the number of zero singular values
inline label nZeros() const;

View File

@ -35,22 +35,22 @@ inline const T Foam::SVD::sign(const T& a, const T& b)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline const Foam::Matrix<Foam::scalar>& Foam::SVD::U() const
inline const Foam::scalarRectangularMatrix& Foam::SVD::U() const
{
return U_;
}
inline const Foam::Matrix<Foam::scalar>& Foam::SVD::V() const
inline const Foam::scalarRectangularMatrix& Foam::SVD::V() const
{
return V_;
}
inline const Foam::DiagonalMatrix<Foam::scalar>& Foam::SVD::S() const
inline const Foam::scalarDiagonalMatrix& Foam::SVD::S() const
{
return S_;
}
inline const Foam::Matrix<Foam::scalar>& Foam::SVD::VSinvUt() const
inline const Foam::scalarRectangularMatrix& Foam::SVD::VSinvUt() const
{
return VSinvUt_;
}

View File

@ -24,39 +24,14 @@ License
\*---------------------------------------------------------------------------*/
#include "scalarMatrix.H"
#include "scalarMatrices.H"
#include "SVD.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::scalarMatrix::scalarMatrix()
{}
Foam::scalarMatrix::scalarMatrix(const label mSize)
:
Matrix<scalar>(mSize, mSize, 0.0)
{}
Foam::scalarMatrix::scalarMatrix(const Matrix<scalar>& matrix)
:
Matrix<scalar>(matrix)
{}
Foam::scalarMatrix::scalarMatrix(Istream& is)
:
Matrix<scalar>(is)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::scalarMatrix::LUDecompose
void Foam::LUDecompose
(
Matrix<scalar>& matrix,
scalarSquareMatrix& matrix,
labelList& pivotIndices
)
{
@ -81,8 +56,8 @@ void Foam::scalarMatrix::LUDecompose
{
FatalErrorIn
(
"scalarMatrix::LUdecompose"
"(Matrix<scalar>& matrix, labelList& rowIndices)"
"LUdecompose"
"(scalarSquareMatrix& matrix, labelList& rowIndices)"
) << "Singular matrix" << exit(FatalError);
}
@ -164,9 +139,9 @@ void Foam::scalarMatrix::LUDecompose
void Foam::multiply
(
Matrix<scalar>& ans, // value changed in return
const Matrix<scalar>& A,
const Matrix<scalar>& B
scalarRectangularMatrix& ans, // value changed in return
const scalarRectangularMatrix& A,
const scalarRectangularMatrix& B
)
{
if (A.m() != B.n())
@ -174,15 +149,15 @@ void Foam::multiply
FatalErrorIn
(
"multiply("
"Matrix<scalar>& answer "
"const Matrix<scalar>& A, "
"const Matrix<scalar>& B)"
"scalarRectangularMatrix& answer "
"const scalarRectangularMatrix& A, "
"const scalarRectangularMatrix& B)"
) << "A and B must have identical inner dimensions but A.m = "
<< A.m() << " and B.n = " << B.n()
<< abort(FatalError);
}
ans = Matrix<scalar>(A.n(), B.m(), scalar(0));
ans = scalarRectangularMatrix(A.n(), B.m(), scalar(0));
for(register label i = 0; i < A.n(); i++)
{
@ -199,10 +174,10 @@ void Foam::multiply
void Foam::multiply
(
Matrix<scalar>& ans, // value changed in return
const Matrix<scalar>& A,
const Matrix<scalar>& B,
const Matrix<scalar>& C
scalarRectangularMatrix& ans, // value changed in return
const scalarRectangularMatrix& A,
const scalarRectangularMatrix& B,
const scalarRectangularMatrix& C
)
{
if (A.m() != B.n())
@ -210,10 +185,10 @@ void Foam::multiply
FatalErrorIn
(
"multiply("
"const Matrix<scalar>& A, "
"const Matrix<scalar>& B, "
"const Matrix<scalar>& C, "
"Matrix<scalar>& answer)"
"const scalarRectangularMatrix& A, "
"const scalarRectangularMatrix& B, "
"const scalarRectangularMatrix& C, "
"scalarRectangularMatrix& answer)"
) << "A and B must have identical inner dimensions but A.m = "
<< A.m() << " and B.n = " << B.n()
<< abort(FatalError);
@ -224,16 +199,16 @@ void Foam::multiply
FatalErrorIn
(
"multiply("
"const Matrix<scalar>& A, "
"const Matrix<scalar>& B, "
"const Matrix<scalar>& C, "
"Matrix<scalar>& answer)"
"const scalarRectangularMatrix& A, "
"const scalarRectangularMatrix& B, "
"const scalarRectangularMatrix& C, "
"scalarRectangularMatrix& answer)"
) << "B and C must have identical inner dimensions but B.m = "
<< B.m() << " and C.n = " << C.n()
<< abort(FatalError);
}
ans = Matrix<scalar>(A.n(), C.m(), scalar(0));
ans = scalarRectangularMatrix(A.n(), C.m(), scalar(0));
for(register label i = 0; i < A.n(); i++)
{
@ -255,10 +230,10 @@ void Foam::multiply
void Foam::multiply
(
Matrix<scalar>& ans, // value changed in return
const Matrix<scalar>& A,
scalarRectangularMatrix& ans, // value changed in return
const scalarRectangularMatrix& A,
const DiagonalMatrix<scalar>& B,
const Matrix<scalar>& C
const scalarRectangularMatrix& C
)
{
if (A.m() != B.size())
@ -266,10 +241,10 @@ void Foam::multiply
FatalErrorIn
(
"multiply("
"const Matrix<scalar>& A, "
"const scalarRectangularMatrix& A, "
"const DiagonalMatrix<scalar>& B, "
"const Matrix<scalar>& C, "
"Matrix<scalar>& answer)"
"const scalarRectangularMatrix& C, "
"scalarRectangularMatrix& answer)"
) << "A and B must have identical inner dimensions but A.m = "
<< A.m() << " and B.n = " << B.size()
<< abort(FatalError);
@ -280,16 +255,16 @@ void Foam::multiply
FatalErrorIn
(
"multiply("
"const Matrix<scalar>& A, "
"const scalarRectangularMatrix& A, "
"const DiagonalMatrix<scalar>& B, "
"const Matrix<scalar>& C, "
"Matrix<scalar>& answer)"
"const scalarRectangularMatrix& C, "
"scalarRectangularMatrix& answer)"
) << "B and C must have identical inner dimensions but B.m = "
<< B.size() << " and C.n = " << C.n()
<< abort(FatalError);
}
ans = Matrix<scalar>(A.n(), C.m(), scalar(0));
ans = scalarRectangularMatrix(A.n(), C.m(), scalar(0));
for(register label i = 0; i < A.n(); i++)
{
@ -304,9 +279,9 @@ void Foam::multiply
}
Foam::Matrix<Foam::scalar> Foam::SVDinv
Foam::RectangularMatrix<Foam::scalar> Foam::SVDinv
(
const Matrix<scalar>& A,
const scalarRectangularMatrix& A,
scalar minCondition
)
{

View File

@ -0,0 +1,137 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
scalarMatrices
Description
Scalar matrices
SourceFiles
scalarMatrices.C
scalarMatricesTemplates.C
\*---------------------------------------------------------------------------*/
#ifndef scalarMatrices_H
#define scalarMatrices_H
#include "RectangularMatrix.H"
#include "SquareMatrix.H"
#include "DiagonalMatrix.H"
#include "scalarField.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef RectangularMatrix<scalar> scalarRectangularMatrix;
typedef SquareMatrix<scalar> scalarSquareMatrix;
typedef DiagonalMatrix<scalar> scalarDiagonalMatrix;
//- Solve the matrix using Gaussian elimination with pivoting,
// returning the solution in the source
template<class Type>
void solve(scalarSquareMatrix& matrix, Field<Type>& source);
//- Solve the matrix using Gaussian elimination with pivoting
// and return the solution
template<class Type>
void solve
(
Field<Type>& psi,
const scalarSquareMatrix& matrix,
const Field<Type>& source
);
//- LU decompose the matrix with pivoting
void LUDecompose
(
scalarSquareMatrix& matrix,
labelList& pivotIndices
);
//- LU back-substitution with given source, returning the solution
// in the source
template<class Type>
void LUBacksubstitute
(
const scalarSquareMatrix& luMmatrix,
const labelList& pivotIndices,
Field<Type>& source
);
//- Solve the matrix using LU decomposition with pivoting
// returning the LU form of the matrix and the solution in the source
template<class Type>
void LUsolve(scalarSquareMatrix& matrix, Field<Type>& source);
void multiply
(
scalarRectangularMatrix& answer, // value changed in return
const scalarRectangularMatrix& A,
const scalarRectangularMatrix& B
);
void multiply
(
scalarRectangularMatrix& answer, // value changed in return
const scalarRectangularMatrix& A,
const scalarRectangularMatrix& B,
const scalarRectangularMatrix& C
);
void multiply
(
scalarRectangularMatrix& answer, // value changed in return
const scalarRectangularMatrix& A,
const DiagonalMatrix<scalar>& B,
const scalarRectangularMatrix& C
);
//- Return the inverse of matrix A using SVD
scalarRectangularMatrix SVDinv
(
const scalarRectangularMatrix& A,
scalar minCondition = 0
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "scalarMatricesTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -24,15 +24,15 @@ License
\*---------------------------------------------------------------------------*/
#include "scalarMatrix.H"
#include "scalarMatrices.H"
#include "Swap.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::scalarMatrix::solve
void Foam::solve
(
Matrix<scalar>& tmpMatrix,
scalarSquareMatrix& tmpMatrix,
Field<Type>& sourceSol
)
{
@ -68,7 +68,7 @@ void Foam::scalarMatrix::solve
// Check that the system of equations isn't singular
if (mag(tmpMatrix[i][i]) < 1e-20)
{
FatalErrorIn("scalarMatrix::solve()")
FatalErrorIn("solve(scalarSquareMatrix&, Field<Type>& sourceSol)")
<< "Singular Matrix"
<< exit(FatalError);
}
@ -102,18 +102,23 @@ void Foam::scalarMatrix::solve
template<class Type>
void Foam::scalarMatrix::solve(Field<Type>& psi, const Field<Type>& source) const
void Foam::solve
(
Field<Type>& psi,
const scalarSquareMatrix& matrix,
const Field<Type>& source
)
{
Matrix<scalar> tmpMatrix = *this;
scalarSquareMatrix tmpMatrix = matrix;
psi = source;
solve(tmpMatrix, psi);
}
template<class Type>
void Foam::scalarMatrix::LUBacksubstitute
void Foam::LUBacksubstitute
(
const Matrix<scalar>& luMatrix,
const scalarSquareMatrix& luMatrix,
const labelList& pivotIndices,
Field<Type>& sourceSol
)
@ -160,9 +165,9 @@ void Foam::scalarMatrix::LUBacksubstitute
template<class Type>
void Foam::scalarMatrix::LUsolve
void Foam::LUsolve
(
Matrix<scalar>& matrix,
scalarSquareMatrix& matrix,
Field<Type>& sourceSol
)
{

View File

@ -1,155 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::scalarMatrix
Description
Foam::scalarMatrix
SourceFiles
scalarMatrix.C
\*---------------------------------------------------------------------------*/
#ifndef scalarMatrix_H
#define scalarMatrix_H
#include "Matrix.H"
#include "DiagonalMatrix.H"
#include "scalarField.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class scalarMatrix Declaration
\*---------------------------------------------------------------------------*/
class scalarMatrix
:
public Matrix<scalar>
{
public:
// Constructors
//- Construct null
scalarMatrix();
//- Construct given size
scalarMatrix(const label);
//- Construct from Matrix<scalar>
scalarMatrix(const Matrix<scalar>&);
//- Construct from Istream
scalarMatrix(Istream&);
// Member Functions
//- Solve the matrix using Gaussian elimination with pivoting,
// returning the solution in the source
template<class Type>
static void solve(Matrix<scalar>& matrix, Field<Type>& source);
//- Solve the matrix using Gaussian elimination with pivoting
// and return the solution
template<class Type>
void solve(Field<Type>& psi, const Field<Type>& source) const;
//- LU decompose the matrix with pivoting
static void LUDecompose
(
Matrix<scalar>& matrix,
labelList& pivotIndices
);
//- LU back-substitution with given source, returning the solution
// in the source
template<class Type>
static void LUBacksubstitute
(
const Matrix<scalar>& luMmatrix,
const labelList& pivotIndices,
Field<Type>& source
);
//- Solve the matrix using LU decomposition with pivoting
// returning the LU form of the matrix and the solution in the source
template<class Type>
static void LUsolve(Matrix<scalar>& matrix, Field<Type>& source);
};
// Global functions
void multiply
(
Matrix<scalar>& answer, // value changed in return
const Matrix<scalar>& A,
const Matrix<scalar>& B
);
void multiply
(
Matrix<scalar>& answer, // value changed in return
const Matrix<scalar>& A,
const Matrix<scalar>& B,
const Matrix<scalar>& C
);
void multiply
(
Matrix<scalar>& answer, // value changed in return
const Matrix<scalar>& A,
const DiagonalMatrix<scalar>& B,
const Matrix<scalar>& C
);
//- Return the inverse of matrix A using SVD
Matrix<scalar> SVDinv(const Matrix<scalar>& A, scalar minCondition = 0);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "scalarMatrixTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -31,7 +31,7 @@ License
template<class Type>
Foam::simpleMatrix<Type>::simpleMatrix(const label mSize)
:
scalarMatrix(mSize),
scalarSquareMatrix(mSize),
source_(mSize, pTraits<Type>::zero)
{}
@ -39,11 +39,11 @@ Foam::simpleMatrix<Type>::simpleMatrix(const label mSize)
template<class Type>
Foam::simpleMatrix<Type>::simpleMatrix
(
const scalarMatrix& matrix,
const scalarSquareMatrix& matrix,
const Field<Type>& source
)
:
scalarMatrix(matrix),
scalarSquareMatrix(matrix),
source_(source)
{}
@ -51,7 +51,7 @@ Foam::simpleMatrix<Type>::simpleMatrix
template<class Type>
Foam::simpleMatrix<Type>::simpleMatrix(Istream& is)
:
scalarMatrix(is),
scalarSquareMatrix(is),
source_(is)
{}
@ -61,10 +61,10 @@ Foam::simpleMatrix<Type>::simpleMatrix(Istream& is)
template<class Type>
Foam::Field<Type> Foam::simpleMatrix<Type>::solve() const
{
scalarMatrix tmpMatrix = *this;
scalarSquareMatrix tmpMatrix = *this;
Field<Type> sourceSol = source_;
scalarMatrix::solve(tmpMatrix, sourceSol);
Foam::solve(tmpMatrix, sourceSol);
return sourceSol;
}
@ -73,10 +73,10 @@ Foam::Field<Type> Foam::simpleMatrix<Type>::solve() const
template<class Type>
Foam::Field<Type> Foam::simpleMatrix<Type>::LUsolve() const
{
scalarMatrix luMatrix = *this;
scalarSquareMatrix luMatrix = *this;
Field<Type> sourceSol = source_;
scalarMatrix::LUsolve(luMatrix, sourceSol);
Foam::LUsolve(luMatrix, sourceSol);
return sourceSol;
}
@ -108,7 +108,7 @@ void Foam::simpleMatrix<Type>::operator=(const simpleMatrix<Type>& m)
<< abort(FatalError);
}
scalarMatrix::operator=(m);
scalarSquareMatrix::operator=(m);
source_ = m.source_;
}
@ -124,8 +124,8 @@ Foam::simpleMatrix<Type> Foam::operator+
{
return simpleMatrix<Type>
(
static_cast<const scalarMatrix&>(m1)
+ static_cast<const scalarMatrix&>(m2),
static_cast<const scalarSquareMatrix&>(m1)
+ static_cast<const scalarSquareMatrix&>(m2),
m1.source_ + m2.source_
);
}
@ -140,8 +140,8 @@ Foam::simpleMatrix<Type> Foam::operator-
{
return simpleMatrix<Type>
(
static_cast<const scalarMatrix&>(m1)
- static_cast<const scalarMatrix&>(m2),
static_cast<const scalarSquareMatrix&>(m1)
- static_cast<const scalarSquareMatrix&>(m2),
m1.source_ - m2.source_
);
}
@ -159,7 +159,7 @@ Foam::simpleMatrix<Type> Foam::operator*(const scalar s, const simpleMatrix<Type
template<class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const simpleMatrix<Type>& m)
{
os << static_cast<const scalarMatrix&>(m) << nl << m.source_;
os << static_cast<const scalarSquareMatrix&>(m) << nl << m.source_;
return os;
}

View File

@ -36,7 +36,7 @@ SourceFiles
#ifndef simpleMatrix_H
#define simpleMatrix_H
#include "scalarMatrix.H"
#include "scalarMatrices.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -48,27 +48,6 @@ namespace Foam
template<class Type>
class simpleMatrix;
template<class Type>
simpleMatrix<Type> operator+
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
template<class Type>
simpleMatrix<Type> operator-
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
template<class Type>
simpleMatrix<Type> operator*
(
const scalar,
const simpleMatrix<Type>&
);
template<class Type>
Ostream& operator<<
(
@ -84,7 +63,7 @@ Ostream& operator<<
template<class Type>
class simpleMatrix
:
public scalarMatrix
public scalarSquareMatrix
{
// Private data
@ -99,7 +78,7 @@ public:
simpleMatrix(const label);
//- Construct from components
simpleMatrix(const scalarMatrix&, const Field<Type>&);
simpleMatrix(const scalarSquareMatrix&, const Field<Type>&);
//- Construct from Istream
simpleMatrix(Istream&);
@ -137,27 +116,6 @@ public:
void operator=(const simpleMatrix<Type>&);
// Friend Operators
friend simpleMatrix<Type> operator+ <Type>
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
friend simpleMatrix<Type> operator- <Type>
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
friend simpleMatrix<Type> operator* <Type>
(
const scalar,
const simpleMatrix<Type>&
);
// Ostream Operator
friend Ostream& operator<< <Type>
@ -168,6 +126,30 @@ public:
};
// Global operators
template<class Type>
simpleMatrix<Type> operator+
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
template<class Type>
simpleMatrix<Type> operator-
(
const simpleMatrix<Type>&,
const simpleMatrix<Type>&
);
template<class Type>
simpleMatrix<Type> operator*
(
const scalar,
const simpleMatrix<Type>&
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -49,11 +49,11 @@ Foam::quadraticFitData::quadraticFitData
:
MeshObject<fvMesh, quadraticFitData>(mesh),
centralWeight_(cWeight),
#ifdef SPHERICAL_GEOMETRY
# ifdef SPHERICAL_GEOMETRY
dim_(2),
#else
# else
dim_(mesh.nGeometricD()),
#endif
# endif
minSize_
(
dim_ == 1 ? 3 :
@ -204,7 +204,7 @@ Foam::label Foam::quadraticFitData::calcFit
scalar scale = 0;
// calculate the matrix of the polynomial components
Matrix<scalar> B(C.size(), minSize_, scalar(0));
scalarRectangularMatrix B(C.size(), minSize_, scalar(0));
for(label ip = 0; ip < C.size(); ip++)
{
@ -212,11 +212,11 @@ Foam::label Foam::quadraticFitData::calcFit
scalar px = (p - p0)&idir;
scalar py = (p - p0)&jdir;
#ifndef SPHERICAL_GEOMETRY
# ifndef SPHERICAL_GEOMETRY
scalar pz = (p - p0)&kdir;
#else
# else
scalar pz = mag(p) - mag(p0);
#endif
# endif
if (ip == 0)
{

View File

@ -114,7 +114,7 @@ Foam::scalarField Foam::chemistryModel::omega
forAll(reactions_, i)
{
const reaction& R = reactions_[i];
scalar omegai = omega
(
R, c, T, p, pf, cf, lRef, pr, cr, rRef
@ -164,13 +164,13 @@ Foam::scalar Foam::chemistryModel::omega
pf = 1.0;
pr = 1.0;
label Nl = R.lhs().size();
label Nr = R.rhs().size();
label slRef = 0;
lRef = R.lhs()[slRef].index;
pf = kf;
for(label s=1; s<Nl; s++)
{
@ -212,7 +212,7 @@ Foam::scalar Foam::chemistryModel::omega
label srRef = 0;
rRef = R.rhs()[srRef].index;
// find the matrix element and element position for the rhs
pr = kr;
for(label s=1; s<Nr; s++)
@ -250,7 +250,7 @@ Foam::scalar Foam::chemistryModel::omega
{
pr *= pow(cr, exp-1.0);
}
}
return pf*cf - pr*cr;
@ -313,12 +313,12 @@ void Foam::chemistryModel::jacobian
const scalar t,
const scalarField& c,
scalarField& dcdt,
Matrix<scalar>& dfdc
scalarSquareMatrix& dfdc
) const
{
scalar T = c[Ns_];
scalar p = c[Ns_ + 1];
scalarField c2(Ns(), 0.0);
for(label i=0; i<Ns(); i++)
{
@ -470,23 +470,23 @@ Foam::tmp<Foam::volScalarField> Foam::chemistryModel::tc() const
scalar pi = thermo_.p()[celli];
scalarField c(Ns_);
scalar cSum = 0.0;
for(label i=0; i<Ns_; i++)
{
scalar Yi = Y_[i][celli];
c[i] = rhoi*Yi/specieThermo_[i].W();
cSum += c[i];
}
forAll(reactions_, i)
{
const reaction& R = reactions_[i];
omega
(
R, c, Ti, pi, pf, cf, lRef, pr, cr, rRef
);
forAll(R.rhs(), s)
{
scalar sr = R.rhs()[s].stoichCoeff;
@ -544,22 +544,22 @@ void Foam::chemistryModel::calculate()
{
RR_[i][celli] = 0.0;
}
scalar rhoi = rho_[celli];
scalar Ti = thermo_.T()[celli];
scalar pi = thermo_.p()[celli];
scalarField c(Ns_);
scalarField dcdt(nEqns(), 0.0);
for(label i=0; i<Ns_; i++)
{
scalar Yi = Y_[i][celli];
c[i] = rhoi*Yi/specieThermo_[i].W();
}
dcdt = omega(c, Ti, pi);
for(label i=0; i<Ns_; i++)
{
RR_[i][celli] = dcdt[i]*specieThermo_[i].W();
@ -624,7 +624,7 @@ Foam::scalar Foam::chemistryModel::solve(const scalar t0, const scalar deltaT)
for(label i=0; i<Ns_; i++)
{
mixture += (c[i]/cTot)*specieThermo_[i];
}
}
Ti = mixture.TH(hi, Ti);
timeLeft -= dt;
@ -639,7 +639,7 @@ Foam::scalar Foam::chemistryModel::solve(const scalar t0, const scalar deltaT)
for(label i=0; i<Ns_; i++)
{
WTot += c[i]*specieThermo_[i].W();
}
}
WTot /= cTot;
for(label i=0; i<Ns_; i++)

View File

@ -39,7 +39,6 @@ SourceFiles
#include "hCombustionThermo.H"
#include "reactingMixture.H"
#include "Matrix.H"
#include "ODE.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -251,7 +250,7 @@ public:
const scalar t,
const scalarField& c,
scalarField& dcdt,
Matrix<scalar>& dfdc
scalarSquareMatrix& dfdc
) const;
//- Calculates the reaction rates