From 8f2f4c5694ad7d6b55bf3c05d20e5b7f94419cf5 Mon Sep 17 00:00:00 2001 From: GabrielKS <23368820+GabrielKS@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:42:42 -0600 Subject: [PATCH] Add `fast_deepcopy_system` function, fix #1233 Closes #1231, closes $1233. --- src/PowerSystems.jl | 3 ++- src/base.jl | 38 +++++++++++++++++++++++++++++++++++++- test/test_system.jl | 25 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/PowerSystems.jl b/src/PowerSystems.jl index 103d62df3a..5445291604 100644 --- a/src/PowerSystems.jl +++ b/src/PowerSystems.jl @@ -605,7 +605,8 @@ import InfrastructureSystems: get_y_coords, get_raw_data_type, supports_time_series, - supports_supplemental_attributes + supports_supplemental_attributes, + fast_deepcopy_system import InfrastructureSystems: ValueCurve, InputOutputCurve, diff --git a/src/base.jl b/src/base.jl index 31d38a9f77..0218e9d7a8 100644 --- a/src/base.jl +++ b/src/base.jl @@ -133,7 +133,7 @@ struct System <: IS.InfrastructureSystemsType "unit_system kwarg ignored. The value in SystemUnitsSetting takes precedence" ) end - bus_numbers = Set{Int}() + bus_numbers = Set(get_number.(IS.get_components(ACBus, data))) return new( data, frequency, @@ -2628,3 +2628,39 @@ function check_time_series_consistency(sys::System, ::Type{T}) where {T <: TimeS end stores_time_series_in_memory(sys::System) = IS.stores_time_series_in_memory(sys.data) + +""" +Make a `deepcopy` of a [`System`](@ref) more quickly by skipping the copying of time +series and/or supplemental attributes. + +# Arguments + + - `data::System`: the `System` to copy + - `skip_time_series::Bool = true`: whether to skip copying time series + - `skip_supplemental_attributes::Bool = true`: whether to skip copying supplemental + attributes + +Note that setting both `skip_time_series` and `skip_supplemental_attributes` to `false` +results in the same behavior as `deepcopy` with no performance improvement. +""" +function fast_deepcopy_system( + sys::System; + skip_time_series::Bool = true, + skip_supplemental_attributes::Bool = true, +) + new_data = IS.fast_deepcopy_system( + sys.data; + skip_time_series = skip_time_series, + skip_supplemental_attributes = skip_supplemental_attributes, + ) + new_sys = System( + new_data, + deepcopy(sys.units_settings), + deepcopy(sys.internal); + runchecks = deepcopy(sys.runchecks[]), + frequency = deepcopy(sys.frequency), + time_series_directory = deepcopy(sys.time_series_directory), + name = deepcopy(sys.metadata.name), + description = deepcopy(sys.metadata.description)) + return new_sys +end diff --git a/test/test_system.jl b/test/test_system.jl index 7e03f5f642..c1896fe398 100644 --- a/test/test_system.jl +++ b/test/test_system.jl @@ -399,6 +399,31 @@ end end end +@testset "Test fast deepcopy of system" begin + systems = Dict( + in_memory => PSB.build_system( + PSITestSystems, + "test_RTS_GMLC_sys"; + time_series_in_memory = in_memory, + force_build = true, + ) for in_memory in (true, false) + ) + @testset for (in_memory, skip_ts, skip_sa) in # Iterate over all permutations + Iterators.product(repeat([(true, false)], 3)...) + sys = systems[in_memory] + + sys2 = IS.fast_deepcopy_system(sys; + skip_time_series = skip_ts, skip_supplemental_attributes = skip_sa) + @test IS.compare_values( + sys, + sys2; + exclude = Set( + [:time_series_manager, :supplemental_attribute_manager][[skip_ts, skip_sa]], + ), + ) + end +end + @testset "Test with compression enabled" begin @test get_compression_settings(System(100.0)) == CompressionSettings(; enabled = false)