Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 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 0 : void MatlabIO::read(const std::string & name) 34 : { 35 0 : std::ifstream in (name.c_str()); 36 : 37 0 : this->read_stream(in); 38 0 : } 39 : 40 : 41 0 : 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 0 : libmesh_assert_equal_to (this->mesh().processor_id(), 0); 47 : 48 : // Get a reference to the mesh 49 0 : MeshBase & the_mesh = MeshInput<MeshBase>::mesh(); 50 : 51 : // Clear any existing mesh data 52 0 : the_mesh.clear(); 53 : 54 : // PDE toolkit only works in 2D 55 0 : 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 0 : libmesh_assert (in.good()); 63 : 64 0 : unsigned int nNodes=0, nElem=0; 65 : 66 0 : in >> nNodes // Read the number of nodes 67 0 : >> nElem; // Read the number of elements 68 : 69 : // Sort of check that it worked 70 0 : libmesh_assert_greater (nNodes, 0); 71 0 : libmesh_assert_greater (nElem, 0); 72 : 73 : // Read the nodal coordinates 74 : { 75 0 : Real x=0., y=0., z=0.; 76 : 77 0 : for (unsigned int i=0; i<nNodes; i++) 78 : { 79 0 : in >> x // x-coordinate value 80 0 : >> y; // y-coordinate value 81 : 82 0 : the_mesh.add_point ( Point(x,y,z), i); 83 : } 84 : } 85 : 86 : // Read the elements (elements) 87 : { 88 0 : unsigned int node=0, dummy=0; 89 : 90 0 : for (unsigned int i=0; i<nElem; i++) 91 : { 92 : // Always build a triangle 93 0 : Elem * elem = the_mesh.add_elem(Elem::build_with_id(TRI3, i)); 94 : 95 0 : for (unsigned int n=0; n<3; n++) // Always read three 3 nodes 96 : { 97 0 : in >> node; 98 0 : elem->set_node(n, the_mesh.node_ptr(node-1)); // Assign the node number 99 : } 100 : 101 : // There is an additional subdomain number here, 102 : // so we read it and get rid of it! 103 0 : in >> dummy; 104 : } 105 : } 106 : 107 0 : } 108 : 109 : } // namespace libMesh