/
maquina.py
219 lines (161 loc) · 6.21 KB
/
maquina.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 14 11:30:31 2013
@author: urielsandoval
"""
from copy import copy
import numpy as np
from cmath import cos, sin, pi, sqrt
from matplotlib import pyplot as plt
from cvxopt import matrix
from rk4 import rk4
from datos import cargar_datos
Volt = []
T1 = 2./3 * np.array([[1, -0.5, -0.5],
[0, sqrt(3)/2, -sqrt(3)/2]])
defase = 2*pi/3
raiz1_2 = 1/sqrt(2).real
raiz2_3 = sqrt(2./3).real
class Maquina(object):
def __init__(self, datos):
# self.eventos = []
# self.nombres = {'V': ('V_d', 'V_q', 'V_0', 'V_f'),
# 'I': ('I_d', 'I_q', 'I_0', 'I_f', 'I_s', 'I_t')}
# self.Vm = np.ones(6)
# self.V = np.array([0, 0, 0, datos['Vfd'], 0, 0])
# self.ws = 2*np.pi*datos['frec']
# # Se cargan datos y se crean matrices
# r = datos['r']
# rf = datos['rf']
# rs = datos['rs']
# rt = datos['rt']
# ld = datos['ld']
# lq = datos['lq']
# l0 = datos['l0']
# ll = datos['ll']
# llfd = datos['llfd']
# llkd = datos['llkd']
# llkq = datos['llkq']
# lmd = ld - ll
# lmq = lq - ll
# lf = llfd + lmd
# ls = llkd + lmd
# lt = llkq + lmq
# mdf = mds = lmd
# mqt = lmq
# self.R = np.diag([-r, -r, -r, rf, rs, rt])
# self.L = np.array([[-ld, 0, 0, mdf, mds, 0],
# [0, -lq, 0, 0, 0, mqt],
# [0, 0, -l0, 0, 0, 0],
# [-mdf, 0, 0, lf, lmd, 0],
# [-mds, 0, 0, lmd, ls, 0],
# [0, -mqt, 0, 0, 0, lt]])
# self.Linv = inv(self.L)
# self.H = datos['H']
# self.pp = datos['pp']
# self.tm = datos['tm']
self.eventos = []
self.ws = 2*np.pi*datos['frec']
self.VA = 1
self.VB = 1
self.VC = 1
def abc2ab(self, Vabc):
return np.dot(T1, Vabc)
def ab2dq(self, beta, Vab):
T = np.array([[cos(beta), sin(beta)],
[-sin(beta), cos(beta)]])
return np.dot(T, Vab)
def transformada_park(self, theta, Vabc):
C = raiz2_3 * matrix([[cos(theta).real, cos(theta - defase).real, cos(theta + defase).real],\
[-sin(theta).real, -sin(theta - defase).real, -sin(theta + defase).real],\
[raiz1_2, raiz1_2, raiz1_2]])
return C.T * Vabc
def aplicar_evento(self, aplicar,evento):
if aplicar:
print 'Se apica evento'
if evento['tipo'] == 'falla_3f':
self.VA = 0
self.VB = 0
self.VC = 0
elif evento['tipo'] == 'mod_par':
self.tm = evento['valor']
else:
print 'Se libera evento'
if evento['tipo'] == 'falla_3f':
self.VA = 1
self.VB = 1
self.VC = 1
elif evento['tipo'] == 'mod_par':
pass
def crea_tiempo_eventos(self, t):
t_eventos = [[] for _ in range(t.size)]
for evento in self.eventos:
ind = (np.abs(t - evento['ti'])).argmin()
ind2 = (np.abs(t - evento['tf'])).argmin()
t_eventos[ind].append((True,evento))
t_eventos[ind2].append((False, evento))
return t_eventos
def dinamico(self, tf, **kwargs):
h = kwargs.get('h', 0.001)
N = int((tf) / h)
self.t = np.linspace(0, tf, N)
ceros = matrix(0.0, (N,1))
ceros_n = matrix(0.0, (N, 8))
ceros_n_2 = matrix(0.0, (N, 6))
t_eventos = self.crea_tiempo_eventos(self.t)
self.X = copy(ceros_n)
self.data = {'V': copy(ceros_n_2),
'I': copy(ceros_n_2),
'Te': copy(ceros),
'Tm': copy(ceros),
'w': copy(ceros),
'd': copy(ceros)}
self.dx = matrix(0.0, (1,8))
self.X[0, :] = self.x0
self.data['w'][0] = self.x0[6] #* 120 / (2*np.pi*self.pp)
self.data['d'][0] = self.x0[7] #* 180 / np.pi
for p in xrange(1, N):
#print 'Iteracion %d'%p
self.p = p
if t_eventos[p-1]:
for evento in t_eventos[p-1]:
print evento
self.aplicar_evento(evento[0], evento[1])
self.X[p, :] = rk4(self.ecuaciones, self.X[p-1, :], self.t[p], h)
# Se almacenan variables
self.data['V'][p, :] = self.V.T
self.data['w'][p] = self.X[p, 6]
self.data['d'][p] = self.X[p, 7]
self.data['I'][p, :] = self.I.T
self.data['Te'][p] = self.te
self.data['Tm'][p] = self.tm
#self.data['w'] *= 120 / (2*np.pi*self.pp)
#self.data['d'] *= 180 / np.pi
#print self.X[-1,:].tolist()
def graficar_todas(self):
plt.close('all')
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col')
ax1.plot(self.t, self.data['w'], linewidth=1, label='$\omega$')
ax1.legend(loc=0)
ax2.plot(self.t, self.data['d'], linewidth=1, label='$\delta$')
ax2.legend(loc=0)
[ax3.plot(self.t, self.data['V'][:, p], linewidth=1, label='$'+nombre+'$') for p,nombre in zip(range(4),self.nombres['V'])]
ax3.legend(loc=0)
[ax4.plot(self.t, self.data['I'][:, p], linewidth=1, label='$'+nombre+'$') for p,nombre in zip(range(6), self.nombres['I'])]
ax4.legend(loc=0)
f1 = plt.figure()
ax3 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
ax2 = plt.subplot2grid((2, 2), (0, 0))
ax1 = plt.subplot2grid((2, 2), (0, 1))
ax1.plot(self.data['d'], self.data['Te'], linewidth=1)
ax1.set_xlabel('$\delta$')
ax1.set_ylabel('$T_e$')
ax2.plot(self.data['w'], self.data['d'], linewidth=1)
ax2.set_xlabel('$\omega_r$')
ax2.set_ylabel('$\delta$')
ax3.plot(self.t, self.data['Te'], linewidth=1, label='$T_e$')
ax3.plot(self.t, self.data['Tm'], linewidth=1, label='$T_m$')
ax3.set_xlabel('$t$')
ax3.set_ylabel('Par')
ax3.legend(loc=1)
plt.show()