Skip to content

Commit

Permalink
Fix getproperty for Vector{Resources} to align with updated array i…
Browse files Browse the repository at this point in the history
…nterface (#785)
  • Loading branch information
lbonaldo authored Nov 18, 2024
1 parent 6fabc96 commit 7147493
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 162 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ charge and storage variables (#760 and #763).
### Fixed
- Add constraint to ensure that electricity charged from the grid cannot exceed
the charging capacity of the storage component in VRE_STOR (#770).
- Update `getproperty` function for vectors of resources to ensure compatibility
with Julia v1.11 (#785).

## [0.4.1] - 2024-08-20

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GenX"
uuid = "5d317b1e-30ec-4ed6-a8ce-8d2d88d7cfac"
authors = ["Bonaldo, Luca", "Chakrabarti, Sambuddha", "Cheng, Fangwei", "Ding, Yifu", "Jenkins, Jesse D.", "Luo, Qian", "Macdonald, Ruaridh", "Mallapragada, Dharik", "Manocha, Aneesha", "Mantegna, Gabe ", "Morris, Jack", "Patankar, Neha", "Pecci, Filippo", "Schwartz, Aaron", "Schwartz, Jacob", "Schivley, Greg", "Sepulveda, Nestor", "Xu, Qingyu", "Zhou, Justin"]
version = "0.4.1-dev.11"
version = "0.4.1-dev.12"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
1 change: 1 addition & 0 deletions src/case_runners/case_runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ run_genx_case!("path/to/case", Gurobi.Optimizer)
```
"""
function run_genx_case!(case::AbstractString, optimizer::Any = HiGHS.Optimizer)
print_genx_version() # Log the GenX version
genx_settings = get_settings_path(case, "genx_settings.yml") # Settings YAML file path
writeoutput_settings = get_settings_path(case, "output_settings.yml") # Write-output settings YAML file path
mysetup = configure_settings(genx_settings, writeoutput_settings) # mysetup dictionary stores settings and GenX-specific parameters
Expand Down
4 changes: 3 additions & 1 deletion src/load_inputs/load_resources_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ function check_retrofit_id(rs::Vector{T}) where {T <: AbstractResource}
retrofit_options = ids_retrofit_options(rs)

# check that all retrofit_ids for resources that can retrofit and retrofit options match
if Set(rs[units_can_retrofit].retrofit_id) != Set(rs[retrofit_options].retrofit_id)
can_retrofit_retro_ids = [r.retrofit_id for r in rs[units_can_retrofit]] # retrofit cluster ids for units that can retrofit
retrofit_option_retro_ids = [r.retrofit_id for r in rs[retrofit_options]] # retrofit cluster ids for retrofit options
if Set(can_retrofit_retro_ids) != Set(retrofit_option_retro_ids)
msg = string("Retrofit IDs for resources that \"can retrofit\" and \"retrofit options\" do not match.\n" *
"All retrofitting units must be associated with a retrofit option.")
push!(warning_strings, msg)
Expand Down
24 changes: 10 additions & 14 deletions src/model/resources/resources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ Allows to set the attribute `sym` of an `AbstractResource` object using dot synt
- `value`: The value to set for the attribute.
"""
Base.setproperty!(r::AbstractResource, sym::Symbol, value) = setindex!(parent(r),
value,
sym)
Base.setproperty!(r::AbstractResource, sym::Symbol, value) = setindex!(parent(r), value, sym)

"""
haskey(r::AbstractResource, sym::Symbol)
Expand Down Expand Up @@ -106,32 +104,31 @@ end
"""
Base.getproperty(rs::Vector{<:AbstractResource}, sym::Symbol)
Allows to access attributes of a vector of `AbstractResource` objects using dot syntax. If the `sym` is an element of the `resource_types` constant, it returns all resources of that type. Otherwise, it returns the value of the attribute for each resource in the vector.
Allows to return all resources of a given type of a vector of `AbstractResource` objects using dot syntax.
# Arguments:
- `rs::Vector{<:AbstractResource}`: The vector of `AbstractResource` objects.
- `sym::Symbol`: The symbol representing the attribute name or a type from `resource_types`.
- `sym::Symbol`: The symbol representing the type from `resource_types`.
# Returns:
- If `sym` is an element of the `resource_types` constant, it returns a vector containing all resources of that type.
- If `sym` is an attribute name, it returns a vector containing the value of the attribute for each resource.
## Examples
```julia
julia> vre_gen = gen.Vre; # gen vector of resources
julia> typeof(vre_gen)
Vector{Vre} (alias for Array{Vre, 1})
julia> vre_gen.zone
julia> GenX.zone_id.(vre_gen)
```
"""
function Base.getproperty(rs::Vector{<:AbstractResource}, sym::Symbol)
# if sym is Type then return a vector resources of that type
# if sym is one of the resource types then return a vector resources of that type
if sym resource_types
res_type = eval(sym)
return Vector{res_type}(rs[isa.(rs, res_type)])
else
return getfield(rs, sym)
end
# if sym is a field of the resource then return that field for all resources
return [getproperty(r, sym) for r in rs]
end

"""
Expand Down Expand Up @@ -255,8 +252,7 @@ julia> findall(r -> max_cap_mwh(r) != 0, gen.Storage)
50
```
"""
Base.findall(f::Function, rs::Vector{<:AbstractResource}) = resource_id.(filter(r -> f(r),
rs))
Base.findall(f::Function, rs::Vector{<:AbstractResource}) = resource_id.(filter(r -> f(r), rs))

"""
interface(name, default=default_zero, type=AbstractResource)
Expand Down Expand Up @@ -531,14 +527,14 @@ const default_zero = 0

# INTERFACE FOR ALL RESOURCES
resource_name(r::AbstractResource) = r.resource
resource_name(rs::Vector{T}) where {T <: AbstractResource} = rs.resource
resource_name(rs::Vector{T}) where {T <: AbstractResource} = resource_name.(rs)

resource_id(r::AbstractResource)::Int64 = r.id
resource_id(rs::Vector{T}) where {T <: AbstractResource} = resource_id.(rs)
resource_type_mga(r::AbstractResource) = r.resource_type

zone_id(r::AbstractResource) = r.zone
zone_id(rs::Vector{T}) where {T <: AbstractResource} = rs.zone
zone_id(rs::Vector{T}) where {T <: AbstractResource} = zone_id.(rs)

# getter for boolean attributes (true or false) with validation
function new_build(r::AbstractResource)
Expand Down
4 changes: 0 additions & 4 deletions src/startup/genx_startup.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
function __init__()
print_genx_version()
end

function print_genx_version()
v = pkgversion(GenX)
ascii_art = raw"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,18 @@ function write_reserve_margin_revenue(path::AbstractString,
gen_VRE_STOR = gen.VreStorage
tempresrev[VRE_STOR] = derating_factor.(gen_VRE_STOR, tag = i) .*
((value.(EP[:vP][VRE_STOR, :])) * weighted_price)
tempresrev[VRE_STOR_STOR] .-= derating_factor.(
gen_VRE_STOR[(gen_VRE_STOR.stor_dc_discharge .!= 0) .| (gen_VRE_STOR.stor_dc_charge .!= 0) .| (gen_VRE_STOR.stor_ac_discharge .!= 0) .| (gen_VRE_STOR.stor_ac_charge .!= 0)],
tag = i) .* (value.(EP[:vCHARGE_VRE_STOR][VRE_STOR_STOR,
:]).data * weighted_price)
tempresrev[DC_DISCHARGE] .+= derating_factor.(
gen_VRE_STOR[(gen_VRE_STOR.stor_dc_discharge .!= 0)],
tag = i) .* ((value.(EP[:vCAPRES_DC_DISCHARGE][DC_DISCHARGE,
:]).data .*
etainverter.(gen_VRE_STOR[(gen_VRE_STOR.stor_dc_discharge .!= 0)])) *
weighted_price)
tempresrev[AC_DISCHARGE] .+= derating_factor.(
gen_VRE_STOR[(gen_VRE_STOR.stor_ac_discharge .!= 0)],
tag = i) .* ((value.(EP[:vCAPRES_AC_DISCHARGE][AC_DISCHARGE,
:]).data) * weighted_price)
tempresrev[DC_CHARGE] .-= derating_factor.(
gen_VRE_STOR[(gen_VRE_STOR.stor_dc_charge .!= 0)],
tag = i) .* ((value.(EP[:vCAPRES_DC_CHARGE][DC_CHARGE, :]).data ./
etainverter.(gen_VRE_STOR[(gen_VRE_STOR.stor_dc_charge .!= 0)])) *
weighted_price)
tempresrev[AC_CHARGE] .-= derating_factor.(
gen_VRE_STOR[(gen_VRE_STOR.stor_ac_charge .!= 0)],
tag = i) .* ((value.(EP[:vCAPRES_AC_CHARGE][AC_CHARGE, :]).data) *
weighted_price)
tempresrev[VRE_STOR_STOR] .-= derating_factor.(gen[VRE_STOR_STOR], tag = i) .*
(value.(EP[:vCHARGE_VRE_STOR][VRE_STOR_STOR, :]).data * weighted_price)
tempresrev[DC_DISCHARGE] .+= derating_factor.(gen[DC_DISCHARGE], tag = i) .*
((value.(EP[:vCAPRES_DC_DISCHARGE][DC_DISCHARGE, :]).data .*
etainverter.(gen[DC_DISCHARGE])) * weighted_price)
tempresrev[AC_DISCHARGE] .+= derating_factor.(gen[AC_DISCHARGE], tag = i) .*
((value.(EP[:vCAPRES_AC_DISCHARGE][AC_DISCHARGE, :]).data) * weighted_price)
tempresrev[DC_CHARGE] .-= derating_factor.(gen[DC_CHARGE], tag = i) .*
((value.(EP[:vCAPRES_DC_CHARGE][DC_CHARGE, :]).data ./
etainverter.(gen[DC_CHARGE])) * weighted_price)
tempresrev[AC_CHARGE] .-= derating_factor.(gen[AC_CHARGE], tag = i) .*
((value.(EP[:vCAPRES_AC_CHARGE][AC_CHARGE, :]).data) * weighted_price)
end
tempresrev *= scale_factor
annual_sum .+= tempresrev
Expand Down
39 changes: 10 additions & 29 deletions src/write_outputs/energy_share_requirement/write_esr_revenue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,20 @@ function write_esr_revenue(path::AbstractString,

if !isempty(VRE_STOR)
if !isempty(SOLAR_ONLY)
solar_resources = ((gen_VRE_STOR.wind .== 0) .& (gen_VRE_STOR.solar .!= 0))
dfESRRev[SOLAR, esr_col] = (value.(EP[:vP_SOLAR][SOLAR, :]).data
.*
etainverter.(gen_VRE_STOR[solar_resources]) *
weight) .*
esr_vrestor.(gen_VRE_STOR[solar_resources],
tag = i) * price
dfESRRev[SOLAR, esr_col] = (value.(EP[:vP_SOLAR][SOLAR, :]).data .*
etainverter.(gen[SOLAR]) * weight) .*
esr_vrestor.(gen[SOLAR], tag = i) * price
end
if !isempty(WIND_ONLY)
wind_resources = ((gen_VRE_STOR.wind .!= 0) .& (gen_VRE_STOR.solar .== 0))
dfESRRev[WIND, esr_col] = (value.(EP[:vP_WIND][WIND, :]).data
*
weight) .*
esr_vrestor.(gen_VRE_STOR[wind_resources],
tag = i) * price
dfESRRev[WIND, esr_col] = (value.(EP[:vP_WIND][WIND, :]).data * weight) .*
esr_vrestor.(gen[WIND], tag = i) * price
end
if !isempty(SOLAR_WIND)
solar_and_wind_resources = ((gen_VRE_STOR.wind .!= 0) .&
(gen_VRE_STOR.solar .!= 0))
dfESRRev[SOLAR_WIND, esr_col] = (((value.(EP[:vP_WIND][SOLAR_WIND,
:]).data * weight)
.*
esr_vrestor.(
gen_VRE_STOR[solar_and_wind_resources],
tag = i) * price) +
(value.(EP[:vP_SOLAR][SOLAR_WIND, :]).data
.*
etainverter.(gen_VRE_STOR[solar_and_wind_resources])
*
weight) .*
esr_vrestor.(
gen_VRE_STOR[solar_and_wind_resources],
tag = i) * price)
dfESRRev[SOLAR_WIND, esr_col] = (((value.(EP[:vP_WIND][SOLAR_WIND, :]).data * weight) .*
esr_vrestor.(gen[SOLAR_WIND], tag = i) * price) +
(value.(EP[:vP_SOLAR][SOLAR_WIND, :]).data .*
etainverter.(gen[SOLAR_WIND]) * weight) .*
esr_vrestor.(gen[SOLAR_WIND], tag = i) * price)
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions src/write_outputs/write_curtailment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ function write_curtailment(path::AbstractString, inputs::Dict, setup::Dict, EP::
SOLAR = setdiff(inputs["VS_SOLAR"], inputs["VS_WIND"])
WIND = setdiff(inputs["VS_WIND"], inputs["VS_SOLAR"])
SOLAR_WIND = intersect(inputs["VS_SOLAR"], inputs["VS_WIND"])
gen_VRE_STOR = gen.VreStorage
if !isempty(SOLAR)
curtailment[SOLAR, :] = (value.(EP[:eTotalCap_SOLAR][SOLAR]).data .*
inputs["pP_Max_Solar"][SOLAR, :] .-
value.(EP[:vP_SOLAR][SOLAR, :]).data) .*
etainverter.(gen_VRE_STOR[(gen_VRE_STOR.solar .!= 0)])
inputs["pP_Max_Solar"][SOLAR, :] .-
value.(EP[:vP_SOLAR][SOLAR, :]).data) .*
etainverter.(gen[SOLAR])
end
if !isempty(WIND)
curtailment[WIND, :] = (value.(EP[:eTotalCap_WIND][WIND]).data .*
Expand All @@ -41,7 +40,7 @@ function write_curtailment(path::AbstractString, inputs::Dict, setup::Dict, EP::
curtailment[SOLAR_WIND, :] = ((value.(EP[:eTotalCap_SOLAR])[SOLAR_WIND].data .*
inputs["pP_Max_Solar"][SOLAR_WIND, :] .-
value.(EP[:vP_SOLAR][SOLAR_WIND, :]).data) .*
etainverter.(gen_VRE_STOR[((gen_VRE_STOR.wind .!= 0) .& (gen_VRE_STOR.solar .!= 0))])
etainverter.(gen[SOLAR_WIND])
+
(value.(EP[:eTotalCap_WIND][SOLAR_WIND]).data .*
inputs["pP_Max_Wind"][SOLAR_WIND, :] .-
Expand Down
Loading

0 comments on commit 7147493

Please sign in to comment.