libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | 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...
 
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
 

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 ()
 

Private Attributes

MeshBase_obj
 A pointer to a non-const object object. More...
 
const bool _is_parallel_format
 Flag specifying whether this format is parallel-capable. More...
 
unsigned int _ascii_precision
 Precision to use when writing ASCII files. More...
 

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 52 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 62 of file ucd_io.h.

62  :
63  MeshInput<MeshBase> (mesh),
64  MeshOutput<MeshBase>(mesh)
65  {}

◆ 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 72 of file ucd_io.h.

72  :
73  MeshOutput<MeshBase> (mesh)
74  {}

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 }

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

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 }

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

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

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 }

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

◆ mesh() [1/2]

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

Definition at line 169 of file mesh_input.h.

170 {
171  if (_obj == nullptr)
172  libmesh_error_msg("ERROR: _obj should not be nullptr!");
173  return *_obj;
174 }

◆ mesh() [2/2]

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 }

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

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 }

References read_implementation().

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

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

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  Elem * elem = Elem::build(et).release();
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 (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  if (mesh.mesh_dimension() > LIBMESH_DIM)
240  libmesh_error_msg("Cannot open dimension " \
241  << mesh.mesh_dimension() \
242  << " mesh file when configured without " \
243  << mesh.mesh_dimension() \
244  << "D support.");
245 #endif
246  }
247 }

References _reading_element_map, libMesh::MeshBase::add_elem(), libMesh::MeshBase::add_point(), libMesh::Elem::build(), libMesh::Elem::dim(), 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::Elem::node_index_range(), libMesh::MeshBase::node_ptr(), libMesh::DofObject::set_id(), libMesh::MeshBase::set_mesh_dimension(), libMesh::Elem::set_node(), libMesh::MeshInput< MeshBase >::skip_comment_lines(), and libMesh::Elem::subdomain_id().

Referenced by read().

◆ 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 91 of file mesh_input.h.

91 { this->mesh().set_n_partitions() = n_parts; }

◆ 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 179 of file mesh_input.h.

181 {
182  char c, line[256];
183 
184  while (in.get(c), c==comment_start)
185  in.getline (line, 255);
186 
187  // put back first character of
188  // first non-comment line
189  in.putback (c);
190 }

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

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 }

References write_implementation().

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

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

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 }

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

Referenced by write_implementation(), and write_nodal_data().

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

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  if (mesh.mesh_dimension() != 3)
259  libmesh_error_msg("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 }

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().

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

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 }

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

Referenced by write_implementation(), and write_nodal_data().

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

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 }

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

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

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

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 }

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

Referenced by write_implementation(), and write_nodal_data().

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

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 }

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().

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.

◆ _is_parallel_format [1/2]

const bool libMesh::MeshInput< MeshBase >::_is_parallel_format
privateinherited

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 121 of file mesh_input.h.

◆ _is_parallel_format [2/2]

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

MeshBase * libMesh::MeshInput< MeshBase >::_obj
privateinherited

A pointer to a non-const object object.

This allows us to read the object from file.

Definition at line 114 of file mesh_input.h.

◆ _reading_element_map

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

Definition at line 153 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 181 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 149 of file ucd_io.h.

Referenced by write_interior_elems().

◆ elems_of_dimension

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

A vector of bools describing what dimension elements have been encountered when reading a mesh.

Definition at line 97 of file mesh_input.h.


The documentation for this class was generated from the following files:
libMesh::UCDIO::_writing_element_map
static std::map< ElemType, std::string > _writing_element_map
Definition: ucd_io.h:149
libMesh::UCDIO::write_implementation
void write_implementation(std::ostream &out_stream)
The actual implementation of the write function.
Definition: ucd_io.C:251
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::PRISM6
Definition: enum_elem_type.h:50
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::MeshBase::set_n_partitions
unsigned int & set_n_partitions()
Definition: mesh_base.h:1667
libMesh::MeshTools::n_elem
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:705
n_vars
unsigned int n_vars
Definition: adaptivity_ex3.C:116
libMesh::HEX8
Definition: enum_elem_type.h:47
libMesh::MeshInput< MeshBase >::skip_comment_lines
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:179
libMesh::MeshBase::n_elem
virtual dof_id_type n_elem() const =0
libMesh::UCDIO::read_implementation
void read_implementation(std::istream &in_stream)
The actual implementation of the read function.
Definition: ucd_io.C:126
libMesh::MeshBase::node_ptr
virtual const Node * node_ptr(const dof_id_type i) const =0
libMesh::MeshBase::mesh_dimension
unsigned int mesh_dimension() const
Definition: mesh_base.C:135
libMesh::UCDIO::write_soln
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::MeshInput< MeshBase >::_obj
MeshBase * _obj
A pointer to a non-const object object.
Definition: mesh_input.h:114
libMesh::TET4
Definition: enum_elem_type.h:45
libMesh::MeshBase::element_ptr_range
virtual SimpleRange< element_iterator > element_ptr_range()=0
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::MeshBase::node_ptr_range
virtual SimpleRange< node_iterator > node_ptr_range()=0
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::ParallelObject::processor_id
processor_id_type processor_id() const
Definition: parallel_object.h:106
libMesh::QUAD4
Definition: enum_elem_type.h:41
libMesh::TRI3
Definition: enum_elem_type.h:39
libMesh::MeshTools::Generation::Private::idx
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.
Definition: mesh_generation.C:72
libMesh::UCDIO::_reading_element_map
static std::map< std::string, ElemType > _reading_element_map
Definition: ucd_io.h:153
libMesh::MeshBase::n_nodes
virtual dof_id_type n_nodes() const =0
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< MeshBase >::mesh
const MeshBase & mesh() const
Definition: mesh_output.h:247
libMesh::MeshBase::add_elem
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
libMesh::PYRAMID5
Definition: enum_elem_type.h:53
libMesh::UCDIO::write_header
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
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
libMesh::MeshInput< MeshBase >::mesh
MeshBase & mesh()
Definition: mesh_input.h:169
libMesh::MeshOutput< MeshBase >::_obj
const MeshBase *const _obj
A pointer to a constant object.
Definition: mesh_output.h:190
libMesh::MeshBase::add_point
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.
libMesh::UCDIO::write_interior_elems
void write_interior_elems(std::ostream &out, const MeshBase &mesh)
Write element information.
Definition: ucd_io.C:311
libMesh::MeshBase::set_mesh_dimension
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:218
libMesh::Elem::build
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:246
libMesh::out
OStreamProxy out
libMesh::UCD
Definition: enum_io_package.h:45
libMesh::MeshOutput< MeshBase >::_ascii_precision
unsigned int _ascii_precision
Precision to use when writing ASCII files.
Definition: mesh_output.h:195
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::UCDIO::write_nodes
void write_nodes(std::ostream &out, const MeshBase &mesh)
Write node information.
Definition: ucd_io.C:293
libMesh::EDGE2
Definition: enum_elem_type.h:35
libMesh::MeshInput< MeshBase >::elems_of_dimension
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:97
libMesh::ElemType
ElemType
Defines an enum for geometric element types.
Definition: enum_elem_type.h:33