| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 4 |
#include "stk_middle_mesh/mesh_agglomerator.hpp" |
| 5 |
#include <unordered_map> |
| 6 |
#include <unordered_set> |
| 7 |
|
| 8 |
namespace stk { |
| 9 |
namespace middle_mesh { |
| 10 |
namespace impl { |
| 11 |
|
| 12 |
namespace { |
| 13 |
using namespace mesh; |
| 14 |
using namespace mesh::impl; |
| 15 |
|
| 16 |
class MeshAgglomeratorTester : public ::testing::Test |
| 17 |
{ |
| 18 |
protected: |
| 19 |
MeshAgglomeratorTester() |
| 20 |
{ |
| 21 |
MeshSpec spec; |
| 22 |
spec.numelX = 4; |
| 23 |
spec.numelY = 4; |
| 24 |
spec.xmin = 0; |
| 25 |
spec.xmax = 1; |
| 26 |
spec.ymin = 0; |
| 27 |
spec.ymax = 1; |
| 28 |
|
| 29 |
auto func = [&](const utils::Point& pt) { return pt; }; |
| 30 |
|
| 31 |
mesh1 = create_mesh(spec, func); |
| 32 |
|
| 33 |
// get the partition edges |
| 34 |
double xval = 0.5; |
| 35 |
double yval = 0.5; |
| 36 |
for (auto& edge : mesh1->get_edges()) |
| 37 |
{ |
| 38 |
auto pt1 = edge->get_down(0)->get_point_orig(0); |
| 39 |
auto pt2 = edge->get_down(1)->get_point_orig(0); |
| 40 |
if ((std::abs(pt1.x - xval) < 1e-13 && std::abs(pt2.x - xval) < 1e-13) || |
| 41 |
(std::abs(pt1.y - yval) < 1e-13 && std::abs(pt2.y - yval) < 1e-13)) |
| 42 |
partitionEdges.insert(edge); |
| 43 |
} |
| 44 |
|
| 45 |
auto funcE = [this](MeshEntityPtr edge) { return partitionEdges.count(edge) > 0; }; |
| 46 |
|
| 47 |
agg = std::make_shared<MeshAgglomerator>(mesh1, funcE); |
| 48 |
} |
| 49 |
|
| 50 |
utils::Point get_lower_left_corner(MeshEntityPtr el) |
| 51 |
{ |
| 52 |
utils::Point centroid = mesh::compute_centroid(el); |
| 53 |
|
| 54 |
if (centroid.x < 0.5 && centroid.y < 0.5) |
| 55 |
{ |
| 56 |
return {0, 0}; |
| 57 |
} else if (centroid.x > 0.5 && centroid.y < 0.5) |
| 58 |
{ |
| 59 |
return {0.5, 0}; |
| 60 |
} else if (centroid.x < 0.5 && centroid.y > 0.5) |
| 61 |
{ |
| 62 |
return {0, 0.5}; |
| 63 |
} else |
| 64 |
{ |
| 65 |
return {0.5, 0.5}; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
std::shared_ptr<Mesh> mesh1; |
| 70 |
std::unordered_set<MeshEntityPtr> partitionEdges; |
| 71 |
std::shared_ptr<MeshAgglomerator> agg; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
MeshEntityPtr get_entity(std::shared_ptr<Mesh> mesh, int dim, const utils::Point& pt, double tol=1e-13) |
| 76 |
{ |
| 77 |
double closestDist = tol; |
| 78 |
MeshEntityPtr closestEntity = nullptr; |
| 79 |
for (auto entity : mesh->get_mesh_entities(dim)) |
| 80 |
if (entity) |
| 81 |
{ |
| 82 |
utils::Point centroid = mesh::compute_centroid(entity); |
| 83 |
double dist = std::sqrt(dot(centroid - pt, centroid - pt)); |
| 84 |
if (dist <= closestDist) |
| 85 |
{ |
| 86 |
closestDist = dist; |
| 87 |
closestEntity = entity; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return closestEntity; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
TEST_F(MeshAgglomeratorTester, GroupCreation) |
| 98 |
{ |
| 99 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 100 |
GTEST_SKIP(); |
| 101 |
|
| 102 |
EXPECT_EQ(agg->get_num_groups(), 4); |
| 103 |
for (int i = 0; i < agg->get_num_groups(); ++i) |
| 104 |
{ |
| 105 |
auto elsI = agg->get_group_elements(i); |
| 106 |
// check that each partition edge only appears once |
| 107 |
std::unordered_map<MeshEntityPtr, int> edgeCounts; |
| 108 |
for (auto& el : elsI) |
| 109 |
for (int j = 0; j < el->count_down(); ++j) |
| 110 |
edgeCounts[el->get_down(j)] += 1; |
| 111 |
|
| 112 |
EXPECT_EQ(elsI.size(), static_cast<unsigned int>(4)); |
| 113 |
for (auto& p : edgeCounts) |
| 114 |
if (partitionEdges.count(p.first) > 0) |
| 115 |
{ |
| 116 |
EXPECT_TRUE(edgeCounts[p.first] <= 1); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
for (int i=0; i < agg->get_num_groups(); ++i) |
| 121 |
{ |
| 122 |
auto els_i = agg->get_group_elements(i); |
| 123 |
EXPECT_EQ(els_i.size(), 4u); |
| 124 |
|
| 125 |
utils::Point lowerLeftCorner = get_lower_left_corner(*(els_i.begin())); |
| 126 |
std::vector<utils::Point> centroidOffsets = { {0.125, 0.125}, {0.375, 0.125}, {0.125, 0.375}, {0.375, 0.375}}; |
| 127 |
for (int j=0; j < 4; ++j) |
| 128 |
{ |
| 129 |
auto centroid = lowerLeftCorner + centroidOffsets[j]; |
| 130 |
auto pred = [&](const MeshEntityPtr& entity) |
| 131 |
{ |
| 132 |
utils::Point elCentroid = mesh::compute_centroid(entity); |
| 133 |
double dist = std::sqrt(dot(elCentroid - centroid, elCentroid - centroid)); |
| 134 |
return dist < 1e-13; |
| 135 |
}; |
| 136 |
|
| 137 |
auto it = std::find_if(els_i.begin(), els_i.end(), pred); |
| 138 |
EXPECT_NE(it, els_i.end()); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
TEST_F(MeshAgglomeratorTester, Verts) |
| 144 |
{ |
| 145 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 146 |
GTEST_SKIP(); |
| 147 |
|
| 148 |
EXPECT_EQ(agg->get_num_groups(), 4); |
| 149 |
|
| 150 |
for (int i=0; i < agg->get_num_groups(); ++i) |
| 151 |
{ |
| 152 |
auto& groupEls = agg->get_group_elements(i); |
| 153 |
auto& groupVerts = agg->get_group_verts(i); |
| 154 |
std::set<MeshEntityPtr, MeshEntityCompare> expectedVerts, actualVerts(groupVerts.begin(), groupVerts.end()); |
| 155 |
|
| 156 |
for (auto el : groupEls) |
| 157 |
{ |
| 158 |
std::array<MeshEntityPtr, mesh::MAX_DOWN> verts; |
| 159 |
int ndown = mesh::get_downward(el, 0, verts.data()); |
| 160 |
for (int j=0; j < ndown; ++j) |
| 161 |
{ |
| 162 |
expectedVerts.insert(verts[j]); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
EXPECT_EQ(expectedVerts.size(), actualVerts.size()); |
| 167 |
for (auto vert : expectedVerts) |
| 168 |
{ |
| 169 |
EXPECT_EQ(actualVerts.count(vert), 1u); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
TEST_F(MeshAgglomeratorTester, GetGroupFromVerts) |
| 175 |
{ |
| 176 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 177 |
GTEST_SKIP(); |
| 178 |
|
| 179 |
MeshEntityPtr v1 = get_entity(mesh1, 0, {0, 0}); |
| 180 |
MeshEntityPtr v2 = get_entity(mesh1, 0, {0.5, 0}); |
| 181 |
MeshEntityPtr v3 = get_entity(mesh1, 0, {0, 0.5}); |
| 182 |
MeshAgglomerator::SetType<MeshEntityPtr> verts = {v1, v2, v3}; |
| 183 |
std::vector<int> groupIdxs = agg->get_group_idxs(verts, 2); |
| 184 |
|
| 185 |
EXPECT_EQ(groupIdxs.size(), 1u); |
| 186 |
utils::Point lowerLeftCorner = get_lower_left_corner(*(agg->get_group_elements(groupIdxs[0]).begin())); |
| 187 |
EXPECT_FLOAT_EQ(lowerLeftCorner.x, 0.0); |
| 188 |
EXPECT_FLOAT_EQ(lowerLeftCorner.y, 0.0); |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace impl |
| 192 |
} // namespace middle_mesh |
| 193 |
} // namespace stk |