libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
libMesh::NameBasedIO Class Reference

This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of the other I/O classes based on filename. More...

#include <namebased_io.h>

Inheritance diagram for libMesh::NameBasedIO:
[legend]

Public Member Functions

 NameBasedIO (const MeshBase &)
 Constructor. More...
 
 NameBasedIO (MeshBase &)
 Constructor. More...
 
virtual void read (const std::string &mesh_file) override
 This method implements reading a mesh from a specified file. More...
 
virtual void write (const std::string &mesh_file) override
 This method implements writing a mesh to a specified file. More...
 
virtual void write_equation_systems (const std::string &filename, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
 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_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...
 
bool is_parallel_file_format (std::string_view filename)
 
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_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...
 

Detailed Description

This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of the other I/O classes based on filename.

Other I/O classes may have more advanced features that are not accessible via this interface.

Author
Roy H. Stogner
Date
2015

Definition at line 44 of file namebased_io.h.

Constructor & Destructor Documentation

◆ NameBasedIO() [1/2]

libMesh::NameBasedIO::NameBasedIO ( 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 110 of file namebased_io.h.

110  :
112 {
113 }
template class LIBMESH_EXPORT MeshOutput< MeshBase >
Definition: mesh_output.C:180

◆ NameBasedIO() [2/2]

libMesh::NameBasedIO::NameBasedIO ( MeshBase mesh)
inlineexplicit

Constructor.

Takes a writable reference to a mesh object. This constructor is required to let us read in a mesh.

Definition at line 116 of file namebased_io.h.

116  :
117  MeshInput<MeshBase> (mesh),
119 {
120 }
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::STLIO::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

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

bool libMesh::NameBasedIO::is_parallel_file_format ( std::string_view  filename)

Definition at line 571 of file namebased_io.C.

References libMesh::Utility::contains(), libMesh::Utility::ends_with(), and libMesh::Quality::name().

Referenced by read(), and write().

572 {
573  using Utility::contains;
574  using Utility::ends_with;
575  return (contains(name, ".xda") || contains(name, ".xdr") ||
576  ends_with(name, ".nem") || ends_with(name, ".n") ||
577  contains(name, ".cp"));
578 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition: utility.C:213
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205

◆ 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::STLIO::read(), libMesh::Nemesis_IO::read(), libMesh::XdrIO::read(), libMesh::ExodusII_IO::read(), libMesh::CheckpointIO::read(), libMesh::VTKIO::read(), libMesh::STLIO::read_ascii(), libMesh::CheckpointIO::read_bcs(), libMesh::STLIO::read_binary(), libMesh::CheckpointIO::read_connectivity(), libMesh::ExodusII_IO::read_header(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), libMesh::UCDIO::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::STLIO::write(), 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(), libMesh::UCDIO::write_header(), libMesh::UCDIO::write_implementation(), libMesh::UCDIO::write_interior_elems(), libMesh::GmshIO::write_mesh(), libMesh::UCDIO::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(), libMesh::UCDIO::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(), libMesh::UCDIO::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::NameBasedIO::read ( const std::string &  mesh_file)
overridevirtual

This method implements reading a mesh from a specified file.

Implements libMesh::MeshInput< MeshBase >.

Definition at line 69 of file namebased_io.C.

References libMesh::MeshBase::allow_renumbering(), libMesh::Utility::basename_of(), libMesh::XdrIO::binary(), libMesh::MeshCommunication::broadcast(), libMesh::Utility::contains(), libMesh::Utility::ends_with(), is_parallel_file_format(), libMesh::XdrIO::legacy(), libMesh::MeshInput< MT >::mesh(), libMesh::ParallelObject::n_processors(), libMesh::Quality::name(), libMesh::ParallelObject::processor_id(), libMesh::OFFIO::read(), libMesh::AbaqusIO::read(), libMesh::TetGenIO::read(), libMesh::UCDIO::read(), libMesh::GMVIO::read(), libMesh::STLIO::read(), libMesh::UNVIO::read(), libMesh::Nemesis_IO::read(), libMesh::GmshIO::read(), libMesh::DynaIO::read(), libMesh::XdrIO::read(), libMesh::MatlabIO::read(), libMesh::ExodusII_IO::read(), libMesh::CheckpointIO::read(), and libMesh::VTKIO::read().

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

70 {
71  MeshBase & mymesh = MeshInput<MeshBase>::mesh();
72 
73  const std::string_view basename = Utility::basename_of(name);
74 
75  using Utility::ends_with;
76  using Utility::contains;
77 
78  // See if the file exists. Perform this check on all processors
79  // so that the code is terminated properly in the case that the
80  // file does not exist.
81 
82  // For Nemesis files, the name we try to read will have suffixes
83  // identifying processor rank
84  if (ends_with(basename, ".nem") || ends_with(basename, ".n"))
85  {
86  std::ostringstream full_name;
87 
88  // Find the length of a string which represents the highest processor ID
89  full_name << (mymesh.n_processors());
90  int field_width = cast_int<int>(full_name.str().size());
91 
92  // reset the string stream
93  full_name.str("");
94 
95  // And build up the full filename
96  full_name << name
97  << '.' << mymesh.n_processors()
98  << '.' << std::setfill('0') << std::setw(field_width) << mymesh.processor_id();
99 
100  std::ifstream in (full_name.str().c_str());
101  libmesh_error_msg_if(!in.good(), "ERROR: cannot locate specified file:\n\t" << full_name.str());
102  }
103  else if (contains(basename, ".cp")) {} // Do error checking in the reader
104  else
105  {
106  std::ifstream in (name.c_str());
107  libmesh_error_msg_if(!in.good(), "ERROR: cannot locate specified file:\n\t" << name);
108  }
109 
110  // Look for parallel formats first
111  if (is_parallel_file_format(basename))
112  {
113  // no need to handle bz2 files here -- the Xdr class does that.
114  if (contains(basename, ".xda") ||
115  contains(basename, ".xdr"))
116  {
117  XdrIO xdr_io(mymesh);
118 
119  // .xda* ==> bzip2/gzip/ASCII flavors
120  if (contains(basename, ".xda"))
121  {
122  xdr_io.binary() = false;
123  xdr_io.read (name);
124  }
125  else // .xdr* ==> true binary XDR file
126  {
127  xdr_io.binary() = true;
128  xdr_io.read (name);
129  }
130 
131  // The xdr_io object gets constructed with legacy() == false.
132  // if legacy() == true then it means that a legacy file was detected and
133  // thus processor 0 performed the read. We therefore need to broadcast the
134  // mesh. Further, for this flavor of mesh solution data ordering is tied
135  // to the node ordering, so we better not reorder the nodes!
136  if (xdr_io.legacy())
137  {
138  mymesh.allow_renumbering(false);
139  MeshCommunication().broadcast(mymesh);
140  }
141 
142  // libHilbert-enabled libMesh builds should construct files
143  // with a canonical node ordering, which libHilbert-enabled
144  // builds will be able to read in again regardless of any
145  // renumbering. So in that case we're free to renumber.
146  // However, if either the writer or the reader of this file
147  // don't have libHilbert, then we'll have to skip
148  // renumbering because we need the numbering to remain
149  // consistent with any solution file we read in next.
150 #ifdef LIBMESH_HAVE_LIBHILBERT
151  // if (!xdr_io.libhilbert_ordering())
152  // skip_renumber_nodes_and_elements = true;
153 #else
154  mymesh.allow_renumbering(false);
155 #endif
156  }
157  else if (contains(basename, ".nem") ||
158  contains(basename, ".n"))
159  Nemesis_IO(mymesh).read (name);
160  else if (contains(basename, ".cp"))
161  {
162  if (contains(basename, ".cpa"))
163  CheckpointIO(mymesh, false).read(name);
164  else
165  CheckpointIO(mymesh, true).read(name);
166  }
167  }
168 
169  // Serial mesh formats
170  else
171  {
172  // Read the file based on extension. Only processor 0
173  // needs to read the mesh. It will then broadcast it and
174  // the other processors will pick it up
175  if (mymesh.processor_id() == 0)
176  {
177  LOG_SCOPE("read()", "NameBasedIO");
178 
179  std::ostringstream pid_suffix;
180  pid_suffix << '_' << getpid();
181  // Nasty hack for reading/writing zipped files
182  std::string new_name = name;
183  if (ends_with(name, ".bz2"))
184  {
185 #ifdef LIBMESH_HAVE_BZIP
186  new_name.erase(new_name.end() - 4, new_name.end());
187  new_name += pid_suffix.str();
188  std::string system_string = "bunzip2 -f -k -c ";
189  system_string += name + " > " + new_name;
190  LOG_SCOPE("system(bunzip2)", "NameBasedIO");
191  if (std::system(system_string.c_str()))
192  libmesh_file_error(system_string);
193 #else
194  libmesh_error_msg("ERROR: need bzip2/bunzip2 to open .bz2 file " << name);
195 #endif
196  }
197  else if (ends_with(name, ".xz"))
198  {
199 #ifdef LIBMESH_HAVE_XZ
200  new_name.erase(new_name.end() - 3, new_name.end());
201  new_name += pid_suffix.str();
202  std::string system_string = "xz -f -d -k -c ";
203  system_string += name + " > " + new_name;
204  LOG_SCOPE("system(xz -d)", "XdrIO");
205  if (std::system(system_string.c_str()))
206  libmesh_file_error(system_string);
207 #else
208  libmesh_error_msg("ERROR: need xz to open .xz file " << name);
209 #endif
210  }
211 
212  if (contains(basename, ".mat"))
213  MatlabIO(mymesh).read(new_name);
214 
215  else if (contains(basename, ".ucd"))
216  UCDIO(mymesh).read (new_name);
217 
218  else if (contains(basename, ".off") ||
219  contains(basename, ".ogl") ||
220  contains(basename, ".oogl"))
221  OFFIO(mymesh).read (new_name);
222 
223  else if (contains(basename, ".unv"))
224  UNVIO(mymesh).read (new_name);
225 
226  else if (contains(basename, ".node") ||
227  contains(basename, ".ele"))
228  TetGenIO(mymesh).read (new_name);
229 
230  else if (contains(basename, ".exd") ||
231  contains(basename, ".e"))
232  ExodusII_IO(mymesh).read (new_name);
233 
234  else if (contains(basename, ".msh"))
235  GmshIO(mymesh).read (new_name);
236 
237  else if (contains(basename, ".gmv"))
238  GMVIO(mymesh).read (new_name);
239 
240  else if (contains(basename, ".stl"))
241  STLIO(mymesh).read (new_name);
242 
243  else if (contains(basename, ".pvtu") ||
244  contains(basename, ".vtu"))
245  VTKIO(mymesh).read(new_name);
246 
247  else if (contains(basename, ".inp"))
248  AbaqusIO(mymesh).read(new_name);
249 
250  else if (contains(basename, ".bext") ||
251  contains(basename, ".bxt"))
252  DynaIO(mymesh).read (new_name);
253 
254  else if (contains(basename, ".bez"))
255  DynaIO(mymesh, false).read (new_name);
256 
257  else
258  {
259  libmesh_error_msg(" ERROR: Unrecognized file extension: " \
260  << name \
261  << "\n I understand the following:\n\n" \
262  << " *.bext -- Bezier files in DYNA format\n" \
263  << " *.bez -- Bezier DYNA files, omit spline nodes\n" \
264  << " *.bxt -- Bezier files in DYNA format\n" \
265  << " *.cpa -- libMesh Checkpoint ASCII format\n" \
266  << " *.cpr -- libMesh Checkpoint binary format\n" \
267  << " *.e -- Sandia's ExodusII format\n" \
268  << " *.exd -- Sandia's ExodusII format\n" \
269  << " *.gmv -- LANL's General Mesh Viewer format\n" \
270  << " *.inp -- Abaqus .inp format\n" \
271  << " *.mat -- Matlab triangular ASCII file\n" \
272  << " *.n -- Sandia's Nemesis format\n" \
273  << " *.nem -- Sandia's Nemesis format\n" \
274  << " *.off -- OOGL OFF surface format\n" \
275  << " *.ogl -- OOGL OFF surface format\n" \
276  << " *.oogl -- OOGL OFF surface format\n" \
277  << " *.pvtu -- Paraview VTK format\n" \
278  << " *.stl -- STereoLithography triangulation format\n" \
279  << " *.ucd -- AVS's ASCII UCD format\n" \
280  << " *.unv -- I-deas Universal format\n" \
281  << " *.vtu -- Paraview VTK format\n" \
282  << " *.xda -- libMesh ASCII format\n" \
283  << " *.xdr -- libMesh binary format\n" \
284  << " *.gz -- any above format gzipped\n" \
285  << " *.bz2 -- any above format bzip2'ed\n" \
286  << " *.xz -- any above format xzipped\n" \
287  );
288  }
289 
290  // If we temporarily decompressed a file, remove the
291  // uncompressed version
292  if (ends_with(basename, ".bz2"))
293  std::remove(new_name.c_str());
294  if (ends_with(basename, ".xz"))
295  std::remove(new_name.c_str());
296  }
297 
298  // Send the mesh & bcs (which are now only on processor 0) to the other
299  // processors
300  MeshCommunication().broadcast (mymesh);
301  }
302 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
bool is_parallel_file_format(std::string_view filename)
Definition: namebased_io.C:571
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition: utility.C:213
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205
std::string_view basename_of(const std::string &fullname)
Definition: utility.C:108

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

◆ 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 libMesh::UCDIO::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::NameBasedIO::write ( const std::string &  mesh_file)
overridevirtual

This method implements writing a mesh to a specified file.

Implements libMesh::MeshOutput< MeshBase >.

Definition at line 305 of file namebased_io.C.

References TIMPI::Communicator::barrier(), libMesh::Utility::basename_of(), TIMPI::Communicator::broadcast(), libMesh::ParallelObject::comm(), libMesh::Utility::contains(), libMesh::Utility::ends_with(), libMesh::err, is_parallel_file_format(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::n_partitions(), libMesh::Quality::name(), libMesh::GMVIO::partitioning(), libMesh::ParallelObject::processor_id(), libMesh::FroIO::write(), libMesh::TecplotIO::write(), libMesh::STLIO::write(), libMesh::GMVIO::write(), libMesh::MEDITIO::write(), libMesh::TetGenIO::write(), libMesh::UCDIO::write(), libMesh::UNVIO::write(), libMesh::Nemesis_IO::write(), libMesh::GmshIO::write(), libMesh::XdrIO::write(), libMesh::VTKIO::write(), libMesh::CheckpointIO::write(), and libMesh::ExodusII_IO::write().

Referenced by libMesh::UnstructuredMesh::find_neighbors(), and libMesh::UnstructuredMesh::write().

306 {
307  const MeshBase & mymesh = MeshOutput<MeshBase>::mesh();
308 
309  const std::string_view basename = Utility::basename_of(name);
310 
311  using Utility::contains;
312  using Utility::ends_with;
313 
314  // parallel formats are special -- they may choose to write
315  // separate files, let's not try to handle the zipping here.
316  if (is_parallel_file_format(basename))
317  {
318  // no need to handle bz2 files here -- the Xdr class does that.
319  if (contains(basename, ".xda"))
320  XdrIO(mymesh).write(name);
321 
322  else if (contains(basename, ".xdr"))
323  XdrIO(mymesh,true).write(name);
324 
325  else if (contains(basename, ".nem") ||
326  contains(basename, ".n"))
327  Nemesis_IO(mymesh).write(name);
328 
329  else if (contains(basename, ".cpa"))
330  CheckpointIO(mymesh,false).write(name);
331 
332  else if (contains(basename, ".cpr"))
333  CheckpointIO(mymesh,true).write(name);
334 
335  else
336  libmesh_error_msg("Couldn't deduce filetype for " << name);
337  }
338 
339  // serial file formats
340  else
341  {
342  // Nasty hack for reading/writing zipped files
343  std::string new_name = name;
344  int pid_0 = 0;
345  if (mymesh.processor_id() == 0)
346  pid_0 = getpid();
347  mymesh.comm().broadcast(pid_0);
348  std::ostringstream pid_suffix;
349  pid_suffix << '_' << pid_0;
350 
351  if (ends_with(name, ".bz2"))
352  {
353  new_name.erase(new_name.end() - 4, new_name.end());
354  new_name += pid_suffix.str();
355  }
356  else if (ends_with(name, ".xz"))
357  {
358  new_name.erase(new_name.end() - 3, new_name.end());
359  new_name += pid_suffix.str();
360  }
361 
362  // New scope so that io will close before we try to zip the file
363  {
364  // Write the file based on extension
365  if (contains(basename, ".dat"))
366  TecplotIO(mymesh).write (new_name);
367 
368  else if (contains(basename, ".plt"))
369  TecplotIO(mymesh,true).write (new_name);
370 
371  else if (contains(basename, ".ucd"))
372  UCDIO (mymesh).write (new_name);
373 
374  else if (contains(basename, ".gmv"))
375  if (mymesh.n_partitions() > 1)
376  GMVIO(mymesh).write (new_name);
377  else
378  {
379  GMVIO io(mymesh);
380  io.partitioning() = false;
381  io.write (new_name);
382  }
383 
384  else if (contains(basename, ".exd") ||
385  contains(basename, ".e"))
386  ExodusII_IO(mymesh).write(new_name);
387 
388  else if (contains(basename, ".unv"))
389  UNVIO(mymesh).write (new_name);
390 
391  else if (contains(basename, ".mesh"))
392  MEDITIO(mymesh).write (new_name);
393 
394  else if (contains(basename, ".poly"))
395  TetGenIO(mymesh).write (new_name);
396 
397  else if (contains(basename, ".msh"))
398  GmshIO(mymesh).write (new_name);
399 
400  else if (contains(basename, ".fro"))
401  FroIO(mymesh).write (new_name);
402 
403  else if (contains(basename, ".pvtu"))
404  VTKIO(mymesh).write (name);
405 
406  else if (contains(basename, ".stl"))
407  STLIO(mymesh).write (new_name);
408 
409  else
410  {
412  << " ERROR: Unrecognized file extension: " << name
413  << "\n I understand the following:\n\n"
414  << " *.cpa -- libMesh ASCII checkpoint format\n"
415  << " *.cpr -- libMesh binary checkpoint format,\n"
416  << " *.dat -- Tecplot ASCII file\n"
417  << " *.e -- Sandia's ExodusII format\n"
418  << " *.exd -- Sandia's ExodusII format\n"
419  << " *.fro -- ACDL's surface triangulation file\n"
420  << " *.gmv -- LANL's GMV (General Mesh Viewer) format\n"
421  << " *.mesh -- MEdit mesh format\n"
422  << " *.msh -- GMSH ASCII file\n"
423  << " *.n -- Sandia's Nemesis format\n"
424  << " *.nem -- Sandia's Nemesis format\n"
425  << " *.plt -- Tecplot binary file\n"
426  << " *.poly -- TetGen ASCII file\n"
427  << " *.pvtu -- VTK (paraview-readable) format\n"
428  << " *.stl -- STereoLithography triangulation format\n" \
429  << " *.ucd -- AVS's ASCII UCD format\n"
430  << " *.unv -- I-deas Universal format\n"
431  << " *.xda -- libMesh ASCII format\n"
432  << " *.xdr -- libMesh binary format,\n"
433  << std::endl
434  << "\n Exiting without writing output\n";
435  }
436  }
437 
438  // Nasty hack for reading/writing zipped files
439  if (ends_with(basename, ".bz2"))
440  {
441  LOG_SCOPE("system(bzip2)", "NameBasedIO");
442  if (mymesh.processor_id() == 0)
443  {
444  std::string system_string = "bzip2 -f -c ";
445  system_string += new_name + " > " + name;
446  if (std::system(system_string.c_str()))
447  libmesh_file_error(system_string);
448  std::remove(new_name.c_str());
449  }
450  mymesh.comm().barrier();
451  }
452  if (ends_with(basename, ".xz"))
453  {
454  LOG_SCOPE("system(xz)", "NameBasedIO");
455  if (mymesh.processor_id() == 0)
456  {
457  std::string system_string = "xz -f -c ";
458  system_string += new_name + " > " + name;
459  if (std::system(system_string.c_str()))
460  libmesh_file_error(system_string);
461  std::remove(new_name.c_str());
462  }
463  mymesh.comm().barrier();
464  }
465  }
466 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
OStreamProxy err
const MT & mesh() const
Definition: mesh_output.h:259
bool is_parallel_file_format(std::string_view filename)
Definition: namebased_io.C:571
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition: utility.C:213
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205
std::string_view basename_of(const std::string &fullname)
Definition: utility.C:108

◆ 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::NameBasedIO::write_equation_systems ( const std::string &  filename,
const EquationSystems es,
const std::set< std::string > *  system_names = nullptr 
)
overridevirtual

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

We override the default MeshOutput::write_equation_systems because it only outputs nodal data by default, whereas we want to output a proper restart file if the requested filename is an XDA or XDR type.

Reimplemented from libMesh::MeshOutput< MeshBase >.

Definition at line 538 of file namebased_io.C.

References libMesh::Utility::basename_of(), libMesh::Utility::contains(), libMesh::ENCODE, libMesh::WRITE, libMesh::EquationSystems::write(), libMesh::EquationSystems::WRITE_ADDITIONAL_DATA, libMesh::EquationSystems::WRITE_DATA, and libMesh::MeshOutput< MT >::write_equation_systems().

Referenced by main().

541 {
542  // XDA/XDR require a separate code path, and currently only support
543  // writing complete restarts
544  if (!system_names)
545  {
546  const std::string_view basename =
547  Utility::basename_of(filename);
548 
549  if (Utility::contains(basename, ".xda"))
550  {
551  es.write(filename,WRITE,
554  return;
555  }
556  else if (Utility::contains(basename, ".xdr"))
557  {
558  es.write(filename,ENCODE,
561  return;
562  }
563  }
564 
565  // Other formats just use the default "write nodal values" path
567  (filename, es, system_names);
568 }
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 ...
Definition: mesh_output.C:31
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205
std::string_view basename_of(const std::string &fullname)
Definition: utility.C:108

◆ write_nodal_data() [1/3]

void libMesh::NameBasedIO::write_nodal_data ( const std::string &  name,
const std::vector< Number > &  v,
const std::vector< std::string > &  vn 
)
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 469 of file namebased_io.C.

References libMesh::Utility::contains(), libMesh::Utility::ends_with(), libMesh::err, libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::n_subdomains(), libMesh::Quality::name(), libMesh::GMVIO::partitioning(), libMesh::TecplotIO::write_nodal_data(), libMesh::MEDITIO::write_nodal_data(), libMesh::GMVIO::write_nodal_data(), libMesh::UCDIO::write_nodal_data(), libMesh::VTKIO::write_nodal_data(), libMesh::GmshIO::write_nodal_data(), libMesh::Nemesis_IO::write_nodal_data(), and libMesh::ExodusII_IO::write_nodal_data().

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

472 {
473  const MeshBase & mymesh = MeshOutput<MeshBase>::mesh();
474 
475  using Utility::contains;
476  using Utility::ends_with;
477 
478  // Write the file based on extension
479  if (contains(name, ".dat"))
480  TecplotIO(mymesh).write_nodal_data (name, v, vn);
481 
482  else if (contains(name, ".exd") ||
483  contains(name, ".e"))
484  ExodusII_IO(mymesh).write_nodal_data(name, v, vn);
485 
486  else if (contains(name, ".gmv"))
487  {
488  if (mymesh.n_subdomains() > 1)
489  GMVIO(mymesh).write_nodal_data (name, v, vn);
490  else
491  {
492  GMVIO io(mymesh);
493  io.partitioning() = false;
494  io.write_nodal_data (name, v, vn);
495  }
496  }
497 
498  else if (contains(name, ".mesh"))
499  MEDITIO(mymesh).write_nodal_data (name, v, vn);
500 
501  else if (contains(name, ".msh"))
502  GmshIO(mymesh).write_nodal_data (name, v, vn);
503 
504  else if (contains(name, ".nem") ||
505  contains(name, ".n"))
506  Nemesis_IO(mymesh).write_nodal_data(name, v, vn);
507 
508  else if (contains(name, ".plt"))
509  TecplotIO(mymesh,true).write_nodal_data (name, v, vn);
510 
511  else if (contains(name, ".pvtu"))
512  VTKIO(mymesh).write_nodal_data (name, v, vn);
513 
514  else if (contains(name, ".ucd"))
515  UCDIO (mymesh).write_nodal_data (name, v, vn);
516 
517  else
518  {
520  << " ERROR: Unrecognized file extension: " << name
521  << "\n I understand the following:\n\n"
522  << " *.dat -- Tecplot ASCII file\n"
523  << " *.e -- Sandia's ExodusII format\n"
524  << " *.exd -- Sandia's ExodusII format\n"
525  << " *.gmv -- LANL's GMV (General Mesh Viewer) format\n"
526  << " *.mesh -- MEdit mesh format\n"
527  << " *.msh -- GMSH ASCII file\n"
528  << " *.n -- Sandia's Nemesis format\n"
529  << " *.nem -- Sandia's Nemesis format\n"
530  << " *.plt -- Tecplot binary file\n"
531  << " *.pvtu -- Paraview VTK file\n"
532  << " *.ucd -- AVS's ASCII UCD format\n"
533  << "\n Exiting without writing output\n";
534  }
535 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
OStreamProxy err
const MT & mesh() const
Definition: mesh_output.h:259
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition: utility.C:213
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition: utility.C:205

◆ 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(); }

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

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

◆ elems_of_dimension

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

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