| 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 |
|
| 36 |
#include <stk_mesh/base/MetaData.hpp> |
| 37 |
#include <stk_mesh/base/BulkData.hpp> |
| 38 |
#include <stk_mesh/base/EntityLess.hpp> |
| 39 |
#include <stk_mesh/base/EntityProcMapping.hpp> |
| 40 |
#include <stk_io/FillMesh.hpp> |
| 41 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 42 |
|
| 43 |
#include <vector> |
| 44 |
#include <set> |
| 45 |
|
| 46 |
using stk::unit_test_util::build_mesh; |
| 47 |
|
| 48 |
TEST(EntityProcMapping, basic) |
| 49 |
{ |
| 50 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 3) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
const unsigned spatialDim = 3; |
| 55 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 56 |
stk::mesh::BulkData& bulk = *bulkPtr; |
| 57 |
stk::io::fill_mesh("generated:1x2x3",bulk); |
| 58 |
|
| 59 |
if (stk::parallel_machine_rank(MPI_COMM_WORLD) != 0) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
stk::mesh::EntityProcMapping entProcMapping(bulk.get_size_of_entity_index_space()); |
| 64 |
stk::mesh::Entity elem1 = bulk.get_entity(stk::topology::ELEM_RANK, 1); |
| 65 |
int proc = 0; |
| 66 |
entProcMapping.addEntityProc(elem1, proc); |
| 67 |
proc = 1; |
| 68 |
entProcMapping.addEntityProc(elem1, proc); |
| 69 |
proc = 0; |
| 70 |
entProcMapping.addEntityProc(elem1, proc); |
| 71 |
|
| 72 |
stk::mesh::Entity elem2 = bulk.get_entity(stk::topology::ELEM_RANK, 2); |
| 73 |
proc = 0; |
| 74 |
entProcMapping.addEntityProc(elem2, proc); |
| 75 |
proc = 1; |
| 76 |
entProcMapping.addEntityProc(elem2, proc); |
| 77 |
proc = 0; |
| 78 |
entProcMapping.addEntityProc(elem2, proc); |
| 79 |
|
| 80 |
EXPECT_EQ(2u, entProcMapping.get_num_entities()); |
| 81 |
EXPECT_EQ(2u, entProcMapping.get_num_procs(elem1)); |
| 82 |
EXPECT_EQ(2u, entProcMapping.get_num_procs(elem2)); |
| 83 |
|
| 84 |
EXPECT_TRUE(entProcMapping.find(stk::mesh::EntityProc(elem1, proc))); |
| 85 |
proc = 5; |
| 86 |
EXPECT_FALSE(entProcMapping.find(stk::mesh::EntityProc(elem1, proc))); |
| 87 |
|
| 88 |
stk::mesh::EntityLess entLess(bulk); |
| 89 |
std::set<stk::mesh::EntityProc, stk::mesh::EntityLess> entityProcSet(entLess); |
| 90 |
entProcMapping.fill_set(entityProcSet); |
| 91 |
EXPECT_EQ(4u, entityProcSet.size()); |
| 92 |
|
| 93 |
std::vector<stk::mesh::EntityProc> entityProcVec; |
| 94 |
entProcMapping.fill_vec(entityProcVec); |
| 95 |
EXPECT_EQ(4u, entityProcVec.size()); |
| 96 |
|
| 97 |
proc = 1; |
| 98 |
entProcMapping.eraseEntityProc(elem1, proc); |
| 99 |
|
| 100 |
entProcMapping.fill_set(entityProcSet); |
| 101 |
EXPECT_EQ(3u, entityProcSet.size()); |
| 102 |
|
| 103 |
entProcMapping.fill_vec(entityProcVec); |
| 104 |
EXPECT_EQ(3u, entityProcVec.size()); |
| 105 |
} |
| 106 |
|
| 107 |
void test_add_two_remove_one_then_other_still_found(stk::mesh::EntityProcMapping& mapping, stk::mesh::Entity entity) |
| 108 |
{ |
| 109 |
mapping.addEntityProc(entity, 0); |
| 110 |
mapping.addEntityProc(entity, 2); |
| 111 |
EXPECT_TRUE(mapping.find(entity,0)); |
| 112 |
EXPECT_TRUE(mapping.find(entity,2)); |
| 113 |
|
| 114 |
mapping.eraseEntityProc(entity,2); |
| 115 |
EXPECT_TRUE(mapping.find(entity,0)); |
| 116 |
EXPECT_FALSE(mapping.find(entity,2)); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(EntityProcMapping, add_two_remove_one_then_other_still_found) |
| 120 |
{ |
| 121 |
stk::mesh::Entity entity(1); |
| 122 |
const unsigned arbitraryMaxNumEntities = 10; |
| 123 |
stk::mesh::EntityProcMapping mapping(arbitraryMaxNumEntities); |
| 124 |
test_add_two_remove_one_then_other_still_found(mapping, entity); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(EntityProcMapping, add_two_remove_one_then_other_still_found_with_reset) |
| 128 |
{ |
| 129 |
stk::mesh::Entity entity(1); |
| 130 |
const unsigned arbitraryMaxNumEntities = 10; |
| 131 |
stk::mesh::EntityProcMapping mapping(arbitraryMaxNumEntities); |
| 132 |
test_add_two_remove_one_then_other_still_found(mapping, entity); |
| 133 |
|
| 134 |
const unsigned largerMaxNumEntities = 128; |
| 135 |
mapping.reset(largerMaxNumEntities); |
| 136 |
EXPECT_FALSE(mapping.find(entity,0)); |
| 137 |
EXPECT_FALSE(mapping.find(entity,2)); |
| 138 |
EXPECT_FALSE(mapping.find(entity)); |
| 139 |
|
| 140 |
test_add_two_remove_one_then_other_still_found(mapping, entity); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(EntityProcMapping, erase_nonexisting_then_previous_proc_still_found) |
| 144 |
{ |
| 145 |
stk::mesh::Entity entity(1); |
| 146 |
const unsigned arbitraryMaxNumEntities = 10; |
| 147 |
stk::mesh::EntityProcMapping mapping(arbitraryMaxNumEntities); |
| 148 |
mapping.addEntityProc(entity, 0); |
| 149 |
EXPECT_TRUE(mapping.find(entity,0)); |
| 150 |
|
| 151 |
mapping.eraseEntityProc(entity,2); |
| 152 |
EXPECT_TRUE(mapping.find(entity,0)); |
| 153 |
} |
| 154 |
|
| 155 |
TEST(EntityProcMapping, visitEntityProcs) |
| 156 |
{ |
| 157 |
stk::mesh::Entity entity1(1), entity2(2); |
| 158 |
const unsigned arbitraryMaxNumEntities = 10; |
| 159 |
stk::mesh::EntityProcMapping mapping(arbitraryMaxNumEntities); |
| 160 |
mapping.addEntityProc(entity1, 2); |
| 161 |
mapping.addEntityProc(entity2, 1); |
| 162 |
mapping.addEntityProc(entity2, 3); |
| 163 |
|
| 164 |
std::vector<stk::mesh::EntityProc> gold = {stk::mesh::EntityProc(entity1,2),stk::mesh::EntityProc(entity2,1),stk::mesh::EntityProc(entity2,3)}; |
| 165 |
std::vector<stk::mesh::EntityProc> entityProcs; |
| 166 |
mapping.visit_entity_procs([&](stk::mesh::Entity entity, int proc){entityProcs.push_back(stk::mesh::EntityProc(entity,proc));}); |
| 167 |
EXPECT_EQ(gold, entityProcs); |
| 168 |
} |
| 169 |
|