Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Julia Formatter #62

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
whitespace_ops_in_indices=true
remove_extra_newlines=true
always_for_in=true
whitespace_typedefs=true
whitespace_in_kwargs=false
format_docstrings=true
always_use_return=false
43 changes: 43 additions & 0 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Format Check

on:
push:
branches:
- 'main'
- 'release-'
tags: '*'
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}

- uses: actions/checkout@v2
- name: Install JuliaFormatter and format
run: |
julia -e 'include("scripts/formatter/formatter_code.jl")'
- uses: reviewdog/action-suggester@v1
if: github.event_name == 'pull_request'
with:
tool_name: JuliaFormatter
fail_on_error: true
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
10 changes: 10 additions & 0 deletions scripts/formatter/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
uuid = "c6367ca8-164d-4469-afe3-c91cf8860505"
authors = ["Jose Daniel Lara <[email protected]>"]

[deps]
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[compat]
JuliaFormatter = "1.0"
julia = "^1.7"
41 changes: 41 additions & 0 deletions scripts/formatter/formatter_code.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()
using JuliaFormatter

main_paths = ["."] #, "./docs/src"] (until we use Documenter.jl)
for main_path in main_paths
format(
main_path;
whitespace_ops_in_indices=true,
remove_extra_newlines=true,
verbose=true,
always_for_in=true,
whitespace_typedefs=true,
whitespace_in_kwargs=false,
format_docstrings=true,
always_use_return=false, # removed since it has false positives.
)
end

# Documentation Formatter
main_paths = [] # "./docs/src"]
for main_path in main_paths
for folder in readdir(main_path)
@show folder_path = joinpath(main_path, folder)
if isfile(folder_path)
!occursin(".md", folder_path) && continue
end
format(
folder_path;
format_markdown=true,
whitespace_ops_in_indices=true,
remove_extra_newlines=true,
verbose=true,
always_for_in=true,
whitespace_typedefs=true,
whitespace_in_kwargs=false,
# always_use_return = true # removed since it has false positives.
)
end
end
6 changes: 3 additions & 3 deletions src/CapacityCredit/CapacityCredit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
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
import ..ResourceAdequacy:
assess, ReliabilityMetric, Result, Shortfall, SimulationSpec, stderror, val

export EFC, ELCC

abstract type CapacityValuationMethod{M<:ReliabilityMetric} end
abstract type CapacityValuationMethod{M <: ReliabilityMetric} end

include("utils.jl")
include("CapacityCreditResult.jl")
Expand Down
22 changes: 12 additions & 10 deletions src/CapacityCredit/CapacityCreditResult.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
struct CapacityCreditResult{
S <: CapacityValuationMethod, M <: ReliabilityMetric, P <: PowerUnit}

S <: CapacityValuationMethod,
M <: ReliabilityMetric,
P <: PowerUnit,
}
target_metric::M
lowerbound::Int
upperbound::Int
bound_capacities::Vector{Int}
bound_metrics::Vector{M}

function CapacityCreditResult{S,M,P}(
target_metric::M, lowerbound::Int, upperbound::Int,
bound_capacities::Vector{Int}, bound_metrics::Vector{M}) where {S,M,P}

function CapacityCreditResult{S, M, P}(
target_metric::M,
lowerbound::Int,
upperbound::Int,
bound_capacities::Vector{Int},
bound_metrics::Vector{M},
) where {S, M, P}
length(bound_capacities) == length(bound_metrics) ||
throw(ArgumentError("Lengths of bound_capacities and bound_metrics must match"))

new{S,M,P}(target_metric, lowerbound, upperbound,
bound_capacities, bound_metrics)

new{S, M, P}(target_metric, lowerbound, upperbound, bound_capacities, bound_metrics)
end

end

minimum(x::CapacityCreditResult) = x.lowerbound
Expand Down
124 changes: 70 additions & 54 deletions src/CapacityCredit/EFC.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
struct EFC{M} <: CapacityValuationMethod{M}

capacity_max::Int
capacity_gap::Int
p_value::Float64
regions::Vector{Tuple{String,Float64}}
regions::Vector{Tuple{String, Float64}}
verbose::Bool

function EFC{M}(
capacity_max::Int, regions::Vector{Pair{String,Float64}};
capacity_gap::Int=1, p_value::Float64=0.05, verbose::Bool=false) where M

capacity_max::Int,
regions::Vector{Pair{String, Float64}};
capacity_gap::Int=1,
p_value::Float64=0.05,
verbose::Bool=false,
) where {M}
@assert capacity_max > 0
@assert capacity_gap > 0
@assert 0 < p_value < 1
@assert sum(x.second for x in regions) ≈ 1.0

return new{M}(capacity_max, capacity_gap, p_value, Tuple.(regions), verbose)

end

end

function EFC{M}(
capacity_max::Int, region::String; kwargs...
) where M
return EFC{M}(capacity_max, [region=>1.0]; kwargs...)
function EFC{M}(capacity_max::Int, region::String; kwargs...) where {M}
return EFC{M}(capacity_max, [region => 1.0]; kwargs...)
end

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

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

regionnames = sys_baseline.regions.names
Expand Down Expand Up @@ -58,10 +58,10 @@ function assess(sys_baseline::S, sys_augmented::S,
push!(metrics, upper_bound_metric)

while true

params.verbose && println(
"\n$(lower_bound) $powerunit\t< EFC <\t$(upper_bound) $powerunit\n",
"$(lower_bound_metric)\t> $(target_metric) >\t$(upper_bound_metric)")
"$(lower_bound_metric)\t> $(target_metric) >\t$(upper_bound_metric)",
)

