Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
symmetrictensor.hh
Go to the documentation of this file.
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set ts=8 sw=4 et sts=4:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_SYMMETRICTENSOR_HH
8#define DUNE_FUFEM_SYMMETRICTENSOR_HH
9
12
20template <int dim, class field_type=double>
21class SymmetricTensor : public Dune::FieldVector<field_type,dim*(dim+1)/2>
22{
23
24public:
28 enum {dimension = dim};
29
30 enum {rows = dim};
31
32 enum {cols = dim};
33
39 SymmetricTensor(bool eye=false): Dune::FieldVector<field_type,dim*(dim+1)/2>(0.0)
40 {
41 if (eye)
42 for (int i = 0; i < dim; ++i)
43 (*this)[i] = 1.0;
44 }
45
50
56
61 {
62 field_type r = 0.0;
63 for (int i = 0; i < dim*(dim+1)/2; ++i)
64 r += (i>=dim ? 2 : 1)*(*this)[i]*B[i];
65 return r;
66 }
67
71 using Dune::template FieldVector<field_type,dim*(dim+1)/2>::operator=;
75 using Dune::template FieldVector<field_type,dim*(dim+1)/2>::operator*=;
76
84 {
85 for (int i = 0; i < dim; ++i)
86 {
87 y[i] += (*this)[i]*x[i];
88 int j = i;
89 while (++j < dim)
90 {
91 y[i] += (*this)[(i+1)*dim - i*(i+1)/2 + j-i-1]*x[j];
92 y[j] += (*this)[(i+1)*dim - i*(i+1)/2 + j-i-1]*x[i];
93 }
94 }
95 }
96
104 {
105 y = 0;
106 umv(x,y);
107 }
108
113 field_type& operator() (int i, int j)
114 {
115 if (i==j)
116 return (*this)[i];
117 else if (i<j)
118 return (*this)[(i+1)*dim - i*(i+1)/2 + (j-i) - 1];
119 else
120 return (*this)[(j+1)*dim - j*(j+1)/2 + (i-j) - 1];
121 }
122
127 const field_type& operator() (int i, int j) const
128 {
129 if (i==j)
130 return (*this)[i];
131 else if (i<j)
132 return (*this)[(i+1)*dim - i*(i+1)/2 + (j-i) - 1];
133 else
134 return (*this)[(j+1)*dim - j*(j+1)/2 + (i-j) - 1];
135 }
136
138 {
139 field_type ret = 0.0;
140 for (int i = 0; i < dim; ++i)
141 ret += (*this)[i];
142
143 return ret;
144 }
145
150 for (int i = 0; i < dim; ++i)
151 (*this)[i] += x;
152 }
153
157 void setDiag(field_type diagValue)
158 {
159 *this = 0.0;
160 addToDiag(diagValue);
161 }
162
167 {
168 *this = 0.0;
169 for (int i = 0; i < dim; ++i)
170 (*this)[i] = diagVector[i];
171 }
172
175
177 for (int i=0; i<dim; i++) {
178 mat[i][i] = (*this)[i];
179 for (int j=0; j<i; j++) {
180 mat[i][j] = (*this)[(j+1)*dim - j*(j+1)/2 + (i-j) - 1];
181 mat[j][i] = mat[i][j];
182 }
183 }
184
185 return mat;
186 }
187
188 /* A matrix-product has not been implemented */
189};
190
193template <int dim, class field_type>
195{
196 for (int i = 0; i < dim; ++i)
197 {
198 for (int j = 0; j < dim; ++j)
199 s << (j>0 ? " " : "") << E(i,j);
200
201 s << std::endl;
202 }
203 return s;
204}
205
206namespace Dune {
207 template< class K, int dim >
209 {
212 };
213}
214
215#endif
Matrix & mat
std::ostream & operator<<(std::ostream &s, const SymmetricTensor< dim, field_type > &E)
formatting tensor for standard output stream
Definition symmetrictensor.hh:194
typename FieldTraits< Type >::real_type real_t
typename FieldTraits< Type >::field_type field_t
size_type dim() const
virtual void operator()()=0
constexpr FieldVector() noexcept(std::is_nothrow_default_constructible_v< K >)
constexpr size_type dim() const
FieldTraits< value_type >::field_type field_type
A class implementing a 2nd order symmetric tensor.
Definition symmetrictensor.hh:22
@ dimension
Definition symmetrictensor.hh:28
void setDiag(const Dune::FieldVector< field_type, dim > &diagVector)
set diagonal according to vector entries and zero non-diagonal entries
Definition symmetrictensor.hh:166
void setDiag(field_type diagValue)
set diagonal to "constant" value and zero non-diagonal entries
Definition symmetrictensor.hh:157
field_type operator*(const SymmetricTensor< dim, field_type > &B) const
This is the contraction product of two tensors of 2nd stage .
Definition symmetrictensor.hh:60
void umv(const Dune::FieldVector< field_type, dim > &x, Dune::FieldVector< field_type, dim > &y) const
Matrix-Vector product.
Definition symmetrictensor.hh:83
@ rows
Definition symmetrictensor.hh:30
@ cols
Definition symmetrictensor.hh:32
void addToDiag(field_type x)
add constant value to the diagonal, leaving off-diagonal entries unaffected.
Definition symmetrictensor.hh:149
void mv(const Dune::FieldVector< field_type, dim > &x, Dune::FieldVector< field_type, dim > &y) const
Matrix-Vector product.
Definition symmetrictensor.hh:103
SymmetricTensor(field_type a)
Constructor for tensor with all entries being the same.
Definition symmetrictensor.hh:49
SymmetricTensor(const Dune::FieldVector< field_type, dim *(dim+1)/2 > fvector)
Construct from FieldVector of correct dimension.
Definition symmetrictensor.hh:55
field_type trace() const
Definition symmetrictensor.hh:137
SymmetricTensor(bool eye=false)
Default constructor.
Definition symmetrictensor.hh:39
Dune::FieldMatrix< field_type, dim, dim > matrix() const
Return the FieldMatrix representation of the symmetric tensor.
Definition symmetrictensor.hh:174
field_t< K > field_type
Definition symmetrictensor.hh:210
real_t< K > real_type
Definition symmetrictensor.hh:211
T endl(T... args)