Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PDE_models #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ExaModelsExamples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ include("quadrotor.jl")
include("goddard.jl")
include("robot.jl")
include("rocket.jl")
include("bearing.jl")
include("bearing.jl")
include("camshape.jl")
include("elec.jl")
include("steering.jl")
include("pinene.jl")
include("marine.jl")
include("gasoil.jl")
include("pde_models.jl")

const NAMES = filter(names(ExaModelsExamples; all = true)) do x
str = string(x)
Expand Down
74 changes: 74 additions & 0 deletions src/catmix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Catalyst Mixing Problem
# Collocation formulation
# COPS 3.0 - November 2002
# COPS 3.1 - March 2004

function catmix_model(nh; T = Float64, backend = nothing, kwargs...)
ne = 2
nc = 3

tf = 1
h = tf / nh # Final time

rho = [
0.11270166537926,
0.50000000000000,
0.88729833462074,
]
bc = [1.0, 0.0] # Boundary conditions for x
alpha = 0.0 # Smoothing parameter
rho_index = [(i, rho[i]) for i in 1:nc]

c = ExaModels.ExaCore(T; backend = backend)
u = ExaModels.variable(c, nh, nc; lvar = zeros(nh, nc), uvar = ones(nh, nc), start = zeros(nh, nc))
v = ExaModels.variable(c, nh, ne; start = [mod(j, ne) for i in 1:nh, j in 1:ne])
w = ExaModels.variable(c, nh, nc, ne; start = zeros(nh, nc, ne))
pp = ExaModels.variable(c, nh, nc, ne; start = [mod(k, ne) for i in 1:nh, j in 1:nc, k in 1:ne])
Dpp = ExaModels.variable(c, nh, nc, ne; start = zeros(nh, nc, ne))
ppf = ExaModels.variable(c, ne; start = [mod(i,ne) for i in 1:ne])

ExaModels.objective(c, -1.0 + ppf[1] + ppf[2])
ExaModels.objective(c, alpha/h*(u[i+1, j] - u[i, j])^2 for i in 1:nh-1, j in 1:nc)

ExaModels.constraint(
c,
pp[i, k, s] - v[i, s] - h*sum(w[i, j, s]*(rho^j/factorial(j)) for j in 1:nc) for i=1:nh, (k, rho) in rho_index, s=1:ne
)

ExaModels.constraint(
c,
Dpp[i, k, s] - sum(w[i, j, s]*(rho^(j-1)/factorial(j-1)) for j in 1:nc) for i=1:nh, (k, rho) in rho_index, s=1:ne
)

ExaModels.constraint(
c,
ppf[s] - v[nh, s] - h * sum(w[nh, j, s] / factorial(j) for j in 1:nc) for s in 1:ne
)

ExaModels.constraint(
c,
v[i, s] + sum(w[i, j, s] * h / factorial(j) for j in 1:nc) - v[i+1, s] for i in 1:nh-1, s in 1:ne
)



ExaModels.constraint(
c,
Dpp[i,j,1] - u[i,j] * (10.0*pp[i,j,2] - pp[i,j,1]) for i=1:nh, j=1:nc
)

ExaModels.constraint(
c,
Dpp[i,j,2] - u[i,j] * (pp[i,j,1] - 10.0*pp[i,j,2]) + (1 - u[i,j])*pp[i,j,2] for i=1:nh, j=1:nc
)


ExaModels.constraint(
c,
v[1, s] - bc for (s, bc) in [(i, bc[i]) for i in 1:ne]
)

return ExaModels.ExaModel(c; kwargs...)
end


72 changes: 72 additions & 0 deletions src/chain.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Hanging Chain

# Find the chain (of uniform density) of length L suspended between two points with minimal
# potential energy.

# This is problem 4 in the COPS (Version 3) collection of
# E. Dolan and J. More'
# see "Benchmarking Optimization Software with COPS"
# Argonne National Labs Technical Report ANL/MCS-246 (2004)

function chain_model(n; T = Float64, backend = nothing, kwargs...)
nh = max(2, div(n - 4, 4))

L = 4
a = 1
b = 3
tmin = b > a ? 1 / 4 : 3 / 4
tf = 1.0
h = tf / nh

c = ExaModels.ExaCore(T; backend = backend)
u = ExaModels.variable(c, nh + 1; start = [4 * abs(b - a) * (k / nh - tmin) for k in 1:nh+1])
x1 = ExaModels.variable(c, nh + 1; start = [4 * abs(b - a) * k / nh * (1 / 2 * k / nh - tmin) + a for k in 1:nh+1])
x2 = ExaModels.variable(c, nh + 1; start = [(4 * abs(b - a) * k / nh * (1 / 2 * k / nh - tmin) + a) *
(4 * abs(b - a) * (k / nh - tmin)) for k in 1:nh+1])
x3 = ExaModels.variable(c, nh + 1; start = [4 * abs(b - a) * (k / nh - tmin) for k in 1:nh+1])

