off_io.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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(nv-1);
101  if (nv == 3)
102  {
103 #if LIBMESH_DIM < 2
104  libMesh::err << "Cannot open dimension 2 mesh file when configured without 2D support." <<
105  std::endl;
106  libmesh_error();
107 #endif
108  }
109  }
110 
111  Elem* elem;
112  switch (nv)
113  {
114  case 2: elem = new Edge2; break;
115  case 3: elem = new Tri3 ; break;
116  default: libmesh_error();
117  }
118 
119  elem->set_id(e);
120  the_mesh.add_elem (elem);
121 
122  for (unsigned int i=0; i<nv; i++)
123  {
124  in >> nid;
125  elem->set_node(i) = the_mesh.node_ptr(nid);
126  }
127  }
128 }
129 
130 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo