Skip to content

Commit

Permalink
Reorganize repository structure
Browse files Browse the repository at this point in the history
  • Loading branch information
GordStephen committed Nov 30, 2024
1 parent 32dd37e commit 10a7c1c
Show file tree
Hide file tree
Showing 91 changed files with 2,348 additions and 2,714 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.jl.mem
Manifest.toml
*.DS_Store
*.pras
26 changes: 26 additions & 0 deletions PRAS/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name = "PRAS"
uuid = "05348d26-1c52-11e9-35e3-9d51842d34b9"
authors = [
"Gord Stephen <Gord.Stephen@nrel.gov>",
"Surya Chandan Dhulipala <SuryaChandan.Dhulipala@nrel.gov>",
"Hari Sundar <Hari.Sundar@nrel.gov>"
]
version = "0.7.0"

[deps]
PRASCapacityCredits = "2e1a2ed5-e89d-4cd3-bc86-c0e88a73d3a3"
PRASCore = "c5c32b99-e7c3-4530-a685-6f76e19f7fe2"
PRASFiles = "a2806276-6d43-4ef5-91c0-491704cd7cf1"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
PRASCapacityCredits = "0.7"
PRASCore = "0.7"
PRASFiles = "0.7"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
10 changes: 10 additions & 0 deletions PRAS/src/PRAS.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module PRAS

using Reexport

@reexport using PRASCore
@reexport using PRASCapacityCredits

import PRASFiles: toymodel, rts_gmlc

end
12 changes: 12 additions & 0 deletions PRAS/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PRAS
using Test

sys = PRAS.rts_gmlc()

sf, = assess(sys, SequentialMonteCarlo(samples=100), Shortfall())

eue = EUE(sf)
lole = LOLE(sf)

@test val(eue) isa Float64
@test stderror(eue) isa Float64
19 changes: 19 additions & 0 deletions PRASCapacityCredits/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "PRASCapacityCredits"
uuid = "2e1a2ed5-e89d-4cd3-bc86-c0e88a73d3a3"
authors = ["Gord Stephen <gord.stephen@nrel.gov>"]
version = "0.7.0"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
PRASCore = "c5c32b99-e7c3-4530-a685-6f76e19f7fe2"

