The discontinuous coefficient is not handled well in that tutorial. The implementation relies on the assumption that mesh entities and dofs are enumerated in the same way, an assumption that does not hold in general.
A better solution is to subclass Expression. See full example below.
from dolfin import *
class DiscontinuousCoefficient(Expression):
def __init__(self, cell_function, coeffs):
self.cell_function = cell_function
self.coeffs = coeffs
def value_shape(self):
return (2,2)
def eval_cell(self, values, x, cell):
subdomain_id = self.cell_function[cell.index]
local_coeff = self.coeffs[subdomain_id]
local_coeff.eval_cell(values, x, cell)
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "CG", 1)
bc = DirichletBC(V, 0., "on_boundary")
cf = CellFunction("size_t", mesh)
right = CompiledSubDomain("x[0]>0.5-DOLFIN_EPS")
right.mark(cf, 1)
C_left = Constant(((1.0, 0.0,),
(0.0, 0.1)))
C_right = Constant(((0.1, 0.0,),
(0.0, 1.0)))
C = DiscontinuousCoefficient(cf, [C_left, C_right])
u, v = TrialFunction(V), TestFunction(V)
a = inner(C * grad(u), grad(v)) * dx
L = v * dx
uh = Function(V)
solve(a == L, uh, bc)