[petsc-users] how to get full trajectory from TS into petsc binary

Jed Brown jed at jedbrown.org
Tue Mar 1 08:05:30 CST 2016


Ed Bueler <elbueler at alaska.edu> writes:

> Barry --
>
> I am reading the resulting file successfully using
>
> import struct
> import sys
> f = open('timesteps','r')
> while True:
>     try:
>         bytes = f.read(8)
>     except:
>         print "f.read() failed"
>         sys.exit(1)

It seems odd to catch the exception and convert to sys.exit(1).  This
just loses the stack trace (which might have more detailed information)
and makes the debugger catch the wrong location.

>     if len(bytes) > 0:
>         print struct.unpack('>d',bytes)[0]
>     else:
>         break
> f.close()
>
> However, was there a more elegant intended method?  

I would use numpy.fromfile, which is one line.

> I am disturbed by the apparent need to specify big-endian-ness (=
> '>d') in the struct.unpack() call.

You need it because PETSc binary files are big-endian.  There is no
place to encode the byte order in these raw binary files, so some
convention is required for the file to portable.

HDF5 includes this information, though it is an annoying dependency.
Numpy files (*.npy) are a simpler alternative that PETSc could decide to
support.

https://docs.scipy.org/doc/numpy/neps/npy-format.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20160301/b2e75d43/attachment.pgp>


More information about the petsc-users mailing list