ordering of mesh.coordinates() vs. function coefficients

Asked by Christian Clason

Hello,

something seems to have changed in the trunk version of the Python wrapper for dolfin regarding the ordering of the vector returned by mesh.coordinates() or (more likely) that of the vector returned by u.vector()[:] so that these do not agree anymore.

For example, the following code used to work (in trunk, early summer) for defining the function u = x_1, but not in current trunk:

from dolfin import *

mesh = UnitSquare(4,4)
V = FunctionSpace(mesh,'CG',1)
u = Function(V)
x = mesh.coordinates()

u.vector()[:] = x[:,0]
plot(u)
interactive()

Has the interface changed? If so, what can I do to define a CG1 function based on the coordinates of the mesh?

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Christian Clason
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Have look at:

  https://answers.launchpad.net/dolfin/+question/167027

and see if that helps you.

Johan

On 10/16/2012 01:46 PM, Christian Clason wrote:
> New question #211368 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/211368
>
> Hello,
>
> something seems to have changed in the trunk version of the Python wrapper for dolfin regarding the ordering of the vector returned by mesh.coordinates() or (more likely) that of the vector returned by u.vector()[:] so that these do not agree anymore.
>
> For example, the following code used to work (in trunk, early summer) for defining the function u = x_1, but not in current trunk:
>
> from dolfin import *
>
> mesh = UnitSquare(4,4)
> V = FunctionSpace(mesh,'CG',1)
> u = Function(V)
> x = mesh.coordinates()
>
> u.vector()[:] = x[:,0]
> plot(u)
> interactive()
>
> Has the interface changed? If so, what can I do to define a CG1 function based on the coordinates of the mesh?
>

Revision history for this message
Christian Clason (christian-clason) said :
#2

The workaround using interpolate does work (thank you!), but only for Expressions that the compiler understands. For my purposes, I need the equivalent of numpy expressions such as
    (np.abs(x[:,0]-0.5)<0.25)*(np.abs(x[:,1]-0.5)<0.25)
where x is a numpy array of the coordinates of the vertices. It's not clear to me how to use the methods in DofMap.py to get a (unique!) list of the coordinates of the vertices in the same ordering as in a Function.vector().

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

Have you tried:

  Expression("(fabs(x[0]-0.5)<0.25)*(fabs(x[1]-0.5)<0.25)")

Johan

On 10/16/2012 02:56 PM, Christian Clason wrote:
> Question #211368 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/211368
>
> Status: Answered => Open
>
> Christian Clason is still having a problem:
> The workaround using interpolate does work (thank you!), but only for Expressions that the compiler understands. For my purposes, I need the equivalent of numpy expressions such as
> (np.abs(x[:,0]-0.5)<0.25)*(np.abs(x[:,1]-0.5)<0.25)
> where x is a numpy array of the coordinates of the vertices. It's not clear to me how to use the methods in DofMap.py to get a (unique!) list of the coordinates of the vertices in the same ordering as in a Function.vector().
>

Revision history for this message
Christian Clason (christian-clason) said :
#4

I had not -- that works, thank you, and is a cleaner way of defining such functions. Is there any document containing a full list of the operators you can use in an Expression?

For the record, Mikael Mortensen's tip on the linked question is a solution to the original question (getting a list of coordinates in the same ordering as the function, needed for a stem plot of the coefficients):

x = interpolate(Expression("x[0]"), V).vector().array()
y = interpolate(Expression("x[1]"), V).vector().array()

Best,

Christian

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

From the doc string of Expression:

    The expressions may depend on x[0], x[1], and x[2] which carry
    information about the coordinates where the expression is
    evaluated. All math functions defined in <cmath> are available
    to the user.

Johan

On 10/16/2012 06:35 PM, Christian Clason wrote:
> Question #211368 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/211368
>
> Status: Answered => Solved
>
> Christian Clason confirmed that the question is solved:
> I had not -- that works, thank you, and is a cleaner way of defining
> such functions. Is there any document containing a full list of the
> operators you can use in an Expression?
>
> For the record, Mikael Mortensen's tip on the linked question is a
> solution to the original question (getting a list of coordinates in the
> same ordering as the function, needed for a stem plot of the
> coefficients):
>
> x = interpolate(Expression("x[0]"), V).vector().array()
> y = interpolate(Expression("x[1]"), V).vector().array()
>
> Best,
>
> Christian
>