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

Julia 1.5+ and JuMP 0.21+ compat #4

Open
wants to merge 3 commits into
base: master
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
14 changes: 14 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name = "PSA"
uuid = "4c80e164-764c-4b93-bf2f-9dd64506ca8b"
authors = ["Tom Brown <tom.brown@kit.edu>"]
version = "0.1.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Clp = "e2554f3b-3117-50c0-817c-e040a3ddf72d"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
9 changes: 4 additions & 5 deletions examples/ac-dc-lopf.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@

# Cf. PyPSA example https://github.com/PyPSA/PyPSA/tree/master/examples/ac-dc-meshed

push!(LOAD_PATH, "/home/tom/fias/lib")

import PSA

using Clp
using JuMP

network = PSA.import_network("/home/tom/fias/lib/pypsa/examples/ac-dc-meshed/ac-dc-data/")

solver = ClpSolver()
solver = Clp.Optimizer

m = PSA.lopf(network, solver)

print(m.objVal)
println(objective_value(m))

print(network.generators_t["p"])
println(network.generators_t["p"])
15 changes: 8 additions & 7 deletions src/PSA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ function import_network(folder)
!ispath("$folder") ? error("Path not existent") : nothing
components = static_components(network)
for component=components
if ispath("$folder/$component.csv")
componentpath = joinpath("$folder", "$component.csv")
if ispath(componentpath)
# fallback for missing values
try
setfield!(network,component,CSV.read("$folder/$component.csv"; truestring="True",
falsestring="False"))
setfield!(network,component,CSV.read(componentpath, DataFrame; truestrings=["True"],
falsestrings=["False"]))
catch y
if (typeof(y)==Missings.MissingException) | (typeof(y) == BoundsError)
setfield!(network,component,readtable("$folder/$component.csv"; truestrings=["True"],
setfield!(network,component,readtable(componentpath; truestrings=["True"],
falsestrings=["False"]))

end
Expand All @@ -164,7 +165,7 @@ function import_network(folder)
for name in names(df)
if name == :name
nothing
elseif typeof(df[name]) == Array{Union{Int64, Missings.Missing},1}
elseif typeof(df[!, name]) == Array{Union{Int64, Missings.Missing},1}
println("converting column $name of $component from Int to Float")
df[name] = float(df[name])
end
Expand All @@ -178,7 +179,7 @@ function import_network(folder)
# fallback for missing values for a non-null column type, might be deprecated soon
try
getfield(network,component_t)[attr]= (
CSV.read("$folder/$component-$attr.csv"; truestring="True", falsestring="False") )
CSV.read("$folder/$component-$attr.csv"; truestrings=["True"], falsestrings=["False"]) )
catch y
if (typeof(y)==Missings.MissingException) | (typeof(y) == BoundsError)
getfield(network,component_t)[attr]= (
Expand All @@ -189,7 +190,7 @@ function import_network(folder)
end
end
initializer = Network()
for field=setdiff(fieldnames(network), fieldnames(initializer))
for field=setdiff(fieldnames(typeof(network)), fieldnames(typeof(initializer)))
setfield!(network, field, getfield(initializer, field))
end
return network
Expand Down
51 changes: 27 additions & 24 deletions src/auxilliaries.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using LightGraphs
using PyCall
const networkx = PyNULL()
copy!(networkx, pyimport("networkx" ))
copy!(networkx, pyimport("networkx"))


function time_dependent_components(network)
fields = String.(fieldnames(network))
fields = String.(fieldnames(typeof(network)))
components = []
for field=fields
if field[end-1:end] == "_t"
Expand All @@ -16,7 +16,7 @@ function time_dependent_components(network)
end

function static_components(network)
fields = String.(fieldnames(network))
fields = String.(fieldnames(typeof(network)))
components = []
for field=fields
if field[end-1:end] != "_t"
Expand All @@ -41,26 +41,26 @@ end
function align_component_order!(network)
components_t = time_dependent_components(network)
for comp=components_t
order = Symbol.(getfield(network, Symbol(String(comp)[1:end-2]))[:name])
order = Symbol.(getfield(network, Symbol(String(comp)[1:end-2]))[!, :name])
for attr in keys(getfield(network, comp))
if length(getfield(network,comp)[attr])==length(order)
if nrow(getfield(network,comp)[attr])==length(order)
getfield(network,comp)[attr]= getfield(network,comp)[attr][:, order]
end
end
end
end

# auxilliary funcitons
idx(dataframe) = Dict(zip(dataframe[:name], Iterators.countfrom(1)))
rev_idx(dataframe) = Dict(zip(Iterators.countfrom(1), dataframe[:name]))
idx_by(dataframe, col, values) = select_by(dataframe, col, values)[:idx]
idx(dataframe) = Dict(zip(dataframe[!, :name], Iterators.countfrom(1)))
rev_idx(dataframe) = Dict(zip(Iterators.countfrom(1), dataframe[!, :name]))
idx_by(dataframe, col, values) = select_by(dataframe, col, values)[!, :idx]

function select_by(dataframe, col, selector)
if length(findin(dataframe[col], selector))==0
if length(findall(in(selector), dataframe[!, col]))==0
return dataframe[repeat(Bool[false],outer=nrow(dataframe)) , :]
else
mdict = Dict(zip(dataframe[col], Iterators.countfrom(1)))
ids = Array{Int,1}(0)
mdict = Dict(zip(dataframe[!, col], Iterators.countfrom(1)))
ids = Array{Int,1}()
for i in selector
push!(ids, mdict[i])
end
Expand All @@ -73,10 +73,10 @@ select_names(a, b) = select_by(a, :name, b)
function append_idx_col!(dataframe)
if typeof(dataframe)==Vector{DataFrames.DataFrame}
for df in dataframe
df[:idx] = collect(1:nrow(df))
df[!, :idx] = collect(1:nrow(df))
end
else
dataframe[:idx] = collect(1:nrow(dataframe))
dataframe[!, :idx] = collect(1:nrow(dataframe))
end
end

Expand All @@ -89,16 +89,16 @@ function get_switchable_as_dense(network, component, attribute, snapshots=0)
if in(attribute, keys(getfield(network, component_t)))
dense = getfield(network, component_t)[attribute]
end
cols = Symbol.(getfield(network, component)[:name])
cols = Symbol.(getfield(network, component)[!, :name])
not_included = String.(setdiff(cols, names(dense)))
if length(not_included)>0
attribute = Symbol.(attribute)
df = select_names(getfield(network, component), not_included)
df = names!(DataFrame(repmat(transpose(Array(df[attribute])), T)),
df = rename!(DataFrame(repeat(transpose(Array(df[!, attribute])), T)),
Symbol.(not_included))
dense = [dense df]
end
return dense[cols]
return dense[!, cols]
end


Expand Down Expand Up @@ -128,7 +128,7 @@ end

function calculate_dependent_values!(network)
function set_default(dataframe, col, default)
!in(col, names(dataframe)) ? dataframe[col] = default : nothing
!(col in propertynames(dataframe)) ? insertcols!(dataframe, col .=> default) : nothing
end

#buses
Expand All @@ -138,7 +138,7 @@ function calculate_dependent_values!(network)
end

# generators
defaults = [(:p_nom_extendable, false), (:p_nom_max, Inf),(:commitable, false),
defaults = [(:p_nom_extendable, false), (:committable, false),
(:p_min_pu, 0), (:p_max_pu, 1), (:p_nom_min, 0),(:capital_cost, 0),
(:min_up_time, 0), (:min_down_time, 0), (:initial_status, true),
(:p_nom, 0.),(:marginal_cost, 0),(:p_nom_opt, 0.)]
Expand All @@ -147,16 +147,18 @@ function calculate_dependent_values!(network)
end

# lines
network.lines[:v_nom]=select_names(network.buses, network.lines[:bus0])[:v_nom]
network.lines[!, :v_nom]=select_names(network.buses, network.lines[!, :bus0])[!, :v_nom]
defaults = [(:s_nom_extendable, false), (:s_nom_min, 0),(:s_nom_max, Inf), (:s_nom, 0.),
(:s_nom_min, 0), (:s_nom_max, Inf), (:capital_cost, 0), (:g, 0)]
for (col, default) in defaults
set_default(network.lines, col, default)
end
network.lines[:x_pu] = network.lines[:x]./(network.lines[:v_nom].^2)
network.lines[:r_pu] = network.lines[:r]./(network.lines[:v_nom].^2)
network.lines[:b_pu] = network.lines[:b].*network.lines[:v_nom].^2
network.lines[:g_pu] = network.lines[:g].*network.lines[:v_nom].^2
pu(a, b) = a ./ b.^2
pu_inverse(a, b) = a .* b.^2
transform!(network.lines, [:x, :v_nom] => pu => :x_pu)
transform!(network.lines, [:r, :v_nom] => pu => :r_pu)
transform!(network.lines, [:b, :v_nom] => pu_inverse => :b_pu)
transform!(network.lines, [:g, :v_nom] => pu_inverse => :g_pu)

# links
defaults = [(:p_nom_extendable, false), (:p_nom_max, Inf), (:p_min_pu, 0),
Expand Down Expand Up @@ -187,7 +189,7 @@ function calculate_dependent_values!(network)
# loads_t
for df_name=keys(network.loads_t)
if nrow(network.loads_t[df_name])>1
for bus=[bus for bus in network.loads[:name] if
for bus=[bus for bus in network.loads[!, :name] if
!in(Symbol(bus), names(network.loads_t[df_name]))]
set_default(network.loads_t[df_name], bus, 0)
end
Expand Down Expand Up @@ -238,6 +240,7 @@ function ptdf_matrix(network)
end

function get_cycles(network)
copy!(networkx, pyimport("networkx"))
busidx = idx(network.buses)
g = networkx[:Graph]()
g[:add_nodes_from](busidx)
Expand Down
Loading