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

345

views

1

Hi, I am solving elastoplastic problem where I need if else statement. How can I in corporate in FEniCS. I am getting error: UFLException: UFL conditions cannot be evaluated as bool in a Python context.

Thanks for the help.

Thanks for the help.

```
from dolfin import *
import matplotlib.pyplot as plt
E = 29e3
K = 500
sy = 36
Area = 0.1
print 'properties:', E,K,sy, Area
mesh = IntervalMesh(60,0,60)
#plot(mesh, interactive = True)
V = FunctionSpace(mesh,'CG',1)
P1 = FunctionSpace(mesh,'DG',0)
u = TrialFunction(V)
v = TestFunction(V)
F = E*inner(grad(u),grad(v))*dx
a = lhs(F)
L = rhs(F)
u = Function(V)
class right(SubDomain):
def inside(self,x,on_boundary):
tol = 1e-10
return abs(x[0]-60.0) < tol and on_boundary
class left(SubDomain):
def inside(self,x,on_boundary):
tol = 1e-10
return abs(x[0]-0.0) < tol and on_boundary
Right = right()
Left = left()
uml = Expression('t', t = 0.0)
bcl= DirichletBC(V, Constant(0.0), Left)
bcr = DirichletBC(V, uml, Right)
bc = [bcl, bcr]
t = 0.0
dt = 0.005
num_steps = (0.5-0.0)/dt
num_steps = 100
delta = 0.00005
pStrain = 0.0
alphan = 0.0
pStrainN = 0.0
for n in range(num_steps):
t+=dt
uml.t = t
solve(a==L, u, bc)
eps = u.dx(0)
ep = project(eps,P1)
Tstrain = eps + delta
Tstress = E*(Tstrain - pStrain)
pStrain = pStrainN
fun = abs(Tstress) -(sy + K*alphan)
fu = project(fun,P1)
stress = Tstress
st = project(stress,P1)
right = Point(60.0)
print 'str:',ep(right), 'stress:',st(right), 'fun:', fu(right)
if fun <=0.0:
stress = Tstress
C_ep = E
else:
Gamma = fun/(E+K)
stress = (1-(Gamma*E)/abs(Tsress))*Tsress
pStrain = pStrain + Gamma*sign(Tsress)
alpha = alphan + Gamma
C_ep = E*K/(E+K)
```

Community: FEniCS Project

### 1 Answer

2

You should read the part of the UFL documentation on conditional expressions.

"c = conditional(condition, true_value, false_value)

evaluates to true_value at run-time if condition evaluates to true, or to false_value otherwise"

"c = conditional(condition, true_value, false_value)

evaluates to true_value at run-time if condition evaluates to true, or to false_value otherwise"

Please login to add an answer/comment or follow this question.

any suggestions ?

Thanks for the help