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

Activate model env before calling RelationshipClass and Parameter #1096

Merged
merged 1 commit into from
Sep 20, 2024
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
16 changes: 12 additions & 4 deletions src/data_structure/stochastic_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,25 @@ function node_investment_stochastic_time_indices(
end

function node_stochastic_scenario_weight(m; kwargs...)
m.ext[:spineopt].stochastic_structure[:node_stochastic_scenario_weight][(; kwargs...)]
_with_model_env(m) do
m.ext[:spineopt].stochastic_structure[:node_stochastic_scenario_weight][(; kwargs...)]
end
end

function unit_stochastic_scenario_weight(m; kwargs...)
m.ext[:spineopt].stochastic_structure[:unit_stochastic_scenario_weight][(; kwargs...)]
_with_model_env(m) do
m.ext[:spineopt].stochastic_structure[:unit_stochastic_scenario_weight][(; kwargs...)]
end
end

function connection_stochastic_scenario_weight(m; kwargs...)
m.ext[:spineopt].stochastic_structure[:connection_stochastic_scenario_weight][(; kwargs...)]
_with_model_env(m) do
m.ext[:spineopt].stochastic_structure[:connection_stochastic_scenario_weight][(; kwargs...)]
end
end

function any_stochastic_scenario_weight(m; kwargs...)
m.ext[:spineopt].stochastic_structure[:any_stochastic_scenario_weight][(; kwargs...)]
_with_model_env(m) do
m.ext[:spineopt].stochastic_structure[:any_stochastic_scenario_weight][(; kwargs...)]
end
end
18 changes: 15 additions & 3 deletions src/data_structure/temporal_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,11 @@ the second starting when the first ends.
- `t_before`: if given, return an `Array` of `TimeSlice`s that start when `t_before` ends.
- `t_after`: if given, return an `Array` of `TimeSlice`s that end when `t_after` starts.
"""
t_before_t(m::Model; kwargs...) = m.ext[:spineopt].temporal_structure[:t_before_t](; kwargs...)
function t_before_t(m::Model; kwargs...)
_with_model_env(m) do
m.ext[:spineopt].temporal_structure[:t_before_t](; kwargs...)
end
end

"""
t_in_t(m; t_short=anything, t_long=anything)
Expand All @@ -567,7 +571,11 @@ the second containing the first.
- `t_short`: if given, return an `Array` of `TimeSlice`s that contain `t_short`.
- `t_long`: if given, return an `Array` of `TimeSlice`s that are contained in `t_long`.
"""
t_in_t(m::Model; kwargs...) = m.ext[:spineopt].temporal_structure[:t_in_t](; kwargs...)
function t_in_t(m::Model; kwargs...)
_with_model_env(m) do
m.ext[:spineopt].temporal_structure[:t_in_t](; kwargs...)
end
end

"""
t_in_t_excl(m; t_short=anything, t_long=anything)
Expand All @@ -578,7 +586,11 @@ Same as [t_in_t](@ref) but exclude tuples of the same `TimeSlice`.
- `t_short`: if given, return an `Array` of `TimeSlice`s that contain `t_short` (other than `t_short` itself).
- `t_long`: if given, return an `Array` of `TimeSlice`s that are contained in `t_long` (other than `t_long` itself).
"""
t_in_t_excl(m::Model; kwargs...) = m.ext[:spineopt].temporal_structure[:t_in_t_excl](; kwargs...)
function t_in_t_excl(m::Model; kwargs...)
_with_model_env(m) do
m.ext[:spineopt].temporal_structure[:t_in_t_excl](; kwargs...)
end
end

"""
t_overlaps_t(m; t)
Expand Down
8 changes: 8 additions & 0 deletions src/util/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ _percentage_str(x::Number) = string(@sprintf("%1.4f", x * 100), "%")

_number_str(x::Number) = @sprintf("%.5e", x)

function _with_model_env(f, m)
st = m.ext[:spineopt].stage
st === nothing && return f()
with_env(stage_scenario(stage=st)) do
f()
end
end

# Base
_ObjectArrayLike = Union{ObjectLike,Array{T,1} where T<:ObjectLike}
_RelationshipArrayLike{K} = NamedTuple{K,V} where {K,V<:Tuple{Vararg{_ObjectArrayLike}}}
Expand Down
Loading