How to fix this error(nan error)

Hello everyone, I am trying to implement some mixed formulation. I am getting “nan” error while running this code. This error comes when I find L2 error. Kindly suggest me something to fix this error.

Thanks in advance.

from fenics import*
import numpy as np
u_ex = Expression('x[0]*(1-x[0])*sin(pi*x[1])', degree=3)
f = Expression('-sin(pi*x[1])*(2+pi*pi*x[0]*(1-x[0]))', degree=3)
mesh = UnitSquareMesh(8,8)
Mh = FiniteElement('DG',mesh.ufl_cell(),1)
Qh = FiniteElement('Discontinuous Lagrange Trace', mesh.ufl_cell(), 0)
element = MixedElement([Mh,Qh])
V = FunctionSpace(mesh,element)
bc = DirichletBC(V.sub(0), u_ex, "on_boundary")
u, p = TrialFunctions(V)
v, q = TestFunctions(V)
n = FacetNormal(mesh)
a0 = inner(grad(u),grad(v))*dx + avg(p)*(v('+')-v('-'))*dS #+ avg(v)*jump(p,n)*dS
a1 = avg(q)*(u('+')-u('-'))*dS #+ jump(sigma,n)*avg(q)*dS + p*dot(tau, n)*ds + dot(sigma,n)*q*ds
L = f*v*dx
A0 = assemble(a0)
A1 = assemble(a1)
A = A0 + A1
b = assemble(L)
bc.apply(A,b)
U = Function(V)
solve(A, U.vector(), b)
(u, p) = U.split(deepcopy=True)
V1 = FunctionSpace(mesh,'DG',1)
u_i = interpolate(u_ex, V1)
u_err = sqrt(assemble((u_i - u)*(u_i - u)*dx))
errors = {'u_L2_err': u_err}
print('u_err =', u_err)

You get NAN as a result, as the resulting solution U is nan in almost all entries.
This can be verified by

print(U.vector().get_local())

This means that your system is not well posed (or the solver is diverging).

Please formulate the strong problem you are trying to solve, along with your variational formulation (and the source of your formulation), as this might pinpoint the reason for this behaviour.