DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_PointImpl.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 <algorithm>
42 #include <limits>
43 
44 #include "DTK_DBC.hpp"
45 #include "DTK_PointImpl.hpp"
46 
47 namespace DataTransferKit
48 {
49 //---------------------------------------------------------------------------//
50 // Default constructor.
51 PointImpl::PointImpl() { /* ... */}
52 
53 //---------------------------------------------------------------------------//
54 // Array constructor.
55 PointImpl::PointImpl( const EntityId global_id, const int owner_rank,
56  const Teuchos::Array<double> &coordinates,
57  const Teuchos::Array<int> &block_ids,
58  const Teuchos::Array<int> &boundary_ids )
59  : d_global_id( global_id )
60  , d_owner_rank( owner_rank )
61  , d_block_ids( block_ids )
62  , d_boundary_ids( boundary_ids )
63  , d_coordinates( coordinates )
64 {
65  std::sort( d_block_ids.begin(), d_block_ids.end() );
66  std::sort( d_boundary_ids.begin(), d_boundary_ids.end() );
67 }
68 
69 //---------------------------------------------------------------------------//
70 // Get the coordinates of the point.
71 void PointImpl::getCoordinates(
72  const Teuchos::ArrayView<double> &coordinates ) const
73 {
74  coordinates.assign( d_coordinates );
75 }
76 
77 //---------------------------------------------------------------------------//
78 // Get the unique global identifier for the entity.
79 EntityId PointImpl::id() const { return d_global_id; }
80 
81 //---------------------------------------------------------------------------//
82 // Get the parallel rank that owns the entity.
83 int PointImpl::ownerRank() const { return d_owner_rank; }
84 
85 //---------------------------------------------------------------------------//
86 // Return the topological dimension of the entity.
87 int PointImpl::topologicalDimension() const { return 0; }
88 
89 //---------------------------------------------------------------------------//
90 // Return the physical dimension of the entity.
91 int PointImpl::physicalDimension() const { return d_coordinates.size(); }
92 
93 //---------------------------------------------------------------------------//
94 // Return the axis-aligned bounding box around the entity.
95 void PointImpl::boundingBox( Teuchos::Tuple<double, 6> &bounds ) const
96 {
97  Teuchos::Array<double> coordinates( this->physicalDimension() );
98  this->getCoordinates( coordinates );
99  double max = std::numeric_limits<double>::max();
100  int space_dim = coordinates.size();
101  if ( 1 == space_dim )
102  {
103  bounds = Teuchos::tuple( coordinates[0], -max, -max, coordinates[0],
104  max, max );
105  }
106  else if ( 2 == space_dim )
107  {
108  bounds = Teuchos::tuple( coordinates[0], coordinates[1], -max,
109  coordinates[0], coordinates[1], max );
110  }
111  else if ( 3 == space_dim )
112  {
113  bounds =
114  Teuchos::tuple( coordinates[0], coordinates[1], coordinates[2],
115  coordinates[0], coordinates[1], coordinates[2] );
116  }
117 }
118 
119 //---------------------------------------------------------------------------//
120 // Determine if an entity is in the block with the given id.
121 bool PointImpl::inBlock( const int block_id ) const
122 {
123  return std::binary_search( d_block_ids.begin(), d_block_ids.end(),
124  block_id );
125 }
126 
127 //---------------------------------------------------------------------------//
128 // Determine if an entity is on the boundary with the given id.
129 bool PointImpl::onBoundary( const int boundary_id ) const
130 {
131  return std::binary_search( d_boundary_ids.begin(), d_boundary_ids.end(),
132  boundary_id );
133 }
134 
135 //---------------------------------------------------------------------------//
136 void PointImpl::describe( Teuchos::FancyOStream &out,
137  const Teuchos::EVerbosityLevel /*verb_level*/ ) const
138 {
139  int space_dim = this->physicalDimension();
140  Teuchos::Array<double> coordinates( space_dim );
141  this->getCoordinates( coordinates() );
142 
143  out << "---" << std::endl;
144  out << description() << std::endl;
145  out << "Id: " << id() << std::endl;
146  out << "Owner rank: " << ownerRank() << std::endl;
147  out << "Block ids: " << d_block_ids << std::endl;
148  out << "Boundary ids: " << d_boundary_ids << std::endl;
149  out << "Coordinates:";
150  for ( int d = 0; d < space_dim; ++d )
151  {
152  out << " " << coordinates[d];
153  }
154  out << std::endl;
155  out << "---" << std::endl;
156 }
157 
158 //---------------------------------------------------------------------------//
164 double PointImpl::measure() const { return 0.0; }
165 
166 //---------------------------------------------------------------------------//
172 void PointImpl::centroid( const Teuchos::ArrayView<double> &centroid ) const
173 {
174  this->getCoordinates( centroid );
175 }
176 
177 //---------------------------------------------------------------------------//
181 bool PointImpl::mapToReferenceFrame(
182  const Teuchos::ArrayView<const double> &point,
183  const Teuchos::ArrayView<double> &reference_point ) const
184 {
185  reference_point.assign( point );
186  return false;
187 }
188 
189 //---------------------------------------------------------------------------//
195 bool PointImpl::checkPointInclusion(
196  const double tolerance,
197  const Teuchos::ArrayView<const double> &reference_point ) const
198 {
199  int space_dim = this->physicalDimension();
200  Teuchos::Array<double> coords( space_dim );
201  this->getCoordinates( coords() );
202  double distance = 0.0;
203  double local_dist = 0.0;
204  for ( int d = 0; d < space_dim; ++d )
205  {
206  local_dist = coords[d] - reference_point[d];
207  distance += local_dist * local_dist;
208  }
209  return ( distance < tolerance * tolerance );
210 }
211 
212 //---------------------------------------------------------------------------//
216 void PointImpl::mapToPhysicalFrame(
217  const Teuchos::ArrayView<const double> &reference_point,
218  const Teuchos::ArrayView<double> &point ) const
219 {
220  point.assign( reference_point );
221 }
222 
223 //---------------------------------------------------------------------------//
224 
225 } // end namespace DataTransferKit
226 
227 //---------------------------------------------------------------------------//
228 // end DTK_PointImpl.cpp
229 //---------------------------------------------------------------------------//
Assertions and Design-by-Contract for error handling.
unsigned long int EntityId
Entity id type.
Definition: DTK_Types.hpp:50
DTK_BasicEntitySet.cpp.