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

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

References TIMPI::Communicator::barrier(), libMesh::Utility::basename_of(), TIMPI::Communicator::broadcast(), libMesh::ParallelObject::comm(), 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::MEDITIO::write(), libMesh::GMVIO::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().

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

References libMesh::Utility::basename_of(), 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().

533 {
534  // XDA/XDR require a separate code path, and currently only support
535  // writing complete restarts
536  if (!system_names)
537  {
538  const std::string_view basename =
539  Utility::basename_of(filename);
540 
541  if (basename.rfind(".xda") < basename.size())
542  {
543  es.write(filename,WRITE,
546  return;
547  }
548  else if (basename.rfind(".xdr") < basename.size())
549  {
550  es.write(filename,ENCODE,
553  return;
554  }
555  }
556 
557  // Other formats just use the default "write nodal values" path
559  (filename, es, system_names);
560 }
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
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 464 of file namebased_io.C.

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

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

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