Given that you don't want to use Expression
and the explanation of your problem is rather cryptic, here's a simple implementation of formulating a diffusion coefficient by projecting a piecewise function onto a FE function space.
from dolfin import *
mesh = UnitSquareMesh(32, 32)
# Set up left and right side subdomains
left_side = AutoSubDomain(lambda x, on_exterior: x[0] <= 0.5)
right_side = AutoSubDomain(lambda x, on_exterior: x[0] >= 0.5)
# Assign a CellFunction we'll use to construct the volume integration element
# Label the left 1 and the right 2
cf = CellFunction('size_t', mesh, 0)
left_side.mark(cf, 1)
right_side.mark(cf, 2)
dx = Measure('dx', domain=mesh, subdomain_data=cf)
# Diffusion coefficient space
V_material = FunctionSpace(mesh, 'DG', 0)
D_trial, D_test = TrialFunction(V_material), TestFunction(V_material)
D = Function(V_material) # The diffusion coefficient
# Initially we project constant values
D_left = Constant(1.0)
D_right = Constant(10.0)
a = D_trial*D_test*dx
L = D_left*D_test*dx(1) + D_right*D_test*dx(2)
solve(a == L, D)
plot(D, interactive=True)
# Now we can project expressions
D_left = Expression('x[0] + 1.0')
D_right = Expression('2.0 - x[0]')
a = D_trial*D_test*dx
L = D_left*D_test*dx(1) + D_right*D_test*dx(2)
solve(a == L, D)
plot(D, interactive=True)
# Solve Poisson equation with the diffusion coefficient, D
V = FunctionSpace(mesh, 'CG', 1)
u, v = TrialFunction(V), TestFunction(V)
a = dot(D*grad(u), grad(v))*dx
L = Constant(1.0)*v*dx
soln = Function(V)
solve(a == L, soln, [DirichletBC(V, Constant(0.0), 'on_boundary')])
plot(soln, interactive=True)
If you don't need something this complicated, you could simply use:
from dolfin import *
mesh = UnitSquareMesh(32, 32)
# Set up left and right side subdomains
left_side = AutoSubDomain(lambda x, on_exterior: x[0] <= 0.5)
right_side = AutoSubDomain(lambda x, on_exterior: x[0] >= 0.5)
# Assign a CellFunction we'll use to construct the volume integration element
# Label the left 1 and the right 2
cf = CellFunction('size_t', mesh, 0)
left_side.mark(cf, 1)
right_side.mark(cf, 2)
dx = Measure('dx', domain=mesh, subdomain_data=cf)
D_left = Expression('x[0] + 1.0')
D_right = Expression('2.0 - x[0]')
V = FunctionSpace(mesh, 'CG', 1)
u, v = TrialFunction(V), TestFunction(V)
a = dot(D_left*grad(u), grad(v))*dx(1) + dot(D_right*grad(u), grad(v))*dx(2)
L = Constant(1.0)*v*dx
soln = Function(V)
solve(a == L, soln, [DirichletBC(V, Constant(0.0), 'on_boundary')])
plot(soln, interactive=True)