libMesh::GnuPlotIO Class Reference

#include <gnuplot_io.h>

Inheritance diagram for libMesh::GnuPlotIO:

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 &) libmesh_override
 
virtual void write_nodal_data (const std::string &, const std::vector< Number > &, const std::vector< std::string > &) libmesh_override
 
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 &, const std::set< std::string > *system_names=libmesh_nullptr)
 
virtual void write_nodal_data (const std::string &, const NumericVector< Number > &, const std::vector< std::string > &)
 
unsigned int & ascii_precision ()
 

Public Attributes

std::string axes_limits
 

Protected Member Functions

const MeshBasemesh () const
 

Protected Attributes

const bool _is_parallel_format
 
const bool _serial_only_needed_on_proc_0
 

Private Member Functions

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

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
Date
2005

Definition at line 43 of file gnuplot_io.h.

Member Enumeration Documentation

Define enumerations to set plotting properties on construction

Enumerator
GRID_ON 
PNG_OUTPUT 

Definition at line 50 of file gnuplot_io.h.

Constructor & Destructor Documentation

libMesh::GnuPlotIO::GnuPlotIO ( const MeshBase mesh_in,
const std::string &  title = std::string("FE 1D Solution"),
int  properties = 0 
)
explicit

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 32 of file gnuplot_io.C.

References _grid, _png_output, GRID_ON, and PNG_OUTPUT.

35  :
36  MeshOutput<MeshBase> (mesh_in),
37  _title(title)
38 {
39  _grid = (mesh_properties & GRID_ON);
40  _png_output = (mesh_properties & PNG_OUTPUT);
41 }
std::string _title
Definition: gnuplot_io.h:119

Member Function Documentation

unsigned int& libMesh::MeshOutput< MeshBase >::ascii_precision ( )
inherited

Return/set the precision to use when writing ASCII files.

By default we use numeric_limits<Real>::digits10 + 2, which should be enough to write out to ASCII and get the exact same Real back when reading in.

Referenced by libMesh::TecplotIO::write_ascii(), libMesh::GMVIO::write_ascii_new_impl(), and libMesh::GMVIO::write_ascii_old_impl().

void libMesh::GnuPlotIO::set_png_output ( bool  png_output)
inline

Write output to a .png file using gnuplot

Definition at line 97 of file gnuplot_io.h.

References _png_output.

97 { _png_output = png_output; }
void libMesh::GnuPlotIO::set_title ( const std::string &  title)
inline

Set title of plot

Definition at line 86 of file gnuplot_io.h.

References _title.

86 { _title = title; }
std::string _title
Definition: gnuplot_io.h:119
void libMesh::GnuPlotIO::use_grid ( bool  grid)
inline

Turn grid on or off.

Definition at line 91 of file gnuplot_io.h.

References _grid.

91 { _grid = grid; }
void libMesh::GnuPlotIO::write ( const std::string &  fname)
virtual

Write the mesh to the specified file.

Implements libMesh::MeshOutput< MeshBase >.

Definition at line 43 of file gnuplot_io.C.

References write_solution().

44 {
45  this->write_solution(fname);
46 }
void write_solution(const std::string &, const std::vector< Number > *=libmesh_nullptr, const std::vector< std::string > *=libmesh_nullptr)
Definition: gnuplot_io.C:59
virtual void libMesh::MeshOutput< MeshBase >::write_equation_systems ( const std::string &  ,
const EquationSystems ,
const std::set< std::string > *  system_names = libmesh_nullptr 
)
virtualinherited

This method implements writing a mesh with data to a specified file where the data is taken from the EquationSystems object.

Reimplemented in libMesh::NameBasedIO.

Referenced by libMesh::Nemesis_IO::write_timestep(), and libMesh::ExodusII_IO::write_timestep().

void libMesh::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 libMesh::MeshOutput< MeshBase >.

Definition at line 48 of file gnuplot_io.C.

References write_solution().

51 {
52  LOG_SCOPE("write_nodal_data()", "GnuPlotIO");
53  this->write_solution(fname, &soln, &names);
54 }
void write_solution(const std::string &, const std::vector< Number > *=libmesh_nullptr, const std::vector< std::string > *=libmesh_nullptr)
Definition: gnuplot_io.C:59
virtual void libMesh::MeshOutput< MeshBase >::write_nodal_data ( const std::string &  ,
const NumericVector< Number > &  ,
const std::vector< std::string > &   
)
virtualinherited

