VTK-m  2.2
LagrangianStructureHelpers.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtkm_filter_flow_internal_LagrangianStructureHelpers_h
11 #define vtkm_filter_flow_internal_LagrangianStructureHelpers_h
12 
13 #include <vtkm/Matrix.h>
14 #include <vtkm/Swap.h>
15 #include <vtkm/Types.h>
16 
17 namespace vtkm
18 {
19 namespace filter
20 {
21 namespace flow
22 {
23 namespace internal
24 {
25 
26 template <typename T>
27 VTKM_EXEC_CONT void ComputeLeftCauchyGreenTensor(vtkm::Matrix<T, 2, 2>& jacobian)
28 {
29  vtkm::Vec<T, 2> j1 = vtkm::MatrixGetRow(jacobian, 0);
30  vtkm::Vec<T, 2> j2 = vtkm::MatrixGetRow(jacobian, 1);
31 
32  // Left Cauchy Green Tensor is J*J^T
33  // j1[0] j1[1] | j1[0] j2[0]
34  // j2[0] j2[1] | j1[1] j2[1]
35 
36  T a = j1[0] * j1[0] + j1[1] * j1[1];
37  T b = j1[0] * j2[0] + j1[1] * j2[1];
38 
39  T d = j2[0] * j2[0] + j2[1] * j2[1];
40 
41  vtkm::MatrixSetRow(jacobian, 0, vtkm::Vec<T, 2>(a, b));
42  vtkm::MatrixSetRow(jacobian, 1, vtkm::Vec<T, 2>(b, d));
43 }
44 
45 template <typename T>
46 VTKM_EXEC_CONT void ComputeLeftCauchyGreenTensor(vtkm::Matrix<T, 3, 3>& jacobian)
47 {
48  vtkm::Vec<T, 3> j1 = vtkm::MatrixGetRow(jacobian, 0);
49  vtkm::Vec<T, 3> j2 = vtkm::MatrixGetRow(jacobian, 1);
50  vtkm::Vec<T, 3> j3 = vtkm::MatrixGetRow(jacobian, 2);
51 
52  // Left Cauchy Green Tensor is J*J^T
53  // j1[0] j1[1] j1[2] | j1[0] j2[0] j3[0]
54  // j2[0] j2[1] j2[2] | j1[1] j2[1] j3[1]
55  // j3[0] j3[1] j3[2] | j1[2] j2[2] j3[2]
56 
57  T a = j1[0] * j1[0] + j1[1] * j1[1] + j1[2] * j1[2];
58  T b = j1[0] * j2[0] + j1[1] * j2[1] + j1[2] * j2[2];
59  T c = j1[0] * j3[0] + j1[1] * j3[1] + j1[2] * j3[2];
60 
61  T d = j2[0] * j2[0] + j2[1] * j2[1] + j2[2] * j2[2];
62  T e = j2[0] * j3[0] + j2[1] * j3[1] + j2[2] * j3[2];
63 
64  T f = j3[0] * j3[0] + j3[1] * j3[1] + j3[2] * j3[2];
65 
66  vtkm::MatrixSetRow(jacobian, 0, vtkm::Vec<T, 3>(a, b, c));
67  vtkm::MatrixSetRow(jacobian, 1, vtkm::Vec<T, 3>(b, d, e));
68  vtkm::MatrixSetRow(jacobian, 2, vtkm::Vec<T, 3>(d, e, f));
69 }
70 
71 template <typename T>
72 VTKM_EXEC_CONT void ComputeRightCauchyGreenTensor(vtkm::Matrix<T, 2, 2>& jacobian)
73 {
74  vtkm::Vec<T, 2> j1 = vtkm::MatrixGetRow(jacobian, 0);
75  vtkm::Vec<T, 2> j2 = vtkm::MatrixGetRow(jacobian, 1);
76 
77  // Right Cauchy Green Tensor is J^T*J
78  // j1[0] j2[0] | j1[0] j1[1]
79  // j1[1] j2[1] | j2[0] j2[1]
80 
81  T a = j1[0] * j1[0] + j2[0] * j2[0];
82  T b = j1[0] * j1[1] + j2[0] * j2[1];
83 
84  T d = j1[1] * j1[1] + j2[1] * j2[1];
85 
86  j1 = vtkm::Vec<T, 2>(a, b);
87  j2 = vtkm::Vec<T, 2>(b, d);
88 }
89 
90 template <typename T>
91 VTKM_EXEC_CONT void ComputeRightCauchyGreenTensor(vtkm::Matrix<T, 3, 3>& jacobian)
92 {
93  vtkm::Vec<T, 3> j1 = vtkm::MatrixGetRow(jacobian, 0);
94  vtkm::Vec<T, 3> j2 = vtkm::MatrixGetRow(jacobian, 1);
95  vtkm::Vec<T, 3> j3 = vtkm::MatrixGetRow(jacobian, 2);
96 
97  // Right Cauchy Green Tensor is J^T*J
98  // j1[0] j2[0] j3[0] | j1[0] j1[1] j1[2]
99  // j1[1] j2[1] j3[1] | j2[0] j2[1] j2[2]
100  // j1[2] j2[2] j3[2] | j3[0] j3[1] j3[2]
101 
102  T a = j1[0] * j1[0] + j2[0] * j2[0] + j3[0] * j3[0];
103  T b = j1[0] * j1[1] + j2[0] * j2[1] + j3[0] * j3[1];
104  T c = j1[0] * j1[2] + j2[0] * j2[2] + j3[0] * j3[2];
105 
106  T d = j1[1] * j1[1] + j2[1] * j2[1] + j3[1] * j3[1];
107  T e = j1[1] * j1[2] + j2[1] * j2[2] + j3[1] * j3[2];
108 
109  T f = j1[2] * j1[2] + j2[2] * j2[2] + j3[2] * j3[2];
110 
111  j1 = vtkm::Vec<T, 3>(a, b, c);
112  j2 = vtkm::Vec<T, 3>(b, d, e);
113  j3 = vtkm::Vec<T, 3>(d, e, f);
114 }
115 
116 template <typename T>
117 VTKM_EXEC_CONT void Jacobi(vtkm::Matrix<T, 2, 2> tensor, vtkm::Vec<T, 2>& eigen)
118 {
119  vtkm::Vec<T, 2> j1 = vtkm::MatrixGetRow(tensor, 0);
120  vtkm::Vec<T, 2> j2 = vtkm::MatrixGetRow(tensor, 1);
121 
122  // Assume a symetric matrix
123  // a b
124  // b c
125  T a = j1[0];
126  T b = j1[1];
127  T c = j2[1];
128 
129  T trace = (a + c) / 2.0f;
130  T det = a * c - b * b;
131  T sqrtr = vtkm::Sqrt(trace * trace - det);
132 
133  // Arrange eigen values from largest to smallest.
134  eigen[0] = trace + sqrtr;
135  eigen[1] = trace - sqrtr;
136 }
137 
138 template <typename T>
139 VTKM_EXEC_CONT void Jacobi(vtkm::Matrix<T, 3, 3> tensor, vtkm::Vec<T, 3>& eigen)
140 {
141  vtkm::Vec<T, 3> j1 = vtkm::MatrixGetRow(tensor, 0);
142  vtkm::Vec<T, 3> j2 = vtkm::MatrixGetRow(tensor, 1);
143  vtkm::Vec<T, 3> j3 = vtkm::MatrixGetRow(tensor, 2);
144 
145  // Assume a symetric matrix
146  // a b c
147  // b d e
148  // c e f
149  T a = j1[0];
150  T b = j1[1];
151  T c = j1[2];
152  T d = j2[1];
153  T e = j2[2];
154  T f = j3[2];
155 
156  T x = (a + d + f) / 3.0f; // trace
157 
158  a -= x;
159  d -= x;
160  f -= x;
161 
162  // Det / 2;
163  T q = (a * d * f + b * e * c + c * b * e - c * d * c - e * e * a - f * b * b) / 2.0f;
164  T r = (a * a + b * b + c * c + b * b + d * d + e * e + c * c + e * e + f * f) / 6.0f;
165 
166  T D = (r * r * r - q * q);
167  T phi = 0.0f;
168 
169  if (D < vtkm::Epsilon<T>())
170  phi = 0.0f;
171  else
172  {
173  phi = vtkm::ATan(vtkm::Sqrt(D) / q) / 3.0f;
174 
175  if (phi < 0)
176  phi += static_cast<T>(vtkm::Pi());
177  }
178 
179  const T sqrt3 = vtkm::Sqrt(3.0f);
180  const T sqrtr = vtkm::Sqrt(r);
181 
182  T sinphi = 0.0f, cosphi = 0.0f;
183  sinphi = vtkm::Sin(phi);
184  cosphi = vtkm::Cos(phi);
185 
186  T w0 = x + 2.0f * sqrtr * cosphi;
187  T w1 = x - sqrtr * (cosphi - sqrt3 * sinphi);
188  T w2 = x - sqrtr * (cosphi + sqrt3 * sinphi);
189 
190  // Arrange eigen values from largest to smallest.
191  if (w1 > w0)
192  vtkm::Swap(w0, w1);
193  if (w2 > w0)
194  vtkm::Swap(w0, w2);
195  if (w2 > w1)
196  vtkm::Swap(w1, w2);
197 
198  eigen[0] = w0;
199  eigen[1] = w1;
200  eigen[2] = w2;
201 }
202 
203 }
204 }
205 }
206 } //vtkm::filter::flow::internal
207 
208 #endif //vtkm_filter_flow_internal_LagrangianStructureHelpers_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Sqrt
vtkm::Float32 Sqrt(vtkm::Float32 x)
Definition: Math.h:943
vtkm::MatrixGetRow
const vtkm::Vec< T, NumCol > & MatrixGetRow(const vtkm::Matrix< T, NumRow, NumCol > &matrix, vtkm::IdComponent rowIndex)
Returns a tuple containing the given row (indexed from 0) of the given matrix.
Definition: Matrix.h:107
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
Matrix.h
Swap.h
vtkm::MatrixSetRow
void MatrixSetRow(vtkm::Matrix< T, NumRow, NumCol > &matrix, vtkm::IdComponent rowIndex, const vtkm::Vec< T, NumCol > &rowValues)
Convenience function for setting a row of a matrix.
Definition: Matrix.h:132
vtkm::Vec< T, 2 >
Definition: Types.h:897
vtkm::Vec< T, 3 >
Definition: Types.h:1013
vtkm::Matrix
Basic Matrix type.
Definition: Matrix.h:33
vtkm::Sin
vtkm::Float32 Sin(vtkm::Float32 x)
Definition: Math.h:166
vtkm::ATan
vtkm::Float32 ATan(vtkm::Float32 x)
Definition: Math.h:471
vtkm::Cos
vtkm::Float32 Cos(vtkm::Float32 x)
Definition: Math.h:227
vtkm::Swap
void Swap(T &a, T &b)
Performs a swap operation. Safe to call from cuda code.
Definition: Swap.h:59