[petsc-users] access da local vector data array in petsc4py

Lisandro Dalcin dalcinl at gmail.com
Tue Oct 12 06:05:02 CDT 2010


On 12 October 2010 05:21, Amal Alghamdi <amal.ghamdi at kaust.edu.sa> wrote:
> Dear All,
> I have a question regarding petsc4py. I'd like to access the data array in a
> local vector generated from a one-dimension da, which has dof = 1. I used  q
> = localVec.getArray(). However, q shape now does not take into account dof
> which is one in my example. I always need to reshape q to match the intended
> dof. Is there a way to do that automatically?
> Thank you very much.
> Amal

Almost all functions and methods in petsc4py treat arrays as linear
data, I mean, unidimensional arrays. As Vec.getArray() knows nothing
about the DA, there is no way to get the DA shape. Note that recently
I've added DA.getVecArray() to provide some support for global
indexing, this new thing is not finished, I want to improve
interoperability with regular numpy arrays, the functionality you want
could be added there.

Other possibility would be to add a method to Vec, let say
Vec.getArrayBlock(), that return an array reshaped to (n/bs, bs) where
"n" is the local vec size and "bs" is the block size (should equal
ndof for Vec's originating from DA). What do you think?

In the mean time, you could use an utility function like the one below:

def get_array_block(vec):
    n = vec.getLocalSize()
    bs = vec.getBlockSize()
    a = numpy.asarray(vec)
    a.shape = (n//bs, bs)
    return a


-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169


More information about the petsc-users mailing list