| 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 |
#include <utility> |
| 7 |
|
| 8 |
namespace { |
| 9 |
|
| 10 |
using stk::middle_mesh::utils::Point; |
| 11 |
|
| 12 |
class XiCoordinatesForTest : public stk::middle_mesh::XiCoordinates |
| 13 |
{ |
| 14 |
public: |
| 15 |
const std::vector<stk::middle_mesh::utils::Point>& get_xi_coords(stk::middle_mesh::mesh::MeshEntityType type) override |
| 16 |
{ |
| 17 |
if (type == stk::middle_mesh::mesh::MeshEntityType::Quad) |
| 18 |
return m_quadXiCoords; |
| 19 |
else if (type == stk::middle_mesh::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(stk::middle_mesh::mesh::MeshEntityType /*type*/) override { return std::make_pair(0, 2); } |
| 26 |
|
| 27 |
private: |
| 28 |
std::vector<stk::middle_mesh::utils::Point> m_triangleXiCoords = {Point(0.1, 0.2), Point(1.5, 0.3)}; |
| 29 |
std::vector<stk::middle_mesh::utils::Point> m_quadXiCoords = {Point(0.1, 0.2), Point(0.6, 0.7), Point(1.4, 0.4)}; |
| 30 |
}; |
| 31 |
|
| 32 |
class ApplicationInterfaceMPMDTester : public ::testing::Test |
| 33 |
{ |
| 34 |
protected: |
| 35 |
void runtest(stk::middle_mesh::ApplicationInterfaceType type, int nprocs1, int nprocs2, |
| 36 |
bool createTriangles, |
| 37 |
const stk::middle_mesh::ParallelSearchOpts& parallelSearchOpts, |
| 38 |
const stk::middle_mesh::VolumeSnapOpts& volumeSnapOpts, |
| 39 |
const stk::middle_mesh::BoundarySnapAndQualityImprovementOpts& boundarySnapOpts, |
| 40 |
const stk::middle_mesh::MiddleGridOpts& middleGridOpts) |
| 41 |
{ |
| 42 |
if (nprocs1 == 0 || nprocs2 == 0) |
| 43 |
throw std::runtime_error("nprocs cannot be zero"); |
| 44 |
|
| 45 |
if (nprocs1 + nprocs2 > stk::middle_mesh::utils::impl::comm_size(m_unionComm)) |
| 46 |
throw std::runtime_error("too many processes specified"); |
| 47 |
|
| 48 |
inputMesh = nullptr; |
| 49 |
middleGrid = nullptr; |
| 50 |
|
| 51 |
int color = create_mesh_comm(nprocs1, nprocs2); |
| 52 |
create_input_meshes(color, createTriangles); |
| 53 |
bool amIMesh1 = color == 0; |
| 54 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> inputMesh1 = amIMesh1 ? inputMesh : nullptr; |
| 55 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> inputMesh2 = amIMesh1 ? nullptr : inputMesh; |
| 56 |
|
| 57 |
auto xiPts = std::make_shared<XiCoordinatesForTest>(); |
| 58 |
auto interface = application_interface_factory(type, inputMesh1, |
| 59 |
inputMesh2, |
| 60 |
m_unionComm, xiPts, parallelSearchOpts, volumeSnapOpts, |
| 61 |
boundarySnapOpts, middleGridOpts); |
| 62 |
|
| 63 |
interface->create_middle_grid(); |
| 64 |
middleGrid = amIMesh1 ? interface->get_middle_grid_for_mesh1() : interface->get_middle_grid_for_mesh2(); |
| 65 |
auto classification = amIMesh1 ? interface->get_mesh1_classification() : interface->get_mesh2_classification(); |
| 66 |
auto invClassification = amIMesh1 ? interface->compute_mesh1_inverse_classification() : interface->compute_mesh2_inverse_classification(); |
| 67 |
auto remoteInfo = amIMesh1 ? interface->get_remote_info_mesh_one_to_two() : interface->get_remote_info_mesh_two_to_one(); |
| 68 |
auto xiPtsOnInputMesh = amIMesh1 ? interface->get_xi_points_on_mesh1() : interface->get_xi_points_on_mesh2(); |
| 69 |
|
| 70 |
EXPECT_EQ(classification->get_mesh(), middleGrid); |
| 71 |
EXPECT_EQ(invClassification->get_mesh(), inputMesh); |
| 72 |
|
| 73 |
stk::middle_mesh::test_util::test_areas_positive(middleGrid); |
| 74 |
stk::middle_mesh::test_util::test_every_element_classified(middleGrid, classification); |
| 75 |
stk::middle_mesh::test_util::test_every_element_classified_inverse(inputMesh, invClassification); |
| 76 |
stk::middle_mesh::test_util::test_area_per_element(inputMesh, invClassification); |
| 77 |
test_remote_info(remoteInfo, amIMesh1); |
| 78 |
test_remote_info(remoteInfo, !amIMesh1); |
| 79 |
test_xi_points(inputMesh, middleGrid, xiPtsOnInputMesh, classification, xiPts); |
| 80 |
|
| 81 |
if (meshComm != MPI_COMM_NULL) |
| 82 |
MPI_Comm_free(&meshComm); |
| 83 |
} |
| 84 |
|
| 85 |
int create_mesh_comm(int nprocs1, int nprocs2) |
| 86 |
{ |
| 87 |
int myrank = stk::middle_mesh::utils::impl::comm_rank(m_unionComm); |
| 88 |
int color = 0; |
| 89 |
if (myrank >= 0 && myrank < nprocs1) |
| 90 |
color = 0; |
| 91 |
else if (myrank >= nprocs1 && myrank <= (nprocs1 + nprocs2)) |
| 92 |
color = 1; |
| 93 |
else |
| 94 |
color = MPI_UNDEFINED; |
| 95 |
|
| 96 |
MPI_Comm_split(m_unionComm, color, 0, &meshComm); |
| 97 |
|
| 98 |
return color; |
| 99 |
} |
| 100 |
|
| 101 |
void create_input_meshes(int color, bool createTriangles) |
| 102 |
{ |
| 103 |
stk::middle_mesh::mesh::impl::MeshSpec spec1, spec2; |
| 104 |
spec1.xmin = 0; |
| 105 |
spec2.xmin = 0; |
| 106 |
spec1.xmax = 1; |
| 107 |
spec2.xmax = 1; |
| 108 |
spec1.ymin = 0; |
| 109 |
spec2.ymin = 0; |
| 110 |
spec1.ymax = 1; |
| 111 |
spec2.ymax = 1; |
| 112 |
spec1.numelX = 4; |
| 113 |
spec2.numelX = 5; |
| 114 |
spec1.numelY = 4; |
| 115 |
spec2.numelY = 5; |
| 116 |
|
| 117 |
auto f = [](const stk::middle_mesh::utils::Point& pt) { return pt; }; |
| 118 |
if (color == 0) |
| 119 |
inputMesh = stk::middle_mesh::mesh::impl::create_mesh(spec1, f, meshComm, createTriangles); |
| 120 |
else if (color == 1) |
| 121 |
inputMesh = stk::middle_mesh::mesh::impl::create_mesh(spec2, f, meshComm, createTriangles); |
| 122 |
} |
| 123 |
|
| 124 |
void test_remote_info(stk::middle_mesh::mesh::FieldPtr<stk::middle_mesh::mesh::RemoteSharedEntity> remoteInfo, bool amISender) |
| 125 |
{ |
| 126 |
stk::middle_mesh::mesh::FieldPtr<stk::middle_mesh::utils::Point> centroidFieldSendPtr, centroidFieldRecvPtr; |
| 127 |
|
| 128 |
if (middleGrid) |
| 129 |
{ |
| 130 |
if (amISender) |
| 131 |
centroidFieldSendPtr = stk::middle_mesh::mesh::create_field<stk::middle_mesh::utils::Point>(middleGrid, |
| 132 |
stk::middle_mesh::mesh::impl::FieldShape(0, 0, 1), 1); |
| 133 |
else |
| 134 |
centroidFieldRecvPtr = stk::middle_mesh::mesh::create_field<stk::middle_mesh::utils::Point>(middleGrid, |
| 135 |
stk::middle_mesh::mesh::impl::FieldShape(0, 0, 1), 1); |
| 136 |
} |
| 137 |
|
| 138 |
if (amISender && middleGrid) |
| 139 |
{ |
| 140 |
auto& centroidField = *centroidFieldSendPtr; |
| 141 |
for (auto el : middleGrid->get_elements()) |
| 142 |
if (el) |
| 143 |
{ |
| 144 |
centroidField(el, 0, 0) = stk::middle_mesh::mesh::compute_centroid(el); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
stk::middle_mesh::MiddleMeshFieldCommunication<stk::middle_mesh::utils::Point> exchanger(m_unionComm, middleGrid, nullptr, remoteInfo, nullptr); |
| 149 |
exchanger.start_exchange(centroidFieldSendPtr, centroidFieldRecvPtr); |
| 150 |
exchanger.finish_exchange(centroidFieldRecvPtr); |
| 151 |
|
| 152 |
if (!amISender && middleGrid) |
| 153 |
{ |
| 154 |
auto& centroidField = *centroidFieldRecvPtr; |
| 155 |
for (auto el : middleGrid->get_elements()) |
| 156 |
if (el) |
| 157 |
{ |
| 158 |
stk::middle_mesh::utils::Point centroidLocal = stk::middle_mesh::mesh::compute_centroid(el); |
| 159 |
stk::middle_mesh::utils::Point centroidRemote = centroidField(el, 0, 0); |
| 160 |
for (int i=0; i < 3; ++i) |
| 161 |
EXPECT_NEAR(centroidLocal[i], centroidRemote[i], 1e-13); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void test_xi_points(std::shared_ptr<stk::middle_mesh::mesh::Mesh> /*inputMeshArg*/, |
| 167 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> middleGridArg, |
| 168 |
stk::middle_mesh::mesh::FieldPtr<stk::middle_mesh::utils::Point> xiCoordsOnInputMeshPtr, |
| 169 |
stk::middle_mesh::mesh::FieldPtr<stk::middle_mesh::mesh::MeshEntityPtr> meshInToInputMeshPtr, |
| 170 |
std::shared_ptr<stk::middle_mesh::XiCoordinates> xiCoords) |
| 171 |
{ |
| 172 |
auto& xiCoordsOnInputMesh = *xiCoordsOnInputMeshPtr; |
| 173 |
auto& meshInToInputMesh = *meshInToInputMeshPtr; |
| 174 |
for (auto& elMiddle : middleGridArg->get_elements()) |
| 175 |
if (elMiddle) |
| 176 |
{ |
| 177 |
stk::middle_mesh::mesh::MeshEntityPtr elInput = meshInToInputMesh(elMiddle, 0, 0); |
| 178 |
auto& xiCoordsEl = xiCoords->get_xi_coords(elMiddle->get_type()); |
| 179 |
std::pair<double, double> xiRangeMiddle = xiCoords->get_xi_coord_range(elMiddle->get_type()); |
| 180 |
std::pair<double, double> xiRangeInput = xiCoords->get_xi_coord_range(elInput->get_type()); |
| 181 |
|
| 182 |
for (size_t i=0; i < xiCoordsEl.size(); ++i) |
| 183 |
{ |
| 184 |
Point xiCoordsMiddle = stk::middle_mesh::mesh::convert_xi_coords_from_range(xiRangeMiddle.first, xiRangeMiddle.second, xiCoordsEl[i]); |
| 185 |
auto middleMeshCoords = stk::middle_mesh::mesh::compute_coords_from_xi_3d(elMiddle, xiCoordsMiddle); |
| 186 |
|
| 187 |
Point xiCoordsInput = stk::middle_mesh::mesh::convert_xi_coords_from_range(xiRangeInput.first, xiRangeInput.second, |
| 188 |
xiCoordsOnInputMesh(elMiddle, i, 0)); |
| 189 |
auto inputMeshCoords = stk::middle_mesh::mesh::compute_coords_from_xi_3d(elInput, xiCoordsInput); |
| 190 |
|
| 191 |
for (int d=0; d < 3; ++d) |
| 192 |
EXPECT_NEAR(middleMeshCoords[d], inputMeshCoords[d], 1e-12); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
MPI_Comm m_unionComm = MPI_COMM_WORLD; |
| 199 |
MPI_Comm meshComm = MPI_COMM_NULL; |
| 200 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> inputMesh; |
| 201 |
std::shared_ptr<stk::middle_mesh::mesh::Mesh> middleGrid; |
| 202 |
}; |
| 203 |
|
| 204 |
const std::vector<stk::middle_mesh::ApplicationInterfaceType> InterfaceTypes = {/*stk::middle_mesh::ApplicationInterfaceType::FakeParallel,*/ stk::middle_mesh::ApplicationInterfaceType::Parallel}; |
| 205 |
|
| 206 |
} // namespace |
| 207 |
|
| 208 |
TEST_F(ApplicationInterfaceMPMDTester, Defaults) |
| 209 |
{ |
| 210 |
int commsize = stk::middle_mesh::utils::impl::comm_size(MPI_COMM_WORLD); |
| 211 |
if (commsize < 2 || commsize > 4) |
| 212 |
GTEST_SKIP(); |
| 213 |
|
| 214 |
stk::middle_mesh::ParallelSearchOpts parallelSearchOpts; |
| 215 |
stk::middle_mesh::VolumeSnapOpts volumeSnapOpts; |
| 216 |
stk::middle_mesh::BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 217 |
stk::middle_mesh::MiddleGridOpts middleGridOpts; |
| 218 |
|
| 219 |
for (bool createTriangles : {false, true}) |
| 220 |
{ |
| 221 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 222 |
{ |
| 223 |
for (int nprocs1 = 1; nprocs1 < commsize; ++nprocs1) |
| 224 |
{ |
| 225 |
int nprocs2 = commsize - nprocs1; |
| 226 |
runtest(type, nprocs1, nprocs2, createTriangles, parallelSearchOpts, volumeSnapOpts, |
| 227 |
boundarySnapOpts, middleGridOpts); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
TEST_F(ApplicationInterfaceMPMDTester, EnableVolumeSnap) |
| 234 |
{ |
| 235 |
int commsize = stk::middle_mesh::utils::impl::comm_size(MPI_COMM_WORLD); |
| 236 |
if (commsize < 2 || commsize > 4) |
| 237 |
GTEST_SKIP(); |
| 238 |
|
| 239 |
stk::middle_mesh::ParallelSearchOpts parallelSearchOpts; |
| 240 |
stk::middle_mesh::VolumeSnapOpts volumeSnapOpts; |
| 241 |
volumeSnapOpts.type = stk::middle_mesh::VolumeSnapType::Standard; |
| 242 |
stk::middle_mesh::BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 243 |
stk::middle_mesh::MiddleGridOpts middleGridOpts; |
| 244 |
|
| 245 |
for (int nprocs1 = 1; nprocs1 < commsize; ++nprocs1) |
| 246 |
{ |
| 247 |
int nprocs2 = commsize - nprocs1; |
| 248 |
runtest(stk::middle_mesh::ApplicationInterfaceType::FakeParallel, |
| 249 |
nprocs1, nprocs2, false, parallelSearchOpts, volumeSnapOpts, boundarySnapOpts, middleGridOpts); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
TEST_F(ApplicationInterfaceMPMDTester, BoundarySnapThenQuality) |
| 254 |
{ |
| 255 |
int commsize = stk::middle_mesh::utils::impl::comm_size(MPI_COMM_WORLD); |
| 256 |
if (commsize < 2 || commsize > 4) |
| 257 |
GTEST_SKIP(); |
| 258 |
|
| 259 |
stk::middle_mesh::ParallelSearchOpts parallelSearchOpts; |
| 260 |
stk::middle_mesh::VolumeSnapOpts volumeSnapOpts; |
| 261 |
stk::middle_mesh::BoundarySnapAndQualityImprovementOpts boundarySnapOpts; |
| 262 |
boundarySnapOpts.type = stk::middle_mesh::BoundarySnapAndQualityImprovementType::SnapThenQuality; |
| 263 |
stk::middle_mesh::MiddleGridOpts middleGridOpts; |
| 264 |
|
| 265 |
for (bool createTriangles : {false, true}) |
| 266 |
{ |
| 267 |
for (stk::middle_mesh::ApplicationInterfaceType type : InterfaceTypes) |
| 268 |
{ |
| 269 |
for (int nprocs1 = 1; nprocs1 < commsize; ++nprocs1) |
| 270 |
{ |
| 271 |
int nprocs2 = commsize - nprocs1; |
| 272 |
runtest(type,nprocs1, nprocs2, createTriangles, parallelSearchOpts, volumeSnapOpts, |
| 273 |
boundarySnapOpts, middleGridOpts); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |