Skip to content

Commit

Permalink
Add Support for Julia v0.7/v1.0 (#106)
Browse files Browse the repository at this point in the history
Updates ThreePhasePowerModels to support Julia v0.7/v1.0 in addition to v0.6.
  • Loading branch information
ccoffrin authored and pseudocubic committed Nov 26, 2018
1 parent 0bb5c24 commit 1869fe0
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 143 deletions.
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ julia:
- 0.6
- 0.7
- 1.0
matrix:
allow_failures:
- julia: 0.7
- julia: 1.0
cache:
directories:
- /home/travis/.julia
Expand All @@ -21,4 +17,12 @@ before_install:
- julia -e '(VERSION >= v"0.7" && using Pkg); Pkg.update()'
after_success:
- julia -e '(VERSION >= v"0.7" && using Pkg); Pkg.add("Coverage"); cd(Pkg.dir("ThreePhasePowerModels")); using Coverage; Codecov.submit(process_folder())'
- julia -e 'if VERSION >= v"0.7" using Pkg; Pkg.add("Documenter"); import ThreePhasePowerModels; include(joinpath(dirname(pathof(ThreePhasePowerModels)), "..", "docs", "make.jl")) end'
jobs:
include:
- stage: "Documentation"
julia: 1.0
os: linux
script:
- julia --project=docs/ -e 'using Pkg; Pkg.instantiate(); Pkg.develop(PackageSpec(path=pwd()))'
- julia --project=docs/ docs/make.jl
after_success: skip
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ThreePhasePowerModels.jl Change Log
- Fixed parsing OpenDSS files containing redirect/compile/buscoords on case-sensitive filesystems
- Add 'source_id' field to components parsed from OpenDSS, to help determine origin and active phases
- Add conversion of OpenDSS PVSystem objects into generators, using KVA for generator limits
- Add compatibility for Julia v0.7/v1.0

### v0.1.2
- Add support for network flow approximation formulation, NFAPowerModel
Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
julia 0.6 0.7-
julia 0.6

JuMP 0.17 0.19-
PowerModels 0.8.8 0.9-
InfrastructureModels 0.0.4 0.2-
Memento 0.8

Compat 1.0
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
4 changes: 0 additions & 4 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,5 @@ makedocs(
)

deploydocs(
deps = nothing,
make = nothing,
target = "build",
repo = "github.com/lanl-ansi/ThreePhasePowerModels.jl.git",
julia = "0.6"
)
15 changes: 12 additions & 3 deletions src/ThreePhasePowerModels.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
# isdefined(Base, :__precompile__) && __precompile__()

module ThreePhasePowerModels

using JuMP
using PowerModels
using InfrastructureModels
using Memento

using Compat.LinearAlgebra

if VERSION < v"0.7.0-"
import Compat: occursin
import Compat: findall
import Compat: undef
import Compat: Nothing
end

const PMs = PowerModels

const LOGGER = getlogger(PowerModels)
function __init__()
global LOGGER = getlogger(PowerModels)
end

include("core/ref.jl")
include("core/multiconductor.jl")
Expand Down
16 changes: 8 additions & 8 deletions src/core/constraint_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ function constraint_tp_voltage_magnitude_difference(pm::GenericPowerModel, i::In

r = branch["br_r"].values
x = branch["br_x"].values
g_sh_fr = diagm(branch["g_fr"].values)
b_sh_fr = diagm(branch["b_fr"].values)
g_sh_fr = diagm(0 => branch["g_fr"].values)
b_sh_fr = diagm(0 => branch["b_fr"].values)
tm = branch["tap"].values

constraint_tp_voltage_magnitude_difference(pm, nw, i, f_bus, t_bus, f_idx, t_idx, r, x, g_sh_fr, b_sh_fr, tm)
Expand All @@ -151,8 +151,8 @@ function constraint_tp_branch_current(pm::GenericPowerModel{T}, i::Int; nw::Int=
t_bus = branch["t_bus"]
f_idx = (i, f_bus, t_bus)

g_sh_fr = diagm(branch["g_fr"].values)
b_sh_fr = diagm(branch["b_fr"].values)
g_sh_fr = diagm(0 => branch["g_fr"].values)
b_sh_fr = diagm(0 => branch["b_fr"].values)

constraint_tp_branch_current(pm, nw, i, f_bus, f_idx, g_sh_fr, b_sh_fr)
end
Expand Down Expand Up @@ -185,10 +185,10 @@ function constraint_tp_flow_losses(pm::GenericPowerModel, i::Int; nw::Int=pm.cnw

r = branch["br_r"].values
x = branch["br_x"].values
g_sh_fr = diagm(branch["g_fr"]).values
g_sh_to = diagm(branch["g_to"]).values
b_sh_fr = diagm(branch["b_fr"]).values
b_sh_to = diagm(branch["b_to"]).values
g_sh_fr = diagm(0 => branch["g_fr"]).values
g_sh_to = diagm(0 => branch["g_to"]).values
b_sh_fr = diagm(0 => branch["b_fr"]).values
b_sh_to = diagm(0 => branch["b_to"]).values

constraint_tp_flow_losses(pm::GenericPowerModel, nw, i, f_bus, t_bus, f_idx, t_idx, r, x, g_sh_fr, g_sh_to, b_sh_fr, b_sh_to)
end
Expand Down
89 changes: 79 additions & 10 deletions src/core/multiconductor.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,104 @@
# https://stackoverflow.com/questions/39039553/lower-triangular-matrix-in-julia
function vec2utri{T}(v::Vector{T})
function vec2utri(v::Vector{T}) where T
d = length(v)
n = Int((sqrt(8d+1)+1)/2)
n*(n-1)/2 == d || error("vec2utri: length of vector is not triangular")
[ i<j ? v[Int((j-1)*(j-2)/2)+i] : 0 for i=1:n, j=1:n ]
end

function vec2ltri{T}(v::Vector{T})
function vec2ltri(v::Vector{T}) where T
vec2utri(v)'
end

function mat2utrivec{T}(m::Matrix{T})
assert(size(m,1) == size(m,2))
function mat2utrivec(m::Matrix{T}) where T
@assert size(m,1) == size(m,2)
n = size(m,1)
[m[i,j] for i=1:n, j=1:n if i < j]
end

function mat2ltrivec{T}(m::Matrix{T})
assert(size(m,1) == size(m,2))
function mat2ltrivec(m::Matrix{T}) where T
@assert size(m,1) == size(m,2)
n = size(m,1)
[m[j,i] for i=1:n, j=1:n if i < j]
end

function make_hermitian_matrix_variable(diag, lowertrianglereal, lowertriangleimag)
matrixreal = diagm( diag) + vec2ltri(lowertrianglereal) + vec2utri(lowertrianglereal)
matriximag = diagm(0*diag) + vec2ltri(lowertriangleimag) - vec2utri(lowertriangleimag)
#TODO if not multiplied with 0, array is not a JuMP type
#TODO clean up
matrixreal = []
if length(diag) == 3
matrixreal = [
diag[1] lowertrianglereal[1] lowertrianglereal[2];
lowertrianglereal[1] diag[2] lowertrianglereal[3];
lowertrianglereal[2] lowertrianglereal[3] diag[3]
]
elseif length(diag) == 4
matrixreal = [
diag[1] lowertrianglereal[1] lowertrianglereal[2] lowertrianglereal[4];
lowertrianglereal[1] diag[2] lowertrianglereal[3] lowertrianglereal[5];
lowertrianglereal[2] lowertrianglereal[3] diag[3] lowertrianglereal[6];
lowertrianglereal[4] lowertrianglereal[5] lowertrianglereal[6] diag[4]
]
elseif length(diag) == 5
matrixreal = [
diag[1] lowertrianglereal[1] lowertrianglereal[2] lowertrianglereal[4] lowertrianglereal[7];
lowertrianglereal[1] diag[2] lowertrianglereal[3] lowertrianglereal[5] lowertrianglereal[8];
lowertrianglereal[2] lowertrianglereal[3] diag[3] lowertrianglereal[6] lowertrianglereal[9];
lowertrianglereal[4] lowertrianglereal[5] lowertrianglereal[6] diag[4] lowertrianglereal[10];
lowertrianglereal[7] lowertrianglereal[8] lowertrianglereal[9] lowertrianglereal[10] diag[5]
]
end

matriximag = []
if length(diag) == 3
matriximag = [
0 -lowertriangleimag[1] -lowertriangleimag[2];
lowertriangleimag[1] 0 -lowertriangleimag[3];
lowertriangleimag[2] lowertriangleimag[3] 0
]
elseif length(diag) == 4
matriximag = [
0 -lowertriangleimag[1] -lowertriangleimag[2] -lowertriangleimag[4];
lowertriangleimag[1] 0 -lowertriangleimag[3] -lowertriangleimag[5];
lowertriangleimag[2] lowertriangleimag[3] 0 -lowertriangleimag[6];
lowertriangleimag[4] lowertriangleimag[5] lowertriangleimag[6] 0
]
elseif length(diag) == 5
matriximag = [
0 -lowertriangleimag[1] -lowertriangleimag[2] -lowertriangleimag[4] -lowertriangleimag[7];
lowertriangleimag[1] 0 -lowertriangleimag[3] -lowertriangleimag[5] -lowertriangleimag[8];
lowertriangleimag[2] lowertriangleimag[3] 0 -lowertriangleimag[6] -lowertriangleimag[9];
lowertriangleimag[4] lowertriangleimag[5] lowertriangleimag[6] 0 -lowertriangleimag[10];
lowertriangleimag[7] lowertriangleimag[8] lowertriangleimag[9] lowertriangleimag[10] 0
]
end
return matrixreal, matriximag
end

function make_full_matrix_variable(diag, lowertriangle, uppertriangle)
matrix = diagm(diag) + vec2ltri(lowertriangle) + vec2utri(uppertriangle)
#TODO clean up
matrix = []
if length(diag) == 3
matrix = [
diag[1] uppertriangle[1] uppertriangle[2];
lowertriangle[1] diag[2] uppertriangle[3];
lowertriangle[2] lowertriangle[3] diag[3]
]
elseif length(diag) == 4
matrix = [
diag[1] uppertriangle[1] uppertriangle[2] uppertriangle[4];
lowertriangle[1] diag[2] uppertriangle[3] uppertriangle[5];
lowertriangle[2] lowertriangle[3] diag[3] uppertriangle[6];
lowertriangle[4] lowertriangle[5] lowertriangle[6] diag[4]
]
elseif length(diag) == 5
matrix = [
diag[1] uppertriangle[1] uppertriangle[2] uppertriangle[4] uppertriangle[7];
lowertriangle[1] diag[2] uppertriangle[3] uppertriangle[5] uppertriangle[8];
lowertriangle[2] lowertriangle[3] diag[3] uppertriangle[6] uppertriangle[9];
lowertriangle[4] lowertriangle[5] lowertriangle[6] diag[4] uppertriangle[10]
lowertriangle[7] lowertriangle[8] lowertriangle[9] lowertriangle[10] diag[5]
]
end
# matrix = diagm(0 => diag) + vec2ltri(lowertriangle) + vec2utri(uppertriangle)
return matrix
end
16 changes: 8 additions & 8 deletions src/core/relaxation_scheme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ year = {2003}
```
"""
function relaxation_psd_to_soc_real(m, mx)
assert(size(mx,1) == size(mx,2))
@assert size(mx,1) == size(mx,2)
n_elements = size(mx,1)
for i in 1:n_elements-1
for j in i+1:n_elements
Expand All @@ -39,7 +39,7 @@ year = {2003}
```
"""
function relaxation_psd_to_soc_complex(m, mxreal, mximag)
assert(size(mxreal) == size(mximag))
@assert size(mxreal) == size(mximag)
n_elements = size(mxreal,1)
for i in 1:n_elements-1
for j in i+1:n_elements
Expand All @@ -64,7 +64,7 @@ year = {2003}
```
"""
function relaxation_psd_to_soc_real_conic(m, mx)
assert(size(mx,1) == size(mx,2))
@assert size(mx,1) == size(mx,2)
n_elements = size(mx,1)
for i in 1:n_elements-1
for j in i+1:n_elements
Expand All @@ -89,7 +89,7 @@ year = {2003}
```
"""
function relaxation_psd_to_soc_complex_conic(m, mxreal, mximag)
assert(size(mxreal) == size(mximag))
@assert size(mxreal) == size(mximag)
n_elements = size(mxreal,1)
for i in 1:n_elements-1
for j in i+1:n_elements
Expand All @@ -115,7 +115,7 @@ year = {2001}
"""
function relaxation_psd_to_soc(m, mxreal, mximag; complex=true)
if complex==false
assert(size(mxreal) == size(mximag))
@assert size(mxreal) == size(mximag)
mx =
[
mxreal -mximag;
Expand Down Expand Up @@ -145,7 +145,7 @@ year = {2001}
"""
function relaxation_psd_to_soc_conic(m, mxreal, mximag; complex=true)
if complex==false
assert(size(mxreal) == size(mximag))
@assert size(mxreal) == size(mximag)
mx =
[
mxreal -mximag;
Expand All @@ -164,8 +164,8 @@ end
real-valued SDP to SDP relaxation based on PSDness of principal minors, default is 3x3 SDP relaxation
"""
function relaxation_psd_to_psd_real(m, mxreal, mximag; ndim=3)
assert(size(mxreal) == size(mximag))
assert(size(mxreal,1) >= ndim)
@assert size(mxreal) == size(mximag)
@assert size(mxreal,1) >= ndim
n_elements = size(mxreal,1)
for i in 1:n_elements-(ndim-1)
j = i+(ndim-1)
Expand Down
2 changes: 1 addition & 1 deletion src/form/bf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LPLinUBFPowerModel(data::Dict{String,Any}; kwargs...) =

"rolls a 1d array left or right by idx"
function roll(array::Array{T, 1}, idx::Int; right=true) where T <: Number
out = Array{T}(size(array))
out = Array{T}(undef, size(array))
pos = idx % length(out)

if right
Expand Down
26 changes: 13 additions & 13 deletions src/form/bf_mx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ end
function variable_tp_branch_flow(pm::GenericPowerModel{T}; n_cond::Int=3, nw::Int=pm.cnw, bounded = true) where T <: AbstractUBFForm
n_diag_el = n_cond
n_lower_triangle_el = Int((n_cond^2 - n_cond)/2)
assert(n_cond<=5)
@assert n_cond<=5

for i in 1:n_diag_el
PMs.variable_active_branch_flow(pm, nw=nw, cnd=i, bounded=bounded)
Expand Down Expand Up @@ -565,8 +565,8 @@ function add_branch_flow_rank(sol, pm::GenericPowerModel{T}; tol = 1e-6) where T
for (nw, network) in pm.ref[:nw]
buses = ref(pm, nw, :bus)
for (b, branch) in ref(pm, nw, :branch)
g_fr = diagm(branch["g_fr"].values)
b_fr = diagm(branch["b_fr"].values)
g_fr = diagm(0 => branch["g_fr"].values)
b_fr = diagm(0 => branch["b_fr"].values)
y_fr = g_fr + im* b_fr

fbus = branch["f_bus"]
Expand Down Expand Up @@ -633,11 +633,11 @@ function add_original_variables(sol, pm::GenericPowerModel)

if !isempty(candidate_arcs_from)
(l,i,j) = arc = candidate_arcs_from[1]
g_fr = diagm(branches[l]["g_fr"].values)
b_fr = diagm(branches[l]["b_fr"].values)
g_fr = diagm(0 => branches[l]["g_fr"].values)
b_fr = diagm(0 => branches[l]["b_fr"].values)
y_fr = g_fr + im* b_fr
g_to = diagm(branches[l]["g_to"].values)
b_to = diagm(branches[l]["b_to"].values)
g_to = diagm(0 => branches[l]["g_to"].values)
b_to = diagm(0 => branches[l]["b_to"].values)
y_to = g_to + im* b_to
r = branches[l]["br_r"].values
x = branches[l]["br_x"].values
Expand All @@ -649,7 +649,7 @@ function add_original_variables(sol, pm::GenericPowerModel)
Sij = Pij + im*Qij

Ssij = Sij - Ui*Ui'*y_fr'
Isij = (1/trace(Ui*Ui'))*(Ssij')*Ui
Isij = (1/tr(Ui*Ui'))*(Ssij')*Ui
Uj = Ui - z*Isij
Iij = Isij + y_fr*Ui

Expand All @@ -670,11 +670,11 @@ function add_original_variables(sol, pm::GenericPowerModel)

elseif !isempty(candidate_arcs_to)
(l,i,j) = arc = candidate_arcs_to[1]
g_fr = diagm(branches[l]["g_to"].values)
b_fr = diagm(branches[l]["b_to"].values)
g_fr = diagm(0 => branches[l]["g_to"].values)
b_fr = diagm(0 => branches[l]["b_to"].values)
y_fr = g_fr + im* b_fr
g_to = diagm(branches[l]["g_fr"].values)
b_to = diagm(branches[l]["b_fr"].values)
g_to = diagm(0 => branches[l]["g_fr"].values)
b_to = diagm(0 => branches[l]["b_fr"].values)
y_to = g_to + im* b_to
r = branches[l]["br_r"].values
x = branches[l]["br_x"].values
Expand All @@ -686,7 +686,7 @@ function add_original_variables(sol, pm::GenericPowerModel)
Sij = Pij + im*Qij

Ssij = Sij - Ui*Ui'*y_fr'
Isij = (1/trace(Ui*Ui'))*(Ssij')*Ui
Isij = (1/tr(Ui*Ui'))*(Ssij')*Ui
Uj = Ui - z*Isij
Iij = Isij + y_fr*Ui

Expand Down
Loading

0 comments on commit 1869fe0

Please sign in to comment.