Objects

Objects are available in Python GEKKO and the APMonitor language to simplify the description of complex models.

Python GEKKO Object Library

Python GEKKO has equation functions and pre-built objects. GEKKO has objects in the Deep Learning (Brain) and Thermo & Flowsheet Chemical libraries. Some of the other pre-built objects and equation functions are:

  • abs(x) absolute value |x|
  • abs2(x) absolute value with MPCC
  • abs3(x) absolute value with binary variable for switch
  • acos(x) inverse cosine, cos^-1(x)
  • acosh(x) inverse hyperbolic cosine, cosh^-1(x)
  • Array(type,size) array of GEKKO objects
  • arx auto-regressive exogenous input (time series) model
  • asin(x) inverse sine, sin^-1(x)
  • asinh(x) inverse hyperbolic sine, sinh^-1(x)
  • atan(x) inverse tangent, tan^-1(x)
  • atanh(x) inverse hyperbolic tangent, tanh^-1(x)
  • bspline bspline for 2D data
  • cos(x) cosine
  • cspline cubic spline for 1D data
  • erf(x) error function
  • erfc(x) complementary error function
  • exp(x) e^x
  • if3(cond,x1,x2) switch between x1 (cond<0) and x2 (cond>=0)
  • log(x) log_e (x), natural log
  • log10(x) log_10 (x), log base 10
  • max2(x1,x2) maximum value with MPCC
  • max3(x1,x2) maximum value with binary variable for switch
  • min2(x1,x2) minimum value with MPCC
  • min3(x1,x2) minimum value with binary variable for switch
  • periodic periodic (initial=final) for dynamic problems
  • pwl piece-wise linear function
  • sign2(x) signum operator with MPCC
  • sign3(x) signum operator with binary variable for switch
  • sin(x) sine
  • sinh(x) hyperbolic sine
  • sqrt(x) square root
  • state_space continuous/discrete and dense/sparse state space
  • sum summation of elements in a list or numpy array
  • tan(x) tangent
  • tanh(x) hyperbolic tangent
  • vsum(x) vertical sum of a single variable in the data direction

In Python GEKKO, objects are defined as components of the model as m.object_name().

Python GEKKO Example Usage (abs3)

from gekko import GEKKO
# define new GEKKO model
m = GEKKO()
# variable
x = m.Var(-0.5)
# calculate y=abs(x) with abs3
y = m.abs3(x)
# solve with APOPT (MINLP solver)
m.solve()
# print solution
print('x: ' + str(x.value))
print('y: ' + str(y.value))

Python GEKKO Example Usage (Array, abs3, sum)

from gekko import GEKKO
import numpy as np
m = GEKKO()
x1 = m.Param(-2)
x2 = m.Param(-1)
x3 = np.linspace(0,1,6)
x4 = m.Array(m.Param,3)
y4 = m.Array(m.Var,3)
for i in range(3):
    x4[i].value=-0.2
    y4[i] = m.abs3(x4[i])
# create variable
y = m.Var()
# y = 0.6 =          -2 -1   + 3       + 0.6
m.Equation(y == sum([x1,x2]) + sum(x3) + sum(y4))
m.solve() # solve
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('y: '  + str(y.value))

APMonitor Object Library

In APMonitor, objects are defined in the Objects ... End Objects section of the model file. New instances of an object are defined by declaring a new object name equal to the parent object type.

new_child = parent_object

 ! example use of ABS as MPEC
 Objects
   a = abs
 End Objects

 Connections
   x = a.x
   y = a.y
 End Connections

 Parameters
   x = -5
 End Parameters

 Variables
   y
 End Variables

The object library consists of common mathematical functions and chemical processing equipment such as feed streams, reactors, pumps, mixers, flash columns, vessels, and distillation stages. It also includes other elements that support distributed control system emulation such as a LAG and a PID controller.


Example - MIN Function


Example - Mixer Application


Example - Distillation Column

 Model distill

 Compounds
  ethylene  
  propylene
  propane
  hydrogen
  nitrogen
 End Compounds

 Objects
  ! feed stream
  feed           = Feed
  feed_lag       = Stream_Lag
  feed_cooler    = Vessel
  feed_flash     = Flash
  liq_mixer      = Mixer
  vap_mixer      = Mixer

  ! condenser and reflux
  condenser      = Vessel
  drum           = Flash
  reflux         = Splitter

  ! column stages
  stage[1:8]     = Stage_1

  ! reboiler
  sump           = Vessel
  reboiler       = Vessel
  reboiler_flash = Flash

  ! mass and massflows
  sump_mass      = Mass
  feed_massflow  = Massflow
  cleu_massflow  = Massflow
  btms_massflow  = Massflow
 End Objects

 Connections
  ! feed streams
  feed.*                      = feed_lag.inlet.*
  feed_lag.outlet.*           = feed_cooler.inlet.*
  feed_cooler.outlet.*        = feed_flash.inlet.*
  feed_flash.outlet_vap.*     = vap_mixer.inlet[1].*
  feed_flash.outlet_liq.*     = liq_mixer.inlet[1].*

  ! liquid down the column
  liq_mixer.inlet[2].*        = stage[1].l_out.*
  liq_mixer.outlet.*          = stage[2].l_in.*
  stage[2:7].l_out.*          = stage[3:8].l_in.*
  stage[8].l_out.*            = sump.inlet.*

  ! reboiler
  sump.outlet.*               = reboiler.inlet.*
  reboiler.outlet.*           = reboiler_flash.inlet.*

  ! vapor up the column
  reboiler_flash.outlet_vap.* = stage[8].v_in.*
  stage[3:8].v_out.*          = stage[2:7].v_in.*
  vap_mixer.inlet[2].*        = stage[2].v_out.*
  vap_mixer.outlet.*          = stage[1].v_in.*

  ! condenser
  stage[1].v_out.*            = condenser.inlet.*
  condenser.outlet.*          = drum.inlet.*
  drum.outlet_liq.*           = reflux.inlet.*
  reflux.outlet[2].*          = stage[1].l_in.*

  ! mass and massflow meters
  sump.reserve.*              = sump_mass.acc.*
  feed.*                      = feed_massflow.stream.*
  drum.outlet_vap.*           = cleu_massflow.stream.*
  reboiler_flash.outlet_liq.* = btms_massflow.stream.*

  ! stream pressures
  strm_p                            = stage[1].v_out.p
  strm_p                            = stage[2:8].l_out.p
  strm_p                            = stage[3:8].v_out.p

  ! feed and stage pressures
  fd_p                              = stage[1:8].l_res.p
 End Connections


 Model custom

  Parameters
     fd_t     = 370.0  ! K
     fd_p     = 3.11e6 ! Pa
     fd_c2h4  = 0.28   ! mol%
     fd_c3h6  = 0.6184 ! mol%
     fd_c3h8  = 0.0916 ! mol%
     fd_h2    = 0.01   ! mol%

     strm_p   = fd_p   ! Pa
     fd_ndot  = 1.0
  End Parameters

  Variables
     fd_mdot  = 0.395  ! kg/sec
  End Variables

  Connections
     fd_t     = feed.t
     fd_p     = feed.p
     fd_c2h4  = feed.x[1]
     fd_c3h6  = feed.x[2]
     fd_c3h8  = feed.x[3]
     fd_h2    = feed.x[4]
     fd_mdot  = feed_massflow.mdot
     fd_ndot  = feed.ndot
  End Connections

  Intermediates
  End Intermediates

  Equations
  End Equations

 End Model