]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Output triangulation and add adaptive mesh refinement.
authorBruno Turcksin <bruno.turcksin@gmail.com>
Mon, 27 Jun 2016 15:16:12 +0000 (11:16 -0400)
committerBruno Turcksin <bruno.turcksin@gmail.com>
Wed, 3 Aug 2016 20:34:02 +0000 (16:34 -0400)
include/deal.II/python/triangulation_wrapper.h
source/python/export_triangulation.cc
source/python/triangulation_wrapper.cc
tests/python/triangulation_wrapper.py

index b63e7472e63e65e9f158ea119b58cec6530b1dd7..d0f1d433da328c1427724c7aa1b01301a4e655d7 100644 (file)
@@ -116,6 +116,16 @@ namespace PyDealII
      */
     void refine_global(const unsigned int n);
 
+    /**
+     * Execute both refinement and coarsening of the Triangulation.
+     */
+    void execute_coarsening_and_refinement();
+
+    /**
+     * Write grid to the output file @filename according to the given data format.
+     */
+    void write(const std::string &filename, const std::string format) const;
+
     /**
      * Write the Triangulation in file.
      */
index 4b7917c2eb3415958135fef2a108f05d9ea046ba..63b855cbd6c0f48b84b7b5085d739bd38032568b 100644 (file)
@@ -82,6 +82,14 @@ namespace PyDealII
          &TriangulationWrapper::refine_global,
          "Refine all the cells times times.",
          boost::python::args("self", "times"))
+    .def("execute_coarsening_and_refinement",
+         &TriangulationWrapper::execute_coarsening_and_refinement,
+         "Execute both refinement and coarsening of the Triangulation.",
+         boost::python::args("self"))
+    .def("write",
+         &TriangulationWrapper::write,
+         "Write grid to the output file according to the given data format. The possible formats are: none, dx, gnuplot, eps, ucd, xfig, msh, svg, mathgl, vtk, and vtu.",
+         boost::python::args("self", "filename", "format"))
     .def("save",
          &TriangulationWrapper::save,
          "Write the Triangulation to a file.",
index d78cdea2599dc2e4937ce0e051294b6c782dd90d..eb1010def362f77ec6caf8cc82540bd30a85d8ac 100644 (file)
@@ -17,6 +17,7 @@
 #include <deal.II/python/cell_accessor_wrapper.h>
 #include <deal.II/grid/tria.h>
 #include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/grid_out.h>
 #include <deal.II/grid/grid_tools.h>
 #include <boost/archive/text_oarchive.hpp>
 #include <boost/archive/text_iarchive.hpp>
@@ -181,6 +182,46 @@ namespace PyDealII
         cells.push_back(CellAccessorWrapper(triangulation_wrapper, cell->level(),
                                             cell->index()));
     }
+
+
+
+    template <int dim, int spacedim>
+    void write(const std::string &filename,
+               const std::string &format,
+               void              *triangulation)
+    {
+      dealii::Triangulation<dim,spacedim> *tria =
+        static_cast<dealii::Triangulation<dim,spacedim>*>(triangulation);
+
+      dealii::GridOut::OutputFormat output_format;
+      if (format.compare("dx") == 0)
+        output_format = dealii::GridOut::OutputFormat::dx;
+      else if (format.compare("gnuplot") == 0)
+        output_format = dealii::GridOut::OutputFormat::gnuplot;
+      else if (format.compare("eps") == 0)
+        output_format = dealii::GridOut::OutputFormat::eps;
+      else if (format.compare("ucd") == 0)
+        output_format = dealii::GridOut::OutputFormat::ucd;
+      else if (format.compare("xfig") == 0)
+        output_format = dealii::GridOut::OutputFormat::xfig;
+      else if (format.compare("msh") == 0)
+        output_format = dealii::GridOut::OutputFormat::msh;
+      else if (format.compare("svg") == 0)
+        output_format = dealii::GridOut::OutputFormat::svg;
+      else if (format.compare("mathgl") == 0)
+        output_format = dealii::GridOut::OutputFormat::mathgl;
+      else if (format.compare("vtk") == 0)
+        output_format = dealii::GridOut::OutputFormat::vtk;
+      else if (format.compare("vtu") == 0)
+        output_format = dealii::GridOut::OutputFormat::vtu;
+      else
+        output_format = dealii::GridOut::OutputFormat::none;
+
+      dealii::GridOut mesh_writer;
+      std::ofstream ofs(filename);
+      mesh_writer.write(*tria, ofs, output_format);
+      ofs.close();
+    }
   }
 
 
