| 1 |
#include "gtest/gtest.h" |
| 2 |
#include "stk_middle_mesh/destination_field_synchronizer.hpp" |
| 3 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 4 |
#include "stk_middle_mesh/utils.hpp" |
| 5 |
|
| 6 |
using namespace stk::middle_mesh; |
| 7 |
|
| 8 |
namespace { |
| 9 |
|
| 10 |
mesh::MeshEntityPtr find_closest_entity(std::shared_ptr<mesh::Mesh> mesh, int dim, const utils::Point& centroid) |
| 11 |
{ |
| 12 |
double minDist = std::numeric_limits<double>::max(); |
| 13 |
mesh::MeshEntityPtr minEntity = nullptr; |
| 14 |
for (auto& entity : mesh->get_mesh_entities(dim)) |
| 15 |
if (entity) |
| 16 |
{ |
| 17 |
utils::Point pt = mesh::compute_centroid(entity); |
| 18 |
utils::Point disp = centroid - pt; |
| 19 |
double dist = std::sqrt(dot(disp, disp)); |
| 20 |
if (dist < minDist) |
| 21 |
{ |
| 22 |
minDist = dist; |
| 23 |
minEntity = entity; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return minEntity; |
| 28 |
} |
| 29 |
|
| 30 |
std::vector<int> get_values(mesh::VariableSizeFieldPtr<int> fieldPtr, mesh::MeshEntityPtr entity, int node) |
| 31 |
{ |
| 32 |
auto& field = *fieldPtr; |
| 33 |
std::vector<int> vals(field(entity, node).begin(), field(entity, node).end()); |
| 34 |
return vals; |
| 35 |
} |
| 36 |
|
| 37 |
void expect_eq(const std::vector<int>& lhs, const std::vector<int>& rhs) |
| 38 |
{ |
| 39 |
EXPECT_EQ(lhs, rhs); |
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
TEST(DestinationFieldSynchronizer, 2Procs) |
| 45 |
{ |
| 46 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 2) |
| 47 |
GTEST_SKIP(); |
| 48 |
|
| 49 |
mesh::impl::MeshSpec spec; |
| 50 |
spec.xmin = 0; spec.ymin = 0; |
| 51 |
spec.xmax = 1; spec.ymax = 1; |
| 52 |
spec.numelX = 2; spec.numelY = 2; |
| 53 |
|
| 54 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 55 |
auto mesh = mesh::impl::create_mesh(spec, f); |
| 56 |
|
| 57 |
auto scatterSpec = std::make_shared<mesh::impl::MeshScatterSpec>(MPI_COMM_WORLD, mesh); |
| 58 |
int myrank = utils::impl::comm_rank(MPI_COMM_WORLD); |
| 59 |
if (myrank == 0) |
| 60 |
{ |
| 61 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.25}); |
| 62 |
auto el2 = find_closest_entity(mesh, 2, {0.25, 0.75}); |
| 63 |
scatterSpec->add_destination(el1, 0); |
| 64 |
scatterSpec->add_destination(el2, 1); |
| 65 |
} else |
| 66 |
{ |
| 67 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.25}); |
| 68 |
auto el2 = find_closest_entity(mesh, 2, {0.75, 0.75}); |
| 69 |
scatterSpec->add_destination(el1, 2); |
| 70 |
scatterSpec->add_destination(el2, 3); |
| 71 |
} |
| 72 |
|
| 73 |
mesh::impl::DestinationFieldSynchronizer gatherer(mesh, scatterSpec); |
| 74 |
auto gatheredDestField = gatherer.synchronize(); |
| 75 |
|
| 76 |
if (myrank == 0) |
| 77 |
{ |
| 78 |
auto v1 = find_closest_entity(mesh, 0, {0, 0, 0}); |
| 79 |
auto v2 = find_closest_entity(mesh, 0, {0.5, 0, 0}); |
| 80 |
auto v3 = find_closest_entity(mesh, 0, {0, 0.5, 0}); |
| 81 |
auto v4 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 82 |
auto v5 = find_closest_entity(mesh, 0, {0, 1, 0}); |
| 83 |
auto v6 = find_closest_entity(mesh, 0, {0.5, 1, 0}); |
| 84 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.25, 0}); |
| 85 |
auto el2 = find_closest_entity(mesh, 2, {0.25, 0.75, 0}); |
| 86 |
|
| 87 |
expect_eq(get_values(gatheredDestField, v1, 0), {0}); |
| 88 |
expect_eq(get_values(gatheredDestField, v2, 0), {0, 2}); |
| 89 |
expect_eq(get_values(gatheredDestField, v3, 0), {0, 1}); |
| 90 |
expect_eq(get_values(gatheredDestField, v4, 0), {0, 1, 2, 3}); |
| 91 |
expect_eq(get_values(gatheredDestField, v5, 0), {1}); |
| 92 |
expect_eq(get_values(gatheredDestField, v6, 0), {1, 3}); |
| 93 |
|
| 94 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {0}); |
| 95 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {0, 2}); |
| 96 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {0, 1}); |
| 97 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {0}); |
| 98 |
|
| 99 |
expect_eq(get_values(gatheredDestField, el2->get_down(1), 0), {1, 3}); |
| 100 |
expect_eq(get_values(gatheredDestField, el2->get_down(2), 0), {1}); |
| 101 |
expect_eq(get_values(gatheredDestField, el2->get_down(3), 0), {1}); |
| 102 |
|
| 103 |
} else |
| 104 |
{ |
| 105 |
auto v1 = find_closest_entity(mesh, 0, {0.5, 0, 0}); |
| 106 |
auto v2 = find_closest_entity(mesh, 0, {1.0, 0, 0}); |
| 107 |
auto v3 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 108 |
auto v4 = find_closest_entity(mesh, 0, {1.0, 0.5, 0}); |
| 109 |
auto v5 = find_closest_entity(mesh, 0, {0.5, 1, 0}); |
| 110 |
auto v6 = find_closest_entity(mesh, 0, {1.0, 1, 0}); |
| 111 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.25, 0}); |
| 112 |
auto el2 = find_closest_entity(mesh, 2, {0.75, 0.75, 0}); |
| 113 |
|
| 114 |
|
| 115 |
expect_eq(get_values(gatheredDestField, v1, 0), {0, 2}); |
| 116 |
expect_eq(get_values(gatheredDestField, v2, 0), {2}); |
| 117 |
expect_eq(get_values(gatheredDestField, v3, 0), {0, 1, 2, 3}); |
| 118 |
expect_eq(get_values(gatheredDestField, v4, 0), {2, 3}); |
| 119 |
expect_eq(get_values(gatheredDestField, v5, 0), {1, 3}); |
| 120 |
expect_eq(get_values(gatheredDestField, v6, 0), {3}); |
| 121 |
|
| 122 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {2}); |
| 123 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {2}); |
| 124 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {2, 3}); |
| 125 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {0, 2}); |
| 126 |
|
| 127 |
expect_eq(get_values(gatheredDestField, el2->get_down(1), 0), {3}); |
| 128 |
expect_eq(get_values(gatheredDestField, el2->get_down(2), 0), {3}); |
| 129 |
expect_eq(get_values(gatheredDestField, el2->get_down(3), 0), {1, 3}); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
TEST(DestinationFieldSynchronizer, 4ProcsMultipleDestinations) |
| 134 |
{ |
| 135 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 4) |
| 136 |
GTEST_SKIP(); |
| 137 |
|
| 138 |
mesh::impl::MeshSpec spec; |
| 139 |
spec.xmin = 0; spec.ymin = 0; |
| 140 |
spec.xmax = 1; spec.ymax = 1; |
| 141 |
spec.numelX = 2; spec.numelY = 2; |
| 142 |
|
| 143 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 144 |
auto mesh = mesh::impl::create_mesh(spec, f); |
| 145 |
|
| 146 |
auto scatterSpec = std::make_shared<mesh::impl::MeshScatterSpec>(MPI_COMM_WORLD, mesh); |
| 147 |
int myrank = utils::impl::comm_rank(MPI_COMM_WORLD); |
| 148 |
if (myrank == 0) |
| 149 |
{ |
| 150 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.25}); |
| 151 |
scatterSpec->add_destination(el1, 0); |
| 152 |
scatterSpec->add_destination(el1, 1); |
| 153 |
} else if (myrank == 1) |
| 154 |
{ |
| 155 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.25}); |
| 156 |
scatterSpec->add_destination(el1, 1); |
| 157 |
scatterSpec->add_destination(el1, 2); |
| 158 |
} else if (myrank == 2) |
| 159 |
{ |
| 160 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.75}); |
| 161 |
scatterSpec->add_destination(el1, 2); |
| 162 |
scatterSpec->add_destination(el1, 3); |
| 163 |
} else if (myrank == 3) |
| 164 |
{ |
| 165 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.75}); |
| 166 |
scatterSpec->add_destination(el1, 3); |
| 167 |
scatterSpec->add_destination(el1, 1); |
| 168 |
} |
| 169 |
|
| 170 |
mesh::impl::DestinationFieldSynchronizer gatherer(mesh, scatterSpec); |
| 171 |
auto gatheredDestField = gatherer.synchronize(); |
| 172 |
|
| 173 |
if (myrank == 0) |
| 174 |
{ |
| 175 |
auto v1 = find_closest_entity(mesh, 0, {0, 0, 0}); |
| 176 |
auto v2 = find_closest_entity(mesh, 0, {0.5, 0, 0}); |
| 177 |
auto v3 = find_closest_entity(mesh, 0, {0, 0.5, 0}); |
| 178 |
auto v4 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 179 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.25, 0}); |
| 180 |
|
| 181 |
expect_eq(get_values(gatheredDestField, v1, 0), {0, 1}); |
| 182 |
expect_eq(get_values(gatheredDestField, v2, 0), {0, 1, 2}); |
| 183 |
expect_eq(get_values(gatheredDestField, v3, 0), {0, 1, 2, 3}); |
| 184 |
expect_eq(get_values(gatheredDestField, v4, 0), {0, 1, 2, 3}); |
| 185 |
|
| 186 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {0, 1}); |
| 187 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {0, 1, 2}); |
| 188 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {0, 1, 2, 3}); |
| 189 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {0, 1}); |
| 190 |
} else if (myrank == 1) |
| 191 |
{ |
| 192 |
auto v1 = find_closest_entity(mesh, 0, {0.5, 0, 0}); |
| 193 |
auto v2 = find_closest_entity(mesh, 0, {1.0, 0, 0}); |
| 194 |
auto v3 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 195 |
auto v4 = find_closest_entity(mesh, 0, {1.0, 0.5, 0}); |
| 196 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.25, 0}); |
| 197 |
|
| 198 |
expect_eq(get_values(gatheredDestField, v1, 0), {0, 1, 2}); |
| 199 |
expect_eq(get_values(gatheredDestField, v2, 0), {1, 2}); |
| 200 |
expect_eq(get_values(gatheredDestField, v3, 0), {0, 1, 2, 3}); |
| 201 |
expect_eq(get_values(gatheredDestField, v4, 0), {1, 2, 3}); |
| 202 |
|
| 203 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {1, 2}); |
| 204 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {1, 2}); |
| 205 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {1, 2, 3}); |
| 206 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {0, 1, 2}); |
| 207 |
} else if (myrank == 2) |
| 208 |
{ |
| 209 |
auto v1 = find_closest_entity(mesh, 0, {0.0, 0.5, 0}); |
| 210 |
auto v2 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 211 |
auto v3 = find_closest_entity(mesh, 0, {0.0, 1.0, 0}); |
| 212 |
auto v4 = find_closest_entity(mesh, 0, {0.5, 1.0, 0}); |
| 213 |
auto el1 = find_closest_entity(mesh, 2, {0.25, 0.75, 0}); |
| 214 |
|
| 215 |
expect_eq(get_values(gatheredDestField, v1, 0), {0, 1, 2, 3}); |
| 216 |
expect_eq(get_values(gatheredDestField, v2, 0), {0, 1, 2, 3}); |
| 217 |
expect_eq(get_values(gatheredDestField, v3, 0), {2, 3}); |
| 218 |
expect_eq(get_values(gatheredDestField, v4, 0), {1, 2, 3}); |
| 219 |
|
| 220 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {0, 1, 2, 3}); |
| 221 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {1, 2, 3}); |
| 222 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {2, 3}); |
| 223 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {2, 3}); |
| 224 |
} else if (myrank == 3) |
| 225 |
{ |
| 226 |
auto v1 = find_closest_entity(mesh, 0, {0.5, 0.5, 0}); |
| 227 |
auto v2 = find_closest_entity(mesh, 0, {1.0, 0.5, 0}); |
| 228 |
auto v3 = find_closest_entity(mesh, 0, {0.5, 1.0, 0}); |
| 229 |
auto v4 = find_closest_entity(mesh, 0, {1.0, 1.0, 0}); |
| 230 |
auto el1 = find_closest_entity(mesh, 2, {0.75, 0.75, 0}); |
| 231 |
|
| 232 |
expect_eq(get_values(gatheredDestField, v1, 0), {0, 1, 2, 3}); |
| 233 |
expect_eq(get_values(gatheredDestField, v2, 0), {1, 2, 3}); |
| 234 |
expect_eq(get_values(gatheredDestField, v3, 0), {1, 2, 3}); |
| 235 |
expect_eq(get_values(gatheredDestField, v4, 0), {1, 3}); |
| 236 |
|
| 237 |
expect_eq(get_values(gatheredDestField, el1->get_down(0), 0), {1, 2, 3}); |
| 238 |
expect_eq(get_values(gatheredDestField, el1->get_down(1), 0), {1, 3}); |
| 239 |
expect_eq(get_values(gatheredDestField, el1->get_down(2), 0), {1, 3}); |
| 240 |
expect_eq(get_values(gatheredDestField, el1->get_down(3), 0), {1, 2, 3}); |
| 241 |
} |
| 242 |
} |