| 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 <stddef.h> // for size_t |
| 37 |
#include <algorithm> // for sort |
| 38 |
#include <stk_mesh/base/BoundaryAnalysis.hpp> // for EntitySideComponent, etc |
| 39 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 40 |
#include <stk_mesh/base/Entity.hpp> // for Entity |
| 41 |
#include <stk_mesh/base/FEMHelpers.hpp> // for get_entity_subcell_id, etc |
| 42 |
#include <stk_mesh/base/GetEntities.hpp> // for count_entities |
| 43 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 44 |
#include <stk_mesh/base/Selector.hpp> // for Selector |
| 45 |
#include <string> // for string |
| 46 |
#include <utility> // for pair, operator== |
| 47 |
#include <vector> // for vector, vector<>::iterator |
| 48 |
#include "mpi.h" // for MPI_COMM_WORLD, etc |
| 49 |
|
| 50 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH |
| 51 |
#include "stk_io/StkMeshIoBroker.hpp" // for StkMeshIoBroker |
| 52 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 53 |
#include "stk_mesh/base/EntityLess.hpp" // for EntityLess |
| 54 |
#include "stk_mesh/base/Types.hpp" // for EntityId, Ordinal, etc |
| 55 |
#include "stk_topology/topology.hpp" // for topology::num_faces |
| 56 |
#include "stk_unit_test_utils/stk_mesh_fixtures/GridFixture.hpp" // for GridFixture |
| 57 |
#include "stk_util/parallel/Parallel.hpp" // for parallel_machine_size, etc |
| 58 |
#include "stk_util/util/NamedPair.hpp" |
| 59 |
namespace stk { namespace mesh { class Part; } } |
| 60 |
|
| 61 |
static const stk::mesh::EntityRank NODE_RANK = stk::topology::NODE_RANK; |
| 62 |
|
| 63 |
using stk::mesh::MetaData; |
| 64 |
|
| 65 |
class UnitTestStkMeshBoundaryAnalysis { |
| 66 |
public: |
| 67 |
UnitTestStkMeshBoundaryAnalysis(stk::ParallelMachine pm) : m_comm(pm), m_num_procs(0), m_rank(0) |
| 68 |
{ |
| 69 |
m_num_procs = stk::parallel_machine_size( m_comm ); |
| 70 |
m_rank = stk::parallel_machine_rank( m_comm ); |
| 71 |
} |
| 72 |
|
| 73 |
void test_boundary_analysis(); |
| 74 |
|
| 75 |
stk::ParallelMachine m_comm; |
| 76 |
int m_num_procs; |
| 77 |
int m_rank; |
| 78 |
}; |
| 79 |
|
| 80 |
namespace { |
| 81 |
|
| 82 |
TEST( UnitTestStkMeshBoundaryAnalysis , testUnit ) |
| 83 |
{ |
| 84 |
UnitTestStkMeshBoundaryAnalysis unit(MPI_COMM_WORLD); |
| 85 |
unit.test_boundary_analysis(); |
| 86 |
} |
| 87 |
|
| 88 |
} //end namespace |
| 89 |
|
| 90 |
void UnitTestStkMeshBoundaryAnalysis::test_boundary_analysis() |
| 91 |
{ |
| 92 |
// Test the boundary_analysis algorithm in stk_mesh/base/BoundaryAnalysis.hpp |
| 93 |
// with a boundary that is both shelled and non-shelled. |
| 94 |
// |
| 95 |
// We will be testing the algorithm on the following 2D mesh: |
| 96 |
// |
| 97 |
// 17---18---19---20---21 |
| 98 |
// | 1 | 2 | 3 || 4 | |
| 99 |
// 22---23---24---25---26 |
| 100 |
// | 5 | 6 | 7 || 8 | |
| 101 |
// 27---28---29---30---31 |
| 102 |
// | 9 | 10 | 11 ||12 | |
| 103 |
// 32---33---34---35---36 |
| 104 |
// | 13 | 14 | 15 ||16 | |
| 105 |
// 37---38---39---40---41 |
| 106 |
// |
| 107 |
// Note the shells along nodes 20-40. |
| 108 |
// |
| 109 |
// We will be computing the boundary of the closure of |
| 110 |
// elements: 6, 7, 10, 11, 14, 15 |
| 111 |
|
| 112 |
// This test will only work for np=1 |
| 113 |
if (m_num_procs > 1) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// set up grid_mesh |
| 118 |
stk::mesh::fixtures::GridFixture grid_mesh(MPI_COMM_WORLD); |
| 119 |
|
| 120 |
stk::mesh::MetaData& fem_meta = grid_mesh.fem_meta(); |
| 121 |
stk::mesh::BulkData& bulk_data = grid_mesh.bulk_data(); |
| 122 |
|
| 123 |
const stk::mesh::EntityRank element_rank = stk::topology::ELEMENT_RANK; |
| 124 |
|
| 125 |
// make shell part |
| 126 |
stk::mesh::Part& shell_part = fem_meta.declare_part_with_topology("shell_part", stk::topology::SHELL_LINE_2); |
| 127 |
|
| 128 |
fem_meta.commit(); |
| 129 |
|
| 130 |
bulk_data.modification_begin(); |
| 131 |
grid_mesh.generate_grid(); |
| 132 |
|
| 133 |
// Add some shells |
| 134 |
const unsigned num_shells = 4; |
| 135 |
|
| 136 |
// get a count of entities that have already been created |
| 137 |
std::vector<size_t> count; |
| 138 |
stk::mesh::Selector locally_owned(fem_meta.locally_owned_part()); |
| 139 |
stk::mesh::count_entities(locally_owned, bulk_data, count); |
| 140 |
const unsigned num_entities = count[NODE_RANK] + count[element_rank]; |
| 141 |
|
| 142 |
// Declare the shell entities, placing them in the shell part |
| 143 |
std::vector<stk::mesh::Entity> shells; |
| 144 |
stk::mesh::PartVector shell_parts; |
| 145 |
shell_parts.push_back(&shell_part); |
| 146 |
for (unsigned i = 1; i <= num_shells; ++i) { |
| 147 |
stk::mesh::Entity new_shell = bulk_data.declare_element(num_entities + i, shell_parts); |
| 148 |
shells.push_back(new_shell); |
| 149 |
} |
| 150 |
|
| 151 |
// declare shell relationships |
| 152 |
unsigned node_list[5] = {20, 25, 30, 35, 40}; |
| 153 |
for (unsigned i = 0; i < num_shells; ++i) { |
| 154 |
stk::mesh::Entity shell = shells[i]; |
| 155 |
stk::mesh::Entity node1 = bulk_data.get_entity(NODE_RANK, node_list[i]); |
| 156 |
stk::mesh::Entity node2 = bulk_data.get_entity(NODE_RANK, node_list[i+1]); |
| 157 |
bulk_data.declare_relation(shell, node1, 0); |
| 158 |
bulk_data.declare_relation(shell, node2, 1); |
| 159 |
} |
| 160 |
|
| 161 |
bulk_data.modification_end(); |
| 162 |
|
| 163 |
// create the closure we want to analyze |
| 164 |
std::vector<stk::mesh::Entity> closure; |
| 165 |
unsigned num_elems_in_closure = 6; |
| 166 |
stk::mesh::EntityId ids_of_entities_in_closure[] = |
| 167 |
{6, 7, 10, 11, 14, 15, 23, 24, 25, 28, 29, 30, 33, 34, 35, 38, 39, 40}; |
| 168 |
for (unsigned i = 0; |
| 169 |
i < sizeof(ids_of_entities_in_closure)/sizeof(stk::mesh::EntityId); |
| 170 |
++i) { |
| 171 |
stk::mesh::EntityRank rank_of_entity=stk::topology::NODE_RANK; |
| 172 |
if (i < num_elems_in_closure) { |
| 173 |
rank_of_entity = element_rank; |
| 174 |
} |
| 175 |
else { |
| 176 |
rank_of_entity = NODE_RANK; |
| 177 |
} |
| 178 |
stk::mesh::Entity closure_entity = |
| 179 |
bulk_data.get_entity(rank_of_entity, ids_of_entities_in_closure[i]); |
| 180 |
closure.push_back(closure_entity); |
| 181 |
} |
| 182 |
// sort the closure (boundary analysis expects it this way) |
| 183 |
std::sort(closure.begin(), closure.end(), stk::mesh::EntityLess(bulk_data)); |
| 184 |
|
| 185 |
// Run the boundary analysis! |
| 186 |
stk::mesh::EntitySideVector boundary; |
| 187 |
stk::mesh::boundary_analysis(bulk_data, closure, element_rank, boundary); |
| 188 |
EXPECT_TRUE(!boundary.empty()); |
| 189 |
|
| 190 |
// Prepare the expected-results as a vector of pairs of pairs representing |
| 191 |
// ( inside, outside ) where |
| 192 |
// inside is ( element-id, side-ordinal ) of element inside the closure |
| 193 |
// outside is ( element-id, side-ordinal ) of element outside the closure |
| 194 |
// and inside and outside border each other |
| 195 |
|
| 196 |
typedef std::pair<stk::mesh::EntityId, stk::mesh::Ordinal> BoundaryItem; |
| 197 |
typedef std::pair<BoundaryItem, BoundaryItem> BoundaryPair; |
| 198 |
|
| 199 |
// Note that certain sides of elements 7, 11, 15 have a boundary with |
| 200 |
// a shell AND the adjacent element outside the closure. |
| 201 |
|
| 202 |
BoundaryPair results[] = { |
| 203 |
BoundaryPair(BoundaryItem(6, 0), BoundaryItem(5, 2)), |
| 204 |
|
| 205 |
BoundaryPair(BoundaryItem(6, 3), BoundaryItem(2, 1)), |
| 206 |
|
| 207 |
BoundaryPair(BoundaryItem(7, 2), BoundaryItem(8, 0)), |
| 208 |
BoundaryPair(BoundaryItem(7, 2), BoundaryItem(43, 0)), |
| 209 |
|
| 210 |
BoundaryPair(BoundaryItem(7, 3), BoundaryItem(3, 1)), |
| 211 |
|
| 212 |
BoundaryPair(BoundaryItem(10, 0), BoundaryItem(9, 2)), |
| 213 |
|
| 214 |
BoundaryPair(BoundaryItem(11, 2), BoundaryItem(12, 0)), |
| 215 |
BoundaryPair(BoundaryItem(11, 2), BoundaryItem(44, 0)), |
| 216 |
|
| 217 |
BoundaryPair(BoundaryItem(14, 0), BoundaryItem(13, 2)), |
| 218 |
|
| 219 |
BoundaryPair(BoundaryItem(14, 1), BoundaryItem(0, 0)), |
| 220 |
|
| 221 |
BoundaryPair(BoundaryItem(15, 1), BoundaryItem(0, 0)), |
| 222 |
|
| 223 |
BoundaryPair(BoundaryItem(15, 2), BoundaryItem(16, 0)), |
| 224 |
BoundaryPair(BoundaryItem(15, 2), BoundaryItem(45, 0)) |
| 225 |
}; |
| 226 |
|
| 227 |
// Convert the boundary returned by boundary_analysis into a data-structure |
| 228 |
// comparable to expected_results |
| 229 |
|
| 230 |
BoundaryPair expected_results[sizeof(results)/sizeof(BoundaryPair)]; |
| 231 |
|
| 232 |
unsigned i = 0; |
| 233 |
stk::mesh::EntitySideVector::iterator itr = boundary.begin(); |
| 234 |
|
| 235 |
for (; itr != boundary.end(); ++itr, ++i) |
| 236 |
{ |
| 237 |
stk::mesh::EntitySide& side = *itr; |
| 238 |
stk::mesh::EntitySideComponent& inside_closure = side.inside; |
| 239 |
stk::mesh::EntityId inside_id = |
| 240 |
bulk_data.is_valid(inside_closure.entity) ? bulk_data.identifier(inside_closure.entity) : 0; |
| 241 |
stk::mesh::EntityId inside_side = |
| 242 |
bulk_data.is_valid(inside_closure.entity) ? inside_closure.side_ordinal : 0; |
| 243 |
stk::mesh::EntitySideComponent& outside_closure = side.outside; |
| 244 |
stk::mesh::EntityId outside_id = |
| 245 |
bulk_data.is_valid(outside_closure.entity) ? bulk_data.identifier(outside_closure.entity) : 0; |
| 246 |
stk::mesh::EntityId outside_side = |
| 247 |
bulk_data.is_valid(outside_closure.entity) ? outside_closure.side_ordinal : 0; |
| 248 |
|
| 249 |
expected_results[i] = BoundaryPair(BoundaryItem(inside_id, inside_side), |
| 250 |
BoundaryItem(outside_id, outside_side)); |
| 251 |
} |
| 252 |
|
| 253 |
// Check that results match expected results |
| 254 |
|
| 255 |
EXPECT_EQ(sizeof(results), sizeof(expected_results)); |
| 256 |
|
| 257 |
for (i = 0; i < sizeof(results)/sizeof(BoundaryPair); ++i) { |
| 258 |
EXPECT_TRUE(results[i] == expected_results[i]); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
TEST(BoundaryAnalysis, get_adjacent_entities) |
| 263 |
{ |
| 264 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 265 |
int numProcs = stk::parallel_machine_size(comm); |
| 266 |
if (numProcs > 1) |
| 267 |
{ |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
stk::io::StkMeshIoBroker meshReader(comm); |
| 272 |
std::string mesh_spec("generated:3x3x3"); |
| 273 |
meshReader.add_mesh_database(mesh_spec, stk::io::READ_MESH); |
| 274 |
meshReader.create_input_mesh(); |
| 275 |
meshReader.populate_bulk_data(); |
| 276 |
|
| 277 |
stk::mesh::BulkData& stkMeshBulkData = meshReader.bulk_data(); |
| 278 |
|
| 279 |
unsigned numEntitiesToTest = 4; |
| 280 |
stk::mesh::EntityId ids[] = { 14, 1, 2, 5 }; |
| 281 |
unsigned goldNumConnectedEntities[] = { 6, 3, 4, 5 }; |
| 282 |
stk::mesh::EntityId nodeIdsForFace[][4] = { |
| 283 |
{22, 23, 39, 38}, |
| 284 |
{23, 27, 43, 39}, |
| 285 |
{27, 26, 42, 43}, |
| 286 |
{22, 38, 42, 26}, |
| 287 |
{22, 26, 27, 23}, |
| 288 |
{38, 39, 43, 42}, |
| 289 |
{1 , 2 , 18, 17}, |
| 290 |
{2 , 6 , 22, 18}, |
| 291 |
{6 , 5 , 21, 22}, |
| 292 |
{1 , 17, 21, 5 }, |
| 293 |
{1 , 5 , 6 , 2 }, |
| 294 |
{17, 18, 22, 21}, |
| 295 |
{2 , 3 , 19, 18}, |
| 296 |
{3 , 7 , 23, 19}, |
| 297 |
{7 , 6 , 22, 23}, |
| 298 |
{2 , 18, 22, 6 }, |
| 299 |
{2 , 6 , 7 , 3 }, |
| 300 |
{18, 19, 23, 22}, |
| 301 |
{6 , 7 , 23, 22}, |
| 302 |
{7 , 11, 27, 23}, |
| 303 |
{11, 10, 26, 27}, |
| 304 |
{6 , 22, 26, 10}, |
| 305 |
{6 , 10, 11, 7 }, |
| 306 |
{22, 23, 27, 26} |
| 307 |
}; |
| 308 |
|
| 309 |
unsigned testCounter=0; |
| 310 |
for (unsigned int i=0;i<numEntitiesToTest;i++) |
| 311 |
{ |
| 312 |
stk::mesh::Entity element = stkMeshBulkData.get_entity(stk::topology::ELEM_RANK, ids[i]); |
| 313 |
|
| 314 |
std::vector<stk::mesh::EntitySideComponent> adjacent_entities; |
| 315 |
stk::topology elemTopology = stkMeshBulkData.bucket(element).topology(); |
| 316 |
|
| 317 |
unsigned numConnectedEntities = 0; |
| 318 |
for(unsigned faceIndex = 0; faceIndex < elemTopology.num_faces(); ++faceIndex) |
| 319 |
{ |
| 320 |
stk::mesh::get_adjacent_entities(stkMeshBulkData, |
| 321 |
element, |
| 322 |
stk::topology::FACE_RANK, |
| 323 |
faceIndex, |
| 324 |
adjacent_entities); |
| 325 |
|
| 326 |
numConnectedEntities += adjacent_entities.size(); |
| 327 |
stk::mesh::EntityVector subcell_nodes; |
| 328 |
stk::topology stksubcell_topo = stk::mesh::get_subcell_nodes(stkMeshBulkData, |
| 329 |
element, |
| 330 |
stk::topology::FACE_RANK, |
| 331 |
faceIndex, |
| 332 |
subcell_nodes); |
| 333 |
EXPECT_FALSE(stksubcell_topo == stk::topology::INVALID_TOPOLOGY); |
| 334 |
|
| 335 |
for(size_t j = 0; j < subcell_nodes.size(); j++) |
| 336 |
{ |
| 337 |
EXPECT_EQ(nodeIdsForFace[testCounter][j], stkMeshBulkData.identifier(subcell_nodes[j])); |
| 338 |
} |
| 339 |
|
| 340 |
size_t local_subcell_num = stk::mesh::get_entity_subcell_id(stkMeshBulkData, |
| 341 |
element, |
| 342 |
stk::topology::FACE_RANK, |
| 343 |
stksubcell_topo, |
| 344 |
subcell_nodes); |
| 345 |
EXPECT_EQ(faceIndex, local_subcell_num); |
| 346 |
testCounter++; |
| 347 |
} |
| 348 |
EXPECT_EQ(goldNumConnectedEntities[i], numConnectedEntities); |
| 349 |
} |
| 350 |
} |