In mixed methods, Neumann boundary conditions become essential boundary condition. In other words, you need to impose them to function space "strongly".
from dolfin import *
import numpy
u_0 = Expression('exp(x[1]) + 1')
u_ex = Expression('exp(x[0])exp(x[1]) + x[0]x[1] + 1')
sigma_ex = Expression(('exp(x[0])exp(x[1]) + x[1]', 'exp(x[0])exp(x[1]) + x[0]'))
f = Expression('2exp(x[0])exp(x[1])')
G = Expression(("exp(x[0])exp(x[1]) + x[1]", "exp(x[0])exp(x[1]) + x[0]"))
mesh = UnitSquareMesh(nx,nx)
Mh = FunctionSpace(mesh, "RT", 1)
Vh = FunctionSpace(mesh, "DG", 0)
V = MixedFunctionSpace([Mh, Vh])
n = FacetNormal(mesh)
class Nbdy(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and ((abs(x[1]-1.) < tol) or (abs(x[0]-1.) < tol) or (abs(x[1]) < tol))
bc = DirichletBC(V.sub(0), sigma_ex, Nbdy())
(tau, v) = TestFunctions(V)
(sigma, u) = TrialFunctions(V)
a = inner(sigma, tau)dx + udiv(tau)dx + div(sigma)vdx
L = fvdx + u_0inner(tau, n)*ds
su = Function(V)
solve(a == L, su, bc)
(sigma, u) = su.split()
u_error = errornorm(u_ex, u, norm_type = 'L2')
sigma_error = errornorm(sigma_ex, sigma, norm_type = 'L2')