Poster of Linux kernelThe best gift for a Linux geek
GnuPlotIO

GnuPlotIO

Section: C Library Functions (3) Updated: Thu Apr 7 2011
Local index Up
 

NAME

GnuPlotIO -  

SYNOPSIS


#include <gnuplot_io.h>

Inherits MeshOutput< MeshBase >.  

Public Types


enum PlottingProperties { GRID_ON = 1, PNG_OUTPUT = 2 }
 

Public Member Functions


GnuPlotIO (const MeshBase &, const std::string &=std::string('FE 1D Solution'), int properties=0)

virtual void write (const std::string &)

virtual void write_nodal_data (const std::string &, const std::vector< Number > &, const std::vector< std::string > &)

void set_title (const std::string &title)

void use_grid (bool grid)

void set_png_output (bool png_output)

virtual void write_equation_systems (const std::string &, const EquationSystems &)
 

Public Attributes


std::string axes_limits
 

Protected Member Functions


const MeshBase & mesh () const
 

Private Member Functions


void write_solution (const std::string &, const std::vector< Number > *=NULL, const std::vector< std::string > *=NULL)
 

Private Attributes


std::string _title

bool _grid

bool _png_output
 

Detailed Description

This class implements writing meshes using GNUplot, designed for use only with 1D meshes.

Author:

David Knezevic, 2005

Definition at line 40 of file gnuplot_io.h.  

Member Enumeration Documentation

 

enum GnuPlotIO::PlottingPropertiesDefine enumerations to set plotting properties on construction

Enumerator:

GRID_ON
PNG_OUTPUT

Definition at line 47 of file gnuplot_io.h.

                          { GRID_ON    = 1,
                            PNG_OUTPUT = 2};
 

Constructor & Destructor Documentation

 

GnuPlotIO::GnuPlotIO (const MeshBase &mesh, const std::string &title = std::string('FE 1D Solution'), intproperties = 0)Constructor. Takes a reference to a constant mesh object. To set the properties, we input a bitwise OR of the GnuPlotIO::PlottingProperties enumerations, e.g. GnuPlotIO::GRID_ON | GnuPlotIO::PNG_OUTPUT

Definition at line 30 of file gnuplot_io.C.

References _grid, _png_output, GRID_ON, and PNG_OUTPUT.

  :
  MeshOutput<MeshBase> (mesh),
  _title(title)
{
  _grid       = (mesh_properties & GRID_ON);
  _png_output = (mesh_properties & PNG_OUTPUT);
}
 

Member Function Documentation

 

const MeshBase & MeshOutput< MeshBase >::mesh () const [protected, inherited]Returns the object as a read-only reference.

Referenced by PostscriptIO::write(), FroIO::write(), MEDITIO::write_ascii(), EnsightIO::write_geometry_ascii(), EnsightIO::write_scalar_ascii(), write_solution(), DivaIO::write_stream(), and EnsightIO::write_vector_ascii().  

void GnuPlotIO::set_png_output (boolpng_output) [inline]Write output to a .png file using gnuplot

Definition at line 87 of file gnuplot_io.h.

References _png_output.

{ _png_output = png_output; }
 

void GnuPlotIO::set_title (const std::string &title) [inline]Set title of plot

Definition at line 76 of file gnuplot_io.h.

References _title.

{ _title = title; }
 

void GnuPlotIO::use_grid (boolgrid) [inline]Turn grid on or off.

Definition at line 81 of file gnuplot_io.h.

References _grid.

{ _grid = grid; }
 

void GnuPlotIO::write (const std::string &fname) [virtual]Write the mesh to the specified file.

Implements MeshOutput< MeshBase >.

Definition at line 41 of file gnuplot_io.C.

References libMesh::processor_id(), and write_solution().

{

  if(libMesh::processor_id() == 0)
    this->write_solution(fname);

}
 

virtual void MeshOutput< MeshBase >::write_equation_systems (const std::string &, const EquationSystems &) [virtual, inherited]This method implements writing a mesh with data to a specified file where the data is taken from the EquationSystems object.

Reimplemented in VTKIO.  

void GnuPlotIO::write_nodal_data (const std::string &fname, const std::vector< Number > &soln, const std::vector< std::string > &names) [virtual]This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are provided.

Reimplemented from MeshOutput< MeshBase >.

