JacquesStrydom
JacquesStrydom

Reputation: 319

How do I get GEKKO chemical flowsheet mixer object to solve

I am exploring the Gekko chemical library and flowsheet implementation. I defined compounds Propylene, Propane, Butane and Pentane and two stream with differing fractions of the compounds and molar flow rates for streams. I want to blend these streams and calculate the total molar flow rate and molar fractions of the combined stream.

The result I get is: Mole fraction in blended stream is: [[0.25], [0.25], [0.25], [0.25]]. It appears to be the initialised values for the outlet stream.

I have perused this StackOverflow link [here] 1 without success.
Sample code here:

from gekko import GEKKO, chemical
import numpy as np

m = GEKKO()    #instantiate GEKKO object
f = chemical.Flowsheet(m,stream_level=0)  #instantiate flowsheet object
c = chemical.Properties(m)
c.compound('propylene')    #define compound 1
c.compound('propane')      #define compound 2
c.compound('butane')       #define compound 3
c.compound('pentane')      #define compound 4

s1=f.stream(fixed=True)    #define stream 1
s1.x=np.array([0.5,0.3,0.1,0.1])     #specify mole fraction of compound 1-4 for stream1
s1.ndot=10                 #specify molar flow rate of stream 1

s2=f.stream(fixed=True)    
s2.x=np.array([0.3,0.3,0.2,0.2])     #specify mole fraction of compound 1-4 for stream2
s2.ndot=20                 #specify molar flow rate of stream 2

outlet=f.stream(fixed=False) #define outlet stream that should be solved

mx = f.mixer(ni=2)        #mixer object with two inlet
#mx.inlet[0]=s1
#mx.inlet[1]=s2
f.connect(mx.inlet[0],s1)  #assign stream 1 to mixer inlet 1
f.connect(mx.inlet[1],s2)  #assign stream 2 to mixer inlet 2


mx.outlet=outlet  #connect mixer outlet to outlet stream obj
m.options.SOLVER=1
m.solve()

print('Mole fractions in blended stream is:',  mx.outlet.x)

Upvotes: 2

Views: 70

Answers (1)

John Hedengren
John Hedengren

Reputation: 14346

Initialize each x[i] component of the inlet streams with:

inlet1 = [0.5,0.3,0.1,0.1]
inlet2 = [0.3,0.3,0.2,0.2] 
for i in range(4):
    s1.x[i].value = inlet1[i]
    s2.x[i].value = inlet2[i]

Use the stream object on the left hand side of the connection function. The connection function keeps the object on the left side (1st argument) and removes the 2nd object reference (e.g. f.connect(keep,remove)). The outlet stream also needs a connection:

f.connect(outlet,mx.outlet)

Here is a complete script:

from gekko import GEKKO, chemical
import numpy as np

m = GEKKO()    #instantiate GEKKO object
f = chemical.Flowsheet(m,stream_level=0)  #instantiate flowsheet object
c = chemical.Properties(m)
c.compound('propylene')    #define compound 1
c.compound('propane')      #define compound 2
c.compound('butane')       #define compound 3
c.compound('pentane')      #define compound 4

s1=f.stream(fixed=True)    #define stream 1
inlet1 = [0.5,0.3,0.1,0.1]     #specify mole fraction of compound 1-4 for stream1
s1.ndot=10                 #specify molar flow rate of stream 1

s2=f.stream(fixed=True)    
inlet2 = [0.3,0.3,0.2,0.2]     #specify mole fraction of compound 1-4 for stream2
s2.ndot=20                 #specify molar flow rate of stream 2

for i in range(4):
    s1.x[i].value = inlet1[i]
    s2.x[i].value = inlet2[i]

outlet=f.stream(fixed=False) #define outlet stream that should be solved

mx = f.mixer(ni=2)        #mixer object with two inlet
#mx.inlet[0]=s1
#mx.inlet[1]=s2
f.connect(s1,mx.inlet[0])  #assign stream 1 to mixer inlet 1
f.connect(s2,mx.inlet[1])  #assign stream 2 to mixer inlet 2
f.connect(outlet,mx.outlet)
#mx.outlet=outlet  #connect mixer outlet to outlet stream obj
m.options.SOLVER=1
m.solve()

print('Mole fractions in blended stream is:',  outlet.x)
# Open run folder to see more detail from model and results
# m.open_folder()

This gives the blending solution:

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
 
 Iter    Objective  Convergence
    0  7.40741E-20  1.11111E+00
    1  1.08350E-35  1.15741E-01
    2  1.08350E-35  1.15741E-01
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.279999999678694E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
Mole fractions in blended stream is: [[0.4], [0.3], [0.15], [0.15]]

Upvotes: 1

Related Questions