pypower的简单应用1

目录

一、背景描述

二、如何打开IEEE30节点并进行潮流计算

三、如何修改已有模型参数

四、完整代码

五、注意事项


pypower与matpower非常类似,可以利用matpower学习pypower,当然也有一些不同之处。下面记录一下应用pypower解决的问题。


一、背景描述

利用pypower中现有的IEEE30节点网络,修改发电机出力,再进行100次潮流计算,将不符合电力系统运行条件的筛选出来。

问题分解为两步:

1. 如何打开IEEE30节点并进行潮流计算

2. 如何修改已有模型的参数


二、如何打开IEEE30节点并进行潮流计算

非常的简单

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.2
  Fixed           20       Fixed                189.2             107.2
  Dispatchable     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              3
                          Minimum                      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.08
   1      1      3     15.08      4.09    -14.96     -5.57     0.127      0.48
   2      2      4     16.07      5.21    -15.89     -6.66     0.178      0.50
   3      3      4     12.56      4.37    -12.54     -4.30     0.018      0.07
   4      2      5     13.79      4.51    -13.68     -6.03     0.110      0.44
   5      2      6     20.28      7.42    -19.99     -8.50     0.289      0.87
   6      4      6     22.50     11.38    -22.43    -11.12     0.066      0.26
   7      5      7     13.68      6.21    -13.56     -6.88     0.120      0.29
   8      6      7      9.27      3.17     -9.24     -4.02     0.031      0.08
   9      6      8     24.82     24.43    -24.69    -23.92     0.128      0.51
  10      6      9      5.79     -3.36     -5.79      3.46    -0.000      0.10
  11      6     10      3.31     -1.92     -3.31      2.00    -0.000      0.09
  12      9     11      0.00      0.00      0.00      0.00    -0.000      0.00
  13      9     10      5.79     -3.46     -5.79      3.51    -0.000      0.05
  14      4     12     -1.67     -2.02      1.67      2.04    -0.000      0.02
  15     12     13    -37.00     -9.26     37.00     11.35    -0.000      2.10
  16     12     14      5.39      0.88     -5.35     -0.80     0.037      0.08
  17     12     15      9.48     -1.06     -9.41      1.19     0.066      0.12
  18     12     16      9.26     -0.10     -9.18      0.28     0.080      0.18
  19     14     15     -0.85     -0.80      0.85      0.80     0.003      0.00
  20     16     17      5.68     -2.08     -5.65      2.15     0.031      0.07
  21     15     18      9.16      0.76     -9.07     -0.57     0.097      0.19
  22     18     19      5.87     -0.33     -5.85      0.38     0.022      0.05
  23     19     20     -3.65     -3.78      3.66      3.80     0.009      0.02
  24     10     20      5.92      4.62     -5.86     -4.50     0.052      0.12
  25     10     17      3.37      8.01     -3.35     -7.95     0.023      0.06
  26     10     21     -2.23    -11.67      2.28     11.77     0.044      0.10
  27     10     22     -3.75     -8.48      3.82      8.62     0.062      0.13
  28     21     22    -19.78    -22.97     19.87     23.16     0.093      0.19
  29     15     23     -8.81     -5.25      8.91      5.47     0.109      0.22
  30     22     24     -2.10      7.80      2.18     -7.68     0.078      0.12
  31     23     24      7.09      0.88     -7.02     -0.75     0.066      0.14
  32     24     25     -3.86      1.77      3.89     -1.71     0.035      0.06
  33     25     26      3.55      2.37     -3.50     -2.30     0.046      0.07
  34     25     27     -7.44     -0.66      7.50      0.78     0.063      0.12
  35     28     27     -6.11     -6.08      6.11      6.40    -0.000      0.31
  36     27     29      6.17      1.68     -6.08     -1.51     0.090      0.17
  37     27     30      7.12      1.67     -6.95     -1.35     0.171      0.32
  38     29     30      3.68      0.61     -3.65     -0.55     0.035      0.07
  39      8     28     -5.31     -6.08      5.34      4.33     0.036      0.12
  40      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: 100
        bus: [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 Flow
  AC 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.2
  Fixed           20       Fixed                189.2             107.2
  Dispatchable     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              3

                          Minimum                      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.016
    4  0.976   -2.839       -         -        7.60      1.60     3.771  -0.021
    5  0.971   -2.486       -         -         -         -       3.744  -0.001
    6  0.972   -3.229       -         -         -         -       3.779  -0.020
    7  0.962   -3.491       -         -       22.80     10.90     3.801   0.003
    8  0.961   -3.682       -         -       30.00     30.00     5.383   1.405
    9  0.990   -4.137       -         -         -         -       3.823   0.020
   10  1.000   -4.600       -         -        5.80      2.00     3.846   0.039
   11  0.990   -4.137       -         -         -         -       3.823   0.020
   12  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.018
   15  1.009   -4.814       -         -        8.20      2.50     3.856   0.018
   16  1.003   -4.839       -         -        3.50      1.80     3.849   0.031
   17  0.995   -4.887       -         -        9.00      5.80     3.862   0.047
   18  0.993   -5.484       -         -        3.20      0.90     3.911   0.047
   19  0.987   -5.688       -         -        9.50      3.40     3.926   0.058
   20  0.990   -5.472       -         -        2.20      0.70     3.910   0.055
   21  1.009   -4.621       -         -       17.50     11.20     3.854   0.017
   22  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.028
   25  1.044   -2.072       -         -         -         -       3.932   0.022
   26  1.027   -2.476       -         -        3.50      2.30     3.999   0.067
   27  1.069   -0.715     39.91     31.75       -         -       3.916     -
   28  0.982   -3.215       -         -         -         -       4.106   0.250
   29  1.050   -1.849       -         -        2.40      0.90     3.966  -0.059
   30  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.28
   2      1      3     20.50     -3.10    -20.28      2.02     0.220      0.84
   3      2      4     18.63     -5.85    -18.40      4.60     0.232      0.66
   4      3      4     17.88     -3.22    -17.84      3.36     0.035      0.14
   5      2      5     14.36     -0.69    -14.25     -0.78     0.108      0.43
   6      2      6     21.66     -4.21    -21.36      3.21     0.300      0.90
   7      4      6     17.58      5.68    -17.54     -5.54     0.036      0.14
   8      5      7     14.25      0.96    -14.15     -1.64     0.109      0.26
   9      6      7      8.70      8.46     -8.65     -9.26     0.049      0.13
  10      6      8     23.82     21.37    -23.71    -20.93     0.108      0.43
  11      6      9      7.27     -8.27     -7.27      8.54     0.000      0.27
  12      6     10      4.15     -4.73     -4.15      4.96     0.000      0.23
  13      9     11      0.00      0.00      0.00      0.00     0.000      0.00
  14      9     10      7.27     -8.54     -7.27      8.68     0.000      0.14
  15      4     12     11.06    -15.24    -11.06     16.21     0.000      0.97
  16     12     13    -16.20    -34.01     16.20     35.93     0.000      1.92
  17     12     14      4.68      2.08     -4.65     -2.01     0.030      0.07
  18     12     15      6.07      3.18     -6.04     -3.12     0.032      0.06
  19     12     16      5.31      5.04     -5.26     -4.94     0.047      0.10
  20     14     15     -1.55      0.41      1.55     -0.41     0.006      0.01
  21     16     17      1.76      3.14     -1.75     -3.12     0.010      0.02
  22     15     18      7.20      3.75     -7.13     -3.60     0.071      0.14
  23     18     19      3.93      2.70     -3.92     -2.67     0.014      0.03
  24     19     20     -5.58     -0.73      5.59      0.75     0.010      0.02
  25     10     20      7.85      1.58     -7.79     -1.45     0.058      0.13
  26     10     17      7.27      2.73     -7.25     -2.68     0.018      0.05
  27     10     21     -4.43    -11.57      4.47     11.67     0.046      0.11
  28     10     22     -5.06     -8.39      5.13      8.54     0.067      0.14
  29     21     22    -21.97    -22.87     22.07     23.07     0.099      0.20
  30     15     23    -10.92     -2.72     11.04      2.97     0.124      0.25
  31     22     24     -4.46      2.59      4.49     -2.54     0.031      0.05
  32     23     24      2.03      2.39     -2.01     -2.37     0.012      0.03
  33     24     25    -11.18     -1.75     11.41      2.16     0.235      0.41
  34     25     26      3.54      2.36     -3.50     -2.30     0.042      0.06
  35     25     27    -14.96     -4.52     15.20      4.99     0.246      0.47
  36     28     27    -11.45    -21.09     11.45     23.48     0.000      2.39
  37     27     29      6.16      1.65     -6.08     -1.50     0.078      0.15
  38     27     30      7.10      1.63     -6.95     -1.36     0.149      0.28
  39     29     30      3.68      0.60     -3.65     -0.54     0.030      0.06
  40      8     28     -6.29     -9.07      6.36      7.41     0.069      0.23
  41      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      -        8
  35     25      -       15.62     16.00     16.00     0.024    27

再来看看修改的地方:

pypower的简单应用1_第1张图片

确实由默认的80改为100.


四、完整代码

想要实现一开始的功能,完整代码如下:

from pypower.api import case30, runpf, loadcase
import numpy as np

data = case30()
result = runpf(data)

line_num = 41
linecapacity = 200 * np.ones((line_num)) #不知道线路载流量是多少,这里先给一个较大的值,不会对结果判断有影响
#Special_line = np.array([30]) #不知都重点线路有多少
#linecapacity[Special_line] = 10


def 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_vector

def 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 = 30
    ACline_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 = False
    r = 0
    if any(BusV > 1.15) or any(BusV < 0.9):
        done = True
    elif len(ob_current) != (Bus_nums + ACline_nums * 2):
        done = True
    else:
        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_limit
        if any(line_rho >= 1) or any(b_gen_rho > 1):
            done = True
        else:
            done = False
    return done

GenScenarioNum = 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() #打开case30
    mpc['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对照着看。比如这里:

 想要得到交流线路的有功和无功

你可能感兴趣的:(pypower应用,python)