From fe9a74c0ab1f2109171eead2b12834b8501622d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Wed, 30 Oct 2024 12:30:24 +0100 Subject: [PATCH] Tests and bugfixes --- src/Mesh.cpp | 5 ++--- src/UnitDimension.cpp | 4 +++- src/binding/python/UnitDimension.cpp | 16 +++++++++++++++- test/python/unittest/API/APITest.py | 8 ++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 69fc6a0263..e2e239299e 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -334,9 +334,8 @@ unit_representations::AsArrays Mesh::gridUnitDimension() const { // gridUnitDimension is an optional attribute // if it is missing, the mesh is interpreted as spatial - std::array spatialMesh; - auxiliary::fromMapOfUnitDimension( - spatialMesh.data(), {{UnitDimension::L, 1}}); + auto spatialMesh = + unit_representations::asArray({{UnitDimension::L, 1}}); auto dim = retrieveMeshDimensionality(*this); unit_representations::AsArrays res(dim, spatialMesh); return res; diff --git a/src/UnitDimension.cpp b/src/UnitDimension.cpp index a2420defec..c9aa449347 100644 --- a/src/UnitDimension.cpp +++ b/src/UnitDimension.cpp @@ -20,7 +20,7 @@ namespace unit_representations { auto asArray(AsMap const &udim) -> AsArray { - AsArray res; + AsArray res{}; auxiliary::fromMapOfUnitDimension(res.data(), udim); return res; } @@ -40,6 +40,7 @@ namespace unit_representations auto asArrays(AsMaps const &vec) -> AsArrays { AsArrays res; + res.reserve(vec.size()); std::transform( vec.begin(), vec.end(), @@ -50,6 +51,7 @@ namespace unit_representations auto asMaps(AsArrays const &vec) -> AsMaps { AsMaps res; + res.reserve(vec.size()); std::transform( vec.begin(), vec.end(), diff --git a/src/binding/python/UnitDimension.cpp b/src/binding/python/UnitDimension.cpp index 6e46a6cfcf..eb32e87de1 100644 --- a/src/binding/python/UnitDimension.cpp +++ b/src/binding/python/UnitDimension.cpp @@ -31,5 +31,19 @@ void init_UnitDimension(py::module &m) .value("I", UnitDimension::I) .value("theta", UnitDimension::theta) .value("N", UnitDimension::N) - .value("J", UnitDimension::J); + .value("J", UnitDimension::J) + .def( + "as_index", + [](UnitDimension ud) -> uint8_t { + return static_cast(ud); + }) + .def( + "from_index", + [](uint8_t idx) -> UnitDimension { + return static_cast(idx); + }) + .def("as_array", &unit_representations::asArray) + .def("as_map", &unit_representations::asMap) + .def("as_arrays", &unit_representations::asArrays) + .def("as_maps", &unit_representations::asMaps); } diff --git a/test/python/unittest/API/APITest.py b/test/python/unittest/API/APITest.py index 59e6b5c97e..f828ab9795 100644 --- a/test/python/unittest/API/APITest.py +++ b/test/python/unittest/API/APITest.py @@ -98,6 +98,14 @@ def testFieldData(self): self.assertEqual(len(i.meshes), 2) for m in i.meshes: self.assertTrue(m in ["E", "rho"]) + self.assertEqual(i.meshes[m].unit_dimension, io.Unit_Dimension.as_array( + io.Unit_Dimension.as_map(i.meshes[m].unit_dimension))) + self.assertEqual(io.Unit_Dimension.as_maps(i.meshes[m].grid_unit_dimension), [ + {io.Unit_Dimension.L: 1}, {io.Unit_Dimension.L: 1}]) + self.assertEqual(io.Unit_Dimension.from_index(0), io.Unit_Dimension.L) + self.assertEqual(io.Unit_Dimension.L.as_index(), 0) + for idx in range(7): + self.assertEqual(idx, io.Unit_Dimension.from_index(idx).as_index()) # Check entries. self.assertEqual(len(i.meshes), 2)