Search code examples
pythongradientpost-processingfenics

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.

Solution

  • 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