libMesh::XdrMGF Class Reference

#include <xdr_mgf.h>

Inheritance diagram for libMesh::XdrMGF:

Public Types

enum  XdrIO_TYPE {
  UNKNOWN = -1, ENCODE =0, DECODE, W_ASCII,
  R_ASCII
}
 

Public Member Functions

 XdrMGF ()
 
 XdrMGF ()
 
void init (XdrIO_TYPE t, const char *fn, const char *type, int icnt)
 
virtual ~XdrMGF ()
 
void fini ()
 
int dataBlk (int *array, int numvar, int size)
 
int dataBlk (Real *array, int numvar, int size)
 
LegacyXdrIO::FileFormat get_orig_flag () const
 
void set_orig_flag (LegacyXdrIO::FileFormat in_orig_flag)
 
void set_num_levels (unsigned int num_levels)
 
unsigned int get_num_levels ()
 

Protected Attributes

unsigned int _num_levels
 
XdrIO_TYPE m_type
 
XDR * mp_xdr_handle
 
LegacyXdrIO::FileFormat orig_flag
 
std::ifstream mp_in
 
std::ofstream mp_out
 

Private Member Functions

void tokenize_first_line (const char *p)
 

Private Attributes

std::FILE * mp_fp
 

Detailed Description

This class is taken directly from MGF. It facilitates reading and writing binary solution/mesh files using the xdr binary format, which allows for portable binary files across various platforms. For more information on the xdr format, see the standard C include file rpc/rpc.h.

Author
Bill Barth, Robert McLay.

There are essentially two inheritance trees and six classes: XdrMGF XdrHEAD ^ ^ ^ ^ | | | | XdrMESH XdrSOLN XdrMHEAD XdrSHEAD

XdrHEAD, XdrMHEAD, and XdrSHEAD just read the headers of solution and mesh files.

XdrMGF, XdrMESH, and XdrSOLN handle the "meat" of the files: everything other than the headers.

Definition at line 85 of file xdr_mgf.h.

Member Enumeration Documentation

This enum specifies the access permission which will be acquired for the current xdr file. Note that it is only possible to read (DECODE) or write (ENCODE) but not both. For ASCII type files, use WRITE or READ instead!

Enumerator
UNKNOWN 
ENCODE 
DECODE 
W_ASCII 
R_ASCII 

Definition at line 98 of file xdr_mgf.h.

98  {UNKNOWN = -1, ENCODE=0, DECODE,
99  W_ASCII , R_ASCII};

Constructor & Destructor Documentation

libMesh::XdrMGF::XdrMGF ( )
inline

Constructor. Intializes the access type, xdr file handle, xdr file pointer, and originator flag. Zero is a good default value for the flag, since that is the DEAL identifier. The xdr file handle is a struct defined in the standard C header rpc/rpc.h.

Definition at line 115 of file xdr_mgf.h.

libMesh::XdrMGF::XdrMGF ( )
inline

Definition at line 117 of file xdr_mgf.h.

libMesh::XdrMGF::~XdrMGF ( )
virtual

Destructor. Frees the memory which was allocated to contain several strings.

Definition at line 28 of file xdr_mgf.C.

References fini().

29 {
30  this->fini();
31 }

Member Function Documentation

int libMesh::XdrMGF::dataBlk ( int *  array,
int  numvar,
int  size 
)

Reads/Writes a block of ints to/from the current xdr file/file handle.

Parameters
arrayPointer to data to be read/written
numvarThe total number of variables (size of the array)
sizeThe size of each individual variable in the array

Definition at line 278 of file xdr_mgf.C.

References DECODE, ENCODE, libMesh::libmesh_assert(), m_type, mp_in, mp_out, mp_xdr_handle, R_ASCII, and W_ASCII.

Referenced by libMesh::XdrMESH::BC(), libMesh::XdrMESH::coord(), libMesh::XdrMESH::Icon(), and libMesh::XdrSOLN::values().

279 {
280  int totalSize = numvar*size;
281 
282  switch (m_type)
283  {
284 
285 #ifdef LIBMESH_HAVE_XDR
286 
287  case (XdrMGF::DECODE):
288  case (XdrMGF::ENCODE):
289  {
290  xdr_vector(mp_xdr_handle,
291  (char *) &array[0],
292  totalSize,
293  sizeof(int),
294  (xdrproc_t) xdr_int);
295  break;
296  }
297 
298 #endif
299 
300  case (XdrMGF::W_ASCII):
301  {
302  for (int i=0; i<size; i++)
303  {
304  for (int j=0; j<numvar; j++)
305  mp_out << array[i*numvar + j] << " ";
306 
307  mp_out << '\n';
308  }
309 
310  mp_out.flush();
311  break;
312  }
313 
314  case (XdrMGF::R_ASCII):
315  {
316  libmesh_assert (mp_in.good());
317 
318  for (int i=0; i<size; i++)
319  {
320  for (int j=0; j<numvar; j++)
321  {
322  mp_in >> array[i*numvar + j];
323  }
324 
325  mp_in.ignore(); // Read newline
326  }
327 
328  break;
329  }
330 
331  default:
332  // Unknown access type
333  libmesh_error();
334  }
335 
336  return totalSize;
337 }
int libMesh::XdrMGF::dataBlk ( Real array,
int  numvar,
int  size 
)

Read/Writes a block of Reals to/from the current xdr file/file handle.

Definition at line 341 of file xdr_mgf.C.

References DECODE, ENCODE, libMesh::libmesh_assert(), m_type, mp_in, mp_out, mp_xdr_handle, R_ASCII, libMesh::Real, and W_ASCII.

342 {
343  int totalSize = numvar*size;
344 
345  // If this function is called by coord(),
346  // numvar is the problem dimension, and
347  // size is the number of nodes in the problem.
348 
349  //libMesh::out << "Total amount of data to be written: " << totalSize << std::endl;
350 
351  switch (m_type)
352  {
353 
354 #ifdef LIBMESH_HAVE_XDR
355 
356  case (XdrMGF::DECODE):
357  case (XdrMGF::ENCODE):
358  {
359  // FIXME - this is probably broken for Real == long double
360  // RHS
361  xdr_vector(mp_xdr_handle,
362  (char *) &array[0],
363  totalSize,
364  sizeof(Real),
365  (xdrproc_t) xdr_REAL);
366  }
367 
368 #endif
369 
370  case (XdrMGF::W_ASCII):
371  {
372  // Save stream flags
373  std::ios_base::fmtflags out_flags = mp_out.flags();
374 
375  // We will use scientific notation with a precision of 16
376  // digits in the following output. The desired precision and
377  // format will automatically determine the width.
378  mp_out << std::scientific
379  << std::setprecision(16);
380 
381  for (int i=0; i<size; i++)
382  {
383  for (int j=0; j<numvar; j++)
384  mp_out << array[i*numvar + j] << " \t";
385 
386  mp_out << '\n';
387  }
388 
389  // Restore stream flags
390  mp_out.flags(out_flags);
391 
392  mp_out.flush();
393  break;
394  }
395 
396  case (XdrMGF::R_ASCII):
397  {
398  libmesh_assert (mp_in.good());
399 
400  for (int i=0; i<size; i++)
401  {
402  libmesh_assert (mp_in.good());
403 
404  for (int j=0; j<numvar; j++)
405  mp_in >> array[i*numvar + j];
406 
407  mp_in.ignore(); // Read newline
408  }
409 
410  break;
411  }
412 
413  default:
414  // Unknown access type
415  libmesh_error();
416  }
417 
418  return totalSize;
419 }
void libMesh::XdrMGF::fini ( )

Finalizes operations on the current xdr file handle, and closes the xdr file.

Uses xdr_destroy found in rpc/rpc.h.

Definition at line 35 of file xdr_mgf.C.

References mp_fp, and mp_xdr_handle.

Referenced by init(), and ~XdrMGF().

36 {
37 
38 #ifdef LIBMESH_HAVE_XDR
39 
40  if (mp_xdr_handle)
41  {
42  //libMesh::out << "Destroying XDR file handle." << std::endl;
43  xdr_destroy(mp_xdr_handle);
44  }
45 
46  //libMesh::out << "Deleting the file handle pointer." << std::endl;
47  delete mp_xdr_handle;
48 
49  mp_xdr_handle = NULL;
50 
51 #endif
52 
53  if (mp_fp)
54  {
55  //libMesh::out << "Closing file." << std::endl;
56  std::fflush(mp_fp);
57  std::fclose(mp_fp);
58  }
59 
60  mp_fp = NULL;
61 }
unsigned int libMesh::XdrMGF::get_num_levels ( )
inline

Get number of levels

Definition at line 190 of file xdr_mgf.h.

References _num_levels.

Referenced by libMesh::XdrMESH::header(), init(), and libMesh::LegacyXdrIO::read_mesh().

190 { return _num_levels; }
LegacyXdrIO::FileFormat libMesh::XdrMGF::get_orig_flag ( ) const
inline

Get the originator flag.

Definition at line 174 of file xdr_mgf.h.

References orig_flag.

Referenced by init(), libMesh::LegacyXdrIO::read_mesh(), and libMesh::LegacyXdrIO::write_mesh().

174 { return orig_flag; }
void libMesh::XdrMGF::init ( XdrMGF::XdrIO_TYPE  t,
const char *  fn,
const char *  type,
int  icnt 
)

Initialization of the xdr file. This function performs the following operations: {itemize} Closes the old xdr file if necessary.

Creates a new xdr file name and opens this file.

Opens the appropriate xdr file handle.

Reads/Writes a signature to the file.

{itemize}

Definition at line 68 of file xdr_mgf.C.

References libMesh::LegacyXdrIO::DEAL, DECODE, ENCODE, libMesh::err, fini(), get_num_levels(), get_orig_flag(), libMesh::LegacyXdrIO::LIBM, m_type, libMesh::LegacyXdrIO::MGF, mp_fp, mp_in, mp_out, mp_xdr_handle, libMesh::Quality::name(), orig_flag, libMesh::out, R_ASCII, tokenize_first_line(), and W_ASCII.

Referenced by libMesh::XdrSOLN::init(), and libMesh::XdrMESH::init().

69 {
70  m_type=t;
71 
72  // Close old file if necessary
73  if (mp_fp) this->fini();
74 
75 
76  // Open file
77  switch (m_type)
78  {
79 
80 #ifdef LIBMESH_HAVE_XDR
81 
82  case (XdrMGF::ENCODE):
83  case (XdrMGF::DECODE):
84  {
85  mp_fp = fopen (fn, (m_type == ENCODE) ? "w" : "r");
86 
87  // Make sure the file is ready for use
88  if (!mp_fp)
89  {
90  libMesh::err << "XDR Error: Accessing file: "
91  << fn
92  << " failed."
93  << std::endl;
94  libmesh_error();
95  }
96 
97  // Create the XDR handle
98  mp_xdr_handle = new XDR;
99  xdrstdio_create(mp_xdr_handle,
100  mp_fp,
101  ((m_type == ENCODE) ? XDR_ENCODE : XDR_DECODE));
102 
103  break;
104  }
105 
106 #endif
107 
108  case (XdrMGF::R_ASCII):
109  {
110  mp_in.open(fn, std::ios::in);
111 
112  // Make sure it opened correctly
113  if (!mp_in.good())
114  libmesh_file_error(fn);
115 
116  break;
117  }
118 
119  case (XdrMGF::W_ASCII):
120  {
121  mp_out.open(fn, std::ios::out);
122 
123  // Make sure it opened correctly
124  if (!mp_out.good())
125  libmesh_file_error(fn);
126 
127  break;
128  }
129 
130  default:
131  {
132  libMesh::out << "Unrecognized file access type!" << std::endl;
133  libmesh_error();
134  }
135  }
136 
137 
138 
139 
140 
141  // Read/Write the file signature
142  const int bufLen = 12;
143  char buf[bufLen+1];
144 
145  switch (m_type)
146  {
147 
148 #ifdef LIBMESH_HAVE_XDR
149 
150  case (XdrMGF::ENCODE):
151  {
152  char* p = &buf[0];
153  const LegacyXdrIO::FileFormat orig = this->get_orig_flag();
154 
155  std::ostringstream name;
156  if (orig == LegacyXdrIO::DEAL)
157  name << "DEAL 003:003";
158 
159  else if (orig == LegacyXdrIO::MGF)
160  name << "MGF 002:000";
161 
162  else if (orig == LegacyXdrIO::LIBM)
163  name << "LIBM " << this->get_num_levels();
164 
165  else
166  libmesh_error();
167 
168  // Fill the buffer
169  std::sprintf(&buf[0], "%s", name.str().c_str());
170 
171  xdr_string(mp_xdr_handle, &p, bufLen); // Writes binary signature
172 
173  break;
174  }
175 
176  case (XdrMGF::DECODE):
177  {
178  char* p = &buf[0];
179  xdr_string(mp_xdr_handle, &p, bufLen); // Reads binary signature
180 
181  // Set the number of levels used in the mesh
182  this->tokenize_first_line(p);
183 
184  break;
185  }
186 
187 #endif
188 
189  case (XdrMGF::W_ASCII):
190  {
191  const LegacyXdrIO::FileFormat orig = this->get_orig_flag();
192 
193  if (orig == LegacyXdrIO::DEAL)
194  std::sprintf(&buf[0], "%s %03d:%03d", "DEAL", 3, 3);
195 
196  else if (orig == LegacyXdrIO::MGF)
197  std::sprintf(&buf[0], "%s %03d:%03d", "MGF ", 2, 0);
198 
199  else if (orig == LegacyXdrIO::LIBM)
200  std::sprintf(&buf[0], "%s %d", "LIBM", this->get_num_levels());
201 
202  mp_out << buf << '\n';
203 
204  break;
205  }
206 
207  case (XdrMGF::R_ASCII):
208  {
209 
210 #ifdef __HP_aCC
211  // weirdly, _only_ here aCC
212  // is not fond of mp_in.getline()
213  // however, using mp_in.getline()
214  // further below is ok...
215  std::string buf_buf;
216  std::getline (mp_in, buf_buf, '\n');
217  libmesh_assert_less_equal (buf_buf.size(), bufLen);
218 
219  buf_buf.copy (buf, std::string::npos);
220 #else
221 
222  // Here we first use getline() to grab the very
223  // first line of the file into a char buffer. Then
224  // this line is tokenized to look for:
225  // 1.) The name LIBM, which specifies the new Mesh style.
226  // 2.) The number of levels in the Mesh which is being read.
227  // Note that "buf" will be further processed below, here we
228  // are just attempting to get the number of levels.
229  mp_in.getline(buf, bufLen+1);
230 
231 #endif
232 
233  // Determine the number of levels in this mesh
234  this->tokenize_first_line(buf);
235 
236  break;
237  }
238 
239  default:
240  libmesh_error();
241  }
242 
243 
244 
245  // If you are reading or decoding, process the signature
246  if ((m_type == R_ASCII) || (m_type == DECODE))
247  {
248  char name[5];
249  std::strncpy(name, &buf[0], 4);
250  name[4] = '\0';
251 
252  if (std::strcmp (name, "DEAL") == 0)
253  {
254  this->orig_flag = LegacyXdrIO::DEAL; // 0 is the DEAL identifier by definition
255  }
256  else if (std::strcmp (name, "MGF ") == 0)
257  {
258  this->orig_flag = LegacyXdrIO::MGF; // 1 is the MGF identifier by definition
259  }
260  else if (std::strcmp (name, "LIBM") == 0)
261  {
262  this->orig_flag = LegacyXdrIO::LIBM; // the New and Improved XDA
263  }
264 
265  else
266  {
267  libMesh::err <<
268  "No originating software can be determined for header string '" <<
269  name << "'. Error." << std::endl;
270  libmesh_error();
271  }
272  }
273 
274 }
void libMesh::XdrMGF::set_num_levels ( unsigned int  num_levels)
inline

