Project

General

Profile

LinearLoadEstimation_v1.py

Rotina principal - Gabriel José Negrelli Gomes, 12/27/2017 07:53 PM

Download (5.7 KB)

 
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Thu Mar 23 02:16:07 2017
4
Parameters Evaluation using RK4 Method
5
University of Sao Paulo
6
@author: Gabriel
7
"""
8

    
9
#Clearing system, importing libraries and setting flags
10
from os import system
11
system('cls')
12

    
13
import datetime
14

    
15
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
16

    
17
from numpy import *
18
import copy
19

    
20
convergence = False
21
print_figs = not False
22
step_by_step = False
23

    
24
if print_figs:
25
    import matplotlib.pyplot as plt
26
    plt.close('all')
27

    
28

    
29
#Run function files
30
runfile('Error.py')
31
runfile('Gamma.py')
32
runfile('Matrix.py')
33
runfile('Classification.py')
34
runfile('rk4.py')
35

    
36
#Real parameters
37
X_r = 0.2089
38
Xl_r = 0.0446
39
To_r = 0.0963
40
M_r = 0.0139
41
Gs_r = 4.1358
42
Bs_r = 2.8004
43
E0_r = 1.0750
44
delta0_r = -0.3689
45
omega0_r = 364.381
46
p_real = array([X_r, Xl_r, To_r, M_r, Gs_r, Bs_r, E0_r, delta0_r])#, omega0_r])
47

    
48
#Entrances
49
u = array([[0.0165]])
50

    
51
#Guessed parameters
52
k = .82
53

    
54
X = k*X_r
55
Xl = k*Xl_r
56
To = k*To_r
57
M = k*M_r
58
Gs = k*Gs_r
59
Bs = k*Bs_r
60
E0 = k*E0_r
61
delta0 = k*delta0_r
62
omega0 = 364
63
p = array([X, Xl, To, M, Gs, Bs, E0, delta0])#, omega0])
64

    
65
#Initial values of voltage magnitude and angle and angular speed
66
x_o = array([[1.0750, -0.3689, 364.381]])
67

    
68
#Initial values of line voltage
69
u0 = array([1])
70

    
71
#Variation on parameters in order to evaluate sensibilities
72
delta_p = .001*ones_like(p)
73

    
74
#Method presets
75
init_time = 0
76
final_time = .5
77
step = .0005
78

    
79

    
80
#Number of parameters analyzed
81
num_param = p.shape[0]
82

    
83
#Variable storing parameters evolution at each iteration
84
evolution = copy.copy(p)
85

    
86
#Loop Restrictions
87
tolerance = .0005   #Error tolerance
88
count = 0           #Number of iterations
89
p_ativo = ones_like(p)
90

    
91
#Real System Behaviour
92
op_real = rk4(init_time,final_time,step,x_o,u0,p_real,delta_p,u,-1)
93

    
94
#Chosen Parameters' System Beahviour
95
op = rk4(init_time,final_time,step,x_o,u0,p,delta_p,u,-1)
96

    
97
pold = copy.copy(op)
98

    
99
#Error Evaluation (Least-Mean Square)
100
Jp = .5*step*Error(op_real[:,1:],op[:,1:])
101
err = Jp          #Variable Storing Error Evolution
102

    
103

    
104
if print_figs:    
105
    f, axarr = plt.subplots(2, sharex=True)
106
    axarr[0].plot(op[:,0],op[:,1],'-.',label="Model", lw=5)
107
    axarr[0].plot(op_real[:,0],op_real[:,1],label="Real")
108
    axarr[0].set_title('Real Power')
109
    axarr[0].set_ylabel(r'$\Delta$P')
110
    axarr[0].set_xlabel("Time (s)")
111
    axarr[0].legend(loc="upper right")
112
    
113
    axarr[1].plot(op[:,0],op[:,2],'-.',label="Model",lw=5)
114
    axarr[1].plot(op_real[:,0],op_real[:,2],label="Real")
115
    axarr[1].set_title('Reactive Power')
116
    axarr[1].set_ylabel(r'$\Delta$Q')
117
    axarr[1].set_xlabel("Time (s)")
118
    axarr[1].legend(loc="upper right")
119

    
120
    f.show()
121
    plt.pause(0.0001)
122

    
123

    
124
"""
125
 The following 'while' loop builds matrices containing the sensibilities 
