Skip to content

Commit

Permalink
Merge pull request #844 from mtao/mtao/autogen_tests
Browse files Browse the repository at this point in the history
Testing tuple_from_id
  • Loading branch information
mtao authored Dec 6, 2024
2 parents 574cfec + b1dc298 commit 22b78e8
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 40 deletions.
26 changes: 5 additions & 21 deletions src/wmtk/TetMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <wmtk/utils/tetmesh_topology_initialization.h>
#include <numeric>
#include <wmtk/autogen/tet_mesh/get_tuple_from_simplex_local_id.hpp>
#include <wmtk/autogen/tet_mesh/is_ccw.hpp>
#include <wmtk/autogen/tet_mesh/local_switch_tuple.hpp>
#include <wmtk/simplex/SimplexCollection.hpp>
Expand Down Expand Up @@ -162,13 +163,7 @@ Tuple TetMesh::vertex_tuple_from_id(int64_t id) const
}
}

const auto [nlvid, leid, lfid] = autogen::tet_mesh::auto_3d_table_complete_vertex[lvid];
assert(lvid == nlvid);

if (lvid < 0 || leid < 0 || lfid < 0) throw std::runtime_error("vertex_tuple_from_id failed");

Tuple v_tuple = Tuple(lvid, leid, lfid, t);
assert(is_ccw(v_tuple));
Tuple v_tuple = autogen::tet_mesh::get_tuple_from_simplex_local_vertex_id(lvid, t);
assert(is_valid(v_tuple));
return v_tuple;
}
Expand All @@ -186,14 +181,7 @@ Tuple TetMesh::edge_tuple_from_id(int64_t id) const
break;
}
}
const auto [lvid, nleid, lfid] = autogen::tet_mesh::auto_3d_table_complete_edge[leid];
assert(leid == nleid);


if (lvid < 0 || leid < 0 || lfid < 0) throw std::runtime_error("edge_tuple_from_id failed");

Tuple e_tuple = Tuple(lvid, leid, lfid, t);
assert(is_ccw(e_tuple));
Tuple e_tuple = autogen::tet_mesh::get_tuple_from_simplex_local_edge_id(leid, t);
assert(is_valid(e_tuple));
return e_tuple;
}
Expand All @@ -212,13 +200,9 @@ Tuple TetMesh::face_tuple_from_id(int64_t id) const
}
}

const auto [lvid, leid, nlfid] = autogen::tet_mesh::auto_3d_table_complete_face[lfid];
assert(lfid == nlfid);

if (lvid < 0 || leid < 0 || lfid < 0) throw std::runtime_error("face_tuple_from_id failed");

Tuple f_tuple = Tuple(lvid, leid, lfid, t);
assert(is_ccw(f_tuple));
assert(lfid >= 0);
Tuple f_tuple = autogen::tet_mesh::get_tuple_from_simplex_local_face_id(lfid, t);
assert(is_valid(f_tuple));
return f_tuple;
}
Expand Down
17 changes: 3 additions & 14 deletions src/wmtk/TriMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <wmtk/autogen/tri_mesh/is_ccw.hpp>
#include <wmtk/autogen/tri_mesh/local_switch_tuple.hpp>
#include <wmtk/utils/Logger.hpp>
#include "wmtk/autogen/tri_mesh/get_tuple_from_simplex_local_id.hpp"

namespace wmtk {

Expand Down Expand Up @@ -291,12 +292,7 @@ Tuple TriMesh::vertex_tuple_from_id(int64_t id) const
auto fv = m_fv_accessor->index_access().const_vector_attribute<3>(f);
for (int64_t i = 0; i < 3; ++i) {
if (fv(i) == id) {
assert(autogen::tri_mesh::auto_2d_table_complete_vertex[i][0] == i);
const int64_t leid = autogen::tri_mesh::auto_2d_table_complete_vertex[i][1];
Tuple v_tuple = Tuple(i, leid, -1, f);
// accessor as parameter
assert(is_ccw(v_tuple)); // is_ccw also checks for validity
return v_tuple;
return autogen::tri_mesh::get_tuple_from_simplex_local_vertex_id(i, f);
}
}
assert(false); // "vertex_tuple_from_id failed"
Expand All @@ -310,14 +306,7 @@ Tuple TriMesh::edge_tuple_from_id(int64_t id) const
auto fe = m_fe_accessor->index_access().const_vector_attribute<3>(f);
for (int64_t i = 0; i < 3; ++i) {
if (fe(i) == id) {
assert(autogen::tri_mesh::auto_2d_table_complete_edge[i][1] == i);
const int64_t lvid = autogen::tri_mesh::auto_2d_table_complete_edge[i][0];


Tuple e_tuple = Tuple(lvid, i, -1, f);
assert(is_ccw(e_tuple));
assert(is_valid(e_tuple));
return e_tuple;
return autogen::tri_mesh::get_tuple_from_simplex_local_edge_id(i, f);
}
}
assert(false); // "edge_tuple_from_id failed"
Expand Down
2 changes: 2 additions & 0 deletions src/wmtk/autogen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ foreach(MESH_TYPE edge_mesh tri_mesh tet_mesh)

subdart_maximal_action_to_face.hpp
subdart_maximal_action_to_face.cpp

get_tuple_from_simplex_local_id.hpp
)
set(SRC_FILES ${SRC_FILES} ${MESH_TYPE}/${FILE_NAME})
endforeach()
Expand Down
28 changes: 28 additions & 0 deletions src/wmtk/autogen/edge_mesh/get_tuple_from_simplex_local_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <cassert>
#include <wmtk/Tuple.hpp>
#include "autogenerated_tables.hpp"
#if !defined(_NDEBUG)
#include "is_ccw.hpp"
#endif

namespace wmtk::autogen::edge_mesh {

inline Tuple get_tuple_from_simplex_local_vertex_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 2);
return Tuple(local_id, -1, -1, global_id);
}
inline Tuple
get_tuple_from_simplex_local_id(PrimitiveType pt, int8_t local_id, int64_t global_fid)
{
switch (pt) {
case PrimitiveType::Vertex: return get_tuple_from_simplex_local_vertex_id(local_id, global_fid);
case PrimitiveType::Edge:
case PrimitiveType::Triangle:
default:
case PrimitiveType::Tetrahedron: assert(false); return {};
}
}
} // namespace wmtk::autogen::edge_mesh
48 changes: 48 additions & 0 deletions src/wmtk/autogen/tet_mesh/get_tuple_from_simplex_local_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include <cassert>
#include <wmtk/Tuple.hpp>
#include "autogenerated_tables.hpp"
#if !defined(_NDEBUG)
#include "is_ccw.hpp"
#endif

namespace wmtk::autogen::tet_mesh {

inline Tuple get_tuple_from_simplex_local_vertex_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 4);
const auto& arr = autogen::tet_mesh::auto_3d_table_complete_vertex[local_id];
const auto& [lvid, leid, lfid] = arr;
Tuple tuple = Tuple(lvid, leid, lfid, global_id);
return tuple;
}
inline Tuple get_tuple_from_simplex_local_edge_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 6);
const auto& arr = autogen::tet_mesh::auto_3d_table_complete_edge[local_id];
const auto& [lvid, leid, lfid] = arr;
Tuple tuple = Tuple(lvid, leid, lfid, global_id);
return tuple;
}
inline Tuple get_tuple_from_simplex_local_face_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 4);
const auto& arr = autogen::tet_mesh::auto_3d_table_complete_face[local_id];
const auto& [lvid, leid, lfid] = arr;
Tuple tuple = Tuple(lvid, leid, lfid, global_id);
return tuple;
}
inline Tuple get_tuple_from_simplex_local_id(PrimitiveType pt, int8_t local_id, int64_t global_fid)
{
switch (pt) {
case PrimitiveType::Vertex: return get_tuple_from_simplex_local_vertex_id(local_id, global_fid);
case PrimitiveType::Edge: return get_tuple_from_simplex_local_edge_id(local_id, global_fid);
case PrimitiveType::Triangle: return get_tuple_from_simplex_local_face_id(local_id, global_fid);
default:
case PrimitiveType::Tetrahedron: assert(false); return {};
}
}
} // namespace wmtk::autogen::tet_mesh
39 changes: 39 additions & 0 deletions src/wmtk/autogen/tri_mesh/get_tuple_from_simplex_local_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <cassert>
#include <wmtk/Tuple.hpp>
#include "autogenerated_tables.hpp"
#if !defined(_NDEBUG)
#include "is_ccw.hpp"
#endif

namespace wmtk::autogen::tri_mesh {
inline Tuple get_tuple_from_simplex_local_vertex_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 3);

