off_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 // C++ includes
19 #include <fstream>
20 
21 // Local includes
22 #include "libmesh/off_io.h"
23 #include "libmesh/mesh_base.h"
24 #include "libmesh/edge_edge2.h"
25 #include "libmesh/face_tri3.h"
26 
27 namespace libMesh
28 {
29 
30 
31 
32 // ------------------------------------------------------------
33 // OFFIO class members
34 
35 void OFFIO::read(const std::string & name)
36 {
37  std::ifstream in (name.c_str());
38 
39  read_stream(in);
40 }
41 
42 
43 
44 void OFFIO::read_stream(std::istream & in)
45 {
46  // This is a serial-only process for now;
47  // the Mesh should be read on processor 0 and
48  // broadcast later
49  libmesh_assert_equal_to (this->mesh().processor_id(), 0);
50 
51  // Get a reference to the mesh
52  MeshBase & the_mesh = MeshInput<MeshBase>::mesh();
53 
54  // Clear any existing mesh data
55  the_mesh.clear();
56 
57  // Check the input buffer
58  libmesh_assert (in.good());
59 
60  unsigned int nn, ne, nf;
61 
62  std::string label;
63 
64  // Read the first string. It should say "OFF"
65  in >> label;
66 
67  libmesh_assert_equal_to (label, "OFF");
68 
69  // read the number of nodes, faces, and edges
70  in >> nn >> nf >> ne;
71 
72 
73  Real x=0., y=0., z=0.;
74 
75  // Read the nodes
76  for (unsigned int n=0; n<nn; n++)
77  {
78  libmesh_assert (in.good());
79 
80  in >> x
81  >> y
82  >> z;
83 
84  the_mesh.add_point ( Point(x,y,z), n );
85  }
86 
87  unsigned int nv, nid;
88 
89  // Read the elements
90  for (unsigned int e=0; e<nf; e++)
91  {
92  libmesh_assert (in.good());
93 
94  // The number of vertices in the element
95  in >> nv;
96 
97  libmesh_assert(nv == 2 || nv == 3);
98  if (e == 0)
99  {
100  the_mesh.set_mesh_dimension(cast_int<unsigned char>(nv-1));
101  if (nv == 3)
102  {
103 #if LIBMESH_DIM < 2
104  libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support.");
105 #endif
106  }
107  }
108 
109  Elem * elem;
110  switch (nv)
111  {
112  case 2:
113  elem = new Edge2;
114  break;
115 
116  case 3:
117  elem = new Tri3;
118  break;
119 
120  default:
121  libmesh_error_msg("Unsupported nv = " << nv);
122  }
123 
124  elem->set_id(e);
125  the_mesh.add_elem (elem);
126 
127  for (unsigned int i=0; i<nv; i++)
128  {
129  in >> nid;
130  elem->set_node(i) = the_mesh.node_ptr(nid);
131  }
132  }
133 }
134 
135 } // namespace libMesh
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
A 2D triangular element with 3 nodes.
Definition: face_tri3.h:56
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:2024
The base class for all geometric element types.
Definition: elem.h:100
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Base class for Mesh.
Definition: mesh_base.h:77
dof_id_type & set_id()
Definition: dof_object.h:664
void read_stream(std::istream &in)
Definition: off_io.C:44
virtual Elem * add_elem(Elem *e)=0
void set_mesh_dimension(unsigned char d)
Definition: mesh_base.h:213
virtual void clear()
Definition: mesh_base.C:260
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void read(const std::string &name) override
Definition: off_io.C:35
A 1D geometric element with 2 nodes.
Definition: edge_edge2.h:43
virtual const Node * node_ptr(const dof_id_type i) const =0
A geometric point in (x,y,z) space.
Definition: point.h:38