Chi-Tech
chi_meshtensor_rank2_dim3.h
Go to the documentation of this file.
1#ifndef _chi_meshtensor_rank2_dim3_h
2#define _chi_meshtensor_rank2_dim3_h
3
4#include "chi_meshvector.h"
5
6//###################################################################
7/**General rank 2 tensor to be used with Vector3*/
9{
10 std::vector<chi_mesh::Vector3> t; ///< Tensor entries.
11
12 /**Default constructor.*/
14 {
15 t.resize(3,chi_mesh::Vector3(0.0,0.0,0.0));
16 }
17 /**Constructor with specified value.
18 * \f$ T_{ii} = \alpha \f$*/
19 explicit TensorRank2Dim3(const double value)
20 {
21 t.resize(3,chi_mesh::Vector3(value,value,value));
22 }
23
24 /**Copy constructor.*/
26 {
27 this->t = that.t;
28 }
29
30 /**Component-wise copy*/
32 {
33 this->t = that.t;
34
35 return *this;
36 }
37
38 /**Element access.*/
40 {
41 return t[index];
42 }
43
44 //============================================= Addition
45 /**Component-wise addition.
46 * \f$ \vec{\vec{W}} = \vec{\vec{X}} + \vec{\vec{Y}} \f$*/
48 {
49 TensorRank2Dim3 new_t;
50 for (int i=0; i<3; ++i)
51 for (int j=0; j<3; ++j)
52 new_t[i](j) = this->t[i][j] + that.t[i][j];
53
54 return new_t;
55 }
56
57 /**In-place component-wise addition.
58 * \f$ \vec{\vec{X}} = \vec{\vec{X}} + \vec{\vec{Y}} \f$*/
60 {
61 for (int i=0; i<3; ++i)
62 for (int j=0; j<3; ++j)
63 this->t[i](j) += that.t[i][j];
64
65 return *this;
66 }
67
68 //============================================= Subtraction
69 /**Component-wise subtraction.
70 * \f$ \vec{\vec{W}} = \vec{\vec{X}} - \vec{\vec{Y}} \f$*/
72 {
73 TensorRank2Dim3 new_t;
74 for (int i=0; i<3; ++i)
75 for (int j=0; j<3; ++j)
76 new_t[i](j) = this->t[i][j] - that.t[i][j];
77
78 return new_t;
79 }
80
81 /**In-place component-wise subtraction.
82 * \f$ \vec{\vec{X}} = \vec{\vec{X}} - \vec{\vec{Y}} \f$*/
84 {
85 for (int i=0; i<3; ++i)
86 for (int j=0; j<3; ++j)
87 this->t[i](j) -= that.t[i][j];
88
89 return *this;
90 }
91
92 //============================================= Multiplication
93 /**Component-wise multiplication by scalar.
94 * \f$ \vec{\vec{W}} = \vec{\vec{X}}\alpha \f$ */
95 TensorRank2Dim3 operator*(const double value) const
96 {
97 TensorRank2Dim3 new_t;
98 for (int i=0; i<3; ++i)
99 for (int j=0; j<3; ++j)
100 new_t[i](j) = this->t[i][j]*value;
101
102 return new_t;
103 }
104
105 /**In-place component-wise multiplication by scalar.
106 * \f$ \vec{\vec{X}} = \vec{\vec{X}}\alpha \f$*/
107 TensorRank2Dim3& operator*=(const double value)
108 {
109 for (int i=0; i<3; ++i)
110 for (int j=0; j<3; ++j)
111 this->t[i](j) *= value;
112
113 return *this;
114 }
115
116 //============================================= Division
117 /**Component-wise division by scalar.
118 * \f$ \vec{\vec{W}} = \vec{\vec{X}}\frac{1}{\alpha} \f$*/
119 TensorRank2Dim3 operator/(const double value) const
120 {
121 TensorRank2Dim3 new_t;
122 for (int i=0; i<3; ++i)
123 for (int j=0; j<3; ++j)
124 new_t[i](j) = this->t[i][j]/value;
125
126 return new_t;
127 }
128
129 /**In-place component-wise division by scalar.
130 * \f$ \vec{\vec{X}} = \vec{\vec{X}}\frac{1}{\alpha} \f$*/
131 TensorRank2Dim3& operator/=(const double value)
132 {
133 for (int i=0; i<3; ++i)
134 for (int j=0; j<3; ++j)
135 this->t[i](j) /= value;
136
137 return *this;
138 }
139
140 //============================================= Transpose
141 /**Classical transpose of the tensor.
142 * \f$ W_{ij} = T_{ji} \f$*/
144 {
145 TensorRank2Dim3 new_t;
146 for (int i=0; i<3; ++i)
147 for (int j=0; j<3; ++j)
148 new_t[i](j) = this->t[j][i];
149
150 return new_t;
151 }
152
153 //============================================= Tensor dot product
154 //Defined in chi_mesh_utilities.cc
155 Vector3 Dot(const chi_mesh::Vector3& v) const;
156
157 //============================================= Get Diagonal
158 //Defined in chi_mesh_utilities.cc
159 chi_mesh::Vector3 Diag() const;
160
161 /**Returns the sum of the diagonal. Sometimes useful to get
162 * divergence of a vector given its gradient.*/
163 double DiagSum() const
164 {
165 double val = 0.0;
166 for (int i=0; i<3; ++i)
167 val += t[i][i];
168
169 return val;
170 }
171
172 //============================================= Printing
173 /**Prints the vector to a string and then returns the string.*/
174 std::string PrintS()
175 {
176 std::stringstream out;
177 out << "[";
178 for (int i=0;i<3;i++)
179 {
180 for (int j=0;j<3;j++)
181 {
182 out << t[i][j] << " ";
183 }
184 if (i!=2)
185 out << "\n ";
186 else
187 out << "]";
188 }
189
190 return out.str();
191 }
192};
193
194//The following functions are defined in chi_mesh_utilities.cc
195//Left multiplcation by scalar
197 operator*(const double value, const chi_mesh::TensorRank2Dim3& that);
198
199#endif
chi_mesh::TensorRank2Dim3 operator*(const double value, const chi_mesh::TensorRank2Dim3 &that)
TensorRank2Dim3 operator-(const TensorRank2Dim3 &that) const
TensorRank2Dim3 operator+(const TensorRank2Dim3 &that) const
TensorRank2Dim3 & operator=(const TensorRank2Dim3 &that)
TensorRank2Dim3 & operator/=(const double value)
TensorRank2Dim3(const TensorRank2Dim3 &that)
TensorRank2Dim3 & operator-=(const TensorRank2Dim3 &that)
std::vector< chi_mesh::Vector3 > t
Tensor entries.
TensorRank2Dim3 operator*(const double value) const
TensorRank2Dim3 & operator*=(const double value)
Vector3 Dot(const chi_mesh::Vector3 &v) const
TensorRank2Dim3 & operator+=(const TensorRank2Dim3 &that)
chi_mesh::Vector3 Diag() const
TensorRank2Dim3 operator/(const double value) const
chi_mesh::Vector3 & operator[](int index)