libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
libMesh::UCDIO Class Reference

This class implements reading & writing meshes in the AVS's UCD format. More...

#include <ucd_io.h>

Inheritance diagram for libMesh::UCDIO:
[legend]

Public Member Functions

 UCDIO (MeshBase &mesh)
 Constructor. More...
 
 UCDIO (const MeshBase &mesh)
 Constructor. More...
 
virtual void read (const std::string &) override
 This method implements reading a mesh from a specified file in UCD format. More...
 
virtual void write (const std::string &) override
 This method implements writing a mesh to a specified file in UCD format. More...
 
virtual void write_nodal_data (const std::string &fname, const std::vector< Number > &soln, const std::vector< std::string > &names) override
 This method implements writing a mesh and solution to a specified file in UCD format. More...
 
bool is_parallel_format () const
 Returns true iff this mesh file format and input class are parallelized, so that all processors can read their share of the data at once. 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...
 

Protected Member Functions

MeshBasemesh ()
 
void set_n_partitions (unsigned int n_parts)
 Sets the number of partitions in the mesh. More...
 
void skip_comment_lines (std::istream &in, const char comment_start)
 Reads input from in, skipping all the lines that start with the character comment_start. More...
 
const MeshBasemesh () const
 
virtual bool get_add_sides ()
 

Protected Attributes

std::vector< bool > elems_of_dimension
 A vector of bools describing what dimension elements have been encountered when reading a mesh. More...
 
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 read_implementation (std::istream &in_stream)
 The actual implementation of the read function. More...
 
void write_implementation (std::ostream &out_stream)
 The actual implementation of the write function. More...
 
void write_header (std::ostream &out, const MeshBase &mesh, dof_id_type n_elems, unsigned int n_vars)
 Write UCD format header. More...
 
void write_nodes (std::ostream &out, const MeshBase &mesh)
 Write node information. More...
 
void write_interior_elems (std::ostream &out, const MeshBase &mesh)
 Write element information. More...
 
void write_soln (std::ostream &out, const MeshBase &mesh, const std::vector< std::string > &names, const std::vector< Number > &soln)
 Writes all nodal solution variables. More...
 

Static Private Member Functions

static std::map< ElemType, std::string > build_writing_element_map ()
 
static std::map< std::string, ElemTypebuild_reading_element_map ()
 

Static Private Attributes

static std::map< ElemType, std::string > _writing_element_map = UCDIO::build_writing_element_map()
 
static std::map< std::string, ElemType_reading_element_map = UCDIO::build_reading_element_map()
 

Detailed Description

This class implements reading & writing meshes in the AVS's UCD format.

Author
Benjamin S. Kirk
Date
2004

Definition at line 44 of file ucd_io.h.

Constructor & Destructor Documentation

◆ UCDIO() [1/2]

libMesh::UCDIO::UCDIO ( MeshBase mesh)
inlineexplicit

Constructor.

Takes a writable reference to a mesh object. This is the constructor required to read a mesh.

Definition at line 54 of file ucd_io.h.

54  :
55  MeshInput<MeshBase> (mesh),
57  {}
template class LIBMESH_EXPORT MeshOutput< MeshBase >
Definition: mesh_output.C:180

◆ UCDIO() [2/2]

libMesh::UCDIO::UCDIO ( const MeshBase mesh)
inlineexplicit

Constructor.

Takes a reference to a constant mesh object. This constructor will only allow us to write the mesh.

Definition at line 64 of file ucd_io.h.

64  :
66  {}
template class LIBMESH_EXPORT MeshOutput< MeshBase >
Definition: mesh_output.C:180

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

Referenced by libMesh::UNVIO::nodes_out(), libMesh::FroIO::write(), libMesh::MEDITIO::write_ascii(), libMesh::TecplotIO::write_ascii(), libMesh::GMVIO::write_ascii_new_impl(), and libMesh::GMVIO::write_ascii_old_impl().

