gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
STensor3.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef STENSOR3_H
7 #define STENSOR3_H
8 
9 #include "SVector3.h"
10 #include "Numeric.h"
11 #include "GmshMessage.h"
12 
13 template <class scalar> class fullVector;
14 template <class scalar> class fullMatrix;
15 
16 // concrete class for symmetric positive definite 3x3 matrix
17 class SMetric3 {
18 protected:
19  // lower diagonal storage
20  // 00 10 11 20 21 22
21  double _val[6];
22 
23 public:
24  // default constructor, identity
25  SMetric3(const double v = 1.0)
26  {
27  _val[0] = _val[2] = _val[5] = v;
28  _val[1] = _val[3] = _val[4] = 0.0;
29  }
30  SMetric3(const SMetric3 &m)
31  {
32  for(int i = 0; i < 6; i++) _val[i] = m._val[i];
33  }
34  SMetric3(const double l1, // l1 = h1^-2
35  const double l2, const double l3, const SVector3 &t1,
36  const SVector3 &t2, const SVector3 &t3);
37  inline int getIndex(int i, int j) const
38  {
39  static int _index[3][3] = {{0, 1, 3}, {1, 2, 4}, {3, 4, 5}};
40  return _index[i][j];
41  }
42  void getMat(fullMatrix<double> &mat) const;
43  void setMat(const fullMatrix<double> &mat);
44  inline double &operator()(int i, int j) { return _val[getIndex(i, j)]; }
45  inline double operator()(int i, int j) const { return _val[getIndex(i, j)]; }
46  SMetric3 invert() const;
47  double determinant() const;
48  SMetric3 operator+(const SMetric3 &other) const
49  {
50  SMetric3 res(*this);
51  for(int i = 0; i < 6; i++) res._val[i] += other._val[i];
52  return res;
53  }
54  SMetric3 &operator+=(const SMetric3 &other)
55  {
56  for(int i = 0; i < 6; i++) _val[i] += other._val[i];
57  return *this;
58  }
59  SMetric3 &operator*=(const double &other)
60  {
61  for(int i = 0; i < 6; i++) _val[i] *= other;
62  return *this;
63  }
64  SMetric3 &operator*=(const SMetric3 &other);
66  void eig(fullMatrix<double> &V, fullVector<double> &S, bool s = false) const;
67  void print(const char *) const;
68 };
69 
70 // scalar product with respect to the metric
71 inline double dot(const SVector3 &a, const SMetric3 &m, const SVector3 &b)
72 {
73  return b.x() * (m(0, 0) * a.x() + m(1, 0) * a.y() + m(2, 0) * a.z()) +
74  b.y() * (m(0, 1) * a.x() + m(1, 1) * a.y() + m(2, 1) * a.z()) +
75  b.z() * (m(0, 2) * a.x() + m(1, 2) * a.y() + m(2, 2) * a.z());
76 }
77 
78 // preserve orientation of m1
80 
81 // preserve orientation of the most anisotropic metric
83  const SMetric3 &m2);
85  const SMetric3 &m2);
86 // compute the largest inscribed ellipsoid...
87 SMetric3 intersection(const SMetric3 &m1, const SMetric3 &m2);
88 SMetric3 intersection_alauzet(const SMetric3 &m1, const SMetric3 &m2);
89 SMetric3 interpolation(const SMetric3 &m1, const SMetric3 &m2, const double t);
90 SMetric3 interpolation(const SMetric3 &m1, const SMetric3 &m2,
91  const SMetric3 &m3, const double u, const double v);
92 SMetric3 interpolation(const SMetric3 &m1, const SMetric3 &m2,
93  const SMetric3 &m3, const SMetric3 &m4, const double u,
94  const double v, const double w);
95 
96 // concrete class for general 3x3 matrix
97 class STensor3 {
98 protected:
99  // 00 01 02 10 11 12 20 21 22
100  double _val[9];
101 
102 public:
103  // default constructor, null tensor
104  STensor3(const double v = 0.0)
105  {
106  _val[0] = _val[4] = _val[8] = v;
107  _val[1] = _val[2] = _val[3] = 0.0;
108  _val[5] = _val[6] = _val[7] = 0.0;
109  }
110  STensor3(const STensor3 &other)
111  {
112  for(int i = 0; i < 9; i++) _val[i] = other._val[i];
113  }
114  inline int getIndex(int i, int j) const
115  {
116  static int _index[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
117  return _index[i][j];
118  }
119  inline void set_m11(double x) { _val[0] = x; }
120  inline void set_m21(double x) { _val[3] = x; }
121  inline void set_m31(double x) { _val[6] = x; }
122  inline void set_m12(double x) { _val[1] = x; }
123  inline void set_m22(double x) { _val[4] = x; }
124  inline void set_m32(double x) { _val[7] = x; }
125  inline void set_m13(double x) { _val[2] = x; }
126  inline void set_m23(double x) { _val[5] = x; }
127  inline void set_m33(double x) { _val[8] = x; }
128  inline double get_m11() { return _val[0]; }
129  inline double get_m21() { return _val[3]; }
130  inline double get_m31() { return _val[6]; }
131  inline double get_m12() { return _val[1]; }
132  inline double get_m22() { return _val[4]; }
133  inline double get_m32() { return _val[7]; }
134  inline double get_m13() { return _val[2]; }
135  inline double get_m23() { return _val[5]; }
136  inline double get_m33() { return _val[8]; }
137  inline const double *data() const { return _val; }
138  inline double *data() { return _val; }
139  void getMat(fullMatrix<double> &mat) const;
140  void setMat(const fullMatrix<double> &mat);
141  inline double &operator()(int i, int j) { return _val[getIndex(i, j)]; }
142  inline double operator()(int i, int j) const { return _val[getIndex(i, j)]; }
143  inline double operator[](int i) const { return _val[i]; }
144  inline double &operator[](int i) { return _val[i]; }
145  STensor3 invert() const
146  {
147  double det = (*this).determinant();
148  double udet = 0.;
149  STensor3 ithis;
150  if(det == 0.)
151  Msg::Error("det=0 in STensor inversion");
152  else
153  udet = 1. / det;
154  ithis(0, 0) =
155  ((*this)(1, 1) * (*this)(2, 2) - (*this)(1, 2) * (*this)(2, 1)) * udet;
156  ithis(1, 0) =
157  -((*this)(1, 0) * (*this)(2, 2) - (*this)(1, 2) * (*this)(2, 0)) * udet;
158  ithis(2, 0) =
159  ((*this)(1, 0) * (*this)(2, 1) - (*this)(1, 1) * (*this)(2, 0)) * udet;
160  ithis(0, 1) =
161  -((*this)(0, 1) * (*this)(2, 2) - (*this)(0, 2) * (*this)(2, 1)) * udet;
162  ithis(1, 1) =
163  ((*this)(0, 0) * (*this)(2, 2) - (*this)(0, 2) * (*this)(2, 0)) * udet;
164  ithis(2, 1) =
165  -((*this)(0, 0) * (*this)(2, 1) - (*this)(0, 1) * (*this)(2, 0)) * udet;
166  ithis(0, 2) =
167  ((*this)(0, 1) * (*this)(1, 2) - (*this)(0, 2) * (*this)(1, 1)) * udet;
168  ithis(1, 2) =
169  -((*this)(0, 0) * (*this)(1, 2) - (*this)(0, 2) * (*this)(1, 0)) * udet;
170  ithis(2, 2) =
171  ((*this)(0, 0) * (*this)(1, 1) - (*this)(0, 1) * (*this)(1, 0)) * udet;
172  return ithis;
173  }
175  {
176  STensor3 ithis;
177  for(int i = 0; i < 3; i++)
178  for(int j = 0; j < 3; j++) ithis(i, j) = (*this)(j, i);
179  return ithis;
180  }
181  STensor3 &operator=(const STensor3 &other)
182  {
183  for(int i = 0; i < 9; i++) _val[i] = other._val[i];
184  return *this;
185  }
186  STensor3 operator+(const STensor3 &other) const
187  {
188  STensor3 res(*this);
189  for(int i = 0; i < 9; i++) res._val[i] += other._val[i];
190  return res;
191  }
192  STensor3 &operator+=(const STensor3 &other)
193  {
194  for(int i = 0; i < 9; i++) _val[i] += other._val[i];
195  return *this;
196  }
197  STensor3 &operator*=(const double &other)
198  {
199  for(int i = 0; i < 9; i++) _val[i] *= other;
200  return *this;
201  }
202  STensor3 &operator*=(const STensor3 &other)
203  {
204  double a00 = 0., a01 = 0., a02 = 0., a10 = 0., a11 = 0., a12 = 0., a20 = 0.,
205  a21 = 0., a22 = 0.;
206  for(int i = 0; i < 3; i++) {
207  a00 += (*this)(0, i) * other(i, 0);
208  a01 += (*this)(0, i) * other(i, 1);
209  a02 += (*this)(0, i) * other(i, 2);
210  a10 += (*this)(1, i) * other(i, 0);
211  a11 += (*this)(1, i) * other(i, 1);
212  a12 += (*this)(1, i) * other(i, 2);
213  a20 += (*this)(2, i) * other(i, 0);
214  a21 += (*this)(2, i) * other(i, 1);
215  a22 += (*this)(2, i) * other(i, 2);
216  }
217  (*this)(0, 0) = a00;
218  (*this)(0, 1) = a01;
219  (*this)(0, 2) = a02;
220  (*this)(1, 0) = a10;
221  (*this)(1, 1) = a11;
222  (*this)(1, 2) = a12;
223  (*this)(2, 0) = a20;
224  (*this)(2, 1) = a21;
225  (*this)(2, 2) = a22;
226  return *this;
227  }
228  void operator-=(const STensor3 &other)
229  {
230  for(int i = 0; i < 9; i++) _val[i] -= other._val[i];
231  }
232  void daxpy(const STensor3 &other, const double alpha = 1.)
233  {
234  if(alpha == 1.)
235  for(int i = 0; i < 9; i++) _val[i] += other._val[i];
236  else
237  for(int i = 0; i < 9; i++) _val[i] += alpha * other._val[i];
238  }
239  double trace() const { return ((_val[0] + _val[4] + _val[8])); }
240  double dotprod() const
241  {
242  double prod = 0;
243  for(int i = 0; i < 9; i++) prod += _val[i] * _val[i];
244  return prod;
245  }
246 
247  double determinant() const
248  {
249  double det =
250  ((*this)(0, 0) *
251  ((*this)(1, 1) * (*this)(2, 2) - (*this)(1, 2) * (*this)(2, 1)) -
252  (*this)(0, 1) *
253  ((*this)(1, 0) * (*this)(2, 2) - (*this)(1, 2) * (*this)(2, 0)) +
254  (*this)(0, 2) *
255  ((*this)(1, 0) * (*this)(2, 1) - (*this)(1, 1) * (*this)(2, 0)));
256  return det;
257  };
258  double norm0() const
259  {
260  double val = 0;
261  for(int i = 0; i < 9; i++)
262  if(fabs(_val[i]) > val) val = fabs(_val[i]);
263  return val;
264  };
265  double norm2() const
266  {
267  double sqr = 0;
268  for(int i = 0; i < 3; i++) {
269  for(int j = 0; j < 3; j++) {
270  sqr += this->operator()(i, j) * this->operator()(i, j);
271  }
272  }
273  return sqrt(sqr);
274  }
275  STensor3 dev() const
276  {
277  double p = trace() / 3.;
278  STensor3 de(*this);
279  de(0, 0) -= p;
280  de(1, 1) -= p;
281  de(2, 2) -= p;
282  return de;
283  }
284  void eig(fullMatrix<double> &V, fullVector<double> &S, bool s = false) const;
285  void print(const char *) const;
286 };
287 
288 // tensor product
289 inline void tensprod(const SVector3 &a, const SVector3 &b, STensor3 &c)
290 {
291  for(int i = 0; i < 3; i++)
292  for(int j = 0; j < 3; j++) c(i, j) = a(i) * b(j);
293 }
294 
295 inline double dot(const STensor3 &a, const STensor3 &b)
296 {
297  double prod = 0;
298  for(int i = 0; i < 3; i++)
299  for(int j = 0; j < 3; j++) prod += a(i, j) * b(i, j);
300  return prod;
301 }
302 
303 inline SVector3 operator*(const STensor3 &t, const SVector3 &v)
304 {
305  SVector3 temp(0., 0., 0.);
306  for(int i = 0; i < 3; i++)
307  for(int j = 0; j < 3; j++) temp[i] += t(i, j) * v[j];
308  return temp;
309 }
310 
311 inline SVector3 operator*(const SVector3 &v, const STensor3 &t)
312 {
313  SVector3 temp(0., 0., 0.);
314  for(int i = 0; i < 3; i++)
315  for(int j = 0; j < 3; j++) temp[j] += v[i] * t(i, j);
316  return temp;
317 }
318 
319 inline STensor3 operator*(const STensor3 &t, double m)
320 {
321  STensor3 val(t);
322  val *= m;
323  return val;
324 }
325 
326 inline STensor3 operator*(double m, const STensor3 &t)
327 {
328  STensor3 val(t);
329  val *= m;
330  return val;
331 }
332 
333 #endif
operator*
SVector3 operator*(const STensor3 &t, const SVector3 &v)
Definition: STensor3.h:303
STensor3::getIndex
int getIndex(int i, int j) const
Definition: STensor3.h:114
STensor3::get_m12
double get_m12()
Definition: STensor3.h:131
sqr
static int sqr(int x)
Definition: gl2gif.cpp:266
STensor3::print
void print(const char *) const
Definition: STensor3.cpp:323
STensor3::get_m31
double get_m31()
Definition: STensor3.h:130
SMetric3::operator+=
SMetric3 & operator+=(const SMetric3 &other)
Definition: STensor3.h:54
dot
double dot(const SVector3 &a, const SMetric3 &m, const SVector3 &b)
Definition: STensor3.h:71
SMetric3
Definition: STensor3.h:17
STensor3::daxpy
void daxpy(const STensor3 &other, const double alpha=1.)
Definition: STensor3.h:232
SMetric3::SMetric3
SMetric3(const double v=1.0)
Definition: STensor3.h:25
STensor3::set_m12
void set_m12(double x)
Definition: STensor3.h:122
fullVector
Definition: MElement.h:26
SMetric3::operator()
double operator()(int i, int j) const
Definition: STensor3.h:45
STensor3::norm2
double norm2() const
Definition: STensor3.h:265
STensor3::set_m11
void set_m11(double x)
Definition: STensor3.h:119
STensor3::trace
double trace() const
Definition: STensor3.h:239
STensor3::get_m23
double get_m23()
Definition: STensor3.h:135
STensor3::_val
double _val[9]
Definition: STensor3.h:100
STensor3::set_m21
void set_m21(double x)
Definition: STensor3.h:120
c
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
Definition: discreteFrechetDistance.cpp:15
STensor3::get_m33
double get_m33()
Definition: STensor3.h:136
STensor3::operator()
double operator()(int i, int j) const
Definition: STensor3.h:142
STensor3::data
double * data()
Definition: STensor3.h:138
STensor3
Definition: STensor3.h:97
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
SMetric3::eig
void eig(fullMatrix< double > &V, fullVector< double > &S, bool s=false) const
Definition: STensor3.cpp:104
SMetric3::SMetric3
SMetric3(const SMetric3 &m)
Definition: STensor3.h:30
tensprod
void tensprod(const SVector3 &a, const SVector3 &b, STensor3 &c)
Definition: STensor3.h:289
interpolation
SMetric3 interpolation(const SMetric3 &m1, const SMetric3 &m2, const double t)
Definition: STensor3.cpp:260
SVector3
Definition: SVector3.h:16
SVector3.h
GmshMessage.h
STensor3::set_m32
void set_m32(double x)
Definition: STensor3.h:124
STensor3::operator+
STensor3 operator+(const STensor3 &other) const
Definition: STensor3.h:186
SMetric3::invert
SMetric3 invert() const
Definition: STensor3.cpp:64
SMetric3::determinant
double determinant() const
Definition: STensor3.cpp:74
fullMatrix
Definition: MElement.h:27
STensor3::get_m13
double get_m13()
Definition: STensor3.h:134
SMetric3::getMat
void getMat(fullMatrix< double > &mat) const
Definition: STensor3.cpp:51
SMetric3::print
void print(const char *) const
Definition: STensor3.cpp:112
STensor3::get_m21
double get_m21()
Definition: STensor3.h:129
SMetric3::getIndex
int getIndex(int i, int j) const
Definition: STensor3.h:37
SMetric3::_val
double _val[6]
Definition: STensor3.h:21
STensor3::get_m11
double get_m11()
Definition: STensor3.h:128
STensor3::set_m13
void set_m13(double x)
Definition: STensor3.h:125
intersection_conserveM1
SMetric3 intersection_conserveM1(const SMetric3 &m1, const SMetric3 &m2)
Definition: STensor3.cpp:164
SMetric3::operator()
double & operator()(int i, int j)
Definition: STensor3.h:44
Numeric.h
STensor3::set_m23
void set_m23(double x)
Definition: STensor3.h:126
SMetric3::operator*=
SMetric3 & operator*=(const double &other)
Definition: STensor3.h:59
STensor3::set_m33
void set_m33(double x)
Definition: STensor3.h:127
STensor3::operator*=
STensor3 & operator*=(const double &other)
Definition: STensor3.h:197
STensor3::get_m32
double get_m32()
Definition: STensor3.h:133
STensor3::get_m22
double get_m22()
Definition: STensor3.h:132
STensor3::data
const double * data() const
Definition: STensor3.h:137
SVector3::x
double x(void) const
Definition: SVector3.h:30
STensor3::norm0
double norm0() const
Definition: STensor3.h:258
S
#define S
Definition: DefaultOptions.h:21
STensor3::determinant
double determinant() const
Definition: STensor3.h:247
STensor3::set_m31
void set_m31(double x)
Definition: STensor3.h:121
STensor3::operator-=
void operator-=(const STensor3 &other)
Definition: STensor3.h:228
intersection_conserve_mostaniso
SMetric3 intersection_conserve_mostaniso(const SMetric3 &m1, const SMetric3 &m2)
Definition: STensor3.cpp:216
SVector3::y
double y(void) const
Definition: SVector3.h:31
STensor3::STensor3
STensor3(const STensor3 &other)
Definition: STensor3.h:110
STensor3::dotprod
double dotprod() const
Definition: STensor3.h:240
STensor3::operator+=
STensor3 & operator+=(const STensor3 &other)
Definition: STensor3.h:192
SVector3::z
double z(void) const
Definition: SVector3.h:32
STensor3::eig
void eig(fullMatrix< double > &V, fullVector< double > &S, bool s=false) const
Definition: STensor3.cpp:315
STensor3::invert
STensor3 invert() const
Definition: STensor3.h:145
STensor3::operator[]
double operator[](int i) const
Definition: STensor3.h:143
intersection
SMetric3 intersection(const SMetric3 &m1, const SMetric3 &m2)
Definition: STensor3.cpp:119
STensor3::operator=
STensor3 & operator=(const STensor3 &other)
Definition: STensor3.h:181
intersection_alauzet
SMetric3 intersection_alauzet(const SMetric3 &m1, const SMetric3 &m2)
Definition: STensor3.cpp:146
SMetric3::transform
SMetric3 transform(fullMatrix< double > &V)
Definition: STensor3.cpp:92
STensor3::set_m22
void set_m22(double x)
Definition: STensor3.h:123
STensor3::operator*=
STensor3 & operator*=(const STensor3 &other)
Definition: STensor3.h:202
STensor3::getMat
void getMat(fullMatrix< double > &mat) const
Definition: STensor3.cpp:302
STensor3::setMat
void setMat(const fullMatrix< double > &mat)
Definition: STensor3.cpp:309
STensor3::STensor3
STensor3(const double v=0.0)
Definition: STensor3.h:104
STensor3::operator()
double & operator()(int i, int j)
Definition: STensor3.h:141
STensor3::dev
STensor3 dev() const
Definition: STensor3.h:275
STensor3::operator[]
double & operator[](int i)
Definition: STensor3.h:144
SMetric3::setMat
void setMat(const fullMatrix< double > &mat)
Definition: STensor3.cpp:58
intersection_conserve_mostaniso_2d
SMetric3 intersection_conserve_mostaniso_2d(const SMetric3 &m1, const SMetric3 &m2)
Definition: STensor3.cpp:236
SMetric3::operator+
SMetric3 operator+(const SMetric3 &other) const
Definition: STensor3.h:48
STensor3::transpose
STensor3 transpose() const
Definition: STensor3.h:174