Hello everyone!
I am a novice at Fenics, but after staring at the variational form for a bit was able to successfully solve a set of PDEs for a scalar field. Now I would like to again use Fenics to solve for a separate scalar field from its gradient which I know at every point within the domain. This seems really elementary but I am struggling to figure it out. I understand how to utilize UserExpressions to specify my gradient at every point in the domain, but I need help with putting it into variational form and solving for it.
Any help would be appreciated,
Garrett
Hi,
It would be helpful to provide some context. Simply based on what you say, consider the following field
u_e = x^2 y
to be recovered from it’s gradient
\nabla u_e = \begin{bmatrix}2xy\\ x^2\end{bmatrix}
from dolfin import *
mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "CG", 3)
u = Function(V)
v = TestFunction(V)
du = TrialFunction(V)
x = SpatialCoordinate(mesh)
gradu = as_vector([2*x[0]*x[1], x[0]*x[0]])
solve(inner(grad(du), grad(v))*dx == inner(gradu, grad(v))*dx, u, [DirichletBC(V, Constant(0), "near(x[0], 0) && on_boundary")])
# Exact solution
ue = Function(V)
ue.interpolate(Expression("x[0]*x[0]*x[1]", degree=3))
# Test the solution
max(abs(u.vector()[:] - ue.vector()[:])) # 9.703349235223868e-14
As long as you can write what you want to achieve in a variational form, you should be able to implement it like above.
3 Likes
Hello bhaveshshrimali,
That answer was just what I was looking for! Thank you so much!
Garrett