Hi (sorry if this gets posted twice, I had a small mistake in my first post)
I'm trying to use a constraint in which an if statement on a variable is present. It seems that this has to be done with the value() function.
Unfortunately, I get weird model behaviour, when I do that. After I initialized the variable in order to use value() the result is that pyomo stays
inside the corresponding if clause even when p_diff takes on values for which the if statement isn't true anymore and one of the other conditions should be used.
In the code below for example pyomo keeps on skipping the constraint, since p_diff was initialized with 0. If I initialize it with a non-negative value it stays in
one of the other if statements until **0.5394 can't be calculated anymore and I get an error
I have tried multiple ways, but nothing seems to work. Does anyone know how to handle such a case?
Example:
m.p_diff=Var(m.a, m.t, initialize=0, doc="Pressure difference for panhandle equation")
def p_diff_def(m, a, t):
return m.p_diff[a,t]==(((10**3*m.p[m.arc_n0[a],t])**2-(10**3*m.p[m.arc_n1[a],t])**2)/(m.spec_grav**0.8539*m.T_avg*m.L[a]*m.z_gas))
m.p_diff_Con=Constraint(m.a, m.t, rule=p_diff_def)
###Panhandle A equation
def panhandle_def(m, a, t):
if value(m.p_diff[a,t])>=0 and value(m.p_diff[a,t])!=0:
return m.m_left_p[a,t]*10**3/m.rho_norm*24==4.5965*10**-3*m.e_pipe*(m.T_std/(m.p_atm*10**3))**1.0788\
*m.p_diff[a,t]**0.5394*m.D[a]**2.6182
if value(m.p_diff[a,t])<=0 and value(m.p_diff[a,t])!=0:
return m.m_right_p[a,t]*10**3/m.rho_norm*24==4.5965*10**-3*m.e_pipe*(m.T_std/(m.p_atm*10**3))**1.0788\
*(-m.p_diff[a,t])**0.5394*m.D[a]**2.6182
else:
return Constraint.Skip
m.panh_con=Constraint(m.a, m.t, rule=panhandle_def, doc="Panhandle A equation")
--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
###Free Flow direction
def flow_p_direction(m, a, t):
return m.p[m.arc_n0[a],t]*(m.p_dir[a,t])+(1-m.p_dir[a,t])*m.p[m.arc_n1[a],t]\
>=m.p[m.arc_n1[a],t]*m.p_dir[a,t]+(1-m.p_dir[a,t])*m.p[m.arc_n0[a],t]+0.0001
m.f_direct_p=Constraint(m.a, m.t, rule=flow_p_direction, doc="Definition of flow direction in passive lines")
###Panhandle A equation
def panhandle_def(m, a, t):
return (m.p_dir[a,t])*m.m_left_p[a,t]*10**3/m.rho_norm*24+(1-m.p_dir[a,t])*m.m_right_p[a,t]*10**3/m.rho_norm*24\
==4.5965*10**-3*m.e_pipe*(m.T_std/(m.p_atm*10**3))**1.0788\
*(((10**3*((m.p_dir[a,t])*m.p[m.arc_n0[a],t]+(1-m.p_dir[a,t])*m.p[m.arc_n1[a],t]))**2
-(10**3*((1-m.p_dir[a,t])*m.p[m.arc_n0[a],t]+(m.p_dir[a,t])*m.p[m.arc_n1[a],t]))**2)
/(m.spec_grav**0.8539*m.T_avg*m.L[a]*m.z_gas))**0.5394*m.D[a]**2.6182
m.panh_con=Constraint(m.a, m.t, rule=panhandle_def, doc="Panhandle A equation")
###Pressure levels at line ends
def flow_p_direction(m, a, t):
return m.p[m.arc_n0[a],t]*(m.p_dir[a,t])+(1-m.p_dir[a,t])*m.p[m.arc_n1[a],t]\
>=m.p[m.arc_n1[a],t]*m.p_dir[a,t]+(1-m.p_dir[a,t])*m.p[m.arc_n0[a],t]+0.001
m.f_direct_p=Constraint(m.a, m.t, rule=flow_p_direction, doc="Definition of pressure at line ends")
###Panhandle A equation
def panhandle_def(m, a, t):
return (m.p_dir[a,t])*m.m_left_p[a,t]*10**3/m.rho_norm*24+(1-m.p_dir[a,t])*m.m_right_p[a,t]*10**3/m.rho_norm*24\
==4.5965*10**-3*m.e_pipe*(m.T_std/(m.p_atm*10**3))**1.0788\
*(((10**3*((m.p_dir[a,t])*m.p[m.arc_n0[a],t]+(1-m.p_dir[a,t])*m.p[m.arc_n1[a],t]))**2
-(10**3*((1-m.p_dir[a,t])*m.p[m.arc_n0[a],t]+(m.p_dir[a,t])*m.p[m.arc_n1[a],t]))**2)
/(m.spec_grav**0.8539*m.T_avg*m.L[a]*m.z_gas))**0.5394*m.D[a]**2.6182
m.panh_con=Constraint(m.a, m.t, rule=panhandle_def, doc="Panhandle A equation")
The strange thing is, that I get an error: "can't evaluate pow' (-57.91, 0.5394) which would mean that the difference turned out to be negative. Do you know why? Is this the case you talked about Gabe where the polynomial expressions have to be used?
<BE_simple.dat><Test_Model.py>