From 088cd8f243576379e31240e841da27c86b331f5b Mon Sep 17 00:00:00 2001 From: Alexander Grayver Date: Tue, 11 Feb 2020 16:41:17 +0100 Subject: [PATCH] Add more python binds --- .../include/triangulation_wrapper.h | 25 ++++ .../source/export_triangulation.cc | 47 ++++++- .../source/triangulation_wrapper.cc | 122 ++++++++++++++++++ .../tests/triangulation_wrapper.py | 26 +++- doc/news/changes/minor/20200211Grayver | 4 + 5 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 doc/news/changes/minor/20200211Grayver diff --git a/contrib/python-bindings/include/triangulation_wrapper.h b/contrib/python-bindings/include/triangulation_wrapper.h index ab9da0868e..3639186059 100644 --- a/contrib/python-bindings/include/triangulation_wrapper.h +++ b/contrib/python-bindings/include/triangulation_wrapper.h @@ -58,6 +58,14 @@ namespace python */ ~TriangulationWrapper(); + /** + * Create a triangulation from a list of vertices and a list of indices, + * each of the latter being a list of 1<( - "Triangulation", boost::python::init()) - .def(boost::python::init()) + "Triangulation", + boost::python::init(boost::python::args("dim"))) + .def(boost::python::init( + boost::python::args("dim", "spacedim"))) .def("n_active_cells", &TriangulationWrapper::n_active_cells, n_active_cells_docstring, @@ -464,6 +489,10 @@ namespace python &TriangulationWrapper::maximal_cell_diameter, maximal_cell_diameter_docstring, boost::python::args("self")) + .def("create_triangulation", + &TriangulationWrapper::create_triangulation, + create_triangulation_docstring, + boost::python::args("self", "vertices", "cells_vertices")) .def("generate_hyper_cube", &TriangulationWrapper::generate_hyper_cube, generate_hyper_cube_overloads( @@ -499,6 +528,16 @@ namespace python boost::python::args( "self", "spacing", "p", "material_id", "colorize"), generate_subdivided_material_hyper_rectangle_docstring)) + .def("generate_hyper_cube_with_cylindrical_hole", + &TriangulationWrapper::generate_hyper_cube_with_cylindrical_hole, + generate_hyper_cube_with_cylindrical_hole_overloads( + boost::python::args("self", + "inner_radius", + "outer_radius", + "L", + "repetitions", + "colorize"), + generate_hyper_cube_with_cylindrical_hole_docstring)) .def("generate_cheese", &TriangulationWrapper::generate_cheese, generate_cheese_docstring, @@ -569,6 +608,10 @@ namespace python &TriangulationWrapper::shift, shift_docstring, boost::python::args("self", "shift")) + .def("scale", + &TriangulationWrapper::scale, + scale_docstring, + boost::python::args("self", "scaling_factor")) .def("merge_triangulations", &TriangulationWrapper::merge_triangulations, merge_docstring, diff --git a/contrib/python-bindings/source/triangulation_wrapper.cc b/contrib/python-bindings/source/triangulation_wrapper.cc index a33173029a..e07bda593b 100644 --- a/contrib/python-bindings/source/triangulation_wrapper.cc +++ b/contrib/python-bindings/source/triangulation_wrapper.cc @@ -35,6 +35,43 @@ namespace python { namespace internal { + template + void + create_triangulation(const boost::python::list &vertices_list, + const boost::python::list &cells_vertices, + void * triangulation) + { + Triangulation *tria = + static_cast *>(triangulation); + tria->clear(); + + const size_t n_vertices = boost::python::len(vertices_list); + std::vector> vertices(n_vertices); + for (size_t i = 0; i < n_vertices; ++i) + { + boost::python::list vertex = + boost::python::extract(vertices_list[i]); + for (int d = 0; d < spacedim; ++d) + vertices[i][d] = boost::python::extract(vertex[d]); + } + + const size_t n_cells = boost::python::len(cells_vertices); + std::vector> cell_data(n_cells); + for (size_t i = 0; i < n_cells; ++i) + { + boost::python::list vertex_indices = + boost::python::extract(cells_vertices[i]); + + for (unsigned int v = 0; v < GeometryInfo::vertices_per_cell; + ++v) + cell_data[i].vertices[v] = + boost::python::extract(vertex_indices[v]); + } + + tria->create_triangulation(vertices, cell_data, SubCellData()); + } + + template void generate_hyper_cube(const double left, @@ -110,6 +147,23 @@ namespace python } + template + void + generate_hyper_cube_with_cylindrical_hole(const double inner_radius, + const double outer_radius, + const double L, + const unsigned int repetitions, + const bool colorize, + void * triangulation) + { + Triangulation *tria = + static_cast *>(triangulation); + tria->clear(); + GridGenerator::hyper_cube_with_cylindrical_hole( + *tria, inner_radius, outer_radius, L, repetitions, colorize); + } + + template void @@ -431,6 +485,17 @@ namespace python + template + void + scale(const double scaling_factor, void *triangulation) + { + Triangulation *tria = + static_cast *>(triangulation); + GridTools::scale(scaling_factor, *tria); + } + + + template void shift(boost::python::list &shift_list, void *triangulation) @@ -793,6 +858,27 @@ namespace python + void + TriangulationWrapper::create_triangulation( + const boost::python::list &vertices, + const boost::python::list &cells_vertices) + { + if ((dim == 2) && (spacedim == 2)) + internal::create_triangulation<2, 2>(vertices, + cells_vertices, + triangulation); + else if ((dim == 2) && (spacedim == 3)) + internal::create_triangulation<2, 3>(vertices, + cells_vertices, + triangulation); + else + internal::create_triangulation<3, 3>(vertices, + cells_vertices, + triangulation); + } + + + void TriangulationWrapper::generate_hyper_cube(const double left, const double right, @@ -1270,6 +1356,29 @@ namespace python + void + TriangulationWrapper::generate_hyper_cube_with_cylindrical_hole( + const double inner_radius, + const double outer_radius, + const double L, + const unsigned int repetitions, + const bool colorize) + { + AssertThrow( + dim == spacedim, + ExcMessage( + "This function is only implemented for dim equal to spacedim.")); + + if (dim == 2) + internal::generate_hyper_cube_with_cylindrical_hole<2>( + inner_radius, outer_radius, L, repetitions, colorize, triangulation); + else + internal::generate_hyper_cube_with_cylindrical_hole<3>( + inner_radius, outer_radius, L, repetitions, colorize, triangulation); + } + + + void TriangulationWrapper::shift(boost::python::list &shift_list) { @@ -1286,6 +1395,19 @@ namespace python + void + TriangulationWrapper::scale(const double scaling_factor) + { + if ((dim == 2) && (spacedim == 2)) + internal::scale<2, 2>(scaling_factor, triangulation); + else if ((dim == 2) && (spacedim == 3)) + internal::scale<2, 3>(scaling_factor, triangulation); + else + internal::scale<3, 3>(scaling_factor, triangulation); + } + + + void TriangulationWrapper::merge_triangulations( TriangulationWrapper &triangulation_1, diff --git a/contrib/python-bindings/tests/triangulation_wrapper.py b/contrib/python-bindings/tests/triangulation_wrapper.py index 890f905066..bd13b682a6 100644 --- a/contrib/python-bindings/tests/triangulation_wrapper.py +++ b/contrib/python-bindings/tests/triangulation_wrapper.py @@ -62,6 +62,22 @@ class TestTriangulationWrapper(unittest.TestCase): n_cells = triangulation.n_active_cells() self.assertEqual(n_cells, len(vertices)) + def test_create_triangulation(self): + for dim in self.dim: + triangulation = Triangulation(dim[0]) + if (dim[0] == '2D'): + vertices = [[0., 0.], [1., 0.], [0., 1.], [1., 1.]] + cell_vertices = [[0, 1, 2, 3]] + else: + vertices = [[0., 0., 0.], [1., 0., 0.],\ + [0., 1., 0.], [1., 1., 0.],\ + [0., 0., 1.], [1., 0., 1.],\ + [0., 1., 1.], [1., 1., 1.]] + cell_vertices = [[0, 1, 2, 3, 4, 5, 6, 7]] + triangulation.create_triangulation(vertices, cell_vertices) + n_cells = triangulation.n_active_cells() + self.assertEqual(n_cells, len(cell_vertices)) + def test_subdivided_hyper_cube(self): for dim in self.dim: triangulation = Triangulation(dim[0], dim[1]) @@ -140,7 +156,15 @@ class TestTriangulationWrapper(unittest.TestCase): n_cells = triangulation.n_active_cells() self.assertEqual(n_cells, 17) - def test_generate_chees(self): + def test_hyper_cube_with_cylindrical_hole(self): + for dim in self.restricted_dim: + triangulation = Triangulation(dim[0]) + triangulation.generate_hyper_cube_with_cylindrical_hole(inner_radius = .25, + outer_radius = .5, L = .5, repetitions = 1, colorize = False) + n_cells = triangulation.n_active_cells() + self.assertEqual(n_cells, 8) + + def test_generate_cheese(self): for dim in self.dim: triangulation = Triangulation(dim[0], dim[1]) if (dim[0] == '2D'): diff --git a/doc/news/changes/minor/20200211Grayver b/doc/news/changes/minor/20200211Grayver new file mode 100644 index 0000000000..f901479969 --- /dev/null +++ b/doc/news/changes/minor/20200211Grayver @@ -0,0 +1,4 @@ +Add: python binds for Triangulation::create_triangulation, GridTools::scale +and GridGenerator::hyper_cube_with_cylindrical_hole. +
+(Alexander Grayver, 2020/02/11) -- 2.39.5