How to calculate the norm of grad(d)?

Dear all,

In my fenics code, I need to calculate \frac{\nabla d}{|| \nabla d ||}. Here d is a Function, as shown below:

from dolfin import *
V_d = FunctionSpace(mesh, "CG", 1) 
d = Function(V_d, name = "d")
norm_of_grad_d = ???????
object_1 = grad(d)/norm_of_grad_d

I used norm(grad(d)) before, but I got the error “TypeError: Do not know how to compute norm of grad(d)”. I checked the definition of norm() and I think its argument can only be Vector or Function, so this error occurred.

So does anyone know how to calculate the norm of grad(d) correctly? I appreciate any help.

Just think of the definition of each component of the gradient of a scalar (I know you know how to do that, because I’ve explained it you in the past), the norm of a vector, and then write your own def norm(d) function.

1 Like

Ok. Got it. I think it is:

def norm_new(x):
    return sqrt(x[0]**2+x[1]**2)

Thank you very much.