Added copy constructors

git-svn-id: http://svn.sintef.no/trondheim/IFEM/trunk@848 e10b68d5-8a6e-419e-a041-bce267b0401d
This commit is contained in:
kmo 2011-02-25 17:26:09 +00:00 committed by Knut Morten Okstad
parent 86cff9c8c8
commit 2b8d092d3d
2 changed files with 31 additions and 15 deletions

View File

@ -1,4 +1,4 @@
// $Id: Tensor.C,v 1.11 2010-12-20 13:52:42 kmo Exp $
// $Id$
//==============================================================================
//!
//! \file Tensor.C
@ -51,6 +51,13 @@ Tensor::Tensor (const std::vector<real>& t1, const std::vector<real>& t2) : n(3)
}
Tensor::Tensor (const Tensor& T) : n(T.n)
{
v.resize(n*n);
this->operator=(T);
}
Tensor& Tensor::operator= (const Tensor& T)
{
if (v.size() == T.v.size())
@ -132,17 +139,15 @@ Tensor& Tensor::operator*= (real val)
}
real Tensor::innerProd(const Tensor& T)
real Tensor::innerProd (const Tensor& T)
{
real value = 0.0;
if (v.size() == T.v.size()) {
std::vector<real> vec = T;
real value = real(0);
if (v.size() == T.v.size())
for (t_ind i = 0; i < v.size(); i++)
value += v[i]*vec[i];
}
value += v[i]*T.v[i];
else
std::cerr <<"Tensor::innerProd(const Tensor&): "
<<"Not implemented for tensors of different size."<< std::endl;
std::cerr <<"Tensor::innerProd(const Tensor&): "
<<"Not implemented for tensors of different size."<< std::endl;
return value;
}
@ -327,6 +332,13 @@ SymmTensor::SymmTensor (const std::vector<real>& vec) : Tensor(0)
}
SymmTensor::SymmTensor (const SymmTensor& T) : Tensor(0)
{
this->redim(T.n);
std::copy(T.v.begin(),T.v.end(),v.begin());
}
/*!
Perform the triple matrix product \f[{\bf A} = {\bf T}^T{\bf A T}\f]
where \b A = \a *this
@ -522,9 +534,9 @@ SymmTensor operator- (const SymmTensor& T, real a)
}
SymmTensor4::SymmTensor4 (const std::vector<real>& x, t_ind nsd) : v(x), n(nsd)
SymmTensor4::SymmTensor4 (const std::vector<real>& x, t_ind nsd)
: n(nsd), m(0), v(x)
{
m = 0;
if (n == 3)
m = 6;
else if (n == 2)
@ -532,7 +544,7 @@ SymmTensor4::SymmTensor4 (const std::vector<real>& x, t_ind nsd) : v(x), n(nsd)
else
std::cerr <<" *** Invalid fourth-order tensor, dim="<< n << std::endl;
if (v.size() < m*m)
if (v.size() < (size_t)m*m)
std::cerr <<" *** Invalid fourth-order tensor,"
<<" matrix represention too small, size="<< v.size() << std::endl;

View File

@ -1,4 +1,4 @@
// $Id: Tensor.h,v 1.9 2010-11-18 11:22:10 kmo Exp $
// $Id$
//==============================================================================
//!
//! \file Tensor.h
@ -44,6 +44,8 @@ public:
Tensor(const t_ind nsd) : n(nsd) { v.resize(n*n,real(0)); }
//! \brief Constructor creating a transformation from two tangent vectors.
Tensor(const std::vector<real>& t1, const std::vector<real>& t2);
//! \brief Copy constructor.
Tensor(const Tensor& T);
//! \brief Set to 0-tensor.
void zero() { std::fill(v.begin(),v.end(),real(0)); }
@ -76,14 +78,14 @@ public:
//! \brief Scaling operator.
Tensor& operator*=(real val);
//! \brief Inner product
//! \brief Inner product of two tensors.
real innerProd(const Tensor& T);
//! \brief Return the dimension of this tensor.
t_ind dim() const { return n; }
//! \brief Query whether this tensor is symmetric or not.
bool symmetric() const { return v.size() == n*(n+1)/2; }
bool symmetric() const { return v.size() == (size_t)(n*(n+1)/2); }
//! brief Query whether this tensor is zero within the given tolerance.
bool isZero(real tol = real(1.0e-6)) const;
@ -153,6 +155,8 @@ public:
SymmTensor(const t_ind nsd) : Tensor(nsd) { v.resize(n*(n+1)/2,real(0)); }
//! \brief Constructor creating a symmetric tensor from a vector.
SymmTensor(const std::vector<real>& vec);
//! \brief Copy constructor.
SymmTensor(const SymmTensor& T);
//! \brief Transpose the symmetric tensor (do nothing).
virtual Tensor& transpose() { return *this; }