DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_STKMeshHelpers_impl.hpp
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 #ifndef DTK_STKMESHHELPERS_IMPL_HPP
42 #define DTK_STKMESHHELPERS_IMPL_HPP
43 
44 #include <limits>
45 #include <unordered_set>
46 
47 #include "DTK_DBC.hpp"
48 
49 #include <stk_mesh/base/Field.hpp>
50 #include <stk_mesh/base/FieldBase.hpp>
51 #include <stk_mesh/base/MetaData.hpp>
52 #include <stk_topology/topology.hpp>
53 
54 namespace DataTransferKit
55 {
56 //---------------------------------------------------------------------------//
57 // Extract the coordinates of multiple entities and return an array ordered as
58 // (C,N,D).
59 template <class FieldType>
60 Intrepid::FieldContainer<double> STKMeshHelpers::extractEntityNodeCoordinates(
61  const Teuchos::Array<stk::mesh::Entity> &stk_entities,
62  const stk::mesh::BulkData &bulk_data, const int space_dim )
63 {
64  // Cast the field.
65  const stk::mesh::FieldBase *coord_field_base =
66  bulk_data.mesh_meta_data().coordinate_field();
67  const stk::mesh::Field<double, FieldType> *coord_field =
68  dynamic_cast<const stk::mesh::Field<double, FieldType> *>(
69  coord_field_base );
70 
71  // Allocate the coordinate array.
72  int num_cells = stk_entities.size();
73  int num_nodes = 0;
74  stk::mesh::EntityRank stk_rank = stk::topology::INVALID_RANK;
75  if ( num_cells > 0 )
76  {
77  stk_rank = bulk_data.entity_rank( stk_entities[0] );
78  if ( stk::topology::NODE_RANK == stk_rank )
79  {
80  num_nodes = 1;
81  }
82  else
83  {
84  const stk::mesh::Entity *begin =
85  bulk_data.begin_nodes( stk_entities[0] );
86  const stk::mesh::Entity *end =
87  bulk_data.end_nodes( stk_entities[0] );
88  num_nodes = std::distance( begin, end );
89  }
90  }
91  Intrepid::FieldContainer<double> coords( num_cells, num_nodes, space_dim );
92 
93  // Extract the coordinates.
94  double *node_coords = 0;
95  for ( int c = 0; c < num_cells; ++c )
96  {
97  if ( stk::topology::NODE_RANK == stk_rank )
98  {
99  node_coords =
100  stk::mesh::field_data( *coord_field, stk_entities[c] );
101  for ( int d = 0; d < space_dim; ++d )
102  {
103  coords( c, 0, d ) = node_coords[d];
104  }
105  }
106  else
107  {
108  const stk::mesh::Entity *begin =
109  bulk_data.begin_nodes( stk_entities[c] );
110  DTK_REMEMBER( const stk::mesh::Entity *end =
111  bulk_data.end_nodes( stk_entities[c] ) );
112  DTK_CHECK( std::distance( begin, end ) == num_nodes );
113  for ( int n = 0; n < num_nodes; ++n )
114  {
115  node_coords = stk::mesh::field_data( *coord_field, begin[n] );
116  for ( int d = 0; d < space_dim; ++d )
117  {
118  coords( c, n, d ) = node_coords[d];
119  }
120  }
121  }
122  }
123 
124  return coords;
125 }
126 
127 //---------------------------------------------------------------------------//
128 
129 } // end namespace DataTransferKit
130 
131 #endif // end DTK_STKMESHHELPERS_IMPL_HPP
132 
133 //---------------------------------------------------------------------------//
134 // end DTK_STKMeshHelpers_impl.hpp
135 //---------------------------------------------------------------------------//
Assertions and Design-by-Contract for error handling.
DTK_BasicEntitySet.cpp.