@@ -418,26 +459,39 @@ namespace PyDealII
 
 
 
-  TriangulationWrapper::iterator TriangulationWrapper::begin()
+  void TriangulationWrapper::execute_coarsening_and_refinement()
   {
-    // Build the vector containing all the CellAccessorWrapper when the iterator
-    // is called. This way, we don't need to keep track of the changes on the
-    // Triangulation.
-    build_cells_vector();
-
-    return cells.begin();
+    if ((dim == 2) && (spacedim == 2))
+      {
+        dealii::Triangulation<2,2> *tria =
+          static_cast<dealii::Triangulation<2,2>*>(triangulation);
+        tria->execute_coarsening_and_refinement();
+      }
+    else if ((dim == 2) && (spacedim == 3))
+      {
+        dealii::Triangulation<2,3> *tria =
+          static_cast<dealii::Triangulation<2,3>*>(triangulation);
+        tria->execute_coarsening_and_refinement();
+      }
+    else
+      {
+        dealii::Triangulation<3,3> *tria =
+          static_cast<dealii::Triangulation<3,3>*>(triangulation);
+        tria->execute_coarsening_and_refinement();
+      }
   }
 
 
 
-  TriangulationWrapper::iterator TriangulationWrapper::end()
+  void TriangulationWrapper::write(const std::string &filename,
+                                   const std::string  format) const
   {
-    // Python seems to call end() before begin() so call build_cells_vector in
-    // both begin() and end(). The updated flag makes sure that the vector of
-    // CellAccessorWrapper is only build once.
-    build_cells_vector();
-
-    return cells.end();
+    if ((dim == 2) && (spacedim == 2))
+      internal::write<2,2>(filename, format, triangulation);
+    else if ((dim == 2) && (spacedim == 3))
+      internal::write<2,3>(filename, format, triangulation);
+    else
+      internal::write<3,3>(filename, format, triangulation);
   }
 
 
@@ -504,6 +558,30 @@ namespace PyDealII
 
 
 
+  TriangulationWrapper::iterator TriangulationWrapper::begin()
+  {
+    // Build the vector containing all the CellAccessorWrapper when the iterator
+    // is called. This way, we don't need to keep track of the changes on the
+    // Triangulation.
+    build_cells_vector();
+
+    return cells.begin();
+  }
+
+
+
+  TriangulationWrapper::iterator TriangulationWrapper::end()
+  {
+    // Python seems to call end() before begin() so call build_cells_vector in
+    // both begin() and end(). The updated flag makes sure that the vector of
+    // CellAccessorWrapper is only build once.
+    build_cells_vector();
+
+    return cells.end();
+  }
+
+
+
   void TriangulationWrapper::setup(const std::string &dimension,
                                    const std::string &spacedimension)
   {
index 04eabbe9050df240beae016519c0946ded65d8ba..964c8ddb1aaf1a6e9af89f318f7e7ddb3c42fc8a 100644 (file)
@@ -123,6 +123,20 @@ class TestTriangulationWrapper(unittest.TestCase):
             n_cells = triangulation.n_active_cells()
             self.assertEqual(n_cells, 2)
 
+    def test_adaptive_refinement(self):
+        for dim in self.dim:
+            triangulation = self.build_hyper_cube_triangulation(dim)
+            triangulation.refine_global(1)
+            for cell in triangulation:
+                cell.refine_flag = 'isotropic'
+                break
+            triangulation.execute_coarsening_and_refinement()
+            n_cells = triangulation.n_active_cells()
+            if (dim[0] == '2D'):
+                self.assertEqual(n_cells, 7)
+            else:
+                self.assertEqual(n_cells, 15)
+
     def test_refine_global(self):
         for dim in self.dim:
             triangulation = self.build_hyper_cube_triangulation(dim)

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.