matlab_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 
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::err << "Cannot open dimension 2 mesh file when configured without 2D support." <<
59  std::endl;
60  libmesh_error();
61 #endif
62 
63  // Check the input buffer
64  libmesh_assert (in.good());
65 
66  unsigned int nNodes=0, nElem=0;
67 
68  in >> nNodes // Read the number of nodes
69  >> nElem; // Read the number of elements
70 
71  // Sort of check that it worked
72  libmesh_assert_greater (nNodes, 0);
73  libmesh_assert_greater (nElem, 0);
74 
75  // Read the nodal coordinates
76  {
77  Real x=0., y=0., z=0.;
78 
79  for (unsigned int i=0; i<nNodes; i++)
80  {
81  in >> x // x-coordinate value
82  >> y; // y-coordinate value
83 
84  the_mesh.add_point ( Point(x,y,z), i);
85  }
86  }
87 
88  // Read the elements (elements)
89  {
90  unsigned int node=0, dummy=0;
91 
92  for (unsigned int i=0; i<nElem; i++)
93  {
94  Elem* elem = new Tri3; // Always build a triangle
95  elem->set_id(i);
96  the_mesh.add_elem (elem);
97 
98  for (unsigned int n=0; n<3; n++) // Always read three 3 nodes
99  {
100  in >> node;
101  elem->set_node(n) = the_mesh.node_ptr(node-1); // Assign the node number
102  }
103 
104  // There is an additional subdomain number here,
105  // so we read it and get rid of it!
106  in >> dummy;
107  }
108  }
109 
110 }
111 
112 } // namespace libMesh

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

Hosted By:
SourceForge.net Logo