| 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/CreateEdges.hpp" |
| 36 |
#include "stk_mesh/base/GetEntities.hpp" |
| 37 |
#include "stk_mesh/base/MeshBuilder.hpp" |
| 38 |
#include "stk_mesh/base/BulkData.hpp" |
| 39 |
#include "stk_mesh/base/MetaData.hpp" |
| 40 |
#include "stk_io/FillMesh.hpp" |
| 41 |
#include "stk_util/parallel/ParallelReduce.hpp" |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
class EdgesOnFacesFixture : public ::testing::Test |
| 46 |
{ |
| 47 |
public: |
| 48 |
void setup(const std::string& meshSpec, bool enable_aura=true) |
| 49 |
{ |
| 50 |
stk::mesh::MeshBuilder builder(stk::parallel_machine_world()); |
| 51 |
builder.set_aura_option(enable_aura ? stk::mesh::BulkData::AUTO_AURA : stk::mesh::BulkData::NO_AUTO_AURA); |
| 52 |
bulk = builder.create(); |
| 53 |
|
| 54 |
stk::io::fill_mesh(meshSpec, *bulk); |
| 55 |
|
| 56 |
stk::mesh::get_entities(*bulk, stk::topology::FACE_RANK, bulk->mesh_meta_data().locally_owned_part(), |
| 57 |
ownedFaces); |
| 58 |
|
| 59 |
surfaceSelector = stk::mesh::Selector(*(bulk->get_sidesets()[0]->get_part())); |
| 60 |
} |
| 61 |
|
| 62 |
std::shared_ptr<stk::mesh::BulkData> bulk; |
| 63 |
stk::mesh::EntityVector ownedFaces; |
| 64 |
stk::mesh::Selector surfaceSelector; |
| 65 |
}; |
| 66 |
|
| 67 |
std::pair<int, int> countEdges(stk::mesh::BulkData& bulk, const stk::mesh::Selector& selector) |
| 68 |
{ |
| 69 |
int edgesInSelector = stk::mesh::count_entities(bulk, stk::topology::EDGE_RANK, |
| 70 |
selector & bulk.mesh_meta_data().locally_owned_part()); |
| 71 |
|
| 72 |
int totalEdges = stk::mesh::count_entities(bulk, stk::topology::EDGE_RANK, |
| 73 |
bulk.mesh_meta_data().locally_owned_part()); |
| 74 |
|
| 75 |
std::array<int, 2> edgeCountsLocal = {edgesInSelector, totalEdges}, edgeCountsGlobal; |
| 76 |
stk::all_reduce_sum(stk::parallel_machine_world(), edgeCountsLocal.data(), edgeCountsGlobal.data(), 2); |
| 77 |
|
| 78 |
return std::make_pair(edgeCountsGlobal[0], edgeCountsGlobal[1]); |
| 79 |
} |
| 80 |
|
| 81 |
void test_faces_have_edges(stk::mesh::BulkData& bulk, const std::vector<stk::mesh::Entity>& faces, |
| 82 |
int numExpectedEdgesPerFace) |
| 83 |
{ |
| 84 |
for (stk::mesh::Entity face : faces) |
| 85 |
{ |
| 86 |
stk::mesh::Entity const* beginEdges = bulk.begin_edges(face); |
| 87 |
stk::mesh::Entity const* endEdges = bulk.end_edges(face); |
| 88 |
ptrdiff_t numEdges = endEdges - beginEdges; |
| 89 |
|
| 90 |
EXPECT_EQ(numEdges, numExpectedEdgesPerFace); |
| 91 |
for (int i=0; i < numEdges; ++i) |
| 92 |
{ |
| 93 |
for (int j=0; j < numEdges; ++j) |
| 94 |
{ |
| 95 |
if (i != j) |
| 96 |
{ |
| 97 |
EXPECT_NE(beginEdges[i], beginEdges[j]); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void test_third_node_is_midpoint(stk::mesh::BulkData& bulk) |
| 105 |
{ |
| 106 |
std::vector<stk::mesh::Entity> edgeNodes; |
| 107 |
std::vector<std::array<double, 3>> edgeNodeCoords; |
| 108 |
const stk::mesh::FieldBase* coordField = bulk.mesh_meta_data().coordinate_field(); |
| 109 |
|
| 110 |
for (stk::mesh::Bucket* bucket : bulk.buckets(stk::topology::EDGE_RANK)) |
| 111 |
{ |
| 112 |
for (stk::mesh::Entity edge : *bucket) |
| 113 |
{ |
| 114 |
edgeNodes.clear(); |
| 115 |
for (stk::mesh::Entity const* node = bulk.begin_nodes(edge); node != bulk.end_nodes(edge); ++node) |
| 116 |
{ |
| 117 |
edgeNodes.push_back(*node); |
| 118 |
double* coordData = static_cast<double*>(stk::mesh::field_data(*coordField, *node)); |
| 119 |
edgeNodeCoords.emplace_back(std::array<double, 3>{coordData[0], coordData[1], coordData[2]}); |
| 120 |
} |
| 121 |
|
| 122 |
EXPECT_EQ(edgeNodes.size(), 3U); |
| 123 |
for (int i=0; i < 3; ++i) |
| 124 |
{ |
| 125 |
EXPECT_FLOAT_EQ(edgeNodeCoords[2][i], 0.5*(edgeNodeCoords[0][i] + edgeNodeCoords[1][i])); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshNoAura) |
| 135 |
{ |
| 136 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 137 |
{ |
| 138 |
GTEST_SKIP(); |
| 139 |
} |
| 140 |
|
| 141 |
setup("generated:3x3x3|sideset:x", false); |
| 142 |
bulk->modification_begin(); |
| 143 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 144 |
bulk->modification_end(); |
| 145 |
|
| 146 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 147 |
EXPECT_EQ(edgesInSelector, 24); |
| 148 |
EXPECT_EQ(edgesTotal, 24); |
| 149 |
test_faces_have_edges(*bulk, ownedFaces, 4); |
| 150 |
} |
| 151 |
|
| 152 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshWithAura) |
| 153 |
{ |
| 154 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 155 |
{ |
| 156 |
GTEST_SKIP(); |
| 157 |
} |
| 158 |
|
| 159 |
setup("generated:3x3x3|sideset:x", true); |
| 160 |
bulk->modification_begin(); |
| 161 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 162 |
bulk->modification_end(); |
| 163 |
|
| 164 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 165 |
EXPECT_EQ(edgesInSelector, 24); |
| 166 |
EXPECT_EQ(edgesTotal, 24); |
| 167 |
test_faces_have_edges(*bulk, ownedFaces, 4); |
| 168 |
} |
| 169 |
|
| 170 |
TEST_F(EdgesOnFacesFixture, Hex4x4MeshWithAuraAndCustomGhosting) |
| 171 |
{ |
| 172 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) != 2) |
| 173 |
{ |
| 174 |
GTEST_SKIP(); |
| 175 |
} |
| 176 |
|
| 177 |
setup("generated:4x4x4|sideset:x", false); // TODO: turn aura back on |
| 178 |
|
| 179 |
bulk->modification_begin(); |
| 180 |
stk::mesh::Ghosting& ghosting = bulk->create_ghosting("my_custom_ghosting"); |
| 181 |
bulk->modification_end(); |
| 182 |
|
| 183 |
bulk->modification_begin(); |
| 184 |
int otherProc = 1 - stk::parallel_machine_rank(stk::parallel_machine_world()); |
| 185 |
std::vector<stk::mesh::EntityProc> sendGhosts; |
| 186 |
for (stk::mesh::Entity face : ownedFaces) |
| 187 |
{ |
| 188 |
sendGhosts.emplace_back(face, otherProc); |
| 189 |
} |
| 190 |
bulk->change_ghosting(ghosting, sendGhosts); |
| 191 |
bulk->modification_end(); |
| 192 |
|
| 193 |
bulk->modification_begin(); |
| 194 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 195 |
bulk->modification_end(); |
| 196 |
|
| 197 |
// its unfortunate we have to do this, because stk does have enough information to update |
| 198 |
// the downward adjacencies of the ghost faces |
| 199 |
bulk->modification_begin(); |
| 200 |
sendGhosts.clear(); |
| 201 |
for (stk::mesh::Bucket* bucket : bulk->get_buckets(stk::topology::EDGE_RANK, *edgePart & bulk->mesh_meta_data().locally_owned_part())) |
| 202 |
{ |
| 203 |
for (stk::mesh::Entity edge : *bucket) |
| 204 |
{ |
| 205 |
sendGhosts.emplace_back(edge, otherProc); |
| 206 |
} |
| 207 |
} |
| 208 |
bulk->change_ghosting(ghosting, sendGhosts); |
| 209 |
bulk->modification_end(); |
| 210 |
|
| 211 |
|
| 212 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 213 |
EXPECT_EQ(edgesInSelector, 40); |
| 214 |
EXPECT_EQ(edgesTotal, 40); |
| 215 |
|
| 216 |
std::vector<stk::mesh::Entity> allFaces; |
| 217 |
stk::mesh::get_selected_entities(surfaceSelector, bulk->buckets(stk::topology::FACE_RANK), allFaces); |
| 218 |
EXPECT_EQ(allFaces.size(), 4*4U); |
| 219 |
test_faces_have_edges(*bulk, allFaces, 4); |
| 220 |
} |
| 221 |
|
| 222 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshEnableAuraLater) |
| 223 |
{ |
| 224 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 225 |
{ |
| 226 |
GTEST_SKIP(); |
| 227 |
} |
| 228 |
|
| 229 |
setup("generated:3x3x3|sideset:x", false); |
| 230 |
bulk->modification_begin(); |
| 231 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 232 |
bulk->modification_end(); |
| 233 |
|
| 234 |
bulk->set_automatic_aura_option(stk::mesh::BulkData::AUTO_AURA, true); |
| 235 |
|
| 236 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 237 |
EXPECT_EQ(edgesInSelector, 24); |
| 238 |
EXPECT_EQ(edgesTotal, 24); |
| 239 |
test_faces_have_edges(*bulk, ownedFaces, 4); |
| 240 |
} |
| 241 |
|
| 242 |
TEST_F(EdgesOnFacesFixture, Hex6x6MeshWithAura) |
| 243 |
{ |
| 244 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 6) |
| 245 |
{ |
| 246 |
GTEST_SKIP(); |
| 247 |
} |
| 248 |
|
| 249 |
setup("generated:6x6x6|sideset:x", true); |
| 250 |
bulk->modification_begin(); |
| 251 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 252 |
bulk->modification_end(); |
| 253 |
|
| 254 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 255 |
EXPECT_EQ(edgesInSelector, 6*7*2); |
| 256 |
EXPECT_EQ(edgesTotal, 6*7*2); |
| 257 |
test_faces_have_edges(*bulk, ownedFaces, 4); |
| 258 |
} |
| 259 |
|
| 260 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshProvidePart) |
| 261 |
{ |
| 262 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 263 |
{ |
| 264 |
GTEST_SKIP(); |
| 265 |
} |
| 266 |
|
| 267 |
setup("generated:3x3x3|sideset:x", true); |
| 268 |
stk::mesh::Part* edgePart = &(bulk->mesh_meta_data().declare_part_with_topology("face_edges_part", stk::topology::LINE_2)); |
| 269 |
|
| 270 |
bulk->modification_begin(); |
| 271 |
stk::mesh::Part* edgePart2 = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector, edgePart); |
| 272 |
bulk->modification_end(); |
| 273 |
EXPECT_EQ(edgePart, edgePart2); |
| 274 |
|
| 275 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 276 |
EXPECT_EQ(edgesInSelector, 24); |
| 277 |
EXPECT_EQ(edgesTotal, 24); |
| 278 |
test_faces_have_edges(*bulk, ownedFaces, 4); |
| 279 |
} |
| 280 |
|
| 281 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshProvideIncorrectPart) |
| 282 |
{ |
| 283 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 284 |
{ |
| 285 |
GTEST_SKIP(); |
| 286 |
} |
| 287 |
|
| 288 |
setup("generated:3x3x3|sideset:x", true); |
| 289 |
stk::mesh::Part* edgePart = &(bulk->mesh_meta_data().declare_part_with_topology("face_edges_part", stk::topology::QUAD_4)); |
| 290 |
|
| 291 |
EXPECT_ANY_THROW(stk::mesh::create_edges_on_faces(*bulk, surfaceSelector, edgePart)); |
| 292 |
} |
| 293 |
|
| 294 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshNoAuraDeleteEdges) |
| 295 |
{ |
| 296 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 297 |
{ |
| 298 |
GTEST_SKIP(); |
| 299 |
} |
| 300 |
|
| 301 |
setup("generated:3x3x3|sideset:x", false); |
| 302 |
bulk->modification_begin(); |
| 303 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 304 |
bulk->modification_end(); |
| 305 |
|
| 306 |
bulk->modification_begin(); |
| 307 |
stk::mesh::delete_edges_on_faces(*bulk, edgePart); |
| 308 |
bulk->modification_end(); |
| 309 |
|
| 310 |
|
| 311 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 312 |
EXPECT_EQ(edgesInSelector, 0); |
| 313 |
EXPECT_EQ(edgesTotal, 0); |
| 314 |
test_faces_have_edges(*bulk, ownedFaces, 0); |
| 315 |
} |
| 316 |
|
| 317 |
TEST_F(EdgesOnFacesFixture, Hex3x3MeshWithAuraDeleteEdges) |
| 318 |
{ |
| 319 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 320 |
{ |
| 321 |
GTEST_SKIP(); |
| 322 |
} |
| 323 |
|
| 324 |
setup("generated:3x3x3|sideset:x", true); |
| 325 |
bulk->modification_begin(); |
| 326 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 327 |
bulk->modification_end(); |
| 328 |
|
| 329 |
bulk->modification_begin(); |
| 330 |
stk::mesh::delete_edges_on_faces(*bulk, edgePart); |
| 331 |
bulk->modification_end(); |
| 332 |
|
| 333 |
|
| 334 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 335 |
EXPECT_EQ(edgesInSelector, 0); |
| 336 |
EXPECT_EQ(edgesTotal, 0); |
| 337 |
test_faces_have_edges(*bulk, ownedFaces, 0); |
| 338 |
} |
| 339 |
|
| 340 |
TEST_F(EdgesOnFacesFixture, Hex2x1HighOrder) |
| 341 |
{ |
| 342 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 2) |
| 343 |
{ |
| 344 |
GTEST_SKIP(); |
| 345 |
} |
| 346 |
std::string meshDesc = "textmesh:" |
| 347 |
"0,1,HEX_20,1,3,11,9, 20,22,30,28, 2,7,10,6, 14,15,18,17, 21,26,29,25\n" |
| 348 |
"0,2,HEX_20,3,5,13,11, 22,24,32,30, 4,8,12,7, 15,16,19,18, 23,27,31,26\n" |
| 349 |
// bottom plane |
| 350 |
"|coordinates: 0,0,0, 1,0,0, 2,0,0, 3,0,0, 4,0,0," |
| 351 |
"0,1,0, 2,1,0, 5,1,0," |
| 352 |
"0,2,0, 1,2,0, 2,2,0, 3,2,0, 4,2,0," |
| 353 |
// middle plane |
| 354 |
"0,0,1, 3,0,1, 5,0,1," |
| 355 |
"0,2,1, 3,2,1, 5,2,1," |
| 356 |
// top plane |
| 357 |
"0,0,1, 1,0,1, 2,0,1, 3,0,1, 4,0,1," |
| 358 |
"0,1,1, 2,1,1, 5,1,1," |
| 359 |
"0,2,1, 1,2,1, 2,2,1, 3,2,1, 4,2,1\n" |
| 360 |
"|sideset:name=surface_1; data=1,1, 2,1"; |
| 361 |
setup(meshDesc, true); |
| 362 |
|
| 363 |
bulk->modification_begin(); |
| 364 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, surfaceSelector); |
| 365 |
bulk->modification_end(); |
| 366 |
|
| 367 |
const auto& [edgesInSelector, edgesTotal] = countEdges(*bulk, *edgePart); |
| 368 |
EXPECT_EQ(edgesInSelector, 7); |
| 369 |
EXPECT_EQ(edgesTotal, 7); |
| 370 |
test_third_node_is_midpoint(*bulk); |
| 371 |
} |
| 372 |
|
| 373 |
TEST_F(EdgesOnFacesFixture, NoFacesSelected) |
| 374 |
{ |
| 375 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) > 3) |
| 376 |
{ |
| 377 |
GTEST_SKIP(); |
| 378 |
} |
| 379 |
|
| 380 |
setup("generated:3x3x3|sideset:x", false); |
| 381 |
bulk->modification_begin(); |
| 382 |
stk::mesh::Part* edgePart = stk::mesh::create_edges_on_faces(*bulk, stk::mesh::Selector()); |
| 383 |
bulk->modification_end(); |
| 384 |
EXPECT_EQ(edgePart, nullptr); |
| 385 |
} |