Skip to content

Commit

Permalink
Add fast_deepcopy_system function, fix #1233
Browse files Browse the repository at this point in the history
Closes #1231, closes #1233.
  • Loading branch information
GabrielKS committed Dec 24, 2024
1 parent 774695e commit 63aad3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/PowerSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 37 additions & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
25 changes: 25 additions & 0 deletions test/test_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 63aad3d

Please sign in to comment.