ExaModels.objective(c, x2[nh + 1])

ExaModels.constraint(
c,
x1[j + 1] - x1[j] - 1 / 2 * h * (u[j] + u[j + 1]) for j in 1:nh
)

ExaModels.constraint(
c,
x1[1] - a
)

ExaModels.constraint(
c,
x1[nh + 1] - b
)

ExaModels.constraint(
c,
x2[1]
)

ExaModels.constraint(
c,
x3[1]
)

ExaModels.constraint(
c,
x3[nh+1] - L
)

ExaModels.constraint(
c,
x2[j + 1] - x2[j] - 1 / 2 * h * (x1[j] * sqrt(1 + u[j]^2) + x1[j + 1] * sqrt(1 + u[j + 1]^2)) for j in 1:nh
)

ExaModels.constraint(
c,
x3[j + 1] - x3[j] - 1 / 2 * h * (sqrt(1 + u[j]^2) + sqrt(1 + u[j + 1]^2)) for j in 1:nh
)

return ExaModels.ExaModel(c; kwargs...)
end

100 changes: 100 additions & 0 deletions src/glider.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Hang Glider Problem
# Trapezoidal formulation
# David Bortz - Summer 1998
# COPS 2.0 - September 2000
# COPS 3.0 - November 2002
# COPS 3.1 - March 2004

function glider_model(nh; T = Float64, backend = nothing, kwargs...)
# Design parameters
x_0 = 0.0
y_0 = 1000.0
y_f = 900.0
vx_0 = 13.23
vx_f = 13.23
vy_0 = -1.288
vy_f = -1.288
u_c = 2.5
r_0 = 100.0
m = 100.0
g = 9.81
c0 = 0.034
c1 = 0.069662
S = 14.0
rho = 1.13
cL_min = 0.0
cL_max = 1.4

c = ExaModels.ExaCore(T; backend = backend)
t_f = ExaModels.variable(c, 1; lvar = 0, start = 1)
x = ExaModels.variable(c, nh+1; lvar = zeros(nh+1), start = [x_0 + vx_0*(k/nh) for k in 0:nh])
y = ExaModels.variable(c, nh+1; start = [y_0 + (k/nh)*(y_f - y_0) for k in 0:nh])
vx = ExaModels.variable(c, nh+1; lvar = zeros(nh+1), start = fill(vx_0, nh+1))
vy = ExaModels.variable(c, nh+1; start = fill(vy_0, nh+1))
cL = ExaModels.variable(c, nh+1; lvar = fill(cL_min,nh+1), uvar = fill(cL_max, nh+1), start = fill(cL_max/2, nh+1))

ExaModels.objective(c, -x[nh+1])

ExaModels.constraint(
c,
x[j] - (x[j-1] + 0.5/nh * (vx[j] + vx[j-1]) * t_f[1]) for j in 2:nh+1
)

ExaModels.constraint(
c,
y[j] - (y[j-1] + 0.5 * t_f[1]/nh * (vy[j] + vy[j-1])) for j in 2:nh+1
)

ExaModels.constraint(
c,
vx[j] - (vx[j-1] + 0.5 * t_f[1]/nh * (vx_dot[j] + vx_dot[j-1])) for j in 2:nh+1
)

ExaModels.constraint(
c,
vy[j] - (vy[j-1] + 0.5 * t_f[1]/nh * (vy_dot[j] + vy_dot[j-1])) for j in 2:nh+1
)

ExaModels.constraint(
c,
x[1] - x_0
)

ExaModels.constraint(
c,
y[1] - y_0
)

ExaModels.constraint(
c,
y[nh+1] - y_f
)

ExaModels.constraint(
c,
vx[1] - vx_0
)

ExaModels.constraint(
c,
vx[nh+1] - vx_f
)

ExaModels.constraint(
c,
vy[1] - vy_0
)

ExaModels.constraint(
c,
vy[nh+1] - vy_f
)

ExaModels.Examodel(c,; kwargs...)
end


println("Running tests...")
using NLPModelsIpopt, ExaModels, MadNLP

result = madnlp((glider_model(200));print_level = MadNLP.INFO )
127 changes: 127 additions & 0 deletions src/methanol.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Methanol-to-Hydrocarbons Problem
# Collocation formulation
# Michael Merritt - Summer 2000
# COPS 2.0 - September 2000
# COPS 3.0 - November 2002
# COPS 3.1 - March 2004

