I implemented the distance-to-boundary
functionality but found that solving the eikonal equation was much more convenient. For example if you want to get the distance from just a small set of the boundary and the geometry is really complicated and you do not want the direct distance but rather the distance within the domain:
from dolfin import *
import math
installation = "/PATH/TO/DOLFIN/share/dolfin"
demo_path = "/demo/documented/stokes-mini/"
mesh_path = installation + demo_path
mesh = Mesh(mesh_path + "dolfin_fine.xml.gz")
V = FunctionSpace(mesh, 'CG', 1)
v = TestFunction(V)
u = TrialFunction(V)
f = Constant(1.0)
y = Function(V)
class DolphinBoundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and ((x[0]-0.5)**2+(x[1]-0.5)**2) < 0.25
boundary = DolphinBoundary()
bc = DirichletBC(V, Constant(0.0), boundary)
# Initialization problem to get good initial guess for nonlinear problem:
F1 = inner(grad(u), grad(v))*dx - f*v*dx
solve(lhs(F1)==rhs(F1), y, bc)
# Stabilized Eikonal equation
print "max cell size:", mesh.hmax()
eps = Constant(mesh.hmax()/25)
F = sqrt(inner(grad(y), grad(y)))*v*dx - f*v*dx + eps*inner(grad(y), grad(v))*dx
solve(F==0, y, bc)
print "Max distance:", y.vector().max()
plot(y, rescale=True, title="Eikonal", interactive=True)