libMesh
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
libMesh::GnuPlotIO Class Reference

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

#include <gnuplot_io.h>

Inheritance diagram for libMesh::GnuPlotIO:
[legend]

Public Types

enum  PlottingProperties { GRID_ON = 1, PNG_OUTPUT = 2 }
 Define enumerations to set plotting properties on construction. More...
 

Public Member Functions

 GnuPlotIO (const MeshBase &, const std::string &=std::string("FE 1D Solution"), int properties=0)
 Constructor. More...
 
virtual void write (const std::string &) override
 Write the mesh to the specified file. More...
 
virtual void write_nodal_data (const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
 This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are provided. More...
 
void set_title (const std::string &title)
 Set title of plot. More...
 
void use_grid (bool grid)
 Turn grid on or off. More...
 
void set_png_output (bool png_output)
 Write output to a .png file using gnuplot. More...
 
virtual void write_equation_systems (const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
 This method implements writing a mesh with data to a specified file where the data is taken from the EquationSystems object. More...
 
virtual void write_discontinuous_equation_systems (const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
 This method implements writing a mesh with discontinuous data to a specified file where the data is taken from the EquationSystems object. More...
 
virtual void write_nodal_data (const std::string &, const NumericVector< Number > &, const std::vector< std::string > &)
 This method may be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data (const std::string &, const EquationSystems &, const std::set< std::string > *)
 This method should be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data_discontinuous (const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
 This method implements writing a mesh with discontinuous data to a specified file where the nodal data and variables names are provided. More...
 
unsigned intascii_precision ()
 Return/set the precision to use when writing ASCII files. More...
 

Public Attributes

std::string axes_limits
 GNUplot automatically adjusts the x and y-axes of 2D plots to "zoom in" on the data. More...
 

Protected Member Functions

const MeshBasemesh () const
 

Protected Attributes

const bool _is_parallel_format
 Flag specifying whether this format is parallel-capable. More...
 
const bool _serial_only_needed_on_proc_0
 Flag specifying whether this format can be written by only serializing the mesh to processor zero. More...
 

Private Member Functions

void write_solution (const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
 This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are optionally provided. More...
 

Private Attributes

std::string _title
 
bool _grid
 
bool _png_output
 
const MeshBase *const _obj
 A pointer to a constant object. More...
 
unsigned int _ascii_precision
 Precision to use when writing ASCII files. More...
 

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

◆ PlottingProperties

Define enumerations to set plotting properties on construction.

Enumerator
GRID_ON 
PNG_OUTPUT 

Definition at line 50 of file gnuplot_io.h.

50  { GRID_ON = 1,
51  PNG_OUTPUT = 2};

Constructor & Destructor Documentation

◆ GnuPlotIO()

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.

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

References _grid, _png_output, GRID_ON, and PNG_OUTPUT.

Member Function Documentation

◆ ascii_precision()

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

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

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

Definition at line 257 of file mesh_output.h.

258 {
259  return _ascii_precision;
260 }

◆ mesh()

const MeshBase & libMesh::MeshOutput< MeshBase >::mesh ( ) const
inlineprotectedinherited
Returns
The object as a read-only reference.

Definition at line 247 of file mesh_output.h.

248 {
250  return *_obj;
251 }

◆ set_png_output()

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.

97 { _png_output = png_output; }

References _png_output.

◆ set_title()

void libMesh::GnuPlotIO::set_title ( const std::string &  title)
inline

Set title of plot.

Definition at line 86 of file gnuplot_io.h.

86 { _title = title; }

References _title.

◆ use_grid()

void libMesh::GnuPlotIO::use_grid ( bool  grid)
inline

Turn grid on or off.

Definition at line 91 of file gnuplot_io.h.

91 { _grid = grid; }

References _grid.

◆ write()

void libMesh::GnuPlotIO::write ( const std::string &  fname)
overridevirtual

Write the mesh to the specified file.

Implements libMesh::MeshOutput< MeshBase >.

Definition at line 43 of file gnuplot_io.C.

44 {
45  this->write_solution(fname);
46 }

References write_solution().

◆ write_discontinuous_equation_systems()

void libMesh::MeshOutput< MeshBase >::write_discontinuous_equation_systems ( const std::string &  fname,
const EquationSystems es,
const std::set< std::string > *  system_names = nullptr 
)
virtualinherited

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

Definition at line 87 of file mesh_output.C.

90 {
91  LOG_SCOPE("write_discontinuous_equation_systems()", "MeshOutput");
92 
93  // We may need to gather and/or renumber a DistributedMesh to output
94  // it, making that const qualifier in our constructor a dirty lie
95  MT & my_mesh = const_cast<MT &>(*_obj);
96 
97  // If we're asked to write data that's associated with a different
98  // mesh, output files full of garbage are the result.
99  libmesh_assert_equal_to(&es.get_mesh(), _obj);
100 
101  // A non-renumbered mesh may not have a contiguous numbering, and
102  // that needs to be fixed before we can build a solution vector.
103  if (my_mesh.max_elem_id() != my_mesh.n_elem() ||
104  my_mesh.max_node_id() != my_mesh.n_nodes())
105  {
106  // If we were allowed to renumber then we should have already
107  // been properly renumbered...
108  libmesh_assert(!my_mesh.allow_renumbering());
109 
110  libmesh_do_once(libMesh::out <<
111  "Warning: This MeshOutput subclass only supports meshes which are contiguously renumbered!"
112  << std::endl;);
113 
114  my_mesh.allow_renumbering(true);
115 
116  my_mesh.renumber_nodes_and_elements();
117 
118  // Not sure what good going back to false will do here, the
119  // renumbering horses have already left the barn...
120  my_mesh.allow_renumbering(false);
121  }
122 
123  MeshSerializer serialize(const_cast<MT &>(*_obj), !_is_parallel_format, _serial_only_needed_on_proc_0);
124 
125  // Build the list of variable names that will be written.
126  std::vector<std::string> names;
127  es.build_variable_names (names, nullptr, system_names);
128 
129  if (!_is_parallel_format)
130  {
131  // Build the nodal solution values & get the variable
132  // names from the EquationSystems object
133  std::vector<Number> soln;
134  es.build_discontinuous_solution_vector (soln, system_names);
135 
136  this->write_nodal_data_discontinuous (fname, soln, names);
137  }
138  else // _is_parallel_format
139  {
140  libmesh_not_implemented();
141  }
142 }

◆ write_equation_systems()

void libMesh::MeshOutput< MeshBase >::write_equation_systems ( const std::string &  fname,
const EquationSystems es,
const std::set< std::string > *  system_names = 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.

Definition at line 31 of file mesh_output.C.

34 {
35  LOG_SCOPE("write_equation_systems()", "MeshOutput");
36 
37  // We may need to gather and/or renumber a DistributedMesh to output
38  // it, making that const qualifier in our constructor a dirty lie
39  MT & my_mesh = const_cast<MT &>(*_obj);
40 
41  // If we're asked to write data that's associated with a different
42  // mesh, output files full of garbage are the result.
43  libmesh_assert_equal_to(&es.get_mesh(), _obj);
44 
45  // A non-renumbered mesh may not have a contiguous numbering, and
46  // that needs to be fixed before we can build a solution vector.
47  if (my_mesh.max_elem_id() != my_mesh.n_elem() ||
48  my_mesh.max_node_id() != my_mesh.n_nodes())
49  {
50  // If we were allowed to renumber then we should have already
51  // been properly renumbered...
52  libmesh_assert(!my_mesh.allow_renumbering());
53 
54  libmesh_do_once(libMesh::out <<
55  "Warning: This MeshOutput subclass only supports meshes which are contiguously renumbered!"
56  << std::endl;);
57 
58  my_mesh.allow_renumbering(true);
59 
60  my_mesh.renumber_nodes_and_elements();
61 
62  // Not sure what good going back to false will do here, the
63  // renumbering horses have already left the barn...
64  my_mesh.allow_renumbering(false);
65  }
66 
68  {
69  MeshSerializer serialize(const_cast<MT &>(*_obj), !_is_parallel_format, _serial_only_needed_on_proc_0);
70 
71  // Build the list of variable names that will be written.
72  std::vector<std::string> names;
73  es.build_variable_names (names, nullptr, system_names);
74 
75  // Build the nodal solution values & get the variable
76  // names from the EquationSystems object
77  std::vector<Number> soln;
78  es.build_solution_vector (soln, system_names);
79 
80  this->write_nodal_data (fname, soln, names);
81  }
82  else // _is_parallel_format
83  this->write_nodal_data (fname, es, system_names);
84 }

◆ write_nodal_data() [1/3]

void libMesh::MeshOutput< MeshBase >::write_nodal_data ( const std::string &  fname,
const EquationSystems es,
const std::set< std::string > *  system_names 
)
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 directly uses EquationSystems current_local_solution vectors to look up nodal values.

If not implemented, reorders the solutions into a nodal-only NumericVector and calls the above version of this function.

Reimplemented in libMesh::Nemesis_IO.

Definition at line 158 of file mesh_output.C.

161 {
162  std::vector<std::string> names;
163  es.build_variable_names (names, nullptr, system_names);
164 
165  std::unique_ptr<NumericVector<Number>> parallel_soln =
166  es.build_parallel_solution_vector(system_names);
167 
168  this->write_nodal_data (fname, *parallel_soln, names);
169 }

◆ write_nodal_data() [2/3]

void libMesh::MeshOutput< MeshBase >::write_nodal_data ( const std::string &  fname,
const NumericVector< Number > &  parallel_soln,
const std::vector< std::string > &  names 
)
virtualinherited

This method may 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.

Definition at line 145 of file mesh_output.C.

148 {
149  // This is the fallback implementation for parallel I/O formats that
150  // do not yet implement proper writing in parallel, and instead rely
151  // on the full solution vector being available on all processors.
152  std::vector<Number> soln;
153  parallel_soln.localize(soln);
154  this->write_nodal_data(fname, soln, names);
155 }

◆ write_nodal_data() [3/3]

void libMesh::GnuPlotIO::write_nodal_data ( const std::string &  fname,
const std::vector< Number > &  soln,
const std::vector< std::string > &  names 
)
overridevirtual

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.

51 {
52  LOG_SCOPE("write_nodal_data()", "GnuPlotIO");
53  this->write_solution(fname, &soln, &names);
54 }

References write_solution().

◆ write_nodal_data_discontinuous()

virtual void libMesh::MeshOutput< MeshBase >::write_nodal_data_discontinuous ( const std::string &  ,
const std::vector< Number > &  ,
const std::vector< std::string > &   
)
inlinevirtualinherited

This method implements writing a mesh with discontinuous data to a specified file where the nodal data and variables names are provided.

Reimplemented in libMesh::ExodusII_IO.

Definition at line 114 of file mesh_output.h.

117  { libmesh_not_implemented(); }

◆ write_solution()

void libMesh::GnuPlotIO::write_solution ( const std::string &  fname,
const std::vector< Number > *  soln = nullptr,
const std::vector< std::string > *  names = 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.

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 != nullptr) && (soln != 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  unsigned int count = 0;
113 
114  for (const auto & el : the_mesh.active_element_ptr_range())
115  {
116  // if el is the left edge of the mesh, print its left node position
117  if (el->neighbor_ptr(0) == nullptr)
118  {
119  x_min = (el->point(0))(0);
120  xtics_stream << "\"\" " << x_min << ", \\\n";
121  }
122  if (el->neighbor_ptr(1) == nullptr)
123  {
124  x_max = (el->point(1))(0);
125  }
126  xtics_stream << "\"\" " << (el->point(1))(0);
127 
128  if (count+1 != n_active_elem)
129  {
130  xtics_stream << ", \\\n";
131  }
132  count++;
133  }
134 
135  out_stream << "set xrange [" << x_min << ":" << x_max << "]\n";
136 
137  if (_grid)
138  out_stream << "set x2tics (" << xtics_stream.str() << ")\nset grid noxtics noytics x2tics\n";
139 
140  if (_png_output)
141  {
142  out_stream << "set terminal png\n";
143  out_stream << "set output \"" << fname << ".png\"\n";
144  }
145 
146  out_stream << "plot "
147  << axes_limits
148  << " \"" << data_file_name << "\" using 1:2 title \"" << (*names)[0]
149  << "\" with lines";
150  if (n_vars > 1)
151  {
152  for (unsigned int i=1; i<n_vars; i++)
153  {
154  out_stream << ", \\\n\"" << data_file_name << "\" using 1:" << i+2
155  << " title \"" << (*names)[i] << "\" with lines";
156  }
157  }
158 
159  out_stream.close();
160 
161 
162  // Create an output stream for data file
163  std::ofstream data(data_file_name.c_str());
164 
165  if (!data.good())
166  libmesh_error_msg("ERROR: opening output data file " << data_file_name);
167 
168  // get ordered nodal data using a map
169  std::map<Real, std::vector<Number>> node_map;
170 
171  for (const auto & elem : the_mesh.active_element_ptr_range())
172  for (const auto & node : elem->node_ref_range())
173  {
174  dof_id_type global_id = node.id();
175 
176  std::vector<Number> values;
177  for (unsigned int c=0; c<n_vars; c++)
178  values.push_back((*soln)[global_id*n_vars + c]);
179 
180  node_map[the_mesh.point(global_id)(0)] = values;
181  }
182 
183  for (const auto & pr : node_map)
184  {
185  const std::vector<Number> & values = pr.second;
186 
187  data << pr.first << "\t";
188 
189  for (const auto & val : values)
190  data << val << "\t";
191 
192  data << "\n";
193  }
194 
195  data.close();
196  }
197 }

References _grid, _png_output, _title, libMesh::MeshBase::active_element_ptr_range(), axes_limits, data, libMesh::libmesh_assert(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshOutput< MeshBase >::mesh(), libMesh::MeshBase::mesh_dimension(), libMesh::MeshBase::n_active_elem(), n_vars, libMesh::MeshBase::point(), and libMesh::Real.

Referenced by write(), and write_nodal_data().

Member Data Documentation

◆ _ascii_precision

unsigned int libMesh::MeshOutput< MeshBase >::_ascii_precision
privateinherited

Precision to use when writing ASCII files.

Definition at line 195 of file mesh_output.h.

◆ _grid

bool libMesh::GnuPlotIO::_grid
private

Definition at line 121 of file gnuplot_io.h.

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

◆ _is_parallel_format

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 172 of file mesh_output.h.

◆ _obj

const MeshBase * const libMesh::MeshOutput< MeshBase >::_obj
privateinherited

A pointer to a constant object.

This allows us to write the object to file.

Definition at line 190 of file mesh_output.h.

◆ _png_output

bool libMesh::GnuPlotIO::_png_output
private

Definition at line 122 of file gnuplot_io.h.

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

◆ _serial_only_needed_on_proc_0

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 181 of file mesh_output.h.

◆ _title

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

Definition at line 119 of file gnuplot_io.h.

Referenced by set_title(), and write_solution().

◆ axes_limits

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:
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::MeshOutput< MeshBase >::_serial_only_needed_on_proc_0
const bool _serial_only_needed_on_proc_0
Flag specifying whether this format can be written by only serializing the mesh to processor zero.
Definition: mesh_output.h:181
libMesh::GnuPlotIO::write_solution
void write_solution(const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: gnuplot_io.C:59
n_vars
unsigned int n_vars
Definition: adaptivity_ex3.C:116
libMesh::GnuPlotIO::PNG_OUTPUT
Definition: gnuplot_io.h:51
libMesh::GnuPlotIO::_title
std::string _title
Definition: gnuplot_io.h:119
libMesh::GnuPlotIO::axes_limits
std::string axes_limits
GNUplot automatically adjusts the x and y-axes of 2D plots to "zoom in" on the data.
Definition: gnuplot_io.h:107
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::NumericVector::localize
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.
libMesh::MeshOutput< MeshBase >::_is_parallel_format
const bool _is_parallel_format
Flag specifying whether this format is parallel-capable.
Definition: mesh_output.h:172
libMesh::GnuPlotIO::_grid
bool _grid
Definition: gnuplot_io.h:121
libMesh::MeshOutput< MeshBase >::write_nodal_data
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: mesh_output.h:105
libMesh::MeshOutput::mesh
const MT & mesh() const
Definition: mesh_output.h:247
libMesh::MeshOutput< MeshBase >::write_nodal_data_discontinuous
virtual void write_nodal_data_discontinuous(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with discontinuous data to a specified file where the nodal dat...
Definition: mesh_output.h:114
data
IterBase * data
Ideally this private member data should have protected access.
Definition: variant_filter_iterator.h:337
libMesh::MeshOutput< MeshBase >::_obj
const MeshBase *const _obj
A pointer to a constant object.
Definition: mesh_output.h:190
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::out
OStreamProxy out
libMesh::GnuPlotIO::_png_output
bool _png_output
Definition: gnuplot_io.h:122
libMesh::MeshOutput< MeshBase >::_ascii_precision
unsigned int _ascii_precision
Precision to use when writing ASCII files.
Definition: mesh_output.h:195
libMesh::GnuPlotIO::GRID_ON
Definition: gnuplot_io.h:50