| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include <stk_util/stk_config.h> |
| 4 |
#include "stk_middle_mesh/mesh.hpp" |
| 5 |
#include "stk_middle_mesh/boundary_fixture.hpp" |
| 6 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 7 |
#include "stk_middle_mesh/create_mesh_quality_improver.hpp" |
| 8 |
#include "stk_middle_mesh/incremental_mesh_boundary_snapper.hpp" |
| 9 |
#include "stk_middle_mesh/mesh_boundary_snapper.hpp" |
| 10 |
#include "stk_middle_mesh/mesh_quality_improver.hpp" |
| 11 |
#include "stk_middle_mesh/mesh_quality_statistics.hpp" |
| 12 |
#include "stk_middle_mesh/regularized_distortion_metric.hpp" |
| 13 |
#include "util/meshes.hpp" |
| 14 |
|
| 15 |
#ifdef STK_BUILT_FOR_SIERRA |
| 16 |
#include "stk_middle_mesh_util/create_stk_mesh.hpp" |
| 17 |
#endif |
| 18 |
|
| 19 |
#include "stk_middle_mesh/mesh_io.hpp" //TODO: DEBUGGING |
| 20 |
|
| 21 |
namespace stk { |
| 22 |
namespace middle_mesh { |
| 23 |
|
| 24 |
using namespace mesh::impl; |
| 25 |
using namespace utils::impl; |
| 26 |
using stk_interface::StkMeshCreator; |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
// func is a lambda (or any other callable) that maps a given |
| 31 |
// point to where it is supposted to be on the surface |
| 32 |
// Ideally this would be the projection to the closest |
| 33 |
// point on the surface, but sometimes we have to settle |
| 34 |
// for the projection along a coordinate axis |
| 35 |
template <typename T> |
| 36 |
double compute_surface_error(std::shared_ptr<mesh::Mesh> mesh, T func) |
| 37 |
{ |
| 38 |
double maxError = 0, minError = std::numeric_limits<double>::max(), avgError = 0, errorNorm = 0; |
| 39 |
|
| 40 |
int nverts = 0; |
| 41 |
for (auto& vert : mesh->get_vertices()) |
| 42 |
{ |
| 43 |
if (vert) |
| 44 |
{ |
| 45 |
auto pt = vert->get_point_orig(0); |
| 46 |
auto ptOnSurface = func(pt); |
| 47 |
|
| 48 |
auto error = std::sqrt(dot(pt - ptOnSurface, pt - ptOnSurface)); |
| 49 |
maxError = std::max(error, maxError); |
| 50 |
minError = std::min(error, minError); |
| 51 |
avgError += error; |
| 52 |
errorNorm += error * error; |
| 53 |
nverts++; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
avgError = avgError / nverts; |
| 58 |
errorNorm = std::sqrt(errorNorm); |
| 59 |
|
| 60 |
std::cout << "max_error = " << maxError << ", min_error = " << minError << ", avg error = " << avgError |
| 61 |
<< ", error_norm = " << errorNorm << std::endl; |
| 62 |
|
| 63 |
return maxError; |
| 64 |
} |
| 65 |
} // namespace |
| 66 |
|
| 67 |
TEST(MeshQualityImprover, AnnulusRotation) |
| 68 |
{ |
| 69 |
// project coordinates onto sector of a sphere. Change the size of |
| 70 |
// the mesh2 sector to test different kinds of topology interactions |
| 71 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 72 |
GTEST_SKIP(); |
| 73 |
|
| 74 |
std::cout << std::setprecision(16) << std::endl; |
| 75 |
double pi = std::atan(1) * 4; |
| 76 |
int nmeshes = 640; |
| 77 |
double dtheta = pi / (16 * nmeshes); |
| 78 |
|
| 79 |
for (int i = 1; i < nmeshes; ++i) |
| 80 |
{ |
| 81 |
std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl; |
| 82 |
std::cout << "dtheta = " << i * dtheta * 180.0 / pi << std::endl; |
| 83 |
|
| 84 |
// std::cout << "creating mesh1" << std::endl; |
| 85 |
std::shared_ptr<mesh::Mesh> mesh1 = impl::make_annulus_mesh(10, 10, 0.5, 1.5, 0); |
| 86 |
|
| 87 |
// std::cout << "\ncreating mesh2" << std::endl; |
| 88 |
std::shared_ptr<mesh::Mesh> mesh2 = impl::make_annulus_mesh(13, 13, 0.5, 1.5, i * dtheta); |
| 89 |
|
| 90 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 91 |
bsnapper->snap(); |
| 92 |
|
| 93 |
std::cout << "number of invalid points = " << bsnapper->get_mesh2_quality_improver()->count_invalid_points() |
| 94 |
<< std::endl; |
| 95 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
TEST(MeshQualityImprover, Ellipsoid) |
| 100 |
{ |
| 101 |
// project coordinates onto an ellipsoid |
| 102 |
|
| 103 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 104 |
GTEST_SKIP(); |
| 105 |
|
| 106 |
int nmeshes = 30; // 60; |
| 107 |
double zscale = 2; |
| 108 |
|
| 109 |
for (int i = 0 /* 0 */; i < nmeshes; ++i) |
| 110 |
{ |
| 111 |
std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl; |
| 112 |
MeshSpec spec, spec2; |
| 113 |
spec.numelX = 5; |
| 114 |
spec.numelY = 5; |
| 115 |
spec.xmin = -0.75; //-1; |
| 116 |
spec.xmax = 0.75; // 1; |
| 117 |
spec.ymin = -0.75; //-1; |
| 118 |
spec.ymax = 0.75; // 1; |
| 119 |
|
| 120 |
spec2.numelX = 5 + i; |
| 121 |
spec2.numelY = 5 + i; |
| 122 |
spec2.xmin = -0.75; //-1; |
| 123 |
spec2.xmax = 0.75; // 1; |
| 124 |
spec2.ymin = -0.75; //-1; |
| 125 |
spec2.ymax = 0.75; // 1; |
| 126 |
|
| 127 |
auto func = [&](const utils::Point& pt) { |
| 128 |
double x = pt.x; |
| 129 |
double y = pt.y; |
| 130 |
|
| 131 |
double xprime = x * std::sqrt(std::max(1 - y * y / 2, 0.0)); |
| 132 |
double yprime = y * std::sqrt(std::max(1 - x * x / 2, 0.0)); |
| 133 |
double zprime = zscale * std::sqrt(std::max(1 - x * x - y * y, 0.0)); |
| 134 |
utils::Point pt2(xprime, yprime, zprime); |
| 135 |
return pt2; |
| 136 |
}; |
| 137 |
|
| 138 |
// std::cout << "creating mesh1" << std::endl; |
| 139 |
std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func); |
| 140 |
|
| 141 |
// std::cout << "\ncreating mesh2" << std::endl; |
| 142 |
std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func); |
| 143 |
|
| 144 |
// printVertEdges("mesh1_initial", mesh1); |
| 145 |
// printVertEdges("mesh2_initial", mesh2); |
| 146 |
|
| 147 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 148 |
bsnapper->snap(); |
| 149 |
std::cout << "after boundary snapper, number of invalid verts = " |
| 150 |
<< bsnapper->get_mesh2_quality_improver()->count_invalid_points() << std::endl; |
| 151 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 152 |
|
| 153 |
// printVertEdges("mesh1_snapped", mesh1); |
| 154 |
// printVertEdges("mesh2_snapped", mesh2); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#ifdef STK_BUILT_FOR_SIERRA |
| 159 |
|
| 160 |
TEST(MeshQualityImprover, EllipsoidFromCAD) |
| 161 |
{ |
| 162 |
// project coordinates onto an ellipsoid |
| 163 |
|
| 164 |
if (comm_size(MPI_COMM_WORLD) != 1) |
| 165 |
GTEST_SKIP(); |
| 166 |
|
| 167 |
std::cout << std::setprecision(16) << std::endl; |
| 168 |
std::string meshPath = "./"; |
| 169 |
std::vector<std::string> fnames = {"half_ellipsoid_2_0.5_quad.g", "half_ellipsoid_2_0.4_quad.g", |
| 170 |
"half_ellipsoid_2_0.3_quad.g", "half_ellipsoid_2_0.2_quad.g", |
| 171 |
"half_ellipsoid_2_0.1_quad.g"}; |
| 172 |
int nmeshes = fnames.size(); |
| 173 |
|
| 174 |
for (int i = 0; i < nmeshes; ++i) |
| 175 |
{ |
| 176 |
StkMeshCreator creator1(meshPath + fnames[0]); |
| 177 |
std::shared_ptr<mesh::Mesh> mesh1 = creator1.create_mesh_from_part("block_1").mesh; |
| 178 |
|
| 179 |
StkMeshCreator creator2(meshPath + fnames[i]); |
| 180 |
std::shared_ptr<mesh::Mesh> mesh2 = creator2.create_mesh_from_part("block_1").mesh; |
| 181 |
|
| 182 |
std::cout << "mesh2 number of elements = " << mesh2->get_elements().size() << std::endl; |
| 183 |
|
| 184 |
// printVertEdges("mesh1_initial", mesh1); |
| 185 |
// printVertEdges("mesh2_initial", mesh2); |
| 186 |
|
| 187 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 188 |
bsnapper->snap(); |
| 189 |
std::cout << "after boundary snapper, number of invalid verts = " |
| 190 |
<< bsnapper->get_mesh2_quality_improver()->count_invalid_points() << std::endl; |
| 191 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 192 |
|
| 193 |
// printVertEdges("mesh1_snapped", mesh1); |
| 194 |
// printVertEdges("mesh2_snapped", mesh2); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#endif |
| 199 |
|
| 200 |
TEST(MeshQualityImprover, AnnulusRefining) |
| 201 |
{ |
| 202 |
// project coordinates onto sector of a sphere. Change the size of |
| 203 |
// the mesh2 sector to test different kinds of topology interactions |
| 204 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 205 |
GTEST_SKIP(); |
| 206 |
|
| 207 |
std::cout << std::setprecision(16) << std::endl; |
| 208 |
int nmeshes = 17; // 17; |
| 209 |
|
| 210 |
for (int i = 1 /*1*/; i < nmeshes; ++i) |
| 211 |
{ |
| 212 |
std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl; |
| 213 |
|
| 214 |
auto errorFunc = [](const utils::Point& pt) { return utils::Point(pt.x, pt.y, 0); }; |
| 215 |
|
| 216 |
std::shared_ptr<mesh::Mesh> mesh1 = impl::make_annulus_mesh(5, 5, 0.5, 1.5, 0); |
| 217 |
|
| 218 |
std::shared_ptr<mesh::Mesh> mesh2 = impl::make_annulus_mesh(5 + i, 5 + i, 0.5, 1.5, 0); |
| 219 |
|
| 220 |
std::cout << "initial mesh1 surface error: "; |
| 221 |
compute_surface_error(mesh1, errorFunc); |
| 222 |
|
| 223 |
std::cout << "initial mesh2 surface error: "; |
| 224 |
compute_surface_error(mesh2, errorFunc); |
| 225 |
|
| 226 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 227 |
bsnapper->snap(); |
| 228 |
|
| 229 |
std::cout << "after snap mesh1 surface error: "; |
| 230 |
compute_surface_error(mesh1, errorFunc); |
| 231 |
|
| 232 |
std::cout << "after snap mesh2 surface error: "; |
| 233 |
compute_surface_error(mesh2, errorFunc); |
| 234 |
|
| 235 |
std::cout << "number of invalid points = " << bsnapper->get_mesh2_quality_improver()->count_invalid_points() |
| 236 |
<< std::endl; |
| 237 |
|
| 238 |
|
| 239 |
std::cout << "final mesh1 surface error: "; |
| 240 |
double maxError1 = compute_surface_error(mesh1, errorFunc); |
| 241 |
|
| 242 |
std::cout << "final mesh2 surface error: "; |
| 243 |
double maxError2 = compute_surface_error(mesh2, errorFunc); |
| 244 |
|
| 245 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 246 |
EXPECT_NEAR(maxError1, 0, 1e-13); |
| 247 |
EXPECT_NEAR(maxError2, 0, 1e-13); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
TEST(MeshQualityImprover, InclinePlaneRefining) |
| 252 |
{ |
| 253 |
// project coordinates onto sector of a sphere. Change the size of |
| 254 |
// the mesh2 sector to test different kinds of topology interactions |
| 255 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 256 |
GTEST_SKIP(); |
| 257 |
|
| 258 |
std::cout << std::setprecision(16) << std::endl; |
| 259 |
int nmeshes = 17; // 17; |
| 260 |
double pi = std::atan(1) * 4; |
| 261 |
double theta = 30 * pi / 180; |
| 262 |
|
| 263 |
for (int i = 1 /*1*/; i < nmeshes; ++i) |
| 264 |
{ |
| 265 |
std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl; |
| 266 |
|
| 267 |
MeshSpec spec, spec2; |
| 268 |
spec.numelX = 5; |
| 269 |
spec.numelY = 5; |
| 270 |
spec.xmin = 0; |
| 271 |
spec.xmax = 2; |
| 272 |
spec.ymin = 0; |
| 273 |
spec.ymax = 3; |
| 274 |
|
| 275 |
spec2.numelX = 5 + i; |
| 276 |
spec2.numelY = 5 + i; |
| 277 |
spec2.xmin = 0; |
| 278 |
spec2.xmax = 2; |
| 279 |
spec2.ymin = 0; |
| 280 |
spec2.ymax = 3; |
| 281 |
|
| 282 |
auto func = [&](const utils::Point& pt) { return utils::Point(pt.x, pt.y, pt.x * std::tan(theta)); }; |
| 283 |
auto errorFunc = [&](const utils::Point& pt) { |
| 284 |
utils::Point normal(-std::cos(pi / 2 - theta), 0, std::sin(pi / 2 - theta)); |
| 285 |
return pt - dot(pt, normal) * normal; |
| 286 |
}; |
| 287 |
|
| 288 |
// std::cout << "creating mesh1" << std::endl; |
| 289 |
std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func); |
| 290 |
|
| 291 |
// std::cout << "\ncreating mesh2" << std::endl; |
| 292 |
std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func); |
| 293 |
|
| 294 |
// printVertEdges("mesh1_initial", mesh1); |
| 295 |
// printVertEdges("mesh2_initial", mesh2); |
| 296 |
|
| 297 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 298 |
bsnapper->snap(); |
| 299 |
|
| 300 |
// printVertEdges("mesh1_snapped", mesh1); |
| 301 |
// printVertEdges("mesh2_snapped", mesh2); |
| 302 |
std::cout << "number of invalid points = " << bsnapper->get_mesh2_quality_improver()->count_invalid_points() |
| 303 |
<< std::endl; |
| 304 |
// EXPECT_GE(fixer2->count_invalid_points(), 0); |
| 305 |
// printVertEdges("mesh1_fixed", mesh1); |
| 306 |
// printVertEdges("mesh2_fixed", mesh2); |
| 307 |
|
| 308 |
std::cout << "final mesh1 surface error: "; |
| 309 |
double maxError1 = compute_surface_error(mesh1, errorFunc); |
| 310 |
|
| 311 |
std::cout << "final mesh2 surface error: "; |
| 312 |
double maxError2 = compute_surface_error(mesh2, errorFunc); |
| 313 |
|
| 314 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 315 |
EXPECT_NEAR(maxError1, 0, 1e-11); |
| 316 |
EXPECT_NEAR(maxError2, 0, 1e-11); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
TEST(MeshQualityImprover, TorusRotation) |
| 321 |
{ |
| 322 |
// project annulus onto surface of sphere (which is not really of torus) |
| 323 |
// Rotate the annulus of |
| 324 |
// the mesh2 sector to test different kinds of topology interactions |
| 325 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 326 |
GTEST_SKIP(); |
| 327 |
|
| 328 |
std::cout << std::setprecision(16) << std::endl; |
| 329 |
double pi = std::atan(1) * 4; |
| 330 |
int nmeshes = 640; |
| 331 |
double dtheta = pi / (16 * nmeshes); |
| 332 |
|
| 333 |
double radiusIn = 0.5; |
| 334 |
double radiusOut = 1.5; |
| 335 |
|
| 336 |
auto func = [radiusOut](const double x, const double y) { |
| 337 |
// project upward onto surface of sphere |
| 338 |
return std::sqrt(std::max(radiusOut * radiusOut - x * x - y * y, 0.0)); |
| 339 |
}; |
| 340 |
|
| 341 |
for (int i = 1; i < nmeshes; ++i) |
| 342 |
{ |
| 343 |
std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl; |
| 344 |
std::cout << "dtheta = " << i * dtheta * 180.0 / pi << std::endl; |
| 345 |
|
| 346 |
// std::cout << "creating mesh1" << std::endl; |
| 347 |
std::shared_ptr<mesh::Mesh> mesh1 = impl::make_annulus_mesh(10, 10, radiusIn, radiusOut, 0, MPI_COMM_WORLD, false, func); |
| 348 |
|
| 349 |
// std::cout << "\ncreating mesh2" << std::endl; |
| 350 |
std::shared_ptr<mesh::Mesh> mesh2 = impl::make_annulus_mesh(13, 13, radiusIn, radiusOut, i * dtheta, MPI_COMM_WORLD, false, func); |
| 351 |
|
| 352 |
// printVertEdges("mesh_initial", mesh2); |
| 353 |
|
| 354 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 355 |
bsnapper->snap(); |
| 356 |
|
| 357 |
// printVertEdges("mesh_snapped", mesh2); |
| 358 |
std::cout << "number of invalid points = " << bsnapper->get_mesh2_quality_improver()->count_invalid_points() |
| 359 |
<< std::endl; |
| 360 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
TEST(MeshQualityImprover, EigthSphereRefining) |
| 365 |
{ |
| 366 |
// project coordinates onto sector of a sphere. Change the size of |
| 367 |
// the mesh2 sector to test different kinds of topology interactions |
| 368 |
if (comm_size(MPI_COMM_WORLD) > 4) |
| 369 |
GTEST_SKIP(); |
| 370 |
|
| 371 |
std::cout << std::setprecision(16) << std::endl; |
| 372 |
int nmeshes = 60; /*60;*/ |
| 373 |
double radius = 1.5; |
| 374 |
for (int i = 0; i < nmeshes; ++i) |
| 375 |
{ |
| 376 |
// computes closest point on sphere to given point |
| 377 |
auto errorFunc = [&](const utils::Point& pt) { |
| 378 |
auto cp = pt / std::sqrt(dot(pt, pt)); |
| 379 |
return radius * cp; |
| 380 |
}; |
| 381 |
|
| 382 |
std::shared_ptr<mesh::Mesh> mesh1 = create_eigth_sphere(5, 5, 0.5, radius); |
| 383 |
|
| 384 |
std::shared_ptr<mesh::Mesh> mesh2 = create_eigth_sphere(5 + i, 5 + i, 0.5, radius); |
| 385 |
|
| 386 |
std::cout << "initial mesh1 surface error: "; |
| 387 |
compute_surface_error(mesh1, errorFunc); |
| 388 |
|
| 389 |
std::cout << "initial mesh2 surface error: "; |
| 390 |
compute_surface_error(mesh2, errorFunc); |
| 391 |
|
| 392 |
// printVertEdges("mesh1_initial", mesh1); |
| 393 |
// printVertEdges("mesh2_initial", mesh2); |
| 394 |
|
| 395 |
auto bsnapper = make_incremental_boundary_snapper(mesh1, mesh2, MPI_COMM_WORLD); |
| 396 |
bsnapper->snap(); |
| 397 |
|
| 398 |
std::cout << "after snap mesh1 surface error: "; |
| 399 |
compute_surface_error(mesh1, errorFunc); |
| 400 |
|
| 401 |
std::cout << "after mesh2 surface error: "; |
| 402 |
compute_surface_error(mesh2, errorFunc); |
| 403 |
|
| 404 |
// printVertEdges("mesh1_snapped", mesh1); |
| 405 |
// printVertEdges("mesh2_snapped", mesh2); |
| 406 |
|
| 407 |
EXPECT_EQ(bsnapper->get_mesh2_quality_improver()->count_invalid_points(), 0); |
| 408 |
|
| 409 |
// printVertEdges("mesh1_fixed", mesh1); |
| 410 |
// printVertEdges("mesh2_fixed", mesh2); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
} // namespace middle_mesh |
| 415 |
} // namespace stk |