[compat]
Distributions = "0.25"
PRASCore = "0.7"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function EFC{M}(
end

function assess(sys_baseline::S, sys_augmented::S,
params::EFC{M}, simulationspec::SimulationSpec
params::EFC{M}, simulationspec::SequentialMonteCarlo
) where {N, L, T, P, S <: SystemModel{N,L,T,P}, M <: ReliabilityMetric}

_, powerunit, _ = unitsymbol(sys_baseline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function ELCC{M}(
end

function assess(sys_baseline::S, sys_augmented::S,
params::ELCC{M}, simulationspec::SimulationSpec
params::ELCC{M}, simulationspec::SequentialMonteCarlo
) where {N, L, T, P, S <: SystemModel{N,L,T,P}, M <: ReliabilityMetric}

_, powerunit, _ = unitsymbol(sys_baseline)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@reexport module CapacityCredit
module PRASCapacityCredits

import PRASCore.Systems: Generators, PowerUnit, Regions, SystemModel, unitsymbol
import PRASCore.Simulations: assess, SequentialMonteCarlo
import PRASCore.Results: ReliabilityMetric, Result, Shortfall, stderror, val

import Base: minimum, maximum, extrema
import Distributions: ccdf, Normal
import ..PRASBase: Generators, PowerUnit, Regions, SystemModel, unitsymbol
import ..ResourceAdequacy: assess, ReliabilityMetric, Result, Shortfall,
SimulationSpec, stderror, val

export EFC, ELCC

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
@testset "CapacityCredit" begin
using PRASCapacityCredits
using PRASCore
using Test

import PRASCore.Systems: TestData

@testset "PRASCapacityCredits" begin

empty_str = String[]
empty_int(x) = Matrix{Int}(undef, 0, x)
Expand Down Expand Up @@ -31,7 +37,7 @@

load = [25, 28, 27, 24]

tz = TimeZone("UTC")
tz = tz"UTC"
timestamps = ZonedDateTime(2010,1,1,0,tz):Hour(1):ZonedDateTime(2010,1,1,3,tz)

sys_before = SystemModel(
Expand All @@ -40,41 +46,27 @@
sys_after = SystemModel(
gens_after, emptystors, emptygenstors, timestamps, load)

threenode2 = deepcopy(TestSystems.threenode)
threenode2 = deepcopy(TestData.threenode)
threenode2.generators.capacity[1, :] .+= 5

conv = Convolution(threaded=false)
smc = SequentialMonteCarlo(samples=100_000, threaded=false)

@testset "EFC" begin

cc = assess(sys_before, sys_before, EFC{EUE}(10, "Region"), conv)
@test extrema(cc) == (0, 1)

cc = assess(sys_before, sys_after, EFC{EUE}(10, ["Region" => 1.0]), conv)
@test extrema(cc) == (8, 9)

cc = assess(sys_before, sys_after, EFC{EUE}(10, "Region"), smc)
@test extrema(cc) == (8, 9)

cc = assess(TestSystems.threenode, threenode2, EFC{EUE}(10, "Region A"), smc)
cc = assess(TestData.threenode, threenode2, EFC{EUE}(10, "Region A"), smc)
@test extrema(cc) == (3, 4)

end

@testset "ELCC" begin


cc = assess(sys_before, sys_before, ELCC{EUE}(10, "Region"), conv)
@test extrema(cc) == (0, 1)

cc = assess(sys_before, sys_after, ELCC{EUE}(10, ["Region" => 1.0]), conv)
@test extrema(cc) == (7, 8)

cc = assess(sys_before, sys_after, ELCC{EUE}(10, "Region"), smc)
@test extrema(cc) == (7, 8)

cc = assess(TestSystems.threenode, threenode2, ELCC{EUE}(10, "Region A"), smc)
cc = assess(TestData.threenode, threenode2, ELCC{EUE}(10, "Region A"), smc)
@test extrema(cc) == (3, 4)

end
Expand Down
10 changes: 4 additions & 6 deletions Project.toml → PRASCore/Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
name = "PRAS"
uuid = "05348d26-1c52-11e9-35e3-9d51842d34b9"
name = "PRASCore"
uuid = "c5c32b99-e7c3-4530-a685-6f76e19f7fe2"
authors = ["Gord Stephen <gord.stephen@nrel.gov>"]
version = "0.6.4"
version = "0.7.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
MinCostFlows = "62286e6e-1779-56f1-888a-1c0056788ce0"
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
OnlineStatsBase = "925886fa-5bf2-5e8e-b522-a9147a512338"
Expand All @@ -18,7 +16,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[compat]
HDF5 = "0.16,0.17"
Reexport = "1.2.2"
julia = "1.10"

[extras]
Expand Down
9 changes: 9 additions & 0 deletions PRASCore/src/PRASCore.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PRASCore

import Reexport: @reexport

include("Systems/Systems.jl")
include("Results/Results.jl")
include("Simulations/Simulations.jl")

end
92 changes: 92 additions & 0 deletions PRASCore/src/Results/Flow.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Flow
Flow metric represents the flow between interfaces at timestamps
in a FlowResult with a (interfaces, timestamps) matrix API.
Separate samples are averaged together into mean and std values.
See [`FlowSamples`](@ref) for all flow samples.
"""
struct Flow <: ResultSpec end

struct FlowAccumulator <: ResultAccumulator{Flow}

flow_interface::Vector{MeanVariance}
flow_interfaceperiod::Matrix{MeanVariance}

flow_interface_currentsim::Vector{Int}

end

function accumulator(
sys::SystemModel{N}, nsamples::Int, ::Flow
) where {N}

n_interfaces = length(sys.interfaces)
flow_interface = [meanvariance() for _ in 1:n_interfaces]
flow_interfaceperiod = [meanvariance() for _ in 1:n_interfaces, _ in 1:N]

flow_interface_currentsim = zeros(Int, n_interfaces)

return FlowAccumulator(
flow_interface, flow_interfaceperiod, flow_interface_currentsim)

end

function merge!(
x::FlowAccumulator, y::FlowAccumulator
)

foreach(merge!, x.flow_interface, y.flow_interface)
foreach(merge!, x.flow_interfaceperiod, y.flow_interfaceperiod)

end

accumulatortype(::Flow) = FlowAccumulator

struct FlowResult{N,L,T<:Period,P<:PowerUnit} <: AbstractFlowResult{N,L,T}

nsamples::Union{Int,Nothing}
interfaces::Vector{Pair{String,String}}
timestamps::StepRange{ZonedDateTime,T}

flow_mean::Matrix{Float64}

flow_interface_std::Vector{Float64}
flow_interfaceperiod_std::Matrix{Float64}

end

function getindex(x::FlowResult, i::Pair{<:AbstractString,<:AbstractString})
i_i, reverse = findfirstunique_directional(x.interfaces, i)
flow = mean(view(x.flow_mean, i_i, :))
return reverse ? -flow : flow, x.flow_interface_std[i_i]
end

function getindex(x::FlowResult, i::Pair{<:AbstractString,<:AbstractString}, t::ZonedDateTime)
i_i, reverse = findfirstunique_directional(x.interfaces, i)
i_t = findfirstunique(x.timestamps, t)
flow = x.flow_mean[i_i, i_t]
return reverse ? -flow : flow, x.flow_interfaceperiod_std[i_i, i_t]
end

function finalize(
acc::FlowAccumulator,
system::SystemModel{N,L,T,P,E},
) where {N,L,T,P,E}

nsamples = length(system.interfaces) > 0 ?
first(acc.flow_interface[1].stats).n : nothing

flow_mean, flow_interfaceperiod_std = mean_std(acc.flow_interfaceperiod)
flow_interface_std = last(mean_std(acc.flow_interface)) / N

fromregions = getindex.(Ref(system.regions.names), system.interfaces.regions_from)
toregions = getindex.(Ref(system.regions.names), system.interfaces.regions_to)

return FlowResult{N,L,T,P}(
nsamples, Pair.(fromregions, toregions), system.timestamps,
flow_mean, flow_interface_std, flow_interfaceperiod_std)

end
74 changes: 74 additions & 0 deletions PRASCore/src/Results/FlowSamples.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
FlowSamples
Flow samples represent the flow between interfaces at timestamps, which has
not been averaged across different samples. This presents a
3D matrix API (interfaces, timestamps, samples).
See [`Flow`](@ref) for sample-averaged flow data.
"""
struct FlowSamples <: ResultSpec end

struct FlowSamplesAccumulator <: ResultAccumulator{FlowSamples}

flow::Array{Int,3}

end

function accumulator(
sys::SystemModel{N}, nsamples::Int, ::FlowSamples
) where {N}

ninterfaces = length(sys.interfaces)
flow = zeros(Int, ninterfaces, N, nsamples)

return FlowSamplesAccumulator(flow)

end

function merge!(
x::FlowSamplesAccumulator, y::FlowSamplesAccumulator
)

x.flow .+= y.flow
return

end

accumulatortype(::FlowSamples) = FlowSamplesAccumulator

struct FlowSamplesResult{N,L,T<:Period,P<:PowerUnit} <: AbstractFlowResult{N,L,T}

interfaces::Vector{Pair{String,String}}
timestamps::StepRange{ZonedDateTime,T}

flow::Array{Int,3}

end

function getindex(x::FlowSamplesResult, i::Pair{<:AbstractString,<:AbstractString})
i_i, reverse = findfirstunique_directional(x.interfaces, i)
flow = vec(mean(view(x.flow, i_i, :, :), dims=1))
return reverse ? -flow : flow
end


function getindex(x::FlowSamplesResult, i::Pair{<:AbstractString,<:AbstractString}, t::ZonedDateTime)
i_i, reverse = findfirstunique_directional(x.interfaces, i)
i_t = findfirstunique(x.timestamps, t)
flow = vec(x.flow[i_i, i_t, :])
return reverse ? -flow : flow
end

function finalize(
acc::FlowSamplesAccumulator,
system::SystemModel{N,L,T,P,E},
) where {N,L,T,P,E}

fromregions = getindex.(Ref(system.regions.names), system.interfaces.regions_from)
toregions = getindex.(Ref(system.regions.names), system.interfaces.regions_to)

return FlowSamplesResult{N,L,T,P}(
Pair.(fromregions, toregions), system.timestamps, acc.flow)

end
Loading

0 comments on commit 10a7c1c

Please sign in to comment.