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

Minor formatting #9

Merged
merged 1 commit into from
Apr 19, 2024
Merged
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
33 changes: 17 additions & 16 deletions src/glider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,33 @@ function glider_model(nh)

model = Model()

@variable(model, step >= 0.0, start=1/nh)
# State variables
@variable(model, 0.0 <= x[k=0:nh], start=x_0 + vx_0*(k/nh))
@variable(model, y[k=0:nh], start=y_0 + (k/nh)*(y_f - y_0))
@variable(model, 0.0 <= vx[k=0:nh], start=vx_0)
@variable(model, vy[k=0:nh], start=vy_0)
# Control variables
@variable(model, cL_min <= cL[k=0:nh] <= cL_max, start=cL_max/2.0)
@variables(model, begin
0 <= t_f, (start=1.0)
0.0 <= x[k=0:nh], (start=x_0 + vx_0*(k/nh))
y[k=0:nh], (start=y_0 + (k/nh)*(y_f - y_0))
0.0 <= vx[k=0:nh], (start=vx_0)
vy[k=0:nh], (start=vy_0)
cL_min <= cL[k=0:nh] <= cL_max, (start=cL_max/2.0)
end)

@objective(model, Max, x[nh])

@expressions(model, begin
r[i=0:nh], (x[i]/r_0 - 2.5)^2
u[i=0:nh], u_c*(1 - r[i])*exp(-r[i])
w[i=0:nh], vy[i] - u[i]
v[i=0:nh], sqrt(vx[i]^2 + w[i]^2)
D[i=0:nh], 0.5*(c0+c1*cL[i]^2)*rho*S*v[i]^2
L[i=0:nh], 0.5*cL[i]*rho*S*v[i]^2
step, t_f / nh
r[i=0:nh], (x[i]/r_0 - 2.5)^2
u[i=0:nh], u_c*(1 - r[i])*exp(-r[i])
w[i=0:nh], vy[i] - u[i]
v[i=0:nh], sqrt(vx[i]^2 + w[i]^2)
D[i=0:nh], 0.5*(c0+c1*cL[i]^2)*rho*S*v[i]^2
L[i=0:nh], 0.5*cL[i]*rho*S*v[i]^2
vx_dot[i=0:nh], (-L[i]*(w[i]/v[i]) - D[i]*(vx[i]/v[i]))/m
vy_dot[i=0:nh], (L[i]*(vx[i]/v[i]) - D[i]*(w[i]/v[i]))/m - g
end)

# Dynamics
@constraints(model, begin
x_eqn[j=1:nh], x[j] == x[j-1] + 0.5 * step * (vx[j] + vx[j-1])
y_eqn[j=1:nh], y[j] == y[j-1] + 0.5 * step * (vy[j] + vy[j-1])
x_eqn[j=1:nh], x[j] == x[j-1] + 0.5 * step * (vx[j] + vx[j-1])
y_eqn[j=1:nh], y[j] == y[j-1] + 0.5 * step * (vy[j] + vy[j-1])
vx_eqn[j=1:nh], vx[j] == vx[j-1] + 0.5 * step * (vx_dot[j] + vx_dot[j-1])
vy_eqn[j=1:nh], vy[j] == vy[j-1] + 0.5 * step * (vy_dot[j] + vy_dot[j-1])
end)
Expand Down
84 changes: 27 additions & 57 deletions src/robot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,38 @@ function robot_model(nh)

model = Model()

@variable(model, 0 <= rho[k=1:nh+1] <= L, start=rho0)
@variable(model, -pi <= the[k=1:nh+1] <= pi, start=2*pi/3*(k/nh)^2)
@variable(model, 0 <= phi[k=1:nh+1] <= pi, start=phi0)
# Derivatives
@variable(model, rho_dot[k=1:nh+1], start=0.0)
@variable(model, the_dot[k=1:nh+1], start=4*pi/3*(k/nh))
@variable(model, phi_dot[k=1:nh+1], start=0.0)
# Control
@variable(model, -max_u_rho <= u_rho[1:nh+1] <= max_u_rho, start=0.0)
@variable(model, -max_u_the <= u_the[1:nh+1] <= max_u_the, start=0.0)
@variable(model, -max_u_phi <= u_phi[1:nh+1] <= max_u_phi, start=0.0)
# Steps and final time
@variable(model, step >= 0.0)
# The moments of inertia
@variable(model, I_the[1:nh+1], start=((L-rho0)^3+rho0^3)*(sin(phi0))^2/3.0)
@variable(model, I_phi[1:nh+1], start=((L-rho0)^3+rho0^3)/3.0)
@variables(model, begin
0 <= rho[k=1:nh+1] <= L, (start=rho0)
-pi <= the[k=1:nh+1] <= pi, (start=2*pi/3*(k/nh)^2)
0 <= phi[k=1:nh+1] <= pi, (start=phi0)
rho_dot[k=1:nh+1], (start=0.0)
the_dot[k=1:nh+1], (start=4*pi/3*(k/nh))
phi_dot[k=1:nh+1], (start=0.0)
-max_u_rho <= u_rho[1:nh+1] <= max_u_rho, (start=0.0)
-max_u_the <= u_the[1:nh+1] <= max_u_the, (start=0.0)
-max_u_phi <= u_phi[1:nh+1] <= max_u_phi, (start=0.0)
tf >= 0.0, (start=1.0)
end)

