Skip to content
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
2 changes: 1 addition & 1 deletion src/TimeStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ include("op_scenarios/rep_periods.jl")
include("op_scenarios/strat_periods.jl")
include("op_scenarios/tree_periods.jl")

include("utils.jl")
include("discount.jl")
include("profiles.jl")
include("utils.jl")

include("partitions/strat_periods.jl")
include("partitions/rep_periods.jl")
Expand Down
4 changes: 3 additions & 1 deletion src/partitions/opscenarios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ struct OpScenPart{N,T} <: AbstractOpScenPart{T}
chunk::NTuple{N,T}
end
PeriodPartition(itr::OperationalScenario, part, chunk) = OpScenPart(itr.scen, part, chunk)
eltype(::Type{PartitionDurationIterator{I}}) where {I<:OperationalScenario} = OpScenPart
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:OperationalScenario,T,D}
return OpScenPart
end

Base.show(io::IO, pd::OpScenPart) = print(io, "sc$(pd.scen)-part$(pd.part)")

Expand Down
10 changes: 8 additions & 2 deletions src/partitions/rep_periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ struct ReprPart{N,T} <: AbstractReprPart{T}
chunk::NTuple{N,T}
end
PeriodPartition(itr::RepresentativePeriod, part, chunk) = ReprPart(itr.rp, part, chunk)
eltype(::Type{PartitionDurationIterator{I}}) where {I<:RepresentativePeriod} = ReprPart
function eltype(
::Type{PartitionDurationIterator{I,T,D}},
) where {I<:RepresentativePeriod,T,D}
return ReprPart
end

Base.show(io::IO, pd::ReprPart) = print(io, "rp$(pd.rp)-part$(pd.part)")

Expand All @@ -26,7 +30,9 @@ end
function PeriodPartition(itr::ReprOpScenario, part, chunk)
return ReprOpScenPart(itr.rp, itr.scen, part, chunk)
end
eltype(::Type{PartitionDurationIterator{I}}) where {I<:ReprOpScenario} = ReprOpScenPart
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:ReprOpScenario,T,D}
return ReprOpScenPart
end

Base.show(io::IO, pd::ReprOpScenPart) = print(io, "rp$(pd.rp)-sc$(pd.scen)-part$(pd.part)")
ScenarioIndexable(::Type{<:ReprOpScenPart}) = HasScenarioIndex()
Expand Down
12 changes: 8 additions & 4 deletions src/partitions/strat_periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct StratPart{N,T} <: AbstractStratPart{T}
chunk::NTuple{N,T}
end
PeriodPartition(itr::StrategicPeriod, part, chunk) = StratPart(itr.sp, part, chunk)
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StrategicPeriod} = StratPart
eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StrategicPeriod,T,D} = StratPart

Base.show(io::IO, pd::StratPart) = print(io, "sp$(pd.sp)-part$(pd.part)")

Expand All @@ -26,7 +26,9 @@ end
function PeriodPartition(itr::StratReprPeriod, part, chunk)
return StratReprPart(itr.sp, itr.rp, part, chunk)
end
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratReprPeriod} = StratReprPart
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratReprPeriod,T,D}
return StratReprPart
end

Base.show(io::IO, pd::StratReprPart) = print(io, "sp$(pd.sp)-rp$(pd.rp)-part$(pd.part)")
RepresentativeIndexable(::Type{<:StratReprPart}) = HasReprIndex()
Expand All @@ -43,7 +45,9 @@ end
function PeriodPartition(itr::StratOpScenario, part, chunk)
return StratOpScenPart(itr.sp, itr.scen, part, chunk)
end
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratOpScenario} = StratOpScenPart
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratOpScenario,T,D}
return StratOpScenPart
end

function Base.show(io::IO, pd::StratOpScenPart)
return print(io, "sp$(pd.sp)-sc$(pd.scen)-part$(pd.part)")
Expand All @@ -63,7 +67,7 @@ end
function PeriodPartition(itr::StratReprOpScenario, part, chunk)
return StratReprOpScenPart(itr.sp, itr.rp, itr.scen, part, chunk)
end
function eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratReprOpScenario}
function eltype(::Type{PartitionDurationIterator{I,T,D}}) where {I<:StratReprOpScenario,T,D}
return StratReprOpScenPart
end

