xdr_mesh.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 // Local includes 00019 #include "libmesh/xdr_mesh.h" 00020 #include "libmesh/xdr_mhead.h" 00021 #include "libmesh/enum_elem_type.h" // for ElemType 00022 00023 namespace libMesh 00024 { 00025 00026 // ------------------------------------------------------------ 00027 // XdrMESH members 00028 int XdrMESH::header(XdrMHEAD *hd) 00029 { 00030 // Temporary variables to facilitate stream reading 00031 const int comm_len= 256; 00032 char comment[comm_len]; 00033 00034 switch (m_type) 00035 { 00036 00037 #ifdef LIBMESH_HAVE_XDR 00038 00039 case (XdrMGF::DECODE): 00040 case (XdrMGF::ENCODE): 00041 { 00042 xdr_int(mp_xdr_handle, &(hd->m_numel)); 00043 xdr_int(mp_xdr_handle, &(hd->m_numNodes)); 00044 xdr_int(mp_xdr_handle, &(hd->m_sumWghts)); 00045 xdr_int(mp_xdr_handle, &(hd->m_numBCs)); 00046 xdr_int(mp_xdr_handle, &(hd->m_strSize)); 00047 break; 00048 } 00049 00050 #endif 00051 00052 case (XdrMGF::W_ASCII): 00053 { 00054 mp_out << hd->m_numel << "\t # Num. Elements\n"; 00055 mp_out << hd->m_numNodes << "\t # Num. Nodes\n"; 00056 mp_out << hd->m_sumWghts << "\t # Sum of Element Weights\n"; 00057 mp_out << hd->m_numBCs << "\t # Num. Boundary Conds.\n"; 00058 mp_out << hd->m_strSize << "\t # String Size (ignore)\n"; 00059 break; 00060 } 00061 00062 case (XdrMGF::R_ASCII): 00063 { 00064 libmesh_assert (mp_in.good()); 00065 00066 mp_in >> hd->m_numel ; mp_in.getline(comment, comm_len); 00067 mp_in >> hd->m_numNodes ; mp_in.getline(comment, comm_len); 00068 mp_in >> hd->m_sumWghts ; mp_in.getline(comment, comm_len); 00069 mp_in >> hd->m_numBCs ; mp_in.getline(comment, comm_len); 00070 mp_in >> hd->m_strSize ; mp_in.getline(comment, comm_len); 00071 00072 libmesh_assert(mp_in.good()); 00073 00074 break; 00075 } 00076 00077 default: 00078 // Unknown access type 00079 libmesh_error(); 00080 00081 } 00082 00083 // Let's write the augmented header information 00084 // before we write the title and id string 00085 00086 // Both DEAL and LIBM style files have augmented headers. 00087 if ((orig_flag == 0) || (orig_flag == 2)) 00088 { 00089 00090 switch (m_type) 00091 { 00092 00093 #ifdef LIBMESH_HAVE_XDR 00094 00095 case (XdrMGF::ENCODE): 00096 case (XdrMGF::DECODE): 00097 { 00098 // this used to be 0. How did that work? 00099 unsigned int temp_n_blocks = hd->get_n_blocks(); 00100 xdr_u_int(mp_xdr_handle, &temp_n_blocks); 00101 hd->set_n_blocks(temp_n_blocks); 00102 00103 // The number of blocks (i.e. the number of element types) 00104 // for any mesh must always 00105 // be at least 1. 00106 libmesh_assert_not_equal_to (hd->get_n_blocks(), 0); 00107 break; 00108 } 00109 00110 #endif 00111 00112 case (XdrMGF::W_ASCII): 00113 { 00114 mp_out << hd->get_n_blocks() << "\t # Num. Element Blocks.\n"; 00115 break; 00116 } 00117 00118 case (XdrMGF::R_ASCII): 00119 { 00120 libmesh_assert (mp_in.good()); 00121 unsigned int temp_n_blocks=0; 00122 mp_in >> temp_n_blocks; 00123 hd->set_n_blocks(temp_n_blocks); 00124 mp_in.getline(comment, comm_len); 00125 break; 00126 } 00127 00128 default: 00129 // Unknown access type 00130 libmesh_error(); 00131 } 00132 00133 00134 std::vector<ElemType> et; 00135 hd->get_block_elt_types(et); 00136 00137 00138 // Note: If DECODING or READING, allocate space in the vector 00139 if ((m_type == DECODE) || (m_type == R_ASCII)) 00140 et.resize(hd->get_n_blocks()); 00141 00142 00143 switch (m_type) 00144 { 00145 00146 #ifdef LIBMESH_HAVE_XDR 00147 00148 case (XdrMGF::ENCODE): 00149 case (XdrMGF::DECODE): 00150 { 00151 xdr_vector(mp_xdr_handle, 00152 (char *) &et[0], 00153 et.size(), 00154 sizeof(unsigned int), 00155 (xdrproc_t) xdr_u_int); 00156 break; 00157 } 00158 00159 #endif 00160 00161 case (XdrMGF::W_ASCII): 00162 { 00163 for (unsigned int i=0; i<hd->get_n_blocks(); i++) 00164 mp_out << et[i] << " "; 00165 00166 mp_out << "\t # Element types in each block.\n"; 00167 break; 00168 } 00169 00170 case (XdrMGF::R_ASCII): 00171 { 00172 libmesh_assert (mp_in.good()); 00173 00174 for (unsigned int i=0; i<hd->get_n_blocks(); i++) 00175 { 00176 // convoluted way of doing it to 00177 // satisfy icc 00178 unsigned int type; 00179 00180 mp_in >> type ; 00181 00182 et[i] = static_cast<ElemType>(type) ; 00183 } 00184 mp_in.getline(comment, comm_len); 00185 break; 00186 } 00187 00188 default: 00189 // Unknown access type 00190 libmesh_error(); 00191 } 00192 00193 00194 00195 // Note: If DECODING or READING, you need to set the value 00196 // in the header data structure. 00197 if ((m_type == DECODE) || (m_type == R_ASCII)) 00198 hd->set_block_elt_types(et); 00199 00200 00201 std::vector<unsigned int> neeb; 00202 hd->get_num_elem_each_block(neeb); 00203 00204 // If DECODING or READING, allocate space for the vector 00205 if ((m_type == DECODE) || (m_type == R_ASCII)) 00206 neeb.resize( hd->get_n_blocks()*(this->get_num_levels()+1) ); 00207 00208 switch (m_type) 00209 { 00210 00211 #ifdef LIBMESH_HAVE_XDR 00212 00213 case (XdrMGF::ENCODE): 00214 case (XdrMGF::DECODE): 00215 { 00216 xdr_vector(mp_xdr_handle, 00217 (char *) &neeb[0], 00218 neeb.size(), 00219 sizeof(unsigned int), 00220 (xdrproc_t) xdr_u_int); 00221 } 00222 00223 #endif 00224 00225 case (XdrMGF::W_ASCII): 00226 { 00227 for (unsigned int i=0; i<neeb.size(); i++) 00228 mp_out << neeb[i] << " "; 00229 00230 mp_out << "\t # Num. of elements in each block at each level.\n"; 00231 break; 00232 } 00233 00234 case (XdrMGF::R_ASCII): 00235 { 00236 00237 // We will treat this line as containing 00238 // 1.) The number of elements in each block OR 00239 // 2.) The number of elements at each level in each block 00240 // Therefore, we don't know a-priori how many ints to read. 00241 00242 // Get the full line from the stream up to the newline 00243 mp_in.getline(comment, comm_len); 00244 00245 // Construct a char buffer to hold the tokens as we 00246 // process them, and construct a std::string object and 00247 // a std::stringstream object for tokenizing this line. 00248 char token[comm_len]; 00249 std::string s_temp(comment); 00250 std::stringstream ss(s_temp); 00251 00252 // Resize the neeb vector to zero so we can push back 00253 // values onto it. Note that we are using a tokenizer 00254 // scheme again here to read the line, but it's not entirely 00255 // necessary since we know the size neeb should have. 00256 neeb.resize(0); 00257 00258 // Process the tokens one at a time 00259 while (ss >> token) 00260 { 00261 // If you reach the hash, the rest of the line is a comment, 00262 // so quit reading. 00263 if (token[0] == '#') 00264 break; 00265 00266 // If you reach an alphabetic character, this is an error 00267 if (!isdigit(token[0])) 00268 { 00269 libMesh::err << "Error: Unrecognized character detected." 00270 << std::endl; 00271 libmesh_error(); 00272 } 00273 00274 // Otherwise, add the value to the neeb vector 00275 neeb.push_back( std::atoi(token) ); 00276 } 00277 00278 // Be sure you have the right number of entries in neeb 00279 libmesh_assert_equal_to (neeb.size(), (hd->get_n_blocks() * (this->get_num_levels()+1))); 00280 00281 break; 00282 } 00283 00284 default: 00285 // Unknown access type 00286 libmesh_error(); 00287 } 00288 00289 if ((m_type == DECODE) || (m_type == R_ASCII)) 00290 hd->set_num_elem_each_block(neeb); 00291 } 00292 00293 00294 else if (orig_flag == 1) // MGF originator 00295 { 00296 } 00297 else // Unknown Originator! 00298 { 00299 libmesh_error(); 00300 } 00301 00302 00303 00304 00305 // Write the ID and TITLE strings (can be safely ignored) 00306 switch (m_type) 00307 { 00308 00309 #ifdef LIBMESH_HAVE_XDR 00310 00311 case (XdrMGF::ENCODE): 00312 case (XdrMGF::DECODE): 00313 { 00314 char* temp = hd->cpyString(hd->getId()); 00315 xdr_string(mp_xdr_handle,&temp, ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize)); 00316 hd->setId(temp); 00317 delete [] temp; 00318 00319 temp = hd->cpyString(hd->getTitle()); 00320 00321 xdr_string(mp_xdr_handle,&temp, ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize)); 00322 hd->setTitle(temp); 00323 delete [] temp; 00324 break; 00325 } 00326 00327 #endif 00328 00329 case (XdrMGF::W_ASCII): 00330 { 00331 mp_out << hd->mp_id << '\n'; 00332 mp_out << hd->mp_title << '\n'; 00333 break; 00334 } 00335 00336 case (XdrMGF::R_ASCII): 00337 { 00338 libmesh_assert (mp_in.good()); 00339 00340 mp_in.getline(comment, comm_len); 00341 hd->setId(comment); 00342 00343 libmesh_assert (mp_in.good()); 00344 00345 mp_in.getline(comment, comm_len); 00346 hd->setTitle(comment); 00347 00348 break; 00349 } 00350 00351 default: 00352 // Unknown access type 00353 libmesh_error(); 00354 } 00355 00356 return 1; 00357 } 00358 00359 } // namespace libMesh
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:49 UTC
Hosted By: