From fd467d48a2585af80bf242daa7c06b78908d7417 Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Wed, 3 Apr 2024 09:09:37 -0400 Subject: [PATCH] Add GridGenerator::cylinder to the python wrappers --- .../include/triangulation_wrapper.h | 5 ++++ .../source/export_triangulation.cc | 22 ++++++++++++++ .../source/triangulation_wrapper.cc | 30 +++++++++++++++++++ .../tests/triangulation_wrapper.py | 11 +++++++ 4 files changed, 68 insertions(+) diff --git a/contrib/python-bindings/include/triangulation_wrapper.h b/contrib/python-bindings/include/triangulation_wrapper.h index 0a617ab24c..7422c11ac6 100644 --- a/contrib/python-bindings/include/triangulation_wrapper.h +++ b/contrib/python-bindings/include/triangulation_wrapper.h @@ -286,6 +286,11 @@ namespace python void generate_half_hyper_ball(PointWrapper ¢er, const double radius = 1.); + /*! @copydoc GridGenerator::cylinder + */ + void + generate_cylinder(const double radius = 1., const double half_length = 1.); + /*! @copydoc GridGenerator::hyper_shell */ void diff --git a/contrib/python-bindings/source/export_triangulation.cc b/contrib/python-bindings/source/export_triangulation.cc index 5a9b28e6e4..65b1bbdac0 100644 --- a/contrib/python-bindings/source/export_triangulation.cc +++ b/contrib/python-bindings/source/export_triangulation.cc @@ -110,6 +110,10 @@ namespace python generate_half_hyper_ball, 1, 2) + BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(generate_cylinder_overloads, + generate_cylinder, + 0, + 2) BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(generate_hyper_shell_overloads, generate_hyper_shell, 3, @@ -330,6 +334,19 @@ namespace python + const char generate_cylinder_docstring[] = + "Create a dim dimensional cylinder where the x-axis serves as the axis \n" + "of the cylinder. For the purposes of this function, a cylinder is \n" + "defined as a (dim - 1) dimensional disk of given radius, extruded \n" + "along the axis of the cylinder (which is the first coordinate \n" + "direction). Consequently, in three dimensions, the cylinder extends \n" + "from x=-half_length to x=+half_length and its projection into the \n" + "yz-plane is a circle of radius radius. In two dimensions, the \n" + "cylinder is a rectangle from x=-half_length to x=+half_length and \n" + "from y=-radius to y=radius. \n"; + + + const char generate_hyper_cube_with_cylindrical_hole_docstring[] = "This function produces a square in the xy-plane with a cylindrical \n" "hole in the middle. In 3d, this geometry is extruded in z direction \n" @@ -711,6 +728,11 @@ namespace python generate_half_hyper_ball_overloads( boost::python::args("self", "center", "radius"), generate_half_hyper_ball_docstring)) + .def("generate_cylinder", + &TriangulationWrapper::generate_cylinder, + generate_cylinder_overloads( + boost::python::args("self", "radius", "half_length"), + generate_cylinder_docstring)) .def("generate_hyper_shell", &TriangulationWrapper::generate_hyper_shell, generate_hyper_shell_overloads(boost::python::args("self", diff --git a/contrib/python-bindings/source/triangulation_wrapper.cc b/contrib/python-bindings/source/triangulation_wrapper.cc index 69ead0d4e6..1f131c00d2 100644 --- a/contrib/python-bindings/source/triangulation_wrapper.cc +++ b/contrib/python-bindings/source/triangulation_wrapper.cc @@ -563,6 +563,20 @@ namespace python + template + void + generate_cylinder(const double radius, + const double half_length, + void *triangulation) + { + Triangulation *tria = + static_cast *>(triangulation); + tria->clear(); + GridGenerator::cylinder(*tria, radius, half_length); + } + + + template void scale(const double scaling_factor, void *triangulation) @@ -1601,6 +1615,22 @@ namespace python + void + TriangulationWrapper::generate_cylinder(const double radius, + const double half_length) + { + AssertThrow( + dim == spacedim, + ExcMessage( + "This function is only implemented for dim equal to spacedim.")); + if (dim == 2) + internal::generate_cylinder<2>(radius, half_length, triangulation); + else + internal::generate_cylinder<3>(radius, half_length, triangulation); + } + + + void TriangulationWrapper::generate_hyper_cube_with_cylindrical_hole( const double inner_radius, diff --git a/contrib/python-bindings/tests/triangulation_wrapper.py b/contrib/python-bindings/tests/triangulation_wrapper.py index a2763730fb..5a645be1f0 100644 --- a/contrib/python-bindings/tests/triangulation_wrapper.py +++ b/contrib/python-bindings/tests/triangulation_wrapper.py @@ -377,6 +377,17 @@ class TestTriangulationWrapper(unittest.TestCase): n_cells = triangulation.n_active_cells() self.assertEqual(n_cells, n_cells_ref) + def test_cylinder(self): + for dim in self.dim: + triangulation = Triangulation(dim[0]) + if (dim[0] == '2D'): + n_cells_ref = 1 + else: + n_cells_ref = 10 + triangulation.generate_cylinder() + n_cells = triangulation.n_active_cells() + self.assertEqual(n_cells, n_cells_ref) + def test_shift_and_merge(self): for dim in self.dim: triangulation_1 = self.build_hyper_cube_triangulation(dim) -- 2.39.5