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