def gcall(self): if self.n == 0: return system.DAE.y = matrix(system.DAE.y) V = system.DAE.y[self.v] V2 = mul(V, V) p = mul(matrix(self.g), V2) q = mul(matrix(self.b), V2) # #V = matrix(0, (system.Shunt.n, 1)) # V = [1.0] * system.Shunt.n # print(V) # for i in range(system.Shunt.n): # V[i] = system.DAE.y[self.v[i]] # print(V) # V = np.array(V) # V2 = V * V # print(V2) # J = [1] * system.Shunt.n # print(self.a) # print(J) # p = [0] * system.Shunt.n # q = [0] * system.Shunt.n # for i in range(system.Shunt.n): # p[i] = self.g[i] * V2[i] # q[i] = self.b[i] * V2[i] for key, value in zip(self.a, p): system.DAE.g[key] += value for key, value in zip(self.v, q): system.DAE.g[key] -= value
def Gycall(self): if self.n == 0: return T1 = self.T1 T2 = self.T2 T3 = self.T3 T4 = self.T4 Kp = self.Kp Kv = self.Kv vsmax = self.vsmax vsmin = self.vsmin vss = system.DAE.y[self.vss] z = [0] * self.n z = matrix(z) for i in range(self.n): if vss[i] < vsmax[i] and vss[i] > vsmin[i]: if self.u[i]: z[i] = 1 system.DAE.Gy = system.DAE.Gy - spmatrix(1, self.vss, self.vss, (system.DAE.ny, system.DAE.ny))\ + spmatrix(z, self.vref, self.vss, (system.DAE.ny, system.DAE.ny)) A = div(T1, T2) C = div(T3, T4) E = mul(C, A) system.DAE.Gy = system.DAE.Gy + spmatrix(mul(z, mul(Kv, E)), self.vss, self.v, (system.DAE.ny, system.DAE.ny)) \ + spmatrix(mul(z, mul(Kp, E)), self.vss, self.p, (system.DAE.ny, system.DAE.ny))
def setx0(self): if self.n == 0: return VSI = [0] * self.n SIw = [0] * self.n SIp = [0] * self.n SIv = [0] * self.n for i in range(self.n): if self.ni[i] == 1: SIw[i] = i VSI[i] = system.DAE.x[self.omega[SIw[i]]] elif self.ni[i] == 2: SIp[i] = i VSI[i] = system.DAE.y[self.p[SIp[i]]] self.Kp[i] = self.Kw[i] self.Kw[i] = 0.0 self.Kv[i] = 0.0 elif self.ni[i] == 3: SIv[i] = i VSI[i] = system.DAE.y[self.v[SIv[i]]] self.Kv[i] = self.Kw[i] self.Kw[i] = 0.0 self.Kp[i] = 0.0 VSI = matrix(VSI) # VSI = system.DAE.x[self.omega[SIw]] Kw = mul(self.u, self.Kw) Kp = mul(self.u, self.Kp) Kv = mul(self.u, self.Kv) Tw = self.Tw T2 = self.T2 T4 = self.T4 Ta = self.Ta for i in range(self.n): if Tw[i] == 0.0: Tw[i] = 0.01 print( ' Tw cannot be zero. Default value Tw = 0.01 will be used') if T2[i] == 0.0: T2[i] = 0.01 print( ' T2 cannot be zero. Default value Tw = 0.01 will be used') if T4[i] == 0.0: T4[i] = 0.01 print( ' T2 cannot be zero. Default value Tw = 0.01 will be used') system.DAE.x[self.v1] = mul(-(Kw + Kp + Kv), VSI) system.DAE.x[self.v2] = 0 system.DAE.x[self.v3] = 0 for i in range(self.n): if self.s2[i] == 0.0: self.s2[i] = -1.0 system.DAE.y[self.vss] = 0.0
def fcall(self): if self.n == 0: return tg1 = system.DAE.x[self.tg1] tg2 = system.DAE.x[self.tg2] tg3 = system.DAE.x[self.tg3] wref = system.DAE.y[self.wref] gain = div(matrix(1.0, (self.n, 1)), self.R) a_s = div(matrix(1.0, (self.n, 1)), self.Ts) ac = div(matrix(1.0, (self.n, 1)), self.Tc) a5 = div(matrix(1.0, (self.n, 1)), self.T5) K1 = mul(self.T3, ac) K2 = 1 - K1 K3 = mul(self.T4, a5) K4 = 1 - K3 system.Syn6.omega = matrix(system.Syn6.omega) omega = system.Syn6.omega[self.a] tin = self.Porder + mul(gain, wref - system.DAE.x[omega]) for i in range(self.n): tin[i] = max(tin[i], self.Pmin[i]) tin[i] = min(tin[i], self.Pmax[i]) system.DAE.f[self.tg1] = mul(self.u, a_s, -tg1+tin) system.DAE.f[self.tg2] = mul(self.u, ac, -tg2 + mul(K2, tg1)) system.DAE.f[self.tg3] = mul(self.u, a5, -tg2 + mul(K4, tg2+mul(K1, tg1)))
def Gycall(self): if self.n == 0: return V = mul(2 * self.u, system.DAE.y[self.v]) system.DAE.Gy = system.DAE.Gy + spmatrix( mul(self.dat[0], V), self.a, self.v, (system.DAE.ny, system.DAE.ny)) - spmatrix( mul(self.dat[1], V), self.v, self.v, (system.DAE.ny, system.DAE.ny))
def Fxcall(self): if self.n == 0: return wref = system.DAE.y[self.wref] gain = div(matrix(1.0, (self.n, 1)), self.R) a_s = div(matrix(1.0, (self.n, 1)), self.Ts) ac = div(matrix(1.0, (self.n, 1)), self.Tc) a5 = div(matrix(1.0, (self.n, 1)), self.T5) K1 = mul(self.T3, ac) K2 = 1 - K1 K3 = mul(self.T4, a5) K4 = 1 - K3 system.Syn6.omega = matrix(system.Syn6.omega) omega = system.Syn6.omega[self.a] tin = self.Porder + mul(gain, wref - system.DAE.x[omega]) for i in range(self.n): tin[i] = max(tin[i], self.Pmin[i]) tin[i] = min(tin[i], self.Pmax[i]) u = matrix(1, (self.n, 1)) # windup limit for i in range(self.n): if tin[i] < self.Pmax[i] and tin[i] > self.Pmin[i]: u[i] = 1 else: u[i] = 0 system.DAE.Fx = system.DAE.Fx \ - spmatrix(mul(u, self.u, a_s, gain), self.tg1, omega, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(a_s, self.tg1, self.tg1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(ac, self.tg2, self.tg2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(a5, self.tg3, self.tg3, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(mul(ac, self.u, K2), self.tg2, self.tg1, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(mul(a5, self.u, K4), self.tg3, self.tg2, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(mul(a5, self.u, K1, K4), self.tg3, self.tg2, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy \ + spmatrix(mul(u, self.u, a_s, gain), self.tg1, self.wref, (system.DAE.nx, system.DAE.ny)) system.DAE.Gx = system.DAE.Gx \ + spmatrix(u, self.pm, self.tg3, (system.DAE.ny, system.DAE.nx)) \ + spmatrix(mul(u, K3), self.pm, self.tg2, (system.DAE.ny, system.DAE.nx)) \ + spmatrix(mul(u, K1, K3), self.pm, self.tg1, (system.DAE.ny, system.DAE.nx))
def gcall(self): if self.n == 0: return V = system.DAE.y[self.v] V2 = mul(self.u, V, V) V2 = matrix(V2) system.DAE.g = system.DAE.g + spmatrix( mul(self.dat[0], V2), self.a, [0], (system.DAE.ny, 1)) - spmatrix(mul(self.dat[1], V2), self.v, [0], (system.DAE.ny, 1))
def base(self, Sb = 100.0, Vb = None): for var in self._voltages: self.__dict__[var] = mul(self.__dict__[var], self.Vn) self.__dict__[var] = div(self.__dict__[var], Vb) for var in self._powers: self.__dict__[var] = mul(self.__dict__[var], self.Sn) self.__dict__[var] = div(self.__dict__[var], Sb) for var in self._currents: self.__dict__[var] = mul(self.__dict__[var], self.Sn) self.__dict__[var] = div(self.__dict__[var], self.Vn) self.__dict__[var] = mul(self.__dict__[var], Vb) self.__dict__[var] /= Sb if len(self._z) or len(self._y): Zn = div(self.Vn**2, self.Sn) #print(Zn) Zb = div(mul(Vb, Vb), Sb) #print(Zb) for var in self._z: self.__dict__[var] = mul(self.__dict__[var], Zn) self.__dict__[var] = div(self.__dict__[var], Zb) for var in self._y: if self.__dict__[var].typecode == 'd': self.__dict__[var] = div(self.__dict__[var], Zn) self.__dict__[var] = mul(self.__dict__[var], Zb) #print(self.__dict__[var]) if self.__dict__[var].typecode == 'z': self.__dict__[var] = div(self.__dict__[var], Zn + 0j) self.__dict__[var] = mul(self.__dict__[var], Zb + 0j)
def Gycall(self): if self.n == 0: return V = 2 * system.DAE.y[self.v] g = matrix(self.g) b = matrix(self.b) m = system.DAE.ny system.DAE.Gy = system.DAE.Gy + spmatrix(mul(V, g), self.a, self.v, (m, m)) - spmatrix( mul(V, b), self.v, self.v, (m, m))
def setx0(self): vg = system.DAE.y[self.bv] vf = mul(self.u, system.Syn6.vf0[self.a]) vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) # 检查参数 for i in range(self.n): if self.Te[i] == 0: self.Te[i] = 1 print('<%i> Te不能小于等于0,设置Te = 1 [s]' % i) if self.Tr[i] <= 0: self.Tr[i] = 0.001 print('<%i> Tr不能小于等于0,设置Te = 0.001 [s]' % i) if self.Ke[i] <= 0: self.Ke[i] = 1 print('<%i> Ke不能小于等于0,设置Ke = 1 ' % i) if self.Tf[i] <= 0: self.Tf[i] = 0.1 print('<%i> T1不能小于等于0,设置T1 = 0.1 [s]' % i) if self.Ta[i] <= 0: self.Ta[i] = 0.1 print('<%i> T4不能小于等于0,设置T4 = 0.1 [s]' % i) # Ce = mul(self.Ke, vf) + mul(mul(self.Ae, exp(mul(self.Be, abs(vf)))), vf) system.DAE.x[self.vm] = mul(self.u, vg) self.vref0 = div(Ce, self.Ka) + vg system.DAE.x[self.vr1] = Ce system.DAE.x[self.vr2] = div(mul(-self.Kf, vf), self.Tf) system.DAE.x[self.vf] = vf system.DAE.y[self.vref] = mul(self.u, self.vref0) for i in range(self.n): if Ce[i] > self.vrmax[i]: print('Warn: vr1超出最大值vrmax') if Ce[i] < self.vrmin[i]: print('Warn: vr1小于最小值vrmin') for i in range(self.n): if self.u[i] == 1: system.Syn6.vf0[self.a[i]] = 0
def __init__(self): base_device.__init__(self) self.u = mul(matrix(self.u), matrix(system.PV.u)) self._data.update({ 'bus': None, 'Type': 1, 'vrmax': 3.3, 'vrmin': -2.6, 'K0': 0, 'T1': 0, 'T2': 0, 'T3': 0, 'T4': 0, 'Te': 0, 'Tr': 0, 'Ae': 0, 'Be': 0 }) self._type = 'Avr1' self._name = 'Avr1' self._bus = {'bus': ['a', 'v']} self._algebs = ['vref'] # 后续得修改 self._states = ['vm', 'vr1', 'vr2', 'vf'] # 后续得修改 self._params.extend([ 'u', 'vrmax', 'vrmin', 'K0', 'T1', 'T2', 'T3', 'T4', 'Te', 'Tr', 'Ae', 'Be' ]) self._voltages = ['V0'] # 后续得修改 self._powers = ['Pg'] # 后续得修改 self.ba = [] self.bv = []
def Gycall(self): system.DAE.y = matrix(system.DAE.y) U = exp(system.DAE.y[system.Bus.a] * 1j) V = mul(system.DAE.y[system.Bus.v] + 0j, U) I = self.Y * V nb = len(system.Bus.a) diagU = spmatrix(U, system.Bus.a, system.Bus.a, (nb, nb), 'z') diagV = spmatrix(V, system.Bus.a, system.Bus.a, (nb, nb), 'z') diagI = spmatrix(I, system.Bus.a, system.Bus.a, (nb, nb), 'z') dS = self.Y * diagU dS = diagV * dS.H.T dS += diagI.H.T * diagU dR = diagI dR = dR - self.Y * diagV dR = diagV.H.T * dR # system.DAE.Gy = matrix(0.0, (system.DAE.ny, system.DAE.ny)) # # system.DAE._list2matrix() # system.DAE.Gy = spmatrix(dR.imag().V, dR.imag().I, dR.imag().J, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dR.real().V, dR.real().I, dR.real().J+system.Bus.n, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dS.real().V, dS.real().I+system.Bus.n, dS.real().J, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dS.imag().V, dS.imag().I+system.Bus.n, dS.imag().J+system.Bus.n, (system.DAE.ny, system.DAE.ny)) Gy = sparse([[dR.imag(), dR.real()], [dS.real(), dS.imag()]]) # system.DAE.Gy = zeros([system.DAE.ny,system.DAE.ny]) system.DAE.Gy = spmatrix(Gy.V, Gy.I, Gy.J, (system.DAE.ny, system.DAE.ny)) system.DAE.Gy = matrix(system.DAE.Gy)
def gcall(self): self.vfd = system.Syn6.vf[self.a] system.DAE.g = system.DAE.g + spmatrix(system.DAE.x[self.vf], self.vfd, [0]*self.n, (system.DAE.ny, 1)) \ + spmatrix(mul(self.u, self.vref0)-system.DAE.y[self.vref], self.vref, [0]*self.n, (system.DAE.ny, 1))
def gcall(self): system.DAE.y = matrix(system.DAE.y) zeros = [0.0] * system.DAE.ny system.DAE.g = zeros system.DAE.g = matrix(system.DAE.g) Vn = exp(system.DAE.y[system.Bus.a] * 1j) Vc = mul(system.DAE.y[system.Bus.v] + 0j, Vn) Ic = self.Y * Vc S = mul(Vc, Ic.H.T) self.p = S.real() self.q = S.imag() for i in range(system.Bus.n): system.DAE.g[i] = self.p[i] system.DAE.g[i+system.Bus.n] = self.q[i]
def synsat(self): # print(type([0.8] * self.n)) # print(matrix(0.8, (self.n, 1))) b = matrix([[matrix(0.8, (self.n, 1))], list([1.0 - self.S10]), list([1.2 * (1.0 - self.S12)])]) # print(matrix([12.5, -25, 12.5])) c2 = b * matrix([12.5, -25, 12.5]) c1 = b * matrix([-27.5, 50, -22.5]) c0 = b * matrix([15, -24, 10]) output = system.DAE.x[self.e1q] for i in range(self.n): if system.DAE.x[self.e1q[i]] > 0.8: output[i] = mul(c2[i], system.DAE.x[self.e1q[i]]**2) + mul( c1[i], system.DAE.x[self.e1q[i]]) + c0[i] return output
def G(u, v, alpha=1.0, beta=0.0, trans='N'): """ If trans is 'N': v := alpha * [ -diag(d), -d, -I; 0, 0, -I ] * u + beta * v. If trans is 'T': v := alpha * [ -diag(d), 0; -d', 0; -I, -I ] * u + beta * v. """ v *= beta if trans is 'N': v[:N] -= alpha * (base.mul(d, u[:N] + u[N]) + u[-N:]) v[-N:] -= alpha * u[-N:] else: v[:N] -= alpha * base.mul(d, u[:N]) v[N] -= alpha * blas.dot(d, u, n=N) v[-N:] -= alpha * (u[:N] + u[N:])
def Gycall(self): system.DAE.y = matrix(system.DAE.y) V = 2 * system.DAE.y[self.v] #shunt bus 的索引? conductance = self.g #网络中每条母线的电导列向量 conductance = matrix(conductance) susceptance = self.b #电纳列向量 susceptance = matrix(susceptance) m = len(system.DAE.y) #代数变量的个数 conducv = mul(conductance, V) suscepv = mul(susceptance, V) spconducv = spmatrix(conducv, self.a, self.v, (m, m), 'z') spcsuscepv = spmatrix(suscepv, self.v, self.v, (m, m), 'z') system.DAE.Gy = system.DAE.Gy + spmatrix(conducv, self.a, self.v, (m, m), 'z') - spmatrix( suscepv, self.v, self.v, (m, m), 'z') system.DAE.Gy = sparse(system.DAE.Gy) system.DAE.Gy = matrix(system.DAE.Gy.real())
def setdata(self): Wn = 2 * 3.1415926 * system.Settings.freq # 转矩系数:A+B*slip+C*slip^2 self.A = self.a1 + self.b + self.c self.B = -self.b - 2 * self.c self.C = self.c # 1/2*Hm self.i2Hm = div(1, 2 * self.Hm) # x0,x',x'' self.x0 = self.xs + self.xm self.x1 = self.xs + div(mul(self.xr1, self.xm), self.xr1 + self.xm) self.x2 = self.xs + div( mul(self.xr1, self.xm, self.xr2), mul(self.xr1, self.xm) + mul(self.xr2, self.xm) + mul(self.xr1, self.xr2)) # T'0,T''0 for i in range(self.n): if self.rr1[i] == 0: self.rr1[i] = 1 if self.rr2[i] == 0: self.rr2[i] = 1 self.T10 = div(self.xr1 + self.xm, mul(self.rr1, Wn)) self.T20 = div( self.xr2 + div(mul(self.xr1, self.xm), self.xr1 + self.xm), mul(self.rr2, Wn)) # 1/xm, x's = xs+xr1 for i in range(self.n): if self.xm[i] == 0: print('第%i 台感应电机的磁抗为0' % i) self.xm[i] = 1 if self.tup[i] < 0: self.tup = 0 self.ixm = div(1, self.xm) self.x1s = self.xs + self.xr1 if self.sup == 0: self.tup = 0
def compute_eigs(As): (mu, N) = eig(matrix(As)) N = matrix(N) n = len(mu) idx = range(n) W = matrix(spmatrix(1.0, idx, idx, (n, n), v.typecode)) gesv(N, W) pf = mul(abs(W.T), abs(V)) b = matrix(1.0, (1, n)) WN = b * pf pf = pf.T for item in idx: mur = mu[item].real mui = mu[item].imag mu[item] = complex(round(mur, 5), round(mui, 5)) pf[item, :] /= WN[item]
def g(x, y, z): # Solve # # [ K d3 ] [ ux_y ] # [ ] [ ] = # [ d3' 1'*d3 ] [ ux_b ] # # [ bx_y ] [ D ] # [ ] - [ ] * D3 * (D2 * bx_v + bx_z - bx_w). # [ bx_b ] [ d' ] x[:N] -= mul(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:])) x[N] -= blas.dot(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:])) # Solve dy1 := K^-1 * x[:N] blas.copy(x, dy1, n=N) chompack.trsm(L, dy1, trans='N') chompack.trsm(L, dy1, trans='T') # Find ux_y = dy1 - ux_b * dy2 s.t # # d3' * ( dy1 - ux_b * dy2 + ux_b ) = x[N] # # i.e. x[N] := ( x[N] - d3'* dy1 ) / ( d3'* ( 1 - dy2 ) ). x[N] = ( x[N] - blas.dot(d3, dy1) ) / \ ( blas.asum(d3) - blas.dot(d3, dy2) ) x[:N] = dy1 - x[N] * dy2 # ux_v = D4 * ( bx_v - D1^-1 (bz_z + D * (ux_y + ux_b)) # - D2^-1 * bz_w ) x[-N:] = mul( d4, x[-N:] - div(z[:N] + mul(d, x[:N] + x[N]), d1) - div(z[N:], d2)) # uz_z = - D1^-1 * ( bx_z - D * ( ux_y + ux_b ) - ux_v ) # uz_w = - D2^-1 * ( bx_w - uz_w ) z[:N] += base.mul(d, x[:N] + x[N]) + x[-N:] z[-N:] += x[-N:] blas.scal(-1.0, z) # Return W['di'] * uz blas.tbmv(W['di'], z, n=2 * N, k=0, ldA=1)
def gcall(self): if self.n == 0: return a_s = div(matrix(1.0, (self.n, 1)), self.Ts) ac = div(matrix(1.0, (self.n, 1)), self.Tc) a5 = div(matrix(1.0, (self.n, 1)), self.T5) K1 = mul(self.T3, ac) K2 = 1 - K1 K3 = mul(self.T4, a5) K4 = 1 - K3 pmech = system.DAE.x[self.tg3] + mul(K3, system.DAE.x[self.tg2]) + mul(K1, system.DAE.x[self.tg1]) system.DAE.g = system.DAE.g \ + spmatrix(mul(self.u, pmech), self.pm, matrix(0, (self.n, 1)), (system.DAE.ny, 1)) \ + spmatrix(mul(self.u, self.wref0) - system.DAE.y[self.wref], self.wref, matrix(0, (self.n, 1)), (system.DAE.ny, 1))
def __init__(self): base_device.__init__(self) self.u = mul(matrix(self.u), matrix(system.PV.u)) self._data.update({ 'bus': None, 'Type': 2, 'vrmax': 5, 'vrmin': 0, 'Ka': 0, 'Ta': 0, 'Kf': 0, 'Tf': 0, 'Ke': 0, 'Te': 0, 'Tr': 0, 'Ae': 0, 'Be': 0 }) self._type = 'Avr2' self._name = 'Avr2' self.n = 0 self._bus = {'bus': ['a', 'v']} self._algebs = ['vref'] # 后续得修改 self._states = ['vm', 'vr1', 'vr2', 'vf'] # 后续得修改 self._params.extend([ 'u', 'vrmax', 'vrmin', 'Ka', 'Ta', 'Kf', 'Tf', 'Ke', 'Te', 'Tr', 'Ae', 'Be' ]) self._voltages = ['V0'] # 后续得修改 self._powers = ['Pg'] # 后续得修改 self.ba = [] self.bv = [] self.properties.update({ 'gcall': True, 'Gycall': True, 'fcall': True, 'Fxcall': True })
def gcall(self): zeros = [0] * system.Bus.n Vn = zeros[:] Vc = zeros[:] for item1, item2 in zip(system.Bus.a, system.Bus.v): Vn[item1] = exp(system.DAE.y[item1] * 1j) Vc[item1] = (system.DAE.y[item2] + 0j) * Vn[item1] # system.DAE.y = matrix(system.DAE.y) # Vn = exp(system.DAE.y[system.Bus.a] * 1j) # Vc = mul(system.DAE.y[system.Bus.v] + 0j, Vn) Vc = matrix(Vc) Ic = self.Y * Vc S = mul(Vc, Ic.H.T) self.p = S.real() self.q = S.imag() for i in range(system.Bus.n): system.DAE.g[i] = self.p[i] system.DAE.g[i + system.Bus.n] = self.q[i]
def compute_eig(As): mu, N = eig(matrix(As)) N = matrix(N) n = len(mu) idx = range(n) W = matrix(spmatrix(1.0, idx, idx, (n, n), N.typecode)) gesv(N, W) # W = np.linalg.pinv(N) # W = matrix(W) pf = mul(abs(W.T), abs(N)) b = matrix(1.0, (1, n)) WN = b * pf pf = pf.T for item in idx: mur = mu[item].real mui = mu[item].imag mu[item] = complex(round(mur, 5), round(mui, 5)) pf[item, :] /= WN[item] # print(pf) return pf
def setx0(self): if self.n == 0: return system.Syn6._list2matrix() self.Porder = system.Syn6.pm0[self.a] # 需要改进,若不是Syn6 gain = div(matrix(1.0, (self.n, 1)), self.R) a_s = div(matrix(1.0, (self.n, 1)), self.Ts) ac = div(matrix(1.0, (self.n, 1)), self.Tc) a5 = div(matrix(1.0, (self.n, 1)), self.T5) K1 = mul(self.T3, ac) K2 = 1 - K1 K3 = mul(self.T4, a5) K4 = 1 - K3 system.DAE.x[self.tg1] = mul(self.u, self.Porder) system.DAE.x[self.tg2] = mul(self.u, K2, self.Porder) system.DAE.x[self.tg3] = mul(self.u, K4, self.Porder) system.DAE.f[self.tg1] = 0 system.DAE.f[self.tg2] = 0 system.DAE.f[self.tg3] = 0 system.Syn6.pm0[self.a] = 0 system.DAE.y[self.wref] = mul(self.u, self.wref0) for i in range(self.n): if self.Porder[i] > self.Pmax[i]: print('第%i 台Tg1机械功率超过最大值'% i) elif self.Porder[i] < self.Pmin[i]: print('第%i 台Tg1机械功率低于最小值' % i) else: print('初始化Tg1完成') self.pmech = system.DAE.x[self.tg3] + mul(K3, system.DAE.x[self.tg2]) + mul(K1, system.DAE.x[self.tg1])
def Fxcall(self): if self.n == 0: return vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) system.DAE.Gx = system.DAE.Gx + spmatrix( self.u, self.vfd, self.vf, (system.DAE.ny, system.DAE.nx)) system.DAE.Fx = system.DAE.Fx - spmatrix( div(matrix(1.0, (self.n, 1)), self.Tr), self.vm, self.vm, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy + spmatrix(div( self.u, self.Tr), self.vm, self.bv, (system.DAE.nx, system.DAE.ny)) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] K1 = div(mul(self.K0, self.T2), self.T1) K2 = self.K0 - K1 K3 = div(self.T4, self.T3) K4 = matrix(1, (self.n, 1)) - K3 vr = mul(self.K0, vr2) + mul(K3, mul(K1, vref - vm) + vr1) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) Se = Se + mul(mul(Se, self.Ae), vf) z = matrix(0, (self.n, 1)) for i in range(self.n): if vr[i] < vrmax[i] and vr[i] > vrmin[i]: z[i] = 1 system.DAE.Fx = system.DAE.Fx \ - spmatrix(div(K2, self.T1), self.vr1, self.vm, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.T1), self.vr1, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(K4, K1), mul(self.K0, self.T3)), self.vr2, self.vm, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(K4, mul(self.T3, self.K0)), self.vr2, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.T3), self.vr2, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(1+Se, self.Te), self.vf, self.vf, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(mul(z, K3), self.Te), self.vf, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(mul(z, K3), K1), self.Te), self.vf, self.vm, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(mul(mul(z, self.u), self.K0), self.Te), self.vf, self.vr2, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy \ + spmatrix(div(K2, self.T1), self.vr1, self.vref, (system.DAE.nx, system.DAE.ny)) \ + spmatrix(div(mul(K4, K1), mul(self.K0, self.T3)), self.vr2, self.vref, (system.DAE.nx, system.DAE.ny)) \ + spmatrix(div(mul(mul(z, K3), K1), self.Te), self.vf, self.vref, (system.DAE.nx, system.DAE.ny))
def fcall(self): if self.n == 0: return vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] system.DAE.f[self.vm] = div(mul(self.u, vg - system.DAE.x[self.vm]), self.Tr) K1 = div(mul(self.K0, self.T2), self.T1) K2 = self.K0 - K1 K3 = div(self.T4, self.T3) K4 = matrix(1, (self.n, 1)) - K3 vr = mul(self.K0, vr2) + mul(K3, mul(K1, vref - vm) + vr1) system.DAE.f[self.vr1] = div(mul(self.u, mul(K2, vref - vm) - vr1), self.T1) system.DAE.f[self.vr2] = div( mul(self.u, mul(K4, vr1 + mul(K1, vref - vm)) - mul(self.K0, vr2)), mul(self.T3, self.K0)) # hard limit for i in range(self.n): vr[i] = min(vr[i], vrmax[i]) vr[i] = max(vr[i], vrmin[i]) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) system.DAE.f[self.vf] = div(mul(self.u, -vf + vr - mul(Se, vf)), self.Te)
def fcall(self, x): fvec = mul(self.A, sin(self.omega*x + self.phi)) return sum(fvec)
def Fxcall(self): vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) system.DAE.Gx = system.DAE.Gx + spmatrix( self.u, self.vfd, self.vf, (system.DAE.ny, system.DAE.nx)) system.DAE.Fx = system.DAE.Fx - spmatrix( div(matrix(1.0, (self.n, 1)), self.Tr), self.vm, self.vm, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy + spmatrix(div( self.u, self.Tr), self.vm, self.bv, (system.DAE.nx, system.DAE.ny)) vr1 = system.DAE.x[self.vr1] vf = system.DAE.x[self.vf] Ka = mul(self.u, self.Ka) Kf = mul(self.u, self.Kf) K5 = div(Kf, self.Tf) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) Se = Se + mul(mul(Se, self.Be), vf) z = matrix(0, (self.n, 1)) for i in range(self.n): if vr1[i] < vrmax[i] and vr1[i] > vrmin[i]: z[i] = 1 system.DAE.Fx = system.DAE.Fx \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.Tf), self.vr2, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(K5, self.Tf), self.vr2, self.vf, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(self.Ke+Se, self.Te), self.vf, self.vf, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(z, self.Te), self.vf, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vm, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.Ta), self.vr1, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(mul(z, K5), Ka), self.Ta), self.vr1, self.vf, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy \ + spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vref, (system.DAE.nx, system.DAE.ny))
def dfcall(self, x): dfvec = mul(mul(self.A, self.omega), cos(self.omega*x + self.phi)) return sum(dfvec)
def fcall(self): vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] system.DAE.f[self.vm] = div(mul(self.u, vg - system.DAE.x[self.vm]), self.Tr) # K5 = div(self.Kf, self.Tf) system.DAE.f[self.vr1] = div( mul(self.u, mul(self.Ka, vref - vm - vr2 - mul(K5, vf)) - vr1), self.Ta) system.DAE.f[self.vr2] = div(mul(-self.u, mul(K5, vf) + vr2), self.Tf) # non-windup limit for i in range(self.n): if vr1[i] >= vrmax[i] and system.DAE.f[self.vr1[i]] > 0: system.DAE.f[self.vr1[i]] = 0 if vr1[i] <= vrmin[i] and system.DAE.f[self.vr1[i]] < 0: system.DAE.f[self.vr1[i]] = 0 for i in range(self.n): vr1[i] = min(vr1[i], vrmax[i]) vr1[i] = max(vr1[i], vrmin[i]) system.DAE.x[self.vr1] = mul(self.u, vr1) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) system.DAE.f[self.vf] = div( mul(self.u, vr1 - mul(self.Ke, vf) - mul(Se, vf)), self.Te)
p, q = d, a else: p, q = a, d x0 = y[:] S = matrix(0.0, (q,1)) # First, compute two initial points on the tradeoff curve lamT = [50.0, 0.02] nrmT = [0.0, 0.0] errT = [0.0, 0.0] for i in xrange(len(lamT)): Cd = matrix(2.0*lamT[i], (n,1)) C = base.spdiag(Cd) d = -base.mul(Cd, x0) sol = nucnrm.nrmapp(A, B, C = C, d = d) x = sol['x'] lapack.gesvd(matrix(A*x, (p,q)) + B, S) nrmT[i] = sum(S) errT[i] = blas.dot(x-x0, x-x0) # plot the tradeoff curve upper/lower bounds with the initial 2 points pylab.figure(0) N = 200 slope = -matrix(lamT) errM = matrix(errT) nrmM = matrix(nrmT) xx = matrix(range(N))*((errM[-1]-errM[0])/(N-1))+errM[0]
def setx0(self): if self.n == 0: return vg = system.DAE.y[self.bv] vf = mul(self.u, system.Syn6.vf0[self.a]) vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) # 检查参数 for i in range(self.n): if self.Te[i] <= 0: self.Te[i] = 1 print('<%i> Te不能小于等于0,设置Te = 1 [s]' % i) if self.Tr[i] <= 0: self.Tr[i] = 0.001 print('<%i> Tr不能小于等于0,设置Te = 0.001 [s]' % i) if self.K0[i] <= 0: self.K0[i] = 400 print('<%i> K0不能小于等于0,设置K0 = 400 ' % i) if self.T1[i] <= 0: self.T1[i] = 0.1 print('<%i> T1不能小于等于0,设置T1 = 0.1 [s]' % i) if self.T4[i] <= 0: self.T4[i] = 0.01 print('<%i> T4不能小于等于0,设置T4 = 0.01 [s]' % i) # Ce = -vf - mul(mul(self.Ae, exp(mul(self.Be, vf))), vf) K1 = mul(self.K0, div(self.T2, self.T1)) K2 = self.K0 - K1 K3 = div(self.T3, self.T4) K4 = 1 - K3 system.DAE.x[self.vm] = mul(self.u, vg) self.vref0 = vg - div(Ce, self.K0) system.DAE.x[self.vr1] = mul(self.u, mul(K2, self.vref0 - vg)) system.DAE.x[self.vr2] = mul(self.u, mul(K4, self.vref0 - vg)) system.DAE.x[self.vf] = vf system.DAE.y[self.vref] = mul(self.u, self.vref0) vr = mul(self.u, mul(self.K0, system.DAE.x[self.vr2])) + mul( K3, mul(K1, self.vref0 - vg) + system.DAE.x[self.vr1]) for i in range(self.n): if vr[i] > self.vrmax[i]: print('Warn: vr1超出最大值vrmax') if vr[i] < self.vrmin[i]: print('Warn: vr1小于最小值vrmin') system.Syn6.vf0[self.a] = 0
def sysid(y, u, vsig, svth = None): """ System identification using the subspace method and nuclear norm optimization. Estimate a linear time-invariant state-space model given inputs and outputs. The algorithm is described in [1]. INPUT y 'd' matrix of size (p, N). y are the measured outputs, p is the number of outputs, and N is the number of data points measured. u 'd' matrix of size (m, N). u are the inputs, m is the number of inputs, and N is the number of data points. vsig a weighting parameter in the nuclear norm optimization, its value is approximately the 1-sigma output noise level svth an optional parameter, if specified, the model order is determined as the number of singular values greater than svth times the maximum singular value. The default value is 1E-3 OUTPUT sol a dictionary with the following words -- 'A', 'B', 'C', 'D' are the state-space matrices -- 'svN', the original singular values of the Hankel matrix -- 'sv', the optimized singular values of the Hankel matrix -- 'x0', the initial state x(0) -- 'n', the model order [1] Zhang Liu and Lieven Vandenberghe. "Interior-point method for nuclear norm approximation with application to system identification." """ m, N, p = u.size[0], u.size[1], y.size[0] if y.size[1] != N: raise ValueError, "y and u must have the same length" # Y = G*X + H*U + V, Y has size a x b, U has size c x b, Un has b x d r = min(int(30/p),int((N+1.0)/(p+m+1)+1.0)) a = r*p c = r*m b = N-r+1 d = b-c # construct Hankel matrix Y Y = Hankel(y,r,b,p=p,q=1) # construct Hankel matrix U U = Hankel(u,r,b,p=m,q=1) # compute Un = null(U) and YUn = Y*Un Vt = matrix(0.0,(b,b)) Stemp = matrix(0.0,(c,1)) Un = matrix(0.0,(b,d)) YUn = matrix(0.0,(a,d)) lapack.gesvd(U,Stemp,jobvt='A',Vt=Vt) Un[:,:] = Vt.T[:,c:] blas.gemm(Y,Un,YUn) # compute original singular values svN = matrix(0.0,(min(a,d),1)) lapack.gesvd(YUn,svN) # variable, [y(1);...;y(N)] # form the coefficient matrices for the nuclear norm optimization # minimize | Yh * Un |_* + alpha * | y - yh |_F AA = Hankel_basis(r,b,p=p,q=1) A = matrix(0.0,(a*d,p*N)) temp = spmatrix([],[],[],(a,b),'d') temp2 = matrix(0.0,(a,d)) for ii in xrange(p*N): temp[:] = AA[:,ii] base.gemm(temp,Un,temp2) A[:,ii] = temp2[:] B = matrix(0.0,(a,d)) # flip the matrix if columns is more than rows if a < d: Itrans = [i+j*a for i in xrange(a) for j in xrange(d)] B[:] = B[Itrans] B.size = (d,a) for ii in xrange(p*N): A[:,ii] = A[Itrans,ii] # regularized term x0 = y[:] Qd = matrix(2.0*svN[0]/p/N/(vsig**2),(p*N,1)) # solve the nuclear norm optimization sol = nrmapp(A, B, C = base.spdiag(Qd), d = -base.mul(x0, Qd)) status = sol['status'] x = sol['x'] # construct YhUn and take the svd YhUn = matrix(B) blas.gemv(A,x,YhUn,beta=1.0) if a < d: YhUn = YhUn.T Uh = matrix(0.0,(a,d)) sv = matrix(0.0,(d,1)) lapack.gesvd(YhUn,sv,jobu='S',U=Uh) # determine model order if svth is None: svth = 1E-3 svthn = sv[0]*svth n=1 while sv[n] >= svthn and n < 10: n=n+1 # estimate A, C Uhn = Uh[:,:n] for ii in xrange(n): blas.scal(sv[ii],Uhn,n=a,offset=ii*a) syseC = Uhn[:p,:] Als = Uhn[:-p,:] Bls = Uhn[p:,:] lapack.gels(Als,Bls) syseA = Bls[:n,:] Als[:,:] = Uhn[:-p,:] Bls[:,:] = Uhn[p:,:] blas.gemm(Als,syseA,Bls,beta=-1.0) Aerr = blas.nrm2(Bls) # stabilize A Sc = matrix(0.0,(n,n),'z') w = matrix(0.0, (n,1), 'z') Vs = matrix(0.0, (n,n), 'z') def F(w): return (abs(w) < 1.0) Sc[:,:] = syseA ns = lapack.gees(Sc, w, Vs, select = F) while ns < n: #print "stabilize matrix A" w[ns:] = w[ns:]**-1 Sc[::n+1] = w Sc = Vs*Sc*Vs.H syseA[:,:] = Sc.real() Sc[:,:] = syseA ns = lapack.gees(Sc, w, Vs, select = F) # estimate B,D,x0 stored in vector [x0; vec(D); vec(B)] F1 = matrix(0.0,(p*N,n)) F1[:p,:] = syseC for ii in xrange(1,N): F1[ii*p:(ii+1)*p,:] = F1[(ii-1)*p:ii*p,:]*syseA F2 = matrix(0.0,(p*N,p*m)) ut = u.T for ii in xrange(p): F2[ii::p,ii::p] = ut F3 = matrix(0.0,(p*N,n*m)) F3t = matrix(0.0,(p*(N-1),n*m)) for ii in xrange(1,N): for jj in xrange(p): for kk in xrange(n): F3t[jj:jj+(N-ii)*p:p,kk::n] = ut[:N-ii,:]*F1[(ii-1)*p+jj,kk] F3[ii*p:,:] = F3[ii*p:,:] + F3t[:(N-ii)*p,:] F = matrix([[F1],[F2],[F3]]) yls = y[:] Sls = matrix(0.0,(F.size[1],1)) Uls = matrix(0.0,(F.size[0],F.size[1])) Vtls = matrix(0.0,(F.size[1],F.size[1])) lapack.gesvd(F, Sls, jobu='S', jobvt='S', U=Uls, Vt=Vtls) Frank=len([ii for ii in xrange(Sls.size[0]) if Sls[ii] >= 1E-6]) #print 'Rank deficiency = ', F.size[1] - Frank xx = matrix(0.0,(F.size[1],1)) xx[:Frank] = Uls.T[:Frank,:] * yls xx[:Frank] = base.mul(xx[:Frank],Sls[:Frank]**-1) xx[:] = Vtls.T[:,:Frank]*xx[:Frank] blas.gemv(F,xx,yls,beta=-1.0) xxerr = blas.nrm2(yls) x0 = xx[:n] syseD = xx[n:n+p*m] syseD.size = (p,m) syseB = xx[n+p*m:] syseB.size = (n,m) return {'A': syseA, 'B': syseB, 'C': syseC, 'D': syseD, 'svN': svN, 'sv': \ sv, 'x0': x0, 'n': n, 'Aerr': Aerr, 'xxerr': xxerr}