Sierra Toolkit Version of the Day
UnitTestTopology.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 
00010 #include <sstream>
00011 #include <stdexcept>
00012 
00013 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
00014 
00015 #include <stk_util/parallel/Parallel.hpp>
00016 
00017 #include <stk_mesh/base/MetaData.hpp>
00018 #include <stk_mesh/base/BulkData.hpp>
00019 #include <stk_mesh/base/GetEntities.hpp>
00020 #include <stk_mesh/base/Field.hpp>
00021 #include <stk_mesh/base/FieldData.hpp>
00022 #include <stk_mesh/base/Comm.hpp>
00023 #include <stk_mesh/base/EntityComm.hpp>
00024 #include <stk_mesh/base/Part.hpp>
00025 #include <stk_mesh/base/Entity.hpp>
00026 #include <stk_mesh/base/GetBuckets.hpp>
00027 #include <stk_mesh/base/Bucket.hpp>
00028 #include <stk_mesh/base/BulkModification.hpp>
00029 
00030 #include <stk_mesh/fem/EntityRanks.hpp>
00031 #include <stk_mesh/fem/TopologyHelpers.hpp>
00032 #include <stk_mesh/fem/TopologicalMetaData.hpp>
00033 #include <stk_mesh/fem/BoundaryAnalysis.hpp>
00034 
00035 #include <unit_tests/UnitTestBucket.hpp>
00036 #include <unit_tests/UnitTestMesh.hpp>
00037 
00038 #include <Shards_BasicTopologies.hpp>
00039 
00040 #include <stk_mesh/base/Entity.hpp>
00041 #include <stk_mesh/base/Bucket.hpp>
00042 #include <stk_mesh/base/Transaction.hpp>
00043 #include <stk_mesh/baseImpl/BucketImpl.hpp>
00044 #include <stk_mesh/base/Ghosting.hpp>
00045 
00046 using stk::unit_test::UnitTestBucket;
00047 
00048 using stk::mesh::MetaData;
00049 using stk::mesh::BulkData;
00050 using stk::mesh::Part;
00051 using stk::mesh::PartVector;
00052 using stk::mesh::EntityRank;
00053 using stk::mesh::EntityId;
00054 using stk::mesh::EntitySideComponent;
00055 using stk::mesh::PairIterRelation;
00056 using stk::mesh::Entity;
00057 using stk::mesh::TopologicalMetaData;
00058 
00059 using stk::ParallelMachine;
00060 using std::cout;
00061 using std::endl;
00062 
00063 class TopologyHelpersTestingFixture
00064 {
00065   public:
00066     TopologyHelpersTestingFixture(ParallelMachine pm);
00067     ~TopologyHelpersTestingFixture() {}
00068 
00069     const int spatial_dimension;
00070     MetaData meta;
00071     BulkData bulk;
00072     stk::mesh::TopologicalMetaData top;
00073     Part & generic_element_part;
00074     Part & element_tet_part;
00075     Part & element_wedge_part;
00076     Part & generic_face_part;
00077     Part & another_generic_face_part;
00078     Part & face_quad_part;
00079     Part & another_generic_element_part;
00080 
00081     EntityId nextEntityId() 
00082     { return psize*(++entity_id)+prank; }
00083 
00084     Entity & create_entity( EntityRank rank, Part& part_membership)
00085     { 
00086       PartVector part_intersection;
00087       part_intersection.push_back ( &part_membership );
00088       return bulk.declare_entity(rank, nextEntityId(), part_intersection); 
00089     }
00090 
00091   private:
00092     EntityId entity_id;
00093     const int psize;
00094     const int prank;
00095 
00096 };
00097 
00098 
00099 TopologyHelpersTestingFixture::TopologyHelpersTestingFixture(ParallelMachine pm)
00100   : spatial_dimension( 3 )
00101   , meta( stk::mesh::TopologicalMetaData::entity_rank_names(spatial_dimension) )
00102   , bulk( meta, pm, 100 )
00103   , top( meta, spatial_dimension )
00104   , generic_element_part( meta.declare_part("another part", top.element_rank ) ) 
00105   , element_tet_part( top.declare_part<shards::Tetrahedron<4> >("block_left_1" ) )
00106   , element_wedge_part( top.declare_part<shards::Wedge<15> >("block_left_2" ) )
00107   , generic_face_part( top.declare_part<shards::Quadrilateral<4> >("A_1" ) )
00108   , another_generic_face_part( meta.declare_part("A_2", top.side_rank ) )
00109   , face_quad_part( meta.declare_part("A_3", top.side_rank ) )
00110   , another_generic_element_part( meta.declare_part("B_3", top.element_rank ) )
00111   , entity_id(0u)
00112   , psize(bulk.parallel_size())
00113   , prank(bulk.parallel_rank())
00114 {
00115   meta.commit();
00116 }
00117  
00118 
00119 STKUNIT_UNIT_TEST( testTopologyHelpers, get_cell_topology_based_on_part)
00120 {
00121   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00122   fix.bulk.modification_begin();
00123   Entity & elem1  = fix.create_entity( fix.top.side_rank, fix.generic_face_part );
00124 
00125   PartVector tmp(1);
00126   tmp[0] = & fix.face_quad_part;
00127   fix.bulk.change_entity_parts ( elem1 , tmp );
00128   STKUNIT_ASSERT_EQUAL( TopologicalMetaData::get_cell_topology(elem1), shards::getCellTopologyData< shards::Quadrilateral<4> >() );
00129   fix.bulk.change_entity_parts ( elem1 , tmp );
00130   STKUNIT_ASSERT_EQUAL( TopologicalMetaData::get_cell_topology(elem1), shards::getCellTopologyData< shards::Quadrilateral<4> >() );
00131   tmp[0] = & fix.another_generic_face_part;
00132   fix.bulk.change_entity_parts ( elem1 , tmp );
00133   STKUNIT_ASSERT_EQUAL( TopologicalMetaData::get_cell_topology(elem1), shards::getCellTopologyData< shards::Quadrilateral<4> >() );
00134   STKUNIT_ASSERT_NE( TopologicalMetaData::get_cell_topology( elem1) , shards::getCellTopologyData< shards::Wedge<15> >() );
00135 
00136   fix.bulk.modification_end();
00137 }
00138 
00139 
00140 
00141 STKUNIT_UNIT_TEST( testTopologyHelpers, get_cell_topology_multiple_topologies )
00142 {
00143   // Coverage for get_cell_topology in TopologyHelpers.cpp; (FAILED WITH MULTIPLE LOCAL TOPOLOGIES)
00144   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00145 
00146   fix.bulk.modification_begin();
00147   Entity & elem  = fix.create_entity( fix.top.element_rank, fix.generic_element_part );
00148   PartVector add_parts;
00149   add_parts.push_back( &fix.element_tet_part );
00150   add_parts.push_back( &fix.element_wedge_part );
00151   fix.bulk.change_entity_parts( elem, add_parts );
00152   fix.bulk.modification_end();
00153   STKUNIT_ASSERT_THROW( TopologicalMetaData::get_cell_topology( elem ), std::runtime_error );
00154 }
00155 
00156 
00157 
00158 STKUNIT_UNIT_TEST( testTopologyHelpers, get_adjacent_entities_trivial )
00159 {
00160   // Element, elem2, has NULL topology
00161   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00162 
00163   if ( 1 == fix.bulk.parallel_size() ) {
00164 
00165     fix.bulk.modification_begin();
00166     Entity & elem2  = fix.create_entity( fix.top.element_rank, fix.generic_element_part );
00167     fix.bulk.modification_end();
00168 
00169     std::vector<EntitySideComponent> adjacent_entities;
00170     const EntityRank subcell_rank = fix.top.element_rank;
00171     const EntityId subcell_identifier = 1;
00172     get_adjacent_entities( elem2 , subcell_rank, subcell_identifier, adjacent_entities);
00173     STKUNIT_ASSERT_TRUE( true );
00174   }
00175 }
00176 
00177 
00178 STKUNIT_UNIT_TEST( testTopologyHelpers, get_adjacent_entities_invalid )
00179 {
00180   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00181   fix.bulk.modification_begin();
00182   Entity & elem3  = fix.create_entity( fix.top.element_rank , fix.generic_element_part );
00183 
00184   PartVector add_parts;
00185   add_parts.push_back( & fix.element_tet_part );
00186   fix.bulk.change_entity_parts ( elem3 , add_parts );
00187   fix.bulk.modification_end();
00188   std::vector<EntitySideComponent> adjacent_entities2;
00189   {
00190     const EntityRank invalid_subcell_rank = 4;
00191     const EntityId valid_subcell_identifier = 0;
00192     STKUNIT_ASSERT_THROW( 
00193         get_adjacent_entities( elem3 , invalid_subcell_rank, valid_subcell_identifier, adjacent_entities2),
00194         std::runtime_error
00195         );
00196   }
00197   {
00198     const EntityRank valid_subcell_rank = 1;
00199     const EntityId invalid_subcell_identifier = 8;
00200     STKUNIT_ASSERT_THROW( 
00201       get_adjacent_entities( elem3 , valid_subcell_rank, invalid_subcell_identifier, adjacent_entities2),
00202       std::runtime_error
00203       );
00204   }
00205 }
00206 
00207 
00208 STKUNIT_UNIT_TEST( testTopologyHelpers, declare_element_side_no_topology )
00209 {
00210   // Coverage for declare_element_side - TopologyHelpers.cpp - "Cannot discern element topology"
00211   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00212 
00213   fix.bulk.modification_begin();
00214   Entity & elem4  = fix.create_entity( fix.top.element_rank , fix.generic_element_part );
00215   STKUNIT_ASSERT_THROW(
00216       declare_element_side( fix.bulk, fix.top.element_rank, elem4, fix.nextEntityId(), &fix.element_wedge_part ),
00217       std::runtime_error
00218       );
00219   fix.bulk.modification_end();
00220 
00221 
00222   {
00223     EntityId elem_node[4];
00224     elem_node[0] = 1;
00225     elem_node[1] = 2;
00226     elem_node[2] = 3;
00227     elem_node[3] = 4;
00228     fix.bulk.modification_begin();
00229     // Cannot declare an element without a topology defined 
00230     STKUNIT_ASSERT_THROW( 
00231         declare_element(fix.bulk, fix.generic_element_part, fix.nextEntityId(), elem_node),
00232         std::runtime_error
00233         );
00234     fix.bulk.modification_end();
00235   }
00236 }
00237 
00238 
00239 STKUNIT_UNIT_TEST( testTopologyHelpers, declare_element_side_wrong_bulk_data)
00240 {
00241   // Coverage for verify_declare_element_side - in TopologyHelpers.cpp - "BulkData for 'elem' and 'side' are different"
00242   TopologyHelpersTestingFixture fix1(MPI_COMM_WORLD);
00243 
00244   fix1.bulk.modification_begin();
00245 
00246   TopologyHelpersTestingFixture fix2(MPI_COMM_WORLD);
00247   fix2.bulk.modification_begin();
00248   Entity & elem4_2  = fix2.create_entity( fix1.top.element_rank , fix2.generic_element_part );
00249   fix2.bulk.modification_end();
00250 
00251   STKUNIT_ASSERT_THROW(
00252       declare_element_side( fix1.bulk, fix1.top.element_rank, elem4_2, fix1.nextEntityId(), &fix1.element_wedge_part),
00253       std::runtime_error
00254       );
00255     fix1.bulk.modification_end();
00256 }
00257 
00258 
00259 STKUNIT_UNIT_TEST( testTopologyHelpers, declare_element_side_no_topology_2 )
00260 {
00261   // Coverage for verify_declare_element_side - in TopologyHelpers.cpp - "No element topology found and cell side id exceeds..."
00262   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00263   fix.bulk.modification_begin();
00264 
00265   EntityId elem_node[4];
00266   elem_node[0] = 1;
00267   elem_node[1] = 2;
00268   elem_node[2] = 3;
00269   elem_node[3] = 4;
00270   Entity & element  = declare_element(fix.bulk, fix.element_tet_part, fix.nextEntityId(), elem_node);
00271   const CellTopologyData * const elem_top = TopologicalMetaData::get_cell_topology( element );
00272   const EntityId nSideCount = elem_top->side_count + 10 ;
00273   STKUNIT_ASSERT_THROW( 
00274       declare_element_side( fix.bulk, fix.nextEntityId(), element, nSideCount, &fix.element_tet_part ),
00275       std::runtime_error
00276       ); 
00277   fix.bulk.modification_end();
00278 }
00279 
00280 
00281 STKUNIT_UNIT_TEST( testTopologyHelpers, declare_element_side_full )
00282 {
00283   // Go all way the through declare_element_side - use new element
00284   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00285 
00286   fix.bulk.modification_begin();
00287 
00288   EntityId elem_node[4];
00289   elem_node[0] = 1;
00290   elem_node[1] = 2;
00291   elem_node[2] = 3;
00292   elem_node[3] = 4;
00293 
00294   Entity& element = declare_element(fix.bulk, fix.element_tet_part, fix.nextEntityId(), elem_node );
00295 
00296   const EntityId zero_side_count = 0;
00297   Entity& face2 = declare_element_side( fix.bulk, fix.nextEntityId(), element, zero_side_count);
00298   fix.bulk.modification_end();
00299 
00300   PairIterRelation rel2 = face2.relations(fix.top.node_rank);
00301 
00302   STKUNIT_ASSERT_TRUE( true );
00303 }
00304 
00305 
00306 STKUNIT_UNIT_TEST( testTopologyHelpers, element_side_polarity_valid )
00307 {
00308   // Coverage of element_side_polarity in TopologyHelpers.cpp 168-181 and 200-215
00309   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00310   EntityId elem_node[4];
00311   elem_node[0] = 1;
00312   elem_node[1] = 2;
00313   elem_node[2] = 3;
00314   elem_node[3] = 4;
00315 
00316   fix.bulk.modification_begin();
00317   Entity & element = declare_element(fix.bulk, fix.element_tet_part, fix.nextEntityId(), elem_node );
00318   const EntityId zero_side_count = 0;
00319   Entity& face2 = declare_element_side( fix.bulk, fix.nextEntityId(), element, zero_side_count);
00320   fix.bulk.modification_end();
00321 
00322   const int local_side_id = 0;
00323   STKUNIT_ASSERT_TRUE( element_side_polarity( element, face2, local_side_id) );
00324 
00325 } 
00326 
00327 
00328 STKUNIT_UNIT_TEST( testTopologyHelpers, element_side_polarity_invalid_1 )
00329 {
00330   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00331   EntityId elem_node[4];
00332   elem_node[0] = 1;
00333   elem_node[1] = 2;
00334   elem_node[2] = 3;
00335   elem_node[3] = 4;
00336 
00337   // Coverage of element_side_polarity in TopologyHelpers.cpp
00338   {
00339     fix.bulk.modification_begin();
00340     Entity & element = declare_element(fix.bulk, fix.element_tet_part, fix.nextEntityId(), elem_node );
00341     const EntityId zero_side_count = 0;
00342     Entity& face = declare_element_side( fix.bulk, fix.nextEntityId(), element, zero_side_count);
00343     fix.bulk.modification_end();
00344 
00345     const int invalid_local_side_id = -1;
00346     // Hits "Unsuported local_side_id" error condition:
00347     STKUNIT_ASSERT_THROW(
00348         element_side_polarity( element, face, invalid_local_side_id),
00349         std::runtime_error
00350         );
00351   }
00352 }
00353 
00354 
00355 STKUNIT_UNIT_TEST( testTopologyHelpers, element_side_polarity_invalid_2 )
00356 {
00357   TopologyHelpersTestingFixture fix(MPI_COMM_WORLD);
00358   EntityId elem_node[4];
00359   elem_node[0] = 1;
00360   elem_node[1] = 2;
00361   elem_node[2] = 3;
00362   elem_node[3] = 4;
00363   
00364   // Coverage of element_side_polarity in TopologyHelpers.cpp - NULL = elem_top
00365   fix.bulk.modification_begin();
00366 
00367   PartVector part_intersection;
00368   part_intersection.push_back ( &fix.generic_element_part);
00369   Entity & element = fix.bulk.declare_entity(fix.top.element_rank, fix.nextEntityId(), part_intersection); 
00370   STKUNIT_ASSERT_TRUE( TopologicalMetaData::get_cell_topology( element ) == NULL );
00371 
00372   Entity & element_with_top = declare_element(fix.bulk, fix.element_tet_part, fix.nextEntityId(), elem_node );
00373   STKUNIT_ASSERT_TRUE( TopologicalMetaData::get_cell_topology( element_with_top ) != NULL );
00374 
00375   const EntityId zero_side_count = 0;
00376   Entity& face_with_top = declare_element_side( fix.bulk, fix.nextEntityId(), element_with_top, zero_side_count);
00377 
00378   fix.bulk.modification_end();
00379 
00380   const int valid_local_side_id = 0;
00381   // Hits "Element has no defined topology" error condition:
00382   STKUNIT_ASSERT_TRUE( TopologicalMetaData::get_cell_topology( element ) == NULL );
00383   STKUNIT_ASSERT_THROW(
00384       element_side_polarity( element, face_with_top, valid_local_side_id),
00385       std::runtime_error
00386       );
00387 
00388 }
00389 
00390 //----------------------------------------------------------------------
00391 //----------------------------------------------------------------------
00392 
00393 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends