FEniCS: evaluate gradient of function at point

Let u be the solution to a given problem solved using FEniCS, on a function space V

from fenics import *
...
u = Function(V)
solve(a==L, u, bcs)

and x be a Point object. How do I evaluate the gradient of u at x ?


I tried

g = grad(u)
g(x)

which raises an error about dimensions :

UFLException: Expecting dim to match the geometric dimension, got dim=1 and gdim=2.

Projecting the gradient on the function space doesn’t give encouraging results either:

h = project(grad(u), V)

also raises an error

UFLException: Shape mismatch.

Answer

I guess you have dimensions problems, while projecting a function in a scalar function space. you might need to do something like this:

 V_vec = VectorFunctionSpace(mesh, "CG", 1)
 h = project(grad(u),V_vec)

Regards, Leonardo

Leave a Reply

Your email address will not be published. Required fields are marked *