@objective(model, Min, step * nh)
@objective(model, Min, tf)

# Physical equations
@constraint(
model,
[i=1:nh+1],
I_the[i] == ((L-rho[i])^3+rho[i]^3)*(sin(phi[i]))^2/3.0
)
@constraint(
model,
[i=1:nh+1],
I_phi[i] == ((L-rho[i])^3+rho[i]^3)/3.0
)
@expressions(model, begin
step, tf /nh
I_the[i=1:nh+1], ((L-rho[i])^3+rho[i]^3)*(sin(phi[i]))^2/3.0
I_phi[i=1:nh+1], ((L-rho[i])^3+rho[i]^3)/3.0
end)

# Dynamics
@constraint(
model,
[j=2:nh+1],
rho[j] == rho[j-1] + 0.5 * step * (rho_dot[j] + rho_dot[j-1]),
)
@constraint(
model,
[j=2:nh+1],
phi[j] == phi[j-1] + 0.5 * step * (phi_dot[j] + phi_dot[j-1]),
)
@constraint(
model,
[j=2:nh+1],
the[j] == the[j-1] + 0.5 * step * (the_dot[j] + the_dot[j-1]),
)
@constraints(model, begin
[j=2:nh+1], rho[j] == rho[j-1] + 0.5 * step * (rho_dot[j] + rho_dot[j-1])
[j=2:nh+1], phi[j] == phi[j-1] + 0.5 * step * (phi_dot[j] + phi_dot[j-1])
[j=2:nh+1], the[j] == the[j-1] + 0.5 * step * (the_dot[j] + the_dot[j-1])
[j=2:nh+1], rho_dot[j] == rho_dot[j-1] + 0.5 * step * (u_rho[j] + u_rho[j-1]) / L
[j=2:nh+1], the_dot[j] == the_dot[j-1] + 0.5 * step * (u_the[j] / I_the[j] + u_the[j-1] / I_the[j-1])
[j=2:nh+1], phi_dot[j] == phi_dot[j-1] + 0.5 * step * (u_phi[j] / I_phi[j] + u_phi[j-1] / I_phi[j-1])
end)

@constraint(
model,
[j=2:nh+1],
rho_dot[j] == rho_dot[j-1] + 0.5 * step * (u_rho[j] + u_rho[j-1]) / L
)
@constraint(
model,
[j=2:nh+1],
the_dot[j] == the_dot[j-1] + 0.5 * step * (u_the[j] / I_the[j] + u_the[j-1] / I_the[j-1])
)
@constraint(
model,
[j=2:nh+1],
phi_dot[j] == phi_dot[j-1] + 0.5 * step * (u_phi[j] / I_phi[j] + u_phi[j-1] / I_phi[j-1])
)
# Boundary condition
@constraints(
model, begin
Expand Down
12 changes: 6 additions & 6 deletions src/rocket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function rocket_model(nh)
model = Model()

@variables(model, begin
0.0 <= h[i=0:nh], (start=1.0)
0.0 <= v[i=0:nh], (start=i/nh*(1.0 - i/nh))
m_f <= m[i=0:nh] <= m_0, (start=(m_f - m_0)*(i/nh) + m_0)
1.0 <= h[i=0:nh], (start=1.0)
0.0 <= v[i=0:nh], (start=i/nh*(1.0 - i/nh))
m_f <= m[i=0:nh] <= m_0, (start=(m_f - m_0)*(i/nh) + m_0)
0.0 <= T[i=0:nh] <= T_max, (start=T_max/2.0)
0.0 <= step, (start=1/nh)
0.0 <= step, (start=1/nh)
end)

@expressions(model, begin
D[i=0:nh], D_c*v[i]^2*exp(-h_c*(h[i] - h_0))/h_0
g[i=0:nh], g_0 * (h_0 / h[i])^2
D[i=0:nh], D_c*v[i]^2*exp(-h_c*(h[i] - h_0))/h_0
g[i=0:nh], g_0 * (h_0 / h[i])^2
dh[i=0:nh], v[i]
dv[i=0:nh], (T[i] - D[i] - m[i]*g[i]) / m[i]
dm[i=0:nh], -T[i]/c
Expand Down
Loading