Skip to content
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ model, vars, cons = opf_model(
form = :polar
)
result = madnlp(model; tol=1e-6)


#Alternatively, solve using DC linearization
model, vars, cons = dcopf_model(
"pglib_opf_case118_ieee.m";
backend = CUDABackend()
)
result = madnlp(model; tol=1e-6)
```

### Security-constrained optimal power flow
Expand Down
13 changes: 7 additions & 6 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ function c_comp(pstd, pstc)
end

# DCOPF
function c_ohms_law_dcopf(br, pf, va_f, va_t, b_val)
function c_ohms_law_dcopf(br, pf, va_f, va_t)
r2_x2 = br.br_r^2 + br.br_x^2
b_val = -br.br_x / r2_x2
return -b_val * (va_f - va_t) - pf
end

function dcopf_branch_b(br)
r2_x2 = br.br_r^2 + br.br_x^2
b_val = -br.br_x / r2_x2
return b_val
end
function c_active_power_balance_dc(b)
return b.pd + b.gs
end

51 changes: 39 additions & 12 deletions src/dcopf.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function build_dcopf(data; backend = nothing, T = Float64, core = nothing, kwargs...)
function dummy_extension(core, vars, cons)
return (;), (;)
end

function build_dcopf(data, user_callback; backend = nothing, T = Float64, core = nothing, kwargs...)

core = isnothing(core) ? ExaCore(T; backend = backend) : core
T, backend = typeof(core).parameters[1], core.backend

va = variable(core, length(data.bus))
pg = variable(core, length(data.gen); lvar = data.pmin, uvar = data.pmax)

pd = parameter(core, map(b->b.pd, data.bus))
bs = parameter(core, map(dcopf_branch_b, data.branch))

pf = variable(
core,
Expand All @@ -21,7 +24,7 @@ function build_dcopf(data; backend = nothing, T = Float64, core = nothing, kwarg

c_ohms_law = constraint(
core,
c_ohms_law_dcopf(br, pf[br.i], va[br.f_bus], va[br.t_bus], bs[br.i])
c_ohms_law_dcopf(br, pf[br.i], va[br.f_bus], va[br.t_bus])
for br in data.branch
)

Expand All @@ -34,7 +37,8 @@ function build_dcopf(data; backend = nothing, T = Float64, core = nothing, kwarg

c_active_power_balance = constraint(
core,
pd[b.i] + b.gs for b in data.bus
c_active_power_balance_dc(b) for b in data.bus

)
constraint!(core, c_active_power_balance, g.bus => -pg[g.i] for g in data.gen)
constraint!(core, c_active_power_balance, br.f_bus => pf[br.i] for br in data.branch)
Expand All @@ -53,24 +57,47 @@ function build_dcopf(data; backend = nothing, T = Float64, core = nothing, kwarg
c_active_power_balance = c_active_power_balance,
)

params = (
pd = pd,
bs = bs,
)

model = ExaModel(core; kwargs...)
vars2, cons2 = user_callback(core, vars, cons)
model =ExaModel(core; kwargs...)

vars = (;vars..., vars2...)
cons = (;cons..., cons2...)

return model, vars, cons, params
return model, vars, cons
end


"""
dcopf_model(filename; backend, T, user_callback)

Return `ExaModel`, variables, and constraints for a static linearized DC Optimal Power Flow (DCOPF) problem from the given file.

# Arguments
- `filename::String`: Path to the data file.
- `backend`: The solver backend to use. Default if nothing.
- `T`: The numeric type to use (default is `Float64`).
- `user_callback`: User function that extends the model
- `kwargs...`: Additional keyword arguments passed to the model builder.

# Returns
A vector `(model, variables, constraints)`:
- `model`: An `ExaModel` object.
- `variables`: NamedTuple of model variables.
- `constraints`: NamedTuple of model constraints.
"""

function dcopf_model(
filename;
backend = nothing,
T = Float64,

user_callback = dummy_extension,
kwargs...,
)
data = parse_ac_power_data(filename)
data = convert_data(data, backend)

return build_dcopf(data; backend = backend, T = T, kwargs...)
return build_dcopf(data, user_callback; backend = backend, T = T, kwargs...)

end
Loading