Difference between revisions of "Batch reactor (JuMP)"

From mintOC
Jump to: navigation, search
(added JuMP code for batch reactor)
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
This is an implementation of the batch reactor problem using JuMP.
+
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.
 
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.
 
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"):
 
Model file ("batchreactor_mod.jl"):
<source lang="Julia">
+
<source lang="AMPL">
 
#JuMP implementation of batch reactor example using collocation
 
#JuMP implementation of batch reactor example using collocation
 
#mod file
 
#mod file
Line 32: Line 32:
  
 
Data file ("batchreactor_dat.jl"):
 
Data file ("batchreactor_dat.jl"):
<source lang="Julia">
+
<source lang="AMPL">
 
#JuMP implementation of batch reactor example using collocation
 
#JuMP implementation of batch reactor example using collocation
 
#dat file
 
#dat file
Line 60: Line 60:
  
 
Run file ("batchreactor_run.jl"):
 
Run file ("batchreactor_run.jl"):
<source lang="Julia">
+
<source lang="AMPL">
 
#JuMP implementation of batch reactor example using collocation
 
#JuMP implementation of batch reactor example using collocation
 
#run file
 
#run file
Line 85: Line 85:
 
</source>
 
</source>
  
[[Category: JuMP]]
+
[[Category: Julia/JuMP]]

Latest revision as of 14:58, 19 January 2016

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;
 
##discretization
#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("----------------------------------------------------")
println("Time used for data")
@time include("batchreactor_dat.jl")
println("----------------------------------------------------")
println("Time used for modeling")
@time include("batchreactor_mod.jl")
println("----------------------------------------------------")
println("Time used for solving")
@time solve(m);
 
println("----------------------------------------------------")
println("----------------------------------------------------")
 
#printing results
println("Optimal objective value is: ", getObjectiveValue(m))
println("Optimal Solution is: \n", getValue(x), getValue(u))