This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

UFL conditions cannot be evaluated as bool in a Python context.

0 votes

I have the following code:

from dolfin import*
import numpy

nx = ny = 20
mesh = UnitSquareMesh(nx, ny)
#plot(mesh)
V = FunctionSpace(mesh, "CG", 1)

Tc = Constant(310)
Tp = Constant(77)

#Define Boundary Conditions
boundary = compile_subdomains('x[0] <= 0.1 && x[1] <= 0.1')
bc = DirichletBC(V, Tp, boundary)

# Initial condition
Tinitial = 'x[0] <= 0.1 && x[1] <= 0.1 ? Tp : Tc'
Temp = Expression(Tinitial, Tp = Tp, Tc = Tc)
T_1 = interpolate(Temp, V)

T = TrialFunction(V)
v = TestFunction(V)

#define Parameters
dt = 1    # time step

def k(T):
    if T > 273:
        return 0.5
    if T > 251 and T < 273:
        return 15.98 - 0.0567*T
    if T < 251:
        return 1005 * T**-1.15
def c(T):
    if T > 273:
        return 3.6*1E6
    if T > 265 and T < 273:
        return (880 - 3.21*T)*1E6
    if T > 251 and T < 265:
        return (2.017*T - 505.3)*1E6
    if T < 251:     
        return 0.00415*T

a = c(T_1)*T*v*dx + dt*inner(k(T_1)*grad(T), grad(v))*dx
L = c(T_1)*T_1*v*dx

A = None
b = None

T = Function(V)

duration = 1         # total simulation time
t = dt           # initialise time t

while t <= duration:
    print 'time =', t
    A = assemble(a)
    b = assemble(L)
    bc.apply(A, b)
    solve(A, T.vector(), b)
    t += dt 
    T_1.assign(T)

print T.compute_vertex_values().max(), T.compute_vertex_values().min()
plot (T, title = 'T plot')
interactive()

On running this, I am getting an error saying:

UFL conditions cannot be evaluated as bool in a Python context.

Can anyone tell me whats wrong with the code..?

asked Feb 26, 2014 by iitmayush FEniCS Novice (280 points)

1 Answer

+2 votes

Conditional statements can be tricky. Remember that 'T' in your functions k() and c()
is a UFL representation of a Function, so it can only be used with valid UFL operators.

Check out the manual for UFL:

http://fenicsproject.org/pub/documents/ufl/ufl-user-manual/ufl-user-manual.pdf

where there is a section on conditional statements.

answered Feb 26, 2014 by chris_richardson FEniCS Expert (31,740 points)
...