Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve a simple mixing operation in gekko?

Tags:

gekko

I am trying to solve a simple mixing operation in gekko. The mixer mx takes two inlet streams Feed1 and Feed2. The expected result is that mass flow of outlet stream mx.outlet should be the summation of mass flows of the inlet streams. Here is what I have tried.

from gekko import GEKKO, chemical
m = GEKKO(remote=False)    

f = chemical.Flowsheet(m)
P = chemical.Properties(m)

c1 = P.compound('Butane')
c2 = P.compound('Propane')

feed1 = f.stream()
m_feed1 = f.massflows(sn= feed1.name)
m_feed1.mdot = 200
m_feed1.mdoti = [50,150]

feed2= f.stream()
m_feed2 = f.massflows(sn= feed2.name)
m_feed2.mdot = 200
m_feed2.mdoti = [50,150]      

mx = f.mixer(ni=2)
mx.inlet = [feed1.name,feed2.name]
m.options.SOLVER = 1

mf = f.massflows(sn = mx.outlet)
m.solve()

The code runs successfully. However, on mf.mdot seems to output incorrect value [-1.8220132454e-06]. The expected value is 400. Any help , what is wrong with my code?

like image 844
Siva-Sg Avatar asked Oct 26 '25 18:10

Siva-Sg


1 Answers

Here is source code that works for this mixing application:

from gekko import GEKKO, chemical
import json

m = GEKKO(remote=False)    

f = chemical.Flowsheet(m)
P = chemical.Properties(m)

# define compounds
c1 = P.compound('Butane')
c2 = P.compound('Propane')

# create feed streams
feed1 = f.stream(fixed=False)
feed2 = f.stream(fixed=False)

# create massflows objects
m_feed1 = f.massflows(sn=feed1.name)
m_feed2 = f.massflows(sn=feed2.name)

# create mixer
mx = f.mixer(ni=2)

# connect feed streams to massflows objects
f.connect(feed1,mx.inlet[0])
f.connect(feed2,mx.inlet[1])
m.options.SOLVER = 1

mf = f.massflows(sn = mx.outlet)

# specify mass inlet flows
mi = [50,150]
for i in range(2):
    m.fix(m_feed1.mdoti[i],val=mi[i])
    m.fix(m_feed2.mdoti[i],val=mi[i])
# fix pressure and temperature
m.fix(feed1.P,val=101325)
m.fix(feed2.P,val=101325)
m.fix(feed1.T,val=300)
m.fix(feed2.T,val=305)

m.solve(disp=True)

# print results
print(f'The total massflow out is {mf.mdot.value}')

print('')

# get additional solution information
with open(m.path+'//results.json') as f:
    r = json.load(f)
for name, val in r.items():
    print(f'{name}={val[0]}')

Below is the solver output. This will only work with APM 0.9.1 and Gekko v0.2.3 (release coming Aug 2019). The thermo and flowsheet object libraries were released with v0.2.2 and there are several features that are still under development. The next release should resolve many of them.

 ----------------------------------------------------------------
 APMonitor, Version 0.9.1
 APMonitor Optimization Suite
 ----------------------------------------------------------------


 --------- APM Model Size ------------
 Each time step contains
   Objects      :  6
   Constants    :  0
   Variables    :  19
   Intermediates:  0
   Connections  :  44
   Equations    :  2
   Residuals    :  2

 Number of state variables:    14
 Number of total equations: -  14
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    0

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------

 Iter    Objective  Convergence
    0  3.86642E-16  1.99000E+02
    1  4.39087E-18  1.11937E+01
    2  8.33448E-19  6.05819E-01
    3  1.84640E-19  1.62783E-01
    4  2.91981E-20  7.21250E-02
    5  1.55439E-21  2.28110E-02
    6  5.51232E-24  1.21437E-03
    7  7.03139E-29  4.30650E-06
    8  7.03139E-29  4.30650E-06
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.0469 sec
 Objective      :  0.
 Successful solution
 ---------------------------------------------------


v1 not found in results file
The total massflow out is [400.0]

time=0.0
feed1.h=44154989.486
feed1.x[2]=0.79815448476
feed1.vdot=104.9180373
feed1.dens=0.040621756423
feed1.c[1]=0.0081993193551
feed1.c[2]=0.032422437068
feed1.mdot=200.0
feed1.y[1]=0.25
feed1.y[2]=0.75
feed1.sfrc=0.0
feed1.lfrc=0.0
feed1.vfrc=1.0
feed2.h=44552246.421
feed2.x[2]=0.79815448476
feed2.vdot=106.66667125
feed2.dens=0.03995582599
feed2.c[1]=0.0080649042837
feed2.c[2]=0.031890921707
feed2.mdot=200.0
feed2.y[1]=0.25
feed2.y[2]=0.75
feed2.sfrc=0.0
feed2.lfrc=0.0
feed2.vfrc=1.0
mixer5.outlet.t=381.10062836
mixer5.outlet.h=44353617.96
mixer5.outlet.ndot=8.5239099109
mixer5.outlet.x[1]=0.20184551524
mixer5.outlet.x[2]=0.79815448476
mixer5.outlet.vdot=1.5797241143
mixer5.outlet.dens=5.5635215396
mixer5.outlet.c[1]=1.0891224437
mixer5.outlet.c[2]=4.3066994177
mixer5.outlet.mdot=400.0
mixer5.outlet.y[1]=0.25
mixer5.outlet.y[2]=0.75
mixer5.outlet.sfrc=0.0
mixer5.outlet.lfrc=1.0
mixer5.outlet.vfrc=0.0
v2=300.0
v3=4.2619549555
v4=0.20184551524
v5=0.79815448476
v6=101325.0
v7=305.0
v8=4.2619549555
v9=0.20184551524
v10=0.79815448476
v11=200.0
v12=50.0
v13=150.0
v14=200.0
v15=50.0
v16=150.0
v17=400.0
v18=100.0
v19=300.0
like image 190
John Hedengren Avatar answered Oct 29 '25 17:10

John Hedengren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!