]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Basic building block for exporting grids in MathGL format (Known bug: Not standard...
authoryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 14 Apr 2013 12:57:32 +0000 (12:57 +0000)
committeryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 14 Apr 2013 12:57:32 +0000 (12:57 +0000)
git-svn-id: https://svn.dealii.org/trunk@29267 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/grid/grid_out.h
deal.II/source/grid/grid_out.cc

index 77710625060559096aaec79d302007e3a8cdcab0..e9c75bd23fc2d7363ec24b9f9c9a95a789d86432 100644 (file)
@@ -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 <int dim, int spacedim>
   void write_svg (const Triangulation<dim,spacedim> &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 <code>mglview</code>, which is bundled with MathGL.  With
+   * <code>mglview</code> 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 <int dim>
   void write_mathgl (const Triangulation<dim> &tria,
@@ -1061,8 +1076,8 @@ public:
    */
   template <int dim, int spacedim>
   void write (const Triangulation<dim,spacedim> &tria,
-              std::ostream             &out,
-              const OutputFormat        output_format,
+              std::ostream                      &out,
+              const OutputFormat                 output_format,
               const Mapping<dim,spacedim>       *mapping=0) const;
 
   /**
@@ -1070,7 +1085,7 @@ public:
    */
   template <int dim, int spacedim>
   void write (const Triangulation<dim,spacedim> &tria,
-              std::ostream             &out,
+              std::ostream                      &out,
               const Mapping<dim,spacedim>       *mapping=0) const;
 
   /**
index 7bdb461df3e512d25de310020931ecce8e3367d8..526ca8aaf247ad741406215889441fd874c63fbf 100644 (file)
@@ -365,13 +365,19 @@ namespace GridOutFlags
   {}
 
   MathGL::MathGL ()
+    :
+    draw_bounding_box (false) // box
   {}
 
   void MathGL::declare_parameters (ParameterHandler &param)
-  {}
+  {
+    param.declare_entry ("Draw bounding box", "false", Patterns::Bool ());
+  }
 
   void MathGL::parse_parameters (ParameterHandler &param)
-  {}
+  {
+    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 <int dim>
 void GridOut::write_mathgl (const Triangulation<dim> &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 <vertex order> <vertex indices>"
+      << "\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 <id> <vertices>"
+      << "\n#" << "\n";
+  
+  // run over all active cells and write out a list of
+  // xyz-coordinates that correspond to vertices
+  typename dealii::Triangulation<dim>::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; i<dim; ++i)
+       {
+         // if (cell->direction_flag ()==true)
+         //   out << "\ntrue";
+         // else
+         //   out << "\nfalse";
+
+         out << "\nlist " << axes[i] << cell_global_index << " ";
+         for (unsigned int j=0; j<GeometryInfo<dim>::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 <vertex order> <id> <style>"
+      << "\n#" << "\n";
+  for (unsigned int i=0; i<tria.n_active_cells (); ++i)
+    {
+      out << "\nquadplot f ";
+      for (unsigned int j=0; j<dim; ++j)
+       out << axes[j] << i << " "; 
+      out << "\'k#\'";
+    }
+  out << "\n";
+  
+  // (vi) write footer
+  out << "\n#" 
+      << "\n#" 
+      << "\n#" << "\n";
+  
+  // make sure everything now gets to the output stream
+  out.flush ();  
+  AssertThrow (out, ExcIO ());
 }
 
 

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.