Error while trying to find the derivative of user-defined expression ["ufl.log.UFLException: Cannot determine geometric dimension from expression."]

Dear Community

I am trying to solve the forced-convection problem in a 2D spherical annulus. The velocity components are specified in the script below as “w_r” and “w_theta” and are known analytically. I need to find the partial derivatives of these components with respect to the “x” and “y” coordinates, so as to set up my variational formulation.

However, when I attempt to find the x-derivative using “w_r.dx(0)”, I get the following error

ufl.log.UFLException: Cannot determine geometric dimension from expression.

The code snippet is given below. Could you please help?

Thank You
Warm Regards

from dolfin import *
from mshr import *
import math
import numpy as np

Re = 10.
Ri = 1.
num_segments=100
domain = Circle(Point(0., 0.), Re, num_segments) - Circle(Point(0., 0.), Ri, num_segments)

mesh_resolution = 128
mesh = generate_mesh(domain, mesh_resolution)
V = FunctionSpace(mesh, "CG", 2)
x = SpatialCoordinate(mesh)

r = Expression("sqrt(x[0]*x[0]+x[1]*x[1])", degree=2)
theta = Expression("atan2(x[1],x[0])", degree=2)

## Defining analytically known velocity components for creeping flow
w_r = Expression("cos(theta)*(1.-(3./(2.*r))+(1./(2.*pow(r,3))))",degree=2,r=r,theta=theta)
w_th = Expression("-sin(theta)*(1.-(3./(4.*r))-(1./(4.*pow(r,3))))",degree=2,r=r,theta=theta)

a4 = w_r.dx(0) # trying to compute the x-derivative of w_r, 
               # results in error mentioned above

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.

3 Likes

Thank you, that works perfectly!