Skip to content

Commit

Permalink
Merge pull request #1080 from daniel-thom/issue-1016
Browse files Browse the repository at this point in the history
Fix component-is-attached-to-system check
  • Loading branch information
jd-lara authored Mar 19, 2024
2 parents 54b4fb4 + 4d7e597 commit 5031237
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
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
_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
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

0 comments on commit 5031237

Please sign in to comment.