270 {
271  return _ascii_precision;
272 }
unsigned int _ascii_precision
Precision to use when writing ASCII files.
Definition: mesh_output.h:207

◆ build_reading_element_map()

std::map< std::string, ElemType > libMesh::UCDIO::build_reading_element_map ( )
staticprivate

Definition at line 70 of file ucd_io.C.

References libMesh::EDGE2, libMesh::HEX8, libMesh::PRISM6, libMesh::PYRAMID5, libMesh::QUAD4, libMesh::TET4, and libMesh::TRI3.

71 {
72  std::map<std::string, ElemType> ret;
73  ret["edge"] = EDGE2;
74  ret["tri"] = TRI3;
75  ret["quad"] = QUAD4;
76  ret["tet"] = TET4;
77  ret["hex"] = HEX8;
78  ret["prism"] = PRISM6;
79  ret["pyramid"] = PYRAMID5;
80  return ret;
81 }

◆ build_writing_element_map()

std::map< ElemType, std::string > libMesh::UCDIO::build_writing_element_map ( )
staticprivate

Definition at line 54 of file ucd_io.C.

References libMesh::EDGE2, libMesh::HEX8, libMesh::PRISM6, libMesh::PYRAMID5, libMesh::QUAD4, libMesh::TET4, and libMesh::TRI3.

55 {
56  std::map<ElemType, std::string> ret;
57  ret[EDGE2] = "edge";
58  ret[TRI3] = "tri";
59  ret[QUAD4] = "quad";
60  ret[TET4] = "tet";
61  ret[HEX8] = "hex";
62  ret[PRISM6] = "prism";
63  ret[PYRAMID5] = "pyramid";
64  return ret;
65 }

◆ get_add_sides()

virtual bool libMesh::MeshOutput< MeshBase >::get_add_sides ( )
inlineprotectedvirtualinherited
Returns
Whether or not added sides are expected to be output, to plot SIDE_DISCONTINUOUS data. Subclasses should override this if they are capable of plotting such data.

Reimplemented in libMesh::ExodusII_IO.

Definition at line 176 of file mesh_output.h.

176 { return false; }

◆ is_parallel_format()

bool libMesh::MeshInput< MeshBase >::is_parallel_format ( ) const
inlineinherited

Returns true iff this mesh file format and input class are parallelized, so that all processors can read their share of the data at once.

Definition at line 87 of file mesh_input.h.

References libMesh::MeshInput< MT >::_is_parallel_format.

87 { return this->_is_parallel_format; }
const bool _is_parallel_format
Flag specifying whether this format is parallel-capable.
Definition: mesh_input.h:130

◆ mesh() [1/2]

MeshBase & libMesh::MeshInput< MeshBase >::mesh ( )
inlineprotectedinherited
Returns
The object as a writable reference.

Definition at line 178 of file mesh_input.h.

