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