Function from MeshFunction

Is it possibile to get a Function out of a MeshFunction?
For ex. something like:

from dolfin import *                                                             

mesh = UnitSquareMesh(5,5)

V = FunctionSpace(mesh, "Lagrange", 1)

# mark mesh with boundary function
class left(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[0])<DOLFIN_EPS
left = left()
tcond = MeshFunction("size_t", mesh, 1)
tcond.set_all(0)
left.mark(tcond, 1)

u = Function(V)

u = interpolate(tcond, V) # gives error

px, py = 0.5, 0.5
print(u([px,py]))
1 Like

Try the following:

from dolfin import *

mesh = UnitSquareMesh(5,5)
V = FunctionSpace(mesh, "Lagrange", 1)

# mark mesh with boundary function
class left(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[0])<DOLFIN_EPS
left = left()

tcond = MeshFunction("size_t", mesh, 0)
tcond.set_all(0)
left.mark(tcond, 1)
v2d = vertex_to_dof_map(V)
u = Function(V)
u.vector()[v2d] = tcond.array()

px, py = 0.5, 0.5

import matplotlib.pyplot as plt
plot(mesh)
plot(u)
plt.show()
print(u([px,py]))
2 Likes

thanks! that worked great