Weird error when differentiating TrialFunction

The relevant code-snippets I use is

def normal_grad(f,n):
return f.Diff(x)*n[0] + f.Diff(y)*n[1]

Vh = H1(mesh, order=order, dirichlet=[1,2,3,4],flags={“dgjumps”: True})
v = Vh.TestFunction()

nF = specialcf.normal(2)

vn = normal_grad(v,n)
vno = normal_grad(v.Other(),nF)

vnno = normal_grad(vno,nF)
vnn = normal_grad(vn,nF)

and I get error

NgException Traceback (most recent call last)
in
179 jump_u_2 = normal_grad(un,nF) - normal_grad(uno,nF)
180 vnno = normal_grad(vno,nF)
→ 181 vnn = normal_grad(vn,nF)
182 jump_v_2 = vnn-vnno
183

in normal_grad(f, n)
1 def normal_grad(f,n):
----> 2 return f.Diff(x)*n[0] + f.Diff(y)*n[1]
3

NgException: Deriv not implemented for CF N5ngfem23NormCoefficientFunctionE

Has anyone stumbled upon this error before? I find it weird since I can do the same thing for a TestFunction, and it works. I am trying to implement higher order directional derivatives.

Best regards,

Henrik

Hi Henrik,

I needed to change “vn = normal_grad(v,n)” to “vn = normal_grad(v,nF)” to get your code-snippet running, but I did not get any further error. Do you have the latest NGSolve version?

To compute the normal derivative of a H1 test/trial function u you have to write

grad(u)*n

as Diff(x) is used for differentiating CoefficientFunctions depending explicitly on x. Using u.Diff(x) returns 0.

Best
Michael

Thank you so much! Did not spot that error. That solved it.

Small addition:
With the above correction you will only get first (or with “hesse”) second order normal derivatives.
In ngsxfem we have a DifferentialOperator “dn” for higher order normal derivatives implemented with limited scope of application. However, perhaps you want to have a look at the “dnjump” use case in the example
https://github.com/ngsxfem/ngsxfem/blob/release/py_tutorials/fictdom_ghostpen.py

Best,
Christoph

Thank you very much Christoph!

Henrik