Skip to content

Commit

Permalink
Convert DenseAxisArray to AxisArray explicitly where necessary
Browse files Browse the repository at this point in the history
Better strategy would be to store everything as AxisArray from the outset.
  • Loading branch information
coroa committed Jun 4, 2019
1 parent 31d13df commit 01a8b8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/components/branch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end

busattributes(c::Branch) = (:bus0, :bus1)
function p(c::Branch)
p = c[:p]
p = AxisArray(c[:p])
((b,t)->p[b,t], # :bus0
(b,t)->-p[b,t]) # :bus1
end
Expand All @@ -25,13 +25,13 @@ end
# nodalbalance(c::Link) = @emaggregator(c, l, c[:bus0] => c[:p][l], c[:bus1] => -c[:p][l])

function p(c::Link)
p = c[:p]
p = AxisArray(c[:p])
eff = c[:efficiency]
((l,t)->-p[l,t], # :bus0
(l,t)->eff[l]*p[l,t]) # :bus1
end

cost(c::Link) = sum(c[:marginal_cost] .* c[:p]) + sum(c[:capital_cost] .* (c[:p_nom] - getparam(c, :p_nom)))
cost(c::Link) = sum(c[:marginal_cost] .* AxisArray(c[:p])) + sum(c[:capital_cost] .* (AxisArray(c[:p_nom]) - getparam(c, :p_nom)))
function addto!(jm::ModelView, m::EnergyModel, c::Link)
T = axis(m, :snapshots)
L = axis(c)
Expand Down Expand Up @@ -175,7 +175,7 @@ function impedance(c::Transformer)
end


cost(c::PassiveBranch) = sum(c[:capital_cost] .* (c[:s_nom] .- getparam(c, :s_nom)))
cost(c::PassiveBranch) = sum(c[:capital_cost] .* (AxisArray(c[:s_nom]) .- getparam(c, :s_nom)))

function addto!(jm::ModelView, m::EnergyModel, c::PassiveBranch)
T = axis(m, :snapshots)
Expand Down
14 changes: 7 additions & 7 deletions src/components/oneport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ end
busattributes(c::OnePort) = (:bus,)

## Defaults for OnePort
cost(c::OnePort) = sum(c[:marginal_cost] .* c[:p]) + sum(c[:capital_cost] .* (c[:p_nom] - getparam(c, :p_nom)))
cost(c::OnePort) = sum(c[:marginal_cost] .* AxisArray(c[:p])) + sum(c[:capital_cost] .* (AxisArray(c[:p_nom]) .- getparam(c, :p_nom)))
function p(c::OnePort)
p = c[:p]
p = AxisArray(c[:p])
((o,t)->p[o,t],)
end

Expand Down Expand Up @@ -49,10 +49,10 @@ end
addelement(Generator, :generators, (:G, :T=>:snapshots), joinpath(@__DIR__, "generators.csv"))

## StorageUnit
cost(c::StorageUnit) = sum(c[:marginal_cost] .* c[:p_dispatch]) + sum(c[:capital_cost] .* (c[:p_nom] - getparam(c, :p_nom)))
cost(c::StorageUnit) = sum(c[:marginal_cost] .* AxisArray(c[:p_dispatch])) + sum(c[:capital_cost] .* (AxisArray(c[:p_nom]) - getparam(c, :p_nom)))
function p(c::StorageUnit)
p_dispatch = c[:p_dispatch]
p_store = c[:p_store]
p_dispatch = AxisArray(c[:p_dispatch])
p_store = AxisArray(c[:p_store])

((s,t)->p_dispatch[s,t] - p_store[s,t],)
end
Expand Down Expand Up @@ -117,7 +117,7 @@ end
addelement(StorageUnit, :storageunits, (:S, :T=>:snapshots), joinpath(@__DIR__, "storageunits.csv"))

## Store
cost(c::Store) = sum(c[:marginal_cost] .* c[:p]) + sum(c[:capital_cost] .* (c[:e_nom] - getparam(c, :e_nom)))
cost(c::Store) = sum(c[:marginal_cost] .* AxisArray(c[:p])) + sum(c[:capital_cost] .* (AxisArray(c[:e_nom]) - getparam(c, :e_nom)))
function addto!(jm::ModelView, m::EnergyModel, c::Store)
T = axis(c, :snapshots)
S = axis(c)
Expand Down Expand Up @@ -166,7 +166,7 @@ addelement(Store, :stores, (:S, :T=>:snapshots), joinpath(@__DIR__, "stores.csv"
## Load
cost(c::Load) = 0.
function p(c::Load)
p = c[:p_set]
p = AxisArray(c[:p_set])
((l,t)->-p[l,t],)
end
addto!(jm::JuMP.AbstractModel, m::EnergyModel, c::Load) = nothing
Expand Down
5 changes: 2 additions & 3 deletions src/containerviews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ end

mapcat(f::Function, cv::AbstractContainerView) = cat(cv, collect(components(cv)), map(f, components(cv)))

# TODO Are both definitions necessary?
Base.get(cv::AbstractContainerView, i) = mapcat(c->c[i], cv)
Base.get(cv::AbstractContainerView, i::Symbol) = mapcat(c->c[i], cv)
# TODO The AxisArray that is spliced in here is a mild hack!
Base.get(cv::AbstractContainerView, i) = mapcat(c->AxisArray(get(c, i)), cv)
Base.get(cv::AbstractContainerView, i, axes...) = WrappedArray(get(cv, i), axes...)

Base.getindex(cv::AbstractContainerView, i) = get(cv, i)

0 comments on commit 01a8b8a

Please sign in to comment.