Sierra Toolkit Version of the Day
UnitTestGetBuckets.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
00010 
00011 #include <stk_mesh/base/Bucket.hpp>
00012 #include <stk_mesh/base/Part.hpp>
00013 #include <stk_mesh/base/Types.hpp>
00014 #include <stk_mesh/base/GetBuckets.hpp>
00015 #include <stk_mesh/base/GetEntities.hpp>
00016 #include <stk_mesh/base/MetaData.hpp>
00017 #include <stk_mesh/base/BulkData.hpp>
00018 
00019 #include <stk_mesh/fem/FEMMetaData.hpp>
00020 
00021 #include <stk_util/parallel/Parallel.hpp>
00022 
00023 #include <stk_mesh/fixtures/SelectorFixture.hpp>
00024 
00025 #include <algorithm>
00026 
00027 #include <boost/foreach.hpp>
00028 
00029 namespace {
00030 
00031 using stk::mesh::Bucket;
00032 using stk::mesh::Part;
00033 using stk::mesh::Selector;
00034 using stk::mesh::BulkData;
00035 using stk::mesh::Entity;
00036 
00037 const stk::mesh::EntityRank NODE_RANK = stk::mesh::fem::FEMMetaData::NODE_RANK;
00038 
00039 // Just make copies so we don't have to worry about constness
00040 template <class T>
00041 void sort_and_compare_eq(std::vector<T*> results,
00042                          std::vector<T*> expected_results)
00043 {
00044   std::sort(expected_results.begin(), expected_results.end());
00045   std::sort(results.begin(), results.end());
00046   STKUNIT_ASSERT_EQ(results.size(), expected_results.size());
00047   for (unsigned i = 0; i < results.size(); ++i) {
00048     STKUNIT_ASSERT(results[i] == expected_results[i]);
00049   }
00050 }
00051 
00052 void check_selected_buckets(const Selector& selector,
00053                             const BulkData& mesh,
00054                             const std::vector<Bucket*>& node_buckets,
00055                             const std::vector<Entity*>& expected_selected_entities)
00056 {
00057   //
00058   // Check buckets
00059   //
00060 
00061   std::vector<Bucket*> get_buckets, get_buckets_range, get_buckets_alt_range, expected_buckets;
00062   std::set<Bucket*> selected_bucket_set;
00063 
00064   // Compute vector of buckets that should be selected
00065   BOOST_FOREACH( Entity* entity, expected_selected_entities ) {
00066     selected_bucket_set.insert(&(entity->bucket()));
00067   }
00068   std::copy(selected_bucket_set.begin(), selected_bucket_set.end(), std::back_inserter(expected_buckets));
00069 
00070   // get buckets selected by selector
00071   stk::mesh::get_buckets(selector, node_buckets, get_buckets);
00072 
00073   // get buckets selected by selector using range API
00074   // all buckets are node buckets, so this should work
00075   stk::mesh::AllSelectedBucketsRange buckets_range = stk::mesh::get_buckets(selector, mesh);
00076   for (stk::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
00077        buckets_itr != buckets_end;
00078        ++buckets_itr) {
00079     get_buckets_range.push_back(*buckets_itr);
00080   }
00081   //BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(selector, mesh) ) {
00082   //  get_buckets_range.push_back(bucket);
00083   //}
00084 
00085   // get buckets selected by selector using alteranate range API
00086   // all buckets are node buckets, so this should work
00087   stk::mesh::AllBucketsRange all_buckets = stk::mesh::get_buckets(mesh);
00088   buckets_range = stk::mesh::get_buckets(selector, all_buckets);
00089   for (stk::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
00090        buckets_itr != buckets_end;
00091        ++buckets_itr) {
00092     get_buckets_alt_range.push_back(*buckets_itr);
00093   }
00094   //BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(selector, all_buckets) ) {
00095   //  get_buckets_alt_range.push_back(bucket);
00096   //}
00097 
00098   sort_and_compare_eq(get_buckets,           expected_buckets);
00099   sort_and_compare_eq(get_buckets_range,     expected_buckets);
00100   sort_and_compare_eq(get_buckets_alt_range, expected_buckets);
00101 
00102   //
00103   // Check entities
00104   //
00105 
00106   std::vector<Entity*> get_entities_range;
00107 
00108   stk::mesh::SelectedBucketRangeEntityIteratorRange selected_entity_range = stk::mesh::get_selected_entities( selector, all_buckets );
00109   for (stk::mesh::SelectedBucketRangeEntityIterator selected_entity_itr = boost::begin(selected_entity_range),
00110                                                     selected_entity_end = boost::end(selected_entity_range);
00111        selected_entity_itr != selected_entity_end;
00112        ++selected_entity_itr) {
00113     get_entities_range.push_back(*selected_entity_itr);
00114   }
00115   // TODO: Figure out why BOOST_FOREACH does not work well with selected entity iterators
00116   // BOOST_FOREACH(Entity* entity, stk::mesh::get_selected_entities(selector, all_buckets) ) {
00117   //   get_entities_range.push_back(entity);
00118   // }
00119 
00120   sort_and_compare_eq(get_entities_range, expected_selected_entities);
00121 }
00122 
00123 STKUNIT_UNIT_TEST( UnitTestGetBuckets, ExampleFixture )
00124 {
00125   // Using the SelectorFixture, test for correct bucket membership and
00126   // correct results from get_buckets.
00127 
00128   // Generate mesh
00129 
00130   stk::mesh::fixtures::SelectorFixture fix ;
00131   fix.m_meta_data.commit();
00132 
00133   fix.m_bulk_data.modification_begin();
00134   fix.generate_mesh();
00135   STKUNIT_ASSERT(fix.m_bulk_data.modification_end());
00136 
00137   // Check bucket membership correctness
00138 
00139   {
00140     const Bucket & bucket = fix.m_entity1->bucket();
00141     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partA ) );
00142     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00143     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00144     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00145   }
00146 
00147   {
00148     const Bucket & bucket = fix.m_entity2->bucket();
00149     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partA ) );
00150     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partB ) );
00151     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00152     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00153   }
00154 
00155   {
00156     const Bucket & bucket = fix.m_entity3->bucket();
00157     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00158     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partB ) );
00159     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partC ) );
00160     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00161   }
00162 
00163   {
00164     const Bucket & bucket = fix.m_entity4->bucket();
00165     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00166     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00167     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partC ) );
00168     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00169   }
00170 
00171   {
00172     const Bucket & bucket = fix.m_entity5->bucket();
00173     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00174     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00175     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00176     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00177   }
00178 
00179   // Check get_buckets correctness
00180 
00181   const std::vector<Bucket*> & node_buckets = fix.m_bulk_data.buckets(NODE_RANK);
00182 
00183   {
00184     std::vector<Bucket*> get_buckets_range;
00185 
00186     // Get all node buckets using range API
00187 
00188     stk::mesh::AllBucketsRange all_node_buckets = stk::mesh::get_buckets(NODE_RANK, fix.m_bulk_data);
00189     for (stk::mesh::AllBucketsIterator buckets_itr = boost::begin(all_node_buckets), buckets_end = boost::end(all_node_buckets);
00190          buckets_itr != buckets_end;
00191          ++buckets_itr) {
00192       get_buckets_range.push_back(*buckets_itr);
00193     }
00194     // BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(NODE_RANK, fix.m_bulk_data) ) {
00195     //   get_buckets_range.push_back(bucket);
00196     // }
00197 
00198     sort_and_compare_eq(get_buckets_range, node_buckets);
00199   }
00200 
00201   {
00202     std::vector<Entity*> expected_selected_entities;
00203     expected_selected_entities.push_back(fix.m_entity1);
00204     expected_selected_entities.push_back(fix.m_entity2);
00205 
00206     check_selected_buckets(Selector(fix.m_partA), fix.m_bulk_data, node_buckets, expected_selected_entities);
00207   }
00208 
00209   {
00210     std::vector<Entity*> expected_selected_entities;
00211     expected_selected_entities.push_back(fix.m_entity2);
00212     expected_selected_entities.push_back(fix.m_entity3);
00213 
00214     check_selected_buckets(Selector(fix.m_partB), fix.m_bulk_data, node_buckets, expected_selected_entities);
00215   }
00216 
00217   {
00218     std::vector<Entity*> expected_selected_entities;
00219     expected_selected_entities.push_back(fix.m_entity3);
00220     expected_selected_entities.push_back(fix.m_entity4);
00221 
00222     check_selected_buckets(Selector(fix.m_partC), fix.m_bulk_data, node_buckets, expected_selected_entities);
00223   }
00224 
00225   // Check get_entities correctness
00226 
00227   {
00228     std::vector<Entity*> all_nodes_expected, all_nodes, all_nodes_range;
00229 
00230     all_nodes_expected.push_back(fix.m_entity1);
00231     all_nodes_expected.push_back(fix.m_entity2);
00232     all_nodes_expected.push_back(fix.m_entity3);
00233     all_nodes_expected.push_back(fix.m_entity4);
00234     all_nodes_expected.push_back(fix.m_entity5);
00235 
00236     stk::mesh::get_entities( fix.m_bulk_data, NODE_RANK, all_nodes );
00237     sort_and_compare_eq(all_nodes, all_nodes_expected);
00238 
00239     stk::mesh::BucketVectorEntityIteratorRange entity_range = stk::mesh::get_entities(NODE_RANK, fix.m_bulk_data);
00240     for (stk::mesh::BucketVectorEntityIterator entity_itr = boost::begin(entity_range), entity_end = boost::end(entity_range);
00241          entity_itr != entity_end;
00242          ++entity_itr) {
00243       all_nodes_range.push_back(*entity_itr);
00244     }
00245     // BOOST_FOREACH( Entity* const entity, stk::mesh::get_entities( NODE_RANK, fix.m_bulk_data ) ) {
00246     //   all_nodes_range.push_back(entity);
00247     // }
00248     sort_and_compare_eq(all_nodes_range, all_nodes_expected);
00249   }
00250 }
00251 
00252 } // empty namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends