File size: 6,997 Bytes
43c68a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
    convert_to_subhourly(instance, next_instance)

Return a deep-copied instance with half the original time step and twice as many
time periods.

Call this before assigning startup/shutdown curves.
"""
function convert_to_subhourly(
    instance::UnitCommitmentInstance,
    next_instance::UnitCommitmentInstance,
)::UnitCommitmentInstance
    modified = deepcopy(instance)
    convert_to_subhourly!(modified, next_instance)
    return modified
end

function convert_to_subhourly!(
    instance::UnitCommitmentInstance,
    next_instance::UnitCommitmentInstance,
)::Nothing
    length(instance.scenarios) == length(next_instance.scenarios) ||
        error("instance and next_instance must have the same number of scenarios")

    for (sc_current, sc_next) in zip(instance.scenarios, next_instance.scenarios)
        _validate_subhourly_inputs(sc_current, sc_next)
        _convert_scenario_to_subhourly!(sc_current, sc_next)
    end

    instance.time *= 2
    return
end

function _validate_subhourly_inputs(
    sc_current::UnitCommitmentScenario,
    sc_next::UnitCommitmentScenario,
)::Nothing
    sc_current.time == sc_next.time ||
        error("current and next scenario must have the same time dimension")
    sc_current.time_step == sc_next.time_step ||
        error("current and next scenario must have the same time_step")
    sc_current.time_step % 2 == 0 ||
        error("time_step $(sc_current.time_step) must be even")

    length(sc_current.buses) == length(sc_next.buses) ||
        error("bus counts do not match")
    length(sc_current.thermal_units) == length(sc_next.thermal_units) ||
        error("thermal unit counts do not match")
    length(sc_current.lines) == length(sc_next.lines) ||
        error("line counts do not match")
    length(sc_current.reserves) == length(sc_next.reserves) ||
        error("reserve counts do not match")
    length(sc_current.price_sensitive_loads) ==
        length(sc_next.price_sensitive_loads) ||
        error("price-sensitive load counts do not match")
    length(sc_current.profiled_units) == length(sc_next.profiled_units) ||
        error("profiled unit counts do not match")
    length(sc_current.storage_units) == length(sc_next.storage_units) ||
        error("storage unit counts do not match")

    for g in sc_current.thermal_units
        isempty(g.startup_curve) && isempty(g.shutdown_curve) || error(
            "call convert_to_subhourly! before assigning startup/shutdown curves",
        )
    end
    return
end

function _convert_scenario_to_subhourly!(
    sc_current::UnitCommitmentScenario,
    sc_next::UnitCommitmentScenario,
)::Nothing
    original_time = sc_current.time

    for i in eachindex(sc_current.buses)
        sc_current.buses[i].load = interpolate_values(
            sc_current.buses[i].load,
            sc_next.buses[i].load[1],
        )
    end

    for i in eachindex(sc_current.thermal_units)
        g = sc_current.thermal_units[i]
        g_next = sc_next.thermal_units[i]

        g.max_power = repeat_values(g.max_power)
        g.min_power = repeat_values(g.min_power)
        g.must_run = repeat_values(g.must_run)
        g.min_power_cost = repeat_values(g.min_power_cost)
        g.commitment_status = repeat_values(g.commitment_status)

        for j in eachindex(g.cost_segments)
            g.cost_segments[j].cost = interpolate_values(
                g.cost_segments[j].cost,
                g_next.cost_segments[j].cost[1],
            ) ./ 2.0
            g.cost_segments[j].mw = interpolate_values(
                g.cost_segments[j].mw,
                g_next.cost_segments[j].mw[1],
            )
        end

        g.ramp_up_limit /= 2.0
        g.ramp_down_limit /= 2.0
        g.min_uptime *= 2
        g.min_downtime *= 2

        for startup_cat in g.startup_categories
            startup_cat.delay *= 2
        end

        if g.initial_status !== nothing
            g.initial_status *= 2
        end
    end

    for i in eachindex(sc_current.lines)
        line = sc_current.lines[i]
        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

    for i in eachindex(sc_current.reserves)
        reserve = sc_current.reserves[i]
        reserve_next = sc_next.reserves[i]
        reserve.amount = interpolate_values(reserve.amount, reserve_next.amount[1])
    end

    for i in eachindex(sc_current.price_sensitive_loads)
        ps = sc_current.price_sensitive_loads[i]
        ps_next = sc_next.price_sensitive_loads[i]
        ps.demand = interpolate_values(ps.demand, ps_next.demand[1])
        ps.revenue = interpolate_values(ps.revenue, ps_next.revenue[1])
    end

    for i in eachindex(sc_current.profiled_units)
        pu = sc_current.profiled_units[i]
        pu_next = sc_next.profiled_units[i]
        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

    for i in eachindex(sc_current.storage_units)
        su = sc_current.storage_units[i]
        su_next = sc_next.storage_units[i]

        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])

        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)

        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

    sc_current.power_balance_penalty = repeat_values(sc_current.power_balance_penalty)
    sc_current.time = 2 * original_time
    sc_current.time_step ÷= 2
    return
end

function interpolate_values(values::Vector{T}, next_first::T) where {T<:Real}
    n = length(values)
    result = Vector{Float64}(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

    result[2 * n - 1] = values[n]
    result[2 * n] = (values[n] + next_first) / 2
    return result
end

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