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