Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 422 lines (304 sloc) 8.273 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
// The libMesh Finite Element Library.
// Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

// C++ includes
#include <iomanip>

// Local includes
#include "libmesh/xdr_mgf.h"

namespace libMesh
{

// XdrMGF member functions
XdrMGF::~XdrMGF()
{
  this->fini();
}



void XdrMGF::fini()
{

#ifdef LIBMESH_HAVE_XDR

  if (mp_xdr_handle)
    {
      //libMesh::out << "Destroying XDR file handle." << std::endl;
      xdr_destroy(mp_xdr_handle);
    }

  //libMesh::out << "Deleting the file handle pointer." << std::endl;
  delete mp_xdr_handle;

  mp_xdr_handle = NULL;

#endif

  if (mp_fp)
    {
      //libMesh::out << "Closing file." << std::endl;
      std::fflush(mp_fp);
      std::fclose(mp_fp);
    }

  mp_fp = NULL;
}






void XdrMGF::init (XdrMGF::XdrIO_TYPE t, const char* fn, const char*, int)
{
  m_type=t;

  // Close old file if necessary
  if (mp_fp) this->fini();


  // Open file
  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::ENCODE):
    case (XdrMGF::DECODE):
      {
mp_fp = fopen (fn, (m_type == ENCODE) ? "w" : "r");

// Make sure the file is ready for use
if (!mp_fp)
{
libMesh::err << "XDR Error: Accessing file: "
<< fn
<< " failed."
<< std::endl;
libmesh_error();
}

// Create the XDR handle
mp_xdr_handle = new XDR;
xdrstdio_create(mp_xdr_handle,
mp_fp,
((m_type == ENCODE) ? XDR_ENCODE : XDR_DECODE));

break;
      }

#endif

    case (XdrMGF::R_ASCII):
      {
mp_in.open(fn, std::ios::in);

        // Make sure it opened correctly
        if (!mp_in.good())
          libmesh_file_error(fn);

break;
      }

    case (XdrMGF::W_ASCII):
      {
mp_out.open(fn, std::ios::out);

        // Make sure it opened correctly
        if (!mp_out.good())
          libmesh_file_error(fn);

break;
      }

    default:
      {
libMesh::out << "Unrecognized file access type!" << std::endl;
libmesh_error();
      }
    }





  // Read/Write the file signature
  const int bufLen = 12;
  char buf[bufLen+1];

  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::ENCODE):
      {
char* p = &buf[0];
const LegacyXdrIO::FileFormat orig = this->get_orig_flag();

std::ostringstream name;
if (orig == LegacyXdrIO::DEAL)
name << "DEAL 003:003";

else if (orig == LegacyXdrIO::MGF)
name << "MGF 002:000";

else if (orig == LegacyXdrIO::LIBM)
name << "LIBM " << this->get_num_levels();

else
libmesh_error();

// Fill the buffer
std::sprintf(&buf[0], "%s", name.str().c_str());

xdr_string(mp_xdr_handle, &p, bufLen); // Writes binary signature

break;
      }

    case (XdrMGF::DECODE):
      {
char* p = &buf[0];
xdr_string(mp_xdr_handle, &p, bufLen); // Reads binary signature

// Set the number of levels used in the mesh
this->tokenize_first_line(p);

break;
      }

