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 #if 0
00053   // the selected bucket itertor is broken
00054   // \TODO fix
00055 void check_selected_buckets(const Selector& selector,
00056                             const BulkData& mesh,
00057                             const std::vector<Bucket*>& node_buckets,
00058                             const std::vector<Entity*>& expected_selected_entities)
00059 {
00060   //
00061   // Check buckets
00062   //
00063 
00064   std::vector<Bucket*> get_buckets, get_buckets_range, get_buckets_alt_range, expected_buckets;
00065   std::set<Bucket*> selected_bucket_set;
00066 
00067   // Compute vector of buckets that should be selected
00068   BOOST_FOREACH( Entity* entity, expected_selected_entities ) {
00069     selected_bucket_set.insert(&(entity->bucket()));
00070   }
00071   std::copy(selected_bucket_set.begin(), selected_bucket_set.end(), std::back_inserter(expected_buckets));
00072 
00073   // get buckets selected by selector
00074   stk::mesh::get_buckets(selector, node_buckets, get_buckets);
00075 
00076   // get buckets selected by selector using range API
00077   // all buckets are node buckets, so this should work
00078   stk::mesh::AllSelectedBucketsRange buckets_range = stk::mesh::get_buckets(selector, mesh);
00079   for (stk::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
00080        buckets_itr != buckets_end;
00081        ++buckets_itr) {
00082     get_buckets_range.push_back(*buckets_itr);
00083   }
00084   //BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(selector, mesh) ) {
00085   //  get_buckets_range.push_back(bucket);
00086   //}
00087 
00088   // get buckets selected by selector using alteranate range API
00089   // all buckets are node buckets, so this should work
00090   stk::mesh::AllBucketsRange all_buckets = stk::mesh::get_buckets(mesh);
00091   buckets_range = stk::mesh::get_buckets(selector, all_buckets);
00092   for (stk::mesh::AllSelectedBucketsIterator buckets_itr = boost::begin(buckets_range), buckets_end = boost::end(buckets_range);
00093        buckets_itr != buckets_end;
00094        ++buckets_itr) {
00095     get_buckets_alt_range.push_back(*buckets_itr);
00096   }
00097   //BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(selector, all_buckets) ) {
00098   //  get_buckets_alt_range.push_back(bucket);
00099   //}
00100 
00101   sort_and_compare_eq(get_buckets,           expected_buckets);
00102   sort_and_compare_eq(get_buckets_range,     expected_buckets);
00103   sort_and_compare_eq(get_buckets_alt_range, expected_buckets);
00104 
00105   //
00106   // Check entities
00107   //
00108 
00109   std::vector<Entity*> get_entities_range;
00110 
00111   stk::mesh::SelectedBucketRangeEntityIteratorRange selected_entity_range = stk::mesh::get_selected_entities( selector, all_buckets );
00112   for (stk::mesh::SelectedBucketRangeEntityIterator selected_entity_itr = boost::begin(selected_entity_range),
00113                                                     selected_entity_end = boost::end(selected_entity_range);
00114        selected_entity_itr != selected_entity_end;
00115        ++selected_entity_itr) {
00116     get_entities_range.push_back(*selected_entity_itr);
00117   }
00118   // TODO: Figure out why BOOST_FOREACH does not work well with selected entity iterators
00119   // BOOST_FOREACH(Entity* entity, stk::mesh::get_selected_entities(selector, all_buckets) ) {
00120   //   get_entities_range.push_back(entity);
00121   // }
00122 
00123   sort_and_compare_eq(get_entities_range, expected_selected_entities);
00124 }
00125 #endif
00126 
00127 STKUNIT_UNIT_TEST( UnitTestGetBuckets, ExampleFixture )
00128 {
00129   // Using the SelectorFixture, test for correct bucket membership and
00130   // correct results from get_buckets.
00131 
00132   // Generate mesh
00133 
00134   stk::mesh::fixtures::SelectorFixture fix ;
00135   fix.m_meta_data.commit();
00136 
00137   fix.m_bulk_data.modification_begin();
00138   fix.generate_mesh();
00139   STKUNIT_ASSERT(fix.m_bulk_data.modification_end());
00140 
00141   // Check bucket membership correctness
00142 
00143   {
00144     const Bucket & bucket = fix.m_entity1->bucket();
00145     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partA ) );
00146     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00147     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00148     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00149   }
00150 
00151   {
00152     const Bucket & bucket = fix.m_entity2->bucket();
00153     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partA ) );
00154     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partB ) );
00155     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00156     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00157   }
00158 
00159   {
00160     const Bucket & bucket = fix.m_entity3->bucket();
00161     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00162     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partB ) );
00163     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partC ) );
00164     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00165   }
00166 
00167   {
00168     const Bucket & bucket = fix.m_entity4->bucket();
00169     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00170     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00171     STKUNIT_ASSERT_TRUE(  bucket.member( fix.m_partC ) );
00172     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00173   }
00174 
00175   {
00176     const Bucket & bucket = fix.m_entity5->bucket();
00177     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partA ) );
00178     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partB ) );
00179     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partC ) );
00180     STKUNIT_ASSERT_FALSE( bucket.member( fix.m_partD ) );
00181   }
00182 
00183   // Check get_buckets correctness
00184 
00185   const std::vector<Bucket*> & node_buckets = fix.m_bulk_data.buckets(NODE_RANK);
00186 
00187   {
00188     std::vector<Bucket*> get_buckets_range;
00189 
00190     // Get all node buckets using range API
00191 
00192     stk::mesh::AllBucketsRange all_node_buckets = stk::mesh::get_buckets(NODE_RANK, fix.m_bulk_data);
00193     for (stk::mesh::AllBucketsIterator buckets_itr = boost::begin(all_node_buckets), buckets_end = boost::end(all_node_buckets);
00194          buckets_itr != buckets_end;
00195          ++buckets_itr) {
00196       get_buckets_range.push_back(*buckets_itr);
00197     }
00198     // BOOST_FOREACH( Bucket* bucket, stk::mesh::get_buckets(NODE_RANK, fix.m_bulk_data) ) {
00199     //   get_buckets_range.push_back(bucket);
00200     // }
00201 
00202     sort_and_compare_eq(get_buckets_range, node_buckets);
00203   }
00204 
00205 #if 0
00206   // the selected bucket itertor is broken
00207   // \TODO fix
00208   {
00209     std::vector<Entity*> expected_selected_entities;
00210     expected_selected_entities.push_back(fix.m_entity1);
00211     expected_selected_entities.push_back(fix.m_entity2);
00212 
00213     check_selected_buckets(Selector(fix.m_partA), fix.m_bulk_data, node_buckets, expected_selected_entities);
00214   }
00215 
00216   {
00217     std::vector<Entity*> expected_selected_entities;
00218     expected_selected_entities.push_back(fix.m_entity2);
00219     expected_selected_entities.push_back(fix.m_entity3);
00220 
00221     check_selected_buckets(Selector(fix.m_partB), fix.m_bulk_data, node_buckets, expected_selected_entities);
00222   }
00223 
00224   {
00225     std::vector<Entity*> expected_selected_entities;
00226     expected_selected_entities.push_back(fix.m_entity3);
00227     expected_selected_entities.push_back(fix.m_entity4);
00228 
00229     check_selected_buckets(Selector(fix.m_partC), fix.m_bulk_data, node_buckets, expected_selected_entities);
00230   }
00231 
00232   // Check get_entities correctness
00233 
00234   {
00235     std::vector<Entity*> all_nodes_expected, all_nodes, all_nodes_range;
00236 
00237     all_nodes_expected.push_back(fix.m_entity1);
00238     all_nodes_expected.push_back(fix.m_entity2);
00239     all_nodes_expected.push_back(fix.m_entity3);
00240     all_nodes_expected.push_back(fix.m_entity4);
00241     all_nodes_expected.push_back(fix.m_entity5);
00242 
00243     stk::mesh::get_entities( fix.m_bulk_data, NODE_RANK, all_nodes );
00244     sort_and_compare_eq(all_nodes, all_nodes_expected);
00245 
00246     stk::mesh::BucketVectorEntityIteratorRange entity_range = stk::mesh::get_entities(NODE_RANK, fix.m_bulk_data);
00247     for (stk::mesh::BucketVectorEntityIterator entity_itr = boost::begin(entity_range), entity_end = boost::end(entity_range);
00248          entity_itr != entity_end;
00249          ++entity_itr) {
00250       all_nodes_range.push_back(*entity_itr);
00251     }
00252     // BOOST_FOREACH( Entity* const entity, stk::mesh::get_entities( NODE_RANK, fix.m_bulk_data ) ) {
00253     //   all_nodes_range.push_back(entity);
00254     // }
00255     sort_and_compare_eq(all_nodes_range, all_nodes_expected);
00256   }
00257 #endif
00258 }
00259 
00260 } // empty namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines