ENH: add col/row access methods for Tensor2D (#1430)

This commit is contained in:
Mark Olesen
2019-09-23 14:40:47 +02:00
committed by Andrew Heather
parent 61dfa9b544
commit 79dff5d8fe
4 changed files with 219 additions and 11 deletions

View File

@ -40,7 +40,7 @@ int main()
Info<< "tensor " << t3 << nl; Info<< "tensor " << t3 << nl;
t3.row<2>(Zero); t3.row<2>(Zero);
Info<< "replaced row<1> = " << t3.row<2>() << nl; Info<< "replaced row<2> = " << t3.row<2>() << nl;
Info<< "tensor " << t3 << nl; Info<< "tensor " << t3 << nl;
triad tr3(t3); triad tr3(t3);

View File

@ -6,9 +6,37 @@ using namespace Foam;
int main() int main()
{ {
vector2D v1(1, 2), v2(3, 4); vector2D v1(1, 2), v2(3, 4);
tensor2D t = v1*v2; tensor2D t3 = v1*v2;
Info<< "v1(1, 2)*v2(3, 4) = " << t << endl; Info<< v1 << "*" << v2 << " = " << t3 << endl;
{
Info<< "rows:" << nl;
for (direction i=0; i < 2; ++i)
{
Info<< " (" << i << ") = " << t3.row(i) << nl;
}
}
{
Info<< "cols:" << nl;
for (direction i=0; i < 2; ++i)
{
Info<< " (" << i << ") = " << t3.col(i) << nl;
}
Info<< "col<0> = " << t3.col<0>() << nl;
Info<< "col<1> = " << t3.col<1>() << nl;
// Compilation error: Info << "col<3> = " << t3.col<3>() << nl;
t3.col<0>({0, 2});
Info<< "replaced col<0> = " << t3.col<0>() << nl;
Info<< "tensor " << t3 << nl;
t3.row<1>(Zero);
Info<< "replaced row<1> = " << t3.row<1>() << nl;
Info<< "tensor " << t3 << nl;
}
Info<< nl;
return 0; return 0;
} }

View File

@ -91,13 +91,13 @@ public:
inline Tensor2D(const Foam::zero); inline Tensor2D(const Foam::zero);
//- Construct given VectorSpace //- Construct given VectorSpace
inline Tensor2D(const VectorSpace<Tensor2D<Cmpt>, Cmpt, 4>&); inline Tensor2D(const VectorSpace<Tensor2D<Cmpt>, Cmpt, 4>& vs);
//- Construct given SymmTensor2D //- Construct given SymmTensor2D
inline Tensor2D(const SymmTensor2D<Cmpt>&); inline Tensor2D(const SymmTensor2D<Cmpt>& st);
//- Construct given SphericalTensor2D //- Construct given SphericalTensor2D
inline Tensor2D(const SphericalTensor2D<Cmpt>&); inline Tensor2D(const SphericalTensor2D<Cmpt>& st);
//- Construct given the two row vectors //- Construct given the two row vectors
inline Tensor2D inline Tensor2D
@ -114,7 +114,7 @@ public:
); );
//- Construct from Istream //- Construct from Istream
Tensor2D(Istream&); inline Tensor2D(Istream&);
// Member Functions // Member Functions
@ -140,6 +140,24 @@ public:
//- Extract vector for column 1 //- Extract vector for column 1
inline Vector2D<Cmpt> cy() const; inline Vector2D<Cmpt> cy() const;
//- Extract vector for given column.
// Compile-time check of column index.
template<direction Col>
inline Vector2D<Cmpt> col() const;
//- Extract vector for given column (0,1).
// Runtime check of column index.
inline Vector2D<Cmpt> col(const direction c) const;
//- Set values of given column
// Compile-time check of column index.
template<direction Col>
inline void col(const Vector2D<Cmpt>& v);
//- Set values of given column (0,1)
// Runtime check of column index.
inline void col(const direction c, const Vector2D<Cmpt>& v);
// Row-vector access. // Row-vector access.
@ -149,10 +167,32 @@ public:
//- Extract vector for row 1 //- Extract vector for row 1
inline Vector2D<Cmpt> y() const; inline Vector2D<Cmpt> y() const;
//- Extract vector for given row.
// Compile-time check of row index.
template<direction Row>
inline Vector2D<Cmpt> row() const;
//- Extract vector for given row (0,1)
// Runtime check of row index.
inline Vector2D<Cmpt> row(const direction r) const;
//- Set values of given row
// Compile-time check of row index.
template<direction Row>
inline void row(const Vector2D<Cmpt>& v);
//- Set values of given row (0,1)
// Runtime check of row index.
inline void row(const direction r, const Vector2D<Cmpt>& v);
//- Return vector for given row (0,1)
// Runtime check of row index.
inline Vector2D<Cmpt> vectorComponent(const direction cmpt) const;
// Tensor Operations // Tensor Operations
//- Transpose //- Return transpose
inline Tensor2D<Cmpt> T() const; inline Tensor2D<Cmpt> T() const;
//- Inner-product of this with another Tensor2D. //- Inner-product of this with another Tensor2D.
@ -164,10 +204,10 @@ public:
// Member Operators // Member Operators
//- Copy SymmTensor2D //- Copy assign from SymmTensor2D
inline void operator=(const SymmTensor2D<Cmpt>&); inline void operator=(const SymmTensor2D<Cmpt>&);
//- Copy SphericalTensor2D //- Copy assign from SphericalTensor2D
inline void operator=(const SphericalTensor2D<Cmpt>&); inline void operator=(const SphericalTensor2D<Cmpt>&);
}; };

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -174,6 +174,146 @@ inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::cy() const
} }
template<class Cmpt>
template<Foam::direction Col>
inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::col() const
{
if (Col == 0) return cx();
else if (Col == 1) return cy();
static_assert(Col < 2, "Invalid column access");
return Zero;
}
template<class Cmpt>
inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::col(const direction c) const
{
switch (c)
{
case 0: return cx(); break;
case 1: return cy(); break;
}
FatalErrorInFunction
<< "Invalid column access " << c << abort(FatalError);
return Zero;
}
template<class Cmpt>
template<Foam::direction Row>
inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::row() const
{
if (Row == 0) return x();
else if (Row == 1) return y();
static_assert(Row < 2, "Invalid row access");
return Zero;
}
template<class Cmpt>
inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::row(const direction r) const
{
switch (r)
{
case 0: return x(); break;
case 1: return y(); break;
}
FatalErrorInFunction
<< "Invalid row access " << r << abort(FatalError);
return Zero;
}
template<class Cmpt>
template<Foam::direction Col>
inline void Foam::Tensor2D<Cmpt>::col(const Vector2D<Cmpt>& v)
{
if (Col == 0)
{
this->v_[XX] = v.x();
this->v_[YX] = v.y();
}
else if (Col == 1)
{
this->v_[XY] = v.x();
this->v_[YY] = v.y();
}
static_assert(Col < 2, "Invalid column access");
}
template<class Cmpt>
template<Foam::direction Row>
inline void Foam::Tensor2D<Cmpt>::row(const Vector2D<Cmpt>& v)
{
if (Row == 0)
{
this->v_[XX] = v.x(); this->v_[XY] = v.y();
}
else if (Row == 1)
{
this->v_[YX] = v.x(); this->v_[YY] = v.y();
}
static_assert(Row < 2, "Invalid row access");
}
template<class Cmpt>
inline void Foam::Tensor2D<Cmpt>::col
(
const direction c,
const Vector2D<Cmpt>& v
)
{
switch (c)
{
case 0: col<0>(v); break;
case 1: col<1>(v); break;
default:
FatalErrorInFunction
<< "Invalid column access " << c << abort(FatalError);
break;
}
}
template<class Cmpt>
inline void Foam::Tensor2D<Cmpt>::row
(
const direction r,
const Vector2D<Cmpt>& v
)
{
switch (r)
{
case 0: row<0>(v); break;
case 1: row<1>(v); break;
default:
FatalErrorInFunction
<< "Invalid row access " << r << abort(FatalError);
break;
}
}
template<class Cmpt>
inline Foam::Vector2D<Cmpt> Foam::Tensor2D<Cmpt>::vectorComponent
(
const direction cmpt
) const
{
return row(cmpt);
}
// * * * * * * * * * * * * * * * Member Operations * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operations * * * * * * * * * * * * * //
template<class Cmpt> template<class Cmpt>