Batch reactor (JuMP)

From mintOC
Revision as of 14:58, 19 January 2016 by FelixMueller (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is an implementation of the Batch reactor problem using JuMP. The problem was discretized and the ODEs are solved using the explicit Euler method. Although not necessary in JuMP the code was divided into three parts (following AMPL) - model file, data file and run file. The run file calls the other files and performs additional tasks such as printing results.

Model file ("batchreactor_mod.jl"):

#JuMP implementation of batch reactor example using collocation
#mod file
#declaring the model
m = Model()
#defining variables
@defVar(m, x[ii=1:n_x, tt=1:N])
@defVar(m, L_control <= u[jj = 1:n_u, tt=1:N] <= U_control)
#setting objective function
@setObjective(m, Max, x[2,N])
#adding constraints
#starting values
@addConstraint(m, starting_value[ii=1:n_x], x[ii,1] == x_start[ii])
#ODE - solved with explicit euler method (i.e. x_k+1 = x_k + stepsize * f(x_k, t_k))
@defNLExpr(k_1[tt=1:N-1], 4000 * exp(-2500/u[1,tt]))
@defNLExpr(k_2[tt=1:N-1], 620000 * exp(-5000/u[1,tt]))
@addNLConstraint(m, ODE_nonlin[ii=1, tt=1:N-1], x[ii,tt+1] - x[ii,tt] - step_size * (-k_1[tt] * x[1,tt]^2) == 0)
@addNLConstraint(m, ODE_nonlin[ii=2, tt=1:N-1], x[ii,tt+1] - x[ii,tt] - step_size * (k_1[tt] * x[1,tt]^2 - k_2[tt] * x[2,tt]) == 0)

Data file ("batchreactor_dat.jl"):

#JuMP implementation of batch reactor example using collocation
#dat file
#number of states
n_x = 2;
#number of controls
n_u = 1;
#number of shooting intervals / discretization points
N = 300;
#starting / end time
t_start = 0;
t_end = 1;
#time discretization
step_size = (t_end - t_start)/N;
#starting value
x_start = [1, 0];
#bounds for control
L_control = 298;
U_control = 398;

Run file ("batchreactor_run.jl"):

#JuMP implementation of batch reactor example using collocation
#run file
using JuMP;
using Ipopt;
println("Time used for data")
@time include("batchreactor_dat.jl")
println("Time used for modeling")
@time include("batchreactor_mod.jl")
println("Time used for solving")
@time solve(m);
#printing results
println("Optimal objective value is: ", getObjectiveValue(m))
println("Optimal Solution is: \n", getValue(x), getValue(u))