Set number of levels

Definition at line 185 of file xdr_mgf.h.

References _num_levels.

Referenced by libMesh::LegacyXdrIO::write_mesh().

185 { _num_levels = num_levels; }
void libMesh::XdrMGF::set_orig_flag ( LegacyXdrIO::FileFormat  in_orig_flag)
inline

Set the originator flag.

Definition at line 179 of file xdr_mgf.h.

References orig_flag.

Referenced by libMesh::LegacyXdrIO::read_mesh(), and libMesh::LegacyXdrIO::write_mesh().

179 { orig_flag = in_orig_flag; }
void libMesh::XdrMGF::tokenize_first_line ( const char *  p)
inlineprivate

This function allows us to set the number of levels in the mesh when reading.

Definition at line 258 of file xdr_mgf.h.

References _num_levels.

Referenced by init().

259  {
260  std::string buf_str(p);
261  std::stringstream ss(buf_str);
262 
263  char token[256];
264  ss >> token;
265  if(std::strcmp(token,"LIBM") == 0)
266  {
267  ss >> token;
268  _num_levels = std::atoi(token);
269  }
270 
271  }

Member Data Documentation

unsigned int libMesh::XdrMGF::_num_levels
protected

Number of levels of refinement in the mesh

Definition at line 197 of file xdr_mgf.h.

Referenced by get_num_levels(), set_num_levels(), and tokenize_first_line().

XdrIO_TYPE libMesh::XdrMGF::m_type
protected

Specifies the read/write permission for the current xdr file. Possibilities are: {itemize} UNKNOWN = -1 ENCODE = 0 DECODE = 1 {itemize}

Definition at line 210 of file xdr_mgf.h.

Referenced by dataBlk(), libMesh::XdrSOLN::header(), libMesh::XdrMESH::header(), and init().

std::FILE* libMesh::XdrMGF::mp_fp
private

Definition at line 252 of file xdr_mgf.h.

Referenced by fini(), and init().

std::ifstream libMesh::XdrMGF::mp_in
protected

An input file stream object

Definition at line 244 of file xdr_mgf.h.

Referenced by dataBlk(), libMesh::XdrSOLN::header(), libMesh::XdrMESH::header(), and init().

std::ofstream libMesh::XdrMGF::mp_out
protected

An output file stream object.

Definition at line 249 of file xdr_mgf.h.

Referenced by dataBlk(), libMesh::XdrSOLN::header(), libMesh::XdrMESH::header(), and init().

XDR* libMesh::XdrMGF::mp_xdr_handle
protected

Pointer to the standard {xdr} struct. See the standard header file rpc/rpc.h for more information.

Definition at line 220 of file xdr_mgf.h.

Referenced by dataBlk(), fini(), libMesh::XdrSOLN::header(), libMesh::XdrMESH::header(), and init().

LegacyXdrIO::FileFormat libMesh::XdrMGF::orig_flag
protected

Flag indicating how much checking we need to do. We can read in mgf meshes more quickly because there is only one type of element in these meshes. Deal meshes on the other hand will require a check for each element to find out what type it is. Possible values are: {itemize} 0: It's an DEAL style mesh 1: It's a MGF style mesh {itemize}

Definition at line 239 of file xdr_mgf.h.

Referenced by get_orig_flag(), libMesh::XdrMESH::header(), init(), and set_orig_flag().


The documentation for this class was generated from the following files:

Site Created By: libMesh Developers
Last modified: February 07 2014 16:58:02 UTC

Hosted By:
SourceForge.net Logo