This method should be overridden by "parallel" output formats for writing nodal data. Instead of getting a localized copy of the nodal solution vector, it is passed a NumericVector of type=PARALLEL which is in node-major order i.e. (u0,v0,w0, u1,v1,w1, u2,v2,w2, u3,v3,w3, ...) and contains n_nodes*n_vars total entries. Then, it is up to the individual I/O class to extract the required solution values from this vector and write them in parallel.

If not implemented, localizes the parallel vector into a std::vector and calls the other version of this function.

Reimplemented in libMesh::Nemesis_IO.

void libMesh::GnuPlotIO::write_solution ( const std::string &  fname,
const std::vector< Number > *  soln = libmesh_nullptr,
const std::vector< std::string > *  names = libmesh_nullptr 
)
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 59 of file gnuplot_io.C.

References _grid, _png_output, _title, libMesh::MeshBase::active_elements_begin(), libMesh::MeshBase::active_elements_end(), axes_limits, data, libMesh::libmesh_assert(), libmesh_nullptr, libMesh::MeshOutput< MeshBase >::mesh(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::mesh_dimension(), libMesh::MeshBase::n_active_elem(), libMesh::Elem::n_nodes(), n_vars, libMesh::Elem::neighbor_ptr(), libMesh::Elem::node_id(), libMesh::Elem::point(), libMesh::MeshBase::point(), libMesh::processor_id(), and libMesh::Real.

Referenced by write(), and write_nodal_data().

62 {
63  // Even when writing on a serialized DistributedMesh, we expect
64  // non-proc-0 help with calls like n_active_elem
65  // libmesh_assert_equal_to (this->mesh().processor_id(), 0);
66 
67  const MeshBase & the_mesh = MeshOutput<MeshBase>::mesh();
68 
69  dof_id_type n_active_elem = the_mesh.n_active_elem();
70 
71  if (this->mesh().processor_id() == 0)
72  {
73  std::stringstream data_stream_name;
74  data_stream_name << fname << "_data";
75  const std::string data_file_name = data_stream_name.str();
76 
77  // This class is designed only for use with 1D meshes
78  libmesh_assert_equal_to (the_mesh.mesh_dimension(), 1);
79 
80  // Make sure we have a solution to plot
81  libmesh_assert ((names != libmesh_nullptr) && (soln != libmesh_nullptr));
82 
83  // Create an output stream for script file
84  std::ofstream out_stream(fname.c_str());
85 
86  // Make sure it opened correctly
87  if (!out_stream.good())
88  libmesh_file_error(fname.c_str());
89 
90  // The number of variables in the equation system
91  const unsigned int n_vars =
92  cast_int<unsigned int>(names->size());
93 
94  // Write header to stream
95  out_stream << "# This file was generated by gnuplot_io.C\n"
96  << "# Stores 1D solution data in GNUplot format\n"
97  << "# Execute this by loading gnuplot and typing "
98  << "\"call '" << fname << "'\"\n"
99  << "reset\n"
100  << "set title \"" << _title << "\"\n"
101  << "set xlabel \"x\"\n"
102  << "set xtics nomirror\n";
103 
104  // Loop over the elements to find the minimum and maximum x values,
105  // and also to find the element boundaries to write out as xtics
106  // if requested.
107  Real x_min=0., x_max=0.;
108 
109  // construct string for xtic positions at element edges
110  std::stringstream xtics_stream;
111 
112  MeshBase::const_element_iterator it = the_mesh.active_elements_begin();
113  const MeshBase::const_element_iterator end_it =
114  the_mesh.active_elements_end();
115 
116  unsigned int count = 0;
117 
118  for( ; it != end_it; ++it)
119  {
120  const Elem * el = *it;
121 
122  // if el is the left edge of the mesh, print its left node position
123  if (el->neighbor_ptr(0) == libmesh_nullptr)
124  {
125  x_min = (el->point(0))(0);
126  xtics_stream << "\"\" " << x_min << ", \\\n";
127  }
128  if (el->neighbor_ptr(1) == libmesh_nullptr)
129  {
130  x_max = (el->point(1))(0);
131  }
132  xtics_stream << "\"\" " << (el->point(1))(0);
133 
134  if(count+1 != n_active_elem)
135  {
136  xtics_stream << ", \\\n";
137  }
138  count++;
139  }
140 
141  out_stream << "set xrange [" << x_min << ":" << x_max << "]\n";
142 
143  if(_grid)
144  out_stream << "set x2tics (" << xtics_stream.str() << ")\nset grid noxtics noytics x2tics\n";
145 
146  if(_png_output)
147  {
148  out_stream << "set terminal png\n";
149  out_stream << "set output \"" << fname << ".png\"\n";
150  }
151 
152  out_stream << "plot "
153  << axes_limits
154  << " \"" << data_file_name << "\" using 1:2 title \"" << (*names)[0]
155  << "\" with lines";
156  if(n_vars > 1)
157  {
158  for(unsigned int i=1; i<n_vars; i++)
159  {
160  out_stream << ", \\\n\"" << data_file_name << "\" using 1:" << i+2
161  << " title \"" << (*names)[i] << "\" with lines";
162  }
163  }
164 
165  out_stream.close();
166 
167 
168  // Create an output stream for data file
169  std::ofstream data(data_file_name.c_str());
170 
171  if (!data.good())
172  libmesh_error_msg("ERROR: opening output data file " << data_file_name);
173 
174  // get ordered nodal data using a map
175  typedef std::pair<Real, std::vector<Number> > key_value_pair;
176  typedef std::map<Real, std::vector<Number> > map_type;
177  typedef map_type::iterator map_iterator;
178 
179  map_type node_map;
180 
181 
182  it = the_mesh.active_elements_begin();
183 
184  for ( ; it != end_it; ++it)
185  {
186  const Elem * elem = *it;
187 
188  for(unsigned int i=0; i<elem->n_nodes(); i++)
189  {
190  std::vector<Number> values;
191 
192  // Get the global id of the node
193  dof_id_type global_id = elem->node_id(i);
194 
195  for(unsigned int c=0; c<n_vars; c++)
196  {
197  values.push_back( (*soln)[global_id*n_vars + c] );
198  }
199 
200  node_map[ the_mesh.point(global_id)(0) ] = values;
201  }
202  }
203 
204 
205  map_iterator map_it = node_map.begin();
206  const map_iterator end_map_it = node_map.end();
207 
208  for( ; map_it != end_map_it; ++map_it)
209  {
210  key_value_pair kvp = *map_it;
211  std::vector<Number> values = kvp.second;
212 
213  data << kvp.first << "\t";
214 
215  for (std::size_t i=0; i<values.size(); i++)
216  data << values[i] << "\t";
217 
218  data << "\n";
219  }
220 
221  data.close();
222  }
223 }
std::string _title
Definition: gnuplot_io.h:119
const class libmesh_nullptr_t libmesh_nullptr
const MT & mesh() const
Definition: mesh_output.h:216
const unsigned int n_vars
Definition: tecplot_io.C:68
libmesh_assert(j)
std::string axes_limits
Definition: gnuplot_io.h:107
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IterBase * data
processor_id_type processor_id()
Definition: libmesh_base.h:96
uint8_t dof_id_type
Definition: id_types.h:64

Member Data Documentation

bool libMesh::GnuPlotIO::_grid
private

Definition at line 121 of file gnuplot_io.h.

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

const bool libMesh::MeshOutput< MeshBase >::_is_parallel_format
protectedinherited

Flag specifying whether this format is parallel-capable. If this is false (default) I/O is only permitted when the mesh has been serialized.

Definition at line 141 of file mesh_output.h.

Referenced by libMesh::FroIO::write(), libMesh::PostscriptIO::write(), and libMesh::EnsightIO::write().

bool libMesh::GnuPlotIO::_png_output
private

Definition at line 122 of file gnuplot_io.h.

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

const bool libMesh::MeshOutput< MeshBase >::_serial_only_needed_on_proc_0
protectedinherited

Flag specifying whether this format can be written by only serializing the mesh to processor zero

If this is false (default) the mesh will be serialized to all processors

Definition at line 150 of file mesh_output.h.

std::string libMesh::GnuPlotIO::_title
private

Definition at line 119 of file gnuplot_io.h.

Referenced by set_title(), and write_solution().

std::string libMesh::GnuPlotIO::axes_limits

GNUplot 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 107 of file gnuplot_io.h.

Referenced by write_solution().


The documentation for this class was generated from the following files: