From cff8228036e02319bd1d53eb153011256a22a98d Mon Sep 17 00:00:00 2001 From: "Toby D. Young" Date: Sun, 14 Apr 2013 12:57:32 +0000 Subject: [PATCH] Basic building block for exporting grids in MathGL format (Known bug: Not standard cell orientations are not properly implemented). git-svn-id: https://svn.dealii.org/trunk@29267 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/include/deal.II/grid/grid_out.h | 29 ++++-- deal.II/source/grid/grid_out.cc | 124 +++++++++++++++++++++++- 2 files changed, 143 insertions(+), 10 deletions(-) diff --git a/deal.II/include/deal.II/grid/grid_out.h b/deal.II/include/deal.II/grid/grid_out.h index 7771062506..e9c75bd23f 100644 --- a/deal.II/include/deal.II/grid/grid_out.h +++ b/deal.II/include/deal.II/grid/grid_out.h @@ -705,6 +705,11 @@ namespace GridOutFlags */ MathGL (); + /** + * Draw a bounding box around the graph. + */ + bool draw_bounding_box; + /** * Declare parameters in ParameterHandler. */ @@ -1041,15 +1046,25 @@ public: */ template void write_svg (const Triangulation &tria, - std::ostream &out) const; + std::ostream &out) const; void write_svg (const Triangulation<2,2> &tria, - std::ostream &out) const; + std::ostream &out) const; /** - * Write triangulation in MathGL format. + * Write triangulation in MathGL script format. To interpret this + * file a version of MathGL>=2.0.0 is required. * - * Not implemented for the codimension one case. + * To get a handle on the resultant MathGL script within a graphical + * environment an interpreter is needed. A suggestion to start with + * is mglview, which is bundled with MathGL. With + * mglview can interpret and display small-to-medium + * MathGL scripts in a graphical window and enables conversion to + * other formats such as EPS, PNG, JPG, SVG, as well as view/display + * animations. Some minor editing, such as modifying the lighting or + * alpha channels, can also be done. + * + * @note Not implemented for the codimensional one case. */ template void write_mathgl (const Triangulation &tria, @@ -1061,8 +1076,8 @@ public: */ template void write (const Triangulation &tria, - std::ostream &out, - const OutputFormat output_format, + std::ostream &out, + const OutputFormat output_format, const Mapping *mapping=0) const; /** @@ -1070,7 +1085,7 @@ public: */ template void write (const Triangulation &tria, - std::ostream &out, + std::ostream &out, const Mapping *mapping=0) const; /** diff --git a/deal.II/source/grid/grid_out.cc b/deal.II/source/grid/grid_out.cc index 7bdb461df3..526ca8aaf2 100644 --- a/deal.II/source/grid/grid_out.cc +++ b/deal.II/source/grid/grid_out.cc @@ -365,13 +365,19 @@ namespace GridOutFlags {} MathGL::MathGL () + : + draw_bounding_box (false) // box {} void MathGL::declare_parameters (ParameterHandler ¶m) - {} + { + param.declare_entry ("Draw bounding box", "false", Patterns::Bool ()); + } void MathGL::parse_parameters (ParameterHandler ¶m) - {} + { + draw_bounding_box = param.get_bool ("Draw bounding box"); + } } // end namespace GridOutFlags @@ -1972,6 +1978,7 @@ void GridOut::write_svg(const Triangulation<2,2> &tria, std::ostream &out) const } + template <> void GridOut::write_mathgl (const Triangulation<1> &, std::ostream &) const @@ -1984,7 +1991,118 @@ template void GridOut::write_mathgl (const Triangulation &tria, std::ostream &out) const { - Assert (false, ExcNotImplemented()); + AssertThrow (out, ExcIO ()); + Assert (dim!=3, ExcNotImplemented ()); + + // (i) write header + if (true) + { + // block this to have local variables destroyed after use + const std::time_t time1 = std::time (0); + const std::tm *time = std::localtime (&time1); + + out << "\n#" + << "\n# This file was generated by the deal.II library." + << "\n# Date = " + << time->tm_year+1900 << "/" + << std::setfill('0') << std::setw (2) << time->tm_mon+1 << "/" + << std::setfill('0') << std::setw (2) << time->tm_mday + << "\n# Time = " + << std::setfill('0') << std::setw (2) << time->tm_hour << ":" + << std::setfill('0') << std::setw (2) << time->tm_min << ":" + << std::setfill('0') << std::setw (2) << time->tm_sec + << "\n#" + << "\n# For a description of the MathGL script format see the MathGL manual. " + << "\n#" + << "\n# Note: This file is understood by MathGL v2.1 and higher only, and can " + << "\n# be quickly viewed in a graphical environment using "mglview". " + << "\n#" << "\n" + ; + } + + // define a helper to keep loops approximately dim-independent + // since MathGL labels axes as x, y, z + Assert (dim<=3, ExcInternalError ()); + const std::string axes = "xyz"; + + // (ii) write preamble and graphing tweaks + out << "\n#" + << "\n# Preamble." + << "\n#" << "\n"; + + if (mathgl_flags.draw_bounding_box) + out << "\nbox"; + + // Default, cf. MathGL / gnuplot. + out << "\nsetsize 800 800"; + + out << "\n"; + + // (iii) write vertex ordering + out << "\n#" + << "\n# Vertex ordering." + << "\n# list " + << "\n#" << "\n"; + + // todo: This denotes the natural ordering of vertices, but it needs + // to check this is really always true for a given grid (it's not + // true in step-1 grid-2 for instance). + out << "\nlist f 0 1 2 3" + << "\n"; + + // (iv) write a list of vertices of cells + out << "\n#" + << "\n# List of vertices." + << "\n# list " + << "\n#" << "\n"; + + // run over all active cells and write out a list of + // xyz-coordinates that correspond to vertices + typename dealii::Triangulation::active_cell_iterator + cell=tria.begin_active (), + endc=tria.end (); + + // No global indices in deal.II, so we make one up here. + unsigned int cell_global_index = 0; + + for (; cell!=endc; ++cell, ++cell_global_index) + { + for (unsigned int i=0; idirection_flag ()==true) + // out << "\ntrue"; + // else + // out << "\nfalse"; + + out << "\nlist " << axes[i] << cell_global_index << " "; + for (unsigned int j=0; j::vertices_per_cell; ++j) + out << cell->vertex(j)[i] << " "; + } + out << '\n'; + } + + // (v) write out cells to plot as quadplot objects + out << "\n#" + << "\n# List of cells to quadplot." + << "\n# quadplot