How to implement inhomogeneus Neumann boundary condition

It’s a little hard to debug without a fully-runnable example, but it looks like you’re trying to use SymPy to take derivatives. That should not be necessary, since FEniCS’s Unified Form Language (UFL) has robust symbolic differentiation capabilities.

For the case of partial derivatives with respect to spatial coordinates, you can use the dx() method of a UFL object, e.g.,

#g = Constant(1/0.3) - Constant(0.4/0.3)*sym.diff(c, x).subs(x,lox)
g = Constant(1/0.3) - Constant(0.4/0.3)*c.dx(0)

where the argument to dx specifies the index of coordinate with respect to which the partial derivative is taken. (N.b. that the dx() method is not related to the integration measure object dx.) Also, because the finite element assembly procedure will only evaluate this term on the specified boundary, there is no need to manually substitute the value of the boundary’s x coordinate.

For other symbolic derivatives, there is a diff function in UFL similar to the SymPy diff; see the discussion here for an example.

1 Like