| 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" |
| 36 |
|
| 37 |
#include "util/parallel_search_test_util.hpp" |
| 38 |
#include "stk_middle_mesh/mesh_scatter_spec.hpp" |
| 39 |
#include "stk_middle_mesh/bounding_box_search.hpp" |
| 40 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 41 |
|
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
TEST(SplitCommTest, one_to_one) |
| 46 |
{ |
| 47 |
int nProcsSendMesh = 1, nProcsRecvMesh = 1; |
| 48 |
SplitCommTestUtil sctu(nProcsSendMesh, nProcsRecvMesh); |
| 49 |
|
| 50 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 51 |
|
| 52 |
EXPECT_EQ(0, sctu.get_local_comm_rank(0)); |
| 53 |
EXPECT_EQ(0, sctu.get_local_comm_rank(1)); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(SplitCommTest, one_to_two) |
| 57 |
{ |
| 58 |
int nProcsSendMesh = 1, nProcsRecvMesh = 2; |
| 59 |
SplitCommTestUtil sctu(nProcsSendMesh, nProcsRecvMesh); |
| 60 |
|
| 61 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 62 |
|
| 63 |
EXPECT_EQ(0, sctu.get_local_comm_rank(0)); |
| 64 |
EXPECT_EQ(0, sctu.get_local_comm_rank(1)); |
| 65 |
EXPECT_EQ(1, sctu.get_local_comm_rank(2)); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(SplitCommTest, two_to_one) |
| 69 |
{ |
| 70 |
int nProcsSendMesh = 2, nProcsRecvMesh = 1; |
| 71 |
SplitCommTestUtil sctu(nProcsSendMesh, nProcsRecvMesh); |
| 72 |
|
| 73 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 74 |
|
| 75 |
EXPECT_EQ(0, sctu.get_local_comm_rank(0)); |
| 76 |
EXPECT_EQ(1, sctu.get_local_comm_rank(1)); |
| 77 |
EXPECT_EQ(0, sctu.get_local_comm_rank(2)); |
| 78 |
} |
| 79 |
|
| 80 |
TEST(SearchBoundingBox, CreateBoundingBox) |
| 81 |
{ |
| 82 |
int commSize; |
| 83 |
MPI_Comm_size(MPI_COMM_WORLD, &commSize); |
| 84 |
if(commSize > 1) { GTEST_SKIP(); } |
| 85 |
|
| 86 |
stk::middle_mesh::mesh::impl::MeshSpec spec; |
| 87 |
spec.numelX = 1; |
| 88 |
spec.numelY = 1; |
| 89 |
spec.xmin = 0; |
| 90 |
spec.xmax = 1; |
| 91 |
spec.ymin = 0; |
| 92 |
spec.ymax = 1; |
| 93 |
|
| 94 |
auto func = [&](stk::middle_mesh::utils::Point const& pt) { return stk::middle_mesh::utils::Point(pt.x, pt.y, 0); }; |
| 95 |
auto mesh = stk::middle_mesh::mesh::impl::create_mesh(spec, func, MPI_COMM_WORLD, false); |
| 96 |
|
| 97 |
auto vertices = mesh->get_vertices(); |
| 98 |
EXPECT_EQ(4u, vertices.size()); |
| 99 |
|
| 100 |
auto elements = mesh->get_elements(); |
| 101 |
EXPECT_EQ(1u, elements.size()); |
| 102 |
|
| 103 |
std::vector<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::BoundingBox> boundingBoxVec; |
| 104 |
stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox meshA(mesh, MPI_COMM_WORLD); |
| 105 |
meshA.fill_bounding_boxes(boundingBoxVec); |
| 106 |
|
| 107 |
EXPECT_EQ(1u, boundingBoxVec.size()); |
| 108 |
auto box = boundingBoxVec[0]; |
| 109 |
EXPECT_NEAR(spec.xmin, box.first.get_x_min(), 1e-5); |
| 110 |
EXPECT_NEAR(spec.ymin, box.first.get_y_min(), 1e-5); |
| 111 |
EXPECT_NEAR(spec.xmax, box.first.get_x_max(), 1e-5); |
| 112 |
EXPECT_NEAR(spec.ymax, box.first.get_y_max(), 1e-5); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
class ParallelSearch : public ::testing::Test { |
| 117 |
protected: |
| 118 |
using BoundingBoxSearch = stk::middle_mesh::search::ElementToElementBoundingBoxSearch; |
| 119 |
ParallelSearch() {} |
| 120 |
|
| 121 |
void setup_comm() |
| 122 |
{ |
| 123 |
m_splitCommUtil = std::make_shared<SplitCommTestUtil>(m_numProcsSendMesh, m_numProcsRecvMesh); |
| 124 |
|
| 125 |
m_color = m_splitCommUtil->get_color(); |
| 126 |
m_splitComm = m_splitCommUtil->get_comm(); |
| 127 |
} |
| 128 |
|
| 129 |
void setup_mesh(unsigned sendNumElemInX, unsigned sendNumElemInY, |
| 130 |
unsigned recvNumElemInX, unsigned recvNumElemInY) |
| 131 |
{ |
| 132 |
m_sendMeshSpec.numelX = sendNumElemInX; |
| 133 |
m_sendMeshSpec.numelY = sendNumElemInY; |
| 134 |
m_sendMeshSpec.xmin = 0; |
| 135 |
m_sendMeshSpec.xmax = 1; |
| 136 |
m_sendMeshSpec.ymin = 0; |
| 137 |
m_sendMeshSpec.ymax = 1; |
| 138 |
|
| 139 |
m_recvMeshSpec.numelX = recvNumElemInX; |
| 140 |
m_recvMeshSpec.numelY = recvNumElemInY; |
| 141 |
m_recvMeshSpec.xmin = 0; |
| 142 |
m_recvMeshSpec.xmax = 1; |
| 143 |
m_recvMeshSpec.ymin = 0; |
| 144 |
m_recvMeshSpec.ymax = 1; |
| 145 |
|
| 146 |
auto func = [&](const stk::middle_mesh::utils::Point& pt) { return stk::middle_mesh::utils::Point(pt.x, pt.y, 0); }; |
| 147 |
|
| 148 |
if (m_color == stk::middle_mesh::search::SplitCommColor::SEND) { |
| 149 |
m_mesh = stk::middle_mesh::mesh::impl::create_mesh(m_sendMeshSpec, func, m_splitComm, false); |
| 150 |
} else if (m_color == stk::middle_mesh::search::SplitCommColor::RECV) { |
| 151 |
m_mesh = stk::middle_mesh::mesh::impl::create_mesh(m_recvMeshSpec, func, m_splitComm, false); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void setup_search() |
| 156 |
{ |
| 157 |
if (m_color == stk::middle_mesh::search::SplitCommColor::SEND) { |
| 158 |
m_searchSendMesh = std::make_shared<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox>(m_mesh, MPI_COMM_WORLD); |
| 159 |
m_search = std::make_shared<stk::middle_mesh::search::ElementToElementBoundingBoxSearch>( |
| 160 |
m_searchSendMesh, m_searchRecvMesh, "BoundingBoxSearch", MPI_COMM_WORLD); |
| 161 |
} else if (m_color == stk::middle_mesh::search::SplitCommColor::RECV) { |
| 162 |
m_searchRecvMesh = std::make_shared<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox>(m_mesh, MPI_COMM_WORLD); |
| 163 |
m_search = std::make_shared<stk::middle_mesh::search::ElementToElementBoundingBoxSearch>( |
| 164 |
m_searchSendMesh, m_searchRecvMesh, "BoundingBoxSearch", MPI_COMM_WORLD); |
| 165 |
} else { |
| 166 |
STK_ThrowRequireMsg(false, "Invalid SplitComm color"); |
| 167 |
} |
| 168 |
|
| 169 |
m_search->coarse_search(); |
| 170 |
} |
| 171 |
|
| 172 |
void setup(int numProcsSendMesh, int numProcsRecvMesh, |
| 173 |
unsigned sendNumElemInX, unsigned sendNumElemInY, |
| 174 |
unsigned recvNumElemInX, unsigned recvNumElemInY) |
| 175 |
{ |
| 176 |
m_numProcsSendMesh = numProcsSendMesh; |
| 177 |
m_numProcsRecvMesh = numProcsRecvMesh; |
| 178 |
|
| 179 |
setup_comm(); |
| 180 |
setup_mesh(sendNumElemInX, sendNumElemInY, recvNumElemInX, recvNumElemInY); |
| 181 |
setup_search(); |
| 182 |
} |
| 183 |
|
| 184 |
void test_search_result(const unsigned expectedNumSearchPairs) |
| 185 |
{ |
| 186 |
int myRankWorld; |
| 187 |
MPI_Comm_rank(MPI_COMM_WORLD, &myRankWorld); |
| 188 |
|
| 189 |
const BoundingBoxSearch::EntityProcRelationVec& pairedEntities = m_search->get_range_to_domain(); |
| 190 |
const std::vector<BoundingBoxSearch::BoundingBoxB>& unpairedEntities = m_search->get_unpaired_recv_entities(); |
| 191 |
|
| 192 |
if(m_color == stk::middle_mesh::search::SplitCommColor::SEND) { |
| 193 |
EXPECT_EQ(expectedNumSearchPairs, pairedEntities.size()); |
| 194 |
EXPECT_EQ(0u, unpairedEntities.size()); |
| 195 |
} |
| 196 |
|
| 197 |
unsigned expectedGlobalRank = 0; |
| 198 |
|
| 199 |
for(const auto &pairedEntity : pairedEntities) { |
| 200 |
const stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::EntityProc& recvEntityProc = pairedEntity.first; |
| 201 |
const stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::EntityProc& sendEntityProc = pairedEntity.second; |
| 202 |
|
| 203 |
if(m_color == stk::middle_mesh::search::SplitCommColor::RECV) { |
| 204 |
bool isGhost = myRankWorld != (int)recvEntityProc.proc(); |
| 205 |
|
| 206 |
if(!isGhost) { |
| 207 |
EXPECT_EQ(0, recvEntityProc.id()); |
| 208 |
EXPECT_EQ(0, sendEntityProc.id()); |
| 209 |
EXPECT_EQ(expectedGlobalRank, sendEntityProc.proc()); |
| 210 |
|
| 211 |
expectedGlobalRank++; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void fill_mesh_scatter(stk::middle_mesh::mesh::impl::MeshScatterSpec& scatter) |
| 218 |
{ |
| 219 |
const BoundingBoxSearch::EntityProcRelationVec& pairedEntities = m_search->get_range_to_domain(); |
| 220 |
const std::vector<BoundingBoxSearch::BoundingBoxB>& unpairedEntities = m_search->get_unpaired_recv_entities(); |
| 221 |
EXPECT_EQ(0u, unpairedEntities.size()); |
| 222 |
|
| 223 |
if(m_color == stk::middle_mesh::search::SplitCommColor::SEND) { |
| 224 |
auto elements = m_search->send_mesh()->get_mesh()->get_elements(); |
| 225 |
|
| 226 |
for(const auto &pairedEntity : pairedEntities) { |
| 227 |
const stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::EntityProc& recvEntityProc = pairedEntity.first; |
| 228 |
const stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::EntityProc& sendEntityProc = pairedEntity.second; |
| 229 |
|
| 230 |
int globalRank = recvEntityProc.proc(); |
| 231 |
int splitRank = m_splitCommUtil->get_local_comm_rank(globalRank); |
| 232 |
|
| 233 |
int entityId = sendEntityProc.id(); |
| 234 |
|
| 235 |
stk::middle_mesh::mesh::MeshEntityPtr entity = elements[entityId]; |
| 236 |
|
| 237 |
scatter.add_destination(entity, splitRank); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
int m_numProcsSendMesh{0}; |
| 243 |
int m_numProcsRecvMesh{0}; |
| 244 |
stk::middle_mesh::search::SplitCommColor m_color{stk::middle_mesh::search::SplitCommColor::INVALID}; |
| 245 |
|
| 246 |
MPI_Comm m_splitComm{MPI_COMM_NULL}; |
| 247 |
|
| 248 |
stk::middle_mesh::mesh::impl::MeshSpec m_sendMeshSpec; |
| 249 |
stk::middle_mesh::mesh::impl::MeshSpec m_recvMeshSpec; |
| 250 |
|
| 251 |
std::shared_ptr<stk::middle_mesh::search::ElementToElementBoundingBoxSearch> m_search; |
| 252 |
|
| 253 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> m_mesh; |
| 254 |
|
| 255 |
std::shared_ptr<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox> m_searchSendMesh; |
| 256 |
std::shared_ptr<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox> m_searchRecvMesh; |
| 257 |
|
| 258 |
std::shared_ptr<SplitCommTestUtil> m_splitCommUtil; |
| 259 |
}; |
| 260 |
|
| 261 |
TEST_F(ParallelSearch, one_by_one_to_one_by_one) |
| 262 |
{ |
| 263 |
unsigned sendNumElemInX = 1, sendNumElemInY = 1; |
| 264 |
unsigned recvNumElemInX = 1, recvNumElemInY = 1; |
| 265 |
|
| 266 |
int numProcsSendMesh = sendNumElemInX*sendNumElemInY; |
| 267 |
int numProcsRecvMesh = recvNumElemInX*recvNumElemInY; |
| 268 |
|
| 269 |
SplitCommTestUtil sctu(numProcsSendMesh, numProcsRecvMesh); |
| 270 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 271 |
|
| 272 |
setup(numProcsSendMesh, numProcsRecvMesh, sendNumElemInX, sendNumElemInY, recvNumElemInX, recvNumElemInY); |
| 273 |
|
| 274 |
unsigned expectedNumSearchPairs = 1u; |
| 275 |
test_search_result(expectedNumSearchPairs); |
| 276 |
|
| 277 |
stk::middle_mesh::mesh::impl::MeshScatterSpec sendMeshSpec(MPI_COMM_WORLD, m_mesh); |
| 278 |
fill_mesh_scatter(sendMeshSpec); |
| 279 |
} |
| 280 |
|
| 281 |
TEST_F(ParallelSearch, one_by_one_to_two_by_two) |
| 282 |
{ |
| 283 |
unsigned sendNumElemInX = 1, sendNumElemInY = 1; |
| 284 |
unsigned recvNumElemInX = 2, recvNumElemInY = 2; |
| 285 |
|
| 286 |
int numProcsSendMesh = sendNumElemInX*sendNumElemInY; |
| 287 |
int numProcsRecvMesh = recvNumElemInX*recvNumElemInY; |
| 288 |
|
| 289 |
SplitCommTestUtil sctu(numProcsSendMesh, numProcsRecvMesh); |
| 290 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 291 |
|
| 292 |
setup(numProcsSendMesh, numProcsRecvMesh, sendNumElemInX, sendNumElemInY, recvNumElemInX, recvNumElemInY); |
| 293 |
|
| 294 |
unsigned expectedNumSearchPairs = 4u; |
| 295 |
test_search_result(expectedNumSearchPairs); |
| 296 |
|
| 297 |
stk::middle_mesh::mesh::impl::MeshScatterSpec sendMeshSpec(MPI_COMM_WORLD, m_mesh); |
| 298 |
fill_mesh_scatter(sendMeshSpec); |
| 299 |
} |
| 300 |
|
| 301 |
TEST_F(ParallelSearch, two_by_two_to_two_by_one) |
| 302 |
{ |
| 303 |
unsigned sendNumElemInX = 2, sendNumElemInY = 2; |
| 304 |
unsigned recvNumElemInX = 2, recvNumElemInY = 1; |
| 305 |
|
| 306 |
int numProcsSendMesh = sendNumElemInX*sendNumElemInY; |
| 307 |
int numProcsRecvMesh = recvNumElemInX*recvNumElemInY; |
| 308 |
|
| 309 |
SplitCommTestUtil sctu(numProcsSendMesh, numProcsRecvMesh); |
| 310 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 311 |
|
| 312 |
setup(numProcsSendMesh, numProcsRecvMesh, sendNumElemInX, sendNumElemInY, recvNumElemInX, recvNumElemInY); |
| 313 |
|
| 314 |
unsigned expectedNumSearchPairs = 2u; |
| 315 |
test_search_result(expectedNumSearchPairs); |
| 316 |
|
| 317 |
stk::middle_mesh::mesh::impl::MeshScatterSpec sendMeshSpec(MPI_COMM_WORLD, m_mesh); |
| 318 |
fill_mesh_scatter(sendMeshSpec); |
| 319 |
} |
| 320 |
|
| 321 |
TEST_F(ParallelSearch, two_by_two_to_two_by_two) |
| 322 |
{ |
| 323 |
unsigned sendNumElemInX = 2, sendNumElemInY = 2; |
| 324 |
unsigned recvNumElemInX = 2, recvNumElemInY = 2; |
| 325 |
|
| 326 |
int numProcsSendMesh = sendNumElemInX*sendNumElemInY; |
| 327 |
int numProcsRecvMesh = recvNumElemInX*recvNumElemInY; |
| 328 |
|
| 329 |
SplitCommTestUtil sctu(numProcsSendMesh, numProcsRecvMesh); |
| 330 |
if (!sctu.get_status()) { GTEST_SKIP(); } |
| 331 |
|
| 332 |
setup(numProcsSendMesh, numProcsRecvMesh, sendNumElemInX, sendNumElemInY, recvNumElemInX, recvNumElemInY); |
| 333 |
|
| 334 |
unsigned expectedNumSearchPairs = 4u; |
| 335 |
test_search_result(expectedNumSearchPairs); |
| 336 |
|
| 337 |
stk::middle_mesh::mesh::impl::MeshScatterSpec sendMeshSpec(MPI_COMM_WORLD, m_mesh); |
| 338 |
fill_mesh_scatter(sendMeshSpec); |
| 339 |
} |
| 340 |
|
| 341 |
TEST(SearchBoundingBox, Regression) |
| 342 |
{ |
| 343 |
if (stk::middle_mesh::utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 344 |
{ |
| 345 |
GTEST_SKIP(); |
| 346 |
} |
| 347 |
|
| 348 |
auto mesh1 = stk::middle_mesh::mesh::make_empty_mesh(); |
| 349 |
auto v1 = mesh1->create_vertex(1.051722092687432, 0.7641208279802152, 0.7483314773547881); |
| 350 |
auto v2 = mesh1->create_vertex(0.8816778784387097, 1.213525491562421, 0); |
| 351 |
auto v3 = mesh1->create_vertex(0.764120827980215, 1.051722092687432, 0.748331477354788); |
| 352 |
mesh1->create_triangle_from_verts(v1, v2, v3); |
| 353 |
|
| 354 |
auto mesh2 = stk::middle_mesh::mesh::make_empty_mesh(); |
| 355 |
auto v4 = mesh2->create_vertex(0.8032742580699905, 1.105558767447774, 0.6184579612837334); |
| 356 |
auto v5 = mesh2->create_vertex(0.6833453909022464, 1.183422925761223, 0.6185056386059162); |
| 357 |
auto v6 = mesh2->create_vertex(0.7166834109365785, 1.241075309649816, 0.4428266523506837); |
| 358 |
mesh2->create_triangle_from_verts(v4, v5, v6); |
| 359 |
|
| 360 |
std::vector<stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox::BoundingBox> boundingBoxVec1, boundingBoxVec2; |
| 361 |
stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox searchMesh1(mesh1, MPI_COMM_WORLD); |
| 362 |
searchMesh1.fill_bounding_boxes(boundingBoxVec1); |
| 363 |
|
| 364 |
stk::middle_mesh::mesh::impl::SearchMeshElementBoundingBox searchMesh2(mesh2, MPI_COMM_WORLD); |
| 365 |
searchMesh2.fill_bounding_boxes(boundingBoxVec2); |
| 366 |
|
| 367 |
EXPECT_TRUE(stk::search::intersects(boundingBoxVec1[0].first, boundingBoxVec2[0].first)); |
| 368 |
} |
| 369 |
|
| 370 |
} |