| 1 |
// Copyright(C) 1999-2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "Ioss_CodeTypes.h" |
| 8 |
#include "Ioss_ConcreteVariableType.h" |
| 9 |
#include "Ioss_ElementPermutation.h" |
| 10 |
#include "Ioss_ElementTopology.h" |
| 11 |
#include "Ioss_Initializer.h" |
| 12 |
#include "Ioss_NullEntity.h" |
| 13 |
#include "Ioss_Utils.h" |
| 14 |
#include "Ioss_VariableType.h" |
| 15 |
|
| 16 |
#include <catch2/catch_session.hpp> |
| 17 |
#include <catch2/catch_test_macros.hpp> |
| 18 |
#include <fmt/core.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
// ======================================================================== |
| 25 |
namespace { |
| 26 |
void test_aliases(const Ioss::NameList &elements); |
| 27 |
void test_element(const std::string &type); |
| 28 |
} // namespace |
| 29 |
// ======================================================================== |
| 30 |
|
| 31 |
int main(IOSS_MAYBE_UNUSED int argc, char **argv) |
| 32 |
{ |
| 33 |
Ioss::StorageInitializer initialize_storage; |
| 34 |
Ioss::Initializer initialize_topologies; |
| 35 |
|
| 36 |
Catch::Session session; // There must be exactly one instance |
| 37 |
int returnCode = session.applyCommandLine(argc, argv); |
| 38 |
if (returnCode != 0) // Indicates a command line error |
| 39 |
return returnCode; |
| 40 |
|
| 41 |
auto ok = session.run() == 0; |
| 42 |
|
| 43 |
if (ok) { |
| 44 |
fmt::print(stderr, "\nSIERRA execution successful.\n"); |
| 45 |
return EXIT_SUCCESS; |
| 46 |
} |
| 47 |
fmt::print(stderr, "\nSIERRA execution failed.\n"); |
| 48 |
return EXIT_FAILURE; |
| 49 |
} |
| 50 |
|
| 51 |
TEST_CASE("nullentity") |
| 52 |
{ |
| 53 |
// Make sure Ioss::NullEntity works. Not used in IOSS itself, |
| 54 |
// but some clients use it, so need to make sure it compiles |
| 55 |
// correctly. |
| 56 |
auto entity = std::make_unique<Ioss::NullEntity>(); |
| 57 |
REQUIRE(entity); |
| 58 |
fmt::print(stderr, "\nThe null entity type is '{}' and it contains '{}'\n", entity->type_string(), |
| 59 |
entity->contains_string()); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_CASE("invalid element") |
| 63 |
{ |
| 64 |
// Check that asking for invalid element returns nullptr pointer. |
| 65 |
Ioss::ElementTopology *invalid = Ioss::ElementTopology::factory("Greg", true); |
| 66 |
CHECK(invalid == nullptr); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("test_all_elements") |
| 70 |
{ |
| 71 |
Ioss::NameList elements = Ioss::ElementTopology::describe(); |
| 72 |
|
| 73 |
for (const auto &element : elements) { |
| 74 |
DYNAMIC_SECTION(element) { test_element(element); } |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
TEST_CASE("test aliases") |
| 79 |
{ |
| 80 |
Ioss::NameList elements = Ioss::ElementTopology::describe(); |
| 81 |
test_aliases(elements); |
| 82 |
} |
| 83 |
|
| 84 |
namespace { |
| 85 |
void test_element(const std::string &type) |
| 86 |
{ |
| 87 |
// NOTE: For true test, should run with purify checking enabled to |
| 88 |
// ensure we are not running off the end of arrays... |
| 89 |
|
| 90 |
Ioss::ElementTopology *element = Ioss::ElementTopology::factory(type); |
| 91 |
REQUIRE(element != nullptr); |
| 92 |
|
| 93 |
// See if the name is an alias for another element (type != name()) |
| 94 |
|
| 95 |
// Check that name is alias for name... |
| 96 |
CHECK(element->is_alias(type)); |
| 97 |
|
| 98 |
// Output the 'shape' |
| 99 |
auto shape = Ioss::Utils::lowercase(Ioss::Utils::shape_to_string(element->shape())); |
| 100 |
|
| 101 |
std::string name = element->name(); |
| 102 |
fmt::print(stderr, "\n{:<25}({})\t[{}]", type, name, shape); |
| 103 |
|
| 104 |
// Check that master element name is an alias... |
| 105 |
CHECK(element->is_alias(element->master_element_name())); |
| 106 |
|
| 107 |
if (type == "unknown" || type == "invalid_topology") { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Check that the hash id method of selecting the element returns the correct element. |
| 112 |
unsigned int hash_val = Ioss::ElementTopology::get_unique_id(name); |
| 113 |
CHECK(Ioss::ElementTopology::factory(hash_val) == element); |
| 114 |
|
| 115 |
int order = element->order(); |
| 116 |
|
| 117 |
bool homo_edges = element->edges_similar(); |
| 118 |
bool homo_faces = element->faces_similar(); |
| 119 |
|
| 120 |
int nn = element->number_nodes(); |
| 121 |
CHECK(nn > 0); |
| 122 |
|
| 123 |
int ncn = element->number_corner_nodes(); |
| 124 |
CHECK(ncn > 0); |
| 125 |
CHECK(ncn <= nn); |
| 126 |
|
| 127 |
auto *perm = element->permutation(); |
| 128 |
REQUIRE(perm != nullptr); |
| 129 |
|
| 130 |
if (element->name() != "node") { |
| 131 |
REQUIRE(static_cast<int>(perm->num_permutation_nodes()) == element->number_corner_nodes()); |
| 132 |
// Element shape matches permutation type |
| 133 |
CHECK(Ioss::Utils::lowercase(Ioss::Utils::shape_to_string(element->shape())) == perm->type()); |
| 134 |
|
| 135 |
if (perm->num_permutations() > 0) { |
| 136 |
const auto &permutation = perm->permutation_indices(0); |
| 137 |
const auto connect = element->element_connectivity(); |
| 138 |
for (size_t i = 0; i < permutation.size(); i++) { |
| 139 |
// permutation matches element connectivity |
| 140 |
CHECK(permutation[i] == connect[i]); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
int ne = element->number_edges(); |
| 146 |
CHECK(ne >= 0); |
| 147 |
|
| 148 |
int nf = element->number_faces(); |
| 149 |
CHECK(nf >= 0); |
| 150 |
|
| 151 |
// Verify Euler's Formula holds... V-E+F=2 |
| 152 |
if (element->parametric_dimension() == 3) { |
| 153 |
int euler = ncn - ne + nf; |
| 154 |
CHECK(euler == 2); |
| 155 |
} |
| 156 |
|
| 157 |
int nne = element->number_nodes_edge(0); |
| 158 |
if (nne == -1) { |
| 159 |
CHECK(homo_edges == false); |
| 160 |
for (int edge = 1; edge <= ne; edge++) { |
| 161 |
int nnei = element->number_nodes_edge(edge); |
| 162 |
CHECK(nnei >= 0); |
| 163 |
CHECK(nnei <= nn); |
| 164 |
} |
| 165 |
} |
| 166 |
else { |
| 167 |
CHECK(nne >= 0); |
| 168 |
CHECK(nne <= nn); |
| 169 |
} |
| 170 |
|
| 171 |
int nnf = element->number_nodes_face(0); |
| 172 |
CHECK(nnf <= nn); |
| 173 |
CHECK(nnf >= -1); |
| 174 |
|
| 175 |
// Check boundary and other topologies... |
| 176 |
if (nf > 0) { |
| 177 |
Ioss::ElementTopology *face0 = element->face_type(0); |
| 178 |
if (homo_faces == false) { |
| 179 |
CHECK(face0 == nullptr); |
| 180 |
} |
| 181 |
else { |
| 182 |
CHECK(face0 != nullptr); |
| 183 |
} |
| 184 |
|
| 185 |
for (int i = 1; i <= nf; i++) { |
| 186 |
Ioss::ElementTopology *face = element->face_type(i); |
| 187 |
CHECK(face != nullptr); |
| 188 |
if (face != nullptr) { |
| 189 |
unsigned int nnfi = element->number_nodes_face(i); |
| 190 |
CHECK(nnfi == static_cast<unsigned int>(face->number_nodes())); |
| 191 |
std::vector<int> conn = element->face_connectivity(i); |
| 192 |
CHECK(nnfi == conn.size()); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// Edges... |
| 198 |
if (ne > 0) { |
| 199 |
Ioss::ElementTopology *edge0 = element->edge_type(0); |
| 200 |
if (homo_edges == false) { |
| 201 |
CHECK(edge0 == nullptr); |
| 202 |
} |
| 203 |
else { |
| 204 |
CHECK(edge0 != nullptr); |
| 205 |
} |
| 206 |
|
| 207 |
for (int i = 1; i <= ne; i++) { |
| 208 |
Ioss::ElementTopology *edge = element->edge_type(i); |
| 209 |
CHECK(edge != nullptr); |
| 210 |
if (edge != nullptr) { |
| 211 |
unsigned int nnei = element->number_nodes_edge(i); |
| 212 |
CHECK(nnei == static_cast<unsigned int>(edge->number_nodes())); |
| 213 |
std::vector<int> conn = element->edge_connectivity(i); |
| 214 |
CHECK(nnei == conn.size()); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
// Variable types... |
| 219 |
const Ioss::VariableType *vt = Ioss::VariableType::factory(element->name()); |
| 220 |
REQUIRE(vt != nullptr); |
| 221 |
// See if component counts match... |
| 222 |
int vt_comp = vt->component_count(); |
| 223 |
CHECK(nn == vt_comp); |
| 224 |
|
| 225 |
// For elements with parametric dimension == 3 |
| 226 |
// Get face-edge-order |
| 227 |
// Get face-node-connectivity |
| 228 |
// Foreach edge in face, get nodal connectivity |
| 229 |
// ensure that node is in face connectivity... |
| 230 |
// |
| 231 |
// For an edge: 1------3------2 |
| 232 |
// |
| 233 |
// For a face: Corner nodes are first in connectivity |
| 234 |
// Center nodes follow. |
| 235 |
// So: |
| 236 |
// |
| 237 |
// 2 x |
| 238 |
// 3----6----2 / \ x |
| 239 |
// | | / \ x |
| 240 |
// 7 5 5 4 x |
| 241 |
// | | / \ x |
| 242 |
// 0----4----1 0----3----1 x |
| 243 |
// |
| 244 |
if (element->parametric_dimension() == 3) { |
| 245 |
for (int i = 1; i <= nf; i++) { |
| 246 |
unsigned int fncn = element->face_type(i)->number_corner_nodes(); |
| 247 |
unsigned int num_edges_face = element->number_edges_face(i); |
| 248 |
CHECK(fncn == num_edges_face); |
| 249 |
|
| 250 |
// Nodes defining face... |
| 251 |
std::vector<int> face_conn = element->face_connectivity(i); |
| 252 |
|
| 253 |
// Edges defining face... |
| 254 |
std::vector<int> face_edge_conn = element->face_edge_connectivity(i); |
| 255 |
REQUIRE(num_edges_face == face_edge_conn.size()); |
| 256 |
for (unsigned int j = 0; j < num_edges_face; j++) { |
| 257 |
// Not implemented in all elements yet... |
| 258 |
std::vector<int> edge_conn = element->edge_connectivity(face_edge_conn[j] + 1); |
| 259 |
// Check that first two nodes in 'edge_conn' match |
| 260 |
// corresponding nodes in 'face_conn' |
| 261 |
bool ok = (edge_conn[0] == face_conn[j] || edge_conn[1] == face_conn[j]) && |
| 262 |
(edge_conn[0] == face_conn[(j + 1) % fncn] || |
| 263 |
edge_conn[1] == face_conn[(j + 1) % fncn]); |
| 264 |
CHECK(ok); |
| 265 |
|
| 266 |
if (element->name() == "wedge12" || element->name() == "hex16") { |
| 267 |
continue; |
| 268 |
} |
| 269 |
|
| 270 |
CHECK(edge_conn.size() == size_t(order) + 1); |
| 271 |
|
| 272 |
CHECK(order >= 1); |
| 273 |
CHECK(face_conn.size() >= fncn + (order - 1) * num_edges_face); |
| 274 |
if (order == 2) { |
| 275 |
CHECK(edge_conn[2] == face_conn[fncn + j]); |
| 276 |
} |
| 277 |
if (order == 3) { |
| 278 |
auto en2 = edge_conn[2]; |
| 279 |
auto en3 = edge_conn[3]; |
| 280 |
|
| 281 |
auto fn2 = face_conn[fncn + 2 * j]; |
| 282 |
auto fn3 = face_conn[fncn + 2 * j + 1]; |
| 283 |
// Mid-Side Node Edge Connectivity must match face connectivity |
| 284 |
CHECK((en2 == fn2 || en3 == fn2 || en2 == fn3 || en3 == fn3)); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
void test_aliases(const Ioss::NameList &elements) |
| 292 |
{ |
| 293 |
int count = elements.size(); |
| 294 |
fmt::print(stderr, "\n\nTesting Element Topology Aliases...\n"); |
| 295 |
|
| 296 |
for (int i = 0; i < count; i++) { |
| 297 |
Ioss::ElementTopology *el_top = Ioss::ElementTopology::factory(elements[i]); |
| 298 |
if (el_top->name() == elements[i]) { |
| 299 |
fmt::print(stderr, "Element: {:<25}({}) has the following aliases:\n\t", elements[i], |
| 300 |
el_top->master_element_name()); |
| 301 |
for (int j = 0; j < count; j++) { |
| 302 |
if (i != j && el_top->is_alias(elements[j])) { |
| 303 |
fmt::print(stderr, "{}, ", elements[j]); |
| 304 |
} |
| 305 |
} |
| 306 |
fmt::print(stderr, "\n"); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} // namespace |