dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #14403
Re: Using the vector().get() function in python
On Monday 13 July 2009 21:08:12 phil marinier wrote:
> When I try to use Function.vector().get() in python it tells me that it
> needs 2 arguments and only 1 is given. It also tells me it expects a numpy
> double array. When I pass it a numpy array Function.vector().get(array([]))
> I get:
>
> --------------------------------------------------------------------------
> MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD
> with errorcode 59.
>
> NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes.
> You may or may not see output from other processes, depending on
> exactly when Open MPI kills them.
> --------------------------------------------------------------------------
>
> It doesn't matter if I pass an empty array, an array of zeros() or an array
> with values I always get this same error. How can I use get() in python?
>
> Thank you for your help
Hello Phil!
Get is a somewhat low level interface that map the c++ function Vector::get()
It demands an already initialized c-array. In python you need to pass an
array of the same size as the vector to use this function.
mesh = UnitSquare(2,2)
V = FunctionSpace(mesh,"CG",1)
f = Function(V)
a = ones(9)
f.vector().get(a)
works for me. Now a is filled with zeros.
I think we will keep this function as a way to map the c++ interface. However,
it is notoriously unsafe (as for now). We plan to add some checks, with
sensible error messages.
You need to send an array of the same size as the Vector. If you send a
smaller, it will segfault, and if you send a larger it will fill the values
up to the size of the Vector.
An other way to access the data from python is to use:
f.vector().array()
or if you are using MTL4 or uBLAS backend
f.vector().data()
where the latter returns an numpy.array with a pointer to the original data in
the Vector. The former will return a copy. You can also use the slice
interface:
f.vector()[:]
Hope this helps!
Johan
References