| 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 "Setup2Block2HexMesh.hpp" |
| 36 |
#include "stk_io/StkMeshIoBroker.hpp" |
| 37 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 38 |
#include "stk_mesh/base/Types.hpp" // for BucketVector, EntityRank |
| 39 |
#include "stk_mesh/baseImpl/MeshImplUtils.hpp" |
| 40 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 41 |
#include <gtest/gtest.h> |
| 42 |
#include <stddef.h> // for size_t |
| 43 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 44 |
#include <stk_mesh/base/Comm.hpp> // for comm_mesh_counts |
| 45 |
#include <stk_mesh/base/CreateFaces.hpp> |
| 46 |
#include <stk_mesh/base/GetEntities.hpp> // for count_entities |
| 47 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 48 |
#include <stk_unit_test_utils/BulkDataTester.hpp> |
| 49 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 50 |
#include <stk_unit_test_utils/getOption.h> |
| 51 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 52 |
#include <vector> // for vector, vector<>::iterator |
| 53 |
|
| 54 |
namespace { |
| 55 |
using stk::unit_test_util::build_mesh; |
| 56 |
|
| 57 |
TEST ( MeshImplUtils, find_elements_these_nodes_have_in_common ) |
| 58 |
{ |
| 59 |
stk::ParallelMachine communicator = MPI_COMM_WORLD; |
| 60 |
int numProcs = stk::parallel_machine_size(communicator); |
| 61 |
if (numProcs > 2) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
const unsigned spatialDim = 3; |
| 66 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator); |
| 67 |
stk::mesh::BulkData& bulk = *bulkPtr; |
| 68 |
|
| 69 |
setup2Block2HexMesh(bulk); |
| 70 |
|
| 71 |
const unsigned numNodesPerEdge = 2; |
| 72 |
|
| 73 |
//edge 2-6 is connected to elements 1 and 2 |
| 74 |
stk::mesh::EntityId edge_2_6_nodeIds[] = {2, 6}; |
| 75 |
stk::mesh::Entity edge_2_6_nodes[numNodesPerEdge]; |
| 76 |
edge_2_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[0]); |
| 77 |
edge_2_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[1]); |
| 78 |
|
| 79 |
//edge 5-6 is only connected to element 1 |
| 80 |
stk::mesh::EntityId edge_5_6_nodeIds[] = {5, 6}; |
| 81 |
stk::mesh::Entity edge_5_6_nodes[numNodesPerEdge]; |
| 82 |
edge_5_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[0]); |
| 83 |
edge_5_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[1]); |
| 84 |
|
| 85 |
std::vector<stk::mesh::Entity> elements; |
| 86 |
|
| 87 |
stk::mesh::impl::find_entities_these_nodes_have_in_common(bulk, stk::topology::ELEMENT_RANK, numNodesPerEdge, edge_2_6_nodes, elements); |
| 88 |
|
| 89 |
size_t expected_num_elements = 2; |
| 90 |
EXPECT_EQ(expected_num_elements, elements.size()); |
| 91 |
|
| 92 |
stk::mesh::impl::find_entities_these_nodes_have_in_common(bulk, stk::topology::ELEMENT_RANK, numNodesPerEdge, edge_5_6_nodes, elements); |
| 93 |
|
| 94 |
expected_num_elements = 1; |
| 95 |
EXPECT_EQ(expected_num_elements, elements.size()); |
| 96 |
} |
| 97 |
|
| 98 |
TEST ( MeshImplUtils, find_faces_these_nodes_have_in_common ) |
| 99 |
{ |
| 100 |
stk::ParallelMachine communicator = MPI_COMM_WORLD; |
| 101 |
int numProcs = stk::parallel_machine_size(communicator); |
| 102 |
if (numProcs > 2) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
const unsigned spatialDim = 3; |
| 107 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator); |
| 108 |
stk::mesh::BulkData& bulk = *bulkPtr; |
| 109 |
|
| 110 |
setup2Block2HexMesh(bulk); |
| 111 |
stk::mesh::create_faces(bulk); |
| 112 |
|
| 113 |
const unsigned numNodesPerEdge = 2; |
| 114 |
|
| 115 |
//edge 2-6 is connected to elements 1 and 2 |
| 116 |
stk::mesh::EntityId edge_2_6_nodeIds[] = {2, 6}; |
| 117 |
stk::mesh::Entity edge_2_6_nodes[numNodesPerEdge]; |
| 118 |
edge_2_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[0]); |
| 119 |
edge_2_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[1]); |
| 120 |
|
| 121 |
//edge 5-6 is only connected to element 1 |
| 122 |
stk::mesh::EntityId edge_5_6_nodeIds[] = {5, 6}; |
| 123 |
stk::mesh::Entity edge_5_6_nodes[numNodesPerEdge]; |
| 124 |
edge_5_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[0]); |
| 125 |
edge_5_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[1]); |
| 126 |
|
| 127 |
std::vector<stk::mesh::Entity> faces; |
| 128 |
|
| 129 |
stk::mesh::impl::find_entities_these_nodes_have_in_common(bulk, stk::topology::FACE_RANK, numNodesPerEdge, &edge_2_6_nodes[0],faces); |
| 130 |
|
| 131 |
size_t expected_num_faces = 3; |
| 132 |
EXPECT_EQ(expected_num_faces, faces.size()); |
| 133 |
|
| 134 |
stk::mesh::impl::find_entities_these_nodes_have_in_common(bulk, stk::topology::FACE_RANK, numNodesPerEdge, &edge_5_6_nodes[0],faces); |
| 135 |
|
| 136 |
expected_num_faces = 2; |
| 137 |
EXPECT_EQ(expected_num_faces, faces.size()); |
| 138 |
} |
| 139 |
|
| 140 |
TEST ( MeshImplUtils, find_locally_owned_elements_these_nodes_have_in_common ) |
| 141 |
{ |
| 142 |
stk::ParallelMachine communicator = MPI_COMM_WORLD; |
| 143 |
int numProcs = stk::parallel_machine_size(communicator); |
| 144 |
if (numProcs > 2) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
const unsigned spatialDim = 3; |
| 149 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator); |
| 150 |
stk::mesh::BulkData& bulk = *bulkPtr; |
| 151 |
|
| 152 |
setup2Block2HexMesh(bulk); |
| 153 |
|
| 154 |
const unsigned numNodesPerEdge = 2; |
| 155 |
|
| 156 |
//edge 2-6 is connected to elements 1 and 2 |
| 157 |
stk::mesh::EntityId edge_2_6_nodeIds[] = {2, 6}; |
| 158 |
stk::mesh::Entity edge_2_6_nodes[numNodesPerEdge]; |
| 159 |
edge_2_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[0]); |
| 160 |
edge_2_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[1]); |
| 161 |
|
| 162 |
//edge 5-6 is only connected to element 1 |
| 163 |
stk::mesh::EntityId edge_5_6_nodeIds[] = {5, 6}; |
| 164 |
stk::mesh::Entity edge_5_6_nodes[numNodesPerEdge]; |
| 165 |
edge_5_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[0]); |
| 166 |
edge_5_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_5_6_nodeIds[1]); |
| 167 |
|
| 168 |
std::vector<stk::mesh::Entity> elements; |
| 169 |
|
| 170 |
stk::mesh::impl::find_locally_owned_elements_these_nodes_have_in_common(bulk, numNodesPerEdge, edge_2_6_nodes, elements); |
| 171 |
|
| 172 |
size_t expected_num_elements = 1; |
| 173 |
if (numProcs == 1) { |
| 174 |
expected_num_elements = 2; |
| 175 |
} |
| 176 |
EXPECT_EQ(expected_num_elements, elements.size()); |
| 177 |
|
| 178 |
stk::mesh::impl::find_locally_owned_elements_these_nodes_have_in_common(bulk, numNodesPerEdge, edge_5_6_nodes, elements); |
| 179 |
|
| 180 |
expected_num_elements = 0; |
| 181 |
if (bulk.parallel_rank() == 0) { |
| 182 |
//edge_5_6 is connected to element 1, which is locally-owned on proc 0 |
| 183 |
expected_num_elements = 1; |
| 184 |
} |
| 185 |
EXPECT_EQ(expected_num_elements, elements.size()); |
| 186 |
} |
| 187 |
|
| 188 |
TEST(MeshImplUtils, do_these_nodes_have_any_shell_elements_in_common_no_nodes) |
| 189 |
{ |
| 190 |
const int spatialDim = 3; |
| 191 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 192 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 193 |
EXPECT_FALSE(stk::mesh::impl::do_these_nodes_have_any_shell_elements_in_common(mesh, 0, NULL)); |
| 194 |
} |
| 195 |
|
| 196 |
TEST(MeshImplUtils, do_these_nodes_have_any_shell_elements_in_common_hexshell) |
| 197 |
{ |
| 198 |
const int spatialDim = 3; |
| 199 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 200 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 201 |
if (mesh.parallel_size() == 1) { |
| 202 |
stk::io::StkMeshIoBroker exodus_file_reader(MPI_COMM_WORLD); |
| 203 |
exodus_file_reader.set_bulk_data(mesh); |
| 204 |
exodus_file_reader.add_mesh_database("generated:1x1x1|shell:X", stk::io::READ_MESH); |
| 205 |
exodus_file_reader.create_input_mesh(); |
| 206 |
exodus_file_reader.populate_bulk_data(); |
| 207 |
{ |
| 208 |
stk::mesh::Entity shell = (*mesh.buckets(stk::topology::ELEMENT_RANK)[1])[0]; |
| 209 |
ASSERT_TRUE(mesh.bucket(shell).topology().is_shell()); |
| 210 |
stk::mesh::EntityVector nodes(4); |
| 211 |
for (unsigned i=0 ; i<4 ; ++i) { |
| 212 |
nodes[i] = mesh.begin_nodes(shell)[i]; |
| 213 |
} |
| 214 |
EXPECT_TRUE(stk::mesh::impl::do_these_nodes_have_any_shell_elements_in_common(mesh, 4, nodes.data())); |
| 215 |
} |
| 216 |
{ |
| 217 |
stk::mesh::Entity hex = (*mesh.buckets(stk::topology::ELEMENT_RANK)[0])[0]; |
| 218 |
ASSERT_FALSE(mesh.bucket(hex).topology().is_shell()); |
| 219 |
stk::mesh::EntityVector nodes(8); |
| 220 |
for (unsigned i=0 ; i<8 ; ++i) { |
| 221 |
nodes[i] = mesh.begin_nodes(hex)[i]; |
| 222 |
} |
| 223 |
EXPECT_FALSE(stk::mesh::impl::do_these_nodes_have_any_shell_elements_in_common(mesh, 8, nodes.data())); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
TEST(MeshImplUtils, do_these_nodes_have_any_shell_elements_in_common_hexshellwrap) |
| 230 |
{ |
| 231 |
const int spatialDim = 3; |
| 232 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 233 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 234 |
if (mesh.parallel_size() == 1) { |
| 235 |
stk::io::StkMeshIoBroker exodus_file_reader(MPI_COMM_WORLD); |
| 236 |
exodus_file_reader.set_bulk_data(mesh); |
| 237 |
exodus_file_reader.add_mesh_database("generated:1x1x1|shell:xXyY", stk::io::READ_MESH); |
| 238 |
exodus_file_reader.create_input_mesh(); |
| 239 |
exodus_file_reader.populate_bulk_data(); |
| 240 |
stk::mesh::Entity hex = (*mesh.buckets(stk::topology::ELEMENT_RANK)[0])[0]; |
| 241 |
ASSERT_FALSE(mesh.bucket(hex).topology().is_shell()); |
| 242 |
stk::mesh::EntityVector nodes(4); |
| 243 |
for (unsigned i=0 ; i<4 ; ++i) { |
| 244 |
nodes[i] = mesh.begin_nodes(hex)[i]; |
| 245 |
} |
| 246 |
EXPECT_FALSE(stk::mesh::impl::do_these_nodes_have_any_shell_elements_in_common(mesh, 4, nodes.data())); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
template<typename T> |
| 251 |
std::string vec_to_string(const std::vector<T> &vec) |
| 252 |
{ |
| 253 |
std::string s = "["; |
| 254 |
for(const T &item : vec) |
| 255 |
s += " " + std::to_string(item); |
| 256 |
return s + " ]"; |
| 257 |
} |
| 258 |
|
| 259 |
class EntitiesNodesHaveInCommon : public stk::unit_test_util::MeshFixture |
| 260 |
{ |
| 261 |
protected: |
| 262 |
void expect_nodes_have_elems_in_common(stk::mesh::EntityId elemId, |
| 263 |
const stk::mesh::EntityIdVector &nodeIds, |
| 264 |
const stk::mesh::EntityIdVector &goldElemIds) |
| 265 |
{ |
| 266 |
stk::mesh::EntityVector nodes(nodeIds.size()); |
| 267 |
for(unsigned i=0; i<nodes.size(); i++) |
| 268 |
nodes[i] = get_bulk().get_entity(stk::topology::NODE_RANK, nodeIds[i]); |
| 269 |
|
| 270 |
stk::mesh::EntityVector elementsInCommon; |
| 271 |
stk::mesh::impl::find_entities_with_larger_ids_these_nodes_have_in_common_and_locally_owned(elemId, |
| 272 |
get_bulk(), |
| 273 |
stk::topology::ELEM_RANK, |
| 274 |
nodes.size(), |
| 275 |
nodes.data(), |
| 276 |
elementsInCommon); |
| 277 |
stk::mesh::EntityIdVector elemIds = entities_to_ids(elementsInCommon); |
| 278 |
ASSERT_EQ(goldElemIds.size(), elemIds.size()) |
| 279 |
<< "expected " << vec_to_string(goldElemIds) << " got " << vec_to_string(elemIds); |
| 280 |
std::sort(elemIds.begin(), elemIds.end()); |
| 281 |
for(size_t i=0; i<goldElemIds.size(); i++) |
| 282 |
EXPECT_EQ(goldElemIds[i], elemIds[i]); |
| 283 |
} |
| 284 |
|
| 285 |
stk::mesh::EntityIdVector entities_to_ids(const stk::mesh::EntityVector &entities) |
| 286 |
{ |
| 287 |
stk::mesh::EntityIdVector ids(entities.size()); |
| 288 |
for(size_t i=0; i<entities.size(); i++) |
| 289 |
ids[i] = get_bulk().identifier(entities[i]); |
| 290 |
return ids; |
| 291 |
} |
| 292 |
}; |
| 293 |
|
| 294 |
TEST_F(EntitiesNodesHaveInCommon, 2elems) |
| 295 |
{ |
| 296 |
if(stk::parallel_machine_size(get_comm()) == 1) |
| 297 |
{ |
| 298 |
setup_mesh("generated:1x1x2", stk::mesh::BulkData::NO_AUTO_AURA); |
| 299 |
|
| 300 |
expect_nodes_have_elems_in_common(0, {1, 2, 3, 4}, {1}); |
| 301 |
expect_nodes_have_elems_in_common(1, {1, 2, 3, 4}, {}); |
| 302 |
expect_nodes_have_elems_in_common(0, {5, 6, 7, 8}, {1, 2}); |
| 303 |
expect_nodes_have_elems_in_common(1, {5, 6, 7, 8}, {2}); |
| 304 |
expect_nodes_have_elems_in_common(0, {9, 10, 11, 12}, {2}); |
| 305 |
|
| 306 |
expect_nodes_have_elems_in_common(0, {5, 9}, {2}); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
TEST_F(EntitiesNodesHaveInCommon, 8elems) |
| 311 |
{ |
| 312 |
if(stk::parallel_machine_size(get_comm()) == 1) |
| 313 |
{ |
| 314 |
setup_mesh("generated:2x2x2", stk::mesh::BulkData::NO_AUTO_AURA); |
| 315 |
|
| 316 |
expect_nodes_have_elems_in_common(0, {14}, {1, 2, 3, 4, 5, 6, 7, 8}); |
| 317 |
expect_nodes_have_elems_in_common(0, {14, 13}, {1, 3, 5, 7}); |
| 318 |
expect_nodes_have_elems_in_common(0, {14, 13, 10}, {1, 5}); |
| 319 |
expect_nodes_have_elems_in_common(0, {14, 13, 10, 1}, {1}); |
| 320 |
expect_nodes_have_elems_in_common(0, {14, 13, 10, 11}, {1, 5}); |
| 321 |
|
| 322 |
expect_nodes_have_elems_in_common(1, {14}, {2, 3, 4, 5, 6, 7, 8}); |
| 323 |
expect_nodes_have_elems_in_common(1, {14, 13}, {3, 5, 7}); |
| 324 |
expect_nodes_have_elems_in_common(1, {14, 13, 10}, {5}); |
| 325 |
expect_nodes_have_elems_in_common(1, {14, 13, 10, 11}, {5}); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
} |