""" Subhourly Module for converting UC instances from 40-minute time periods (36 periods/day) to 20-minute time periods (72 periods/day). """ module Subhourly using Dates import ..UnitCommitment export convert_to_subhourly, interpolate_values, repeat_values """ convert_to_subhourly(instance_path::AbstractString, next_day_path::AbstractString) Convert a UC instance from 36 time periods (40 minutes each) to 72 time periods (20 minutes each). # Arguments - `instance_path::AbstractString`: Path to the current day's instance - `next_day_path::AbstractString`: Path to the next day's instance (needed for interpolation at boundaries) # Returns - Modified `UnitCommitmentInstance` with 72 time periods # Details The function performs the following transformations: 1. **Interpolated quantities** (demands, profiled unit outputs): Linear interpolation using next day's first period 2. **Repeated quantities** (max_power, min_power, flow limits): Each value repeated twice 3. **Ramping capacities**: Halved (since time periods are half the duration) 4. **Time period counts** (min_uptime, min_downtime, startup delay): Doubled 5. **Power production costs (not the fixed startup costs)**: Halved # Example ```julia current_instance = convert_to_subhourly( "matpower/case14/2017-01-01", "matpower/case14/2017-01-02" ) println("Total time periods: ", current_instance.time) # Should be 72 ``` """ function convert_to_subhourly(instance_path::AbstractString, next_day_path::AbstractString) # Read instances instance = read_instance(instance_path) next_instance = read_instance(next_day_path) return convert_to_subhourly(instance, next_instance) end """ convert_to_subhourly(instance::UnitCommitment.UnitCommitmentInstance, next_instance::UnitCommitment.UnitCommitmentInstance) Convert a UC instance from 36 time periods to 72 time periods using instance objects. """ function convert_to_subhourly(instance, next_instance) sc_current = instance.scenarios[1] sc_next = next_instance.scenarios[1] # Process buses - interpolate loads for i in 1:length(sc_current.buses) load_current = sc_current.buses[i].load load_next_first = sc_next.buses[i].load[1] sc_current.buses[i].load = interpolate_values(load_current, load_next_first) end # Process thermal units for i in 1:length(sc_current.thermal_units) unit = sc_current.thermal_units[i] unit_next = sc_next.thermal_units[i] # Repeat time-dependent vector quantities unit.max_power = repeat_values(unit.max_power) unit.min_power = repeat_values(unit.min_power) unit.must_run = repeat_values(unit.must_run) unit.min_power_cost = repeat_values(unit.min_power_cost) # Process cost segments for j in 1:length(unit.cost_segments) # cost should be repeated and then halved unit.cost_segments[j].cost = interpolate_values(unit.cost_segments[j].cost, unit.cost_segments[j].cost[1]) ./ 2.0 unit.cost_segments[j].mw = interpolate_values(unit.cost_segments[j].mw, unit.cost_segments[j].mw[1]) end # Repeat commitment status unit.commitment_status = repeat_values(unit.commitment_status) # Halve ramping capacities per time period (since time periods are half the duration) unit.ramp_up_limit = unit.ramp_up_limit / 2.0 unit.ramp_down_limit = unit.ramp_down_limit / 2.0 # Note: startup_limit and shutdown_limit are NOT modified (they are power limits, not per-period rates) # Double time period counts unit.min_uptime = unit.min_uptime * 2 unit.min_downtime = unit.min_downtime * 2 # Double startup delays in startup categories for startup_cat in unit.startup_categories startup_cat.delay = startup_cat.delay * 2 end end # Process transmission lines for i in 1:length(sc_current.lines) line = sc_current.lines[i] # Repeat flow limits line.normal_flow_limit = repeat_values(line.normal_flow_limit) line.emergency_flow_limit = repeat_values(line.emergency_flow_limit) line.flow_limit_penalty = repeat_values(line.flow_limit_penalty) end # Process reserves - interpolate for i in 1:length(sc_current.reserves) reserve = sc_current.reserves[i] reserve_next = sc_next.reserves[i] # Interpolate reserve requirements reserve.amount = interpolate_values(reserve.amount, reserve_next.amount[1]) end # Process price-sensitive loads - interpolate for i in 1:length(sc_current.price_sensitive_loads) psl = sc_current.price_sensitive_loads[i] psl_next = sc_next.price_sensitive_loads[i] # Interpolate demand and revenue psl.demand = interpolate_values(psl.demand, psl_next.demand[1]) psl.revenue = interpolate_values(psl.revenue, psl_next.revenue[1]) end # Process profiled units (renewables) for i in 1:length(sc_current.profiled_units) pu = sc_current.profiled_units[i] pu_next = sc_next.profiled_units[i] # Interpolate renewable profiles pu.min_power = interpolate_values(pu.min_power, pu_next.min_power[1]) pu.max_power = interpolate_values(pu.max_power, pu_next.max_power[1]) pu.cost = interpolate_values(pu.cost, pu_next.cost[1]) end # Process storage units for i in 1:length(sc_current.storage_units) su = sc_current.storage_units[i] su_next = sc_next.storage_units[i] # Interpolate storage levels su.min_level = interpolate_values(su.min_level, su_next.min_level[1]) su.max_level = interpolate_values(su.max_level, su_next.max_level[1]) # Repeat other storage parameters su.simultaneous_charge_and_discharge = repeat_values(su.simultaneous_charge_and_discharge) su.charge_cost = interpolate_values(su.charge_cost, su_next.charge_cost[1]) su.discharge_cost = interpolate_values(su.discharge_cost, su_next.discharge_cost[1]) su.charge_efficiency = repeat_values(su.charge_efficiency) su.discharge_efficiency = repeat_values(su.discharge_efficiency) su.loss_factor = repeat_values(su.loss_factor) # Repeat rate limits su.min_charge_rate = repeat_values(su.min_charge_rate) su.max_charge_rate = repeat_values(su.max_charge_rate) su.min_discharge_rate = repeat_values(su.min_discharge_rate) su.max_discharge_rate = repeat_values(su.max_discharge_rate) end # Process scenario-level fields sc_current.power_balance_penalty = repeat_values(sc_current.power_balance_penalty) # Update time count sc_current.time = 72 instance.time = 72 return instance end """ interpolate_values(values::Vector{T}, next_first::T) where T Interpolate a vector of 36 values to 72 values using linear interpolation. # Arguments - `values::Vector{T}`: Original 36-element vector - `next_first::T`: First value from the next day (for boundary interpolation) # Returns - 72-element vector with interpolated values # Details For each pair of consecutive values, inserts an interpolated midpoint. Uses `next_first` to interpolate the value after the last period. # Example ```julia interpolate_values([10.0, 20.0, 30.0], 40.0) # Returns: [10.0, 15.0, 20.0, 25.0, 30.0, 35.0] ``` """ function interpolate_values(values::Vector{T}, next_first::T) where T n = length(values) result = Vector{T}(undef, 2 * n) for i in 1:n-1 result[2*i-1] = values[i] result[2*i] = (values[i] + values[i+1]) / 2 end # Handle the last period using next day's first value result[2*n-1] = values[n] result[2*n] = (values[n] + next_first) / 2 return result end """ repeat_values(values::Vector{T}) where T Repeat each element of a 36-element vector to create a 72-element vector. # Arguments - `values::Vector{T}`: Original 36-element vector # Returns - 72-element vector with each value repeated twice # Example ```julia repeat_values([1, 2, 3]) # Returns: [1, 1, 2, 2, 3, 3] ``` """ function repeat_values(values::Vector{T}) where T n = length(values) result = Vector{T}(undef, 2 * n) for i in 1:n result[2*i-1] = values[i] result[2*i] = values[i] end return result end """ read_instance(path::AbstractString) Read a UC instance from a local file or benchmark. # Arguments - `path::AbstractString`: Path to instance file or benchmark name # Returns - `UnitCommitmentInstance` object """ function read_instance(path::AbstractString) # Check if path exists locally with common extensions if isfile(path) return UnitCommitment.read(path) elseif isfile(path * ".json.gz") return UnitCommitment.read(path * ".json.gz") elseif isfile(path * ".json") return UnitCommitment.read(path * ".json") else # Try reading as benchmark return UnitCommitment.read_benchmark(path) end end end # module