From 50e2c77b1da4162926479eaba5e24a4eece3bcef Mon Sep 17 00:00:00 2001 From: fleimgruber Date: Sat, 20 Feb 2021 08:44:03 +0100 Subject: [PATCH 1/3] fix: API changes for Julia v0.7+ --- examples/ac-dc-lopf.jl | 4 +- src/PSA.jl | 15 +++-- src/auxilliaries.jl | 47 +++++++------- src/lopf.jl | 141 +++++++++++++++++++++-------------------- 4 files changed, 107 insertions(+), 100 deletions(-) diff --git a/examples/ac-dc-lopf.jl b/examples/ac-dc-lopf.jl index e9d0289..bf109b5 100644 --- a/examples/ac-dc-lopf.jl +++ b/examples/ac-dc-lopf.jl @@ -1,15 +1,13 @@ # 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 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) diff --git a/src/PSA.jl b/src/PSA.jl index edc3e1b..1f27ea9 100644 --- a/src/PSA.jl +++ b/src/PSA.jl @@ -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 @@ -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 @@ -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]= ( @@ -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 diff --git a/src/auxilliaries.jl b/src/auxilliaries.jl index 1900735..1d48347 100644 --- a/src/auxilliaries.jl +++ b/src/auxilliaries.jl @@ -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" @@ -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" @@ -51,16 +51,16 @@ function align_component_order!(network) 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 @@ -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 @@ -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 @@ -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 @@ -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.)] @@ -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), @@ -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 @@ -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) diff --git a/src/lopf.jl b/src/lopf.jl index e1fedd0..6cd3c79 100644 --- a/src/lopf.jl +++ b/src/lopf.jl @@ -1,3 +1,4 @@ +using LinearAlgebra using JuMP @@ -35,7 +36,7 @@ function lopf(network, solver) #solver = ClpSolver() - m = Model(solver=solver) + m = Model(solver) calculate_dependent_values!(network) buses = network.buses @@ -50,9 +51,9 @@ function lopf(network, solver) # 1. add all generators to the model # 1.1 set different generator types generators = network.generators - fix_gens_b = ((.!generators[:p_nom_extendable]) .& (.!generators[:commitable])) - ext_gens_b = convert(BitArray, generators[:p_nom_extendable]) - com_gens_b = convert(BitArray, generators[:commitable]) + fix_gens_b = ((.!generators[!, :p_nom_extendable]) .& (.!generators[!, :committable])) + ext_gens_b = convert(BitArray, generators[!, :p_nom_extendable]) + com_gens_b = convert(BitArray, generators[!, :committable]) # 1.2 fix bounds for iterating N_fix = sum(fix_gens_b) @@ -79,7 +80,7 @@ function lopf(network, solver) p_min_pu = select_time_dep(network, "generators", "p_min_pu",components=ext_gens_b) p_max_pu = select_time_dep(network, "generators", "p_max_pu",components=ext_gens_b) p_nom_min = network.generators[ext_gens_b,:p_nom_min] - p_nom_max = network.generators[ext_gens_b,:p_nom_max] + p_nom_max = network.generators[ext_gens_b,:p_nom] @variable m G_ext[gr=1:N_ext,t = 1:T] @@ -94,9 +95,9 @@ function lopf(network, solver) G = [G_fix; G_ext] # G is the concatenated variable array generators = [generators[fix_gens_b,:]; generators[ext_gens_b,:] ] # sort generators the same # new booleans - fix_gens_b = ((.!generators[:p_nom_extendable]) .& (.!generators[:commitable])) - ext_gens_b = convert(BitArray, generators[:p_nom_extendable]) - com_gens_b = convert(BitArray, generators[:commitable]) + fix_gens_b = ((.!generators[!, :p_nom_extendable]) .& (.!generators[!, :committable])) + ext_gens_b = convert(BitArray, generators[!, :p_nom_extendable]) + com_gens_b = convert(BitArray, generators[!, :committable]) @@ -105,7 +106,7 @@ function lopf(network, solver) # 2. add all lines to the model # 2.1 set different lines types lines = network.lines - fix_lines_b = (.!lines[:s_nom_extendable]) + fix_lines_b = (.!lines[!, :s_nom_extendable]) ext_lines_b = .!fix_lines_b # 2.2 iterator bounds @@ -127,7 +128,7 @@ function lopf(network, solver) LN = [LN_fix; LN_ext] lines = [lines[fix_lines_b,:]; lines[ext_lines_b,:]] - fix_lines_b = (.!lines[:s_nom_extendable]) + fix_lines_b = (.!lines[!, :s_nom_extendable]) ext_lines_b = .!fix_lines_b # -------------------------------------------------------------------------------------------------------- @@ -135,7 +136,7 @@ function lopf(network, solver) # 3. add all links to the model # 3.1 set different link types links = network.links - fix_links_b = .!links[:p_nom_extendable] + fix_links_b = .!links[!, :p_nom_extendable] ext_links_b = .!fix_links_b # 3.2 iterator bounds @@ -158,14 +159,14 @@ function lopf(network, solver) LK = [LK_fix; LK_ext] links = [links[fix_links_b,:]; links[ext_links_b,:]] - fix_links_b = .!links[:p_nom_extendable] + fix_links_b = .!links[!, :p_nom_extendable] ext_links_b = .!fix_links_b # -------------------------------------------------------------------------------------------------------- # 4. define storage_units # 4.1 set different storage_units types storage_units = network.storage_units - fix_sus_b = .!storage_units[:p_nom_extendable] + fix_sus_b = .!storage_units[!, :p_nom_extendable] ext_sus_b = .!fix_sus_b inflow = get_switchable_as_dense(network, "storage_units", "inflow") @@ -187,7 +188,7 @@ function lopf(network, solver) SU_p_nom[s=1:N_ext] >= 0 0 <= SU_soc_fix[s=1:N_fix,t=1:T] <= (storage_units[fix_sus_b,:max_hours] - .*storage_units[fix_sus_b,:p_nom])[s] + .* storage_units[fix_sus_b,:p_nom])[s] SU_soc_ext[s=1:N_ext,t=1:T] >= 0 0 <= SU_spill_fix[s=1:N_fix,t=1:T] <= inflow[:,fix_sus_b][t,s] @@ -211,24 +212,24 @@ function lopf(network, solver) storage_units = [storage_units[fix_sus_b,:]; storage_units[ext_sus_b,:]] inflow = [inflow[:,fix_sus_b] inflow[:,ext_sus_b]] - ext_sus_b = BitArray(storage_units[:p_nom_extendable]) + ext_sus_b = BitArray(storage_units[!, :p_nom_extendable]) - is_cyclic_i = collect(1:N_sus)[BitArray(storage_units[:cyclic_state_of_charge])] - not_cyclic_i = collect(1:N_sus)[.!storage_units[:cyclic_state_of_charge]] + is_cyclic_i = collect(1:N_sus)[BitArray(storage_units[!, :cyclic_state_of_charge])] + not_cyclic_i = collect(1:N_sus)[.!storage_units[!, :cyclic_state_of_charge]] @constraints(m, begin [s=is_cyclic_i], SU_soc[s,1] == (SU_soc[s,T] + storage_units[s,:efficiency_store] * SU_store[s,1] - - (1./storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,1] + - (1 ./ storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,1] + inflow[1,s] - SU_spill[s,1] ) [s=not_cyclic_i], SU_soc[s,1] == (storage_units[s,:state_of_charge_initial] + storage_units[s,:efficiency_store] * SU_store[s,1] - - (1./storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,1] + - (1 ./ storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,1] + inflow[1,s] - SU_spill[s,1]) [s=1:N_sus,t=2:T], SU_soc[s,t] == (SU_soc[s,t-1] + storage_units[s,:efficiency_store] * SU_store[s,t] - - (1./storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,t] + - (1 ./ storage_units[s,:efficiency_dispatch]) * SU_dispatch[s,t] + inflow[t,s] - SU_spill[s,t] ) end) @@ -239,7 +240,7 @@ function lopf(network, solver) # 5.1 set different stores types stores = network.stores - fix_stores_b = .!stores[:e_nom_extendable] + fix_stores_b = .!stores[!, :e_nom_extendable] ext_stores_b = .!fix_stores_b inflow = get_switchable_as_dense(network, "stores", "inflow") @@ -288,10 +289,10 @@ function lopf(network, solver) inflow = [inflow[:,fix_stores_b] inflow[:,ext_stores_b]] - ext_stores_b = BitArray(stores[:e_nom_extendable]) + ext_stores_b = BitArray(stores[!, :e_nom_extendable]) - is_cyclic_i = collect(1:N_st)[BitArray(stores[:cyclic_state_of_charge])] - not_cyclic_i = collect(1:N_st)[.!stores[:cyclic_state_of_charge]] + is_cyclic_i = collect(1:N_st)[BitArray(stores[!, :cyclic_state_of_charge])] + not_cyclic_i = collect(1:N_st)[.!stores[!, :cyclic_state_of_charge]] @constraints(m, begin [s=is_cyclic_i,t=1], ST_soc[s,t] == (ST_soc[s,T] @@ -316,19 +317,23 @@ function lopf(network, solver) ## 6. define nodal balance constraint #load data in correct order - loads = network.loads_t["p"][:,Symbol.(network.loads[:name])] + if nrow(network.loads_t["p"]) > 1 + loads = network.loads_t["p"][:,Symbol.(network.loads[!, :name])] + else + loads = network.loads_t["p"] + end @constraint(m, balance[n=1:N, t=1:T], ( - sum(G[findin(generators[:bus], [reverse_busidx[n]]), t]) - + sum(LN[ findin(lines[:bus1], [reverse_busidx[n]]) ,t]) - + sum(links[findin(links[:bus1], [reverse_busidx[n]]),:efficiency] - .* LK[ findin(links[:bus1], [reverse_busidx[n]]) ,t]) - + sum(SU_dispatch[ findin(storage_units[:bus], [reverse_busidx[n]]) ,t]) + sum(G[findall(in([reverse_busidx[n]]), generators[!, :bus]), t]) + + sum(LN[ findall(in([reverse_busidx[n]]), lines[!, :bus1]) ,t]) + + sum(links[findall(in([reverse_busidx[n]]), links[!, :bus1]),:efficiency] + .* LK[ findall(in([reverse_busidx[n]]), links[!, :bus1]) ,t]) + + sum(SU_dispatch[ findall(in([reverse_busidx[n]]), storage_units[!, :bus]) ,t]) - - row_sum(loads[t,findin(network.loads[:bus],[reverse_busidx[n]])],1) - - sum(LN[ findin(lines[:bus0], [reverse_busidx[n]]) ,t]) - - sum(LK[ findin(links[:bus0], [reverse_busidx[n]]) ,t]) - - sum(SU_store[ findin(storage_units[:bus], [reverse_busidx[n]]) ,t]) + # - row_sum(loads[t,findall(in([reverse_busidx[n]]), network.loads[!, :bus])],1) + - sum(LN[ findall(in([reverse_busidx[n]]), lines[!, :bus0]) ,t]) + - sum(LK[ findall(in([reverse_busidx[n]]), links[!, :bus0]) ,t]) + - sum(SU_store[ findall(in([reverse_busidx[n]]), storage_units[!, :bus]) ,t]) == 0 )) @@ -364,13 +369,13 @@ function lopf(network, solver) bus0 = cycles[cyc][bus] bus1 = cycles[cyc][(bus)%length(cycles[cyc])+1] try - push!(cycles_branch[cyc],branches[((branches[:bus0].==reverse_busidx[bus0]) - .&(branches[:bus1].==reverse_busidx[bus1])),:idx][1] ) + push!(cycles_branch[cyc],branches[((branches[!, :bus0].==reverse_busidx[bus0]) + .&(branches[!, :bus1].==reverse_busidx[bus1])),:idx][1] ) push!(directions[cyc], 1.) catch y if isa(y, BoundsError) - push!(cycles_branch[cyc], branches[((branches[:bus0].==reverse_busidx[bus1]) - .&(branches[:bus1].==reverse_busidx[bus0])),:idx][1] ) + push!(cycles_branch[cyc], branches[((branches[!, :bus0].==reverse_busidx[bus1]) + .&(branches[!, :bus1].==reverse_busidx[bus0])),:idx][1] ) push!(directions[cyc], -1.) else return y @@ -393,82 +398,82 @@ function lopf(network, solver) # 8. set global_constraints # only for co2_emissions till now - if nrow(network.global_constraints)>0 && in("primary_energy", network.global_constraints[:type]) - co2_limit = network.global_constraints[network.global_constraints[:name].=="co2_limit", :constant] - nonnull_carriers = network.carriers[network.carriers[:co2_emissions].!=0, :] - emmssions = Dict(zip(nonnull_carriers[:name], nonnull_carriers[:co2_emissions])) - carrier_index(carrier) = findin(generators[:carrier], [carrier]) - @constraint(m, sum(sum(dot(1./generators[carrier_index(carrier) , :efficiency], + if nrow(network.global_constraints)>0 && in("primary_energy", network.global_constraints[!, :type]) + co2_limit = network.global_constraints[network.global_constraints[!, :name].=="co2_limit", :constant] + nonnull_carriers = network.carriers[network.carriers[!, :co2_emissions].!=0, :] + emmssions = Dict(zip(nonnull_carriers[!, :name], nonnull_carriers[!, :co2_emissions])) + carrier_index(carrier) = findall(in([carrier]), generators[!, :carrier]) + @constraint(m, sum(sum(dot(1 ./ generators[carrier_index(carrier) , :efficiency], G[carrier_index(carrier),t]) for t=1:T) - * select_names(network.carriers, [carrier])[:co2_emissions] - for carrier in network.carriers[:name]) .<= co2_limit) + * select_names(network.carriers, [carrier])[!, :co2_emissions][1] + for carrier in network.carriers[!, :name]) .<= co2_limit) end # -------------------------------------------------------------------------------------------------------- # 9. set objective function @objective(m, Min, - sum(dot(generators[:marginal_cost], G[:,t]) for t=1:T) + sum(dot(generators[!, :marginal_cost], G[:,t]) for t=1:T) # consider already build infrastructur + dot(generators[ext_gens_b,:capital_cost], gen_p_nom[:] ) + dot(lines[ext_lines_b,:capital_cost], LN_s_nom[:]) + dot(links[ext_links_b,:capital_cost], LK_p_nom[:]) - + sum(dot(storage_units[:marginal_cost], SU_dispatch[:,t]) for t=1:T) + + sum(dot(storage_units[!, :marginal_cost], SU_dispatch[:,t]) for t=1:T) + dot(storage_units[ext_sus_b, :capital_cost], SU_p_nom[:]) - + sum(dot(stores[:marginal_cost], ST_dispatch[:,t]) for t=1:T) + + sum(dot(stores[!, :marginal_cost], ST_dispatch[:,t]) for t=1:T) + dot(stores[ext_stores_b, :capital_cost], ST_e_nom[:]) ) - status = solve(m) + optimize!(m) # -------------------------------------------------------------------------------------------------------- # 10. extract optimisation results - if status==:Optimal - orig_gen_order = network.generators[:name] - generators[:p_nom_opt] = deepcopy(generators[:p_nom]) + if termination_status(m)==MOI.OPTIMAL + orig_gen_order = network.generators[!, :name] + generators[!, :p_nom_opt] = deepcopy(generators[!, :p_nom]) generators[ext_gens_b,:p_nom_opt] = getvalue(gen_p_nom) network.generators = generators - network.generators_t["p"] = names!(DataFrame(transpose(getvalue(G))), Symbol.(generators[:name])) + network.generators_t["p"] = rename!(DataFrame(transpose(getvalue(G))), Symbol.(generators[!, :name])) network.generators = select_names(network.generators, orig_gen_order) - orig_line_order = network.lines[:name] + orig_line_order = network.lines[!, :name] network.lines = lines - lines[:s_nom_opt] = deepcopy(lines[:s_nom]) + lines[:s_nom_opt] = deepcopy(lines[!, :s_nom]) network.lines[ext_lines_b,:s_nom_opt] = getvalue(LN_s_nom) - network.lines_t["p0"] = names!(DataFrame(transpose(getvalue(LN))), Symbol.(lines[:name])) + network.lines_t["p0"] = rename!(DataFrame(transpose(getvalue(LN))), Symbol.(lines[!, :name])) network.lines = select_names(network.lines, orig_line_order) # network.buses_t["p"] = DataFrame(ncols=nrow(network.buses)) if nrow(links)>0 - orig_link_order = network.links[:name] + orig_link_order = network.links[!, :name] network.links = links - links[:p_nom_opt] = deepcopy(links[:p_nom]) + links[:p_nom_opt] = deepcopy(links[!, :p_nom]) network.links[ext_links_b,:p_nom_opt] = getvalue(LK_p_nom) - network.links_t["p0"] = names!(DataFrame(transpose(getvalue(LK))), Symbol.(links[:name])) + network.links_t["p0"] = rename!(DataFrame(transpose(getvalue(LK))), Symbol.(links[!, :name])) network.links = select_names(network.links, orig_link_order) end if nrow(storage_units)>0 - orig_sus_order = network.storage_units[:name] + orig_sus_order = network.storage_units[!, :name] network.storage_units = storage_units - storage_units[:p_nom_opt] = deepcopy(storage_units[:p_nom]) + storage_units[:p_nom_opt] = deepcopy(storage_units[!, :p_nom]) network.storage_units[ext_sus_b,:p_nom_opt] = getvalue(SU_p_nom) # network.storage_units_t["spill"] = spillage # network.storage_units_t["spill"][:,spill_sus_b] = names!(DataFrame(transpose(getvalue(SU_spill))), # names(spillage)[spill_sus_b]) - network.storage_units_t["spill"] = names!(DataFrame(transpose(getvalue(SU_spill))), - Symbol.(storage_units[:name])) - network.storage_units_t["p"] = names!(DataFrame(transpose(getvalue(SU_dispatch .- SU_store))), - Symbol.(storage_units[:name])) - network.storage_units_t["state_of_charge"] = names!(DataFrame(transpose(getvalue(SU_soc))), - Symbol.(storage_units[:name])) + network.storage_units_t["spill"] = rename!(DataFrame(transpose(getvalue(SU_spill))), + Symbol.(storage_units[!, :name])) + network.storage_units_t["p"] = rename!(DataFrame(transpose(getvalue(SU_dispatch .- SU_store))), + Symbol.(storage_units[!, :name])) + network.storage_units_t["state_of_charge"] = rename!(DataFrame(transpose(getvalue(SU_soc))), + Symbol.(storage_units[!, :name])) network.storage_units = select_names(network.storage_units, orig_sus_order) end align_component_order!(network) From b215782e18b5b04835482ae20f1dcdd23911085c Mon Sep 17 00:00:00 2001 From: fleimgruber Date: Sat, 20 Feb 2021 08:51:52 +0100 Subject: [PATCH 2/3] build: project definition with dependencies for dev and dist --- Project.toml | 14 ++++++++++++++ examples/ac-dc-lopf.jl | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Project.toml diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..7825c23 --- /dev/null +++ b/Project.toml @@ -0,0 +1,14 @@ +name = "PSA" +uuid = "4c80e164-764c-4b93-bf2f-9dd64506ca8b" +authors = ["Tom Brown "] +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" diff --git a/examples/ac-dc-lopf.jl b/examples/ac-dc-lopf.jl index bf109b5..8012ccb 100644 --- a/examples/ac-dc-lopf.jl +++ b/examples/ac-dc-lopf.jl @@ -4,6 +4,7 @@ import PSA using Clp +using JuMP network = PSA.import_network("/home/tom/fias/lib/pypsa/examples/ac-dc-meshed/ac-dc-data/") @@ -11,6 +12,6 @@ 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"]) From d55ae60716881c287fdfc19699e052c20b7678c8 Mon Sep 17 00:00:00 2001 From: fleimgruber Date: Thu, 25 Feb 2021 09:50:55 +0100 Subject: [PATCH 3/3] fix: DataFrames and JuMP API for solution --- src/auxilliaries.jl | 4 ++-- src/lopf.jl | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/auxilliaries.jl b/src/auxilliaries.jl index 1d48347..d4b8076 100644 --- a/src/auxilliaries.jl +++ b/src/auxilliaries.jl @@ -41,9 +41,9 @@ 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 diff --git a/src/lopf.jl b/src/lopf.jl index 6cd3c79..3b76147 100644 --- a/src/lopf.jl +++ b/src/lopf.jl @@ -80,7 +80,7 @@ function lopf(network, solver) p_min_pu = select_time_dep(network, "generators", "p_min_pu",components=ext_gens_b) p_max_pu = select_time_dep(network, "generators", "p_max_pu",components=ext_gens_b) p_nom_min = network.generators[ext_gens_b,:p_nom_min] - p_nom_max = network.generators[ext_gens_b,:p_nom] + p_nom_max = ones(length(p_nom_min)) * Inf64 @variable m G_ext[gr=1:N_ext,t = 1:T] @@ -435,17 +435,17 @@ function lopf(network, solver) if termination_status(m)==MOI.OPTIMAL orig_gen_order = network.generators[!, :name] generators[!, :p_nom_opt] = deepcopy(generators[!, :p_nom]) - generators[ext_gens_b,:p_nom_opt] = getvalue(gen_p_nom) + generators[ext_gens_b,:p_nom_opt] = value.(gen_p_nom) network.generators = generators - network.generators_t["p"] = rename!(DataFrame(transpose(getvalue(G))), Symbol.(generators[!, :name])) + network.generators_t["p"] = rename!(DataFrame(transpose(value.(G))), Symbol.(generators[!, :name])) network.generators = select_names(network.generators, orig_gen_order) orig_line_order = network.lines[!, :name] network.lines = lines - lines[:s_nom_opt] = deepcopy(lines[!, :s_nom]) - network.lines[ext_lines_b,:s_nom_opt] = getvalue(LN_s_nom) - network.lines_t["p0"] = rename!(DataFrame(transpose(getvalue(LN))), Symbol.(lines[!, :name])) + lines[!, :s_nom_opt] = deepcopy(lines[!, :s_nom]) + network.lines[ext_lines_b,:s_nom_opt] = value.(LN_s_nom) + network.lines_t["p0"] = rename!(DataFrame(transpose(value.(LN))), Symbol.(lines[!, :name])) network.lines = select_names(network.lines, orig_line_order) # network.buses_t["p"] = DataFrame(ncols=nrow(network.buses)) @@ -453,9 +453,9 @@ function lopf(network, solver) if nrow(links)>0 orig_link_order = network.links[!, :name] network.links = links - links[:p_nom_opt] = deepcopy(links[!, :p_nom]) - network.links[ext_links_b,:p_nom_opt] = getvalue(LK_p_nom) - network.links_t["p0"] = rename!(DataFrame(transpose(getvalue(LK))), Symbol.(links[!, :name])) + links[!, :p_nom_opt] = deepcopy(links[!, :p_nom]) + network.links[ext_links_b,:p_nom_opt] = value.(LK_p_nom) + network.links_t["p0"] = rename!(DataFrame(transpose(value.(LK))), Symbol.(links[!, :name])) network.links = select_names(network.links, orig_link_order) end @@ -463,21 +463,21 @@ function lopf(network, solver) orig_sus_order = network.storage_units[!, :name] network.storage_units = storage_units - storage_units[:p_nom_opt] = deepcopy(storage_units[!, :p_nom]) - network.storage_units[ext_sus_b,:p_nom_opt] = getvalue(SU_p_nom) + storage_units[!, :p_nom_opt] = deepcopy(storage_units[!, :p_nom]) + network.storage_units[ext_sus_b,:p_nom_opt] = value.(SU_p_nom) # network.storage_units_t["spill"] = spillage # network.storage_units_t["spill"][:,spill_sus_b] = names!(DataFrame(transpose(getvalue(SU_spill))), # names(spillage)[spill_sus_b]) - network.storage_units_t["spill"] = rename!(DataFrame(transpose(getvalue(SU_spill))), + network.storage_units_t["spill"] = rename!(DataFrame(transpose(value.(SU_spill))), Symbol.(storage_units[!, :name])) - network.storage_units_t["p"] = rename!(DataFrame(transpose(getvalue(SU_dispatch .- SU_store))), + network.storage_units_t["p"] = rename!(DataFrame(transpose(value.(SU_dispatch .- SU_store))), Symbol.(storage_units[!, :name])) - network.storage_units_t["state_of_charge"] = rename!(DataFrame(transpose(getvalue(SU_soc))), + network.storage_units_t["state_of_charge"] = rename!(DataFrame(transpose(value.(SU_soc))), Symbol.(storage_units[!, :name])) network.storage_units = select_names(network.storage_units, orig_sus_order) end align_component_order!(network) - println("Reduce cost to $(m.objVal)") + println("Reduce cost to $(objective_value(m))") end return m end