Definition at line 49 of file gnuplot_io.C.

References libMesh::processor_id(), and write_solution().

{
  if(libMesh::processor_id() == 0)
    this->write_solution(fname, &soln, &names);

}
 

void GnuPlotIO::write_solution (const std::string &fname, const std::vector< Number > *soln = NULL, const std::vector< std::string > *names = NULL) [private]This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are optionally provided. This will write an ASCII file.

Definition at line 61 of file gnuplot_io.C.

References _grid, _png_output, _title, MeshBase::active_elements_begin(), MeshBase::active_elements_end(), MeshBase::active_local_elements_begin(), MeshBase::active_local_elements_end(), axes_limits, Elem::get_node(), MeshOutput< MeshBase >::mesh(), MeshBase::mesh_dimension(), MeshBase::n_active_elem(), Elem::n_nodes(), Elem::neighbor(), Elem::node(), MeshBase::point(), and libMesh::processor_id().

Referenced by write(), and write_nodal_data().

{
  libmesh_assert(libMesh::processor_id() == 0);

  const MeshBase& mesh = MeshOutput<MeshBase>::mesh();

  std::stringstream data_stream_name;
  data_stream_name << fname << '_data';
  const std::string data_file_name = data_stream_name.str();
  
  // This class is designed only for use with 1D meshes
  libmesh_assert (mesh.mesh_dimension() == 1);

  // Make sure we have a solution to plot
  libmesh_assert ((names != NULL) && (soln != NULL));

  // Create an output stream for script file
  std::ofstream out(fname.c_str());

  // Make sure it opened correctly
  if (!out.good())
    libmesh_file_error(fname.c_str());

  // The number of variables in the equation system
  const unsigned int n_vars = names->size();
  
  // Write header to stream
  out << '# This file was generated by gnuplot_io.C
      << '# Stores 1D solution data in GNUplot format
      << '# Execute this by loading gnuplot and typing '
      << ''call '' << fname << '''
      << 'reset
      << 'set title '' << _title << ''
      << 'set xlabel 'x'
      << 'set autoscale
      << 'set xtics nomirror;
      
  if(_grid)
  {
    // construct string for xtic positions at element edges
    std::string xtics;
    std::stringstream xtics_stream;

    MeshBase::const_element_iterator it = mesh.active_local_elements_begin();
    const MeshBase::const_element_iterator end_it =
      mesh.active_local_elements_end();

    unsigned int count = 0;
    unsigned int n_active_elem = mesh.n_active_elem();

    for( ; it != end_it; ++it)
    {
      Elem* el = *it;
      
      // if el is the left edge of the mesh, print its left node position
      if(el->neighbor(0) == NULL)
      {
        xtics_stream << ''' ' << (*(el->get_node(0)))(0) << ', \;
      }
      xtics_stream << ''' ' << (*(el->get_node(1)))(0);

      if(count+1 != n_active_elem)
      {
         xtics_stream << ', \;
      }
      count++;
    }
    xtics = xtics_stream.str();
    
  
    out << 'set x2tics (' << xtics << ')et grid noxtics noytics x2tics;
  }

  if(_png_output)
  {
    out << 'set terminal png;
    out << 'set output '' << fname << '.png';
  }

  out << 'plot '
      << axes_limits
      << ' '' << data_file_name << '' using 1:2 title '' << (*names)[0]
      << '' with lines';
  if(n_vars > 1)
  {
    for(unsigned int i=1; i<n_vars; i++)
    {
      out << ', \'' << data_file_name << '' using 1:' << i+2 
          << ' title '' << (*names)[i] << '' with lines';
    }
  }

  out.close();


  // Create an output stream for data file
  std::ofstream data(data_file_name.c_str());

  if (!data.good())
    {
      std::cerr << 'ERROR: opening output data file ' << std::endl;
      libmesh_error();
    }

  // get ordered nodal data using a map
  typedef std::pair<Real, std::vector<Number> > key_value_pair;
  typedef std::map<Real, std::vector<Number> > map_type;
  typedef map_type::iterator map_iterator;

  map_type node_map;


  MeshBase::const_element_iterator       it  = mesh.active_elements_begin();
  const MeshBase::const_element_iterator end = mesh.active_elements_end(); 

  for ( ; it != end; ++it)
  {
    const Elem* elem = *it;

    for(unsigned int i=0; i<elem->n_nodes(); i++)
    {
      std::vector<Number> values;

      // Get the global id of the node
      unsigned int global_id = elem->node(i);

      for(unsigned int c=0; c<n_vars; c++)
      {
        values.push_back( (*soln)[global_id*n_vars + c] );
      }

      node_map[ mesh.point(global_id)(0) ] = values;
    }
  }

    
  map_iterator map_it = node_map.begin();
  const map_iterator end_map_it = node_map.end();

  for( ; map_it != end_map_it; ++map_it)
  {
    key_value_pair kvp = *map_it;
    std::vector<Number> values = kvp.second;

    data << kvp.first << '	';

    for(unsigned int i=0; i<values.size(); i++)
    {
      data << values[i] << '	';
    }

    data << ';
  }

  data.close();
}
 

Member Data Documentation

 

bool GnuPlotIO::_grid [private]

Definition at line 111 of file gnuplot_io.h.

Referenced by GnuPlotIO(), use_grid(), and write_solution().  

bool GnuPlotIO::_png_output [private]

Definition at line 112 of file gnuplot_io.h.

Referenced by GnuPlotIO(), set_png_output(), and write_solution().  

std::string GnuPlotIO::_title [private]

Definition at line 109 of file gnuplot_io.h.

Referenced by set_title(), and write_solution().  

std::string GnuPlotIO::axes_limitsGNUplot automatically adjusts the x and y-axes of 2D plots to 'zoom in' on the data. You can set this string to force GNUplot to maintain a fixed set of axes. Example: axes_limits = '[0:1] [0:1]' would force x and y to be plotted on the range 0<=x<=1 and 0<=y<=1 regardless of where the data lie.

Definition at line 97 of file gnuplot_io.h.

Referenced by write_solution().

 

Author

Generated automatically by Doxygen for libMesh from the source code.


 

Index

NAME
SYNOPSIS
Public Types
Public Member Functions
Public Attributes
Protected Member Functions
Private Member Functions
Private Attributes
Detailed Description
Member Enumeration Documentation
enum GnuPlotIO::PlottingPropertiesDefine enumerations to set plotting properties on construction
Constructor & Destructor Documentation
GnuPlotIO::GnuPlotIO (const MeshBase &mesh, const std::string &title = std::string('FE 1D Solution'), intproperties = 0)Constructor. Takes a reference to a constant mesh object. To set the properties, we input a bitwise OR of the GnuPlotIO::PlottingProperties enumerations, e.g. GnuPlotIO::GRID_ON | GnuPlotIO::PNG_OUTPUT
Member Function Documentation
const MeshBase & MeshOutput< MeshBase >::mesh () const [protected, inherited]Returns the object as a read-only reference.
void GnuPlotIO::set_png_output (boolpng_output) [inline]Write output to a .png file using gnuplot
void GnuPlotIO::set_title (const std::string &title) [inline]Set title of plot
void GnuPlotIO::use_grid (boolgrid) [inline]Turn grid on or off.
void GnuPlotIO::write (const std::string &fname) [virtual]Write the mesh to the specified file.
virtual void MeshOutput< MeshBase >::write_equation_systems (const std::string &, const EquationSystems &) [virtual, inherited]This method implements writing a mesh with data to a specified file where the data is taken from the EquationSystems object.
void GnuPlotIO::write_nodal_data (const std::string &fname, const std::vector< Number > &soln, const std::vector< std::string > &names) [virtual]This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are provided.
void GnuPlotIO::write_solution (const std::string &fname, const std::vector< Number > *soln = NULL, const std::vector< std::string > *names = NULL) [private]This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are optionally provided. This will write an ASCII file.
Member Data Documentation
bool GnuPlotIO::_grid [private]
bool GnuPlotIO::_png_output [private]
std::string GnuPlotIO::_title [private]
std::string GnuPlotIO::axes_limitsGNUplot automatically adjusts the x and y-axes of 2D plots to 'zoom in' on the data. You can set this string to force GNUplot to maintain a fixed set of axes. Example: axes_limits = '[0:1] [0:1]' would force x and y to be plotted on the range 0<=x<=1 and 0<=y<=1 regardless of where the data lie.
Author

This document was created by man2html, using the manual pages.
Time: 21:47:15 GMT, April 16, 2011