function methanol_model(nh; T = Float64, backend = nothing, kwargs...)
ne = 3
np = 5
nc = 3
nm = 17

rho = [0.11270166537926, 0.5, 0.88729833462074]
# times at which observations made
tau = [
0,
0.050,
0.065,
0.080,
0.123,
0.233,
0.273,
0.354,
0.397,
0.418,
0.502,
0.553,
0.681,
0.750,
0.916,
0.937,
1.122,
]
tf = tau[nm] # ODEs defined in [0,tf]
h = tf / nh # uniform interval length
t = [(i-1)*h for i in 1:nh+1] # partition

# itau[i] is the largest integer k with t[k] <= tau[i]
itau = Int[min(nh, floor(tau[i]/h)+1) for i in 1:nm]

# Concentrations
z = [
1.0000 0 0;
0.7085 0.1621 0.0811;
0.5971 0.1855 0.0965;
0.5537 0.1989 0.1198;
0.3684 0.2845 0.1535;
0.1712 0.3491 0.2097;
0.1198 0.3098 0.2628;
0.0747 0.3576 0.2467;
0.0529 0.3347 0.2884;
0.0415 0.3388 0.2757;
0.0261 0.3557 0.3167;
0.0208 0.3483 0.2954;
0.0085 0.3836 0.2950;
0.0053 0.3611 0.2937;
0.0019 0.3609 0.2831;
0.0018 0.3485 0.2846;
0.0006 0.3698 0.2899;
]
con1_matrix = [(j, s, itau[j], tau[j], z[j,s], t[itau[j]]) for j in 1:nm, s in 1:ne]
bc = [1.0, 0.0, 0.0]

# Starting-value
v0 = zeros(nh, ne)
for i in 1:itau[1], s in 1:ne
v0[i, s] = bc[s]
end
for j in 2:nm, i in itau[j-1]+1:itau[j], s in 1:ne
v0[i, s] = z[j, s]
end
for i in itau[nm]+1:nh, s in 1:ne
v0[i, s] = z[nm, s]
end
v0 .= 0.001

c = ExaModels.ExaCore(T; backend = backend)
theta = ExaModels.variable(c, np; lvar = 0, start = fill(1, np))
v = ExaModels.variable(c, nh, ne; start = v0)
w = ExaModels.variable(c, nh, nc, ne; start = 0)
uc = ExaModels.variable(c, nh, nc, ne; start = [v0[i,s] for i=1:nh, j=1:nc, s=1:ne])
Duc = ExaModels.variable(c, nh, nc, ne; start = 0)

ExaModels.objective(c, (v[itau,s] + sum(w[itau,k,s]*(tau-t)^k/(factorial(k)*h^(k-1)) for k in 1:nc) - z)^2 for (j,s,itau,tau,z,t) in con1_matrix)

ExaModels.constraint(
c,
uc[i, j, s] - v[i,s] - h*sum(w[i,k,s]*(rho^k/factorial(k)) for k in 1:nc) for i=1:nh, (j,rho) in [(j, rho[j]) for j in 1:nc], s=1:ne
)

ExaModels.constraint(
c,
Duc[i, j, s] - sum(w[i,k,s]*(rho^(k-1)/factorial(k-1)) for k in 1:nc) for i=1:nh, (j,rho) in [(j, rho[j]) for j in 1:nc], s=1:ne
)

ExaModels.constraint(
c,
v[1, s] - bc for (s, bc) in [(s, bc[s]) for s in 1:ne]

)

ExaModels.constraint(
c,
v[i, s] + sum(w[i, j, s]*h/factorial(j) for j in 1:nc) - v[i+1, s] for i=1:nh-1, s=1:ne
)

ExaModels.constraint(
c,
Duc[i,j,1] + ((2*theta[2] - (theta[1]*uc[i,j,2])/((theta[2]+theta[5])*uc[i,j,1]+uc[i,j,2]) +
theta[3] + theta[4])*uc[i,j,1]) for i=1:nh, j=1:nc
)

ExaModels.constraint(
c,
Duc[i,j,2] - ((theta[1]*uc[i,j,1]*(theta[2]*uc[i,j,1]-uc[i,j,2]))/ ((theta[2]+theta[5])*uc[i,j,1]+uc[i,j,2]) +
theta[3]*uc[i,j,1]) for i=1:nh, j=1:nc
)

ExaModels.constraint(
c,
Duc[i,j,3] - ((theta[1]*uc[i,j,1]*(uc[i,j,2]+theta[5]*uc[i,j,1]))/ ((theta[2]+theta[5])*uc[i,j,1]+uc[i,j,2]) +
theta[4]*uc[i,j,1]) for i=1:nh, j=1:nc
)

return ExaModels.ExaModel(c; kwargs...)
end
Loading