Sierra Toolkit  Version of the Day
UnitTestGetBuckets.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
10 
11 #include <stk_mesh/base/Bucket.hpp>
12 #include <stk_mesh/base/Part.hpp>
13 #include <stk_mesh/base/Types.hpp>
14 #include <stk_mesh/base/GetBuckets.hpp>
15 #include <stk_mesh/base/GetEntities.hpp>
16 #include <stk_mesh/base/MetaData.hpp>
17 #include <stk_mesh/base/BulkData.hpp>
18 
19 #include <stk_mesh/fem/FEMMetaData.hpp>
20 
21 #include <stk_util/parallel/Parallel.hpp>
22 
23 #include <stk_mesh/fixtures/SelectorFixture.hpp>
24 
25 #include <algorithm>
26 
27 #include <boost/foreach.hpp>
28 
29 namespace {
30 
36 
37 const stk_classic::mesh::EntityRank NODE_RANK = stk_classic::mesh::fem::FEMMetaData::NODE_RANK;
38 
39 // Just make copies so we don't have to worry about constness
40 template <class T>
41 void sort_and_compare_eq(std::vector<T*> results,
42  std::vector<T*> expected_results)
43 {
44  std::sort(expected_results.begin(), expected_results.end());
45  std::sort(results.begin(), results.end());
46  STKUNIT_ASSERT_EQ(results.size(), expected_results.size());
47  for (unsigned i = 0; i < results.size(); ++i) {
48  STKUNIT_ASSERT(results[i] == expected_results[i]);
49  }
50 }
51 
52 #if 0
53  // the selected bucket itertor is broken
54  // \TODO fix
55 void check_selected_buckets(const Selector& selector,
56  const BulkData& mesh,
57  const std::vector<Bucket*>& node_buckets,
58  const std::vector<Entity*>& expected_selected_entities)
59 {
60  //
61  // Check buckets
62  //
63 
64  std::vector<Bucket*> get_buckets, get_buckets_range, get_buckets_alt_range, expected_buckets;
65  std::set<Bucket*> selected_bucket_set;
66 
67  // Compute vector of buckets that should be selected
68  BOOST_FOREACH( Entity* entity, expected_selected_entities ) {
69  selected_bucket_set.insert(&(entity->bucket()));
70  }
71  std::copy(selected_bucket_set.begin(), selected_bucket_set.end(), std::back_inserter(expected_buckets));
72 
73  // get buckets selected by selector
74  stk_classic::mesh::get_buckets(selector, node_buckets, get_buckets);
75 
76  // get buckets selected by selector using range API
77  // all buckets are node buckets, so this should work
78  stk_classic::mesh::AllSelectedBucketsRange buckets_range = stk_classic::mesh::get_buckets(selector, mesh);
79  for (stk_classic::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
80  buckets_itr != buckets_end;
81  ++buckets_itr) {
82  get_buckets_range.push_back(*buckets_itr);
83  }
84  //BOOST_FOREACH( Bucket* bucket, stk_classic::mesh::get_buckets(selector, mesh) ) {
85  // get_buckets_range.push_back(bucket);
86  //}
87 
88  // get buckets selected by selector using alteranate range API
89  // all buckets are node buckets, so this should work
90  stk_classic::mesh::AllBucketsRange all_buckets = stk_classic::mesh::get_buckets(mesh);
91  buckets_range = stk_classic::mesh::get_buckets(selector, all_buckets);
92  for (stk_classic::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
93  buckets_itr != buckets_end;
94  ++buckets_itr) {
95  get_buckets_alt_range.push_back(*buckets_itr);
96  }
97  //BOOST_FOREACH( Bucket* bucket, stk_classic::mesh::get_buckets(selector, all_buckets) ) {
98  // get_buckets_alt_range.push_back(bucket);
99  //}
100 
101  sort_and_compare_eq(get_buckets, expected_buckets);
102  sort_and_compare_eq(get_buckets_range, expected_buckets);
103  sort_and_compare_eq(get_buckets_alt_range, expected_buckets);
104 
105  //
106  // Check entities
107  //
108 
109  std::vector<Entity*> get_entities_range;
110 
111  stk_classic::mesh::SelectedBucketRangeEntityIteratorRange selected_entity_range = stk_classic::mesh::get_selected_entities( selector, all_buckets );
112  for (stk_classic::mesh::SelectedBucketRangeEntityIterator selected_entity_itr = boost::begin(selected_entity_range),
113  selected_entity_end = boost::end(selected_entity_range);
114  selected_entity_itr != selected_entity_end;
115  ++selected_entity_itr) {
116  get_entities_range.push_back(*selected_entity_itr);
117  }
118  // TODO: Figure out why BOOST_FOREACH does not work well with selected entity iterators
119  // BOOST_FOREACH(Entity* entity, stk_classic::mesh::get_selected_entities(selector, all_buckets) ) {
120  // get_entities_range.push_back(entity);
121  // }
122 
123  sort_and_compare_eq(get_entities_range, expected_selected_entities);
124 }
125 #endif
126 
127 STKUNIT_UNIT_TEST( UnitTestGetBuckets, ExampleFixture )
128 {
129  // Using the SelectorFixture, test for correct bucket membership and
130  // correct results from get_buckets.
131 
132  // Generate mesh
133 
134  stk_classic::mesh::fixtures::SelectorFixture fix ;
135  fix.m_meta_data.commit();
136 
137  fix.m_bulk_data.modification_begin();
138  fix.generate_mesh();
139  STKUNIT_ASSERT(fix.m_bulk_data.modification_end());
140 
141  // Check bucket membership correctness
142 
143  {
144  const Bucket & bucket = fix.m_entity1->bucket();
145  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partA ) );
146  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
147  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
148  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
149  }
150 
151  {
152  const Bucket & bucket = fix.m_entity2->bucket();
153  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partA ) );
154  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partB ) );
155  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
156  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
157  }
158 
159  {
160  const Bucket & bucket = fix.m_entity3->bucket();
161  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
162  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partB ) );
163  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partC ) );
164  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
165  }
166 
167  {
168  const Bucket & bucket = fix.m_entity4->bucket();
169  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
170  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
171  STKUNIT_ASSERT_TRUE( bucket.member( fix.m_partC ) );
172  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
173  }
174 
175  {
176  const Bucket & bucket = fix.m_entity5->bucket();
177  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
178  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
179  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
180  STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
181  }
182 
183  // Check get_buckets correctness
184 
185  const std::vector<Bucket*> & node_buckets = fix.m_bulk_data.buckets(NODE_RANK);
186 
187  {
188  std::vector<Bucket*> get_buckets_range;
189 
190  // Get all node buckets using range API
191 
192  stk_classic::mesh::AllBucketsRange all_node_buckets = stk_classic::mesh::get_buckets(NODE_RANK, fix.m_bulk_data);
193  for (stk_classic::mesh::AllBucketsIterator buckets_itr = boost::begin(all_node_buckets), buckets_end = boost::end(all_node_buckets);
194  buckets_itr != buckets_end;
195  ++buckets_itr) {
196  get_buckets_range.push_back(*buckets_itr);
197  }
198  // BOOST_FOREACH( Bucket* bucket, stk_classic::mesh::get_buckets(NODE_RANK, fix.m_bulk_data) ) {
199  // get_buckets_range.push_back(bucket);
200  // }
201 
202  sort_and_compare_eq(get_buckets_range, node_buckets);
203  }
204 
205 #if 0
206  // the selected bucket itertor is broken
207  // \TODO fix
208  {
209  std::vector<Entity*> expected_selected_entities;
210  expected_selected_entities.push_back(fix.m_entity1);
211  expected_selected_entities.push_back(fix.m_entity2);
212 
213  check_selected_buckets(Selector(fix.m_partA), fix.m_bulk_data, node_buckets, expected_selected_entities);
214  }
215 
216  {
217  std::vector<Entity*> expected_selected_entities;
218  expected_selected_entities.push_back(fix.m_entity2);
219  expected_selected_entities.push_back(fix.m_entity3);
220 
221  check_selected_buckets(Selector(fix.m_partB), fix.m_bulk_data, node_buckets, expected_selected_entities);
222  }
223 
224  {
225  std::vector<Entity*> expected_selected_entities;
226  expected_selected_entities.push_back(fix.m_entity3);
227  expected_selected_entities.push_back(fix.m_entity4);
228 
229  check_selected_buckets(Selector(fix.m_partC), fix.m_bulk_data, node_buckets, expected_selected_entities);
230  }
231 
232  // Check get_entities correctness
233 
234  {
235  std::vector<Entity*> all_nodes_expected, all_nodes, all_nodes_range;
236 
237  all_nodes_expected.push_back(fix.m_entity1);
238  all_nodes_expected.push_back(fix.m_entity2);
239  all_nodes_expected.push_back(fix.m_entity3);
240  all_nodes_expected.push_back(fix.m_entity4);
241  all_nodes_expected.push_back(fix.m_entity5);
242 
243  stk_classic::mesh::get_entities( fix.m_bulk_data, NODE_RANK, all_nodes );
244  sort_and_compare_eq(all_nodes, all_nodes_expected);
245 
246  stk_classic::mesh::BucketVectorEntityIteratorRange entity_range = stk_classic::mesh::get_entities(NODE_RANK, fix.m_bulk_data);
247  for (stk_classic::mesh::BucketVectorEntityIterator entity_itr = boost::begin(entity_range), entity_end = boost::end(entity_range);
248  entity_itr != entity_end;
249  ++entity_itr) {
250  all_nodes_range.push_back(*entity_itr);
251  }
252  // BOOST_FOREACH( Entity* const entity, stk_classic::mesh::get_entities( NODE_RANK, fix.m_bulk_data ) ) {
253  // all_nodes_range.push_back(entity);
254  // }
255  sort_and_compare_eq(all_nodes_range, all_nodes_expected);
256  }
257 #endif
258 }
259 
260 } // empty namespace
This is a class for selecting buckets based on a set of meshparts and set logic.
Definition: Selector.hpp:112
An application-defined subset of a problem domain.
Definition: Part.hpp:49
void get_selected_entities(const Selector &selector, const std::vector< Bucket * > &input_buckets, std::vector< Entity * > &entities)
Get entities in selected buckets (selected by the given selector instance), and sorted by ID...
Definition: GetEntities.cpp:77
void get_entities(const BulkData &mesh, EntityRank entity_rank, std::vector< Entity *> &entities)
Get all entities of the specified type, sorted by ID.
Definition: GetEntities.cpp:25
Manager for an integrated collection of entities, entity relations, and buckets of field data...
Definition: BulkData.hpp:49
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
AllSelectedBucketsRange get_buckets(const Selector &selector, const BulkData &mesh)
Definition: GetBuckets.cpp:26
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94