matlab_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 // C++ includes
20 #include <fstream>
21 
22 // Local includes
23 #include "libmesh/matlab_io.h"
24 #include "libmesh/mesh_base.h"
25 #include "libmesh/face_tri3.h"
26 
27 namespace libMesh
28 {
29 
30 // ------------------------------------------------------------
31 // MatlabIO class members
32 
33 void MatlabIO::read(const std::string & name)
34 {
35  std::ifstream in (name.c_str());
36 
37  this->read_stream(in);
38 }
39 
40 
41 void MatlabIO::read_stream(std::istream & in)
42 {
43  // This is a serial-only process for now;
44  // the Mesh should be read on processor 0 and
45  // broadcast later
46  libmesh_assert_equal_to (this->mesh().processor_id(), 0);
47 
48  // Get a reference to the mesh
49  MeshBase & the_mesh = MeshInput<MeshBase>::mesh();
50 
51  // Clear any existing mesh data
52  the_mesh.clear();
53 
54  // PDE toolkit only works in 2D
55  the_mesh.set_mesh_dimension(2);
56 
57 #if LIBMESH_DIM < 2
58  libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support.");
59 #endif
60 
61  // Check the input buffer
62  libmesh_assert (in.good());
63 
64  unsigned int nNodes=0, nElem=0;
65 
66  in >> nNodes // Read the number of nodes
67  >> nElem; // Read the number of elements
68 
69  // Sort of check that it worked
70  libmesh_assert_greater (nNodes, 0);
71  libmesh_assert_greater (nElem, 0);
72 
73  // Read the nodal coordinates
74  {
75  Real x=0., y=0., z=0.;
76 
77  for (unsigned int i=0; i<nNodes; i++)
78  {
79  in >> x // x-coordinate value
80  >> y; // y-coordinate value
81 
82  the_mesh.add_point ( Point(x,y,z), i);
83  }
84  }
85 
86  // Read the elements (elements)
87  {
88  unsigned int node=0, dummy=0;
89 
90  for (unsigned int i=0; i<nElem; i++)
91  {
92  Elem * elem = new Tri3; // Always build a triangle
93  elem->set_id(i);
94  the_mesh.add_elem (elem);
95 
96  for (unsigned int n=0; n<3; n++) // Always read three 3 nodes
97  {
98  in >> node;
99  elem->set_node(n) = the_mesh.node_ptr(node-1); // Assign the node number
100  }
101 
102  // There is an additional subdomain number here,
103  // so we read it and get rid of it!
104  in >> dummy;
105  }
106  }
107 
108 }
109 
110 } // 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
virtual void read(const std::string &name) override
Definition: matlab_io.C:33
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
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
void read_stream(std::istream &in)
Definition: matlab_io.C:41
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual const Node * node_ptr(const dof_id_type i) const =0
A geometric point in (x,y,z) space.
Definition: point.h:38