forked from e2nIEE/pandapower
-
Notifications
You must be signed in to change notification settings - Fork 9
/
runpm.py
395 lines (301 loc) · 21.4 KB
/
runpm.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
import os
from pandapower import pp_dir
from pandapower.auxiliary import _add_ppc_options, _add_opf_options
from pandapower.converter.powermodels.from_pm import read_ots_results, read_tnep_results
from pandapower.opf.pm_storage import add_storage_opf_settings, read_pm_storage_results
from pandapower.opf.run_powermodels import _runpm
def runpm(net, julia_file=None, pp_to_pm_callback=None, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
correct_pm_network_data=True, pm_model="ACPPowerModel", pm_solver="ipopt",
pm_mip_solver="cbc", pm_nl_solver="ipopt", pm_time_limits=None, pm_log_level=0,
delete_buffer_file=True, pm_file_path = None, opf_flow_lim="S", **kwargs): # pragma: no cover
"""
Runs a power system optimization using PowerModels.jl. with a custom julia file.
Flexibilities, constraints and cost parameters are defined in the pandapower element tables.
Flexibilities can be defined in net.sgen / net.gen /net.load
net.sgen.controllable if a static generator is controllable. If False,
the active and reactive power are assigned as in a normal power flow. If True, the following
flexibilities apply:
- net.sgen.min_p_mw / net.sgen.max_p_mw
- net.sgen.min_q_mvar / net.sgen.max_q_mvar
- net.load.min_p_mw / net.load.max_p_mw
- net.load.min_q_mvar / net.load.max_q_mvar
- net.gen.min_p_mw / net.gen.max_p_mw
- net.gen.min_q_mvar / net.gen.max_q_mvar
- net.ext_grid.min_p_mw / net.ext_grid.max_p_mw
- net.ext_grid.min_q_mvar / net.ext_grid.max_q_mvar
- net.dcline.min_q_to_mvar / net.dcline.max_q_to_mvar / net.dcline.min_q_from_mvar / net.dcline.max_q_from_mvar
Controllable loads behave just like controllable static generators. It must be stated if they are controllable.
Otherwise, they are not respected as flexibilities.
Dc lines are controllable per default
Network constraints can be defined for buses, lines and transformers the elements in the following columns:
- net.bus.min_vm_pu / net.bus.max_vm_pu
- net.line.max_loading_percent
- net.trafo.max_loading_percent
- net.trafo3w.max_loading_percent
How these costs are combined into a cost function depends on the cost_function parameter.
INPUT:
**net** - The pandapower format network
OPTIONAL:
**julia_file** (str, None) - path to a custom julia optimization file
**pp_to_pm_callback** (function, None) - callback function to add data to the PowerModels data structure
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
**pm_model** (str, "ACPPowerModel") - The PowerModels.jl model to use
**pm_solver** (str, "ipopt") - The "main" power models solver
**pm_mip_solver** (str, "cbc") - The mixed integer solver (when "main" solver == juniper)
**pm_nl_solver** (str, "ipopt") - The nonlinear solver (when "main" solver == juniper)
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300., "pm_nl_time_limit": 300., "pm_mip_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
**delete_buffer_file** (Bool, True) - If True, the .json file used by powermodels will be deleted after
optimization.
**pm_file_path** (str, None) - Specifiy the filename, under which the .json file for powermodels is stored. If
you want to keep the file after optimization, you should also set
delete_buffer_file to False!
**opf_flow_lim** (str, "I") - Quantity to limit for branch flow constraints, in line with matpower's
"opf.flowlim" parameter
"S" - apparent power flow (limit in MVA),
"I" - current magnitude (limit in MVA at 1 p.u. voltage)
"""
net._options = {}
ac = True if "DC" not in pm_model else False
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels.jl') if julia_file is None else julia_file
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_solver=pm_solver, pm_model=pm_model,
correct_pm_network_data=correct_pm_network_data, pm_mip_solver=pm_mip_solver,
pm_nl_solver=pm_nl_solver, pm_time_limits=pm_time_limits, pm_log_level=pm_log_level,
opf_flow_lim=opf_flow_lim)
_runpm(net, delete_buffer_file=delete_buffer_file, pm_file_path = pm_file_path)
def runpm_dc_opf(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
correct_pm_network_data=True, pm_model="DCPPowerModel", pm_solver="ipopt",
pm_time_limits=None, pm_log_level=0, **kwargs): # pragma: no cover
"""
Runs a linearized power system optimization using PowerModels.jl.
Flexibilities, constraints and cost parameters are defined in the pandapower element tables.
Flexibilities can be defined in net.sgen / net.gen /net.load
net.sgen.controllable if a static generator is controllable. If False,
the active and reactive power are assigned as in a normal power flow. If True, the following
flexibilities apply:
- net.sgen.min_p_mw / net.sgen.max_p_mw
- net.sgen.min_q_mvar / net.sgen.max_q_mvar
- net.load.min_p_mw / net.load.max_p_mw
- net.load.min_q_mvar / net.load.max_q_mvar
- net.gen.min_p_mw / net.gen.max_p_mw
- net.gen.min_q_mvar / net.gen.max_q_mvar
- net.ext_grid.min_p_mw / net.ext_grid.max_p_mw
- net.ext_grid.min_q_mvar / net.ext_grid.max_q_mvar
- net.dcline.min_q_to_mvar / net.dcline.max_q_to_mvar / net.dcline.min_q_from_mvar / net.dcline.max_q_from_mvar
Controllable loads behave just like controllable static generators. It must be stated if they are controllable.
Otherwise, they are not respected as flexibilities.
Dc lines are controllable per default
Network constraints can be defined for buses, lines and transformers the elements in the following columns:
- net.bus.min_vm_pu / net.bus.max_vm_pu
- net.line.max_loading_percent
- net.trafo.max_loading_percent
- net.trafo3w.max_loading_percent
How these costs are combined into a cost function depends on the cost_function parameter.
INPUT:
**net** - The pandapower format network
OPTIONAL:
**pp_to_pm_callback** (function, None) - callback function to add data to the PowerModels data structure
**pm_model** (str, "DCPPowerModel") - model to use. Default is DC model
**pm_solver** (str, "ipopt") - The "main" power models solver
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels.jl')
ac = True if "DC" not in pm_model else False
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file,
correct_pm_network_data=correct_pm_network_data, pm_model=pm_model, pm_solver=pm_solver,
pm_time_limits=pm_time_limits, pm_log_level=pm_log_level, opf_flow_lim="S")
_runpm(net)
def runpm_ac_opf(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
pm_model="ACPPowerModel", pm_solver="ipopt", correct_pm_network_data=True,
pm_time_limits=None, pm_log_level=0, pm_file_path = None, delete_buffer_file=True,
opf_flow_lim="S", **kwargs): # pragma: no cover
"""
Runs a non-linear power system optimization using PowerModels.jl.
Flexibilities, constraints and cost parameters are defined in the pandapower element tables.
Flexibilities can be defined in net.sgen / net.gen /net.load
net.sgen.controllable if a static generator is controllable. If False,
the active and reactive power are assigned as in a normal power flow. If True, the following
flexibilities apply:
- net.sgen.min_p_mw / net.sgen.max_p_mw
- net.sgen.min_q_mvar / net.sgen.max_q_mvar
- net.load.min_p_mw / net.load.max_p_mw
- net.load.min_q_mvar / net.load.max_q_mvar
- net.gen.min_p_mw / net.gen.max_p_mw
- net.gen.min_q_mvar / net.gen.max_q_mvar
- net.ext_grid.min_p_mw / net.ext_grid.max_p_mw
- net.ext_grid.min_q_mvar / net.ext_grid.max_q_mvar
- net.dcline.min_q_to_mvar / net.dcline.max_q_to_mvar / net.dcline.min_q_from_mvar / net.dcline.max_q_from_mvar
Controllable loads behave just like controllable static generators. It must be stated if they are controllable.
Otherwise, they are not respected as flexibilities.
Dc lines are controllable per default
Network constraints can be defined for buses, lines and transformers the elements in the following columns:
- net.bus.min_vm_pu / net.bus.max_vm_pu
- net.line.max_loading_percent
- net.trafo.max_loading_percent
- net.trafo3w.max_loading_percent
How these costs are combined into a cost function depends on the cost_function parameter.
INPUT:
**net** - The pandapower format network
OPTIONAL:
**pp_to_pm_callback** (function, None) - callback function to add data to the PowerModels data structure
**pm_model** (str, "ACPPowerModel") - model to use. Default is AC model
**pm_solver** (str, "ipopt") - default solver to use. If ipopt is not available use Ipopt
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
**opf_flow_lim** (str, "I") - Quantity to limit for branch flow constraints, in line with matpower's
"opf.flowlim" parameter
"S" - apparent power flow (limit in MVA),
"I" - current magnitude (limit in MVA at 1 p.u. voltage)
**delete_buffer_file** (Bool, True) - If True, the .json file used by powermodels will be deleted after
optimization.
**pm_file_path** (str, None) - Specifiy the filename, under which the .json file for powermodels is stored. If
you want to keep the file after optimization, you should also set
delete_buffer_file to False!
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels.jl')
ac = True if "DC" not in pm_model else False
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data, pm_time_limits=pm_time_limits,
pm_log_level=pm_log_level, opf_flow_lim=opf_flow_lim)
_runpm(net, pm_file_path=pm_file_path, delete_buffer_file=delete_buffer_file)
def runpm_tnep(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
pm_model="DCPPowerModel", pm_solver=None, correct_pm_network_data=True,
pm_nl_solver="ipopt", pm_mip_solver="cbc", pm_time_limits=None, pm_log_level=0,
opf_flow_lim="S", **kwargs): # pragma: no cover
"""
Runs a non-linear transmission network extension planning (tnep) optimization using PowerModels.jl.
OPTIONAL:
**julia_file** (str, None) - path to a custom julia optimization file
**pp_to_pm_callback** (function, None) - callback function to add data to the PowerModels data structure
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
**pm_model** (str, "ACPPowerModel") - The PowerModels.jl model to use
**pm_solver** (str, "juniper") - The "main" power models solver
**pm_mip_solver** (str, "cbc") - The mixed integer solver (when "main" solver == juniper)
**pm_nl_solver** (str, "ipopt") - The nonlinear solver (when "main" solver == juniper)
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300., "pm_nl_time_limit": 300., "pm_mip_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_tnep.jl')
ac = True if "DC" not in pm_model else False
if pm_solver is None:
if pm_model == "DCPPowerModel":
pm_solver = "gurobi"
else:
pm_solver = "juniper"
if "ne_line" not in net:
raise ValueError("ne_line DataFrame missing in net. Please define to run tnep")
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data, pm_nl_solver=pm_nl_solver,
pm_mip_solver=pm_mip_solver, pm_time_limits=pm_time_limits, pm_log_level=pm_log_level,
opf_flow_lim=opf_flow_lim)
_runpm(net)
read_tnep_results(net)
def runpm_ots(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
pm_model="DCPPowerModel", pm_solver="juniper", pm_nl_solver="ipopt", pm_mip_solver="cbc",
correct_pm_network_data=True, pm_time_limits=None, pm_log_level=0, **kwargs): # pragma: no cover
"""
Runs a non-linear optimal transmission switching (OTS) optimization using PowerModels.jl.
OPTIONAL:
**julia_file** (str, None) - path to a custom julia optimization file
**pp_to_pm_callback** (function, None) - callback function to add data to the PowerModels data structure
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
**pm_model** (str, "ACPPowerModel") - The PowerModels.jl model to use
**pm_solver** (str, "juniper") - The "main" power models solver
**pm_mip_solver** (str, "cbc") - The mixed integer solver (when "main" solver == juniper)
**pm_nl_solver** (str, "ipopt") - The nonlinear solver (when "main" solver == juniper)
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300., "pm_nl_time_limit": 300., "pm_mip_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_ots.jl')
ac = True if "DC" not in pm_model else False
if pm_solver is None:
pm_solver = "juniper"
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data, pm_mip_solver=pm_mip_solver,
pm_nl_solver=pm_nl_solver, pm_time_limits=pm_time_limits, pm_log_level=pm_log_level,
opf_flow_lim="S")
_runpm(net)
read_ots_results(net)
def runpm_storage_opf(net, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
n_timesteps=24, time_elapsed=1.0, correct_pm_network_data=True,
pm_model="ACPPowerModel", pm_time_limits=None, pm_log_level=0, **kwargs): # pragma: no cover
"""
Runs a non-linear power system optimization with storages and time series using PowerModels.jl.
INPUT:
**net** - The pandapower format network
OPTIONAL:
**n_timesteps** (int, 24) - number of time steps to optimize
**time_elapsed** (float, 1.0) - time elapsed between time steps (1.0 = 1 hour)
**pm_time_limits** (Dict, None) - Time limits in seconds for power models interface. To be set as a dict like
{"pm_time_limit": 300., "pm_nl_time_limit": 300., "pm_mip_time_limit": 300.}
**pm_log_level** (int, 0) - solver log level in power models
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_mn_storage.jl')
ac = True if "DC" not in pm_model else False
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=add_storage_opf_settings, julia_file=julia_file,
correct_pm_network_data=correct_pm_network_data, pm_model=pm_model, pm_time_limits=pm_time_limits,
pm_log_level=pm_log_level)
net._options["n_time_steps"] = n_timesteps
net._options["time_elapsed"] = time_elapsed
_runpm(net)
storage_results = read_pm_storage_results(net)
return storage_results