DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_CoarseLocalSearch.cpp
Go to the documentation of this file.
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 <cmath>
43 
45 #include "DTK_DBC.hpp"
47 
48 namespace DataTransferKit
49 {
50 //---------------------------------------------------------------------------//
55  const EntityIterator &entity_iterator,
56  const Teuchos::RCP<EntityLocalMap> &local_map,
57  const Teuchos::ParameterList &parameters )
58 {
59  // Setup the centroid array. These will be interleaved.
60  int space_dim = 0;
61  int num_entity = entity_iterator.size();
62  if ( num_entity > 0 )
63  {
64  space_dim = entity_iterator.begin()->physicalDimension();
65  }
66  d_entity_centroids.resize( space_dim * num_entity );
67 
68  // Add the centroids.
69  EntityIterator entity_it;
70  EntityIterator begin_it = entity_iterator.begin();
71  EntityIterator end_it = entity_iterator.end();
72  int entity_local_id = 0;
73  for ( entity_it = begin_it; entity_it != end_it; ++entity_it )
74  {
75  local_map->centroid(
76  *entity_it,
77  d_entity_centroids( space_dim * entity_local_id, space_dim ) );
78  d_entity_map.emplace( entity_local_id, *entity_it );
79  ++entity_local_id;
80  }
81 
82  // Build a static search tree.
83  int leaf_size = 20;
84  if ( parameters.isParameter( "Coarse Local Search Leaf Size" ) )
85  {
86  leaf_size = parameters.get<int>( "Coarse Local Search Leaf Size" );
87  }
88  leaf_size = std::min( leaf_size, num_entity );
90  space_dim, d_entity_centroids(), leaf_size );
91  DTK_ENSURE( Teuchos::nonnull( d_tree ) );
92 }
93 
94 //---------------------------------------------------------------------------//
98 void CoarseLocalSearch::search( const Teuchos::ArrayView<const double> &point,
99  const Teuchos::ParameterList &parameters,
100  Teuchos::Array<Entity> &neighbors ) const
101 {
102  // Find the leaf of nearest neighbors.
103  int num_neighbors = 100;
104  if ( parameters.isParameter( "Coarse Local Search kNN" ) )
105  {
106  num_neighbors = parameters.get<int>( "Coarse Local Search kNN" );
107  }
108  num_neighbors =
109  std::min( num_neighbors, Teuchos::as<int>( d_entity_map.size() ) );
110  Teuchos::Array<unsigned> local_neighbors =
111  d_tree->nnSearch( point, num_neighbors );
112 
113  // Extract the neighbors.
114  neighbors.resize( local_neighbors.size() );
115  Teuchos::Array<unsigned>::const_iterator local_it;
116  Teuchos::Array<Entity>::iterator entity_it;
117  for ( local_it = local_neighbors.begin(), entity_it = neighbors.begin();
118  local_it != local_neighbors.end(); ++local_it, ++entity_it )
119  {
120  DTK_CHECK( d_entity_map.count( *local_it ) );
121  *entity_it = d_entity_map.find( *local_it )->second;
122  }
123 }
124 
125 //---------------------------------------------------------------------------//
126 
127 } // end namespace DataTransferKit
128 
129 //---------------------------------------------------------------------------//
130 // end DTK_CoarseLocalSearch.cpp
131 //---------------------------------------------------------------------------//
Search tree factory.
Entity iterator interface.
void search(const Teuchos::ArrayView< const double > &point, const Teuchos::ParameterList &parameters, Teuchos::Array< Entity > &neighbors) const
Find the set of entities a point neighbors.
static Teuchos::RCP< StaticSearchTree > createStaticTree(const unsigned dim, const Teuchos::ArrayView< const double > &points, const unsigned leaf_size)
Static tree creation method.
Assertions and Design-by-Contract for error handling.
int physicalDimension() const
Return the physical dimension of the entity.
Definition: DTK_Entity.cpp:106
CoarseLocalSearch(const EntityIterator &entity_iterator, const Teuchos::RCP< EntityLocalMap > &local_map, const Teuchos::ParameterList &parameters)
Constructor.
CoarseLocalSearch declaration.
DTK_BasicEntitySet.cpp.