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

Manually Setting Values to a Function

+4 votes

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)
asked Apr 13, 2014 by sixtysymbols FEniCS User (2,280 points)

1 Answer

+3 votes
 
Best answer

Starting from version 1.2 or so, the numbering of the coordinates in the mesh and the DOFs in serial computations do not necessarily coincide anymore; in parallel this was never the case as far as I know. To work around such issues, you can have a look here or here. There is also an undocumented (?) parameter reorder_dofs_serial which you can set to False to obtain the old way of ordering in serial.

answered Apr 13, 2014 by Christian Waluga FEniCS Expert (12,310 points)
selected Apr 13, 2014 by sixtysymbols

Hi,

parameters["reorder_dofs_serial"] = False worked fine. Thanks.

Is this parameter undocumented because it is depreciated/not recommended?

...