UFL conditions cannot be evaluated as bool in a Python context -error message

I have written the following codes for generating a 3D Cylindrical mesh.
from fenics import *
from mshr import *
from math import *
from dolfin import *
# Create Geometry
Rair = 5
cyltot = Cylinder(Point(0,0,10),Point(0,0,0),Rair,Rair)
mesh_tot = generate_mesh(cyltot,64)
vtkfile = File(‘Mesh.pvd’)
vtkfile << mesh_tot
V = FunctionSpace(mesh_tot,‘P’,1)

# Finding Subdomains
rho = Expression('sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2])',degree=2)


tol = 1E-14
class Copper(SubDomain):
    def inside(self, x, on_boundary):
        return rho <= 1 + tol

    
class Air(SubDomain):
    def inside(self, x, on_boundary):
        return rho >= 1 - tol
    

materials = MeshFunction('size_t',mesh_tot,mesh_tot.topology().dim())
Subdom_cu = Copper()
Subdom_air = Air()
materials.set_all(2)
Subdom_air.mark(materials, 3)

import matplotlib.pyplot as plt
plot(materials)
plt.show()

After running the above,I get the following error

UFL conditions cannot be evaluated as bool in a Python context.
Traceback (most recent call last):
File “/home/fenics/shared/Cylinder/cylinder.py”, line 34, in
Subdom_air.mark(materials, 3)
RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details)

Can somebody help me in understanding what is the error here and point me towards a solution?

1 Like

I just went through a few posts this and came to know that subdomains in 3D are not yet supported.If this is the problem in the above problem, can someone please suggest a workaround?

You should do:

import numpy as np
tol = 1E-14
class Copper(SubDomain):
    def inside(self, x, on_boundary):
        return np.sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]) <= 1 + tol

    
class Air(SubDomain):
    def inside(self, x, on_boundary):
        return np.sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]) >= 1 - tol
    

as you need to use the input coordinates in the inside function