Wildmeshing Toolkit
get_attributes.cpp
Go to the documentation of this file.
1 #include <fmt/ranges.h>
2 #include <catch2/catch_test_macros.hpp>
3 #include <nlohmann/json.hpp>
4 #include <wmtk/Mesh.hpp>
11 #include <wmtk/utils/Logger.hpp>
13 
14 #include "utils.hpp"
15 
18 
20 namespace {
21 // hack to deal with constructor ambiguity in the AD class
22 auto make_AD = [](const std::string_view& name,
23  const std::optional<uint8_t>& dim,
24  const std::optional<AT>& at) { return AD{name, dim, at}; };
25 
26 } // namespace
27 
28 TEST_CASE("multimesh_attribute_description_json", "[components][multimesh]")
29 {
32  using JS = nlohmann::json;
33  auto check = [](const AD& ad, const JS& js) {
34  JS js2 = ad;
35  auto ad2 = js.get<AD>();
36  CHECK(js2 == js);
37  CHECK(ad == js.get<AD>());
38  };
39  {
40  AD ad{"double_test", 0, AT::Double};
41  JS js{{"path", "double_test"}, {"dimension", 0}, {"type", "double"}};
42  check(ad, js);
43  }
44  {
45  AD ad{"rational_test", 0, AT::Rational};
46  JS js{{"path", "rational_test"}, {"dimension", 0}, {"type", "rational"}};
47  check(ad, js);
48  }
49  {
50  AD ad{"int_test", 0, AT::Int64};
51  JS js{{"path", "int_test"}, {"dimension", 0}, {"type", "int"}};
52  check(ad, js);
53  }
54  {
55  AD ad{"char_test", 0, AT::Char};
56  JS js{{"path", "char_test"}, {"dimension", 0}, {"type", "char"}};
57  check(ad, js);
58  }
59  {
60  AD ad{"double_test", 1, AT::Double};
61  JS js{{"path", "double_test"}, {"dimension", 1}, {"type", "double"}};
62  check(ad, js);
63  }
64  {
65  AD ad{"double_test", 2, AT::Double};
66  JS js{{"path", "double_test"}, {"dimension", 2}, {"type", "double"}};
67  check(ad, js);
68  }
69 
70  {
71  auto ad = make_AD("double_test", {}, AT::Double);
72  JS js{{"path", "double_test"}, {"type", "double"}};
73  check(ad, js);
74  }
75  {
76  auto ad = make_AD("double_test", 2, {});
77  JS js{{"path", "double_test"}, {"dimension", 2}};
78  check(ad, js);
79  }
80  {
81  auto ad = make_AD("double_test", {}, {});
82  JS js{{"path", "double_test"}};
83  check(ad, js);
84  }
85 }
86 
87 TEST_CASE("named_multimesh_parse_attributes", "[components][multimesh]")
88 {
89  {
90  auto m = make_mesh();
93  mc.emplace_mesh(*m, std::string("roo"));
94 
95  auto double_test_handle =
96  m->register_attribute<double>("double_test", wmtk::PrimitiveType::Vertex, 1);
97  auto char_test_handle =
98  m->register_attribute<char>("char_test", wmtk::PrimitiveType::Vertex, 1);
99  auto int_test_handle =
100  m->register_attribute<int64_t>("int_test", wmtk::PrimitiveType::Vertex, 1);
101  auto rational_test_handle =
102  m->register_attribute<wmtk::Rational>("rational_test", wmtk::PrimitiveType::Vertex, 1);
103 
104  { // double check that path extraction is working
105  std::vector<AD> double_ads;
106  double_ads.emplace_back(make_AD("double_test", 0, AT::Double));
107  double_ads.emplace_back(make_AD("/double_test", 0, AT::Double));
108  double_ads.emplace_back(make_AD("roo/double_test", 0, AT::Double));
109  double_ads.emplace_back(make_AD("double_test", {}, AT::Double));
110  double_ads.emplace_back(make_AD("/double_test", 0, {}));
111  double_ads.emplace_back(make_AD("roo/double_test", {}, {}));
112 
113 
114  for (const auto& ad : double_ads) {
116  CHECK(double_test_handle == h);
117  // just on mesh also works
119  CHECK(double_test_handle == h2);
122  CHECK(double_test_handle == h3);
123  }
124  }
125  { // check that other types work
126  CHECK(
128  named_mm,
129  AD{"rational_test", 0, AT::Rational}));
130  CHECK(
132  named_mm,
133  AD{"int_test", 0, AT::Int64}));
134  CHECK(
136  named_mm,
137  AD{"char_test", 0, AT::Char}));
138  }
139  auto edge_handle =
140  m->register_attribute<double>("double_test_e", wmtk::PrimitiveType::Edge, 1);
141  auto tri_handle =
142  m->register_attribute<double>("double_test_f", wmtk::PrimitiveType::Triangle, 1);
143  { // check that other simplex types work
144 
145  CHECK(
147  named_mm,
148  make_AD("double_test_e", 1, AT::Double)));
149  CHECK(
151  named_mm,
152  make_AD("double_test_f", 2, AT::Double)));
153  CHECK(
155  named_mm,
156  make_AD("double_test_f", {}, AT::Double)));
157  CHECK(
159  named_mm,
160  make_AD("double_test_f", {}, AT::Double)));
161  CHECK(
163  named_mm,
164  make_AD("double_test_f", {}, {})));
165  CHECK(
167  named_mm,
168  make_AD("double_test_e", {}, {})));
169  // TODO: lazy about testing tet
170  }
171  {
172  CHECK_THROWS_AS(
174  named_mm,
175  AD{"doub2r2le_test_e", 2, AT::Double}),
177  CHECK_THROWS_AS(
179  named_mm,
180  AD{"double_test_e", 2, AT::Double}),
182  CHECK_THROWS_AS(
184  named_mm,
185  AD{"double_test_f", 1, AT::Double}),
187 
188  auto double_test_handle =
189  m->register_attribute<double>("double_test", wmtk::PrimitiveType::Edge, 1);
190  CHECK_THROWS_AS(
192  named_mm,
193  make_AD("double_test", {}, AT::Double)),
195  }
196  }
197 
198 
199  {
201  auto m = make_mesh();
202  auto children = make_child(*m, {0});
203  REQUIRE(children.size() == 1);
204  auto child = children[0];
205 
206 
208  named_mm.set_mesh(*m);
209  {
210  nlohmann::json js;
211  js["roo"] = nlohmann::json::array({"child"});
212  named_mm.set_names(js);
213  }
214  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
215  CHECK(std::vector<int64_t>{0} == named_mm.get_id("roo.child"));
216  auto attr_handle =
217  m->register_attribute<double>("double_test", wmtk::PrimitiveType::Vertex, 1);
218  auto child_attr_handle =
219  child->register_attribute<double>("double_test", wmtk::PrimitiveType::Vertex, 1);
221  CHECK(
223  named_mm,
224  AD{"double_test", 0, AT::Double}));
225  CHECK(
227  named_mm,
228  AD{"/double_test", 0, AT::Double}));
229  CHECK(
231  named_mm,
232  AD{"roo/double_test", 0, AT::Double}));
233  CHECK(
235  named_mm,
236  AD{"roo.child/double_test", 0, AT::Double}));
237  CHECK(
239  named_mm,
240  AD{".child/double_test", 0, AT::Double}));
241  }
242 }
243 
NamedMultiMesh & emplace_mesh(Args &&... args)
std::vector< int64_t > get_id(const std::string_view &path) const
void set_mesh(Mesh &m)
Navigates to the root of the multimesh.
void set_names(const nlohmann::json &js)
TEST_CASE("multimesh_attribute_description_json", "[components][multimesh]")
nlohmann::json json
wmtk::attribute::MeshAttributeHandle get_attribute(const NamedMultiMesh &mesh, const AttributeDescription &description)
nlohmann::json json
Definition: input.cpp:9
auto make_child(wmtk::Mesh &m, const std::vector< int64_t > &path) -> std::vector< std::shared_ptr< wmtk::Mesh >>
Definition: utils.cpp:11
std::shared_ptr< wmtk::Mesh > make_mesh()
Definition: utils.cpp:6