Expand Down
26 changes: 26 additions & 0 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,29 @@ _total_duration(tss::Vector) = sum(duration(ts) for ts in tss)
_multiple_adj(ts::TimeStructure, per) = 1.0

stripunit(val) = val

"""
abstract type PeriodPartition{T<:TimePeriod}

Supertype for individual partitions for operational time periods. Subtypes must be created
for all potential time structures to be able to identify the respective
[`TimeStructurePeriod`](@ref).
"""

abstract type PeriodPartition{T<:TimePeriod} end

Base.iterate(pd::PeriodPartition) = iterate(pd.chunk)
Base.iterate(pd::PeriodPartition, state) = iterate(pd.chunk, state)
Base.length(pd::PeriodPartition) = length(pd.chunk)
Base.first(pd::PeriodPartition) = first(pd.chunk)
Base.last(pd::PeriodPartition) = last(pd.chunk)

_part(pd::PeriodPartition) = pd.part

abstract type PartitionIndexable end

struct HasPartIndex <: PartitionIndexable end
struct NoPartIndex <: PartitionIndexable end

PartitionIndexable(::Type) = NoPartIndex()
PartitionIndexable(::Type{<:PeriodPartition}) = HasPartIndex()
56 changes: 15 additions & 41 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,44 +171,15 @@ function chunk_duration(_::StratTreeNodes{S,T,OP}, _) where {S,T,OP}
"`chunk_duration` can not be used when iterating nodes of a strategic tree structure.",
)
end

"""
abstract type PeriodPartition{T<:TimePeriod}

Supertype for individual partitions based on durations for operational time periods. Subtypes
must be created for all potential time structures to be able to identify the respective
[`TimeStructurePeriod`](@ref).
"""

abstract type PeriodPartition{T<:TimePeriod} end

function PeriodPartition(itr, part, chunk)
return throw(
ArgumentError(
"The type` PeriodPartition` called through `period_duration` is not " *
"implemented for iterator type $(typeof(itr))",
),
)
end
Base.iterate(pd::PeriodPartition) = iterate(pd.chunk)
Base.iterate(pd::PeriodPartition, state) = iterate(pd.chunk, state)
Base.length(pd::PeriodPartition) = length(pd.chunk)
Base.first(pd::PeriodPartition) = first(pd.chunk)
Base.last(pd::PeriodPartition) = last(pd.chunk)

_part(pd::PeriodPartition) = pd.part

abstract type PartitionIndexable end

struct HasPartIndex <: PartitionIndexable end
struct NoPartIndex <: PartitionIndexable end

PartitionIndexable(::Type) = NoPartIndex()
PartitionIndexable(::Type{<:PeriodPartition}) = HasPartIndex()

struct PartitionDurationIterator{I<:TimeStructure}
struct PartitionDurationIterator{I<:TimeStructure,T<:Duration,D<:TimeProfile{T}}
itr::I
duration::TimeStruct.Duration
duration::D
end
function PartitionDurationIterator(itr::TimeStructure, dur::Duration)
return PartitionDurationIterator(itr, FixedProfile(dur))
end
function PartitionDurationIterator(itr::TimeStructure, dur::Vector{<:Duration})
return PartitionDurationIterator(itr, PartitionProfile(dur))
end

"""
Expand All @@ -228,19 +199,22 @@ over the following time periods until at least `dur` time is covered or the end
partition_duration(itr, dur) = PartitionDurationIterator(itr, dur)

IteratorSize(::Type{<:PartitionDurationIterator}) = Base.SizeUnknown()
IteratorEltype(::Type{PartitionDurationIterator{I}}) where {I} = Base.HasEltype()

function Base.iterate(w::PartitionDurationIterator, state = (nothing, 1))
function Base.iterate(
w::PartitionDurationIterator{I,T,D},
state = (nothing, 1),
) where {I,T,D}
isa(state[1], Iterators.IterationCutShort) && return nothing
y = iterate(w.itr, state[1])
isnothing(y) && return nothing
part = state[2]
dur_part = w.duration[PeriodPartition(w.itr, part, (y[1],))]
chunk = eltype(w.itr)[]
acc = zero(w.duration)
acc = zero(T)
while !isnothing(y)
push!(chunk, y[1])
acc += duration(y[1])
acc >= w.duration && break
acc >= dur_part && break
y = iterate(w.itr, y[2])
end
pd = PeriodPartition(w.itr, part, Tuple(chunk))
Expand Down
Loading
Loading