Does something like this fit your needs? This is not a particularly efficient method if you have a large problem.
from dolfin import *
class MyExpression(Expression):
def __init__(self, F):
self.F = F
def eval(self, val, x):
f_val = F(x)
val[0] = 10.0 if f_val > 0.5 else 1.0
val[1] = 10.0 if f_val > 0.5 else 1.0
def value_shape(self):
return (2,)
mesh = UnitSquareMesh(64, 64)
V = FunctionSpace(mesh, 'CG', 1)
u, v = TrialFunction(V), TestFunction(V)
F = interpolate(Expression('x[0]'), V)
my_exp = MyExpression(F)
a = dot(grad(u), grad(v))*dx
L = dot(my_exp, my_exp)*v*dx
soln = Function(V)
bc = DirichletBC(V, Constant(0.0), 'on_boundary')
solve(a == L, soln, bcs=[bc])
plot(soln, interactive=True)