libMesh
matlab_io.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
The Tri3 is an element in 2D composed of 3 nodes.
Definition: face_tri3.h:54
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:1941
This is the base class from which all geometric element types are derived.
Definition: elem.h:89
virtual const Node * node_ptr(const dof_id_type i) const =0
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:68
dof_id_type & set_id()
Definition: dof_object.h:641
libmesh_assert(j)
PetscErrorCode Vec x
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
virtual void read(const std::string &name) libmesh_override
Reads in a matlab data file based on the string you pass it.
Definition: matlab_io.C:33
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:199
virtual void clear()
Deletes all the data that are currently stored.
Definition: mesh_base.C:285
void read_stream(std::istream &in)
Implementation of the read() function.
Definition: matlab_io.C:41
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
processor_id_type processor_id()
Definition: libmesh_base.h:96
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38