Referenced by libMesh::GMVIO::_read_one_cell(), libMesh::VTKIO::cells_to_vtk(), libMesh::ExodusII_IO::copy_elemental_solution(), libMesh::Nemesis_IO::copy_elemental_solution(), libMesh::ExodusII_IO::copy_nodal_solution(), libMesh::TetGenIO::element_in(), libMesh::UNVIO::elements_in(), libMesh::UNVIO::elements_out(), libMesh::VTKIO::get_local_node_values(), libMesh::ExodusII_IO::get_sideset_data_indices(), libMesh::UNVIO::groups_in(), libMesh::TetGenIO::node_in(), libMesh::UNVIO::nodes_in(), libMesh::UNVIO::nodes_out(), libMesh::VTKIO::nodes_to_vtk(), libMesh::Nemesis_IO::prepare_to_write_nodal_data(), libMesh::GMVIO::read(), libMesh::Nemesis_IO::read(), libMesh::ExodusII_IO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read(), libMesh::VTKIO::read(), libMesh::CheckpointIO::read_bcs(), libMesh::CheckpointIO::read_connectivity(), libMesh::ExodusII_IO::read_header(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), read_implementation(), libMesh::UNVIO::read_implementation(), libMesh::GmshIO::read_mesh(), libMesh::DynaIO::read_mesh(), libMesh::CheckpointIO::read_nodes(), libMesh::CheckpointIO::read_nodesets(), libMesh::CheckpointIO::read_remote_elem(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::ExodusII_IO::read_sideset_data(), libMesh::OFFIO::read_stream(), libMesh::MatlabIO::read_stream(), libMesh::CheckpointIO::read_subdomain_names(), libMesh::TetGenIO::write(), libMesh::Nemesis_IO::write(), libMesh::XdrIO::write(), libMesh::CheckpointIO::write(), libMesh::ExodusII_IO::write(), libMesh::GMVIO::write_ascii_new_impl(), libMesh::GMVIO::write_ascii_old_impl(), libMesh::GMVIO::write_binary(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::Nemesis_IO::write_element_data(), libMesh::ExodusII_IO::write_element_data(), libMesh::ExodusII_IO::write_elemsets(), write_header(), write_implementation(), write_interior_elems(), libMesh::GmshIO::write_mesh(), write_nodal_data(), libMesh::VTKIO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data_common(), libMesh::ExodusII_IO::write_nodal_data_discontinuous(), write_nodes(), libMesh::CheckpointIO::write_nodesets(), libMesh::XdrIO::write_parallel(), libMesh::GmshIO::write_post(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), libMesh::XdrIO::write_serialized_nodesets(), libMesh::XdrIO::write_serialized_subdomain_names(), libMesh::ExodusII_IO::write_sideset_data(), write_soln(), and libMesh::CheckpointIO::write_subdomain_names().

179 {
180  libmesh_error_msg_if(_obj == nullptr, "ERROR: _obj should not be nullptr!");
181  return *_obj;
182 }
MeshBase * _obj
A pointer to a non-const object object.
Definition: mesh_input.h:123

◆ mesh() [2/2]

const MeshBase & libMesh::MeshOutput< MeshBase >::mesh ( ) const
inlineprotectedinherited

◆ read()

void libMesh::UCDIO::read ( const std::string &  file_name)
overridevirtual

This method implements reading a mesh from a specified file in UCD format.

Implements libMesh::MeshInput< MeshBase >.

Definition at line 84 of file ucd_io.C.

References read_implementation().

Referenced by libMesh::NameBasedIO::read().

85 {
86  if (file_name.rfind(".gz") < file_name.size())
87  {
88 #ifdef LIBMESH_HAVE_GZSTREAM
89  igzstream in_stream (file_name.c_str());
90  this->read_implementation (in_stream);
91 #else
92  libmesh_error_msg("ERROR: You must have the zlib.h header files and libraries to read and write compressed streams.");
93 #endif
94  }
95 
96  else
97  {
98  std::ifstream in_stream (file_name.c_str());
99  this->read_implementation (in_stream);
100  }
101 }
void read_implementation(std::istream &in_stream)
The actual implementation of the read function.
Definition: ucd_io.C:126

◆ read_implementation()

void libMesh::UCDIO::read_implementation ( std::istream &  in_stream)
private

The actual implementation of the read function.

The public read interface simply decides which type of stream to pass the implementation.

Definition at line 126 of file ucd_io.C.

References _reading_element_map, libMesh::MeshBase::add_elem(), libMesh::MeshBase::add_point(), libMesh::Elem::build(), libMesh::MeshInput< MeshBase >::elems_of_dimension, libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshInput< MT >::mesh(), libMesh::MeshBase::mesh_dimension(), libMesh::MeshBase::n_nodes(), libMesh::MeshBase::node_ptr(), libMesh::MeshBase::set_mesh_dimension(), and libMesh::MeshInput< MeshBase >::skip_comment_lines().

Referenced by read().

127 {
128  // This is a serial-only process for now;
129  // the Mesh should be read on processor 0 and
130  // broadcast later
131  libmesh_assert_equal_to (MeshOutput<MeshBase>::mesh().processor_id(), 0);
132 
133  // Check input buffer
134  libmesh_assert (in.good());
135 
136  MeshBase & mesh = MeshInput<MeshBase>::mesh();
137 
138  // Keep track of what kinds of elements this file contains
139  elems_of_dimension.clear();
140  elems_of_dimension.resize(4, false);
141 
142  this->skip_comment_lines (in, '#');
143 
144  unsigned int nNodes=0, nElem=0, dummy=0;
145 
146  in >> nNodes // Read the number of nodes from the stream
147  >> nElem // Read the number of elements from the stream
148  >> dummy
149  >> dummy
150  >> dummy;
151 
152 
153  // Read the nodal coordinates. Note that UCD format always
154  // stores (x,y,z), and in 2D z=0. We don't need to store this,
155  // however. So, we read in x,y,z for each node and make a point
156  // in the proper way based on what dimension we're in
157  {
158  for (unsigned int i=0; i<nNodes; i++)
159  {
160  libmesh_assert (in.good());
161 
162  std::array<Real, 3> xyz;
163 
164  in >> dummy // Point number
165  >> xyz[0] // x-coordinate value
166  >> xyz[1] // y-coordinate value
167  >> xyz[2]; // z-coordinate value
168 
169  Point p(xyz[0]);
170 #if LIBMESH_DIM > 1
171  p(1) = xyz[1];
172 #else
173  libmesh_assert_equal_to(xyz[1], 0);
174 #endif
175 #if LIBMESH_DIM > 2
176  p(2) = xyz[2];
177 #else
178  libmesh_assert_equal_to(xyz[2], 0);
179 #endif
180 
181  // Build the node
182  mesh.add_point (p, i);
183  }
184  }
185 
186  // Read the elements from the stream. Notice that the UCD node-numbering
187  // scheme is 1-based, and we just created a 0-based scheme above
188  // (which is of course what we want). So, when we read in the nodal
189  // connectivity for each element we need to take 1 off the value of
190  // each node so that we get the right thing.
191  {
192  unsigned int material_id=0, node=0;
193  std::string type;
194 
195  for (unsigned int i=0; i<nElem; i++)
196  {
197  libmesh_assert (in.good());
198 
199  // The cell type can be either tri, quad, tet, hex, or prism.
200  in >> dummy // Cell number, means nothing to us
201  >> material_id // We'll use this for the element subdomain id.
202  >> type; // string describing cell type
203 
204  // Convert the UCD type string to a libmesh ElemType
205  ElemType et = libmesh_map_find(_reading_element_map, type);
206 
207  // Build the required type and release it into our custody.
208  auto elem = Elem::build(et);
209 
210  for (auto n : elem->node_index_range())
211  {
212  libmesh_assert (in.good());
213 
214  in >> node; // read the current node
215  node -= 1; // UCD is 1-based, so subtract
216 
217  libmesh_assert_less (node, mesh.n_nodes());
218 
219  // assign the node
220  elem->set_node(n) = mesh.node_ptr(node);
221  }
222 
223  elems_of_dimension[elem->dim()] = true;
224 
225  // Set the element's subdomain ID based on the material_id.
226  elem->subdomain_id() = cast_int<subdomain_id_type>(material_id);
227 
228  // Add the element to the mesh
229  elem->set_id(i);
230  mesh.add_elem (std::move(elem));
231  }
232 
233  // Set the mesh dimension to the largest encountered for an element
234  for (unsigned char i=0; i!=4; ++i)
235  if (elems_of_dimension[i])
237 
238 #if LIBMESH_DIM < 3
239  libmesh_error_msg_if(mesh.mesh_dimension() > LIBMESH_DIM,
240  "Cannot open dimension "
241  << mesh.mesh_dimension()
242  << " mesh file when configured without "
243  << mesh.mesh_dimension()
244  << "D support.");
245 #endif
246  }
247 }
const MeshBase & mesh() const
Definition: mesh_output.h:259
ElemType
Defines an enum for geometric element types.
std::vector< bool > elems_of_dimension
A vector of bools describing what dimension elements have been encountered when reading a mesh...
Definition: mesh_input.h:107
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
void skip_comment_lines(std::istream &in, const char comment_start)
Reads input from in, skipping all the lines that start with the character comment_start.
Definition: mesh_input.h:187
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:273
libmesh_assert(ctx)
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:269
unsigned int mesh_dimension() const
Definition: mesh_base.C:324
static std::map< std::string, ElemType > _reading_element_map
Definition: ucd_io.h:145
virtual const Node * node_ptr(const dof_id_type i) const =0
virtual dof_id_type n_nodes() const =0

◆ set_n_partitions()

void libMesh::MeshInput< MeshBase >::set_n_partitions ( unsigned int  n_parts)
inlineprotectedinherited

Sets the number of partitions in the mesh.

Typically this gets done by the partitioner, but some parallel file formats begin "pre-partitioned".

Definition at line 101 of file mesh_input.h.

References libMesh::MeshInput< MT >::mesh().

Referenced by libMesh::Nemesis_IO::read(), and libMesh::XdrIO::read_header().

101 { this->mesh().set_n_partitions() = n_parts; }
unsigned int & set_n_partitions()
Definition: mesh_base.h:1789

◆ skip_comment_lines()

void libMesh::MeshInput< MeshBase >::skip_comment_lines ( std::istream &  in,
const char  comment_start 
)
protectedinherited

Reads input from in, skipping all the lines that start with the character comment_start.

Definition at line 187 of file mesh_input.h.

Referenced by libMesh::TetGenIO::read(), and read_implementation().

189 {
190  char c, line[256];
191 
192  while (in.get(c), c==comment_start)
193  in.getline (line, 255);
194 
195  // put back first character of
196  // first non-comment line
197  in.putback (c);
198 }

◆ write()

void libMesh::UCDIO::write ( const std::string &  file_name)
overridevirtual

This method implements writing a mesh to a specified file in UCD format.

Implements libMesh::MeshOutput< MeshBase >.

Definition at line 105 of file ucd_io.C.

References write_implementation().

Referenced by libMesh::NameBasedIO::write().

106 {
107  if (file_name.rfind(".gz") < file_name.size())
108  {
109 #ifdef LIBMESH_HAVE_GZSTREAM
110  ogzstream out_stream (file_name.c_str());
111  this->write_implementation (out_stream);
112 #else
113  libmesh_error_msg("ERROR: You must have the zlib.h header files and libraries to read and write compressed streams.");
114 #endif
115  }
116 
117  else
118  {
119  std::ofstream out_stream (file_name.c_str());
120  this->write_implementation (out_stream);
121  }
122 }
void write_implementation(std::ostream &out_stream)
The actual implementation of the write function.
Definition: ucd_io.C:251

◆ 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 89 of file mesh_output.C.

References libMesh::EquationSystems::build_discontinuous_solution_vector(), libMesh::EquationSystems::build_variable_names(), libMesh::EquationSystems::get_mesh(), libMesh::libmesh_assert(), and libMesh::out.

Referenced by libMesh::ExodusII_IO::write_timestep_discontinuous().

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

◆ 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.

References libMesh::EquationSystems::build_solution_vector(), libMesh::EquationSystems::build_variable_names(), libMesh::EquationSystems::get_mesh(), libMesh::libmesh_assert(), and libMesh::out.

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

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-parallel format, non-renumbered mesh may not have a contiguous
46  // numbering, and that needs to be fixed before we can build a solution vector.
47  if (!_is_parallel_format &&
48  (my_mesh.max_elem_id() != my_mesh.n_elem() ||
49  my_mesh.max_node_id() != my_mesh.n_nodes()))
50  {
51  // If we were allowed to renumber then we should have already
52  // been properly renumbered...
53  libmesh_assert(!my_mesh.allow_renumbering());
54 
55  libmesh_do_once(libMesh::out <<
56  "Warning: This MeshOutput subclass only supports meshes which are contiguously renumbered!"
57  << std::endl;);
58 
59  my_mesh.allow_renumbering(true);
60 
61  my_mesh.renumber_nodes_and_elements();
62 
63  // Not sure what good going back to false will do here, the
64  // renumbering horses have already left the barn...
65  my_mesh.allow_renumbering(false);
66  }
67 
69  {
70  MeshSerializer serialize(const_cast<MT &>(*_obj), !_is_parallel_format, _serial_only_needed_on_proc_0);
71 
72  // Build the list of variable names that will be written.
73  std::vector<std::string> names;
74  es.build_variable_names (names, nullptr, system_names);
75 
76  // Build the nodal solution values & get the variable
77  // names from the EquationSystems object
78  std::vector<Number> soln;
79  es.build_solution_vector (soln, system_names,
80  this->get_add_sides());
81 
82  this->write_nodal_data (fname, soln, names);
83  }
84  else // _is_parallel_format
85  this->write_nodal_data (fname, es, system_names);
86 }
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:109
const MeshBase *const _obj
A pointer to a constant object.
Definition: mesh_output.h:202
const bool _is_parallel_format
Flag specifying whether this format is parallel-capable.
Definition: mesh_output.h:184
libmesh_assert(ctx)
OStreamProxy out
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:193

◆ write_header()

void libMesh::UCDIO::write_header ( std::ostream &  out,
const MeshBase mesh,
dof_id_type  n_elems,
unsigned int  n_vars 
)
private

Write UCD format header.

Definition at line 274 of file ucd_io.C.

References libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshBase::n_nodes(), and n_vars.

Referenced by write_implementation(), and write_nodal_data().

278 {
279  libmesh_assert (out_stream.good());
280  // TODO: We could print out the libmesh revision used to write this file here.
281  out_stream << "# For a description of the UCD format see the AVS Developer's guide.\n"
282  << "#\n";
283 
284  // Write the mesh info
285  out_stream << mesh.n_nodes() << " "
286  << n_elems << " "
287  << n_vars << " "
288  << " 0 0\n";
289 }
unsigned int n_vars
libmesh_assert(ctx)
virtual dof_id_type n_nodes() const =0

◆ write_implementation()

void libMesh::UCDIO::write_implementation ( std::ostream &  out_stream)
private

The actual implementation of the write function.

The public write interface simply decides which type of stream to pass the implementation.

Definition at line 251 of file ucd_io.C.

References libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::mesh_dimension(), libMesh::MeshBase::n_elem(), write_header(), write_interior_elems(), and write_nodes().

Referenced by write().

252 {
253  libmesh_assert (out_stream.good());
254 
255  const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
256 
257  // UCD doesn't work any dimension except 3?
258  libmesh_error_msg_if(mesh.mesh_dimension() != 3,
259  "Error: Can't write boundary elements for meshes of dimension less than 3. "
260  "Mesh dimension = " << mesh.mesh_dimension());
261 
262  // Write header
263  this->write_header(out_stream, mesh, mesh.n_elem(), 0);
264 
265  // Write the node coordinates
266  this->write_nodes(out_stream, mesh);
267 
268  // Write the elements
269  this->write_interior_elems(out_stream, mesh);
270 }
const MT & mesh() const
Definition: mesh_output.h:259
libmesh_assert(ctx)
void write_nodes(std::ostream &out, const MeshBase &mesh)
Write node information.
Definition: ucd_io.C:293
unsigned int mesh_dimension() const
Definition: mesh_base.C:324
void write_interior_elems(std::ostream &out, const MeshBase &mesh)
Write element information.
Definition: ucd_io.C:311
virtual dof_id_type n_elem() const =0
void write_header(std::ostream &out, const MeshBase &mesh, dof_id_type n_elems, unsigned int n_vars)
Write UCD format header.
Definition: ucd_io.C:274

◆ write_interior_elems()

void libMesh::UCDIO::write_interior_elems ( std::ostream &  out,
const MeshBase mesh 
)
private

Write element information.

Definition at line 311 of file ucd_io.C.

References _writing_element_map, libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), and libMesh::UCD.

