matlab_io.C
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 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 (libMesh::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::err << "Cannot open dimension 2 mesh file when configured without 2D support." << 00059 std::endl; 00060 libmesh_error(); 00061 #endif 00062 00063 // Check the input buffer 00064 libmesh_assert (in.good()); 00065 00066 unsigned int nNodes=0, nElem=0; 00067 00068 in >> nNodes // Read the number of nodes 00069 >> nElem; // Read the number of elements 00070 00071 // Sort of check that it worked 00072 libmesh_assert_greater (nNodes, 0); 00073 libmesh_assert_greater (nElem, 0); 00074 00075 // Read the nodal coordinates 00076 { 00077 Real x=0., y=0., z=0.; 00078 00079 for (unsigned int i=0; i<nNodes; i++) 00080 { 00081 in >> x // x-coordinate value 00082 >> y; // y-coordinate value 00083 00084 the_mesh.add_point ( Point(x,y,z), i); 00085 } 00086 } 00087 00088 // Read the elements (elements) 00089 { 00090 unsigned int node=0, dummy=0; 00091 00092 for (unsigned int i=0; i<nElem; i++) 00093 { 00094 Elem* elem = new Tri3; // Always build a triangle 00095 elem->set_id(i); 00096 the_mesh.add_elem (elem); 00097 00098 for (unsigned int n=0; n<3; n++) // Always read three 3 nodes 00099 { 00100 in >> node; 00101 elem->set_node(n) = the_mesh.node_ptr(node-1); // Assign the node number 00102 } 00103 00104 // There is an additional subdomain number here, 00105 // so we read it and get rid of it! 00106 in >> dummy; 00107 } 00108 } 00109 00110 } 00111 00112 } // namespace libMesh
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: