assign to coefficients vector of function

Asked by Martin Eigel

Hi,

How can I access the non-const coefficient vector of a Function? This does not seem to work

mesh=UnitSquare(3,3)
V=FunctionSpace(mesh,'CG',1)
F=Function(V)
F.vector().array()[:]=numpy.ones(16)

F.vector() still is zero. I think it is suggested like this in the tutorial. Am I missing something?

Besides, how is Function.compute_vertex_values supposed to be used? This

v=numpy.array(16,dtype='d')
f.compute_vertex_values(a,mesh)

just bombs out.

Cheers, Martin

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Eigel
Solved:
Last query:
Last reply:
Revision history for this message
Martin Sandve Alnæs (martinal) said :
#1

On 4 January 2012 11:41, Martin Eigel
<email address hidden> wrote:
> New question #183699 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/183699
>
> Hi,
>
> How can I access the non-const coefficient vector of a Function? This does not seem to work
>
> mesh=UnitSquare(3,3)
> V=FunctionSpace(mesh,'CG',1)
> F=Function(V)
> F.vector().array()[:]=numpy.ones(16)
>
> F.vector() still is zero. I think it is suggested like this in the tutorial. Am I missing something?

I think .array() returns a copy. Try dropping '.array()' from the above.

> Besides, how is Function.compute_vertex_values supposed to be used? This
>
> v=numpy.array(16,dtype='d')
> f.compute_vertex_values(a,mesh)
>
> just bombs out.

I don't know what 'bombs out' mean, but are you sure it should be 'a'
there and not 'v'?

Martin

> Cheers, Martin
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

Revision history for this message
Johan Hake (johan-hake) said :
#2

On Wednesday January 4 2012 11:41:12 Martin Eigel wrote:
> New question #183699 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/183699
>
> Hi,
>
> How can I access the non-const coefficient vector of a Function? This does
> not seem to work
>
> mesh=UnitSquare(3,3)
> V=FunctionSpace(mesh,'CG',1)
> F=Function(V)
> F.vector().array()[:]=numpy.ones(16)

It should be:

  F.vector()[:]=numpy.ones(16)

The array returned by .array() is a copy of the values from vector().

> F.vector() still is zero. I think it is suggested like this in the
> tutorial.

Are you sure? Then the tutorial is wrong.

> Am I missing something?
>
> Besides, how is Function.compute_vertex_values supposed to be used? This
>
> v=numpy.array(16,dtype='d')
> f.compute_vertex_values(a,mesh)

Provide an array with the correct size. Change above to:

  v=numpy.zeros(16,dtype='d')

> just bombs out.

Which it shouldn't. Will fix!

Johan

> Cheers, Martin

Revision history for this message
Martin Eigel (meigel) said :
#3

Thanks all for the quick answers!
Yes, it should have been 'a' (or 'v') in both cases in the second question.

Problem solved.
Martin

(in my print out of the tutorial, it's just before section 1.1.5 p. 12: "We can equally well insert the entries of u_array into u's numpy array...")

Revision history for this message
Martin Eigel (meigel) said :
#4

This
v=numpy.zeros(16,dtype='d')
f.compute_vertex_values(v,mesh)
actually works. My bad with the incorrect usage of numpy.array...

Martin

Revision history for this message
Kristian B. Ølgaard (k.b.oelgaard) said :
#5

On 4 January 2012 12:10, Martin Eigel
<email address hidden> wrote:
> Question #183699 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/183699
>
>    Status: Answered => Solved
>
> Martin Eigel confirmed that the question is solved:
> Thanks all for the quick answers!
> Yes, it should have been 'a' (or 'v') in both cases in the second question.
>
> Problem solved.
> Martin
>
> (in my print out of the tutorial, it's just before section 1.1.5 p. 12:
> "We can equally well insert the entries of u_array into u's numpy
> array...")

Try the tutorial from the FEniCS book, http://fenicsproject.org/book/

> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

Revision history for this message
Martin Eigel (meigel) said :
#6

> Try the tutorial from the FEniCS book, http://fenicsproject.org/book/

Ok, this apparently has changed and the current version of the tutorial states it correctly.

Revision history for this message
Johan Hake (johan-hake) said :
#7

On Wednesday January 4 2012 13:30:51 Kristian B. Ølgaard wrote:
> Question #183699 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/183699
>
> Kristian B. Ølgaard posted a new comment:
> On 4 January 2012 12:10, Martin Eigel
>
> <email address hidden> wrote:
> > Question #183699 on DOLFIN changed:
> > https://answers.launchpad.net/dolfin/+question/183699
> >
> > Status: Answered => Solved
> >
> > Martin Eigel confirmed that the question is solved:
> > Thanks all for the quick answers!
> > Yes, it should have been 'a' (or 'v') in both cases in the second
> > question.
> >
> > Problem solved.
> > Martin
> >
> > (in my print out of the tutorial, it's just before section 1.1.5 p. 12:
> > "We can equally well insert the entries of u_array into u's numpy
> > array...")
>
> Try the tutorial from the FEniCS book, http://fenicsproject.org/book/

Exactly!

Here the text has also been updated:

   A call like u.vector().array() returns a copy of the data in
   u.vector(). One must therefore never perform assignments like
   u.vector.array()[:] = ..., but instead extract the numpy array
   (that is, a copy), manipulate it, and insert it back with
   u.vector()[:] = or u.set_local(...).

Johan

> > --
> > You received this question notification because you are a member of
> > DOLFIN Team, which is an answer contact for DOLFIN.

Revision history for this message
Johan Hake (johan-hake) said :
#8

> v=numpy.array(16,dtype='d')
> f.compute_vertex_values(a,mesh)
>
> just bombs out.

It no longer bombs :)

Johan