const auto& arr = autogen::tri_mesh::auto_2d_table_complete_vertex[local_id];
const auto& [lvid, leid] = arr;
Tuple tuple = Tuple(lvid, leid, -1, global_id);
return tuple;
}
inline Tuple get_tuple_from_simplex_local_edge_id(int8_t local_id, int64_t global_id)
{
assert(local_id >= 0);
assert(local_id < 3);
const auto& arr = autogen::tri_mesh::auto_2d_table_complete_edge[local_id];
const auto& [lvid, leid] = arr;
Tuple tuple = Tuple(lvid, leid, -1, global_id);
return tuple;
}
inline Tuple get_tuple_from_simplex_local_id(PrimitiveType pt, int8_t local_id, int64_t global_fid)
{
switch (pt) {
case PrimitiveType::Vertex: return get_tuple_from_simplex_local_vertex_id(local_id, global_fid);
case PrimitiveType::Edge: return get_tuple_from_simplex_local_edge_id(local_id, global_fid);
default:
case PrimitiveType::Triangle:
case PrimitiveType::Tetrahedron: assert(false); return {};
}
}
} // namespace wmtk::autogen::tri_mesh
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ target_link_libraries(wmtk_test_tools PUBLIC
set(TEST_SOURCES
test_topology.cpp
test_mesh.cpp
test_autogen.cpp
test_tuple.cpp
test_tuple_1d.cpp
test_tuple_2d.cpp
Expand Down
1 change: 1 addition & 0 deletions tests/autogen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
set(TEST_SOURCES
dart.cpp
actions.cpp
tables.cpp
)
target_sources(wmtk_tests PRIVATE ${TEST_SOURCES})
126 changes: 122 additions & 4 deletions tests/test_autogen.cpp → tests/autogen/tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@
#include <iostream>
#include <tuple>
#include <wmtk/Tuple.hpp>
#include <wmtk/autogen/edge_mesh/get_tuple_from_simplex_local_id.hpp>
#include <wmtk/autogen/edge_mesh/is_ccw.hpp>
#include <wmtk/autogen/edge_mesh/local_switch_tuple.hpp>
#include <wmtk/autogen/is_ccw.hpp>
#include <wmtk/autogen/local_switch_tuple.hpp>
#include <wmtk/autogen/tet_mesh/autogenerated_tables.hpp>
#include <wmtk/autogen/tet_mesh/get_tuple_from_simplex_local_id.hpp>
#include <wmtk/autogen/tet_mesh/is_ccw.hpp>
#include <wmtk/autogen/tet_mesh/local_id_table_offset.hpp>
#include <wmtk/autogen/tet_mesh/local_switch_tuple.hpp>
#include <wmtk/autogen/tri_mesh/autogenerated_tables.hpp>
#include <wmtk/autogen/tri_mesh/get_tuple_from_simplex_local_id.hpp>
#include <wmtk/autogen/tri_mesh/is_ccw.hpp>
#include <wmtk/autogen/tri_mesh/local_id_table_offset.hpp>
#include <wmtk/autogen/tri_mesh/local_switch_tuple.hpp>
#include "tools/all_valid_local_tuples.hpp"
#include "../tools/DEBUG_Tuple.hpp"
#include "../tools/all_valid_local_tuples.hpp"

using namespace wmtk;
using namespace wmtk::autogen;
using namespace wmtk::tests;

namespace {

template <typename T>
auto get_array_range(const T& array) -> std::array<decltype(&array[0]), 2>
{
return std::array<decltype(&array[0]), 2>{{array, array + std::size(array)}};
}
} // namespace

TEST_CASE("tuple_autogen_sizes", "[tuple]")
{
Expand All @@ -32,9 +44,6 @@ TEST_CASE("tuple_autogen_sizes", "[tuple]")
REQUIRE(all_valid_local_tuples(PrimitiveType::Triangle).size() == valid_face);
REQUIRE(all_valid_local_tuples(PrimitiveType::Tetrahedron).size() == valid_tet);

auto get_array_range = [](const auto& array) -> std::array<decltype(&array[0]), 2> {
return std::array<decltype(&array[0]), 2>{{array, array + std::size(array)}};
};
{ // ccw check
{ // tri
auto ccw_range = get_array_range(tri_mesh::auto_2d_table_ccw);
Expand Down Expand Up @@ -222,3 +231,112 @@ TEST_CASE("tuple_autogen_switch_still_valid", "[tuple]")
}
}
}
TEST_CASE("tuple_autogen_is_ccw", "[tuple]")
{
for (const auto& ve : tri_mesh::auto_2d_table_complete_vertex) {
wmtk::Tuple t(ve[0], ve[1], -1, 0);
CHECK(tri_mesh::tuple_is_valid_for_ccw(t));
CHECK(tri_mesh::is_ccw(t));
CHECK(tuple_is_valid_for_ccw(PrimitiveType::Triangle, t));
CHECK(is_ccw(PrimitiveType::Triangle, t));
}
for (const auto& ve : tri_mesh::auto_2d_table_complete_edge) {
wmtk::Tuple t(ve[0], ve[1], -1, 0);
CHECK(tri_mesh::tuple_is_valid_for_ccw(t));
CHECK(tri_mesh::is_ccw(t));
CHECK(tuple_is_valid_for_ccw(PrimitiveType::Triangle, t));
CHECK(is_ccw(PrimitiveType::Triangle, t));
}
for (const auto& ve : tet_mesh::auto_3d_table_complete_vertex) {
wmtk::Tuple t(ve[0], ve[1], ve[2], 0);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
CHECK(tet_mesh::is_ccw(t));
CHECK(tuple_is_valid_for_ccw(PrimitiveType::Tetrahedron, t));
CHECK(is_ccw(PrimitiveType::Tetrahedron, t));
}
for (const auto& ve : tet_mesh::auto_3d_table_complete_edge) {
wmtk::Tuple t(ve[0], ve[1], ve[2], 0);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
CHECK(tet_mesh::is_ccw(t));
CHECK(tuple_is_valid_for_ccw(PrimitiveType::Tetrahedron, t));
CHECK(is_ccw(PrimitiveType::Tetrahedron, t));
}
for (const auto& ve : tet_mesh::auto_3d_table_complete_face) {
wmtk::Tuple t(ve[0], ve[1], ve[2], 0);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
CHECK(tet_mesh::is_ccw(t));
CHECK(tuple_is_valid_for_ccw(PrimitiveType::Tetrahedron, t));
CHECK(is_ccw(PrimitiveType::Tetrahedron, t));
}
}
TEST_CASE("tuple_autogen_from_id_is_ccw", "[tuple]")
{
auto run_checks = [](int ic, const Tuple& t, const Tuple& t2, int i, PrimitiveType pt) {
wmtk::tests::DEBUG_Tuple dt(t);
switch (ic) {
case 4: REQUIRE(dt.local_fid() >= 0); REQUIRE(dt.local_fid() < 4);
case 3: REQUIRE(dt.local_eid() >= 0); REQUIRE(dt.local_eid() < 6);
case 2: REQUIRE(dt.local_vid() >= 0); REQUIRE(dt.local_vid() < 4);
default: break;
}
switch (pt) {
case PrimitiveType::Tetrahedron: CHECK(dt.local_fid() == i); break;
case PrimitiveType::Edge: CHECK(dt.local_eid() == i); break;
case PrimitiveType::Vertex: CHECK(dt.local_vid() == i); break;
default: break;
}

CHECK(t == t2);
};
PrimitiveType pt = PrimitiveType::Vertex;
for (int i = 0; i < 2; ++i) {
Tuple t = wmtk::autogen::edge_mesh::get_tuple_from_simplex_local_vertex_id(i, 0);
Tuple t2 = wmtk::autogen::edge_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
run_checks(2, t, t2, i, pt);
// not every simplex has a ccw tuple in an edge_mesh
CHECK(edge_mesh::tuple_is_valid_for_ccw(t));
CHECK(edge_mesh::is_ccw(t) == (i == 0));
}
pt = PrimitiveType::Vertex;
for (int i = 0; i < 3; ++i) {
Tuple t = wmtk::autogen::tri_mesh::get_tuple_from_simplex_local_vertex_id(i, 0);
Tuple t2 = wmtk::autogen::tri_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
run_checks(3, t, t2, i, pt);
CHECK(tri_mesh::tuple_is_valid_for_ccw(t));
CHECK(tri_mesh::is_ccw(t));
}
pt = PrimitiveType::Edge;
for (int i = 0; i < 3; ++i) {
Tuple t = wmtk::autogen::tri_mesh::get_tuple_from_simplex_local_edge_id(i, 0);
Tuple t2 = wmtk::autogen::tri_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
run_checks(3, t, t2, i, pt);
CHECK(tri_mesh::tuple_is_valid_for_ccw(t));
CHECK(tri_mesh::is_ccw(t));
}

pt = PrimitiveType::Vertex;
for (int i = 0; i < 4; ++i) {
Tuple t = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_vertex_id(i, 0);
Tuple t2 = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
run_checks(4, t, t2, i, pt);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
CHECK(tet_mesh::is_ccw(t));
}
pt = PrimitiveType::Edge;
for (int i = 0; i < 6; ++i) {
Tuple t = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_edge_id(i, 0);
Tuple t2 = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
run_checks(4, t, t2, i, pt);
CHECK(tet_mesh::is_ccw(t));
}

pt = PrimitiveType::Triangle;
for (int i = 0; i < 4; ++i) {
Tuple t = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_face_id(i, 0);
Tuple t2 = wmtk::autogen::tet_mesh::get_tuple_from_simplex_local_id(pt, i, 0);
CHECK(tet_mesh::tuple_is_valid_for_ccw(t));
run_checks(4, t, t2, i, pt);
CHECK(tet_mesh::is_ccw(t));
}
}

0 comments on commit 22b78e8

Please sign in to comment.