From: Bruno Turcksin Date: Mon, 27 Jun 2016 15:16:12 +0000 (-0400) Subject: Output triangulation and add adaptive mesh refinement. X-Git-Tag: v8.5.0-rc1~809^2~11 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a95e8defd8072b538375c1d32f0736652677cda;p=dealii.git Output triangulation and add adaptive mesh refinement. --- diff --git a/include/deal.II/python/triangulation_wrapper.h b/include/deal.II/python/triangulation_wrapper.h index b63e7472e6..d0f1d433da 100644 --- a/include/deal.II/python/triangulation_wrapper.h +++ b/include/deal.II/python/triangulation_wrapper.h @@ -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. */ diff --git a/source/python/export_triangulation.cc b/source/python/export_triangulation.cc index 4b7917c2eb..63b855cbd6 100644 --- a/source/python/export_triangulation.cc +++ b/source/python/export_triangulation.cc @@ -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.", diff --git a/source/python/triangulation_wrapper.cc b/source/python/triangulation_wrapper.cc index d78cdea259..eb1010def3 100644 --- a/source/python/triangulation_wrapper.cc +++ b/source/python/triangulation_wrapper.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -181,6 +182,46 @@ namespace PyDealII cells.push_back(CellAccessorWrapper(triangulation_wrapper, cell->level(), cell->index())); } + + + + template + void write(const std::string &filename, + const std::string &format, + void *triangulation) + { + dealii::Triangulation *tria = + static_cast*>(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*>(triangulation); + tria->execute_coarsening_and_refinement(); + } + else if ((dim == 2) && (spacedim == 3)) + { + dealii::Triangulation<2,3> *tria = + static_cast*>(triangulation); + tria->execute_coarsening_and_refinement(); + } + else + { + dealii::Triangulation<3,3> *tria = + static_cast*>(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) { diff --git a/tests/python/triangulation_wrapper.py b/tests/python/triangulation_wrapper.py index 04eabbe905..964c8ddb1a 100644 --- a/tests/python/triangulation_wrapper.py +++ b/tests/python/triangulation_wrapper.py @@ -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)