[petsc-users] reading petsc binary vec in python.

Ethan Coon ecoon at lanl.gov
Mon Jul 18 17:37:43 CDT 2011


On Mon, 2011-07-18 at 16:56 -0500, Ataollah Mesgarnejad wrote:
> Dear all,
> 
> I create a petsc binary output using VecView from a structured da grid. How can I read it in python if I know domains size?
> 

This is currently a work in progress, and this shouldn't be assumed to
be the best way in a few days, but the following works now.  It's still
a little buggy for matrices, but will get fixed soon.   

If you're using petsc-dev, you'll want to:

In [1]: import sys, os
In [2]: sys.path.append(os.path.join(os.environ['PETSC_DIR'],'bin','python'))
In [3]: import PetscBinaryRead

If you're using a release version of petsc, download the following
module to a handy place ($PETSC_DIR/bin/python is where this goes in
petsc-dev). 

http://petsc.cs.iit.edu/petsc/petsc-dev/raw-file/tip/bin/python/PetscBinaryRead.py

Add it to your path and import it.

Then, a simple:

In [4]: petsc_objs = PetscBinaryRead.readBinaryFile('myfile.dat')

petsc_objs will now be an N-length list of 2-tuples.  N indicates the
number of PETSc objects that were "View"ed into the binary file, and
each 2-tuple consists of a string to identify what it is ('Vec', 'Mat',
or 'IS')  and a numpy representation of the data.

You then can reshape the array to any shape you want:

In [5]: da_vec = petsc_objs[0][1].reshape((NZ, NY, NX, NDOFS))

Note the order!  

Ethan




> Best,
> Ata
> 

-- 
------------------------------------
Ethan Coon
Post-Doctoral Researcher
Applied Mathematics - T-5
Los Alamos National Laboratory
505-665-8289

http://www.ldeo.columbia.edu/~ecoon/
------------------------------------



More information about the petsc-users mailing list