$extrastylesheet
matlab_io.C
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 // C++ includes
00020 #include <fstream>
00021 
00022 // Local includes
00023 #include "libmesh/matlab_io.h"
00024 #include "libmesh/mesh_base.h"
00025 #include "libmesh/face_tri3.h"
00026 
00027 namespace libMesh
00028 {
00029 
00030 // ------------------------------------------------------------
00031 // MatlabIO class members
00032 
00033 void MatlabIO::read(const std::string& name)
00034 {
00035   std::ifstream in (name.c_str());
00036 
00037   this->read_stream(in);
00038 }
00039 
00040 
00041 void MatlabIO::read_stream(std::istream& in)
00042 {
00043   // This is a serial-only process for now;
00044   // the Mesh should be read on processor 0 and
00045   // broadcast later
00046   libmesh_assert_equal_to (this->mesh().processor_id(), 0);
00047 
00048   // Get a reference to the mesh
00049   MeshBase& the_mesh = MeshInput<MeshBase>::mesh();
00050 
00051   // Clear any existing mesh data
00052   the_mesh.clear();
00053 
00054   // PDE toolkit only works in 2D
00055   the_mesh.set_mesh_dimension(2);
00056 
00057 #if LIBMESH_DIM < 2
00058   libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support.");
00059 #endif
00060 
00061   // Check the input buffer
00062   libmesh_assert (in.good());
00063 
00064   unsigned int nNodes=0, nElem=0;
00065 
00066   in >> nNodes   // Read the number of nodes
00067      >> nElem;   // Read the number of elements
00068 
00069   // Sort of check that it worked
00070   libmesh_assert_greater (nNodes, 0);
00071   libmesh_assert_greater (nElem, 0);
00072 
00073   // Read the nodal coordinates
00074   {
00075     Real x=0., y=0., z=0.;
00076 
00077     for (unsigned int i=0; i<nNodes; i++)
00078       {
00079         in >> x   // x-coordinate value
00080            >> y;  // y-coordinate value
00081 
00082         the_mesh.add_point ( Point(x,y,z), i);
00083       }
00084   }
00085 
00086   // Read the elements (elements)
00087   {
00088     unsigned int node=0, dummy=0;
00089 
00090     for (unsigned int i=0; i<nElem; i++)
00091       {
00092         Elem* elem = new Tri3; // Always build a triangle
00093         elem->set_id(i);
00094         the_mesh.add_elem (elem);
00095 
00096         for (unsigned int n=0; n<3; n++)  // Always read three 3 nodes
00097           {
00098             in >> node;
00099             elem->set_node(n) = the_mesh.node_ptr(node-1);  // Assign the node number
00100           }
00101 
00102         // There is an additional subdomain number here,
00103         // so we read it and get rid of it!
00104         in >> dummy;
00105       }
00106   }
00107 
00108 }
00109 
00110 } // namespace libMesh