Referenced by write_implementation(), and write_nodal_data().

313 {
314  // 1-based element number for UCD
315  unsigned int e=1;
316 
317  // Write element information
318  for (const auto & elem : mesh.element_ptr_range())
319  {
320  libmesh_assert (out_stream.good());
321 
322  // Look up the corresponding UCD element type in the static map.
323  std::string elem_string = libmesh_map_find(_writing_element_map, elem->type());
324 
325  // Write the element's subdomain ID as the UCD "material_id".
326  out_stream << e++ << " " << elem->subdomain_id() << " " << elem_string << "\t";
327  elem->write_connectivity(out_stream, UCD);
328  }
329 }
static std::map< ElemType, std::string > _writing_element_map
Definition: ucd_io.h:141
libmesh_assert(ctx)

◆ write_nodal_data() [1/3]

void libMesh::UCDIO::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 and solution to a specified file in UCD format.

This is internally called by MeshOutput::write_equation_systems

Reimplemented from libMesh::MeshOutput< MeshBase >.

Definition at line 333 of file ucd_io.C.

References libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::mesh_dimension(), libMesh::MeshTools::n_elem(), libMesh::MeshBase::n_elem(), libMesh::ParallelObject::processor_id(), write_header(), write_interior_elems(), write_nodes(), and write_soln().

