Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoffrin committed Feb 1, 2024
2 parents aa22d05 + bdfc24d commit 415b962
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 24 deletions.
12 changes: 8 additions & 4 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "00bfc230370ab2d06ed7ae6128542d1a85a5a113"
project_hash = "2b196f4165a07d56342f51102f8b4a3a616731b5"

[[deps.ADNLPModels]]
deps = ["ColPack", "ForwardDiff", "LinearAlgebra", "NLPModels", "Requires", "ReverseDiff", "SparseArrays"]
Expand Down Expand Up @@ -444,22 +444,26 @@ uuid = "90fa49ef-747e-5e6f-a989-263ba693cf1a"
version = "0.5.2"

[[deps.ExaModels]]
deps = ["NLPModels", "SolverCore"]
git-tree-sha1 = "054b99d8f8d19a81a1fbbad74223af16f8012f2a"
deps = ["NLPModels", "Printf", "SolverCore"]
git-tree-sha1 = "606d0f6c1bcdda9ec7841a4892337b00814e2334"
uuid = "1037b233-b668-4ce9-9b63-f9f681f55dd2"
version = "0.4.2"
version = "0.5.0"

[deps.ExaModels.extensions]
ExaModelsAMDGPU = "AMDGPU"
ExaModelsCUDA = "CUDA"
ExaModelsJuMP = "JuMP"
ExaModelsKernelAbstractions = "KernelAbstractions"
ExaModelsMOI = "MathOptInterface"
ExaModelsOneAPI = "oneAPI"
ExaModelsSpecialFunctions = "SpecialFunctions"

