| 1 |
#include <stk_middle_mesh/application_interface.hpp> |
| 2 |
#include "util/nonconformal_interface_helpers.hpp" |
| 3 |
#include <stk_middle_mesh/utils.hpp> |
| 4 |
#include "stk_middle_mesh/communication_api.hpp" |
| 5 |
#include "gtest/gtest.h" |
| 6 |
|
| 7 |
namespace { |
| 8 |
|
| 9 |
using namespace stk::middle_mesh; |
| 10 |
|
| 11 |
|
| 12 |
class XiCoordinatesForTest : public XiCoordinates |
| 13 |
{ |
| 14 |
public: |
| 15 |
const std::vector<utils::Point>& get_xi_coords(mesh::MeshEntityType type) override |
| 16 |
{ |
| 17 |
if (type == mesh::MeshEntityType::Quad) |
| 18 |
return m_quadXiCoords; |
| 19 |
else if (type == mesh::MeshEntityType::Triangle) |
| 20 |
return m_triangleXiCoords; |
| 21 |
else |
| 22 |
throw std::runtime_error("only triangles and quads supported"); |
| 23 |
} |
| 24 |
|
| 25 |
std::pair<double, double> get_xi_coord_range(mesh::MeshEntityType /*type*/) override { return std::make_pair(0, 2); } |
| 26 |
|
| 27 |
private: |
| 28 |
std::vector<utils::Point> m_triangleXiCoords = {utils::Point(0.1, 0.2), utils::Point(1.5, 0.3)}; |
| 29 |
std::vector<utils::Point> m_quadXiCoords = {utils::Point(0.1, 0.2), utils::Point(0.6, 0.7), utils::Point(1.4, 0.4)}; |
| 30 |
}; |
| 31 |
|
| 32 |
class ApplicationInterfaceSPMDTester : public ::testing::Test |
| 33 |
{ |
| 34 |
protected: |
| 35 |
void runtest(ApplicationInterfaceType type, int nprocs1, int nprocs2, bool createTriangles, const ParallelSearchOpts& parallelSearchOpts, const VolumeSnapOpts& volumeSnapOpts, |
| 36 |
const BoundarySnapAndQualityImprovementOpts& boundarySnapOpts, const MiddleGridOpts& middleGridOpts) |
| 37 |
{ |
| 38 |
if (nprocs1 == 0) |
| 39 |
throw std::runtime_error("nprocs cannot be zero"); |
| 40 |
|
| 41 |
if (nprocs1 > utils::impl::comm_size(MPI_COMM_WORLD)) |
| 42 |
throw std::runtime_error("too many processes specified"); |
| 43 |
|
| 44 |
if (nprocs2 == 0) |
| 45 |
throw std::runtime_error("nprocs cannot be zero"); |
| 46 |
|
| 47 |
if (nprocs2 > utils::impl::comm_size(MPI_COMM_WORLD)) |
| 48 |
throw std::runtime_error("too many processes specified"); |
| 49 |
|
| 50 |
inputMesh1 = nullptr; |
| 51 |
inputMesh2 = nullptr; |
| 52 |
middleGrid1 = nullptr; |
| 53 |
middleGrid2 = nullptr; |
| 54 |
|
| 55 |
create_mesh_comms(nprocs1, nprocs2); |
| 56 |
|
| 57 |
create_input_meshes(createTriangles); |
| 58 |
|
| 59 |
auto xiPts = std::make_shared<XiCoordinatesForTest>(); |
| 60 |
auto interface = |
| 61 |
application_interface_factory(type, inputMesh1, inputMesh2, MPI_COMM_WORLD, |
| 62 |
xiPts, |
| 63 |
parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 64 |
|
| 65 |
interface->create_middle_grid(); |
| 66 |
middleGrid1 = inputMesh1 ? interface->get_middle_grid_for_mesh1() : nullptr; |
| 67 |
middleGrid2 = inputMesh2 ? interface->get_middle_grid_for_mesh2() : nullptr; |
| 68 |
auto classification1 = inputMesh1 ? interface->get_mesh1_classification() : nullptr; |
| 69 |
auto classification2 = inputMesh2 ? interface->get_mesh2_classification() : nullptr; |
| 70 |
auto invClassification1 = inputMesh1 ? interface->compute_mesh1_inverse_classification() : nullptr; |
| 71 |
auto invClassification2 = inputMesh2 ? interface->compute_mesh2_inverse_classification() : nullptr; |
| 72 |
auto remoteInfoOneToTwo = inputMesh1 ? interface->get_remote_info_mesh_one_to_two() : nullptr; |
| 73 |
auto remoteInfoTwoToOne = inputMesh2 ? interface->get_remote_info_mesh_two_to_one() : nullptr; |
| 74 |
auto xiPtsOnMesh1 = inputMesh1 ? interface->get_xi_points_on_mesh1() : nullptr; |
| 75 |
auto xiPtsOnMesh2 = inputMesh2 ? interface->get_xi_points_on_mesh2() : nullptr; |
| 76 |
|
| 77 |
|
| 78 |
if (inputMesh1) |
| 79 |
{ |
| 80 |
EXPECT_EQ(classification1->get_mesh(), middleGrid1); |
| 81 |
EXPECT_EQ(invClassification1->get_mesh(), inputMesh1); |
| 82 |
test_util::test_areas_positive(middleGrid1); |
| 83 |
test_util::test_every_element_classified(middleGrid1, classification1); |
| 84 |
test_util::test_every_element_classified_inverse(inputMesh1, invClassification1); |
| 85 |
test_util::test_area_per_element(inputMesh1, invClassification1); |
| 86 |
test_xi_points(inputMesh1, middleGrid1, xiPtsOnMesh1, classification1, xiPts); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
if (inputMesh2) |
| 91 |
{ |
| 92 |
EXPECT_EQ(classification2->get_mesh(), middleGrid2); |
| 93 |
EXPECT_EQ(invClassification2->get_mesh(), inputMesh2); |
| 94 |
test_util::test_areas_positive(middleGrid2); |
| 95 |
test_util::test_every_element_classified(middleGrid2, classification2); |
| 96 |
test_util::test_every_element_classified_inverse(inputMesh2, invClassification2); |
| 97 |
test_util::test_area_per_element(inputMesh2, invClassification2); |
| 98 |
test_xi_points(inputMesh2, middleGrid2, xiPtsOnMesh2, classification2, xiPts); |
| 99 |
} |
| 100 |
|
| 101 |
test_remote_info(remoteInfoOneToTwo, remoteInfoTwoToOne, true); |
| 102 |
test_remote_info(remoteInfoOneToTwo, remoteInfoTwoToOne, false); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if (meshComm1 != MPI_COMM_NULL) |
| 108 |
MPI_Comm_free(&meshComm1); |
| 109 |
|
| 110 |
if (meshComm2 != MPI_COMM_NULL) |
| 111 |
MPI_Comm_free(&meshComm2); |
| 112 |
} |
| 113 |
|
| 114 |
bool create_mesh_comms(int nprocs1, int nprocs2) |
| 115 |
{ |
| 116 |
int myrank = utils::impl::comm_rank(MPI_COMM_WORLD); |
| 117 |
int color1 = myrank < nprocs1 ? 0 : MPI_UNDEFINED; |
| 118 |
MPI_Comm_split(MPI_COMM_WORLD, color1, 0, &meshComm1); |
| 119 |
|
| 120 |
int color2 = myrank < nprocs2 ? 0 : MPI_UNDEFINED; |
| 121 |
MPI_Comm_split(MPI_COMM_WORLD, color2, 0, &meshComm2); |
| 122 |
|
| 123 |
return color1 == 0 || color2 == 0; |
| 124 |
} |
| 125 |
|
| 126 |
void create_input_meshes(bool createTriangles) |
| 127 |
{ |
| 128 |
mesh::impl::MeshSpec spec1, spec2; |
| 129 |
spec1.xmin = 0; |
| 130 |
spec2.xmin = 0; |
| 131 |
spec1.xmax = 1; |
| 132 |
spec2.xmax = 1; |
| 133 |
spec1.ymin = 0; |
| 134 |
spec2.ymin = 0; |
| 135 |
spec1.ymax = 1; |
| 136 |
spec2.ymax = 1; |
| 137 |
spec1.numelX = 4; |
| 138 |
spec2.numelX = 5; |
| 139 |
spec1.numelY = 4; |
| 140 |
spec2.numelY = 5; |
| 141 |
|
| 142 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 143 |
|
| 144 |
if (meshComm1 != MPI_COMM_NULL) |
| 145 |
inputMesh1 = create_mesh(spec1, f, meshComm1, createTriangles); |
| 146 |
|
| 147 |
if (meshComm2 != MPI_COMM_NULL) |
| 148 |
inputMesh2 = create_mesh(spec2, f, meshComm2, createTriangles); |
| 149 |
} |
| 150 |
|
| 151 |
void test_remote_info(mesh::FieldPtr<mesh::RemoteSharedEntity> remoteInfoOneToTwo, |
| 152 |
mesh::FieldPtr<mesh::RemoteSharedEntity> remoteInfoTwoToOne, |
| 153 |
bool sendOneToTwo) |
| 154 |
{ |
| 155 |
|
| 156 |
auto middleGridSend = sendOneToTwo ? middleGrid1 : middleGrid2; |
| 157 |
auto middleGridRecv = sendOneToTwo ? middleGrid2 : middleGrid1; |
| 158 |
|
| 159 |
mesh::FieldPtr<utils::Point> centroidFieldSendPtr, centroidFieldRecvPtr; |
| 160 |
if (middleGridSend) |
| 161 |
{ |
| 162 |
centroidFieldSendPtr = mesh::create_field<utils::Point>(middleGridSend, |
| 163 |
mesh::impl::FieldShape(0, 0, 1), 1); |
| 164 |
auto& centroidFieldSend = *centroidFieldSendPtr; |
| 165 |
for (auto el : middleGridSend->get_elements()) |
| 166 |
if (el) |
| 167 |
{ |
| 168 |
centroidFieldSend(el, 0, 0) = mesh::compute_centroid(el); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (middleGridRecv) |
| 173 |
centroidFieldRecvPtr = mesh::create_field<utils::Point>(middleGridRecv, |
| 174 |
mesh::impl::FieldShape(0, 0, 1), 1); |
| 175 |
|
| 176 |
MiddleMeshFieldCommunication<utils::Point> exchanger(MPI_COMM_WORLD, middleGrid1, middleGrid2, |
| 177 |
remoteInfoOneToTwo, remoteInfoTwoToOne); |
| 178 |
exchanger.start_exchange(centroidFieldSendPtr, centroidFieldRecvPtr); |
| 179 |
exchanger.finish_exchange(centroidFieldRecvPtr); |
| 180 |
|
| 181 |
if (middleGridRecv) |
| 182 |
{ |
| 183 |
auto& centroidFieldRecv = *centroidFieldRecvPtr; |
| 184 |
for (auto el : middleGridRecv->get_elements()) |
| 185 |
if (el) |
| 186 |
{ |
| 187 |
utils::Point centroidLocal = mesh::compute_centroid(el); |
| 188 |
utils::Point centroidRemote = centroidFieldRecv(el, 0, 0); |
| 189 |
for (int i=0; i < 3; ++i) |
| 190 |
EXPECT_NEAR(centroidLocal[i], centroidRemote[i], 1e-13); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void test_xi_points(std::shared_ptr<mesh::Mesh> /*inputMesh*/, std::shared_ptr<mesh::Mesh> middleGrid, |
| 196 |
mesh::FieldPtr<utils::Point> xiCoordsOnInputMeshPtr, |
| 197 |
mesh::FieldPtr<mesh::MeshEntityPtr> meshInToInputMeshPtr, |
| 198 |
std::shared_ptr<XiCoordinates> xiCoords) |
| 199 |
{ |
| 200 |
auto& xiCoordsOnInputMesh = *xiCoordsOnInputMeshPtr; |
| 201 |
auto& meshInToInputMesh = *meshInToInputMeshPtr; |
| 202 |
for (auto& elMiddle : middleGrid->get_elements()) |
| 203 |
if (elMiddle) |
| 204 |
{ |
| 205 |
mesh::MeshEntityPtr elInput = meshInToInputMesh(elMiddle, 0, 0); |
| 206 |
auto& xiCoordsEl = xiCoords->get_xi_coords(elMiddle->get_type()); |
| 207 |
std::pair<double, double> xiRangeMiddle = xiCoords->get_xi_coord_range(elMiddle->get_type()); |
| 208 |
std::pair<double, double> xiRangeInput = xiCoords->get_xi_coord_range(elInput->get_type()); |
| 209 |
|
| 210 |
for (size_t i=0; i < xiCoordsEl.size(); ++i) |
| 211 |
{ |
| 212 |
utils::Point xiCoordsMiddle = mesh::convert_xi_coords_from_range(xiRangeMiddle.first, xiRangeMiddle.second, xiCoordsEl[i]); |
| 213 |
auto middleMeshCoords = mesh::compute_coords_from_xi_3d(elMiddle, xiCoordsMiddle); |
| 214 |
|
| 215 |
utils::Point xiCoordsInput = mesh::convert_xi_coords_from_range(xiRangeInput.first, xiRangeInput.second, |
| 216 |
xiCoordsOnInputMesh(elMiddle, i, 0)); |
| 217 |
auto inputMeshCoords = mesh::compute_coords_from_xi_3d(elInput, xiCoordsInput); |
| 218 |
|
| 219 |
for (int d=0; d < 3; ++d) |
| 220 |
EXPECT_NEAR(middleMeshCoords[d], inputMeshCoords[d], 1e-12); |
| 221 |
} |
| 222 |
|
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
MPI_Comm meshComm1 = MPI_COMM_NULL; |
| 227 |
MPI_Comm meshComm2 = MPI_COMM_NULL; |
| 228 |
std::shared_ptr<mesh::Mesh> inputMesh1; |
| 229 |
std::shared_ptr<mesh::Mesh> inputMesh2; |
| 230 |
std::shared_ptr<mesh::Mesh> middleGrid1; |
| 231 |
std::shared_ptr<mesh::Mesh> middleGrid2; |
| 232 |
}; |
| 233 |
|
| 234 |
const std::vector<stk::middle_mesh::ApplicationInterfaceType> InterfaceTypes = {/*stk::middle_mesh::ApplicationInterfaceType::FakeParallel,*/ stk::middle_mesh::ApplicationInterfaceType::Parallel}; |
| 235 |
|
| 236 |
|
| 237 |
} // namespace |
| 238 |
|
| 239 |
TEST_F(ApplicationInterfaceSPMDTester, Defaults) |
| 240 |
{ |
| 241 |
int commsize = utils::impl::comm_size(MPI_COMM_WORLD); |
| 242 |
if (commsize > 4) |
| 243 |
GTEST_SKIP(); |
| 244 |
|
| 245 |
ParallelSearchOpts parallelSearchOpts; |
| 246 |
VolumeSnapOpts volumeSnapOpts; |
| 247 |
BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 248 |
MiddleGridOpts middleGridOpts; |
| 249 |
|
| 250 |
for (bool createTriangles : {false, true}) |
| 251 |
{ |
| 252 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 253 |
{ |
| 254 |
for (int nprocs = 1; nprocs <= commsize; ++nprocs) |
| 255 |
{ |
| 256 |
runtest(type, nprocs, nprocs, createTriangles, parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
TEST_F(ApplicationInterfaceSPMDTester, MToN) |
| 263 |
{ |
| 264 |
int commsize = utils::impl::comm_size(MPI_COMM_WORLD); |
| 265 |
if (commsize > 4) |
| 266 |
GTEST_SKIP(); |
| 267 |
|
| 268 |
ParallelSearchOpts parallelSearchOpts; |
| 269 |
VolumeSnapOpts volumeSnapOpts; |
| 270 |
BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 271 |
boundarySnapOpts.type = BoundarySnapAndQualityImprovementType::None; |
| 272 |
MiddleGridOpts middleGridOpts; |
| 273 |
|
| 274 |
for (bool createTriangles : {false, true}) |
| 275 |
{ |
| 276 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 277 |
{ |
| 278 |
for (int nprocs1 = 1; nprocs1 <= commsize; ++nprocs1) |
| 279 |
for (int nprocs2 = 1; nprocs2 <= commsize; ++nprocs2) |
| 280 |
{ |
| 281 |
runtest(type, nprocs1, nprocs2, createTriangles, parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
TEST_F(ApplicationInterfaceSPMDTester, EnableVolumeSnap) |
| 288 |
{ |
| 289 |
int commsize = utils::impl::comm_size(MPI_COMM_WORLD); |
| 290 |
if (commsize > 4) |
| 291 |
GTEST_SKIP(); |
| 292 |
|
| 293 |
ParallelSearchOpts parallelSearchOpts; |
| 294 |
VolumeSnapOpts volumeSnapOpts; |
| 295 |
volumeSnapOpts.type = VolumeSnapType::Standard; |
| 296 |
BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 297 |
MiddleGridOpts middleGridOpts; |
| 298 |
|
| 299 |
for (int nprocs = 1; nprocs <= commsize; ++nprocs) |
| 300 |
{ |
| 301 |
runtest(ApplicationInterfaceType::FakeParallel, nprocs, nprocs, false, parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
TEST_F(ApplicationInterfaceSPMDTester, BoundarySnapThenQuality) |
| 306 |
{ |
| 307 |
int commsize = utils::impl::comm_size(MPI_COMM_WORLD); |
| 308 |
if (commsize > 4) |
| 309 |
GTEST_SKIP(); |
| 310 |
|
| 311 |
ParallelSearchOpts parallelSearchOpts; |
| 312 |
VolumeSnapOpts volumeSnapOpts; |
| 313 |
BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 314 |
boundarySnapOpts.type = BoundarySnapAndQualityImprovementType::SnapThenQuality; |
| 315 |
MiddleGridOpts middleGridOpts; |
| 316 |
|
| 317 |
for (bool createTriangles : {false, true}) |
| 318 |
{ |
| 319 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 320 |
{ |
| 321 |
for (int nprocs = 1; nprocs < commsize; ++nprocs) |
| 322 |
{ |
| 323 |
runtest(type, nprocs, nprocs, createTriangles, parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
TEST(ApplicationInterfaceSPMD, MeshNotCreatedError) |
| 330 |
{ |
| 331 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 2) |
| 332 |
GTEST_SKIP(); |
| 333 |
|
| 334 |
mesh::impl::MeshSpec spec1; |
| 335 |
spec1.xmin = 0; |
| 336 |
spec1.xmax = 1; |
| 337 |
spec1.ymin = 0; |
| 338 |
spec1.ymax = 1; |
| 339 |
spec1.numelX = 4; |
| 340 |
spec1.numelY = 4; |
| 341 |
|
| 342 |
MPI_Comm comm1 = MPI_COMM_WORLD; |
| 343 |
|
| 344 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 345 |
auto inputMesh1 = mesh::impl::create_mesh(spec1, f, comm1); |
| 346 |
auto inputMesh2 = mesh::impl::create_mesh(spec1, f, comm1); |
| 347 |
|
| 348 |
|
| 349 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 350 |
{ |
| 351 |
auto interface = application_interface_factory(type, inputMesh1, inputMesh2, comm1); |
| 352 |
|
| 353 |
EXPECT_ANY_THROW(interface->get_middle_grid_for_mesh1()); |
| 354 |
EXPECT_ANY_THROW(interface->get_middle_grid_for_mesh2()); |
| 355 |
EXPECT_ANY_THROW(interface->get_mesh1_classification()); |
| 356 |
EXPECT_ANY_THROW(interface->get_mesh2_classification()); |
| 357 |
EXPECT_ANY_THROW(interface->compute_mesh1_inverse_classification()); |
| 358 |
EXPECT_ANY_THROW(interface->compute_mesh2_inverse_classification()); |
| 359 |
} |
| 360 |
} |