| 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 |
#include <gtest/gtest.h> |
| 35 |
#include <stk_mesh/base/Comm.hpp> |
| 36 |
#include <stk_mesh/base/BulkData.hpp> |
| 37 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 38 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 39 |
#include <stk_tools/block_extractor/ExtractBlocks.hpp> |
| 40 |
#include "stk_mesh/base/Entity.hpp" |
| 41 |
|
| 42 |
namespace |
| 43 |
{ |
| 44 |
using stk::unit_test_util::build_mesh; |
| 45 |
|
| 46 |
class MeshWithTwoBlocks : public stk::unit_test_util::MeshFixture |
| 47 |
{ |
| 48 |
protected: |
| 49 |
void switch_half_mesh_to_part(stk::mesh::Part &addPart, stk::mesh::Part &removePart) |
| 50 |
{ |
| 51 |
get_bulk().modification_begin(); |
| 52 |
switch_elem_to_part(1, addPart, removePart); |
| 53 |
switch_elem_to_part(2, addPart, removePart); |
| 54 |
get_bulk().modification_end(); |
| 55 |
} |
| 56 |
|
| 57 |
void switch_elem_to_part(stk::mesh::EntityId elemId, stk::mesh::Part &addPart, stk::mesh::Part &removePart) |
| 58 |
{ |
| 59 |
stk::mesh::Entity elem = get_bulk().get_entity(stk::topology::ELEM_RANK, elemId); |
| 60 |
if(get_bulk().is_valid(elem) && get_bulk().bucket(elem).owned()) |
| 61 |
get_bulk().change_entity_parts(elem, stk::mesh::ConstPartVector{&addPart}, stk::mesh::ConstPartVector{&removePart}); |
| 62 |
} |
| 63 |
|
| 64 |
void expect_num_elems_in_part(stk::mesh::BulkData &bulk, size_t expectedNum, stk::mesh::Part& block) |
| 65 |
{ |
| 66 |
stk::mesh::Selector selector(block); |
| 67 |
std::vector<size_t> counts; |
| 68 |
stk::mesh::comm_mesh_counts(bulk, counts, &selector); |
| 69 |
EXPECT_EQ(expectedNum, counts[stk::topology::ELEM_RANK]); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
TEST_F(MeshWithTwoBlocks, extractBlock2_onlyHaveBlock2) |
| 74 |
{ |
| 75 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 76 |
stk::mesh::Part &block2 = get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8); |
| 77 |
stk::io::fill_mesh("generated:1x1x4", get_bulk()); |
| 78 |
|
| 79 |
stk::mesh::Part &block1 = *get_meta().get_part("block_1"); |
| 80 |
switch_half_mesh_to_part(block2, block1); |
| 81 |
|
| 82 |
expect_num_elems_in_part(get_bulk(), 2, block1); |
| 83 |
expect_num_elems_in_part(get_bulk(), 2, block2); |
| 84 |
|
| 85 |
std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm()); |
| 86 |
stk::mesh::BulkData& newBulk = *newBulkPtr; |
| 87 |
stk::tools::extract_blocks(get_bulk(), newBulk, {"block_2"}); |
| 88 |
|
| 89 |
expect_num_elems_in_part(newBulk, 0, block1); |
| 90 |
expect_num_elems_in_part(newBulk, 2, block2); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_F(MeshWithTwoBlocks, getNodesetPartsAndNames) |
| 94 |
{ |
| 95 |
|
| 96 |
setup_mesh("generated:1x1x4|nodeset:xX", stk::mesh::BulkData::AUTO_AURA); |
| 97 |
|
| 98 |
|
| 99 |
std::vector<std::string> ns_names = stk::tools::find_nodeset_names_from_id(get_bulk(), {2,1}); |
| 100 |
|
| 101 |
std::vector<std::string> ns_names_expected = {"nodelist_2","nodelist_1"}; |
| 102 |
|
| 103 |
ASSERT_EQ(ns_names.size(), ns_names_expected.size() ); |
| 104 |
|
| 105 |
for(size_t k = 0 ; k < ns_names_expected.size(); ++k) |
| 106 |
EXPECT_EQ(ns_names[k], ns_names_expected[k] ); |
| 107 |
|
| 108 |
std::vector<stk::mesh::Part*> parts; |
| 109 |
stk::tools::GetPartsByName( parts,get_bulk(),ns_names); |
| 110 |
ASSERT_EQ(static_cast<size_t>(2), (parts.size()) ); |
| 111 |
stk::mesh::Part &ns1 = *get_meta().get_part("nodelist_1"); |
| 112 |
EXPECT_EQ(ns1, *(parts[1]) ); |
| 113 |
|
| 114 |
stk::mesh::Part &ns2 = *get_meta().get_part("nodelist_2"); |
| 115 |
EXPECT_EQ(ns2, *(parts[0]) ); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
TEST_F(MeshWithTwoBlocks, getBlockPartsAndNames) |
| 120 |
{ |
| 121 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 122 |
stk::mesh::Part &block2 = get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8); |
| 123 |
stk::io::fill_mesh("generated:1x1x4|nodeset:xX", get_bulk()); |
| 124 |
|
| 125 |
stk::mesh::Part &block1 = *get_meta().get_part("block_1"); |
| 126 |
switch_half_mesh_to_part(block2, block1); |
| 127 |
|
| 128 |
std::vector<std::string> blk_names = stk::tools::GetBlockNamesFromIDs(get_bulk(), {1,-1}); |
| 129 |
|
| 130 |
|
| 131 |
std::vector<std::string> blk_names_expected = {"block_1","block_2"}; |
| 132 |
|
| 133 |
ASSERT_EQ(blk_names.size(), blk_names_expected.size() ); |
| 134 |
|
| 135 |
for(size_t k = 0 ; k < blk_names_expected.size(); ++k) |
| 136 |
EXPECT_EQ(blk_names[k], blk_names_expected[k] ); |
| 137 |
|
| 138 |
std::vector<stk::mesh::Part*> parts; |
| 139 |
stk::tools::GetPartsByName( parts,get_bulk(),blk_names); |
| 140 |
ASSERT_EQ(static_cast<size_t>(2), (parts.size()) ); |
| 141 |
stk::mesh::Part &blk1 = *get_meta().get_part("block_1"); |
| 142 |
EXPECT_EQ(blk1, *(parts[0]) ); |
| 143 |
|
| 144 |
stk::mesh::Part &blk2 = *get_meta().get_part("block_2"); |
| 145 |
EXPECT_EQ(blk2, *(parts[1]) ); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
TEST_F(MeshWithTwoBlocks, getOneBlockAndOneNodeset) |
| 150 |
{ |
| 151 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 152 |
stk::mesh::Part &block2 = get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8); |
| 153 |
stk::io::fill_mesh("generated:1x1x4|nodeset:xX", get_bulk()); |
| 154 |
|
| 155 |
stk::mesh::Part &block1 = *get_meta().get_part("block_1"); |
| 156 |
switch_half_mesh_to_part(block2, block1); |
| 157 |
|
| 158 |
// apparently that block two gets constructed with ID=-1... |
| 159 |
std::vector<std::string> blk_names = stk::tools::GetBlockNamesFromIDs(get_bulk(), {-1}); |
| 160 |
std::vector<std::string> ns_names = stk::tools::find_nodeset_names_from_id(get_bulk(), {1}); |
| 161 |
|
| 162 |
stk::mesh::Selector theSelector = stk::tools::GetBlockAndNodesetSelector(get_bulk(), ns_names, blk_names); |
| 163 |
|
| 164 |
std::vector<size_t> counts; |
| 165 |
stk::mesh::comm_mesh_counts(get_bulk(), counts, &theSelector); |
| 166 |
size_t expectedNumElem = 2; // two elements in block 2 |
| 167 |
size_t expectedNumNode = 16; // 12 nodes associated with 2 elems in block 2, then 4 nodes from nodeset 1 |
| 168 |
|
| 169 |
EXPECT_EQ(expectedNumElem, counts[stk::topology::ELEM_RANK]); |
| 170 |
EXPECT_EQ(expectedNumNode, counts[stk::topology::NODE_RANK]); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
class MeshWithOneBlock : public stk::unit_test_util::MeshFixture |
| 175 |
{ |
| 176 |
|
| 177 |
}; |
| 178 |
|
| 179 |
TEST_F(MeshWithOneBlock, extractBlock2_throws) |
| 180 |
{ |
| 181 |
setup_mesh("generated:1x1x4", stk::mesh::BulkData::AUTO_AURA); |
| 182 |
std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm()); |
| 183 |
EXPECT_THROW(stk::tools::extract_blocks(get_bulk(), *newBulkPtr, {"block_2"}), std::exception); |
| 184 |
} |
| 185 |
|
| 186 |
} |