### assign out put of conditional statement

314
views
0
11 months ago by
Hi, I am trying to assign output of conditional statement but getting error: AttributeError: 'float' object has no attribute 'assign'
from dolfin import *
import matplotlib.pyplot as plt
E = 29e3
K = 500.0
sy = 36.0

mesh = IntervalMesh(60,0,60)
V  = FunctionSpace(mesh,'CG',1)
P1 = FunctionSpace(mesh,'DG',0)
u = TrialFunction(V)
v = TestFunction(V)

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.0-(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)

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)
stress = Tstress
st = project(E*(Tstrain),P1)
right = Point(60.0)
print 'str:',ep(right), 'Tstress', st(right)
Gamma = fun/(E+K)
stress = conditional(le(fun,0.0),Tstress,stre(Gamma,E,Tstress))
C_ep = conditional(le(fun,0.0),E, E_val(K,E) )
pStrain = conditional(le(fun,0.0),pStrain,pStrain_val(pStrainN,Gamma,Tstress ) )
alphaT = conditional(le(fun,0.0),alphan,alpha_val(alphan,Gamma))
alphan.assign(alphaT)
pStraiN.assign(pStrain)​

Thanks for help.

Community: FEniCS Project
Dear hsk,
This error is related to a misunderstanding of python.
alphan​ = 0.0

was defined as a float. Thus, the structure assign, that is specific property of some specific FEniCS variables, is not recognized.
If you want to update the value of a python variable, just declare again, maybe as:

alphan = alphaT

I don't know if this would be the appropriate solution though.
All the best, Murilo.

written 11 months ago by Murilo Moreira
I am getting error also in assigning conditional statement to function.
I don not want to use alphan = alphaT
any suggestions.
written 11 months ago by hirshikesh

4
11 months ago by
Hi,

to add some more detail to Murilo's comment: what the conditional returns is a ufl-object. In order to use this ufl-object, project to a (proper) FunctionSpace. This operation returns a Function object, which can be used for, e.g., plotting,  extracting numeric values, etc., etc.
Consider the following MWE:
from dolfin import *

mesh = UnitIntervalMesh(10)
V = FunctionSpace(mesh,'CG',1)

expr = Expression("x[0]")
c1   = conditional(le(expr,0.5), expr, 0.5)

# Project to function
cmesh= project(c1,V)

# Plot 'conditional expression'
plot(cmesh)
interactive()
# Return vector of values
print(cmesh.vector().array())​

Hope this helps