### gradient of Expression results in UFLException

194

views

0

Using 2017.1 and the following throws an exception. This used to work with older versions. Anything wrong with this?

```
P1 = FiniteElement("Lagrange", triangle, 1)
f = Expression("x[0]", degree=2, element=P1)
mesh = UnitSquareMesh(5,5)
V2 = VectorFunctionSpace(mesh, "CG", 1)
project(grad(f), V2) # UFLException: Invalid cell None.
v = TrialFunction(V2)
assemble(inner(grad(f),v)*dx) # UFLException: Invalid cell None.
```

Community: FEniCS Project

### 1 Answer

1

Interpolating the expression to the mesh solves the issue...

Or if you prefer an exact approach, investigate here:

https://fenicsproject.org/qa/9175/possible-differentiate-expression-respect-user_parameter

```
from fenics import *
mesh = UnitSquareMesh(5,5)
V = VectorFunctionSpace(mesh, 'CG', 1)
P1 = FunctionSpace(mesh, 'CG', 1)
v = TrialFunction(V)
f = interpolate(Expression("pow(x[0],2)", element=P1._ufl_element), P1)
project(grad(f), V)
assemble(inner(grad(f),v)*dx)
```

Thanks for this workaround. Still, also without the interpolation it should be valid code.

written
6 months ago by
meigel

I prefer the code the way it is; it forces users to understand that mathematical expressions must first be interpolated onto the grid prior to computing numerical derivatives. Without this, people might think UFL computes exact derivatives using this syntax, which it does not.

written
6 months ago by
pf4d

Please login to add an answer/comment or follow this question.