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