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 : 20 : #ifndef LIBMESH_MESH_INPUT_H 21 : #define LIBMESH_MESH_INPUT_H 22 : 23 : 24 : // Local includes 25 : #include "libmesh/libmesh_common.h" 26 : #include "libmesh/mesh_base.h" 27 : 28 : // C++ includes 29 : #include <cstddef> 30 : #include <istream> 31 : #include <string> 32 : #include <vector> 33 : 34 : namespace libMesh 35 : { 36 : 37 : 38 : 39 : /** 40 : * This class defines an abstract interface for \p Mesh input. 41 : * Specific classes derived from this class actually implement 42 : * reading various mesh formats. 43 : * 44 : * \author Benjamin S. Kirk 45 : * \date 2004 46 : */ 47 : template <class MT> 48 : class MeshInput 49 : { 50 : protected: 51 : 52 : /** 53 : * Default constructor. Will set the _obj to nullptr, effectively 54 : * rendering this object useless. 55 : */ 56 : explicit 57 : MeshInput (bool is_parallel_format = false); 58 : 59 : /** 60 : * Constructor. Takes a writable reference to an object. 61 : * This is the constructor required to read an object. 62 : */ 63 : explicit 64 : MeshInput (MT &, const bool is_parallel_format = false); 65 : 66 : public: 67 : 68 : /** 69 : * Destructor. 70 : */ 71 : virtual ~MeshInput (); 72 : 73 : /** 74 : * This method implements reading a mesh from a specified file. 75 : * 76 : * This should typically be called in parallel if 77 : * is_parallel_format(), but should be called only on processor 0 78 : * (after which a mesh broadcast() is called) otherwise. 79 : */ 80 : virtual void read (const std::string &) = 0; 81 : 82 : /** 83 : * Returns true iff this mesh file format and input class are 84 : * parallelized, so that all processors can read their share of the 85 : * data at once. 86 : */ 87 : bool is_parallel_format () const { return this->_is_parallel_format; } 88 : 89 : protected: 90 : 91 : /** 92 : * \returns The object as a writable reference. 93 : */ 94 : MT & mesh (); 95 : 96 : /** 97 : * Sets the number of partitions in the mesh. Typically this gets 98 : * done by the partitioner, but some parallel file formats begin 99 : * "pre-partitioned". 100 : */ 101 4362 : void set_n_partitions (unsigned int n_parts) { this->mesh().set_n_partitions() = n_parts; } 102 : 103 : /** 104 : * A vector of bools describing what dimension elements 105 : * have been encountered when reading a mesh. 106 : */ 107 : std::vector<bool> elems_of_dimension; 108 : 109 : /** 110 : * Reads input from \p in, skipping all the lines 111 : * that start with the character \p comment_start. 112 : */ 113 : void skip_comment_lines (std::istream & in, 114 : const char comment_start); 115 : 116 : private: 117 : 118 : 119 : /** 120 : * A pointer to a non-const object object. 121 : * This allows us to read the object from file. 122 : */ 123 : MT * _obj; 124 : 125 : /** 126 : * Flag specifying whether this format is parallel-capable. 127 : * If this is false (default) I/O is only permitted when the mesh 128 : * has been serialized. 129 : */ 130 : const bool _is_parallel_format; 131 : }; 132 : 133 : 134 : 135 : // ------------------------------------------------------------ 136 : // MeshInput inline members 137 : template <class MT> 138 : inline 139 4656 : MeshInput<MT>::MeshInput (const bool is_parallel_format) : 140 : elems_of_dimension(), 141 3352 : _obj (nullptr), 142 5308 : _is_parallel_format(is_parallel_format) 143 : { 144 652 : } 145 : 146 : 147 : 148 : template <class MT> 149 : inline 150 83937 : MeshInput<MT>::MeshInput (MT & obj, const bool is_parallel_format) : 151 : elems_of_dimension(), 152 79090 : _obj (&obj), 153 83937 : _is_parallel_format(is_parallel_format) 154 : { 155 83937 : if (!_is_parallel_format && !this->mesh().is_serial()) 156 : { 157 47003 : if (this->mesh().processor_id() == 0) 158 : { 159 5888 : libmesh_do_once(libMesh::out << 160 : "Warning: This MeshOutput subclass only supports meshes which have been serialized!" 161 : << std::endl;); 162 : } 163 : } 164 83937 : } 165 : 166 : 167 : 168 : template <class MT> 169 : inline 170 1328 : MeshInput<MT>::~MeshInput () 171 : { 172 51649 : } 173 : 174 : 175 : 176 : template <class MT> 177 : inline 178 225209 : MT & MeshInput<MT>::mesh () 179 : { 180 225209 : libmesh_error_msg_if(_obj == nullptr, "ERROR: _obj should not be nullptr!"); 181 225209 : return *_obj; 182 : } 183 : 184 : 185 : 186 : template <class MT> 187 34 : void MeshInput<MT>::skip_comment_lines (std::istream & in, 188 : const char comment_start) 189 : { 190 : char c, line[256]; 191 : 192 100 : while (in.get(c), c==comment_start) 193 72 : in.getline (line, 255); 194 : 195 : // put back first character of 196 : // first non-comment line 197 28 : in.putback (c); 198 28 : } 199 : 200 : 201 : } // namespace libMesh 202 : 203 : 204 : #endif // LIBMESH_MESH_INPUT_H