libMesh
off_io.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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  std::unique_ptr<Elem> elem;
110  switch (nv)
111  {
112  case 2:
113  elem = Elem::build(EDGE2);
114  break;
115 
116  case 3:
117  elem = Elem::build(TRI3);
118  break;
119 
120  default:
121  libmesh_error_msg("Unsupported nv = " << nv);
122  }
123 
124  elem->set_id(e);
125 
126  for (unsigned int i=0; i<nv; i++)
127  {
128  in >> nid;
129  elem->set_node(i) = the_mesh.node_ptr(nid);
130  }
131 
132  the_mesh.add_elem(std::move(elem));
133  }
134 }
135 
136 } // namespace libMesh
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
The libMesh namespace provides an interface to certain functionality in the library.
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
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
This is the MeshBase class.
Definition: mesh_base.h:74
void read_stream(std::istream &in)
Implementation of the read() function.
Definition: off_io.C:44
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:273
libmesh_assert(ctx)
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:269
virtual void clear()
Deletes all the element and node data that is currently stored.
Definition: mesh_base.C:862
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void read(const std::string &name) override
Reads in an OFF OOGL data file based on the string you pass it.
Definition: off_io.C:35
virtual const Node * node_ptr(const dof_id_type i) const =0
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39