Dear all,
if we have a Function u and we want to calculate \frac{\partial{u}}{\partial{x}}, we use u.dx(0). Now I need to calculate the derivative of Function u with respect to a direction, I showed this direction in the following figure:
Does anyone know how to do this in Fenics?
Thank you very much in advance.
I can interpret from your figure that you want to compute the gradient in the normal direction at a facet.
What you would then do is
n = ufl.FacetNormal(mesh)
dudn = ufl.dot(ufl.grad(u), n)
This can then in turn be used in variational formulations with the ds or dS integral measure.
If you want another direction, simply replace n with ufl.as_vector((x_dir, y_dir, z_dir)) where *_dir should be replaced by the direction you would like.
As you haven’t given any futher context, as to what you would use this for, or in which version of FEniCS you need this (legacy dolfin/dolfinx, and which version on said software), I will not add any further details.
Thank you for your kind help, Dokken. I am using the legacy dolfin on ubuntu. I think my version is the latest legacy dolfin since I installed it last year (I don’t know how to check the version).
I use fenics to study the adhesive interface in solid mechanics. My whole code is long. To illustrate my question, I think I need to simplify my variational form as much as possible. This is the simplified version of my variational form : \int_{\Omega} (......+\frac{\partial w}{\partial x_n}\frac{\partial v}{\partial x_n})dV. The w is a vector function. Its physical meaning is displacement jump, and v is its test function, as shown by this:
V = VectorFunctionSpace(mesh, "CG", 1)
w = Function(V)
v = TestFunction(V)
The mesh that I use is shown in the following figure:
The x_n in the variational form is the normal direction of the adhesive interface (as shown in the figure), and the adhesive interface is the line connecting the left-bottom point and the right-top point.
The length and width of this mesh are both 1, so the unit vector in the x_n direction is (-\frac{1}{\sqrt2}, \frac{1}{\sqrt2}). If I understand your reply correctly, I should write my code as:
from dolfin import *
V = VectorFunctionSpace(mesh, "CG", 1)
w = Function(V)
v = TestFunction(V)
#n is the unit vector in the direction of x_n.
n=as_vector([-1/sqrt(2),1/sqrt(2)])
dwdn=dot(grad(w), n)
dvdn=dot(grad(v), n)
#or
#dwdn=dot(nabla_grad(w), n)
#dvdn=dot(nabla_grad(v), n)
variational_form = ( ...... + dot(dwdn, dvdn) )*dx
But I am not sure if I should use “grad” or “nabla_grad” here. Could you please tell me if my code is correct? Thank you very much.
Dear Dokken,
I tested my code shown above. The result is correct. But it seems that when I included dwdn=dot(nabla_grad(w), n) and dvdn=dot(nabla_grad(v), n) into the variational form, just like the variational form I showed above: variational_form = ( ...... + dot(dwdn, dvdn) )*dx, the computational cost is very large. Is this a normal phenomenon? Do we have any method to decrease the computational cost?