diff --git a/applications/test/tensor/Test-tensor.C b/applications/test/tensor/Test-tensor.C index cd72200975..9371be3439 100644 --- a/applications/test/tensor/Test-tensor.C +++ b/applications/test/tensor/Test-tensor.C @@ -40,7 +40,7 @@ int main() Info<< "tensor " << t3 << nl; t3.row<2>(Zero); - Info<< "replaced row<1> = " << t3.row<2>() << nl; + Info<< "replaced row<2> = " << t3.row<2>() << nl; Info<< "tensor " << t3 << nl; triad tr3(t3); diff --git a/applications/test/tensor2D/Test-tensor2D.C b/applications/test/tensor2D/Test-tensor2D.C index 367c924470..a3f0ab0411 100644 --- a/applications/test/tensor2D/Test-tensor2D.C +++ b/applications/test/tensor2D/Test-tensor2D.C @@ -6,9 +6,37 @@ using namespace Foam; int main() { 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; } diff --git a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H index 45d8f132fe..f0cff04b73 100644 --- a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H +++ b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H @@ -91,13 +91,13 @@ public: inline Tensor2D(const Foam::zero); //- Construct given VectorSpace - inline Tensor2D(const VectorSpace, Cmpt, 4>&); + inline Tensor2D(const VectorSpace, Cmpt, 4>& vs); //- Construct given SymmTensor2D - inline Tensor2D(const SymmTensor2D&); + inline Tensor2D(const SymmTensor2D& st); //- Construct given SphericalTensor2D - inline Tensor2D(const SphericalTensor2D&); + inline Tensor2D(const SphericalTensor2D& st); //- Construct given the two row vectors inline Tensor2D @@ -114,7 +114,7 @@ public: ); //- Construct from Istream - Tensor2D(Istream&); + inline Tensor2D(Istream&); // Member Functions @@ -140,6 +140,24 @@ public: //- Extract vector for column 1 inline Vector2D cy() const; + //- Extract vector for given column. + // Compile-time check of column index. + template + inline Vector2D col() const; + + //- Extract vector for given column (0,1). + // Runtime check of column index. + inline Vector2D col(const direction c) const; + + //- Set values of given column + // Compile-time check of column index. + template + inline void col(const Vector2D& v); + + //- Set values of given column (0,1) + // Runtime check of column index. + inline void col(const direction c, const Vector2D& v); + // Row-vector access. @@ -149,10 +167,32 @@ public: //- Extract vector for row 1 inline Vector2D y() const; + //- Extract vector for given row. + // Compile-time check of row index. + template + inline Vector2D row() const; + + //- Extract vector for given row (0,1) + // Runtime check of row index. + inline Vector2D row(const direction r) const; + + //- Set values of given row + // Compile-time check of row index. + template + inline void row(const Vector2D& v); + + //- Set values of given row (0,1) + // Runtime check of row index. + inline void row(const direction r, const Vector2D& v); + + //- Return vector for given row (0,1) + // Runtime check of row index. + inline Vector2D vectorComponent(const direction cmpt) const; + // Tensor Operations - //- Transpose + //- Return transpose inline Tensor2D T() const; //- Inner-product of this with another Tensor2D. @@ -164,10 +204,10 @@ public: // Member Operators - //- Copy SymmTensor2D + //- Copy assign from SymmTensor2D inline void operator=(const SymmTensor2D&); - //- Copy SphericalTensor2D + //- Copy assign from SphericalTensor2D inline void operator=(const SphericalTensor2D&); }; diff --git a/src/OpenFOAM/primitives/Tensor2D/Tensor2DI.H b/src/OpenFOAM/primitives/Tensor2D/Tensor2DI.H index 725400c6b1..f5a88023e8 100644 --- a/src/OpenFOAM/primitives/Tensor2D/Tensor2DI.H +++ b/src/OpenFOAM/primitives/Tensor2D/Tensor2DI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -174,6 +174,146 @@ inline Foam::Vector2D Foam::Tensor2D::cy() const } +template +template +inline Foam::Vector2D Foam::Tensor2D::col() const +{ + if (Col == 0) return cx(); + else if (Col == 1) return cy(); + + static_assert(Col < 2, "Invalid column access"); + return Zero; +} + + +template +inline Foam::Vector2D Foam::Tensor2D::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 +template +inline Foam::Vector2D Foam::Tensor2D::row() const +{ + if (Row == 0) return x(); + else if (Row == 1) return y(); + + static_assert(Row < 2, "Invalid row access"); + return Zero; +} + + +template +inline Foam::Vector2D Foam::Tensor2D::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 +template +inline void Foam::Tensor2D::col(const Vector2D& 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 +template +inline void Foam::Tensor2D::row(const Vector2D& 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 +inline void Foam::Tensor2D::col +( + const direction c, + const Vector2D& 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 +inline void Foam::Tensor2D::row +( + const direction r, + const Vector2D& 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 +inline Foam::Vector2D Foam::Tensor2D::vectorComponent +( + const direction cmpt +) const +{ + return row(cmpt); +} + + // * * * * * * * * * * * * * * * Member Operations * * * * * * * * * * * * * // template