DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_EntityLocalMap.cpp
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #include <cmath>
42 #include <limits>
43 
44 #include "DTK_DBC.hpp"
45 #include "DTK_EntityLocalMap.hpp"
46 
47 namespace DataTransferKit
48 {
49 //---------------------------------------------------------------------------//
50 // Constructor.
52 
53 //---------------------------------------------------------------------------//
54 // Destructor.
56 
57 //---------------------------------------------------------------------------//
58 // Perform a safeguard check for mapping a point to the reference space
59 // of an entity using the given tolerance. Default implementation checks if
60 // the point is inside the bounding box of the entity.
62  const Entity &entity, const Teuchos::ArrayView<const double> &point ) const
63 {
64  // Get the bounding box of the entity.
65  Teuchos::Tuple<double, 6> entity_box;
66  entity.boundingBox( entity_box );
67 
68  // Check if the point is in the bounding box of the entity.
69  double tolerance = 1.0e-6;
70  int space_dim = entity.physicalDimension();
71  bool in_x = true;
72  if ( space_dim > 0 )
73  {
74  double x_tol = ( entity_box[3] - entity_box[0] ) * tolerance;
75  in_x = ( ( point[0] >= ( entity_box[0] - x_tol ) ) &&
76  ( point[0] <= ( entity_box[3] + x_tol ) ) );
77  }
78  bool in_y = true;
79  if ( space_dim > 1 )
80  {
81  double y_tol = ( entity_box[4] - entity_box[1] ) * tolerance;
82  in_y = ( ( point[1] >= ( entity_box[1] - y_tol ) ) &&
83  ( point[1] <= ( entity_box[4] + y_tol ) ) );
84  }
85  bool in_z = true;
86  if ( space_dim > 2 )
87  {
88  double z_tol = ( entity_box[5] - entity_box[2] ) * tolerance;
89  in_z = ( ( point[2] >= ( entity_box[2] - z_tol ) ) &&
90  ( point[2] <= ( entity_box[5] + z_tol ) ) );
91  }
92  return ( in_x && in_y && in_z );
93 }
94 
95 //---------------------------------------------------------------------------//
96 // Compute the normal on a face (3D) or edge (2D) at a given reference point.
98  const Entity &entity, const Entity &parent_entity,
99  const Teuchos::ArrayView<const double> &reference_point,
100  const Teuchos::ArrayView<double> &normal ) const
101 {
102  // Determine the reference dimension.
103  int physical_dim = entity.physicalDimension();
104  int ref_dim = physical_dim - 1;
105 
106  // Create a perturbation.
107  double perturbation = std::sqrt( std::numeric_limits<double>::epsilon() );
108 
109  // 3D/face case.
110  if ( 2 == ref_dim )
111  {
112  DTK_CHECK( 3 == reference_point.size() );
113  DTK_CHECK( 3 == normal.size() );
114 
115  // Create extra points.
116  Teuchos::Array<double> ref_p1( reference_point );
117  Teuchos::Array<double> ref_p2( reference_point );
118 
119  // Apply a perturbation to the extra points.
120  double p1_sign = 1.0;
121  ref_p1[0] += perturbation;
122  if ( !this->checkPointInclusion( entity, ref_p1() ) )
123  {
124  ref_p1[0] -= 2 * perturbation;
125  p1_sign = -1.0;
126  }
127  double p2_sign = 1.0;
128  ref_p2[1] += perturbation;
129  if ( !this->checkPointInclusion( entity, ref_p2() ) )
130  {
131  ref_p2[1] -= 2 * perturbation;
132  p2_sign = -1.0;
133  }
134 
135  // Map the perturbed points to the physical frame.
136  Teuchos::Array<double> p0( physical_dim );
137  this->mapToPhysicalFrame( entity, reference_point(), p0() );
138  Teuchos::Array<double> p1( physical_dim );
139  this->mapToPhysicalFrame( entity, ref_p1(), p1() );
140  Teuchos::Array<double> p2( physical_dim );
141  this->mapToPhysicalFrame( entity, ref_p2(), p2() );
142 
143  // Compute the cross product of the tangents produced by the
144  // perturbation.
145  Teuchos::Array<double> tan1( physical_dim );
146  Teuchos::Array<double> tan2( physical_dim );
147  for ( int d = 0; d < physical_dim; ++d )
148  {
149  tan1[d] = p1_sign * ( p1[d] - p0[d] );
150  tan2[d] = p2_sign * ( p2[d] - p0[d] );
151  }
152  normal[0] = tan1[1] * tan2[2] - tan1[2] * tan2[1];
153  normal[1] = tan1[2] * tan2[0] - tan1[0] * tan2[2];
154  normal[2] = tan1[0] * tan2[1] - tan1[1] * tan2[0];
155  }
156 
157  // 2D/edge case.
158  else if ( 1 == ref_dim )
159  {
160  DTK_CHECK( 2 == reference_point.size() );
161  DTK_CHECK( 2 == normal.size() );
162 
163  // Create extra points.
164  Teuchos::Array<double> ref_p1( reference_point );
165 
166  // Apply a perturbation to the extra points.
167  double p1_sign = 1.0;
168  ref_p1[0] += perturbation;
169  if ( !this->checkPointInclusion( entity, ref_p1() ) )
170  {
171  ref_p1[0] -= 2 * perturbation;
172  p1_sign = -1.0;
173  }
174 
175  // Map the perturbed points to the physical frame.
176  Teuchos::Array<double> p0( physical_dim );
177  this->mapToPhysicalFrame( entity, reference_point(), p0() );
178  Teuchos::Array<double> p1( physical_dim );
179  this->mapToPhysicalFrame( entity, ref_p1(), p1() );
180 
181  // Compute the cross product of the tangents produced by the
182  // perturbation.
183  Teuchos::Array<double> tan( physical_dim );
184  for ( int d = 0; d < physical_dim; ++d )
185  {
186  tan[d] = p1_sign * ( p1[d] - p0[d] );
187  }
188  normal[0] = -tan[0];
189  normal[1] = tan[1];
190  }
191 
192  // Normalize the normal vector.
193  double norm = 0.0;
194  for ( int d = 0; d < physical_dim; ++d )
195  {
196  norm += normal[d] * normal[d];
197  }
198  norm = std::sqrt( norm );
199  for ( int d = 0; d < physical_dim; ++d )
200  {
201  normal[d] /= norm;
202  }
203 }
204 
205 //---------------------------------------------------------------------------//
206 
207 } // end namespace DataTransferKit
208 
209 //---------------------------------------------------------------------------//
210 // end DTK_EntityLocalMap.cpp
211 //---------------------------------------------------------------------------//
Geometric entity interface definition.
Definition: DTK_Entity.hpp:61
void boundingBox(Teuchos::Tuple< double, 6 > &bounds) const
Return the Cartesian bounding box around an entity.
Definition: DTK_Entity.cpp:114
virtual bool isSafeToMapToReferenceFrame(const Entity &entity, const Teuchos::ArrayView< const double > &physical_point) const
(Safeguard the reverse map) Perform a safeguard check for mapping a point to the reference space of a...
virtual bool checkPointInclusion(const Entity &entity, const Teuchos::ArrayView< const double > &reference_point) const =0
Determine if a reference point is in the parameterized space of an entity.
virtual void normalAtReferencePoint(const Entity &entity, const Entity &parent_entity, const Teuchos::ArrayView< const double > &reference_point, const Teuchos::ArrayView< double > &normal) const
Compute the normal on a face (3D) or edge (2D) at a given reference point. A default implementation i...
Assertions and Design-by-Contract for error handling.
virtual ~EntityLocalMap()
Destructor.
int physicalDimension() const
Return the physical dimension of the entity.
Definition: DTK_Entity.cpp:106
DTK_BasicEntitySet.cpp.
virtual void mapToPhysicalFrame(const Entity &entity, const Teuchos::ArrayView< const double > &reference_point, const Teuchos::ArrayView< double > &physical_point) const =0
(Forward Map) Map a reference point to the physical space of an entity.