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


345
views
1
10 months ago by
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.
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
10 months ago by
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"
Hi, I have used conditional statement but that makes code very slow, I have used like this:

from dolfin import *
import matplotlib.pyplot as plt
E = 29e3
K = 500.0
sy = 36.0
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

def stre(Gamma,E,Tstress):
    return (1-(Gamma*E)/abs(Tstress))*Tstress
def E_val(K,E):
    return E*K/(E+K)
def alpha_val(alphan,Gamma):
    return alphan+Gamma
def pStrain_val(pStrainN,Gamma,Tstress ):
    return pStrainN + Gamma*sign(Tstress)

for n in range(num_steps):
    t+=dt
    uml.t = t
    solve(a==L, u, bc)
    eps = u.dx(0)
    #print u.vector().norm('l2')
    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)
    Gamma = fun/(E+K)
    stress = conditional(le(fu,0.0),Tstress,stre(Gamma,E,Tstress))
    C_ep = conditional(le(fu,0.0),E, E_val(K,E) )
    pStrain = conditional(le(fu,0.0),pStrain,pStrain_val(pStrainN,Gamma,Tstress ) )
    alpha = conditional(le(fu,0.0),alphan,alpha_val(alphan,Gamma))
    alphan = alpha
    pStraiN = pStrain
        


    ​

any suggestions ?


Thanks for the help

written 10 months ago by hirshikesh  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »