| 1 |
#include "gtest/gtest.h" |
| 2 |
#include "stk_middle_mesh/mesh.hpp" |
| 3 |
#include "stk_middle_mesh/field.hpp" |
| 4 |
#include "stk_middle_mesh/variable_size_field.hpp" |
| 5 |
|
| 6 |
using namespace stk::middle_mesh::mesh; |
| 7 |
|
| 8 |
//BEGIN_FIELD_ACCESS |
| 9 |
void foo(std::shared_ptr<Mesh> mesh, FieldPtr<double> fieldPtr) |
| 10 |
{ |
| 11 |
auto& field = *fieldPtr; |
| 12 |
for (MeshEntityPtr vert : mesh->get_vertices()) |
| 13 |
if (vert) |
| 14 |
{ |
| 15 |
double val = field(vert, 0, 0); |
| 16 |
// do something with val |
| 17 |
std::cout << "field value for vert " << vert << " = " << val << std::endl; |
| 18 |
} |
| 19 |
} |
| 20 |
//END_FIELD_ACCESS |
| 21 |
|
| 22 |
|
| 23 |
TEST(Mesh, CreateMesh) |
| 24 |
{ |
| 25 |
//BEGIN_CREATE_MESH |
| 26 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 27 |
std::shared_ptr<Mesh> mesh = make_empty_mesh(comm); |
| 28 |
//END_CREATE_MESH |
| 29 |
|
| 30 |
//BEGIN_CREATE_VERTS |
| 31 |
MeshEntityPtr v1 = mesh->create_vertex(0, 0, 0); |
| 32 |
MeshEntityPtr v2 = mesh->create_vertex(1, 0, 0); |
| 33 |
MeshEntityPtr v3 = mesh->create_vertex(0, 1, 0); |
| 34 |
//END_CREATE_VERTS |
| 35 |
|
| 36 |
|
| 37 |
//BEGIN_CREATE_EDGES |
| 38 |
MeshEntityPtr edge1 = mesh->create_edge(v1, v2); |
| 39 |
MeshEntityPtr edge2 = mesh->create_edge(v2, v3); |
| 40 |
MeshEntityPtr edge3 = mesh->create_edge(v3, v1); |
| 41 |
//END_CREATE_EDGES |
| 42 |
|
| 43 |
|
| 44 |
//BEGIN_CREATE_TRI_FROM_EDGES |
| 45 |
MeshEntityPtr tri = mesh->create_triangle(edge1, edge2, edge3, EntityOrientation::Standard); |
| 46 |
//END_CREATE_TRI_FROM_EDGES |
| 47 |
|
| 48 |
int dim = 0; |
| 49 |
//BEGIN_MESH_ITERATION_FUNCTIONS |
| 50 |
mesh->get_vertices(); |
| 51 |
mesh->get_edges(); |
| 52 |
mesh->get_elements(); |
| 53 |
mesh->get_mesh_entities(dim); // returns same as one of the above functions |
| 54 |
// depending on dim |
| 55 |
//END_MESH_ITERATION_FUNCTIONS |
| 56 |
|
| 57 |
//BEGIN_ITERATE_MESH |
| 58 |
for (MeshEntityPtr vert : mesh->get_vertices()) |
| 59 |
if (vert) |
| 60 |
{ |
| 61 |
// do stuff with vert |
| 62 |
} |
| 63 |
//END_ITERATE_MESH |
| 64 |
|
| 65 |
int i = 0; |
| 66 |
//BEGIN_MESH_ENTITY_FUNCTIONS |
| 67 |
MeshEntityType type = edge1->get_type(); // returns enum telling if this entity is a vert, |
| 68 |
// edge, tri, or quad |
| 69 |
int localId = edge1->get_id(); // returns the local ID of this entity |
| 70 |
MeshEntityPtr vert = edge1->get_down(i); // returns the i'th downward adjacent entity |
| 71 |
int numVerts = edge1->count_down(); // returns the number of downward adjacent |
| 72 |
// entities |
| 73 |
MeshEntityPtr triUp = edge1->get_up(i); // returns the i'th upward adjacent entity |
| 74 |
int numEls = edge1->count_up(); // returns the number of upward adjacent entities |
| 75 |
//END_MESH_ENTITY_FUNCTIONS |
| 76 |
EXPECT_EQ(type, MeshEntityType::Edge); |
| 77 |
EXPECT_EQ(localId, 0); |
| 78 |
EXPECT_EQ(vert->get_id(), 0); |
| 79 |
EXPECT_EQ(numVerts, 2); |
| 80 |
EXPECT_EQ(triUp->get_id(), 0); |
| 81 |
EXPECT_EQ(numEls, 1); |
| 82 |
|
| 83 |
int dimEl = 2; |
| 84 |
//BEGIN_ADJACENCY_FUNCTIONS |
| 85 |
std::array<MeshEntityPtr, MAX_DOWN> downEntities; |
| 86 |
int numDown = get_downward(tri, dim, downEntities.data()); |
| 87 |
|
| 88 |
std::vector<MeshEntityPtr> upEntities; |
| 89 |
int numUp = get_upward(v1, dimEl, upEntities); |
| 90 |
//END_ADJACENCY_FUNCTIONS |
| 91 |
EXPECT_EQ(numDown, 3); |
| 92 |
EXPECT_EQ(numUp, 1); |
| 93 |
|
| 94 |
int remoteRank = 0, remoteId = 0; |
| 95 |
//BEGIN_REMOTE_ENTITY_FUNCTIONS |
| 96 |
// registers that the given `RemoteSharedEntity` represents the same mesh entity as this one |
| 97 |
v1->add_remote_shared_entity(RemoteSharedEntity{remoteRank, remoteId}); |
| 98 |
|
| 99 |
// returns the i'th `RemoteSharedEntity` |
| 100 |
const RemoteSharedEntity& remote = v1->get_remote_shared_entity(i); |
| 101 |
|
| 102 |
// returns the number of remote shared entities |
| 103 |
int numRemotes = v1->count_remote_shared_entities(); |
| 104 |
//END_REMOTE_ENTITY_FUNCTIONS |
| 105 |
EXPECT_EQ(remote.remoteRank, 0); |
| 106 |
EXPECT_EQ(remote.remoteRank, 0); |
| 107 |
EXPECT_EQ(numRemotes, 1); |
| 108 |
|
| 109 |
{ |
| 110 |
//BEGIN_REMOTE_ENTITY_FREE_FUNCTIONS |
| 111 |
int ownerRank = get_owner(mesh, v1); |
| 112 |
RemoteSharedEntity remote2 = get_remote_shared_entity(v1, remoteRank); |
| 113 |
//END_REMOTE_ENTITY_FREE_FUNCTIONS |
| 114 |
EXPECT_EQ(ownerRank, 0); |
| 115 |
EXPECT_EQ(remote2.remoteRank, remoteRank); |
| 116 |
} |
| 117 |
|
| 118 |
int componentsPerNode = 1; |
| 119 |
double init = 0; |
| 120 |
//BEGIN_FIELD_CREATION |
| 121 |
FieldPtr<double> field = create_field<double>(mesh, FieldShape(1, 0, 0), componentsPerNode, init); |
| 122 |
//END_FIELD_CREATION |
| 123 |
|
| 124 |
|
| 125 |
foo(mesh, field); |
| 126 |
|
| 127 |
|
| 128 |
//BEGIN_VARIABLE_FIELD_CREATION |
| 129 |
VariableSizeFieldPtr<double> variField = create_variable_size_field<double>(mesh, FieldShape(1, 0, 0)); |
| 130 |
//END_VARIABLE_FIELD_CREATION |
| 131 |
|
| 132 |
|
| 133 |
int node = 0; |
| 134 |
double val = 0; |
| 135 |
{ |
| 136 |
//BEGIN_VARIABLE_FIELD_INSERT |
| 137 |
variField->insert(v1, node, val); |
| 138 |
//END_VARIABLE_FIELD_INSERT |
| 139 |
} |
| 140 |
|
| 141 |
{ |
| 142 |
//BEGIN_VARIABLE_FIELD_NUM_COMP |
| 143 |
int numComp = variField->get_num_comp(v1, node); |
| 144 |
//END_VARIABLE_FIELD_NUM_COMP |
| 145 |
EXPECT_EQ(numComp, 1); |
| 146 |
} |
| 147 |
|
| 148 |
{ |
| 149 |
int comp = 0; |
| 150 |
//BEGIN_VARIABLE_FIELD_ACCESS |
| 151 |
auto& variFieldRef = *field; |
| 152 |
double val2 = variFieldRef(v1, node, comp); |
| 153 |
//END_VARIABLE_FIELD_ACCESS |
| 154 |
EXPECT_EQ(val2, 0); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
TEST(Mesh, TriFromVerts) |
| 160 |
{ |
| 161 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 162 |
std::shared_ptr<Mesh> mesh = make_empty_mesh(comm); |
| 163 |
|
| 164 |
MeshEntityPtr v1 = mesh->create_vertex(0, 0, 0); |
| 165 |
MeshEntityPtr v2 = mesh->create_vertex(1, 0, 0); |
| 166 |
MeshEntityPtr v3 = mesh->create_vertex(0, 1, 0); |
| 167 |
|
| 168 |
//BEGIN_CREATE_TRI_FROM_VERTS |
| 169 |
MeshEntityPtr tri = mesh->create_triangle_from_verts(v1, v2, v3); |
| 170 |
//END_CREATE_TRI_FROM_VERTS |
| 171 |
EXPECT_EQ(tri->get_id(), 0); |
| 172 |
|
| 173 |
{ |
| 174 |
//BEGIN_FIELD2_CREATION |
| 175 |
FieldPtr<double> field = create_field<double>(mesh, FieldShape(0, 0, 3), 5); |
| 176 |
//END_FIELD2_CREATION |
| 177 |
} |
| 178 |
} |