midpoint = div(lower_bound + upper_bound, 2)
capacity_gap = upper_bound - lower_bound
Expand All @@ -70,7 +70,8 @@ function assess(sys_baseline::S, sys_augmented::S,

## Return the bounds if they are within solution tolerance of each other
if capacity_gap <= params.capacity_gap
params.verbose && @info "Capacity bound gap within tolerance, stopping bisection."
params.verbose &&
@info "Capacity bound gap within tolerance, stopping bisection."
break
end

Expand Down Expand Up @@ -99,30 +100,37 @@ function assess(sys_baseline::S, sys_augmented::S,
upper_bound = midpoint
upper_bound_metric = midpoint_metric
end

end

return CapacityCreditResult{typeof(params), typeof(target_metric), P}(
target_metric, lower_bound, upper_bound, capacities, metrics)

target_metric,
lower_bound,
upper_bound,
capacities,
metrics,
)
end

function add_firmcapacity(
s1::SystemModel{N,L,T,P,E}, s2::SystemModel{N,L,T,P,E},
region_shares::Vector{Tuple{String,Float64}}
) where {N,L,T,P,E}

s1::SystemModel{N, L, T, P, E},
s2::SystemModel{N, L, T, P, E},
region_shares::Vector{Tuple{String, Float64}},
) where {N, L, T, P, E}
n_regions = length(s1.regions.names)
n_region_allocs = length(region_shares)

region_allocations = allocate_regions(s1.regions.names, region_shares)
efc_gens = similar(region_allocations)

new_gen(i::Int) = Generators{N,L,T,P}(
["_EFC_$i"], ["_EFC Calculation Dummy Generator"],
zeros(Int, 1, N), zeros(1, N), ones(1, N))
new_gen(i::Int) = Generators{N, L, T, P}(
["_EFC_$i"],
["_EFC Calculation Dummy Generator"],
zeros(Int, 1, N),
zeros(1, N),
ones(1, N),
)

variable_gens = Generators{N,L,T,P}[]
variable_gens = Generators{N, L, T, P}[]
variable_region_gen_idxs = similar(s1.region_gen_idxs)

target_gens = similar(variable_gens)
Expand All @@ -131,59 +139,67 @@ function add_firmcapacity(
ra_idx = 0

for r in 1:n_regions

s1_range = s1.region_gen_idxs[r]
s2_range = s2.region_gen_idxs[r]

if (ra_idx < n_region_allocs) && (r == first(region_allocations[ra_idx+1]))

if (ra_idx < n_region_allocs) && (r == first(region_allocations[ra_idx + 1]))
ra_idx += 1

variable_region_gen_idxs[r] = incr_range(s1_range, ra_idx-1, ra_idx)
target_region_gen_idxs[r] = incr_range(s2_range, ra_idx-1, ra_idx)
variable_region_gen_idxs[r] = incr_range(s1_range, ra_idx - 1, ra_idx)
target_region_gen_idxs[r] = incr_range(s2_range, ra_idx - 1, ra_idx)

gen = new_gen(ra_idx)
push!(variable_gens, gen)
push!(target_gens, gen)
efc_gens[ra_idx] = (
first(s1_range) + ra_idx - 1,
last(region_allocations[ra_idx]))
efc_gens[ra_idx] =
(first(s1_range) + ra_idx - 1, last(region_allocations[ra_idx]))

else

variable_region_gen_idxs[r] = incr_range(s1_range, ra_idx)
target_region_gen_idxs[r] = incr_range(s2_range, ra_idx)

end

push!(variable_gens, s1.generators[s1_range])
push!(target_gens, s2.generators[s2_range])

end

sys_variable = SystemModel(
s1.regions, s1.interfaces,
vcat(variable_gens...), variable_region_gen_idxs,
s1.storages, s1.region_stor_idxs,
s1.generatorstorages, s1.region_genstor_idxs,
s1.lines, s1.interface_line_idxs, s1.timestamps)
s1.regions,
s1.interfaces,
vcat(variable_gens...),
variable_region_gen_idxs,
s1.storages,
s1.region_stor_idxs,
s1.generatorstorages,
s1.region_genstor_idxs,
s1.lines,
s1.interface_line_idxs,
s1.timestamps,
)

sys_target = SystemModel(
s2.regions, s2.interfaces,
vcat(target_gens...), target_region_gen_idxs,
s2.storages, s2.region_stor_idxs,
s2.generatorstorages, s2.region_genstor_idxs,
s2.lines, s2.interface_line_idxs, s2.timestamps)
s2.regions,
s2.interfaces,
vcat(target_gens...),
target_region_gen_idxs,
s2.storages,
s2.region_stor_idxs,
s2.generatorstorages,
s2.region_genstor_idxs,
s2.lines,
s2.interface_line_idxs,
s2.timestamps,
)

return efc_gens, sys_variable, sys_target

end

function update_firmcapacity!(
sys::SystemModel, gens::Vector{Tuple{Int,Float64}}, capacity::Int)

sys::SystemModel,
gens::Vector{Tuple{Int, Float64}},
capacity::Int,
)
for (g, share) in gens
sys.generators.capacity[g, :] .= round(Int, share * capacity)
end

end
Loading