#endif

    case (XdrMGF::W_ASCII):
      {
const LegacyXdrIO::FileFormat orig = this->get_orig_flag();

if (orig == LegacyXdrIO::DEAL)
std::sprintf(&buf[0], "%s %03d:%03d", "DEAL", 3, 3);

else if (orig == LegacyXdrIO::MGF)
std::sprintf(&buf[0], "%s %03d:%03d", "MGF ", 2, 0);

else if (orig == LegacyXdrIO::LIBM)
std::sprintf(&buf[0], "%s %d", "LIBM", this->get_num_levels());

mp_out << buf << '\n';

break;
      }

    case (XdrMGF::R_ASCII):
      {

#ifdef __HP_aCC
// weirdly, _only_ here aCC
// is not fond of mp_in.getline()
// however, using mp_in.getline()
// further below is ok...
std::string buf_buf;
std::getline (mp_in, buf_buf, '\n');
libmesh_assert_less_equal (buf_buf.size(), bufLen);

buf_buf.copy (buf, std::string::npos);
#else

// Here we first use getline() to grab the very
// first line of the file into a char buffer. Then
// this line is tokenized to look for:
// 1.) The name LIBM, which specifies the new Mesh style.
// 2.) The number of levels in the Mesh which is being read.
// Note that "buf" will be further processed below, here we
// are just attempting to get the number of levels.
mp_in.getline(buf, bufLen+1);

#endif

// Determine the number of levels in this mesh
this->tokenize_first_line(buf);

break;
      }

    default:
      libmesh_error();
    }



  // If you are reading or decoding, process the signature
  if ((m_type == R_ASCII) || (m_type == DECODE))
    {
      char name[5];
      std::strncpy(name, &buf[0], 4);
      name[4] = '\0';

      if (std::strcmp (name, "DEAL") == 0)
{
this->orig_flag = LegacyXdrIO::DEAL; // 0 is the DEAL identifier by definition
}
      else if (std::strcmp (name, "MGF ") == 0)
{
this->orig_flag = LegacyXdrIO::MGF; // 1 is the MGF identifier by definition
}
      else if (std::strcmp (name, "LIBM") == 0)
{
this->orig_flag = LegacyXdrIO::LIBM; // the New and Improved XDA
}

      else
{
libMesh::err <<
            "No originating software can be determined for header string '" <<
            name << "'. Error." << std::endl;
libmesh_error();
}
    }

}



int XdrMGF::dataBlk(int* array, int numvar, int size)
{
  int totalSize = numvar*size;

  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::DECODE):
    case (XdrMGF::ENCODE):
      {
xdr_vector(mp_xdr_handle,
(char *) &array[0],
totalSize,
sizeof(int),
(xdrproc_t) xdr_int);
break;
      }

#endif

    case (XdrMGF::W_ASCII):
      {
for (int i=0; i<size; i++)
{
for (int j=0; j<numvar; j++)
mp_out << array[i*numvar + j] << " ";

mp_out << '\n';
}

mp_out.flush();
break;
      }

    case (XdrMGF::R_ASCII):
      {
libmesh_assert (mp_in.good());

for (int i=0; i<size; i++)
{
for (int j=0; j<numvar; j++)
              {
mp_in >> array[i*numvar + j];
              }

mp_in.ignore(); // Read newline
}

break;
      }

    default:
      // Unknown access type
      libmesh_error();
    }

  return totalSize;
}



int XdrMGF::dataBlk(Real* array, int numvar, int size)
{
  int totalSize = numvar*size;

  // If this function is called by coord(),
  // numvar is the problem dimension, and
  // size is the number of nodes in the problem.

  //libMesh::out << "Total amount of data to be written: " << totalSize << std::endl;

  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::DECODE):
    case (XdrMGF::ENCODE):
      {
// FIXME - this is probably broken for Real == long double
// RHS
xdr_vector(mp_xdr_handle,
(char *) &array[0],
totalSize,
sizeof(Real),
(xdrproc_t) xdr_REAL);
      }

#endif

    case (XdrMGF::W_ASCII):
      {
        // Save stream flags
        std::ios_base::fmtflags out_flags = mp_out.flags();

        // We will use scientific notation with a precision of 16
        // digits in the following output. The desired precision and
        // format will automatically determine the width.
        mp_out << std::scientific
               << std::setprecision(16);

for (int i=0; i<size; i++)
{
for (int j=0; j<numvar; j++)
              mp_out << array[i*numvar + j] << " \t";

mp_out << '\n';
}

        // Restore stream flags
        mp_out.flags(out_flags);

mp_out.flush();
break;
      }

    case (XdrMGF::R_ASCII):
      {
libmesh_assert (mp_in.good());

for (int i=0; i<size; i++)
{
libmesh_assert (mp_in.good());

for (int j=0; j<numvar; j++)
mp_in >> array[i*numvar + j];

mp_in.ignore(); // Read newline
}

break;
      }

    default:
      // Unknown access type
      libmesh_error();
    }

  return totalSize;
}

} // namespace libMesh
Something went wrong with that request. Please try again.