libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | Private 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 (const std::string &filename)
 
virtual void write_discontinuous_equation_systems (const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
 This method implements writing a mesh with discontinuous data to a specified file where the data is taken from the EquationSystems object. More...
 
virtual void write_nodal_data (const std::string &, const NumericVector< Number > &, const std::vector< std::string > &)
 This method may be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data (const std::string &, const EquationSystems &, const std::set< std::string > *)
 This method should be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data_discontinuous (const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
 This method implements writing a mesh with discontinuous data to a specified file where the nodal data and variables names are provided. More...
 
unsigned intascii_precision ()
 Return/set the precision to use when writing ASCII files. More...
 

Protected Member Functions

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

Protected Attributes

std::vector< bool > elems_of_dimension
 A vector of bools describing what dimension elements have been encountered when reading a mesh. More...
 
const bool _is_parallel_format
 Flag specifying whether this format is parallel-capable. More...
 
const bool _serial_only_needed_on_proc_0
 Flag specifying whether this format can be written by only serializing the mesh to processor zero. More...
 

Private Attributes

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

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  :
111  MeshOutput<MeshBase> (mesh)
112 {
113 }

◆ 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),
118  MeshOutput<MeshBase>(mesh)
119 {
120 }

Member Function Documentation

◆ ascii_precision()

unsigned int & libMesh::MeshOutput< MeshBase >::ascii_precision ( )
inlineinherited

Return/set the precision to use when writing ASCII files.

By default we use numeric_limits<Real>::max_digits10, which should be enough to write out to ASCII and get the exact same Real back when reading in.

Definition at line 257 of file mesh_output.h.

258 {
259  return _ascii_precision;
260 }

◆ is_parallel_file_format()

bool libMesh::NameBasedIO::is_parallel_file_format ( const std::string &  filename)
inline

Definition at line 124 of file namebased_io.h.

125 {
126  return ((name.rfind(".xda") < name.size()) ||
127  (name.rfind(".xdr") < name.size()) ||
128  (name.rfind(".nem") + 4 == name.size()) ||
129  (name.rfind(".n") + 2 == name.size()) ||
130  (name.rfind(".cp") < name.size())
131  );
132 }

References libMesh::Quality::name().

Referenced by read(), and write().

◆ mesh() [1/2]

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

Definition at line 169 of file mesh_input.h.

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

◆ mesh() [2/2]

const MeshBase & libMesh::MeshOutput< MeshBase >::mesh ( ) const
inlineprotectedinherited
Returns
The object as a read-only reference.

Definition at line 247 of file mesh_output.h.

248 {
250  return *_obj;
251 }

◆ read()

void libMesh::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 62 of file namebased_io.C.

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

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::Nemesis_IO::read(), libMesh::ExodusII_IO::read(), libMesh::UNVIO::read(), libMesh::UCDIO::read(), libMesh::GMVIO::read(), libMesh::GmshIO::read(), libMesh::DynaIO::read(), libMesh::XdrIO::read(), libMesh::MatlabIO::read(), libMesh::CheckpointIO::read(), and libMesh::VTKIO::read().

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

◆ set_n_partitions()

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

Sets the number of partitions in the mesh.

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

Definition at line 91 of file mesh_input.h.

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

◆ skip_comment_lines()

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

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

Definition at line 179 of file mesh_input.h.

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

◆ write()

void libMesh::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 285 of file namebased_io.C.

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

References 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::TetGenIO::write(), libMesh::GMVIO::write(), libMesh::Nemesis_IO::write(), libMesh::ExodusII_IO::write(), libMesh::UNVIO::write(), libMesh::UCDIO::write(), libMesh::GmshIO::write(), libMesh::XdrIO::write(), libMesh::VTKIO::write(), and libMesh::CheckpointIO::write().

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

◆ write_discontinuous_equation_systems()

void libMesh::MeshOutput< MeshBase >::write_discontinuous_equation_systems ( const std::string &  fname,
const EquationSystems es,
const std::set< std::string > *  system_names = nullptr 
)
virtualinherited

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

Definition at line 87 of file mesh_output.C.

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

◆ write_equation_systems()

void libMesh::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 506 of file namebased_io.C.

509 {
510  // XDA/XDR require a separate code path, and currently only support
511  // writing complete restarts
512  if (!system_names)
513  {
514  if (filename.rfind(".xda") < filename.size())
515  {
516  es.write(filename,WRITE,
519  return;
520  }
521  else if (filename.rfind(".xdr") < filename.size())
522  {
523  es.write(filename,ENCODE,
526  return;
527  }
528  }
529 
530  // Other formats just use the default "write nodal values" path
532  (filename, es, system_names);
533 }

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

◆ write_nodal_data() [1/3]

void libMesh::MeshOutput< MeshBase >::write_nodal_data ( const std::string &  fname,
const EquationSystems es,
const std::set< std::string > *  system_names 
)
virtualinherited

This method should be overridden by "parallel" output formats for writing nodal data.

Instead of getting a localized copy of the nodal solution vector, it directly uses EquationSystems current_local_solution vectors to look up nodal values.

If not implemented, reorders the solutions into a nodal-only NumericVector and calls the above version of this function.

Reimplemented in libMesh::Nemesis_IO.

Definition at line 158 of file mesh_output.C.

161 {
162  std::vector<std::string> names;
163  es.build_variable_names (names, nullptr, system_names);
164 
165  std::unique_ptr<NumericVector<Number>> parallel_soln =
166  es.build_parallel_solution_vector(system_names);
167 
168  this->write_nodal_data (fname, *parallel_soln, names);
169 }

◆ write_nodal_data() [2/3]

void libMesh::MeshOutput< MeshBase >::write_nodal_data ( const std::string &  fname,
const NumericVector< Number > &  parallel_soln,
const std::vector< std::string > &  names 
)
virtualinherited

This method may be overridden by "parallel" output formats for writing nodal data.

Instead of getting a localized copy of the nodal solution vector, it is passed a NumericVector of type=PARALLEL which is in node-major order i.e. (u0,v0,w0, u1,v1,w1, u2,v2,w2, u3,v3,w3, ...) and contains n_nodes*n_vars total entries. Then, it is up to the individual I/O class to extract the required solution values from this vector and write them in parallel.

If not implemented, localizes the parallel vector into a std::vector and calls the other version of this function.

Reimplemented in libMesh::Nemesis_IO.

Definition at line 145 of file mesh_output.C.

148 {
149  // This is the fallback implementation for parallel I/O formats that
150  // do not yet implement proper writing in parallel, and instead rely
151  // on the full solution vector being available on all processors.
152  std::vector<Number> soln;
153  parallel_soln.localize(soln);
154  this->write_nodal_data(fname, soln, names);
155 }

◆ write_nodal_data() [3/3]

void libMesh::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 441 of file namebased_io.C.

444 {
445  MeshBase & mymesh = MeshInput<MeshBase>::mesh();
446 
447  // Write the file based on extension
448  if (name.rfind(".dat") < name.size())
449  TecplotIO(mymesh).write_nodal_data (name, v, vn);
450 
451  else if (name.rfind(".e") < name.size())
452  ExodusII_IO(mymesh).write_nodal_data(name, v, vn);
453 
454  else if (name.rfind(".gmv") < name.size())
455  {
456  if (mymesh.n_subdomains() > 1)
457  GMVIO(mymesh).write_nodal_data (name, v, vn);
458  else
459  {
460  GMVIO io(mymesh);
461  io.partitioning() = false;
462  io.write_nodal_data (name, v, vn);
463  }
464  }
465 
466  else if (name.rfind(".mesh") < name.size())
467  MEDITIO(mymesh).write_nodal_data (name, v, vn);
468 
469  else if (name.rfind(".msh") < name.size())
470  GmshIO(mymesh).write_nodal_data (name, v, vn);
471 
472  else if (name.rfind(".nem") < name.size() ||
473  name.rfind(".n") < name.size())
474  Nemesis_IO(mymesh).write_nodal_data(name, v, vn);
475 
476  else if (name.rfind(".plt") < name.size())
477  TecplotIO(mymesh,true).write_nodal_data (name, v, vn);
478 
479  else if (name.rfind(".pvtu") < name.size())
480  VTKIO(mymesh).write_nodal_data (name, v, vn);
481 
482  else if (name.rfind(".ucd") < name.size())
483  UCDIO (mymesh).write_nodal_data (name, v, vn);
484 
485  else
486  {
488  << " ERROR: Unrecognized file extension: " << name
489  << "\n I understand the following:\n\n"
490  << " *.dat -- Tecplot ASCII file\n"
491  << " *.e -- Sandia's ExodusII format\n"
492  << " *.exd -- Sandia's ExodusII format\n"
493  << " *.gmv -- LANL's GMV (General Mesh Viewer) format\n"
494  << " *.mesh -- MEdit mesh format\n"
495  << " *.msh -- GMSH ASCII file\n"
496  << " *.n -- Sandia's Nemesis format\n"
497  << " *.nem -- Sandia's Nemesis format\n"
498  << " *.plt -- Tecplot binary file\n"
499  << " *.pvtu -- Paraview VTK file\n"
500  << " *.ucd -- AVS's ASCII UCD format\n"
501  << "\n Exiting without writing output\n";
502  }
503 }

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::VTKIO::write_nodal_data(), libMesh::GMVIO::write_nodal_data(), libMesh::UCDIO::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().

◆ write_nodal_data_discontinuous()

virtual void libMesh::MeshOutput< MeshBase >::write_nodal_data_discontinuous ( const std::string &  ,
const std::vector< Number > &  ,
const std::vector< std::string > &   
)
inlinevirtualinherited

This method implements writing a mesh with discontinuous data to a specified file where the nodal data and variables names are provided.

Reimplemented in libMesh::ExodusII_IO.

Definition at line 114 of file mesh_output.h.

117  { libmesh_not_implemented(); }

Member Data Documentation

◆ _ascii_precision

unsigned int libMesh::MeshOutput< MeshBase >::_ascii_precision
privateinherited

Precision to use when writing ASCII files.

Definition at line 195 of file mesh_output.h.

◆ _is_parallel_format [1/2]

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

Flag specifying whether this format is parallel-capable.

If this is false (default) I/O is only permitted when the mesh has been serialized.

Definition at line 121 of file mesh_input.h.

◆ _is_parallel_format [2/2]

const bool libMesh::MeshOutput< MeshBase >::_is_parallel_format
protectedinherited

Flag specifying whether this format is parallel-capable.

If this is false (default) I/O is only permitted when the mesh has been serialized.

Definition at line 172 of file mesh_output.h.

◆ _obj

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

A pointer to a non-const object object.

This allows us to read the object from file.

Definition at line 114 of file mesh_input.h.

◆ _serial_only_needed_on_proc_0

const bool libMesh::MeshOutput< MeshBase >::_serial_only_needed_on_proc_0
protectedinherited

Flag specifying whether this format can be written by only serializing the mesh to processor zero.

If this is false (default) the mesh will be serialized to all processors

Definition at line 181 of file mesh_output.h.

◆ elems_of_dimension

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

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

Definition at line 97 of file mesh_input.h.


The documentation for this class was generated from the following files:
libMesh::MeshOutput< MeshBase >::_serial_only_needed_on_proc_0
const bool _serial_only_needed_on_proc_0
Flag specifying whether this format can be written by only serializing the mesh to processor zero.
Definition: mesh_output.h:181
libMesh::MeshBase::set_n_partitions
unsigned int & set_n_partitions()
Definition: mesh_base.h:1667
libMesh::EquationSystems::WRITE_DATA
Definition: equation_systems.h:93
libMesh::WRITE
Definition: enum_xdr_mode.h:40
libMesh::MeshInput< MeshBase >::_obj
MeshBase * _obj
A pointer to a non-const object object.
Definition: mesh_input.h:114
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::NameBasedIO::is_parallel_file_format
bool is_parallel_file_format(const std::string &filename)
Definition: namebased_io.h:124
libMesh::NumericVector::localize
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.
libMesh::MeshOutput< MeshBase >::_is_parallel_format
const bool _is_parallel_format
Flag specifying whether this format is parallel-capable.
Definition: mesh_output.h:172
libMesh::MeshOutput::write_equation_systems
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
libMesh::ENCODE
Definition: enum_xdr_mode.h:38
libMesh::MeshOutput< MeshBase >::write_nodal_data
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: mesh_output.h:105
libMesh::MeshOutput< MeshBase >::write_nodal_data_discontinuous
virtual void write_nodal_data_discontinuous(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with discontinuous data to a specified file where the nodal dat...
Definition: mesh_output.h:114
libMesh::MeshInput< MeshBase >::mesh
MeshBase & mesh()
Definition: mesh_input.h:169
libMesh::MeshOutput< MeshBase >::_obj
const MeshBase *const _obj
A pointer to a constant object.
Definition: mesh_output.h:190
libMesh::EquationSystems::WRITE_ADDITIONAL_DATA
Definition: equation_systems.h:94
libMesh::err
OStreamProxy err
libMesh::out
OStreamProxy out
libMesh::MeshOutput< MeshBase >::_ascii_precision
unsigned int _ascii_precision
Precision to use when writing ASCII files.
Definition: mesh_output.h:195
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42