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::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)
inline

Definition at line 124 of file namebased_io.h.

References libMesh::Quality::name().

Referenced by read(), and write().

125 {
126  return ((name.rfind(".xda") < name.size()) ||
127  (name.rfind(".xdr") < name.size()) ||
128  (name.rfind(".nem") == name.size() - 4) ||
129  (name.rfind(".n") == name.size() - 2) ||
130  (name.rfind(".cp") < name.size())
131  );
132 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42

◆ 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(), 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::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 80 of file namebased_io.C.

References libMesh::MeshBase::allow_renumbering(), libMesh::XdrIO::binary(), libMesh::MeshCommunication::broadcast(), 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::Nemesis_IO::read(), libMesh::UNVIO::read(), libMesh::GmshIO::read(), libMesh::DynaIO::read(), libMesh::XdrIO::read(), libMesh::ExodusII_IO::read(), libMesh::MatlabIO::read(), libMesh::CheckpointIO::read(), and libMesh::VTKIO::read().

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

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

◆ 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 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 310 of file namebased_io.C.

References TIMPI::Communicator::barrier(), TIMPI::Communicator::broadcast(), libMesh::ParallelObject::comm(), libMesh::err, is_parallel_file_format(), libMesh::MeshInput< MT >::mesh(), libMesh::MeshBase::n_partitions(), libMesh::Quality::name(), libMesh::GMVIO::partitioning(), libMesh::ParallelObject::processor_id(), libMesh::FroIO::write(), libMesh::TecplotIO::write(), libMesh::MEDITIO::write(), libMesh::GMVIO::write(), libMesh::TetGenIO::write(), libMesh::UCDIO::write(), libMesh::Nemesis_IO::write(), libMesh::UNVIO::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().

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

◆ 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 533 of file namebased_io.C.

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

536 {
537  // XDA/XDR require a separate code path, and currently only support
538  // writing complete restarts
539  if (!system_names)
540  {
541  const std::string_view basename = basename_of(filename);
542 
543  if (basename.rfind(".xda") < basename.size())
544  {
545  es.write(filename,WRITE,
548  return;
549  }
550  else if (basename.rfind(".xdr") < basename.size())
551  {
552  es.write(filename,ENCODE,
555  return;
556  }
557  }
558 
559  // Other formats just use the default "write nodal values" path
561  (filename, es, system_names);
562 }
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

◆ 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 467 of file namebased_io.C.

References libMesh::err, libMesh::MeshInput< 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().

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

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