Referenced by libMesh::NameBasedIO::write_nodal_data().

336 {
337  const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
338 
339  const dof_id_type n_elem = mesh.n_elem();
340 
341  // Only processor 0 does the writing
342  if (mesh.processor_id())
343  return;
344 
345  std::ofstream out_stream(fname.c_str());
346 
347  // UCD doesn't work in 1D
349 
350  // Write header
351  this->write_header(out_stream,mesh,n_elem,
352  cast_int<unsigned int>(names.size()));
353 
354  // Write the node coordinates
355  this->write_nodes(out_stream, mesh);
356 
357  // Write the elements
358  this->write_interior_elems(out_stream, mesh);
359 
360  // Write the solution
361  this->write_soln(out_stream, mesh, names, soln);
362 }
const MT & mesh() const
Definition: mesh_output.h:259
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Count up the number of elements of a specific type (as defined by an iterator range).
Definition: mesh_tools.C:850
void write_soln(std::ostream &out, const MeshBase &mesh, const std::vector< std::string > &names, const std::vector< Number > &soln)
Writes all nodal solution variables.
Definition: ucd_io.C:366
libmesh_assert(ctx)
void write_nodes(std::ostream &out, const MeshBase &mesh)
Write node information.
Definition: ucd_io.C:293
unsigned int mesh_dimension() const
Definition: mesh_base.C:324
void write_interior_elems(std::ostream &out, const MeshBase &mesh)
Write element information.
Definition: ucd_io.C:311
virtual dof_id_type n_elem() const =0
processor_id_type processor_id() const
uint8_t dof_id_type
Definition: id_types.h:67
void write_header(std::ostream &out, const MeshBase &mesh, dof_id_type n_elems, unsigned int n_vars)
Write UCD format header.
Definition: ucd_io.C:274

