tensor_tools.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_TENSOR_TOOLS_H
21 #define LIBMESH_TENSOR_TOOLS_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/compare_types.h"
26 
27 namespace libMesh
28 {
29 // Forward declarations
30 template <typename T> class TypeVector;
31 template <typename T> class VectorValue;
32 template <typename T> class TypeTensor;
33 template <typename T> class TensorValue;
34 template <unsigned int N, typename T> class TypeNTensor;
35 
36 namespace TensorTools
37 {
38 // Any tensor-rank-independent code will need to include
39 // tensor_tools.h, so we define a product/dot-product here, starting
40 // with the generic case to apply to scalars.
41 // Vector specializations will follow.
42 
43 template <typename T, typename T2>
44 inline
46  typename CompareTypes<T, T2>::supertype>::type
47 inner_product(const T & a, const T2& b)
48 { return a * b; }
49 
50 template <typename T, typename T2>
51 inline
54 { return a * b; }
55 
56 template <typename T, typename T2>
57 inline
60 { return a.contract(b); }
61 
62 template <unsigned int N, typename T, typename T2>
63 inline
66 { return a.contract(b); }
67 
68 template<typename T>
69 inline
70 T norm_sq(T a) { return a*a; }
71 
72 template<typename T>
73 inline
74 T norm_sq(std::complex<T> a) { return std::norm(a); }
75 
76 template <typename T>
77 inline
79 {return a.norm_sq();}
80 
81 template <typename T>
82 inline
84 {return a.norm_sq();}
85 
86 // Any tensor-rank-independent code will need to include
87 // tensor_tools.h, so we define rank-increasing and real-to-number type
88 // conversion functions here, starting with the generic case to apply
89 // to scalars.
90 // Tensor(and higher?) specializations will go in the tensor
91 // header(s).
92 template <typename T>
94 {
96 };
97 
98 template <typename T>
100 {
102 };
103 
104 
105 template <typename T>
107 {
109 };
110 
111 template <typename T>
113 {
115 };
116 
117 
118 template <typename T>
120 {
122 };
123 
124 template <unsigned int N, typename T>
126 {
128 };
129 
130 
131 // Also need rank-decreasing case
132 template <typename T>
134 {
135  // The default case is typically an error, but for simpler
136  // templated code we need it to be compatible with Number
137  // operations...
138  typedef T type;
139 };
140 
141 template <typename T>
143 {
144  typedef T type;
145 };
146 
147 template <typename T>
149 {
150  typedef T type;
151 };
152 
153 template <typename T>
155 {
157 };
158 
159 template <typename T>
161 {
163 };
164 
165 template <unsigned int N, typename T>
167 {
168  typedef TypeNTensor<N-1,T> type;
169 };
170 
171 // Handle the complex-valued case
172 template <typename T>
174 {
175 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
176  typedef std::complex<T> type;
177 #else
178  typedef T type;
179 #endif
180 };
181 
182 template <typename T>
183 struct MakeNumber<std::complex<T>>
184 {
185  // Compile-time error: we shouldn't need to make numbers out of
186  // numbers
187  //typedef std::complex<T> type;
188 };
189 
190 
191 template <typename T>
193 {
195 };
196 
197 template <typename T>
199 {
201 };
202 
203 template <typename T>
205 {
207 };
208 
209 template <typename T>
211 {
213 };
214 
215 template <unsigned int N, typename T>
217 {
218 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
220 #else
222 #endif
223 };
224 
225 // A utility for determining real-valued (e.g. shape function)
226 // types from corresponding complex-valued types
227 template <typename T>
228 struct MakeReal
229 {
230  typedef T type;
231 };
232 
233 template <typename T>
234 struct MakeReal<std::complex<T>>
235 {
236  typedef T type;
237 };
238 
239 template <typename T>
241 {
243 };
244 
245 template <typename T>
247 {
249 };
250 
251 template <typename T>
253 {
255 };
256 
257 template <typename T>
259 {
261 };
262 
263 template <unsigned int N, typename T>
264 struct MakeReal<TypeNTensor<N,T>>
265 {
267 };
268 
269 // Needed for ExactSolution to compile
271 
274 
278 
281 
284 
288 
289 }//namespace TensorTools
290 
291 }//namespace libMesh
292 
293 #endif // LIBMESH_TENSOR_TOOLS_H
Number div_from_grad(const VectorValue< Number > &)
Dummy. Divergence of a scalar not defined, but is needed for ExactSolution to compile.
Definition: tensor_tools.C:54
TypeTensor< typename MakeNumber< T >::type > type
Definition: tensor_tools.h:212
TypeVector< typename MakeNumber< T >::type > type
Definition: tensor_tools.h:194
TypeTensor< typename MakeReal< T >::type > type
Definition: tensor_tools.h:260
TypeVector< typename MakeReal< T >::type > type
Definition: tensor_tools.h:242
TypeTensor< typename MakeReal< T >::type > type
Definition: tensor_tools.h:254
VectorValue< typename MakeReal< T >::type > type
Definition: tensor_tools.h:248
TypeNTensor< N, typename MakeReal< T >::type > type
Definition: tensor_tools.h:266
CompareTypes< T, T2 >::supertype contract(const TypeTensor< T2 > &) const
Definition: type_tensor.h:1204
Real norm_sq() const
Definition: type_vector.h:943
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
TypeTensor< typename MakeNumber< T >::type > type
Definition: tensor_tools.h:206
VectorValue< typename MakeNumber< T >::type > type
Definition: tensor_tools.h:200
boostcopy::enable_if_c< ScalarTraits< T >::value &&ScalarTraits< T2 >::value, typename CompareTypes< T, T2 >::supertype >::type inner_product(const T &a, const T2 &b)
Definition: tensor_tools.h:47
Number curl_from_grad(const VectorValue< Number > &)
Definition: tensor_tools.C:28
CompareTypes< T, T2 >::supertype contract(const TypeNTensor< N, T2 > &) const