[deps.ExaModels.weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
ADNLPModels = "0.7"
ExaModels = "0.4"
ExaModels = "0.5"
Ipopt = "1"
JuMP = "1.15"
NLPModelsIpopt = "0.10"
Expand Down
131 changes: 131 additions & 0 deletions debug/optim-jump.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
time_start = time()

using PowerModels
using JuMP
using Optim

pkg_load_time = time() - time_start


time_start = time()

file_name = "../data/pglib_opf_case5_pjm.m"
#file_name = "../data/pglib_opf_case118_ieee.m"

data = PowerModels.parse_file(file_name)
PowerModels.standardize_cost_terms!(data, order=2)
PowerModels.calc_thermal_limits!(data)
ref = PowerModels.build_ref(data)[:it][:pm][:nw][0]

data_load_time = time() - time_start


time_start = time()

model = JuMP.Model()

@variable(model, va[i in keys(ref[:bus])])
@variable(model, ref[:bus][i]["vmin"] <= vm[i in keys(ref[:bus])] <= ref[:bus][i]["vmax"], start=1.0)

@variable(model, ref[:gen][i]["pmin"] <= pg[i in keys(ref[:gen])] <= ref[:gen][i]["pmax"], start=(ref[:gen][i]["pmin"] + ref[:gen][i]["pmax"])/2)
@variable(model, ref[:gen][i]["qmin"] <= qg[i in keys(ref[:gen])] <= ref[:gen][i]["qmax"], start=(ref[:gen][i]["qmin"] + ref[:gen][i]["qmax"])/2)

@variable(model, -ref[:branch][l]["rate_a"] <= p[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"])
@variable(model, -ref[:branch][l]["rate_a"] <= q[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"])


@objective(model, Min, sum(gen["cost"][1]*pg[i]^2 + gen["cost"][2]*pg[i] + gen["cost"][3] for (i,gen) in ref[:gen]))


for (i,bus) in ref[:ref_buses]
@constraint(model, va[i] == 0)
end

for (i,bus) in ref[:bus]
bus_loads = [ref[:load][l] for l in ref[:bus_loads][i]]
bus_shunts = [ref[:shunt][s] for s in ref[:bus_shunts][i]]

@constraint(model,
sum(p[a] for a in ref[:bus_arcs][i]) ==
sum(pg[g] for g in ref[:bus_gens][i]) -
sum(load["pd"] for load in bus_loads) -
sum(shunt["gs"] for shunt in bus_shunts)*vm[i]^2
)

@constraint(model,
sum(q[a] for a in ref[:bus_arcs][i]) ==
sum(qg[g] for g in ref[:bus_gens][i]) -
sum(load["qd"] for load in bus_loads) +
sum(shunt["bs"] for shunt in bus_shunts)*vm[i]^2
)
end

# Branch power flow physics and limit constraints
for (i,branch) in ref[:branch]
f_idx = (i, branch["f_bus"], branch["t_bus"])
t_idx = (i, branch["t_bus"], branch["f_bus"])

p_fr = p[f_idx] # p_fr is a reference to the optimization variable p[f_idx]
q_fr = q[f_idx] # q_fr is a reference to the optimization variable q[f_idx]
p_to = p[t_idx] # p_to is a reference to the optimization variable p[t_idx]
q_to = q[t_idx] # q_to is a reference to the optimization variable q[t_idx]

vm_fr = vm[branch["f_bus"]] # vm_fr is a reference to the optimization variable vm on the from side of the branch
vm_to = vm[branch["t_bus"]] # vm_to is a reference to the optimization variable vm on the to side of the branch
va_fr = va[branch["f_bus"]] # va_fr is a reference to the optimization variable va on the from side of the branch
va_to = va[branch["t_bus"]] # va_fr is a reference to the optimization variable va on the to side of the branch

g, b = PowerModels.calc_branch_y(branch)
tr, ti = PowerModels.calc_branch_t(branch)
tm = tr^2 + ti^2
g_fr = branch["g_fr"]
b_fr = branch["b_fr"]
g_to = branch["g_to"]
b_to = branch["b_to"]

# From side of the branch flow
@constraint(model, p_fr == (g+g_fr)/tm*vm_fr^2 + (-g*tr+b*ti)/tm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-b*tr-g*ti)/tm*(vm_fr*vm_to*sin(va_fr-va_to)) )
@constraint(model, q_fr == -(b+b_fr)/tm*vm_fr^2 - (-b*tr-g*ti)/tm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-g*tr+b*ti)/tm*(vm_fr*vm_to*sin(va_fr-va_to)) )

# To side of the branch flow
@constraint(model, p_to == (g+g_to)*vm_to^2 + (-g*tr-b*ti)/tm*(vm_to*vm_fr*cos(va_to-va_fr)) + (-b*tr+g*ti)/tm*(vm_to*vm_fr*sin(va_to-va_fr)) )
@constraint(model, q_to == -(b+b_to)*vm_to^2 - (-b*tr+g*ti)/tm*(vm_to*vm_fr*cos(va_fr-va_to)) + (-g*tr-b*ti)/tm*(vm_to*vm_fr*sin(va_to-va_fr)) )

# Voltage angle difference limit
@constraint(model, branch["angmin"] <= va_fr - va_to <= branch["angmax"])

# Apparent power limit, from side and to side
@constraint(model, p_fr^2 + q_fr^2 <= branch["rate_a"]^2)
@constraint(model, p_to^2 + q_to^2 <= branch["rate_a"]^2)
end


set_optimizer(model, Optim.Optimizer)
set_optimizer_attribute(model, "method", IPNewton())
set_optimizer_attribute(model, "show_trace", true)

model_build_time = time() - time_start


time_start = time()

JuMP.optimize!(model)
cost = JuMP.objective_value(model)
feasible = JuMP.termination_status(model)

solve_time = time() - time_start


println("")
println("\033[1mSummary\033[0m")
println(" case..........: $(file_name)")
println(" cost..........: $(round(Int, cost))")
println(" feasible......: $(feasible)")
println(" primal status.: $(JuMP.primal_status(model))")
println(" dual status...: $(JuMP.dual_status(model))")
println(" pkg time......: $(pkg_load_time)")
println(" data time.....: $(data_load_time)")
println(" build time....: $(model_build_time)")
println(" solve time....: $(solve_time)")
println("")

41 changes: 22 additions & 19 deletions examodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,14 @@ function solve_opf(file_name)
c13 = ExaModels.constraint!(w, c9, g.bus => -pg[g.i] for g in data.gen)
c14 = ExaModels.constraint!(w, c10, g.bus => -qg[g.i] for g in data.gen)

model = ExaModels.ExaModel(w)
model = ExaModels.TimedNLPModel(
ExaModels.ExaModel(w)
)

model_build_time = time() - time_model_start


time_solve_start = time()

result = NLPModelsIpopt.ipopt(model)

cost = result.objective
Expand All @@ -206,14 +207,14 @@ function solve_opf(file_name)
solve_time = time() - time_solve_start
total_time = time() - time_data_start

# TODO: once a built-in timer is implemented in ExaModels, report callback timing

# total_callback_time =
# nlp_block.evaluator.eval_objective_timer +
# nlp_block.evaluator.eval_objective_gradient_timer +
# nlp_block.evaluator.eval_constraint_timer +
# nlp_block.evaluator.eval_constraint_jacobian_timer +
# nlp_block.evaluator.eval_hessian_lagrangian_timer
total_callback_time =
model.stats.obj_time +
model.stats.grad_time +
model.stats.cons_time +
model.stats.jac_coord_time +
model.stats.hess_coord_time +
model.stats.jac_structure_time +
model.stats.hess_structure_time

println("")
println("\033[1mSummary\033[0m")
Expand All @@ -226,15 +227,15 @@ function solve_opf(file_name)
println(" data time.: $(data_load_time)")
println(" build time: $(model_build_time)")
println(" solve time: $(solve_time)")
# println(" callbacks: $(total_callback_time)")
println(" callbacks: $(total_callback_time)")
println("")
println(" callbacks time:")
println(" * obj.....: $(model.stats.obj_time)")
println(" * grad....: $(model.stats.grad_time)")
println(" * cons....: $(model.stats.cons_time)")
println(" * jac.....: $(model.stats.jac_coord_time + model.stats.jac_structure_time)")
println(" * hesslag.: $(model.stats.hess_coord_time + model.stats.hess_structure_time)")
println("")
# println(" callbacks time:")
# println(" * obj.....: $(nlp_block.evaluator.eval_objective_timer)")
# println(" * grad....: $(nlp_block.evaluator.eval_objective_gradient_timer)")
# println(" * cons....: $(nlp_block.evaluator.eval_constraint_timer)")
# println(" * jac.....: $(nlp_block.evaluator.eval_constraint_jacobian_timer)")
# println(" * hesslag.: $(nlp_block.evaluator.eval_hessian_lagrangian_timer)")
# println("")

va_sol = ExaModels.solution(result, va)
va_dict = Dict("va_$(b.j)" => va_sol[b.i] for (i,b) in enumerate(data.bus))
Expand Down Expand Up @@ -264,7 +265,7 @@ function solve_opf(file_name)
"time_data" => data_load_time,
"time_build" => model_build_time,
"time_solve" => solve_time,
# "time_callbacks" => total_callback_time,
"time_callbacks" => total_callback_time,
"solution" => Dict(
va_dict...,
vm_dict...,
Expand All @@ -280,3 +281,5 @@ write_out_tuple((i,j,k)) = "$(i)_$(j)_$(k)"
if isinteractive() == false
solve_opf("$(@__DIR__)/data/opf_warmup.m")
end


0 comments on commit 415b962

Please sign in to comment.