目录
一、背景描述
二、如何打开IEEE30节点并进行潮流计算
三、如何修改已有模型参数
四、完整代码
五、注意事项
pypower与matpower非常类似,可以利用matpower学习pypower,当然也有一些不同之处。下面记录一下应用pypower解决的问题。
利用pypower中现有的IEEE30节点网络,修改发电机出力,再进行100次潮流计算,将不符合电力系统运行条件的筛选出来。
问题分解为两步:
1. 如何打开IEEE30节点并进行潮流计算
2. 如何修改已有模型的参数
非常的简单
from pypower.api import case30, runpf, loadcase
import numpy as np
data = case30()
result = runpf(data)
结果:
C:\Users\Admin\anaconda3\envs\pppp\python.exe "C:/Program Files/JetBrains/PyCharm 2022.2.3/plugins/python/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 58498 --file "C:\First semester of Senior year\safe_and_stable_project\PSASP\psasp_pypower.py"
已连接到 pydev 调试器(内部版本号 222.4345.23)PYPOWER Version 5.1.4, 27-June-2018 -- AC Power Flow (Newton)
Newton's method power flow converged in 3 iterations.
Converged in 0.05 seconds
================================================================================
| System Summary |
================================================================================
How many? How much? P (MW) Q (MVAr)
--------------------- ------------------- ------------- -----------------
Buses 30 Total Gen Capacity 335.0 -95.0 to 405.9
Generators 6 On-line Capacity 335.0 -95.0 to 405.9
Committed Gens 6 Generation (actual) 191.6 100.4
Loads 20 Load 189.2 107.2Fixed 20 Fixed 189.2 107.2Dispatchable 0 Dispatchable 0.0 of 0.0 0.0
Shunts 2 Shunt (inj) -0.0 0.2
Branches 41 Losses (I^2 * Z) 2.44 8.99
Transformers 0 Branch Charging (inj) - 15.6
Inter-ties 7 Total Inter-tie Flow 33.2 27.1
Areas 3Minimum Maximum------------------------- --------------------------------
Voltage Magnitude 0.961 p.u. @ bus 8 1.000 p.u. @ bus 1
Voltage Angle -3.96 deg @ bus 19 1.48 deg @ bus 13
P Losses (I^2*R) - 0.29 MW @ line 2-6
Q Losses (I^2*X) - 2.10 MVAr @ line 12-13
================================================================================
| Bus Data |
================================================================================Bus Voltage Generation Load # Mag(pu) Ang(deg) P (MW) Q (MVAr) P (MW) Q (MVAr)
----- ------- -------- -------- -------- -------- --------1 1.000 0.000* 25.97 -1.00 - - 2 1.000 -0.415 60.97 32.00 21.70 12.70 3 0.983 -1.522 - - 2.40 1.20 4 0.980 -1.795 - - 7.60 1.60 5 0.982 -1.864 - - - - 6 0.973 -2.267 - - - - 7 0.967 -2.652 - - 22.80 10.90 8 0.961 -2.726 - - 30.00 30.00 9 0.981 -2.997 - - - - 10 0.984 -3.375 - - 5.80 2.00 11 0.981 -2.997 - - - - 12 0.985 -1.537 - - 11.20 7.50 13 1.000 1.476 37.00 11.35 - - 14 0.977 -2.308 - - 6.20 1.60 15 0.980 -2.312 - - 8.20 2.50 16 0.977 -2.644 - - 3.50 1.80 17 0.977 -3.392 - - 9.00 5.80 18 0.968 -3.478 - - 3.20 0.90 19 0.965 -3.958 - - 9.50 3.40 20 0.969 -3.871 - - 2.20 0.70 21 0.993 -3.488 - - 17.50 11.20 22 1.000 -3.393 21.59 39.57 - - 23 1.000 -1.589 19.20 7.95 3.20 1.60 24 0.989 -2.631 - - 8.70 6.70 25 0.990 -1.690 - - - - 26 0.972 -2.139 - - 3.50 2.30 27 1.000 -0.828 26.91 10.54 - - 28 0.975 -2.266 - - - - 29 0.980 -2.128 - - 2.40 0.90 30 0.968 -3.042 - - 10.60 1.90 -------- -------- -------- --------Total: 191.64 100.41 189.20 107.20
================================================================================
| Branch Data |
================================================================================
Brnch From To From Bus Injection To Bus Injection Loss (I^2 * Z) # Bus Bus P (MW) Q (MVAr) P (MW) Q (MVAr) P (MW) Q (MVAr)
----- ----- ----- -------- -------- -------- -------- -------- --------0 1 2 10.89 -5.09 -10.86 2.17 0.026 0.081 1 3 15.08 4.09 -14.96 -5.57 0.127 0.482 2 4 16.07 5.21 -15.89 -6.66 0.178 0.503 3 4 12.56 4.37 -12.54 -4.30 0.018 0.074 2 5 13.79 4.51 -13.68 -6.03 0.110 0.445 2 6 20.28 7.42 -19.99 -8.50 0.289 0.876 4 6 22.50 11.38 -22.43 -11.12 0.066 0.267 5 7 13.68 6.21 -13.56 -6.88 0.120 0.298 6 7 9.27 3.17 -9.24 -4.02 0.031 0.089 6 8 24.82 24.43 -24.69 -23.92 0.128 0.5110 6 9 5.79 -3.36 -5.79 3.46 -0.000 0.1011 6 10 3.31 -1.92 -3.31 2.00 -0.000 0.0912 9 11 0.00 0.00 0.00 0.00 -0.000 0.0013 9 10 5.79 -3.46 -5.79 3.51 -0.000 0.0514 4 12 -1.67 -2.02 1.67 2.04 -0.000 0.0215 12 13 -37.00 -9.26 37.00 11.35 -0.000 2.1016 12 14 5.39 0.88 -5.35 -0.80 0.037 0.0817 12 15 9.48 -1.06 -9.41 1.19 0.066 0.1218 12 16 9.26 -0.10 -9.18 0.28 0.080 0.1819 14 15 -0.85 -0.80 0.85 0.80 0.003 0.0020 16 17 5.68 -2.08 -5.65 2.15 0.031 0.0721 15 18 9.16 0.76 -9.07 -0.57 0.097 0.1922 18 19 5.87 -0.33 -5.85 0.38 0.022 0.0523 19 20 -3.65 -3.78 3.66 3.80 0.009 0.0224 10 20 5.92 4.62 -5.86 -4.50 0.052 0.1225 10 17 3.37 8.01 -3.35 -7.95 0.023 0.0626 10 21 -2.23 -11.67 2.28 11.77 0.044 0.1027 10 22 -3.75 -8.48 3.82 8.62 0.062 0.1328 21 22 -19.78 -22.97 19.87 23.16 0.093 0.1929 15 23 -8.81 -5.25 8.91 5.47 0.109 0.2230 22 24 -2.10 7.80 2.18 -7.68 0.078 0.1231 23 24 7.09 0.88 -7.02 -0.75 0.066 0.1432 24 25 -3.86 1.77 3.89 -1.71 0.035 0.0633 25 26 3.55 2.37 -3.50 -2.30 0.046 0.0734 25 27 -7.44 -0.66 7.50 0.78 0.063 0.1235 28 27 -6.11 -6.08 6.11 6.40 -0.000 0.3136 27 29 6.17 1.68 -6.08 -1.51 0.090 0.1737 27 30 7.12 1.67 -6.95 -1.35 0.171 0.3238 29 30 3.68 0.61 -3.65 -0.55 0.035 0.0739 8 28 -5.31 -6.08 5.34 4.33 0.036 0.1240 6 28 -0.77 -2.70 0.77 1.75 0.001 0.00-------- --------Total: 2.444 8.99
注意:
runpf表示用牛拉法进行求解,也有其他的求解器,比如opf,cplex等等,目前还用不到;
这里matpower与pypower不同
先来看pypower:
先打开case30模型,然后对应部分进行赋值,最后再算;
对于matpower,略复杂:
define_constants;
mpc = loadcase('case30')
mpc.gen(1,9)=100;
runopf(mpc);
需要先用函数:define_constants;
然后打开case30文件
再修改对应位置的参数值;
最后运行修改过的
结果为:
mpc = 包含以下字段的 struct:version: '2'baseMVA: 100bus: [30×13 double]gen: [6×21 double]branch: [41×13 double]gencost: [6×7 double]MATPOWER Version 7.1, 08-Oct-2020 -- AC Optimal Power FlowAC OPF formulation: polar voltages, power balance eqns
MATPOWER Interior Point Solver -- MIPS, Version 1.4, 08-Oct-2020(using built-in linear solver)
Converged!Converged in 0.25 seconds
Objective Function Value = 576.89 $/hr
================================================================================
| System Summary |
================================================================================How many? How much? P (MW) Q (MVAr)
--------------------- ------------------- ------------- -----------------
Buses 30 Total Gen Capacity 355.0 -95.0 to 405.9
Generators 6 On-line Capacity 355.0 -95.0 to 405.9
Committed Gens 6 Generation (actual) 192.1 105.1
Loads 20 Load 189.2 107.2Fixed 20 Fixed 189.2 107.2Dispatchable 0 Dispatchable -0.0 of -0.0 -0.0
Shunts 2 Shunt (inj) -0.0 0.2
Branches 41 Losses (I^2 * Z) 2.86 13.33
Transformers 0 Branch Charging (inj) - 15.2
Inter-ties 7 Total Inter-tie Flow 51.0 58.1
Areas 3Minimum Maximum------------------------- --------------------------------
Voltage Magnitude 0.961 p.u. @ bus 8 1.069 p.u. @ bus 27
Voltage Angle -5.69 deg @ bus 19 0.00 deg @ bus 1
P Losses (I^2*R) - 0.30 MW @ line 2-6
Q Losses (I^2*X) - 2.39 MVAr @ line 28-27
Lambda P 3.66 $/MWh @ bus 1 5.38 $/MWh @ bus 8
Lambda Q -0.06 $/MWh @ bus 29 1.40 $/MWh @ bus 8 ================================================================================
| Bus Data |
================================================================================Bus Voltage Generation Load Lambda($/MVA-hr)# Mag(pu) Ang(deg) P (MW) Q (MVAr) P (MW) Q (MVAr) P Q
----- ------- -------- -------- -------- -------- -------- ------- -------1 0.982 0.000* 41.54 -5.44 - - 3.662 -2 0.979 -0.763 55.40 1.67 21.70 12.70 3.689 -3 0.977 -2.390 - - 2.40 1.20 3.754 -0.0164 0.976 -2.839 - - 7.60 1.60 3.771 -0.0215 0.971 -2.486 - - - - 3.744 -0.0016 0.972 -3.229 - - - - 3.779 -0.0207 0.962 -3.491 - - 22.80 10.90 3.801 0.0038 0.961 -3.682 - - 30.00 30.00 5.383 1.4059 0.990 -4.137 - - - - 3.823 0.02010 1.000 -4.600 - - 5.80 2.00 3.846 0.03911 0.990 -4.137 - - - - 3.823 0.02012 1.017 -4.498 - - 11.20 7.50 3.810 -13 1.064 -3.298 16.20 35.93 - - 3.810 -14 1.007 -5.040 - - 6.20 1.60 3.868 0.01815 1.009 -4.814 - - 8.20 2.50 3.856 0.01816 1.003 -4.839 - - 3.50 1.80 3.849 0.03117 0.995 -4.887 - - 9.00 5.80 3.862 0.04718 0.993 -5.484 - - 3.20 0.90 3.911 0.04719 0.987 -5.688 - - 9.50 3.40 3.926 0.05820 0.990 -5.472 - - 2.20 0.70 3.910 0.05521 1.009 -4.621 - - 17.50 11.20 3.854 0.01722 1.016 -4.503 22.74 34.20 - - 3.843 -23 1.026 -3.756 16.27 6.96 3.20 1.60 3.813 -24 1.017 -3.885 - - 8.70 6.70 3.884 0.02825 1.044 -2.072 - - - - 3.932 0.02226 1.027 -2.476 - - 3.50 2.30 3.999 0.06727 1.069 -0.715 39.91 31.75 - - 3.916 -28 0.982 -3.215 - - - - 4.106 0.25029 1.050 -1.849 - - 2.40 0.90 3.966 -0.05930 1.039 -2.643 - - 10.60 1.90 4.051 -0.012-------- -------- -------- --------Total: 192.06 105.08 189.20 107.20================================================================================
| Branch Data |
================================================================================
Brnch From To From Bus Injection To Bus Injection Loss (I^2 * Z) # Bus Bus P (MW) Q (MVAr) P (MW) Q (MVAr) P (MW) Q (MVAr)
----- ----- ----- -------- -------- -------- -------- -------- --------1 1 2 21.04 -2.34 -20.95 -0.27 0.092 0.282 1 3 20.50 -3.10 -20.28 2.02 0.220 0.843 2 4 18.63 -5.85 -18.40 4.60 0.232 0.664 3 4 17.88 -3.22 -17.84 3.36 0.035 0.145 2 5 14.36 -0.69 -14.25 -0.78 0.108 0.436 2 6 21.66 -4.21 -21.36 3.21 0.300 0.907 4 6 17.58 5.68 -17.54 -5.54 0.036 0.148 5 7 14.25 0.96 -14.15 -1.64 0.109 0.269 6 7 8.70 8.46 -8.65 -9.26 0.049 0.1310 6 8 23.82 21.37 -23.71 -20.93 0.108 0.4311 6 9 7.27 -8.27 -7.27 8.54 0.000 0.2712 6 10 4.15 -4.73 -4.15 4.96 0.000 0.2313 9 11 0.00 0.00 0.00 0.00 0.000 0.0014 9 10 7.27 -8.54 -7.27 8.68 0.000 0.1415 4 12 11.06 -15.24 -11.06 16.21 0.000 0.9716 12 13 -16.20 -34.01 16.20 35.93 0.000 1.9217 12 14 4.68 2.08 -4.65 -2.01 0.030 0.0718 12 15 6.07 3.18 -6.04 -3.12 0.032 0.0619 12 16 5.31 5.04 -5.26 -4.94 0.047 0.1020 14 15 -1.55 0.41 1.55 -0.41 0.006 0.0121 16 17 1.76 3.14 -1.75 -3.12 0.010 0.0222 15 18 7.20 3.75 -7.13 -3.60 0.071 0.1423 18 19 3.93 2.70 -3.92 -2.67 0.014 0.0324 19 20 -5.58 -0.73 5.59 0.75 0.010 0.0225 10 20 7.85 1.58 -7.79 -1.45 0.058 0.1326 10 17 7.27 2.73 -7.25 -2.68 0.018 0.0527 10 21 -4.43 -11.57 4.47 11.67 0.046 0.1128 10 22 -5.06 -8.39 5.13 8.54 0.067 0.1429 21 22 -21.97 -22.87 22.07 23.07 0.099 0.2030 15 23 -10.92 -2.72 11.04 2.97 0.124 0.2531 22 24 -4.46 2.59 4.49 -2.54 0.031 0.0532 23 24 2.03 2.39 -2.01 -2.37 0.012 0.0333 24 25 -11.18 -1.75 11.41 2.16 0.235 0.4134 25 26 3.54 2.36 -3.50 -2.30 0.042 0.0635 25 27 -14.96 -4.52 15.20 4.99 0.246 0.4736 28 27 -11.45 -21.09 11.45 23.48 0.000 2.3937 27 29 6.16 1.65 -6.08 -1.50 0.078 0.1538 27 30 7.10 1.63 -6.95 -1.36 0.149 0.2839 29 30 3.68 0.60 -3.65 -0.54 0.030 0.0640 8 28 -6.29 -9.07 6.36 7.41 0.069 0.2341 6 28 -5.05 -14.50 5.09 13.68 0.047 0.14-------- --------Total: 2.860 13.33================================================================================
| Voltage Constraints |
================================================================================
Bus # Vmin mu Vmin |V| Vmax Vmax mu
----- -------- ----- ----- ----- --------29 - 0.950 1.050 1.050 29.810================================================================================
| Branch Flow Constraints (S in MVA) |
================================================================================
Brnch From "From" End Limit "To" End To# Bus |Sf| mu |Sf| |Smax| |St| |St| mu Bus
----- ----- ------- -------- -------- -------- ------- -----10 6 2.387 32.00 32.00 31.63 - 835 25 - 15.62 16.00 16.00 0.024 27
再来看看修改的地方:
确实由默认的80改为100.
想要实现一开始的功能,完整代码如下:
from pypower.api import case30, runpf, loadcase
import numpy as npdata = case30()
result = runpf(data)line_num = 41
linecapacity = 200 * np.ones((line_num)) #不知道线路载流量是多少,这里先给一个较大的值,不会对结果判断有影响
#Special_line = np.array([30]) #不知都重点线路有多少
#linecapacity[Special_line] = 10def Gen_vector(genmaxP, genP):gennum = len(genP)#vector_11 = (1 - (-1)) * np.random.random(size=gennum) + (-1) #产生一个(-1, 1)的随机数vector01 = np.random.random(size=gennum) #产生一个0-1的随机数gen_vector = vector01 * (genmaxP - genP) + genP #保证加扰动后的发电机出力不会超过最大值return gen_vectordef read_results():BusV = result[0]['bus'][:,7]# 读取交流线结果数据AClineP = result[0]['branch'][:, 13]AClineQ = result[0]['branch'][:, 14]# 读取发电机结果数据GenP = result[0]['gen'][:,1]GenQ = result[0]['gen'][:,2]return BusV, AClineP, AClineQ, GenP# 进行潮流计算结果异常与否的判定
def normalcheck(line_cap):Bus_nums = 30ACline_nums = 41 # TODO:更灵活和自适应的设备定义形式Balance_Gen_id = [0, 1] # 平衡机标号 TODO:后续应该按照名称进行记录以保证稳定性Balance_Gen_limit = 80 # 平衡机出力上限BusV, AClineP, AClineQ, GenP = read_results() # 当前仅考虑电压越限ob_current = list(BusV) + list(AClineP) + list(AClineQ)done = Falser = 0if any(BusV > 1.15) or any(BusV < 0.9):done = Trueelif len(ob_current) != (Bus_nums + ACline_nums * 2):done = Trueelse:Linetranspower = np.array(ob_current[Bus_nums:(Bus_nums + ACline_nums)])line_rho = abs(Linetranspower) / line_cap #abs返回数字的绝对值,也就是AClineP部分的绝对值b_gen_rho = GenP[Balance_Gen_id] / Balance_Gen_limitif any(line_rho >= 1) or any(b_gen_rho > 1):done = Trueelse:done = Falsereturn doneGenScenarioNum = 100
AvaScene = [] # 可用于训练的非正常场景
NormalScene = [] # 正常场景
for i in range(GenScenarioNum):print("current step:", i)# 设定场景初始配置genmaxP = result[0]['gen'][:, 8]genP = result[0]['gen'][:, 1]genP_modif = Gen_vector(genmaxP, genP)mpc = case30() #打开case30mpc['gen'][:, 1] = genP_modif #修改发电机出力#运行潮流计算runpf(mpc)# 进行潮流结果的判定并差异化存储done = normalcheck(linecapacity)if done:AvaScene.append(genP_modif)else:NormalScene.append(genP_modif)print(len(AvaScene))
print(len(NormalScene))#np.save("AvaS", AvaScene)
#np.save("NormalS", NormalScene)
在用pypower时,最大的问题是无法一目了然的知道数组中的每一列代表的含义,需要借助matpower对照着看。比如这里:
想要得到交流线路的有功和无功
上一篇:Eclipse常用设置
下一篇:动态规划之01背包问题