You might try adding a tiny epsilon to the argument, e.g.,
def safe_sqrt(x):
return sqrt(x + DOLFIN_EPS)
I’ve run into problems before with x exactly equal to zero, where sqrt(x) is fine, but its symbolic derivative (as computed automatically when using the derivative function) involves a divide-by-zero.