◆ 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 149 of file mesh_output.C.

References libMesh::NumericVector< T >::localize().

152 {
153  // This is the fallback implementation for parallel I/O formats that
154  // do not yet implement proper writing in parallel, and instead rely
155  // on the full solution vector being available on all processors.
156  std::vector<Number> soln;
157  parallel_soln.localize(soln);
158  this->write_nodal_data(fname, soln, names);
159 }
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:109
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.

◆ write_nodal_data() [3/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 162 of file mesh_output.C.

References libMesh::EquationSystems::build_parallel_solution_vector(), and libMesh::EquationSystems::build_variable_names().

165 {
166  std::vector<std::string> names;
167  es.build_variable_names (names, nullptr, system_names);
168 
169  std::unique_ptr<NumericVector<Number>> parallel_soln =
170  es.build_parallel_solution_vector(system_names);
171 
172  this->write_nodal_data (fname, *parallel_soln, names);
173 }
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:109

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

121  { libmesh_not_implemented(); }

◆ write_nodes()

void libMesh::UCDIO::write_nodes ( std::ostream &  out,
const MeshBase mesh 
)
private

Write node information.

Definition at line 293 of file ucd_io.C.

References libMesh::libmesh_assert(), and libMesh::MeshInput< MeshBase >::mesh().

Referenced by write_implementation(), and write_nodal_data().

295 {
296  // 1-based node number for UCD
297  unsigned int n=1;
298 
299  // Write the node coordinates
300  for (auto & node : mesh.node_ptr_range())
301  {
302  libmesh_assert (out_stream.good());
303 
304  out_stream << n++ << "\t";
305  node->write_unformatted(out_stream);
306  }
307 }
libmesh_assert(ctx)

◆ write_soln()

void libMesh::UCDIO::write_soln ( std::ostream &  out,
const MeshBase mesh,
const std::vector< std::string > &  names,
const std::vector< Number > &  soln 
)
private

Writes all nodal solution variables.

Definition at line 366 of file ucd_io.C.

References libMesh::MeshTools::Generation::Private::idx(), libMesh::libmesh_assert(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshBase::n_nodes(), and libMesh::Quality::name().

Referenced by write_nodal_data().

370 {
371  libmesh_assert (out_stream.good());
372 
373  // First write out how many variables and how many components per variable
374  out_stream << names.size();
375  for (std::size_t i = 0, ns = names.size(); i < ns; i++)
376  {
377  libmesh_assert (out_stream.good());
378  // Each named variable has only 1 component
379  out_stream << " 1";
380  }
381  out_stream << std::endl;
382 
383  // Now write out variable names and units. Since we don't store units
384  // We just write out dummy.
385  for (const auto & name : names)
386  {
387  libmesh_assert (out_stream.good());
388  out_stream << name << ", dummy" << std::endl;
389  }
390 
391  // Now, for each node, write out the solution variables.
392  // We use a 1-based node numbering for UCD.
393  std::size_t nv = names.size();
394  for (auto n : IntRange<std::size_t>(1, mesh.n_nodes()))
395  {
396  libmesh_assert (out_stream.good());
397  out_stream << n;
398 
399  for (std::size_t var = 0; var != nv; var++)
400  {
401  std::size_t idx = nv*(n-1) + var;
402 
403  out_stream << " " << soln[idx];
404  }
405  out_stream << std::endl;
406  }
407 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libmesh_assert(ctx)
virtual dof_id_type n_nodes() const =0
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
A useful inline function which replaces the macros used previously.

Member Data Documentation

◆ _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 184 of file mesh_output.h.

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

◆ _reading_element_map

std::map< std::string, ElemType > libMesh::UCDIO::_reading_element_map = UCDIO::build_reading_element_map()
staticprivate

Definition at line 145 of file ucd_io.h.

Referenced by read_implementation().

◆ _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 193 of file mesh_output.h.

◆ _writing_element_map

std::map< ElemType, std::string > libMesh::UCDIO::_writing_element_map = UCDIO::build_writing_element_map()
staticprivate

Definition at line 141 of file ucd_io.h.

Referenced by write_interior_elems().

◆ elems_of_dimension

std::vector<bool> libMesh::MeshInput< MeshBase >::elems_of_dimension
protectedinherited

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