This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Assign expression to function

+2 votes

Hi,

Is it possible to assign an expression to a Function defined on a FunctionSpace?

E.g. With the following

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "CG", 1)
u = Function(V)

f = Expression("x[0]+x[1]")

Is there a way to assign u the expression f, while still keeping u a Function()?

asked Jun 11, 2014 by sixtysymbols FEniCS User (2,280 points)
edited Jun 11, 2014 by sixtysymbols

1 Answer

+1 vote
 
Best answer

You can interpolate it:

u.interpolate(f)
answered Jun 11, 2014 by johanhake FEniCS Expert (22,480 points)
selected Jun 12, 2014 by sixtysymbols
print  u.interpolate(f).vector().array()
AttributeError: 'NoneType' object has no attribute 'vector'

Am I doing something wrong ?

Yes, try:

u.interpolate(f)
u.vector().array() 
...