| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include <iostream> |
| 4 |
|
| 5 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 6 |
#include "stk_middle_mesh/field.hpp" |
| 7 |
#include "stk_middle_mesh/mesh.hpp" |
| 8 |
|
| 9 |
namespace stk { |
| 10 |
namespace middle_mesh { |
| 11 |
namespace impl { |
| 12 |
|
| 13 |
using namespace mesh; |
| 14 |
using namespace mesh::impl; |
| 15 |
|
| 16 |
int get_value(const Field<int>& field, MeshEntityPtr e, const int node, const int comp) |
| 17 |
{ |
| 18 |
int dim = get_type_dimension(e->get_type()); |
| 19 |
int id = e->get_id(); |
| 20 |
return id * (field.get_num_nodes(dim) * field.get_num_comp()) + node * field.get_num_comp() + comp; |
| 21 |
} |
| 22 |
|
| 23 |
void test_field(std::shared_ptr<Mesh> mesh, const FieldShape& fshape, Field<int>& field) |
| 24 |
{ |
| 25 |
// test getting and setting values |
| 26 |
for (int dim = 0; dim < 3; ++dim) |
| 27 |
for (auto& e : mesh->get_mesh_entities(dim)) |
| 28 |
for (int node = 0; node < fshape.count[0]; ++node) |
| 29 |
for (int comp = 0; comp < field.get_num_comp(); ++comp) |
| 30 |
field(e, node, comp) = get_value(field, e, node, comp); |
| 31 |
|
| 32 |
for (int dim = 0; dim < 3; ++dim) |
| 33 |
for (auto& e : mesh->get_mesh_entities(dim)) |
| 34 |
for (int node = 0; node < fshape.count[0]; ++node) |
| 35 |
for (int comp = 0; comp < field.get_num_comp(); ++comp) |
| 36 |
EXPECT_EQ(field(e, node, comp), get_value(field, e, node, comp)); |
| 37 |
} |
| 38 |
|
| 39 |
TEST(FieldShape, operators) |
| 40 |
{ |
| 41 |
EXPECT_TRUE(FieldShape(1, 1, 1) == FieldShape(1, 1, 1)); |
| 42 |
EXPECT_FALSE(FieldShape(1, 1, 0) == FieldShape(1, 1, 1)); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(Field, Values) |
| 46 |
{ |
| 47 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 48 |
GTEST_SKIP(); |
| 49 |
|
| 50 |
MeshSpec spec; |
| 51 |
spec.numelX = 5; |
| 52 |
spec.numelY = 5; |
| 53 |
spec.xmin = 0; |
| 54 |
spec.xmax = 1; |
| 55 |
spec.ymin = 0; |
| 56 |
spec.ymax = 1; |
| 57 |
|
| 58 |
auto func = [&](const utils::Point& pt) { return pt; }; |
| 59 |
|
| 60 |
std::shared_ptr<Mesh> mesh = create_mesh(spec, func); |
| 61 |
|
| 62 |
FieldShape fshape(2, 3, 4); |
| 63 |
auto fieldPtr = create_field<int>(mesh, fshape, 5, -1); |
| 64 |
Field<int>& field = *fieldPtr; |
| 65 |
|
| 66 |
// test initial value |
| 67 |
for (int dim = 0; dim < 3; ++dim) |
| 68 |
for (auto& e : mesh->get_mesh_entities(dim)) |
| 69 |
for (int node = 0; node < fshape.count[0]; ++node) |
| 70 |
for (int comp = 0; comp < field.get_num_comp(); ++comp) |
| 71 |
EXPECT_EQ(field(e, node, comp), -1); |
| 72 |
|
| 73 |
test_field(mesh, fshape, field); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(Field, Grow) |
| 77 |
{ |
| 78 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 1) |
| 79 |
GTEST_SKIP(); |
| 80 |
|
| 81 |
int nelemX = 4, nelemY = 0; |
| 82 |
double xmin = 0, xmax = 1, ymin = 0, ymax = 1; |
| 83 |
double dx = (xmax - xmin) / nelemX; |
| 84 |
double dy = (ymax - ymin) / nelemY; |
| 85 |
std::vector<MeshEntityPtr> verts((nelemX + 1) * (nelemY + 1)); |
| 86 |
|
| 87 |
auto getIdx = [nelemY](const int i, const int j) { return j + i * (nelemY + 1); }; |
| 88 |
|
| 89 |
auto mesh = make_empty_mesh(); |
| 90 |
FieldShape fshape(2, 3, 4); |
| 91 |
auto fieldPtr = create_field<int>(mesh, fshape, 5, -1); |
| 92 |
Field<int>& field = *fieldPtr; |
| 93 |
|
| 94 |
for (int i = 0; i < (nelemX + 1); ++i) |
| 95 |
for (int j = 0; j < (nelemY + 1); ++j) |
| 96 |
verts[getIdx(i, j)] = mesh->create_vertex(xmin + i * dx, ymin + j * dy); |
| 97 |
|
| 98 |
for (int i = 0; i < nelemX; ++i) |
| 99 |
for (int j = 0; j < nelemY; ++j) |
| 100 |
{ |
| 101 |
auto v1 = verts[getIdx(i, j)]; |
| 102 |
auto v2 = verts[getIdx(i + 1, j)]; |
| 103 |
auto v3 = verts[getIdx(i + 1, j + 1)]; |
| 104 |
auto v4 = verts[getIdx(i, j + 1)]; |
| 105 |
mesh->create_quad_from_verts(v1, v2, v3, v4); |
| 106 |
} |
| 107 |
|
| 108 |
test_field(mesh, fshape, field); |
| 109 |
} |
| 110 |
|
| 111 |
} // namespace impl |
| 112 |
} // namespace middle_mesh |
| 113 |
} // namespace stk |