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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TimeStruct"
uuid = "f9ed5ce0-9f41-4eaa-96da-f38ab8df101c"
authors = ["Lars Hellemo <Lars.Hellemo@sintef.no>, Truls.Flatberg <Truls.Flatberg@sintef.no>"]
version = "0.9.11"
version = "0.9.12"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
1 change: 1 addition & 0 deletions src/TimeStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ include("profiles.jl")
include("utils.jl")

include("partitions/strat_periods.jl")
include("partitions/tree_periods.jl")
include("partitions/rep_periods.jl")
include("partitions/opscenarios.jl")

Expand Down
134 changes: 134 additions & 0 deletions src/partitions/tree_periods.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Add generic partition duration type
abstract type AbstractTreePart{T} <: AbstractStratPart{T} end

StrategicTreeIndexable(::Type{<:AbstractTreePart}) = HasStratTreeIndex()
_branch(pd::AbstractTreePart) = pd.branch

# Add partition type with constructor for indexing when strategic periods are present
struct StratNodePart{N,T} <: AbstractTreePart{T}
sp::Int
branch::Int
part::Int
chunk::NTuple{N,T}
end
function PeriodPartition(itr::StratNode, part, chunk)
return StratNodePart(_strat_per(itr), _branch(itr), part, chunk)
end
eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratNode,T,D} = StratNodePart

function Base.show(io::IO, pd::StratNodePart)
return print(io, "sp$(_strat_per(pd))-br$(_branch(pd))-part$(_part(pd))")
end

# Add partition type with constructor for indexing when strategic and representative periods
# are present
struct StratNodeReprPart{N,T} <: AbstractTreePart{T}
sp::Int
branch::Int
rp::Int
part::Int
chunk::NTuple{N,T}
end
function PeriodPartition(itr::StratNodeReprPeriod, part, chunk)
return StratNodeReprPart(_strat_per(itr), _branch(itr), _rper(itr), part, chunk)
end
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratNodeReprPeriod,T,D}
return StratNodeReprPart
end

RepresentativeIndexable(::Type{<:StratNodeReprPart}) = HasReprIndex()
_rper(pd::StratNodeReprPart) = pd.rp

function Base.show(io::IO, pd::StratNodeReprPart)
return print(io, "sp$(_strat_per(pd))-br$(_branch(pd))-rp$(_rper(pd))-part$(_part(pd))")
end

# Add partition type with constructor for indexing when strategic periods and operational
# scenarios are present
struct StratNodeOpScenPart{N,T} <: AbstractTreePart{T}
sp::Int
branch::Int
scen::Int
part::Int
chunk::NTuple{N,T}
end
function PeriodPartition(itr::StratNodeOpScenario, part, chunk)
return StratNodeOpScenPart(_strat_per(itr), _branch(itr), _opscen(itr), part, chunk)
end
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratNodeOpScenario,T,D}
return StratNodeOpScenPart
end

function Base.show(io::IO, pd::StratNodeOpScenPart)
return print(
io,
"sp$(_strat_per(pd))-br$(_branch(pd))-sc$(_opscen(pd))-part$(_part(pd))",
)
end
ScenarioIndexable(::Type{<:StratNodeOpScenPart}) = HasScenarioIndex()
_opscen(pd::StratNodeOpScenPart) = pd.scen

# Add partition type with constructor for indexing when strategic periods, representative
# periods and operational scenarios are present
struct StratNodeReprOpScenPart{N,T} <: AbstractTreePart{T}
sp::Int
branch::Int
rp::Int
scen::Int
part::Int
chunk::NTuple{N,T}
end
function PeriodPartition(itr::StratNodeReprOpScenario, part, chunk)
return StratNodeReprOpScenPart(
_strat_per(itr),
_branch(itr),
_rper(itr),
_opscen(itr),
part,
chunk,
)
end
function eltype(
::Type{PartitionDurationIterator{I,T,D}},
) where {I<:StratNodeReprOpScenario,T,D}
return StratNodeReprOpScenPart
end

function Base.show(io::IO, pd::StratNodeReprOpScenPart)
return print(
io,
"sp$(_strat_per(pd))-br$(_branch(pd))-rp$(_rper(pd))-sc$(_opscen(pd))-part$(_part(pd))",
)
end
RepresentativeIndexable(::Type{<:StratNodeReprOpScenPart}) = HasReprIndex()
ScenarioIndexable(::Type{<:StratNodeReprOpScenPart}) = HasScenarioIndex()
_rper(pd::StratNodeReprOpScenPart) = pd.rp
_opscen(pd::StratNodeReprOpScenPart) = pd.scen

# Add function for generation of partitions from higher level
function partition_duration(ts::TwoLevelTree, dur)
return collect(
Iterators.flatten(partition_duration(sp, dur) for sp in strategic_periods(ts)),
)
end
function partition_duration(
ts::StratNode{S,T,OP},
dur,
) where {S,T,OP<:RepresentativePeriods}
return collect(
Iterators.flatten(partition_duration(rp, dur) for rp in repr_periods(ts)),
)
end
function partition_duration(ts::StratNode{S,T,OP}, dur) where {S,T,OP<:OperationalScenarios}
return collect(
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
)
end
function partition_duration(
ts::StratNodeReprPeriod{T,RepresentativePeriod{T,OP}},
dur,
) where {T,OP<:OperationalScenarios}
return collect(
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
)
end
8 changes: 6 additions & 2 deletions src/profiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ end
function Base.getindex(
ssp::StrategicStochasticProfile,
period::T,
) where {T<:Union{TimePeriod,TimeStructurePeriod}}
) where {T<:Union{TimePeriod,TimeStructurePeriod,PeriodPartition}}
return _value_lookup(StrategicTreeIndexable(T), ssp, period)
end

Expand All @@ -384,7 +384,7 @@ Base.getindex(TP::TimeProfile, inds...) = [TP[i] for i in inds]
function Base.getindex(
TP::TimeProfile,
inds::Vector{T},
) where {T<:Union{TimePeriod,TimeStructurePeriod}}
) where {T<:Union{TimePeriod,TimeStructurePeriod,PeriodPartition}}
return [TP[i] for i in inds]
end
function Base.getindex(
Expand Down Expand Up @@ -563,6 +563,10 @@ function +(a::StrategicStochasticProfile{T}, b::OperationalProfile{S}) where {T,
return StrategicStochasticProfile([[p + b for p in v] for v in a.vals])
end
+(a::OperationalProfile{T}, b::StrategicStochasticProfile{S}) where {T,S} = b + a
function +(a::StrategicStochasticProfile{T}, b::PartitionProfile{S}) where {T,S}
return StrategicStochasticProfile([[p + b for p in v] for v in a.vals])
end
+(a::PartitionProfile{T}, b::StrategicStochasticProfile{S}) where {T,S} = b + a
function +(a::FixedProfile{T}, b::StrategicStochasticProfile{S}) where {T,S}
return StrategicStochasticProfile([[a + p for p in v] for v in b.vals])
end
Expand Down
Loading
Loading