TypeError: must be real number, not GK_Operators

81 views
Skip to first unread message

Edward Iwuchukwu

unread,
Nov 13, 2021, 9:04:02 PM11/13/21
to apmonitor
Good day everyone,
I hope you are all doing well?
I have been trying to simulate the below polymerization system of index-1 with GEKKO but i keep getting the above stated error
Can someone advice on what to tweak in order to eliminate the error? Thanks.

# define the model
m = GEKKO()    # create GEKKO model

# Defining the temperature in degrees kelvin.
T=(100+273);
# Pre-defining the kinetic parameters we have:
k_d=1.99*10**(6)*math.exp(-14842/T);
k_i=1.712*10**(15)*math.exp(-15924/T);
k_iterm=2.019*10**(1)*math.exp(-13810/T);
k_p=1.051*10**(7)*math.exp(-3577/T);
k_trM=2.31*10**(-6)*math.exp(-6377/T);
k_trS=1.8;
k_td=0.99*1.255*10**(9)*math.exp(-844/T);
k_tc=1.255*10**(9)*math.exp(-844/T);
M0=104.15; # molecular mass of Styrene monomer
f = 0.65;  # Initiator efficiency
dm=0.909; # density of styrene monomer
dp=1.000;  # density of Polystyrene
e=(dm-dp)/dp;

# create GEKKO variables
X  = m.Var(0.0) # monomer conversion
M  = m.Var(70.0) # monomer concentration
I  = m.Var(30.0) # initiator concentration
l_0 = m.Var(0.0)
l_1 = m.Var(0.0)
l_2 = m.Var(0.0)
u_0 = m.Var(0.0)
u_1 = m.Var(0.0)
u_2 = m.Var(0.0)
p = m.Var(0.0)

# create GEKKO equations
m.Equations([p==(M/M0),l_0==math.sqrt((2*f*k_d*I)/(k_tc+k_p*e*p)),
             (l_1==2*f*k_d + k_p*M*l_0+k_trM*M*l_0)/(k_trM*M+k_tc*l_0+k_p*e*l_0*p),
             l_2==l_1+((2*k_p*M*l_1)/(k_i*l_0+k_p*e*p*l_0)),
             u_1.dt()==k_trM*M*l_0+k_tc*l_0*l_1-k_p*u_1*l_0*e*p,
             u_0.dt()==k_trM*M*l_0+k_td*l_0**2+k_tc/2*(l_0**2)-u_0*l_0*e*k_p*p,
             M.dt()==-k_p*p*l_0*(1+e*p),
             u_2.dt()== k_trM*M*l_2+k_tc*l_0*l_2+k_tc*l_1**2-k_p*u_2*l_0*e*p,
             I.dt()==-k_d*I-k_p*I*l_0*e*p,
             X==(M0-M)/(M0+e*M),
             Mn==Mm*(u_1+l_1)/(u_0+l_0),
             Mw==Mm*(u_2+l_2)/(u_1+l_1),
             PDI==Mw/Mn
            ])

John Hedengren

unread,
Nov 13, 2021, 10:19:43 PM11/13/21
to apmo...@googlegroups.com

Edward,

 

Try using the gekko function m.sqrt() instead of math.sqrt() for the equation:

 

l_0==math.sqrt((2*f*k_d*I)/(k_tc+k_p*e*p))

 

It may also help to eliminate the sqrt as and potential divide-by-zero:

 

(k_tc+k_p*e*p) * l_0**2== (2*f*k_d*I)

 

This is an equivalent equation but may be much easier for a solver to find a solution by avoiding undefined evaluations in search of a solution. There are several other equations in the model that may also benefit from rearrangement.

 

Best regards,

John Hedengren

--
--
APMonitor user's group e-mail list.
- To post a message, send email to apmo...@googlegroups.com
- To unsubscribe, send email to apmonitor+...@googlegroups.com
- Visit this group at http://groups.google.com/group/apmonitor
---
You received this message because you are subscribed to the Google Groups "apmonitor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apmonitor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apmonitor/8e975b14-607f-415f-ae22-007be675b125n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages