Batch Reactor Optimization

Apps.BatchReactor History

Hide minor edits - Show changes to output

March 27, 2023, at 05:18 PM by 10.35.117.248 -
Changed lines 5-6 from:
%width=550px%Attach:batch_reactor.png
to:
(:html:)
<iframe width="560" height="315" src="https://www
.youtube.com/embed/FHq4xYWm6ww" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
(:htmlend:)

Added lines 89-90:

%width=550px%Attach:batch_reactor.png
March 26, 2023, at 02:35 AM by 136.36.4.38 -
Changed line 94 from:
📄[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Gekko Publication and References]]
to:
📄 [[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Gekko Publication and References]]
March 26, 2023, at 02:34 AM by 136.36.4.38 -
Changed lines 92-94 from:
The [[https://gekko.readthedocs.io/en/latest/|Gekko Optimization Suite]] is a machine learning and optimization package in Python for mixed-integer and differential algebraic equations. The GEKKO package is available in Python with '''pip install gekko'''. There are [[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|additional example problems]] for equation solving, optimization, regression, dynamic simulation, model predictive control, and machine learning.

📄[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Gekko Publication and References]]
to:
The [[https://gekko.readthedocs.io/en/latest/|Gekko Optimization Suite]] is a machine learning and optimization package in Python for mixed-integer and differential algebraic equations.

📄[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Gekko Publication and References]]

The GEKKO package is available in Python with
'''pip install gekko'''. There are [[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|additional example problems]] for equation solving, optimization, regression, dynamic simulation, model predictive control, and machine learning.
March 26, 2023, at 02:33 AM by 136.36.4.38 -
Changed line 94 from:
📄[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Cite Gekko Paper]]
to:
📄[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Gekko Publication and References]]
March 26, 2023, at 02:32 AM by 136.36.4.38 -
Changed lines 92-94 from:
The [[https://gekko.readthedocs.io/en/latest/|Gekko Optimization Suite]] is a machine learning and optimization package in Python for mixed-integer and differential algebraic equations. The GEKKO package is available in Python with '''pip install gekko'''. There are [[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|additional example problems]] for equation solving, optimization, regression, dynamic simulation, model predictive control, and machine learning. [[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Cite Gekko Paper]]
to:
The [[https://gekko.readthedocs.io/en/latest/|Gekko Optimization Suite]] is a machine learning and optimization package in Python for mixed-integer and differential algebraic equations. The GEKKO package is available in Python with '''pip install gekko'''. There are [[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|additional example problems]] for equation solving, optimization, regression, dynamic simulation, model predictive control, and machine learning.

📄
[[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Cite Gekko Paper]]
March 26, 2023, at 12:16 AM by 136.36.4.38 -
Changed line 69 from:
         lw=2,label=r'$x_2$')
to:
         lw=2,label=r'$x_3$')
March 26, 2023, at 12:15 AM by 136.36.4.38 -
Changed lines 11-14 from:
{$\begin{array}{llcl}\max_{x,u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2\\ & k_1 & = & 4000 \; e^{-2500/T(t)}\\ & k_2 & = & 620000 \; e^{-5000/T(t)}\\ & x(0) &=& (1, 0)^T\\ & T(t) &\in&  [298, 398]\end{array}$}

where {`x_1
(t)`} is the concentration of ''A'' and {`x_2(t)`} is the concentration of  ''B'' at time-point ''t''. The control function ''T(t)'' is the temperature. The starting time is 0 and end time is 1.
to:
{$\begin{array}{llcl}\max_{x,u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2\\ & \frac{dx_3}{dt} & = & k_2 x_2 \\ & k_1 & = & 4000 \; e^{-2500/T(t)}\\ & k_2 & = & 620000 \; e^{-5000/T(t)}\\ & x(0) &=& (1, 0)^T\\ & T(t) &\in&  [298, 398]\end{array}$}

where
{`x_1(t)`} is the concentration of ''A'', {`x_2(t)`} is the concentration of  ''B'', and {`x_3(t)`} is the concentration of ''C'' at time-point ''t''. The control function ''T(t)'' is the temperature. The starting time is 0 and end time is 1.
Added line 32:
x3 = m.Var(value=0)
Added lines 43-46:
# Intermediates
r1 = m.Intermediate(k1*x1**2)
r2 = m.Intermediate(k2*x2)

Changed lines 48-50 from:
m.Equation(x1.dt()==-k1*x1**2)
m.Equation(x2.dt()==k1*x1**2 - k2*x2)
to:
m.Equation(x1.dt()==-r1)
m.Equation(x2.dt()== r1 - r2)
m.Equation(x3.dt()== r2
)
Added lines 68-69:
plt.plot(m.time,x3.value,'r--',\
        lw=2,label=r'$x_2$')
Changed line 76 from:
plt.plot(m.time,T.value,'r--',\
to:
plt.plot(m.time,T.value,'r-',\
March 25, 2023, at 11:53 PM by 136.36.4.38 -
Changed line 11 from:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2\\ & k_1 & = & 4000 \; e^{-2500/T(t)}\\ & k_2 & = & 620000 \; e^{-5000/T(t)}\\ & x(0) &=& (1, 0)^T\\ & T(t) &\in&  [298, 398]\end{array}$}
to:
{$\begin{array}{llcl}\max_{x,u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2\\ & k_1 & = & 4000 \; e^{-2500/T(t)}\\ & k_2 & = & 620000 \; e^{-5000/T(t)}\\ & x(0) &=& (1, 0)^T\\ & T(t) &\in&  [298, 398]\end{array}$}
March 25, 2023, at 11:52 PM by 136.36.4.38 -
Changed line 11 from:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2.\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{-2500/T(t)}, \\ & k_2 & = & 620000 \; e^{-5000/T(t)}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]\end{array}$}
to:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2\\ & k_1 & = & 4000 \; e^{-2500/T(t)}\\ & k_2 & = & 620000 \; e^{-5000/T(t)}\\ & x(0) &=& (1, 0)^T\\ & T(t) &\in&  [298, 398]\end{array}$}
March 25, 2023, at 11:51 PM by 136.36.4.38 -
Changed line 11 from:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \dot{x}_1 & = & -k_1 x_1^2.\\  & \dot{x}_2 & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{(-2500/T(t))}, \\ & k_2 & = & 620000 \; e^{(-5000/T(t))}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]\end{array}$}
to:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \frac{dx_1}{dt} & = & -k_1 x_1^2.\\  & \frac{dx_2}{dt} & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{-2500/T(t)}, \\ & k_2 & = & 620000 \; e^{-5000/T(t)}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]\end{array}$}
March 25, 2023, at 11:48 PM by 136.36.4.38 -
Changed lines 11-12 from:
{$ \begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \dot{x}_1 & = & -k_1 x_1^2.\\  & \dot{x}_2 & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{(-2500/T(t))}, \\ & k_2 & = & 620000 \; e^{(-5000/T(t))}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]
\end{array} $}
to:
{$\begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \dot{x}_1 & = & -k_1 x_1^2.\\  & \dot{x}_2 & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{(-2500/T(t))}, \\ & k_2 & = & 620000 \; e^{(-5000/T(t))}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]\end{array}$}
March 25, 2023, at 11:47 PM by 136.36.4.38 -
Added lines 1-85:
(:title Batch Reactor Optimization:)
(:keywords optimization, optimal control, benchmark, nonlinear control, dynamic optimization, engineering optimization, Python, GEKKO, differential, algebraic, modeling language:)
(:description Batch reactor optimization of temperature profile solved with Python Gekko.:)

%width=550px%Attach:batch_reactor.png

Batch reactors are pre-loaded with reactants and undergo processing steps to produce a final product in a finite time. Batch reactors are typical for smaller or high-value production processes where the reactor is loaded, processed, and product removed from the reactor to prepare for the next batch. Batch reactors are common in chemical and pharmaceutical manufacture, biofuel production, food and beverage industries, and agricultural production.

This batch reactor problem describes the reaction of reactant ''A'' that produces intermediate ''B'' as the desired product. A second reaction converts ''B'' into an undesirable product ''C''. The batch reactor temperature is continuously controlled with a cooling jacket throughout the time window. A mathematical statement of the optimal control problem is given by

{$ \begin{array}{llcl} \max_{x, u} & x_2(t_f) \\ \mbox{s.t.} & \dot{x}_1 & = & -k_1 x_1^2.\\  & \dot{x}_2 & = & k_1 x_1^2 - k_2 x_2,\\ & k_1 & = & 4000 \; e^{(-2500/T(t))}, \\ & k_2 & = & 620000 \; e^{(-5000/T(t))}, \\ & x(0) &=& (1, 0)^T, \\ & T(t) &\in&  [298, 398]
\end{array} $}

where {`x_1(t)`} is the concentration of ''A'' and {`x_2(t)`} is the concentration of  ''B'' at time-point ''t''. The control function ''T(t)'' is the temperature. The starting time is 0 and end time is 1.

