Hi,
The FEniCS fundamentals document gives the following example of how to manipulate a Function on an FE space with degree 1 Lagrange elements. In the example, the function is divided by the max value at all points
u_nodal_values = u.vector()
u_array = u_nodal_values.array()
max_u = u_array.max()
u_array /= max_u
u.vector()[:] = u_array
u.vector().set_local(u_array) # alternative
print u.vector().array()
When I try to use a similar method in the following example, however, it doesn't work. The ordering seems to get mixed up. Is there a simple way to assign values to the vertices of a FE Function?
from dolfin import *
#3D FE space
mesh = UnitCubeMesh(20,20,20)
V = FunctionSpace(mesh,"Lagrange",1)
#solve 3D poisson equation on cube
def u0_boundary(x,on_boundary):
return on_boundary
bc = DirichletBC(V,Constant(0.0) , u0_boundary)
u = TestFunction(V)
v = TrialFunction(V)
a = inner(grad(u),grad(v))*dx
L = Constant("1.0")*v*dx
u = Function(V)
solve(a==L,u,bc)
#Assign slice at z=.5 to 2D function
#2D FE space w/ function
mesh2 = UnitSquareMesh(20,20)
W = FunctionSpace(mesh2,"Lagrange",1)
w = Function(W)
coor = mesh2.coordinates()
w_array = w.vector().array()
if mesh2.num_vertices() == len(w_array):
for i in range(mesh2.num_vertices()):
w_array[i] = u(coor[i][0],coor[i][1],.5)
w.vector().set_local(w_array)
plot(w,interactive=True)