126
 (derivatives) of each state variable for each parameter in order to 
127
 calculate both the Gamma Function and the Error sensisibility. Later, it
128
 refreshes the paramenters values and recalculates the error.
129
"""
130

    
131
while Jp > tolerance and count < 50:
132

    
133
#If analysis at each iteration is needed, this will pause at each run
134
    if step_by_step:
135
        raw_input("Press Enter to continue.")
136

    
137
    if count == 10:
138
        p_ativo = ones_like(p)
139
    
140
    """
141
    This 'for' loop builds matrices containing the sensibilities for each
142
    parameter. The 3D Matrix 'dopdp' contains all of them, e.g.,
143
    dopdp(:,:,1) contains the Matrix of Sensibility for parameter p(1), in
144
    this case, 'b'.
145
    """
146
    for i in range(0,num_param):
147
        op_p = rk4(init_time,final_time,step,x_o,u0,p,delta_p,u,i)
148
    
149
        if i == 0:
150
            dopdp = (op_p - op)/delta_p[i]
151
        else:
152
            dopdp = dstack((dopdp,((op_p - op)/delta_p[i])))
153

    
154

    
155
    #Gamma Function and dJ/dp are calculated by the following function
156
    (gamma, dJdp) = Gamma(dopdp,op,op_real)
157

    
158

    
159
    #Parameters area classified due to its conditioning
160
    if count == 0:
161
        Classification(gamma)
162

    
163

    
164
    #Parameters are modified (added DP) and stored
165
    DP = -linalg.solve(gamma, dJdp)
166
    p += p_ativo.reshape(num_param,) * DP.reshape(num_param,)
167
    evolution = vstack((evolution,p))
168

    
169
    print p
170

    
171

    
172
    #System output is reevaluated, now with the modified parameters
173
    op = rk4(init_time,final_time,step,x_o,u0,p,delta_p,u,-1)
174

    
175

    
176
    #Error is recalculated and stored
177
    Jp = .5*step*Error(op_real[:,1:],op[:,1:])
178
    err = hstack((err, Jp))
179

    
180

    
181
    #Number of iterations is increased
182
    count += 1
183

    
184

    
185
    #Plot of outputs at each iteration
186
    if print_figs:
187
        axarr[0].cla()
188
        axarr[0].plot(op[:,0],op[:,1],'-.',label="Model",lw=5)
189
        axarr[0].plot(op_real[:,0],op_real[:,1],label="Real")        
190
        axarr[0].set_title('Real Power')
191
        axarr[0].set_ylabel(r'$\Delta$P')
192
        axarr[0].set_xlabel("Time (s)")
193
        axarr[0].legend(loc="upper right")
194
    
195
    
196
        axarr[1].cla()
197
        axarr[1].plot(op[:,0],op[:,2],'-.',label="Model",lw=5)
198
        axarr[1].plot(op_real[:,0],op_real[:,2],label="Real")        
199
        axarr[1].set_title('Reactive Power')
200
        axarr[1].set_ylabel(r'$\Delta$Q')
201
        axarr[1].set_xlabel("Time (s)")
202
        axarr[1].legend(loc="upper right")
203
    
204
        plt.draw()
205
        plt.pause(0.0001)
206

    
207
print "\n\nNumber of iterations: %d\n\n" %count
208
print p
209
print p_real
210

    
211
if print_figs:
212
    plt.figure()
213
    plt.plot(pold[:,0],pold[:,1],'.',label="Initial",lw=2.5)
214
    plt.plot(op[:,0],op[:,1],'-.',label="Final", lw=5)
215
    plt.plot(op_real[:,0],op_real[:,1],label="Real")
216
    plt.title('Real Power')
217
    plt.ylabel(r'$\Delta$P')
218
    plt.xlabel("Time (s)")
219
    plt.legend()
220

    
221
print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))