From 106f61766ba6a6e1161eddd9401b15d7dd1197cb Mon Sep 17 00:00:00 2001 From: Alexander Grayver Date: Fri, 13 Dec 2019 15:21:01 +0100 Subject: [PATCH] Add tests --- .../include/cell_accessor_wrapper.h | 2 +- .../python-bindings/include/mapping_wrapper.h | 7 ++- .../include/triangulation_wrapper.h | 9 ++-- .../source/cell_accessor_wrapper.cc | 2 + .../source/export_triangulation.cc | 10 ++++- .../python-bindings/source/mapping_wrapper.cc | 9 ++++ .../source/triangulation_wrapper.cc | 45 ++++++++++--------- .../tests/triangulation_wrapper.py | 30 ++++++++++++- 8 files changed, 84 insertions(+), 30 deletions(-) diff --git a/contrib/python-bindings/include/cell_accessor_wrapper.h b/contrib/python-bindings/include/cell_accessor_wrapper.h index 0c7f471a06..ef57882256 100644 --- a/contrib/python-bindings/include/cell_accessor_wrapper.h +++ b/contrib/python-bindings/include/cell_accessor_wrapper.h @@ -90,7 +90,7 @@ namespace python get_barycenter() const; /** - * Get the barycenter of the cell. + * Get the center of the cell. */ PointWrapper get_center(const bool respect_manifold = false, diff --git a/contrib/python-bindings/include/mapping_wrapper.h b/contrib/python-bindings/include/mapping_wrapper.h index a51d437b64..f9d3d8e1c7 100644 --- a/contrib/python-bindings/include/mapping_wrapper.h +++ b/contrib/python-bindings/include/mapping_wrapper.h @@ -37,6 +37,11 @@ namespace python */ MappingQGenericWrapper(const MappingQGenericWrapper &other); + /** + * Default constructor. + */ + MappingQGenericWrapper(); + /** * Constructor. */ @@ -62,7 +67,7 @@ namespace python transform_real_to_unit_cell(CellAccessorWrapper &cell, PointWrapper &point); /** - * Get the underlying mapping + * Get the underlying mapping. */ void * get_mapping(); diff --git a/contrib/python-bindings/include/triangulation_wrapper.h b/contrib/python-bindings/include/triangulation_wrapper.h index 63f28e0d7f..bb2505c628 100644 --- a/contrib/python-bindings/include/triangulation_wrapper.h +++ b/contrib/python-bindings/include/triangulation_wrapper.h @@ -348,12 +348,13 @@ namespace python /** * Find and return an active cell that surrounds a given point p. - * The mapping used to determine whether the given point is inside a given - * cell. + * The mapping is used to determine whether the given point is inside a + * given cell. */ CellAccessorWrapper - find_active_cell_around_point(PointWrapper & p, - MappingQGenericWrapper &mapping); + find_active_cell_around_point( + PointWrapper & p, + MappingQGenericWrapper mapping = MappingQGenericWrapper()); /** * Assign a manifold object to a certain part of the triangulation. diff --git a/contrib/python-bindings/source/cell_accessor_wrapper.cc b/contrib/python-bindings/source/cell_accessor_wrapper.cc index d8ede1bafb..c17981ed45 100644 --- a/contrib/python-bindings/source/cell_accessor_wrapper.cc +++ b/contrib/python-bindings/source/cell_accessor_wrapper.cc @@ -173,6 +173,8 @@ namespace python return PointWrapper(barycenter_list); } + + template PointWrapper get_center(const bool respect_manifold, diff --git a/contrib/python-bindings/source/export_triangulation.cc b/contrib/python-bindings/source/export_triangulation.cc index 65835a57bb..cb1e000973 100644 --- a/contrib/python-bindings/source/export_triangulation.cc +++ b/contrib/python-bindings/source/export_triangulation.cc @@ -101,6 +101,11 @@ namespace python distort_random, 1, 2) + BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( + find_active_cell_around_point_overloads, + find_active_cell_around_point, + 1, + 2) const char n_active_cells_docstring[] = "Return the number of active cells \n"; @@ -536,8 +541,9 @@ namespace python boost::python::args("self", "transformation")) .def("find_active_cell_around_point", &TriangulationWrapper::find_active_cell_around_point, - find_active_cell_around_point_docstring, - boost::python::args("self", "point", "mapping")) + find_active_cell_around_point_overloads( + boost::python::args("self", "point", "mapping"), + find_active_cell_around_point_docstring)) .def("refine_global", &TriangulationWrapper::refine_global, refine_global_docstring, diff --git a/contrib/python-bindings/source/mapping_wrapper.cc b/contrib/python-bindings/source/mapping_wrapper.cc index 97c317fe24..edf344b3d6 100644 --- a/contrib/python-bindings/source/mapping_wrapper.cc +++ b/contrib/python-bindings/source/mapping_wrapper.cc @@ -87,6 +87,15 @@ namespace python + MappingQGenericWrapper::MappingQGenericWrapper() + : dim(-1) + , spacedim(-1) + , degree(-1) + , mapping_ptr(nullptr) + {} + + + MappingQGenericWrapper::MappingQGenericWrapper(const int dim, const int spacedim, const int degree) diff --git a/contrib/python-bindings/source/triangulation_wrapper.cc b/contrib/python-bindings/source/triangulation_wrapper.cc index a9adb51806..0805fd8e56 100644 --- a/contrib/python-bindings/source/triangulation_wrapper.cc +++ b/contrib/python-bindings/source/triangulation_wrapper.cc @@ -539,14 +539,22 @@ namespace python Point point = *(static_cast *>(p.get_point())); - const MappingQGeneric *mapping = - static_cast *>( - mapping_wrapper.get_mapping()); - - auto cell_pair = - GridTools::find_active_cell_around_point(*mapping, *tria, point); - - return std::make_pair(cell_pair.first->level(), cell_pair.first->index()); + if (mapping_wrapper.get_mapping() != nullptr) + { + const MappingQGeneric *mapping = + static_cast *>( + mapping_wrapper.get_mapping()); + + auto cell_pair = + GridTools::find_active_cell_around_point(*mapping, *tria, point); + return std::make_pair(cell_pair.first->level(), + cell_pair.first->index()); + } + else + { + auto cell = GridTools::find_active_cell_around_point(*tria, point); + return std::make_pair(cell->level(), cell->index()); + } } @@ -1280,17 +1288,12 @@ namespace python ExcMessage( "The output Triangulation must be of dimension three")); - if ((dim == 2) && (spacedim == 2)) - { - Triangulation<2, 2> *tria = - static_cast *>(triangulation); - Triangulation<3, 3> *tria_out = static_cast *>( - triangulation_out.get_triangulation()); - GridGenerator::extrude_triangulation(*tria, - n_slices, - height, - *tria_out); - } + + Triangulation<2, 2> *tria = + static_cast *>(triangulation); + Triangulation<3, 3> *tria_out = + static_cast *>(triangulation_out.get_triangulation()); + GridGenerator::extrude_triangulation(*tria, n_slices, height, *tria_out); } @@ -1324,8 +1327,8 @@ namespace python CellAccessorWrapper TriangulationWrapper::find_active_cell_around_point( - PointWrapper & p, - MappingQGenericWrapper &mapping) + PointWrapper & p, + MappingQGenericWrapper mapping) { std::pair level_index_pair; if ((dim == 2) && (spacedim == 2)) diff --git a/contrib/python-bindings/tests/triangulation_wrapper.py b/contrib/python-bindings/tests/triangulation_wrapper.py index d23d8c9aad..890f905066 100644 --- a/contrib/python-bindings/tests/triangulation_wrapper.py +++ b/contrib/python-bindings/tests/triangulation_wrapper.py @@ -16,7 +16,6 @@ import unittest from PyDealII.Debug import * - class TestTriangulationWrapper(unittest.TestCase): def setUp(self): @@ -375,6 +374,35 @@ class TestTriangulationWrapper(unittest.TestCase): else: self.assertEqual(n_cells, 64) + + def test_transform(self): + for dim in self.dim: + triangulation_1 = self.build_hyper_cube_triangulation(dim) + triangulation_1.refine_global(1) + triangulation_2 = self.build_hyper_cube_triangulation(dim) + triangulation_2.refine_global(1) + + triangulation_1.transform(lambda p: [v + 1. for v in p]) + + if dim[1] == '3D': + offset = Point([1., 1., 1.]) + else: + offset = Point([1., 1.]) + + for (cell_1, cell_2) in zip(triangulation_1.active_cells(), triangulation_2.active_cells()): + self.assertTrue(cell_1.center().distance(cell_2.center() + offset) < 1e-8) + + + def test_find_active_cell_around_point(self): + for dim in self.dim: + triangulation = self.build_hyper_cube_triangulation(dim) + triangulation.refine_global(2) + + for cell in triangulation.active_cells(): + cell_ret = triangulation.find_active_cell_around_point(cell.center()) + self.assertTrue(cell.center().distance(cell_ret.center()) < 1e-8) + + def test_save_load(self): for dim in self.dim: triangulation_1 = self.build_hyper_cube_triangulation(dim) -- 2.39.5