| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 36 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 37 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 38 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, entity_rank_names, etc |
| 39 |
#include <stk_topology/topology.hpp> // for topology, etc |
| 40 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 41 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 42 |
#include "stk_mesh/base/Types.hpp" // for EntityId |
| 43 |
namespace stk { namespace mesh { class Part; } } |
| 44 |
|
| 45 |
namespace { |
| 46 |
|
| 47 |
void check_connectivities_for_stkMeshHowTo_generateNewEntities(stk::mesh::BulkData &mesh, |
| 48 |
stk::mesh::Entity tet_elem, stk::mesh::Entity hex_elem, |
| 49 |
const std::vector<stk::mesh::Entity> &generated_entities) |
| 50 |
{ |
| 51 |
// Check that entities are connected as expected. |
| 52 |
unsigned node_i = 0; |
| 53 |
EXPECT_EQ(4u, mesh.num_nodes(tet_elem)); |
| 54 |
stk::mesh::Entity const *node_rels = mesh.begin_nodes(tet_elem); |
| 55 |
for(unsigned node_ord = 0 ; node_ord < 4; ++node_ord, ++node_i) |
| 56 |
{ |
| 57 |
EXPECT_TRUE(mesh.is_valid(node_rels[node_ord])); |
| 58 |
EXPECT_EQ(generated_entities[node_i], node_rels[node_ord]); |
| 59 |
} |
| 60 |
EXPECT_EQ(8u, mesh.num_nodes(hex_elem)); |
| 61 |
node_rels = mesh.begin_nodes(hex_elem); |
| 62 |
for(unsigned node_ord = 0 ; node_ord < 8; ++node_ord, ++node_i) |
| 63 |
{ |
| 64 |
EXPECT_TRUE(mesh.is_valid(node_rels[node_ord])); |
| 65 |
EXPECT_EQ(generated_entities[node_i], node_rels[node_ord]); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
//-BEGIN |
| 70 |
TEST(stkMeshHowTo, generateNewEntities) |
| 71 |
{ |
| 72 |
const unsigned spatialDimension = 3; |
| 73 |
|
| 74 |
stk::mesh::MeshBuilder builder(MPI_COMM_WORLD); |
| 75 |
builder.set_spatial_dimension(spatialDimension); |
| 76 |
builder.set_entity_rank_names(stk::mesh::entity_rank_names()); |
| 77 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = builder.create(); |
| 78 |
stk::mesh::MetaData& metaData = bulkPtr->mesh_meta_data(); |
| 79 |
stk::mesh::Part &tetPart = metaData.declare_part_with_topology("tetElementPart", stk::topology::TET_4); |
| 80 |
stk::mesh::Part &hexPart = metaData.declare_part_with_topology("hexElementPart", stk::topology::HEX_8); |
| 81 |
metaData.commit(); |
| 82 |
|
| 83 |
// Parts vectors handy for setting topology later. |
| 84 |
std::vector<stk::mesh::Part *> add_tetPart(1); |
| 85 |
add_tetPart[0] = &tetPart; |
| 86 |
std::vector<stk::mesh::Part *> add_hexPart(1); |
| 87 |
add_hexPart[0] = &hexPart; |
| 88 |
|
| 89 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 90 |
mesh.modification_begin(); |
| 91 |
|
| 92 |
std::vector<size_t> requests(metaData.entity_rank_count(), 0); |
| 93 |
const size_t num_nodes_requested = 12; |
| 94 |
const size_t num_elems_requested = 2; |
| 95 |
requests[stk::topology::NODE_RANK] = num_nodes_requested; |
| 96 |
requests[stk::topology::ELEMENT_RANK] = num_elems_requested; |
| 97 |
std::vector<stk::mesh::Entity> requested_entities; |
| 98 |
|
| 99 |
mesh.generate_new_entities(requests, requested_entities); |
| 100 |
|
| 101 |
// Set topologies of new entities with rank > stk::topology::NODE_RANK. |
| 102 |
stk::mesh::Entity elem1 = requested_entities[num_nodes_requested]; |
| 103 |
mesh.change_entity_parts(elem1, add_tetPart); |
| 104 |
stk::mesh::Entity elem2 = requested_entities[num_nodes_requested + 1]; |
| 105 |
mesh.change_entity_parts(elem2, add_hexPart); |
| 106 |
|
| 107 |
// Set downward relations of entities with rank > stk::topology::NODE_RANK |
| 108 |
unsigned node_i = 0; |
| 109 |
for(unsigned node_ord = 0 ; node_ord < 4; ++node_ord, ++node_i) |
| 110 |
{ |
| 111 |
mesh.declare_relation( elem1 , requested_entities[node_i] , node_ord); |
| 112 |
} |
| 113 |
for(unsigned node_ord = 0 ; node_ord < 8; ++node_ord, ++node_i) |
| 114 |
{ |
| 115 |
mesh.declare_relation( elem2 , requested_entities[node_i] , node_ord); |
| 116 |
} |
| 117 |
mesh.modification_end(); |
| 118 |
|
| 119 |
check_connectivities_for_stkMeshHowTo_generateNewEntities(mesh, elem1, elem2, requested_entities); |
| 120 |
|
| 121 |
// Not setting topologies of new entities with rank > stk::topology::NODE_RANK causes throw |
| 122 |
mesh.modification_begin(); |
| 123 |
std::vector<stk::mesh::Entity> more_requested_entities; |
| 124 |
mesh.generate_new_entities(requests, more_requested_entities); |
| 125 |
#ifdef NDEBUG |
| 126 |
mesh.modification_end(); |
| 127 |
#else |
| 128 |
EXPECT_THROW(mesh.modification_end(), std::logic_error); |
| 129 |
#endif |
| 130 |
} |
| 131 |
//-END |
| 132 |
} |