Convert ufl.algebra object to function for errornorm

Hello!
I have a function defined as

x = SpatialCoordinate(mesh)
u_D = -((pow(x[0],2)-pow(x[1],2))*x[2])/12

and I want to plug it into errornorm like this

errornorm(u_D, u, 'H1')

where u is the solution to some PDE.
Unfortunately, as it stands, u_D is an object of type ufl.algebra.Division, and errornorm complains that

AttributeError: 'Division' object has no attribute 'ufl_element'

so I am trying to convert this u_D into anything that can be used into errornorm.
I tried assembling the error manually but I get numerical instabilities; I know I can use Expression but I was advised to avoid it.

Is there any way I can convert my u_D in something I can use in errornorm?

I have no idea why anyone would advise you to avoid Expression

Interpolate or project u_D onto an appropriate function space.

Thanks a lot for the reply!

Let’s leave aside the question of whether to Expression or not to Expression.

I tried projecting with

u_D = project(u_D, FunctionSpace(mesh,FiniteElement("CG", mesh.ufl_cell(), 1)))

but I always get the AttributeError above, then I tried interpolating with

u_e = Function(FunctionSpace(mesh,FiniteElement("CG", mesh.ufl_cell(), 1)))
u_e.interpolate(u_D)

but I got

AttributeError: 'Division' object has no attribute '_cpp_object'

Is this what you meant? What’s the correct way of doing it?

Hi, what FEniCS version do you run and what is the code that raises the attribute error? The following works on 2017.2.0, 2018.1.0.

from dolfin import *

mesh = UnitCubeMesh(4, 4, 4)

x = SpatialCoordinate(mesh)
u_D = -((pow(x[0],2)-pow(x[1],2))*x[2])/12

u_D = project(u_D, FunctionSpace(mesh,FiniteElement("CG", mesh.ufl_cell(), 1)))

I’m using 2019.1.0.
Now, as it’s customary in computer science, I pasted your code and it worked. Then I tried again the code I posted above, just the way it was, and now it worked. This totally eludes my comprehension XD

Now that I looked silly enough, I can go back to it.
I saw UMFPACK running out of memory at some point so it’s gonna be a fun day.

Thank you very much for your help, both! I would mark both your messages as “Solution” but I can only mark one, apparently.