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

Fix component-is-attached-to-system check #1080

Merged
merged 1 commit into from
Mar 19, 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
26 changes: 20 additions & 6 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,24 @@ function System(
services = nothing,
kwargs...,
)
for component in Iterators.flatten((generators, loads))
if get_name(component) == "init"
set_bus!(component, first(buses))
end
end
_services = isnothing(services) ? [] : services
return System(base_power, buses, generators, loads, _services; kwargs...)
_branches = isnothing(branches) ? [] : branches
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, the demo constructor never added branches or services...

_storage = isnothing(storage) ? [] : storage
return System(
base_power,
buses,
generators,
loads,
_branches,
_storage,
_services;
kwargs...,
)
end

"""Constructs a System from a file path ending with .m, .RAW, or .json
Expand Down Expand Up @@ -1049,11 +1065,9 @@ end
Return true if the component is attached to the system.
"""
function is_attached(component::T, sys::System) where {T <: Component}
return is_attached(T, get_name(component), sys)
end

function is_attached(::Type{T}, name, sys::System) where {T <: Component}
return !isnothing(get_component(T, sys, name))
existing_component = get_component(T, sys, get_name(component))
isnothing(existing_component) && return false
return component === existing_component
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix.

end

"""
Expand Down
2 changes: 2 additions & 0 deletions test/test_dynamic_generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ end
bus = ACBus(nothing)
add_component!(sys, bus)
static_injector = ThermalStandard(nothing)
static_injector.bus = bus
add_component!(sys, static_injector)
add_component!(sys, derd, static_injector)
DERDs = collect(get_components(GenericDER, sys))
Expand Down Expand Up @@ -714,6 +715,7 @@ end
bus = ACBus(nothing)
add_component!(sys, bus)
static_injector = ThermalStandard(nothing)
static_injector.bus = bus
add_component!(sys, static_injector)
add_component!(sys, dera, static_injector)
DERAs = collect(get_components(AggregateDistributedGenerationA, sys))
Expand Down
4 changes: 4 additions & 0 deletions test/test_dynamic_loads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
bus = ACBus(nothing)
add_component!(sys, bus)
static_load = PowerLoad(nothing)
static_load.bus = bus
add_component!(sys, static_load)
add_component!(sys, im, static_load)
IMs = collect(get_components(SingleCageInductionMachine, sys))
Expand Down Expand Up @@ -69,6 +70,7 @@
bus = ACBus(nothing)
add_component!(sys, bus)
static_load = PowerLoad(nothing)
static_load.bus = bus
add_component!(sys, static_load)
add_component!(sys, im, static_load)
IMs = collect(get_components(SimplifiedSingleCageInductionMachine, sys))
Expand Down Expand Up @@ -100,6 +102,7 @@ end
bus = ACBus(nothing)
add_component!(sys, bus)
static_load = PowerLoad(nothing)
static_load.bus = bus
add_component!(sys, static_load)
add_component!(sys, al, static_load)
ALs = collect(get_components(ActiveConstantPowerLoad, sys))
Expand All @@ -123,6 +126,7 @@ end
bus = ACBus(nothing)
add_component!(sys, bus)
static_load = PowerLoad(nothing)
static_load.bus = bus
add_component!(sys, static_load)
add_component!(sys, al, static_load)
ALs = collect(get_components(DynamicExponentialLoad, sys))
Expand Down
8 changes: 8 additions & 0 deletions test/test_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,11 @@ end
@test metadata["time_series_resolution_milliseconds"] == 3600000
@test metadata["user_data"]["author"] == "test"
end

@testset "Test addition of service to the wrong system" begin
sys1 = PSB.build_system(PSITestSystems, "test_RTS_GMLC_sys")
sys2 = PSB.build_system(PSITestSystems, "test_RTS_GMLC_sys")
service1 = first(get_components(VariableReserve{ReserveDown}, sys1))
device2 = first(get_components(ThermalStandard, sys2))
@test_throws ArgumentError add_service!(device2, service1, sys2)
end
2 changes: 1 addition & 1 deletion test/test_validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ end
)

#test custom range (active_power and active_power_limits)
nodes = nodes5()
bad_therm_gen_act_power = thermal_generators5(nodes)
bad_therm_gen_act_power[1].active_power = 10
nodes = nodes5()
# This is an explicit check for one error message.
@test_logs (:warn, r"Invalid range") System(
100.0,
Expand Down
Loading