Issue with function 'errornorm': object has no attribute 'ufl_element'

Hello,

I was trying to run a simple tutorial script:

from dolfin import *
mesh = UnitSquareMesh(32, 32)
(x, y) = SpatialCoordinate(mesh)
element = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, element)
u = Function(V)
v = TestFunction(V)
f = Constant(-6.0)
g = 1 + x**2 + 2*y**2
bc = DirichletBC(V, g, DomainBoundary())
F = inner(grad(u), grad(v))*dx - f*v*dx
solve(F == 0, u, bc)
print(errornorm(g,u, norm_type = "l2"))

using Python 3 and Docker. However, I get the following error message:

Traceback (most recent call last):
  File "01.py", line 15, in <module>
    print(errornorm(g, u, 'L2'))
  File "/usr/local/lib/python3.6/dist-packages/dolfin/fem/norms.py", line 246, in errornorm
    degree_u = u.ufl_element().degree()
AttributeError: 'Sum' object has no attribute 'ufl_element'

Since I could not find anything similar online I decided to make this post. Could someone help me out?

Hi, the issue is due to the fact that the first argument to the errornorm must be an object that can be interpolated, e.g. Expression type (see below). In your code g is an UFL-expression, which (sadly) does not have this property.

from dolfin import *


mesh = UnitSquareMesh(32, 32)

element = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, element)
u = Function(V)
v = TestFunction(V)
f = Constant(-6.0)
g = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)
bc = DirichletBC(V, g, DomainBoundary())
F = inner(grad(u), grad(v))*dx - f*v*dx
solve(F == 0, u, bc)
print(errornorm(g,u, norm_type = "l2"))

Thanks a lot! This works.