The problem is that UFL cannot automatically infer what domain w_r
is defined on. You can specify it manually as a keyword argument, i.e.,
w_r = Expression("cos(theta)*(1.-(3./(2.*r))+(1./(2.*pow(r,3))))",
degree=2,r=r,theta=theta,
domain=mesh)
which gets rid of the error.