Intrepid
Intrepid_MiniTensor_Geometry.i.h
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #if !defined(Intrepid_MiniTensor_Geometry_i_h)
43 #define Intrepid_MiniTensor_Geometry_i_h
44 
45 
46 namespace Intrepid {
47 
48 //
49 // Helper functions for determining the type of element
50 //
51 namespace {
52 
53 inline
54 ELEMENT::Type
55 find_type_1D(Index const nodes)
56 {
57  switch (nodes) {
58  case 2: return ELEMENT::SEGMENTAL;
59  default: return ELEMENT::UNKNOWN;
60  }
61 }
62 
63 inline
64 ELEMENT::Type
65 find_type_2D(Index const nodes)
66 {
67  switch (nodes) {
68  case 3: return ELEMENT::TRIANGULAR;
69  case 4: return ELEMENT::QUADRILATERAL;
70  default: return ELEMENT::UNKNOWN;
71  }
72 }
73 
74 inline
75 ELEMENT::Type
76 find_type_3D(Index const nodes)
77 {
78  switch (nodes) {
79  case 4: return ELEMENT::TETRAHEDRAL;
80  case 8: return ELEMENT::HEXAHEDRAL;
81  default: return ELEMENT::UNKNOWN;
82  }
83 }
84 
85 } // anonymous namespace
86 
87 
88 //
89 //
90 //
91 inline
92 ELEMENT::Type
93 find_type(Index const dimension, Index const number_nodes)
94 {
95 
96  ELEMENT::Type
97  type = ELEMENT::UNKNOWN;
98 
99  switch (dimension) {
100 
101  case 1:
102  type = find_type_1D(number_nodes);
103  break;
104 
105  case 2:
106  type = find_type_2D(number_nodes);
107  break;
108 
109  case 3:
110  type = find_type_3D(number_nodes);
111  break;
112 
113  default:
114  break;
115 
116  }
117 
118  if (type == ELEMENT::UNKNOWN) {
119  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
120  std::cerr << std::endl;
121  std::cerr << "Unknown element type." << std::endl;
122  std::cerr << std::endl;
123  std::cerr << "Spatial dimension: ";
124  std::cerr << dimension << std::endl;
125  std::cerr << "Vertices per element: ";
126  std::cerr << number_nodes << std::endl;
127  exit(1);
128  }
129 
130  return type;
131 }
132 
133 //
134 // Constructor for SphericalParametrization
135 //
136 template<typename T>
137 inline
139  Tensor4<T> const & A) : tangent_(A)
140 {
141  minimum_ = std::numeric_limits<T>::max();
142  maximum_ = std::numeric_limits<T>::min();
143  return;
144 }
145 
146 //
147 // Evaluation for SphericalParemetrization
148 //
149 template<typename T>
150 inline
151 void
153 {
154  assert(parameters.get_dimension() == 2);
155 
156  T const &
157  phi = parameters(0);
158 
159  T const &
160  theta = parameters(1);
161 
162  Vector<T> const
163  normal(sin(phi) * sin(theta), cos(phi), sin(phi) * cos(theta));
164 
165  // Localization tensor
166  Tensor<T> const
167  Q = dot(normal, dot(tangent_, normal));
168 
169  T const
170  determinant = det(Q);
171 
172  if (determinant < minimum_) {
173  minimum_ = determinant;
174  arg_minimum_ = parameters;
175  }
176 
177  if (determinant > maximum_) {
178  maximum_ = determinant;
179  arg_maximum_ = parameters;
180  }
181 
182  return;
183 }
184 
185 //
186 // Constructor for StereographicParametrization
187 //
188 template<typename T>
189 inline
191  Tensor4<T> const & A) : tangent_(A)
192 {
193  minimum_ = std::numeric_limits<T>::max();
194  maximum_ = std::numeric_limits<T>::min();
195  return;
196 }
197 
198 //
199 // Evaluation for StereographicParemetrization
200 //
201 template<typename T>
202 inline
203 void
205 {
206  assert(parameters.get_dimension() == 2);
207 
208  T const &
209  x = parameters(0);
210 
211  T const &
212  y = parameters(1);
213 
214  T const
215  r2 = x * x + y * y;
216 
217  Vector<T>
218  normal(2.0 * x, 2.0 * y, r2 - 1.0);
219 
220  normal /= (r2 + 1.0);
221 
222  // Localization tensor
223  Tensor<T> const
224  Q = dot(normal, dot(tangent_, normal));
225 
226  T const
227  determinant = det(Q);
228 
229  if (determinant < minimum_) {
230  minimum_ = determinant;
231  arg_minimum_ = parameters;
232  }
233 
234  if (determinant > maximum_) {
235  maximum_ = determinant;
236  arg_maximum_ = parameters;
237  }
238 
239  return;
240 }
241 
242 //
243 // Constructor for ProjectiveParametrization
244 //
245 template<typename T>
246 inline
248  Tensor4<T> const & A) : tangent_(A)
249 {
250  minimum_ = std::numeric_limits<T>::max();
251  maximum_ = std::numeric_limits<T>::min();
252  return;
253 }
254 
255 //
256 // Evaluation for ProjectiveParemetrization
257 //
258 template<typename T>
259 inline
260 void
262 {
263  assert(parameters.get_dimension() == 4);
264 
265  T const &
266  x = parameters(0);
267 
268  T const &
269  y = parameters(1);
270 
271  T const &
272  z = parameters(2);
273 
274  T const &
275  lambda = parameters(3);
276 
277  const Vector<T>
278  normal(x, y, z);
279 
280  // Localization tensor
281  Tensor<T> const
282  Q = dot(normal, dot(tangent_, normal));
283 
284  T const
285  determinant = det(Q);
286 
287  T const
288  function = determinant + lambda * (x * x + y * y + z * z - 1.0);
289 
290  if (function < minimum_) {
291  minimum_ = function;
292  arg_minimum_ = parameters;
293  }
294 
295  if (function > maximum_) {
296  maximum_ = function;
297  arg_maximum_ = parameters;
298  }
299 
300  return;
301 }
302 
303 //
304 // Constructor for TangentParametrization
305 //
306 template<typename T>
307 inline
309  Tensor4<T> const & A) : tangent_(A)
310 {
311  minimum_ = std::numeric_limits<T>::max();
312  maximum_ = std::numeric_limits<T>::min();
313  return;
314 }
315 
316 //
317 // Evaluation for TangentParemetrization
318 //
319 template<typename T>
320 inline
321 void
323 {
324  assert(parameters.get_dimension() == 2);
325 
326  T const &
327  x = parameters(0);
328 
329  T const &
330  y = parameters(1);
331 
332  T const
333  r = std::sqrt(x * x + y * y);
334 
335  Vector<T>
336  normal(3, ZEROS);
337 
338  if (r > 0.0) {
339  normal(0) = x * sin(r) / r;
340  normal(1) = y * sin(r) / r;
341  normal(2) = cos(r);
342  }
343 
344  // Localization tensor
345  Tensor<T> const
346  Q = dot(normal, dot(tangent_, normal));
347 
348  T const
349  determinant = det(Q);
350 
351  if (determinant < minimum_) {
352  minimum_ = determinant;
353  arg_minimum_ = parameters;
354  }
355 
356  if (determinant > maximum_) {
357  maximum_ = determinant;
358  arg_maximum_ = parameters;
359  }
360 
361  return;
362 }
363 
364 //
365 // Constructor for CartesianParametrization
366 //
367 template<typename T>
368 inline
370  Tensor4<T> const & A) : tangent_(A)
371 {
372  minimum_ = std::numeric_limits<T>::max();
373  maximum_ = std::numeric_limits<T>::min();
374  return;
375 }
376 
377 //
378 // Evaluation for CartesianParemetrization
379 //
380 template<typename T>
381 inline
382 void
384 {
385  assert(parameters.get_dimension() == 3);
386 
387  T const &
388  x = parameters(0);
389 
390  T const &
391  y = parameters(1);
392 
393  T const
394  z = parameters(2);
395 
396  const Vector<T>
397  normal(x, y, z);
398 
399  // Localization tensor
400  Tensor<T> const
401  Q = dot(normal, dot(tangent_, normal));
402 
403  T const
404  determinant = det(Q);
405 
406  if (determinant < minimum_) {
407  minimum_ = determinant;
408  arg_minimum_ = parameters;
409  }
410 
411  if (determinant > maximum_) {
412  maximum_ = determinant;
413  arg_maximum_ = parameters;
414  }
415 
416  return;
417 }
418 
419 //
420 // Constructor for ParametricGrid
421 //
422 template<typename T>
423 inline
425  Vector<T> const & lower,
426  Vector<T> const & upper,
427  Vector<Index> const & points_per_dimension)
428 {
429  assert(lower.get_dimension() == upper.get_dimension());
430  assert(lower.get_dimension() == points_per_dimension.get_dimension());
431 
432  lower_ = lower;
433  upper_ = upper;
434  points_per_dimension_ = points_per_dimension;
435 
436  return;
437 }
438 
439 //
440 // Traverse the grid and apply the visitor to each point.
441 //
442 template<typename T>
443 template<typename Visitor>
444 inline
445 void
446 ParametricGrid<T>::traverse(Visitor & visitor) const
447 {
448  // Loop over the grid
449  Index const
450  number_parameters = lower_.get_dimension();
451 
452  LongCount
453  total_number_points = 1;
454 
455  for (Index dimension = 0; dimension < number_parameters; ++dimension) {
456  total_number_points *= points_per_dimension_(dimension);
457  }
458 
459  //std::cout << "Total number of points: " << total_number_points << std::endl;
460 
462  steps(number_parameters, ONES);
463 
464  for (Index dimension = 1; dimension < number_parameters; ++dimension) {
465  steps(dimension) =
466  steps(dimension - 1) * points_per_dimension_(dimension - 1);
467  }
468 
469  //std::cout << "Points per dimension : " << points_per_dimension_ << std::endl;
470  //std::cout << "Steps : " << steps << std::endl;
471 
473  indices(number_parameters, ZEROS);
474 
475  Vector<T>
476  position_in_grid(number_parameters, ZEROS);
477 
478  Vector<T>
479  span = upper_ - lower_;
480 
481  for (LongCount point = 1; point <= total_number_points; ++point) {
482 
483  //std::cout << "Indices : ";
484 
485  for (Index dimension = 0; dimension < number_parameters; ++dimension) {
486 
487  position_in_grid(dimension) = indices(dimension) * span(dimension) /
488  (points_per_dimension_(dimension) - 1) + lower_(dimension);
489 
490  visitor(position_in_grid);
491 
492  //std::cout << indices(dimension) << " ";
493 
494  // Check if index needs to be increased or rolled back
495  if (point % steps(dimension) == 0) {
496  ++indices(dimension);
497  }
498  if (indices(dimension) == points_per_dimension_(dimension)) {
499  indices(dimension) = 0;
500  }
501 
502  }
503 
504  //std::cout << std::endl;
505  //std::cout << "Position : " << position_in_grid << std::endl;
506 
507  }
508 
509  return;
510 }
511 
512 } // namespace Intrepid
513 
514 #endif // Intrepid_MiniTensor_Geometry_i_h
515 
StereographicParametrization(Tensor4< T > const &A=Tensor4< T >())
SphericalParametrization(Tensor4< T > const &A=Tensor4< T >())
CartesianParametrization(Tensor4< T > const &A=Tensor4< T >())
TangentParametrization(Tensor4< T > const &A=Tensor4< T >())
ProjectiveParametrization(Tensor4< T > const &A=Tensor4< T >())