Hi,
I am a new user of Fenics, and i don't understand how to compute a flux...
I would like to calculate integral(grad u.n ds), knowing grad u.
My problem is I don't get it how to define n (the unit normal vector)
Here is a simple version of the code I use
# Import libraries
from dolfin import *
# Create the mesh
mesh = UnitCubeMesh(4,4,4)
# Discrete function space V
V = FunctionSpace(mesh, 'Lagrange', 1)
# Boundary condition
tolerance_boundary_location = 1e-15
def apply_boundary_omega1(x):
return ( abs(x[0]) < tolerance_boundary_location or abs(x[0]-1) < tolerance_boundary_location
or abs(x[1]) < tolerance_boundary_location or abs(x[1]-1) < tolerance_boundary_location
or abs(x[2]) < tolerance_boundary_location or abs(x[2]-1) < tolerance_boundary_location )
bc_value = Expression('-400*x[0] + 500')
bc = DirichletBC(V, bc_value, apply_boundary_omega1)
# Define trial function u
u_trial = TrialFunction(V)
# Define test functions v
v_test = TestFunction(V)
# Define function f
f = Constant(0)
# Integrands of a(u,v)
a = inner (nabla_grad(u_trial),nabla_grad(v_test))*dx
# Integrands of L(v)
L = f*v_test*dx
# Define the unknown function to be computed
u = Function(V)
# Solve the PDE variational problem
solve(a == L, u, bc)
# Plot the solution
plot(u, interactive=True)
# Calculate the gradient through the projection method, using built-in function project for the calculation
gradient_u = project(-grad(u),VectorFunctionSpace(mesh, 'Lagrange', 1))
# Plot the gradient
plot(gradient_u, interactive=True)
Then, what is the best solution to compute integral (grad u .n ds) on the the plane x=1 and normal to it (then n=[0, 1])
- should i use the function FacetNormal ? (this function is not clear for me)
- should i define manually n such as n=[0, 1] and assemble ? (it's not working)
- should i perform a loop on each cell, grab each local grad u, calculate locally grad u.n and sum all the values ? (seems not a very elegant solution...)
Any help will be very appreciate.
Regards,