]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add hyper_shell generator
authorAlexander Grayver <agrayver@erdw.ethz.ch>
Fri, 29 Nov 2019 10:34:05 +0000 (11:34 +0100)
committerAlexander Grayver <agrayver@erdw.ethz.ch>
Fri, 29 Nov 2019 11:27:10 +0000 (12:27 +0100)
contrib/python-bindings/include/manifold_wrapper.h
contrib/python-bindings/include/triangulation_wrapper.h
contrib/python-bindings/source/export_manifold.cc
contrib/python-bindings/source/export_triangulation.cc
contrib/python-bindings/source/manifold_wrapper.cc
contrib/python-bindings/source/triangulation_wrapper.cc

index e72018c3388b7c326cdbeeb9ae8d5b56e3ac42b6..54b848d34c544403bddcf3dc8c14009936cfe69e 100644 (file)
@@ -60,7 +60,7 @@ namespace python
      * Create CylindricalManifold
      */
     void
-    create_cylindrical(const int axis, const double tolerance);
+    create_cylindrical(const int axis = 0, const double tolerance = 1e-10);
 
     /**
      * Return pointer to an underlying manifold object
index bbab008dee95cbdc3c0901440b0a1e4b17879367..a599e62ed5e291bebe23046d6f8e0b60be7fe88f 100644 (file)
@@ -267,6 +267,19 @@ namespace python
     void
     generate_half_hyper_ball(PointWrapper &center, const double radius = 1.);
 
+    /**
+     * Produce a hyper-shell, the region between two spheres around center,
+     * with given inner_radius and outer_radius. The number n_cells indicates
+     * the number of cells of the resulting triangulation, i.e., how many
+     * cells form the ring (in 2d) or the shell (in 3d).
+     * The appropriate manifold class is SphericalManifold.
+     */
+    void
+    generate_hyper_shell(PointWrapper & center,
+                         const double   inner_radius,
+                         const double   outer_radius,
+                         const unsigned n_cells = 0);
+
     /**
      * Shift each vertex of the Triangulation by the given @p shift_list.
      */
@@ -334,6 +347,13 @@ namespace python
     void
     write(const std::string &filename, const std::string format) const;
 
+    /**
+     * Read mesh from the file @filename using the given data
+     * format.
+     */
+    void
+    read(const std::string &filename, const std::string format) const;
+
     /**
      * Write the Triangulation in file.
      */
index e752153655714c80af71d570a3117a68485d8eb0..d9225359b32ec1f7befee9a4ed4a13cb20c77957 100644 (file)
@@ -21,6 +21,12 @@ DEAL_II_NAMESPACE_OPEN
 
 namespace python
 {
+  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(create_cylindrical_overloads,
+                                         create_cylindrical,
+                                         0,
+                                         2)
+
+
   const char create_spherical_docstring[] =
     " Create spherical manifold with a given center point       \n";
 
@@ -39,7 +45,7 @@ namespace python
   export_manifold()
   {
     boost::python::class_<ManifoldWrapper>(
-      "ManifoldWrapper",
+      "Manifold",
       boost::python::init<const int, const int>(
         boost::python::args("dim", "spacedim")))
       .def("create_spherical",
@@ -52,8 +58,9 @@ namespace python
            boost::python::args("self", "center"))
       .def("create_cylindrical",
            &ManifoldWrapper::create_cylindrical,
-           create_cylindrical_docstring,
-           boost::python::args("self", "axis", "tolerance"));
+           create_cylindrical_overloads(
+             boost::python::args("self", "axis", "tolerance"),
+             create_cylindrical_docstring));
   }
 } // namespace python
 
index f1bab25860306aa240f8f3a7beb39d88feec8225..8992688d916fad5e5b8fc00009d3dcdf88988041 100644 (file)
@@ -94,7 +94,10 @@ namespace python
                                          1,
                                          2)
 
-
+  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(generate_hyper_shell_overloads,
+                                         generate_hyper_shell,
+                                         3,
+                                         4)
 
   const char n_active_cells_docstring[] =
     "Return the number of active cells                                      \n";
@@ -252,6 +255,15 @@ namespace python
 
 
 
+  const char generate_hyper_shell_docstring[] =
+    "Produce a hyper-shell, the region between two spheres around center,   \n"
+    "with given inner_radius and outer_radius. The number n_cells indicates \n"
+    "the number of cells of the resulting triangulation, i.e., how many     \n"
+    "cells form the ring (in 2d) or the shell (in 3d).                      \n"
+    "The appropriate manifold class is SphericalManifold.                   \n";
+
+
+
   const char generate_half_hyper_ball_docstring[] =
     "Generate a half hyper-ball around center, which contains four          \n"
     "elements in 2d and 6 in 3d. The cut plane is perpendicular to the      \n"
@@ -323,6 +335,14 @@ namespace python
 
 
 
+  const char read_docstring[] =
+    "Read a mesh from the file according to the given data format.          \n"
+    "The possible formats are:                                              \n"
+    "  - msh                                                                \n"
+    "  - vtk                                                                \n";
+
+
+
   const char save_docstring[] =
     "Write the Triangulation to a file                                      \n";
 
@@ -442,6 +462,14 @@ namespace python
            generate_half_hyper_ball_overloads(
              boost::python::args("self", "center", "radius"),
              generate_half_hyper_ball_docstring))
