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 : // C++ includes 19 : #include <fstream> 20 : 21 : // Local includes 22 : #include "libmesh/off_io.h" 23 : #include "libmesh/mesh_base.h" 24 : #include "libmesh/edge_edge2.h" 25 : #include "libmesh/face_tri3.h" 26 : 27 : namespace libMesh 28 : { 29 : 30 : 31 : 32 : // ------------------------------------------------------------ 33 : // OFFIO class members 34 : 35 12 : void OFFIO::read(const std::string & name) 36 : { 37 14 : std::ifstream in (name.c_str()); 38 : 39 12 : read_stream(in); 40 12 : } 41 : 42 : 43 : 44 12 : void OFFIO::read_stream(std::istream & in) 45 : { 46 : // This is a serial-only process for now; 47 : // the Mesh should be read on processor 0 and 48 : // broadcast later 49 1 : libmesh_assert_equal_to (this->mesh().processor_id(), 0); 50 : 51 : // Get a reference to the mesh 52 12 : MeshBase & the_mesh = MeshInput<MeshBase>::mesh(); 53 : 54 : // Clear any existing mesh data 55 12 : the_mesh.clear(); 56 : 57 : // Check the input buffer 58 1 : libmesh_assert (in.good()); 59 : 60 : unsigned int nn, ne, nf; 61 : 62 2 : std::string label; 63 : 64 : // Read the first string. It should say "OFF" 65 12 : in >> label; 66 : 67 1 : libmesh_assert_equal_to (label, "OFF"); 68 : 69 : // read the number of nodes, faces, and edges 70 1 : in >> nn >> nf >> ne; 71 : 72 : 73 12 : Real x=0., y=0., z=0.; 74 : 75 : // Read the nodes 76 72 : for (unsigned int n=0; n<nn; n++) 77 : { 78 5 : libmesh_assert (in.good()); 79 : 80 5 : in >> x 81 5 : >> y 82 5 : >> z; 83 : 84 65 : the_mesh.add_point ( Point(x,y,z), n ); 85 : } 86 : 87 : unsigned int nv, nid; 88 : 89 : // Read the elements 90 60 : for (unsigned int e=0; e<nf; e++) 91 : { 92 4 : libmesh_assert (in.good()); 93 : 94 : // The number of vertices in the element 95 4 : in >> nv; 96 : 97 4 : libmesh_assert(nv == 2 || nv == 3); 98 48 : if (e == 0) 99 : { 100 22 : the_mesh.set_mesh_dimension(cast_int<unsigned char>(nv-1)); 101 1 : if (nv == 3) 102 : { 103 : #if LIBMESH_DIM < 2 104 : libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support."); 105 : #endif 106 : } 107 : } 108 : 109 40 : std::unique_ptr<Elem> elem; 110 48 : switch (nv) 111 : { 112 0 : case 2: 113 0 : elem = Elem::build(EDGE2); 114 0 : break; 115 : 116 48 : case 3: 117 88 : elem = Elem::build(TRI3); 118 48 : break; 119 : 120 0 : default: 121 0 : libmesh_error_msg("Unsupported nv = " << nv); 122 : } 123 : 124 40 : elem->set_id(e); 125 : 126 192 : for (unsigned int i=0; i<nv; i++) 127 : { 128 12 : in >> nid; 129 144 : elem->set_node(i, the_mesh.node_ptr(nid)); 130 : } 131 : 132 56 : the_mesh.add_elem(std::move(elem)); 133 40 : } 134 12 : } 135 : 136 : } // namespace libMesh