Skip to content

Commit

Permalink
Merge branch 'psy4' into gks/costs_2_cleanup_2
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS authored May 2, 2024
2 parents 66213e6 + 9318161 commit 571bd86
Show file tree
Hide file tree
Showing 54 changed files with 223 additions and 1,292 deletions.
8 changes: 5 additions & 3 deletions docs/src/model_developer_guide/adding_custom_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Refer to the
[managing components guide](https://nrel-sienna.github.io/InfrastructureSystems.jl/stable/dev_guide/components_and_container/)
for component requirements.

**Note**: `get_internal`, `get_name`, and `get_time_series_container` are
imported into `PowerSystems`, so you should implement your methods as
`PowerSystems` methods.
In particular, please note the methods `supports_time_series` (default = false) and
`supports_supplemental_attributes` (default = true) that you may need to implement.

**Note**: `get_internal` and `get_name` are imported into `PowerSystems`, so you should
implement your methods as `PowerSystems` methods.

Some abstract types define required interface functions in docstring. Be sure
to implement each of them for your new type.
Expand Down
6 changes: 0 additions & 6 deletions docs/src/modeler_guide/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,3 @@ jq '.data.components | .[] | select(.__metadata__.type == "ThermalStandard" and
```zsh
jq '.data.components | .[] | select(.__metadata__.type == "ThermalStandard" and .active_power > 2.3)' system.json
```

- View the time series metadata for a component.

```zsh
jq '.data.components | .[] | select(.__metadata__.type == "RenewableDispatch") | .time_series_container' system.json
```
4 changes: 2 additions & 2 deletions docs/src/tutorials/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ print(join(tt(PowerSystems.IS.InfrastructureSystemsType, concrete = false), ""))
### `TimeSeriesData`

[_Read the Docs!_](https://nrel-sienna.github.io/PowerSystems.jl/stable/modeler_guide/time_series/)
Every `Component` has a `time_series_container::InfrastructureSystems.TimeSeriesContainer`
field. `TimeSeriesData` are used to hold time series information that describes the
Some `Component` types support time series data (refer to `supports_time_series(component)`.
`TimeSeriesData` are used to hold time series information that describes the
temporally dependent data of fields within the same struct. For example, the
`ThermalStandard.time_series_container` field can
describe other fields in the struct (`available`, `activepower`, `reactivepower`).
Expand Down
12 changes: 8 additions & 4 deletions src/PowerSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ export iterate_supplemental_attributes
export get_time_series
export get_time_series_array
export list_time_series_resolutions
export supports_time_series
export supports_supplemental_attributes
export get_time_series_timestamps
export get_time_series_values
export get_time_series_counts
Expand Down Expand Up @@ -364,7 +366,6 @@ export get_bus_numbers
export get_name
export set_name!
export get_component_uuids
export get_supplemental_attributes_container
export get_description
export set_description!
export get_base_power
Expand Down Expand Up @@ -507,11 +508,9 @@ import InfrastructureSystems:
get_component_uuids,
get_supplemental_attribute,
get_supplemental_attributes,
get_supplemental_attributes_container,
set_name!,
get_internal,
set_internal!,
get_time_series_container,
iterate_windows,
get_time_series,
has_time_series,
Expand Down Expand Up @@ -561,7 +560,9 @@ import InfrastructureSystems:
get_points, # TODO possible rename to disambiguate from geographical information
get_x_coords,
get_y_coords,
get_raw_data_type
get_raw_data_type,
supports_time_series,
supports_supplemental_attributes

const IS = InfrastructureSystems

Expand All @@ -588,6 +589,9 @@ abstract type Component <: IS.InfrastructureSystemsComponent end
""" Supertype for "devices" (bus, line, etc.) """
abstract type Device <: Component end

supports_time_series(::Device) = true
supports_supplemental_attributes(::Device) = true

# Include utilities
include("utils/logging.jl")
include("utils/IO/base_checks.jl")
Expand Down
69 changes: 37 additions & 32 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,6 @@ function IS.from_json(
kwargs...,
)
data = JSON3.read(io, Dict)
# These objects could be removed in to_json(sys). Doing it here will allow us to
# keep that JSON string fully consistent with time series and potentially use it in the
# future.
for component in data["data"]["components"]
if haskey(component, "time_series_container")
empty!(component["time_series_container"])
end
end
sys = from_dict(System, data; kwargs...)
_post_deserialize_handling(
sys;
Expand Down Expand Up @@ -999,10 +991,13 @@ end
"""
Check to see if the component of type T with name exists.
"""
function has_component(::Type{T}, sys::System, name::AbstractString) where {T <: Component}
return IS.has_component(T, sys.data.components, name)
function has_component(sys::System, T::Type{<:Component}, name::AbstractString)
return IS.has_component(sys.data, T, name)
end

has_component(T::Type{<:Component}, sys::System, name::AbstractString) =
has_component(sys, T, name)

"""
Get the component of type T with name. Returns nothing if no component matches. If T is an abstract
type then the names of components across all subtypes of T must be unique.
Expand Down Expand Up @@ -1857,19 +1852,18 @@ Allow types to implement handling of special cases during deserialization.
"""
handle_deserialization_special_cases!(component::Dict, ::Type{<:Component}) = nothing

function handle_deserialization_special_cases!(component::Dict, ::Type{DynamicBranch})
# IS handles deserialization of supplemental attribues in each component.
# In this case the DynamicBranch's composed branch is not part of the system and so
# IS will not handle it. It can never attributes.
if !isempty(component["branch"]["supplemental_attributes_container"])
error(
"Bug: serialized DynamicBranch.branch has supplemental attributes: $component",
)
end
component["branch"]["supplemental_attributes_container"] =
IS.SupplementalAttributesContainer()
return
end
# TODO DT: Do I need to handle this in the new format upgrade?
#function handle_deserialization_special_cases!(component::Dict, ::Type{DynamicBranch})
# # IS handles deserialization of supplemental attribues in each component.
# # In this case the DynamicBranch's composed branch is not part of the system and so
# # IS will not handle it. It can never attributes.
# if !isempty(component["branch"]["supplemental_attributes_container"])
# error(
# "Bug: serialized DynamicBranch.branch has supplemental attributes: $component",
# )
# end
# return
#end

"""
Return bus with name.
Expand Down Expand Up @@ -2303,9 +2297,7 @@ function convert_component!(
line.angle_limits,
line.services,
line.ext,
InfrastructureSystems.TimeSeriesContainer(),
InfrastructureSystems.SupplementalAttributesContainer(),
deepcopy(line.internal),
_copy_internal_for_conversion(line),
)
IS.assign_new_uuid!(sys, line)
add_component!(sys, new_line)
Expand Down Expand Up @@ -2348,9 +2340,7 @@ function convert_component!(
line.angle_limits,
line.services,
line.ext,
InfrastructureSystems.TimeSeriesContainer(),
InfrastructureSystems.SupplementalAttributesContainer(),
deepcopy(line.internal),
_copy_internal_for_conversion(line),
)
IS.assign_new_uuid!(sys, line)
add_component!(sys, new_line)
Expand Down Expand Up @@ -2381,10 +2371,8 @@ function convert_component!(
max_constant_active_power = get_max_active_power(old_load),
max_constant_reactive_power = get_max_active_power(old_load),
dynamic_injector = get_dynamic_injector(old_load),
internal = deepcopy(get_internal(old_load)),
internal = _copy_internal_for_conversion(old_load),
services = Device[],
supplemental_attributes_container = InfrastructureSystems.SupplementalAttributesContainer(),
time_series_container = InfrastructureSystems.TimeSeriesContainer(),
)
IS.assign_new_uuid!(sys, old_load)
add_component!(sys, new_load)
Expand All @@ -2397,6 +2385,21 @@ function convert_component!(
remove_component!(sys, old_load)
end

# Use this function to avoid deepcopy of shared_system_references.
function _copy_internal_for_conversion(component::Component)
internal = get_internal(component)
refs = internal.shared_system_references
return InfrastructureSystemsInternal(;
uuid = deepcopy(internal.uuid),
units_info = deepcopy(internal.units_info),
shared_system_references = IS.SharedSystemReferences(;
supplemental_attribute_manager = refs.supplemental_attribute_manager,
time_series_manager = refs.time_series_manager,
),
ext = deepcopy(internal.ext),
)
end

function _validate_or_skip!(sys, component, skip_validation)
if skip_validation && get_runchecks(sys)
@warn(
Expand Down Expand Up @@ -2440,3 +2443,5 @@ Throws InfrastructureSystems.InvalidValue if any time series is inconsistent.
function check_time_series_consistency(sys::System, ::Type{T}) where {T <: TimeSeriesData}
return IS.check_time_series_consistency(sys.data, T)
end

stores_time_series_in_memory(sys::System) = IS.stores_time_series_in_memory(sys.data)
42 changes: 1 addition & 41 deletions src/data_format_conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@
const COST_CONTAINERS =
["MultiStartCost", "StorageManagementCost", "ThreePartCost", "TwoPartCost"]

function _convert_data!(
raw::Dict{String, Any},
::Val{Symbol("1.0.0")},
::Val{Symbol("2.0.0")},
)
for component in raw["data"]["components"]
for ts_metadata in get(component, "time_series_container", [])
if ts_metadata["__metadata__"]["type"] == "DeterministicMetadata" &&
!haskey(ts_metadata, "time_series_type")
# This will allow deserialization to work.
# post_deserialize_conversion will fix the type.
ts_metadata["time_series_type"] = Dict(
"__metadata__" => Dict(
"module" => "InfrastructureSystems",
"type" => "AbstractDeterministic",
),
)
end
end
end
return
end

function _convert_data!(
raw::Dict{String, Any},
::Val{Symbol("2.0.0")},
Expand Down Expand Up @@ -166,24 +143,7 @@ end
# Conversions to occur at the end of deserialization
function post_deserialize_conversion!(sys::System, raw)
old = raw["data_format_version"]
if old == "1.0.0"
for component in IS.iterate_components_with_time_series(sys.data.components)
ts_container = get_time_series_container(component)
for key in keys(ts_container.data)
if key.time_series_type == IS.DeterministicMetadata
ts_metadata = ts_container.data[key]
ts = get_time_series(
AbstractDeterministic,
component,
get_name(ts_metadata);
len = get_horizon(ts_metadata),
count = 1,
)
ts_metadata.time_series_type = typeof(ts)
end
end
end
elseif old == "1.0.1" || old == "2.0.0" || old == "3.0.0"
if old == "1.0.1" || old == "2.0.0" || old == "3.0.0"
# Version 1.0.1 can be converted
raw["data_format_version"] = DATA_FORMAT_VERSION
@warn(
Expand Down
Loading

0 comments on commit 571bd86

Please sign in to comment.