+      .def("generate_hyper_shell",
+           &TriangulationWrapper::generate_hyper_shell,
+           generate_hyper_shell_overloads(boost::python::args("self",
+                                                              "center",
+                                                              "inner_radius",
+                                                              "outer_radius",
+                                                              "n_cells"),
+                                          generate_hyper_shell_docstring))
       .def("shift",
            &TriangulationWrapper::shift,
            shift_docstring,
@@ -470,6 +498,10 @@ namespace python
            &TriangulationWrapper::write,
            write_docstring,
            boost::python::args("self", "filename", "format"))
+      .def("read",
+           &TriangulationWrapper::read,
+           read_docstring,
+           boost::python::args("self", "filename", "format"))
       .def("save",
            &TriangulationWrapper::save,
            save_docstring,
index 9c78c60cda96c1ecc3f91225bc2b5f4d2e2816a8..239ba9dfec621bb22eecd994a81667246802a979 100644 (file)
@@ -26,7 +26,7 @@ namespace python
   {
     template <int dim, int spacedim>
     Manifold<dim, spacedim> *
-    create_spherical_manifold(PointWrapper center)
+    create_spherical_manifold(const PointWrapper &center)
     {
       const Point<spacedim> *point =
         static_cast<const Point<spacedim> *>(center.get_point());
@@ -37,7 +37,7 @@ namespace python
 
     template <int dim, int spacedim>
     Manifold<dim, spacedim> *
-    create_polar_manifold(PointWrapper center)
+    create_polar_manifold(const PointWrapper &center)
     {
       const Point<spacedim> *point =
         static_cast<const Point<spacedim> *>(center.get_point());
@@ -72,6 +72,12 @@ namespace python
         {
           return new PolarManifold<dim, spacedim>(*d);
         }
+      else if (const CylindricalManifold<dim, spacedim> *d =
+                 dynamic_cast<const CylindricalManifold<dim, spacedim> *>(
+                   manifold))
+        {
+          return new CylindricalManifold<dim, spacedim>(*d);
+        }
       else
         ExcMessage("Unsupported manifold type in clone.");
 
index 45b1024ea820df205d15c151724cb58d19a6b24b..5457d2f00e725d830827b89bc2717c82849bbeb1 100644 (file)
@@ -16,6 +16,7 @@
 #include <deal.II/base/types.h>
 
 #include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/grid_in.h>
 #include <deal.II/grid/grid_out.h>
 #include <deal.II/grid/grid_tools.h>
 #include <deal.II/grid/tria.h>
@@ -373,6 +374,25 @@ namespace python
     }
 
 
+    template <int dim>
+    void
+    generate_hyper_shell(PointWrapper & center,
+                         const double   inner_radius,
+                         const double   outer_radius,
+                         const unsigned n_cells,
+                         void *         triangulation)
+    {
+      // Cast the PointWrapper object to Point<dim>
+      Point<dim> center_point =
+        *(static_cast<Point<dim> *>(center.get_point()));
+
+      Triangulation<dim> *tria =
+        static_cast<Triangulation<dim> *>(triangulation);
+      tria->clear();
+      GridGenerator::hyper_shell(
+        *tria, center_point, inner_radius, outer_radius, n_cells);
+    }
+
 
     template <int dim>
     void
@@ -519,6 +539,34 @@ namespace python
       mesh_writer.write(*tria, ofs, output_format);
       ofs.close();
     }
+
+
+
+    template <int dim, int spacedim>
+    void
+    read(const std::string &filename,
+         const std::string &format,
+         void *             triangulation)
+    {
+      Triangulation<dim, spacedim> *tria =
+        static_cast<Triangulation<dim, spacedim> *>(triangulation);
+
+      tria->clear();
+
+      typename GridIn<dim, spacedim>::Format input_format;
+      if (format.compare("msh") == 0)
+        input_format = GridIn<dim, spacedim>::Format::msh;
+      else if (format.compare("vtk") == 0)
+        input_format = GridIn<dim, spacedim>::Format::vtk;
+      else
+        ExcMessage("Cannot read triangulation of the given format.");
+
+      GridIn<dim, spacedim> mesh_reader;
+      mesh_reader.attach_triangulation(*tria);
+      std::ifstream ifs(filename);
+      mesh_reader.read(ifs, input_format);
+      ifs.close();
+    }
   } // namespace internal
 
 
@@ -1004,6 +1052,27 @@ namespace python
   }
 
 
+
+  void
+  TriangulationWrapper::generate_hyper_shell(PointWrapper & center,
+                                             const double   inner_radius,
+                                             const double   outer_radius,
+                                             const unsigned n_cells)
+  {
+    AssertThrow(
+      dim == spacedim,
+      ExcMessage(
+        "This function is only implemented for dim equal to spacedim."));
+    if (dim == 2)
+      internal::generate_hyper_shell<2>(
+        center, inner_radius, outer_radius, n_cells, triangulation);
+    else
+      internal::generate_hyper_shell<3>(
+        center, inner_radius, outer_radius, n_cells, triangulation);
+  }
+
+
+
   void
   TriangulationWrapper::generate_hyper_sphere(PointWrapper &center,
                                               const double  radius)
@@ -1190,6 +1259,20 @@ namespace python
 
 
 
+  void
+  TriangulationWrapper::read(const std::string &filename,
+                             const std::string  format) const
+  {
+    if ((dim == 2) && (spacedim == 2))
+      internal::read<2, 2>(filename, format, triangulation);
+    else if ((dim == 2) && (spacedim == 3))
+      internal::read<2, 3>(filename, format, triangulation);
+    else
+      internal::read<3, 3>(filename, format, triangulation);
+  }
+
+
+
   void
   TriangulationWrapper::save(const std::string &filename) const
   {

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.