Hi, as you say
the order of the values is not the same
The reason for this is that w.vector() entries are ordered by dofmap of function space of w, whereas you ordered f2_vector by coordinates. These orderings are not the same and also the number of mesh vertices in general does not equal the number of degrees of freedom. Finally, you might want to vectorize your code.
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(50, 50)
V = FunctionSpace(mesh, "CG", 1)
u = Function(V)
u_vector = u.vector()
dim = V.dim()
N = mesh.geometry().dim()
dof_coordinates = V.dofmap().tabulate_all_coordinates(mesh).reshape(dim, N)
x = dof_coordinates[:, 0]
y = dof_coordinates[:, 1]
# serial approach
def f2_s(x, y, f1):
return 1 if f1(x, y) <= DOLFIN_EPS else 2
def f1_s(x, y):
return sin(4*pi*x)*cos(4*pi*y)
tic()
for i in xrange(dim):
u_vector[i] = f2_s(x[i], y[i], f1_s)
ser = toc(); print "Serial", ser
# vectorized approach
def f2(x, y, f1):
return np.where(f1(x, y) <= DOLFIN_EPS, 1, 2)
def f1(x, y):
return np.sin(4*pi*x)*np.cos(4*pi*y)
tic()
u_vector[:] = f2(x, y, f1)
vec = toc(); print "Vectorized", vec, "Speed up", int(ser/vec)