medit_io.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // C++ includes
21 #include <iomanip>
22 #include <fstream>
23 
24 // Local includes
25 #include "libmesh/libmesh_config.h"
27 #include "libmesh/mesh_base.h"
28 #include "libmesh/medit_io.h"
29 #include "libmesh/elem.h"
30 
31 namespace libMesh
32 {
33 
34 
35 // ------------------------------------------------------------
36 // MEDITIO members
37 void MEDITIO::write (const std::string & fname)
38 {
39  if (this->mesh().processor_id() == 0)
40  if (!this->binary())
41  this->write_ascii (fname);
42 }
43 
44 
45 
46 void MEDITIO::write_nodal_data (const std::string & fname,
47  const std::vector<Number> & soln,
48  const std::vector<std::string> & names)
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 }
56 
57 
58 
59 void MEDITIO::write_ascii (const std::string & fname,
60  const std::vector<Number> * vec,
61  const std::vector<std::string> * solution_names)
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 (unsigned int v=0; v<the_mesh.n_nodes(); v++)
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 (dof_id_type n=0; n<the_mesh.n_nodes(); n++)
193  bbout << std::setprecision(10) << (*vec)[n*n_vars + scalar_idx] << " ";
194  bbout << "\n";
195  } // endif
196 }
197 
198 } // namespace libMesh
const MeshBase & mesh() const
Definition: mesh_output.h:234
unsigned int scalar_idx
Definition: medit_io.h:105
virtual void write(const std::string &) override
Definition: medit_io.C:37
virtual SimpleRange< element_iterator > active_element_ptr_range()=0
const unsigned int n_vars
Definition: tecplot_io.C:69
Base class for Mesh.
Definition: mesh_base.h:77
bool & binary()
Definition: medit_io.h:130
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
Definition: medit_io.C:46
virtual const Point & point(const dof_id_type i) const =0
virtual dof_id_type n_nodes() const =0
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
uint8_t dof_id_type
Definition: id_types.h:64
void write_ascii(const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
Definition: medit_io.C:59