%width=550px%Attach:batch_reactor_solution.png

(:toggle hide solution button show="Batch Reactor Source":)
(:div id=solution:)
(:source lang=python:)
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

m = GEKKO(remote=False)

nt = 501
m.time = np.linspace(0,1,nt)

# Variables
x1 = m.Var(value=1)
x2 = m.Var(value=0)
T = m.MV(value=398,lb=298,ub=398)
T.STATUS = 1
T.DCOST = 1e-6
k1 = m.Intermediate(4000*m.exp(-2500/T))
k2 = m.Intermediate(6.2e5*m.exp(-5000/T))

p = np.zeros(nt)
p[-1] = 1.0
final = m.Param(value=p)

# Equations
m.Equation(x1.dt()==-k1*x1**2)
m.Equation(x2.dt()==k1*x1**2 - k2*x2)

# Objective Function
# maximize final x2
m.Maximize(x2*final)

m.options.IMODE = 6
m.options.NODES = 3
m.solve()

print(f'Optimal x2(tf): {x2.value[-1]:0.4f}')

plt.figure(figsize=(6,4))
plt.subplot(2,1,1)
plt.plot(m.time,x1.value,'k:',\
        lw=2,label=r'$x_1$')
plt.plot(m.time,x2.value,'b-',\
        lw=2,label=r'$x_2$')
plt.plot(m.time[-1],x2.value[-1],'o',\
        color='orange',markersize=5,\
        label=r'$x_2\left(t_f\right)$')
plt.legend(loc='best'); plt.grid()
plt.ylabel('Mole Fraction')
plt.subplot(2,1,2)
plt.plot(m.time,T.value,'r--',\
        lw=2,label='Temperature')
plt.ylabel(r'$T/;(^oC)$'); plt.legend(loc='best')
plt.xlabel('Time'); plt.ylabel('Temperature (K)')
plt.grid(); plt.tight_layout()
plt.savefig('batch_results.png',dpi=300)
plt.show()
(:sourceend:)
(:divend:)

The optimal objective value of the problem is ''x2(tf)'' = 0.6108.

----

%width=200px%Attach:gekko.png

The [[https://gekko.readthedocs.io/en/latest/|Gekko Optimization Suite]] is a machine learning and optimization package in Python for mixed-integer and differential algebraic equations. The GEKKO package is available in Python with '''pip install gekko'''. There are [[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|additional example problems]] for equation solving, optimization, regression, dynamic simulation, model predictive control, and machine learning. [[https://apmonitor.com/wiki/index.php/Main/APMonitorReferences|Cite Gekko Paper]]