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

This class implements writing meshes in the mesh format used by the MEdit visualization tool developed in the Gamma Project at INRIA Roquencourt. More...

#include <medit_io.h>

Inheritance diagram for libMesh::MEDITIO:
[legend]

Public Member Functions

 MEDITIO (const MeshBase &)
 Constructor. More...
 
 MEDITIO (const MeshBase &, unsigned int c)
 Constructor. More...
 
virtual void write (const std::string &) override
 This method implements writing a mesh to a specified ".mesh" file. 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 & binary ()
 Flag indicating whether or not to write a binary file. More...
 
virtual void write_equation_systems (const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
 This method implements writing a mesh with data to a specified file where the data is taken from the EquationSystems object. More...
 
virtual void write_discontinuous_equation_systems (const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
 This method implements writing a mesh with discontinuous data to a specified file where the data is taken from the EquationSystems object. More...
 
virtual void write_nodal_data (const std::string &, const NumericVector< Number > &, const std::vector< std::string > &)
 This method may be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data (const std::string &, const EquationSystems &, const std::set< std::string > *)
 This method should be overridden by "parallel" output formats for writing nodal data. More...
 
virtual void write_nodal_data_discontinuous (const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
 This method implements writing a mesh with discontinuous data to a specified file where the nodal data and variables names are provided. More...
 
unsigned intascii_precision ()
 Return/set the precision to use when writing ASCII files. More...
 

Protected Member Functions

const MeshBasemesh () const
 

Protected Attributes

const bool _is_parallel_format
 Flag specifying whether this format is parallel-capable. More...
 
const bool _serial_only_needed_on_proc_0
 Flag specifying whether this format can be written by only serializing the mesh to processor zero. More...
 

Private Member Functions

void write_ascii (const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
 This method implements writing a mesh with nodal data to a specified file where the nodal data and variable names are optionally provided. More...
 

Private Attributes

bool _binary
 Flag to write binary data. More...
 
unsigned int scalar_idx
 
const MeshBase *const _obj
 A pointer to a constant object. More...
 
unsigned int _ascii_precision
 Precision to use when writing ASCII files. More...
 

Detailed Description

This class implements writing meshes in the mesh format used by the MEdit visualization tool developed in the Gamma Project at INRIA Roquencourt.

For a full description of the mesh format and to obtain the MEdit software see the MEdit home page.

Author
Florian Prill
Date
2004

Definition at line 47 of file medit_io.h.

Constructor & Destructor Documentation

◆ MEDITIO() [1/2]

libMesh::MEDITIO::MEDITIO ( const MeshBase mesh_in)
inlineexplicit

Constructor.

Takes a reference to a constant mesh object. This constructor will only allow us to write the mesh.

Definition at line 113 of file medit_io.h.

113  :
114  MeshOutput<MeshBase> (mesh_in),
115  _binary (false),
116  scalar_idx (0)
117 {
118 }

◆ MEDITIO() [2/2]

libMesh::MEDITIO::MEDITIO ( const MeshBase mesh_in,
unsigned int  c 
)
inline

Constructor.

Takes a reference to a constant mesh object. and the desired scalar index for mesh colouring. MEdit seems to understand only one scalar value.

Definition at line 121 of file medit_io.h.

121  :
122  MeshOutput<MeshBase> (mesh_in),
123  _binary (false),
124  scalar_idx (c)
125 {
126 }

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 }

◆ binary()

bool & libMesh::MEDITIO::binary ( )
inline

Flag indicating whether or not to write a binary file.

Definition at line 130 of file medit_io.h.

131 {
132  return _binary;
133 }

References _binary.

Referenced by write(), and write_nodal_data().

◆ mesh()

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 }

◆ write()

void libMesh::MEDITIO::write ( const std::string &  fname)
overridevirtual

This method implements writing a mesh to a specified ".mesh" file.

Implements libMesh::MeshOutput< MeshBase >.

Definition at line 37 of file medit_io.C.

38 {
39  if (this->mesh().processor_id() == 0)
40  if (!this->binary())
41  this->write_ascii (fname);
42 }

References binary(), libMesh::MeshOutput< MeshBase >::mesh(), and write_ascii().

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

◆ write_ascii()

void libMesh::MEDITIO::write_ascii ( const std::string &  fname,
const std::vector< Number > *  vec = nullptr,
const std::vector< std::string > *  solution_names = nullptr 
)
private

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

This will write an ASCII file.

Definition at line 59 of file medit_io.C.

62 {
63  // Current lacks in implementation:
64  // (i) only 3D meshes.
65  // (ii) only QUAD4, TRI3, TET4 elements, others are omitted !
66  // (iii) no distinction between materials.
67  // (iv) no vector output, just first scalar as output
68 
69  // libmesh_assert three dimensions (should be extended later)
70  libmesh_assert_equal_to (MeshOutput<MeshBase>::mesh().mesh_dimension(), 3);
71 
72  // Open the output file stream
73  std::ofstream out_stream (fname.c_str());
74 
75  // Make sure it opened correctly
76  if (!out_stream.good())
77  libmesh_file_error(fname.c_str());
78 
79  // Get a reference to the mesh
80  const MeshBase & the_mesh = MeshOutput<MeshBase>::mesh();
81 
82  // Begin interfacing with the MEdit data file
83  {
84  // header:
85  out_stream << "MeshVersionFormatted 1\n";
86  out_stream << "Dimension 3\n";
87  out_stream << "# Mesh generated by libmesh\n\n";
88 
89  // write the nodes:
90  out_stream << "# Set of mesh vertices\n";
91  out_stream << "Vertices\n";
92  out_stream << the_mesh.n_nodes() << "\n";
93 
94  for (auto v : IntRange<unsigned int>(0, the_mesh.n_nodes()))
95  out_stream << the_mesh.point(v)(0) << " " << the_mesh.point(v)(1) << " " << the_mesh.point(v)(2) << " 0\n";
96  }
97 
98  {
99  // write the connectivity:
100  out_stream << "\n# Set of Polys\n\n";
101 
102  // count occurrences of output elements:
103  int n_tri3 = 0;
104  int n_quad4 = 0;
105  int n_tet4 = 0;
106 
107  for (const auto & elem : the_mesh.active_element_ptr_range())
108  {
109  if (elem->type() == TRI3) n_tri3++;
110  if (elem->type() == QUAD4) n_quad4++;
111  if (elem->type() == QUAD9) n_quad4+=4; // (QUAD9 is written as 4 QUAD4.)
112  if (elem->type() == TET4) n_tet4++;
113  }
114 
115  // First: write out TRI3 elements:
116  out_stream << "Triangles\n";
117  out_stream << n_tri3 << "\n";
118 
119  for (const auto & elem : the_mesh.active_element_ptr_range())
120  if (elem->type() == TRI3)
121  out_stream << elem->node_id(0)+1 << " "
122  << elem->node_id(1)+1 << " "
123  << elem->node_id(2)+1 << " 0\n";
124 
125  // Second: write out QUAD4 elements:
126  out_stream << "Quadrilaterals\n";
127  out_stream << n_quad4 << "\n";
128 
129  for (const auto & elem : the_mesh.active_element_ptr_range())
130  {
131  if (elem->type() == QUAD4)
132  {
133  out_stream << elem->node_id(0)+1 << " "
134  << elem->node_id(1)+1 << " "
135  << elem->node_id(2)+1 << " "
136  << elem->node_id(3)+1 <<" 0\n";
137  } // if
138  else if (elem->type() == QUAD9)
139  {
140  out_stream << elem->node_id(0)+1 << " "
141  << elem->node_id(4)+1 << " "
142  << elem->node_id(8)+1 << " "
143  << elem->node_id(7)+1 <<" 0\n";
144  out_stream << elem->node_id(7)+1 << " "
145  << elem->node_id(8)+1 << " "
146  << elem->node_id(6)+1 << " "
147  << elem->node_id(3)+1 <<" 0\n";
148  out_stream << elem->node_id(4)+1 << " "
149  << elem->node_id(1)+1 << " "
150  << elem->node_id(5)+1 << " "
151  << elem->node_id(8)+1 <<" 0\n";
152  out_stream << elem->node_id(8)+1 << " "
153  << elem->node_id(5)+1 << " "
154  << elem->node_id(2)+1 << " "
155  << elem->node_id(6)+1 <<" 0\n";
156  }
157  }
158 
159  // Third: write out TET4 elements:
160  out_stream << "Tetrahedra\n";
161  out_stream << n_tet4 << "\n";
162 
163  for (const auto & elem : the_mesh.active_element_ptr_range())
164  if (elem->type() == TET4)
165  {
166  out_stream << elem->node_id(0)+1 << " "
167  << elem->node_id(1)+1 << " "
168  << elem->node_id(2)+1 << " "
169  << elem->node_id(3)+1 <<" 0\n";
170  }
171  }
172  // end of the out file
173  out_stream << '\n' << "# end of file\n";
174 
175  // optionally write the data
176  if ((solution_names != nullptr) &&
177  (vec != nullptr))
178  {
179  // Open the ".bb" file stream
180  std::size_t idx = fname.find_last_of(".");
181  std::string bbname = fname.substr(0,idx) + ".bb";
182 
183  std::ofstream bbout (bbname.c_str());
184 
185  // Make sure it opened correctly
186  if (!bbout.good())
187  libmesh_file_error(bbname.c_str());
188 
189  // Header: 3: 3D mesh, 1: scalar output, 2: node-indexed
190  const std::size_t n_vars = solution_names->size();
191  bbout << "3 1 " << the_mesh.n_nodes() << " 2\n";
192  for (auto n : IntRange<dof_id_type>(0, the_mesh.n_nodes()))
193  bbout << std::setprecision(this->ascii_precision()) << (*vec)[n*n_vars + scalar_idx] << " ";
194  bbout << "\n";
195  } // endif
196 }

References libMesh::MeshBase::active_element_ptr_range(), libMesh::MeshOutput< MeshBase >::ascii_precision(), libMesh::MeshTools::Generation::Private::idx(), libMesh::MeshOutput< MT >::mesh(), libMesh::MeshBase::n_nodes(), n_vars, libMesh::MeshBase::point(), libMesh::QUAD4, libMesh::QUAD9, scalar_idx, libMesh::TET4, and libMesh::TRI3.

Referenced by write(), and write_nodal_data().

◆ write_discontinuous_equation_systems()

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

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

Definition at line 87 of file mesh_output.C.

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

◆ write_equation_systems()

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

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

Reimplemented in libMesh::NameBasedIO.

Definition at line 31 of file mesh_output.C.

34 {
35  LOG_SCOPE("write_equation_systems()", "MeshOutput");
36 
37  // We may need to gather and/or renumber a DistributedMesh to output
38  // it, making that const qualifier in our constructor a dirty lie
39  MT & my_mesh = const_cast<MT &>(*_obj);
40 
41  // If we're asked to write data that's associated with a different
42  // mesh, output files full of garbage are the result.
43  libmesh_assert_equal_to(&es.get_mesh(), _obj);
44 
45  // A non-renumbered mesh may not have a contiguous numbering, and
46  // that needs to be fixed before we can build a solution vector.
47  if (my_mesh.max_elem_id() != my_mesh.n_elem() ||
48  my_mesh.max_node_id() != my_mesh.n_nodes())
49  {
50  // If we were allowed to renumber then we should have already
51  // been properly renumbered...
52  libmesh_assert(!my_mesh.allow_renumbering());
53 
54  libmesh_do_once(libMesh::out <<
55  "Warning: This MeshOutput subclass only supports meshes which are contiguously renumbered!"
56  << std::endl;);
57 
58  my_mesh.allow_renumbering(true);
59 
60  my_mesh.renumber_nodes_and_elements();
61 
62  // Not sure what good going back to false will do here, the
63  // renumbering horses have already left the barn...
64  my_mesh.allow_renumbering(false);
65  }
66 
68  {
69  MeshSerializer serialize(const_cast<MT &>(*_obj), !_is_parallel_format, _serial_only_needed_on_proc_0);
70 
71  // Build the list of variable names that will be written.
72  std::vector<std::string> names;
73  es.build_variable_names (names, nullptr, system_names);
74 
75  // Build the nodal solution values & get the variable
76  // names from the EquationSystems object
77  std::vector<Number> soln;
78  es.build_solution_vector (soln, system_names);
79 
80  this->write_nodal_data (fname, soln, names);
81  }
82  else // _is_parallel_format
83  this->write_nodal_data (fname, es, system_names);
84 }

◆ write_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::MEDITIO::write_nodal_data ( const std::string &  fname,
const std::vector< Number > &  soln,
const std::vector< std::string > &  names 
)
overridevirtual

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

Reimplemented from libMesh::MeshOutput< MeshBase >.

Definition at line 46 of file medit_io.C.

49 {
50  LOG_SCOPE("write_nodal_data()", "MEDITIO");
51 
52  if (this->mesh().processor_id() == 0)
53  if (!this->binary())
54  this->write_ascii (fname, &soln, &names);
55 }

References binary(), libMesh::MeshOutput< MeshBase >::mesh(), and write_ascii().

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

◆ write_nodal_data_discontinuous()

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

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

Reimplemented in libMesh::ExodusII_IO.

Definition at line 114 of file mesh_output.h.

117  { libmesh_not_implemented(); }

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.

◆ _binary

bool libMesh::MEDITIO::_binary
private

Flag to write binary data.

Definition at line 103 of file medit_io.h.

Referenced by binary().

◆ _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 172 of file mesh_output.h.

◆ _obj

const MeshBase * const libMesh::MeshOutput< MeshBase >::_obj
privateinherited

A pointer to a constant object.

This allows us to write the object to file.

Definition at line 190 of file mesh_output.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.

◆ scalar_idx

unsigned int libMesh::MEDITIO::scalar_idx
private

Definition at line 105 of file medit_io.h.

Referenced by write_ascii().


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
n_vars
unsigned int n_vars
Definition: adaptivity_ex3.C:116
libMesh::TET4
Definition: enum_elem_type.h:45
libMesh::MeshOutput< MeshBase >::ascii_precision
unsigned int & ascii_precision()
Return/set the precision to use when writing ASCII files.
Definition: mesh_output.h:257
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::MEDITIO::write_ascii
void write_ascii(const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: medit_io.C:59
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::MEDITIO::binary
bool & binary()
Flag indicating whether or not to write a binary file.
Definition: medit_io.h:130
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::QUAD4
Definition: enum_elem_type.h:41
libMesh::TRI3
Definition: enum_elem_type.h:39
libMesh::MeshTools::Generation::Private::idx
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
A useful inline function which replaces the macros used previously.
Definition: mesh_generation.C:72
libMesh::MeshOutput< MeshBase >::write_nodal_data
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: mesh_output.h:105
libMesh::MeshOutput< MeshBase >::mesh
const MeshBase & mesh() const
Definition: mesh_output.h:247
libMesh::MEDITIO::_binary
bool _binary
Flag to write binary data.
Definition: medit_io.h:103
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::MeshOutput< MeshBase >::_obj
const MeshBase *const _obj
A pointer to a constant object.
Definition: mesh_output.h:190
libMesh::QUAD9
Definition: enum_elem_type.h:43
libMesh::out
OStreamProxy out
libMesh::MEDITIO::scalar_idx
unsigned int scalar_idx
Definition: medit_io.h:105
libMesh::MeshOutput< MeshBase >::_ascii_precision
unsigned int _ascii_precision
Precision to use when writing ASCII files.
Definition: mesh_output.h:195