def gycall(self, dae): self.pll_gycall(dae) self.outer_gycall(dae) self.current_gycall(dae) dae.add_jac(Gy, cos(dae.y[self.a] - dae.y[self.adq]), self.vd, self.v) dae.add_jac(Gy, - mul(dae.y[self.v], sin(dae.y[self.a] - dae.y[self.adq])), self.vd, self.a) dae.add_jac(Gy, mul(dae.y[self.v], sin(dae.y[self.a] - dae.y[self.adq])), self.vd, self.adq) dae.add_jac(Gy, - sin(dae.y[self.a] - dae.y[self.adq]), self.vq, self.v) dae.add_jac(Gy, - mul(dae.y[self.v], cos(dae.y[self.a] - dae.y[self.adq])), self.vq, self.a) dae.add_jac(Gy, mul(dae.y[self.v], cos(dae.y[self.a] - dae.y[self.adq])), self.vq, self.adq) dae.add_jac(Gy, dae.x[self.Iq], self.p, self.vq) dae.add_jac(Gy, dae.x[self.Id], self.p, self.vd) dae.add_jac(Gy, - dae.x[self.Id], self.q, self.vq) dae.add_jac(Gy, dae.x[self.Iq], self.q, self.vd)
def makefig1(): pylab.figure(1, facecolor='w', figsize=(6, 6)) pylab.plot(V[0, :].T, V[1, :].T, 'b-') nopts = 1000 angles = matrix([a * 2.0 * pi / nopts for a in range(nopts)], (1, nopts)) circle = matrix(0.0, (2, nopts)) circle[0, :], circle[1, :] = cos(angles), sin(angles) for k in range(len(C)): c = C[k] pylab.plot([c[0]], [c[1]], 'ow') pylab.text(c[0], c[1], "s%d" % k) pylab.plot(c[0] + circle[0, :].T, c[1] + circle[1, :].T, 'g:') if k >= 1: v = V[:, k - 1] if k == 1: dir = 0.5 * (C[k] + C[-1]) - v else: dir = 0.5 * (C[k] + C[k - 1]) - v pylab.plot([v[0], v[0] + 5 * dir[0]], [v[1], v[1] + 5 * dir[1]], 'b-') ellipse = +circle blas.trsm(L, ellipse, transA='T') pylab.plot(xc[0] + ellipse[0, :].T, xc[1] + ellipse[1, :].T, 'r-') for Xk in X: pylab.plot([Xk[0]], [Xk[1]], 'ro') pylab.axis([-5, 5, -5, 5]) pylab.title('Geometrical interpretation of Chebyshev bound (fig. 7.7)') pylab.axis('off')
def gcall(self, dae): nzeros = [0] * self.n v = mul(self.u, dae.y[self.v]) vd = dae.y[self.vd] vq = dae.y[self.vq] Id = dae.y[self.Id] Iq = dae.y[self.Iq] self.ss = sin(dae.x[self.delta] - dae.y[self.a]) self.cc = cos(dae.x[self.delta] - dae.y[self.a]) dae.g -= spmatrix(dae.y[self.p], self.a, nzeros, (dae.m, 1), 'd') dae.g -= spmatrix(dae.y[self.q], self.v, nzeros, (dae.m, 1), 'd') dae.g -= spmatrix(vd - mul(v, self.ss), self.vd, nzeros, (dae.m, 1), 'd') # note d(vd)/d(delta) dae.g -= spmatrix(vq - mul(v, self.cc), self.vq, nzeros, (dae.m, 1), 'd') # note d(vq)/d(delta) dae.g += spmatrix( mul(vd, Id) + mul(vq, Iq) - dae.y[self.p], self.p, nzeros, (dae.m, 1), 'd') dae.g += spmatrix( mul(vq, Id) - mul(vd, Iq) - dae.y[self.q], self.q, nzeros, (dae.m, 1), 'd') dae.g += spmatrix(dae.y[self.pm] - self.pm0, self.pm, nzeros, (dae.m, 1), 'd') dae.g += spmatrix(dae.y[self.vf] - self.vf0, self.vf, nzeros, (dae.m, 1), 'd')
def makefig1(): pylab.figure(1, facecolor='w', figsize=(6,6)) pylab.plot(V[0,:].T, V[1,:].T, 'b-') nopts = 1000 angles = matrix( [a*2.0*pi/nopts for a in range(nopts) ], (1,nopts) ) circle = matrix(0.0, (2,nopts)) circle[0,:], circle[1,:] = cos(angles), sin(angles) for k in range(len(C)): c = C[k] pylab.plot([c[0]], [c[1]], 'og') pylab.text(c[0], c[1], "s%d" %k) pylab.plot(c[0] + circle[0,:].T, c[1]+circle[1,:].T, 'g:') if k >= 1: v = V[:,k-1] if k==1: dir = 0.5 * (C[k] + C[-1]) - v else: dir = 0.5 * (C[k] + C[k-1]) - v pylab.plot([v[0], v[0] + 5*dir[0]], [v[1], v[1] + 5*dir[1]], 'b-') ellipse = +circle blas.trsm(L, ellipse, transA='T') pylab.plot(xc[0] + ellipse[0,:].T, xc[1]+ellipse[1,:].T, 'r-') for Xk in X: pylab.plot([Xk[0]], [Xk[1]], 'ro') pylab.axis([-5, 5, -5, 5]) pylab.title('Geometrical interpretation of Chebyshev bound (fig. 7.7)') pylab.axis('off')
def gycall(self, dae): self.speed_gycall(dae) self.voltage_gycall(dae) self.current_gycall(dae) dae.add_jac(Gy, dae.x[self.Iq], self.p, self.vq) dae.add_jac(Gy, dae.x[self.Id], self.p, self.vd) dae.add_jac(Gy, - dae.x[self.Id], self.q, self.vq) dae.add_jac(Gy, dae.x[self.Iq], self.q, self.vd) dae.add_jac(Gy, - mul(self.u, dae.y[self.v], sin(dae.y[self.a] - dae.x[self.adq])), self.vd, self.a) dae.add_jac(Gy, mul(self.u, cos(dae.y[self.a] - dae.x[self.adq])), self.vd, self.v) dae.add_jac(Gy, - mul(self.u, sin(dae.y[self.a] - dae.x[self.adq])), self.vq, self.v) dae.add_jac(Gy, - mul(self.u, dae.y[self.v], cos(dae.y[self.a] - dae.x[self.adq])), self.vq, self.a) dae.add_jac(Gy, mul(self.u, (dae.y[self.v1] - dae.y[self.v2])**-2, mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v1, self.v2) dae.add_jac(Gy, - mul(self.u, (dae.y[self.v1] - dae.y[self.v2])**-2, mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v1, self.v1) dae.add_jac(Gy, - mul(self.u, (dae.y[self.v1] - dae.y[self.v2])**-2, mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v2, self.v2) dae.add_jac(Gy, mul(self.u, (dae.y[self.v1] - dae.y[self.v2])**-2, mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v2, self.v1)
def gycall(self, dae): dae.add_jac(Gy, mul(self.xmu, 1 - dae.x[self.omega_m]), self.vrd, self.isq) dae.add_jac(Gy, -mul(self.xmu, 1 - dae.x[self.omega_m]), self.vrq, self.isd) dae.add_jac(Gy, -sin(dae.y[self.a]), self.vsd, self.v) dae.add_jac(Gy, -mul(dae.y[self.v], cos(dae.y[self.a])), self.vsd, self.a) dae.add_jac(Gy, cos(dae.y[self.a]), self.vsq, self.v) dae.add_jac(Gy, -mul(dae.y[self.v], sin(dae.y[self.a])), self.vsq, self.a) dae.add_jac( Gy, mul(0.5, self.ngen, pi, self.rho, (self.R)**2, (self.Vwn)**3, div(1, self.mva_mega), (dae.x[self.vw])**3), self.pw, self.cp) dae.add_jac( Gy, mul(-25.52, (dae.y[self.ilamb])**-2, exp(mul(-12.5, div(1, dae.y[self.ilamb])))) + mul( 12.5, (dae.y[self.ilamb])**-2, -1.1 + mul(25.52, div(1, dae.y[self.ilamb])) + mul(-0.088, dae.x[self.theta_p]), exp(mul(-12.5, div(1, dae.y[self.ilamb])))), self.cp, self.ilamb) dae.add_jac( Gy, mul((dae.y[self.lamb] + mul(0.08, dae.x[self.theta_p]))**-2, (div(1, dae.y[self.lamb] + mul(0.08, dae.x[self.theta_p])) + mul(-0.035, div(1, 1 + (dae.x[self.theta_p])**3)))**-2), self.ilamb, self.lamb) dae.add_jac(Gy, -mul(dae.y[self.isd], self.u0), self.a, self.vsd) dae.add_jac(Gy, -mul(dae.x[self.irq], self.u0), self.a, self.vrq) dae.add_jac(Gy, -mul(self.u0, dae.y[self.vsq]), self.a, self.isq) dae.add_jac(Gy, -mul(dae.x[self.ird], self.u0), self.a, self.vrd) dae.add_jac(Gy, -mul(dae.y[self.isq], self.u0), self.a, self.vsq) dae.add_jac(Gy, -mul(self.u0, dae.y[self.vsd]), self.a, self.isd) dae.add_jac( Gy, mul( self.u0, mul(2, dae.y[self.v], div(1, self.x0)) + mul(dae.x[self.ird], self.xmu, div(1, self.x0))), self.v, self.v)
def gcall(self, dae): toSb = div(self.Sn, self.system.Settings.mva) dae.g[self.isd] = -dae.y[self.vsd] + mul( dae.x[self.irq], self.xmu) + mul(dae.y[self.isq], self.x0) - mul( dae.y[self.isd], self.rs) dae.g[self.isq] = -dae.y[self.vsq] - mul( dae.x[self.ird], self.xmu) - mul(dae.y[self.isd], self.x0) - mul( dae.y[self.isq], self.rs) dae.g[self.vrd] = -dae.y[self.vrd] + mul( 1 - dae.x[self.omega_m], mul(dae.x[self.irq], self.x1) + mul(dae.y[self.isq], self.xmu)) - mul(dae.x[self.ird], self.rr) dae.g[self.vrq] = -dae.y[self.vrq] - mul( dae.x[self.irq], self.rr) - mul( 1 - dae.x[self.omega_m], mul(dae.x[self.ird], self.x1) + mul(dae.y[self.isd], self.xmu)) dae.g[self.vsd] = -dae.y[self.vsd] - mul(dae.y[self.v], sin(dae.y[self.a])) dae.g[self.vsq] = -dae.y[self.vsq] + mul(dae.y[self.v], cos(dae.y[self.a])) dae.g[self.vref] = self.vref0 - dae.y[self.vref] # dae.g[self.pwa] = mul(2*dae.x[self.omega_m] - 1, toSb) - dae.y[self.pwa] dae.g[self.pwa] = mmax(mmin(2 * dae.x[self.omega_m] - 1, 1), 0) - dae.y[self.pwa] dae.hard_limit(self.pwa, 0, 1) dae.g[self.pw] = -dae.y[self.pw] + mul( 0.5, dae.y[self.cp], self.ngen, pi, self.rho, (self.R)**2, (self.Vwn)**3, div(1, self.mva_mega), (dae.x[self.vw])**3) dae.g[self.cp] = -dae.y[self.cp] + mul( -1.1 + mul(25.52, div(1, dae.y[self.ilamb])) + mul(-0.08800000000000001, dae.x[self.theta_p]), exp(mul(-12.5, div(1, dae.y[self.ilamb])))) dae.g[self.lamb] = -dae.y[self.lamb] + mul( 4, self.R, self.fn, self.ngb, dae.x[self.omega_m], pi, div(1, self.Vwn), div(1, self.npole), div(1, dae.x[self.vw])) dae.g[self.ilamb] = div( 1, div(1, dae.y[self.lamb] + mul(0.08, dae.x[self.theta_p])) + mul(-0.035, div(1, 1 + (dae.x[self.theta_p])**3))) - dae.y[self.ilamb] dae.g += spmatrix( mul( self.u0, -mul(dae.x[self.ird], dae.y[self.vrd]) - mul(dae.x[self.irq], dae.y[self.vrq]) - mul(dae.y[self.isd], dae.y[self.vsd]) - mul(dae.y[self.isq], dae.y[self.vsq])), self.a, [0] * self.n, (dae.m, 1), 'd') dae.g += spmatrix( mul( self.u0, mul((dae.y[self.v])**2, div(1, self.x0)) + mul(dae.x[self.ird], dae.y[self.v], self.xmu, div( 1, self.x0))), self.v, [0] * self.n, (dae.m, 1), 'd')
def gcall(self, dae): self.speed_gcall(dae) self.voltage_gcall(dae) self.current_gcall(dae) dae.g[self.p] = -dae.y[self.p] + mul(dae.x[self.Id], dae.y[self.vd]) + mul(dae.x[self.Iq], dae.y[self.vq]) dae.g[self.q] = -dae.y[self.q] + mul(dae.x[self.Iq], dae.y[self.vd]) - mul(dae.x[self.Id], dae.y[self.vq]) dae.g[self.vd] = -dae.y[self.vd] + mul(self.u, dae.y[self.v], cos(dae.y[self.a] - dae.x[self.adq])) dae.g[self.vq] = -dae.y[self.vq] - mul(self.u, dae.y[self.v], sin(dae.y[self.a] - dae.x[self.adq])) dae.g += spmatrix(- mul(dae.y[self.p], self.u), self.a, [0]*self.n, (dae.m, 1), 'd') dae.g += spmatrix(- mul(dae.y[self.q], self.u), self.v, [0]*self.n, (dae.m, 1), 'd') dae.g += spmatrix(mul(self.u, div(1, dae.y[self.v1] - dae.y[self.v2]), mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v1, [0]*self.n, (dae.m, 1), 'd') dae.g += spmatrix(- mul(self.u, div(1, dae.y[self.v1] - dae.y[self.v2]), mul(dae.x[self.Id], dae.x[self.ud]) + mul(dae.x[self.Iq], dae.x[self.uq])), self.v2, [0]*self.n, (dae.m, 1), 'd')
def cvx(): # Figures 6.8-10, pages 313-314 # Quadratic smoothing. from math import pi from cvxopt import blas, lapack, matrix, sin, mul, normal n = 4000 t = matrix(list(range(n)), tc='d') ex = 0.5 * mul(sin(2 * pi / n * t), sin(0.01 * t)) corr = ex + 0.05 * normal(n, 1) # A = D'*D is an n by n tridiagonal matrix with -1.0 on the # upper/lower diagonal and 1, 2, 2, ..., 2, 2, 1 on the diagonal. Ad = matrix([1.0] + (n - 2) * [2.0] + [1.0]) As = matrix(-1.0, (n - 1, 1)) nopts = 50 deltas = -10.0 + 20.0 / (nopts - 1) * matrix(list(range(nopts))) cost1, cost2 = [], [] for delta in deltas: xr = +corr lapack.ptsv(1.0 + 10 ** delta * Ad, 10 ** delta * As, xr) cost1 += [blas.nrm2(xr - corr)] cost2 += [blas.nrm2(xr[1:] - xr[:-1])] # Find solutions with ||xhat - xcorr || roughly equal to 8.0, 3.1, 1.0. time.sleep(1) mv1, k1 = min(zip([abs(c - 8.0) for c in cost1], range(nopts))) xr1 = +corr lapack.ptsv(1.0 + 10 ** deltas[k1] * Ad, 10 ** deltas[k1] * As, xr1) mv2, k2 = min(zip([abs(c - 3.1) for c in cost1], range(nopts))) xr2 = +corr lapack.ptsv(1.0 + 10 ** deltas[k2] * Ad, 10 ** deltas[k2] * As, xr2) mv3, k3 = min(zip([abs(c - 1.0) for c in cost1], range(nopts))) xr3 = +corr lapack.ptsv(1.0 + 10 ** deltas[k3] * Ad, 10 ** deltas[k3] * As, xr3)
def init1(self, dae): self.servcall(dae) self.pll_init1(dae) dae.y[self.ref1] = self.ref10 dae.y[self.ref2] = self.ref20 dae.y[self.vd] = mul(dae.y[self.v], cos(dae.y[self.a] - dae.y[self.adq])) dae.y[self.vq] = - mul(dae.y[self.v], sin(dae.y[self.a] - dae.y[self.adq])) self.current_init1(dae) self.outer_init1(dae) dae.y[self.p] = mul(dae.x[self.Id], dae.y[self.vd]) + mul(dae.x[self.Iq], dae.y[self.vq]) dae.y[self.q] = mul(dae.x[self.Iq], dae.y[self.vd]) - mul(dae.x[self.Id], dae.y[self.vq]) for idx in self.vsc: self.system.VSC.disable(idx)
def gcall(self, dae): self.pll_gcall(dae) self.outer_gcall(dae) self.current_gcall(dae) dae.g[self.ref1] = self.ref10 - dae.y[self.ref1] \ + mul(self.PQ + self.PV, mul(self.v12 - self.v120, self.Kdc)) \ - mul(self.PQ + self.PV, mul(dae.x[self.w] - 1), self.Kf) \ + mul(self.vV + self.vQ, self.Kp, dae.y[self.p] - self.pref0)\ - mul(self.vV + self.vQ, mul(dae.x[self.w] - 1), self.Kf) dae.g[self.ref2] = self.ref20 - dae.y[self.ref2] dae.g[self.vd] = -dae.y[self.vd] + mul(dae.y[self.v], cos(dae.y[self.a] - dae.y[self.adq])) dae.g[self.vq] = -dae.y[self.vq] - mul(dae.y[self.v], sin(dae.y[self.a] - dae.y[self.adq])) dae.g[self.p] = -dae.y[self.p] + mul(dae.x[self.Id], dae.y[self.vd]) + mul(dae.x[self.Iq], dae.y[self.vq]) dae.g[self.q] = -dae.y[self.q] + mul(dae.x[self.Iq], dae.y[self.vd]) - mul(dae.x[self.Id], dae.y[self.vq]) dae.g += spmatrix(- dae.y[self.p], self.a, [0]*self.n, (dae.m, 1), 'd') dae.g += spmatrix(- dae.y[self.q], self.v, [0]*self.n, (dae.m, 1), 'd')
def fxcall(self, dae): self.speed_fxcall(dae) self.voltage_fxcall(dae) self.current_fxcall(dae) dae.add_jac(Gx, dae.y[self.vq], self.p, self.Iq) dae.add_jac(Gx, dae.y[self.vd], self.p, self.Id) dae.add_jac(Gx, dae.y[self.vd], self.q, self.Iq) dae.add_jac(Gx, - dae.y[self.vq], self.q, self.Id) dae.add_jac(Gx, mul(self.u, dae.y[self.v], sin(dae.y[self.a] - dae.x[self.adq])), self.vd, self.adq) dae.add_jac(Gx, mul(self.u, dae.y[self.v], cos(dae.y[self.a] - dae.x[self.adq])), self.vq, self.adq) dae.add_jac(Gx, mul(self.u, dae.x[self.uq], div(1, dae.y[self.v1] - dae.y[self.v2])), self.v1, self.Iq) dae.add_jac(Gx, mul(self.u, dae.x[self.ud], div(1, dae.y[self.v1] - dae.y[self.v2])), self.v1, self.Id) dae.add_jac(Gx, mul(dae.x[self.Id], self.u, div(1, dae.y[self.v1] - dae.y[self.v2])), self.v1, self.ud) dae.add_jac(Gx, mul(dae.x[self.Iq], self.u, div(1, dae.y[self.v1] - dae.y[self.v2])), self.v1, self.uq) dae.add_jac(Gx, - mul(self.u, dae.x[self.uq], div(1, dae.y[self.v1] - dae.y[self.v2])), self.v2, self.Iq) dae.add_jac(Gx, - mul(self.u, dae.x[self.ud], div(1, dae.y[self.v1] - dae.y[self.v2])), self.v2, self.Id) dae.add_jac(Gx, - mul(dae.x[self.Id], self.u, div(1, dae.y[self.v1] - dae.y[self.v2])), self.v2, self.ud) dae.add_jac(Gx, - mul(dae.x[self.Iq], self.u, div(1, dae.y[self.v1] - dae.y[self.v2])), self.v2, self.uq)
from math import pi from cvxopt import blas, lapack, solvers from cvxopt import matrix, spmatrix, sin, mul, div, normal #solvers.options['show_progress'] = 0 try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 2000 t = matrix(list(range(n)), tc='d') ex = matrix( n//4*[1.0] + n//4*[-1.0] + n//4*[1.0] + n//4*[-1.0] ) + \ 0.5 * sin( 2.0*pi/n * t ) corr = ex + 0.1 * normal(n, 1) if pylab_installed: pylab.figure(1, facecolor='w', figsize=(8, 5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel('x[i]') pylab.xlabel('i') pylab.axis([0, 2000, -2, 2]) pylab.title('Original and corrupted signal (fig. 6.11)') pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel('xcor[i]') pylab.xlabel('i') pylab.axis([0, 2000, -2, 2])
# Build dictionary matrix ts = (1.0/N) * matrix(range(N), tc='d') B = ts[:, K*[0]] - tau * matrix(range(K), (1,K), 'd')[N*[0],:] B = exp(-(B/sigma)**2) A = matrix(0.0, (N, K*(2*L+1))) # First K columns are DC pulses for k = 0,...,K-1 A[:,:K] = B for l in range(L): # Cosine pulses for omega = (l+1)*omega0 and k = 0,...,K-1. A[:, K+l*(2*K) : K+l*(2*K)+K] = mul(B, cos((l+1)*omega0*ts)[:, K*[0]]) # Sine pulses for omega = (l+1)*omega0 and k = 0,...,K-1. A[:, K+l*(2*K)+K : K+(l+1)*(2*K)] = \ mul(B, sin((l+1)*omega0*ts)[:,K*[0]]) if pylab_installed: pylab.figure(1, facecolor='w') pylab.subplot(311) # DC pulse for k = 250 (tau = 0.5) pylab.plot(ts, A[:,250]) pylab.ylabel('f(0.5, 0, c)') pylab.axis([0, 1, -1, 1]) pylab.title('Three basis elements (fig. 6.21)') # Cosine pulse for k = 250 (tau = 0.5) and l = 15 (omega = 75) pylab.subplot(312) pylab.ylabel('f(0.5, 75, c)') pylab.plot(ts, A[:, K + 14*(2*K) + 250]) pylab.axis([0, 1, -1, 1])
B = ts[:, K * [0]] - tau * matrix(range(K), (1, K), 'd')[N * [0], :] B = exp(-(B / sigma)**2) A = matrix(0.0, (N, K * (2 * L + 1))) # First K columns are DC pulses for k = 0,...,K-1 A[:, :K] = B for l in range(L): # Cosine pulses for omega = (l+1)*omega0 and k = 0,...,K-1. A[:, K + l * (2 * K):K + l * (2 * K) + K] = mul( B, cos((l + 1) * omega0 * ts)[:, K * [0]]) # Sine pulses for omega = (l+1)*omega0 and k = 0,...,K-1. A[:, K+l*(2*K)+K : K+(l+1)*(2*K)] = \ mul(B, sin((l+1)*omega0*ts)[:,K*[0]]) if pylab_installed: pylab.figure(1, facecolor='w') pylab.subplot(311) # DC pulse for k = 250 (tau = 0.5) pylab.plot(ts, A[:, 250]) pylab.ylabel('f(0.5, 0, c)') pylab.axis([0, 1, -1, 1]) pylab.title('Three basis elements (fig. 6.21)') # Cosine pulse for k = 250 (tau = 0.5) and l = 15 (omega = 75) pylab.subplot(312) pylab.ylabel('f(0.5, 75, c)') pylab.plot(ts, A[:, K + 14 * (2 * K) + 250]) pylab.axis([0, 1, -1, 1])
# Figures 6.11-14, pages 315-317. # Total variation reconstruction. from math import pi from cvxopt import blas, lapack, solvers from cvxopt import matrix, spmatrix, sin, mul, div, normal #solvers.options['show_progress'] = 0 try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 2000 t = matrix( list(range(n)), tc='d' ) ex = matrix( n//4*[1.0] + n//4*[-1.0] + n//4*[1.0] + n//4*[-1.0] ) + \ 0.5 * sin( 2.0*pi/n * t ) corr = ex + 0.1 * normal(n,1) if pylab_installed: pylab.figure(1, facecolor='w', figsize=(8,5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel('x[i]') pylab.xlabel('i') pylab.axis([0, 2000, -2, 2]) pylab.title('Original and corrupted signal (fig. 6.11)') pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel('xcor[i]') pylab.xlabel('i') pylab.axis([0, 2000, -2, 2])
def init1(self, dae): """New initialization function""" self.servcall(dae) retval = True mva = self.system.mva self.p0 = mul(self.p0, self.gammap) self.q0 = mul(self.q0, self.gammaq) dae.y[self.vsd] = mul(dae.y[self.v], -sin(dae.y[self.a])) dae.y[self.vsq] = mul(dae.y[self.v], cos(dae.y[self.a])) rs = matrix(self.rs) rr = matrix(self.rr) xmu = matrix(self.xmu) x1 = matrix(self.xs) + xmu x2 = matrix(self.xr) + xmu Pg = matrix(self.p0) Qg = matrix(self.q0) Vc = dae.y[self.v] vsq = dae.y[self.vsq] vsd = dae.y[self.vsd] toSn = div(mva, self.Sn) # to machine base toSb = self.Sn / mva # to system base # rotor speed omega = 1 * (ageb(mva * Pg, self.Sn)) + \ mul(0.5 + 0.5 * mul(Pg, toSn), aandb(agtb(Pg, 0), altb(mva * Pg, self.Sn))) + \ 0.5 * (aleb(mva * Pg, 0)) slip = 1 - omega theta = mul(self.Kp, mround(1000 * (omega - 1)) / 1000) theta = mmax(theta, 0) # prepare for the iterations irq = mul(-x1, toSb, (2 * omega - 1), div(1, Vc), div(1, xmu), div(1, omega)) isd = zeros(*irq.size) isq = zeros(*irq.size) # obtain ird isd isq for i in range(self.n): A = sparse([[-rs[i], vsq[i]], [x1[i], -vsd[i]]]) B = matrix([vsd[i] - xmu[i] * irq[i], Qg[i]]) linsolve(A, B) isd[i] = B[0] isq[i] = B[1] ird = -div(vsq + mul(rs, isq) + mul(x1, isd), xmu) vrd = -mul(rr, ird) + mul( slip, mul(x2, irq) + mul(xmu, isq)) # todo: check x1 or x2 vrq = -mul(rr, irq) - mul(slip, mul(x2, ird) + mul(xmu, isd)) # main iterations for i in range(self.n): mis = ones(6, 1) rows = [0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5] cols = [0, 1, 3, 0, 1, 2, 2, 4, 3, 5, 0, 1, 2] x = matrix([isd[i], isq[i], ird[i], irq[i], vrd[i], vrq[i]]) # vals = [-rs, x1, xmu, -x1, -rs, -xmu, -rr, # -1, -rr, -1, vsd, vsq, -xmu * Vc / x1] vals = [ -rs[i], x1[i], xmu[i], -x1[i], -rs[i], -xmu[i], -rr[i], -1, -rr[i], -1, vsd[i], vsq[i], -xmu[i] * Vc[i] / x1[i] ] jac0 = spmatrix(vals, rows, cols, (6, 6), 'd') niter = 0 while max(abs(mis)) > self.system.tds.config.tol: if niter > 20: logger.error('Initialization of DFIG <{}> failed.'.format( self.name[i])) retval = False break mis[0] = -rs[i] * x[0] + x1[i] * x[1] + xmu[i] * x[3] - vsd[i] mis[1] = -rs[i] * x[1] - x1[i] * x[0] - xmu[i] * x[2] - vsq[i] mis[2] = -rr[i] * x[2] + slip[i] * (x2[i] * x[3] + xmu[i] * x[1]) - x[4] mis[3] = -rr[i] * x[3] - slip[i] * (x2[i] * x[2] + xmu[i] * x[0]) - x[5] mis[4] = vsd[i] * x[0] + vsq[i] * x[1] + x[4] * x[2] + \ x[5] * x[3] - Pg[i] mis[5] = -xmu[i] * Vc[i] * x[2] / x1[i] - \ Vc[i] * Vc[i] / x1[i] - Qg[i] rows = [2, 2, 3, 3, 4, 4, 4, 4] cols = [1, 3, 0, 2, 2, 3, 4, 5] vals = [ slip[i] * xmu[i], slip[i] * x2[i], -slip[i] * xmu[i], -slip[i] * x2[i], x[4], x[5], x[2], x[3] ] jac = jac0 + spmatrix(vals, rows, cols, (6, 6), 'd') linsolve(jac, mis) x -= mis niter += 1 isd[i] = x[0] isq[i] = x[1] ird[i] = x[2] irq[i] = x[3] vrd[i] = x[4] vrq[i] = x[5] dae.x[self.ird] = mul(self.u0, ird) dae.x[self.irq] = mul(self.u0, irq) dae.y[self.isd] = isd dae.y[self.isq] = isq dae.y[self.vrd] = vrd dae.y[self.vrq] = vrq dae.x[self.omega_m] = mul(self.u0, omega) dae.x[self.theta_p] = mul(self.u0, theta) dae.y[self.pwa] = mmax(mmin(2 * dae.x[self.omega_m] - 1, 1), 0) self.vref0 = mul(aneb(self.KV, 0), Vc - div(ird + div(Vc, xmu), self.KV)) dae.y[self.vref] = self.vref0 k = mul(div(x1, Vc, xmu, omega), toSb) self.irq_off = -mul(k, mmax(mmin(2 * omega - 1, 1), 0)) - irq # electrical torque in pu te = mul( xmu, mul(dae.x[self.irq], dae.y[self.isd]) - mul(dae.x[self.ird], dae.y[self.isq])) for i in range(self.n): if te[i] < 0: logger.error( 'Pe < 0 on bus <{}>. Wind speed initialize failed.'.format( self.bus[i])) retval = False # wind power in pu pw = mul(te, omega) dae.y[self.pw] = pw # wind speed initialization loop R = 4 * pi * self.system.freq * mul(self.R, self.ngb, div( 1, self.npole)) AA = pi * self.R**2 vw = 0.9 * self.Vwn for i in range(self.n): mis = 1 niter = 0 while abs(mis) > self.system.tds.config.tol: if niter > 50: logger.error( 'Wind <{}> init failed. ' 'Try increasing the nominal wind speed.'.format( self.wind[i])) retval = False break pw_iter, jac = self.windpower(self.ngen[i], self.rho[i], vw[i], AA[i], R[i], omega[i], theta[i]) mis = pw_iter - pw[i] inc = -mis / jac[1] vw[i] += inc niter += 1 # set wind speed dae.x[self.vw] = div(vw, self.Vwn) lamb = div(omega, vw, div(1, R)) ilamb = div(1, (div(1, lamb + 0.08 * theta) - div(0.035, theta**3 + 1))) cp = 0.22 * mul( div(116, ilamb) - 0.4 * theta - 5, exp(div(-12.5, ilamb))) dae.y[self.lamb] = lamb dae.y[self.ilamb] = ilamb dae.y[self.cp] = cp self.system.rmgen(self.gen) if not retval: logger.error('DFIG initialization failed') return retval
# Figures 6.8-10, pages 313-314 # Quadratic smoothing. from math import pi from cvxopt import blas, lapack, matrix, sin, mul, normal try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 4000 t = matrix(list(range(n)), tc='d') ex = 0.5 * mul(sin(2 * pi / n * t), sin(0.01 * t)) corr = ex + 0.05 * normal(n, 1) if pylab_installed: pylab.figure(1, facecolor='w', figsize=(8, 5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel('x[i]') pylab.xlabel('i') pylab.title('Original and corrupted signal (fig. 6.8)') pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel('xcor[i]') pylab.xlabel('i') # A = D'*D is an n by n tridiagonal matrix with -1.0 on the # upper/lower diagonal and 1, 2, 2, ..., 2, 2, 1 on the diagonal.
# Tikhonov solution: minimize || A*x - b ||_2^2 + 0.1*||x||^2_2 xtik = A.T*b S = A.T*A S[::n+1] += 0.1 lapack.posv(S, xtik) # Worst case solution xwc = wcls(A, Ap, b) notrials = 100000 r = sqrt(uniform(1,notrials)) theta = 2.0 * pi * uniform(1,notrials) u = matrix(0.0, (2,notrials)) u[0,:] = mul(r, cos(theta)) u[1,:] = mul(r, sin(theta)) # LS solution q = A*xls - b P = matrix(0.0, (m,2)) P[:,0], P[:,1] = Ap[0]*xls, Ap[1]*xls r = P*u + q[:,notrials*[0]] resls = sqrt( matrix(1.0, (1,m)) * mul(r,r) ) q = A*xtik - b P[:,0], P[:,1] = Ap[0]*xtik, Ap[1]*xtik r = P*u + q[:,notrials*[0]] restik = sqrt( matrix(1.0, (1,m)) * mul(r,r) ) q = A*xwc - b P[:,0], P[:,1] = Ap[0]*xwc, Ap[1]*xwc
def gycall(self, dae): if sum(self.u) == 0: return Zsh = self.rsh + 1j * self.xsh iZsh = div(self.u, abs(Zsh)) Vh = polar(dae.y[self.v], dae.y[self.a] * 1j) Vsh = polar(dae.y[self.vsh], dae.y[self.ash] * 1j) Ish = div(Vsh - Vh + 1e-6, Zsh) iIsh = div(self.u, Ish) gsh = div(self.u, Zsh).real() bsh = div(self.u, Zsh).imag() V = dae.y[self.v] theta = dae.y[self.a] Vsh = dae.y[self.vsh] thetash = dae.y[self.ash] Vdc = dae.y[self.v1] - dae.y[self.v2] iVdc2 = div(self.u, Vdc ** 2) dae.add_jac(Gy, div(self.u, Vdc), self.v1, self.pdc) dae.add_jac(Gy, -mul(self.u, dae.y[self.pdc], iVdc2), self.v1, self.v1) dae.add_jac(Gy, mul(self.u, dae.y[self.pdc], iVdc2), self.v1, self.v2) dae.add_jac(Gy, -div(self.u, Vdc), self.v2, self.pdc) dae.add_jac(Gy, mul(self.u, dae.y[self.pdc], iVdc2), self.v2, self.v1) dae.add_jac(Gy, -mul(self.u, dae.y[self.pdc], iVdc2), self.v2, self.v2) dae.add_jac(Gy, -2*mul(gsh, V) + mul(gsh, Vsh, cos(theta - thetash)) + mul(bsh, Vsh, sin(theta - thetash)), self.ash, self.v) dae.add_jac(Gy, mul(gsh, V, cos(theta - thetash)) + mul(bsh, V, sin(theta - thetash)), self.ash, self.vsh) dae.add_jac(Gy, -mul(gsh, V, Vsh, sin(theta - thetash)) + mul(bsh, V, Vsh, cos(theta - thetash)), self.ash, self.a) dae.add_jac(Gy, mul(gsh, V, Vsh, sin(theta - thetash)) - mul(bsh, V, Vsh, cos(theta - thetash)), self.ash, self.ash) dae.add_jac(Gy, 2*mul(bsh, V) + mul(gsh, Vsh, sin(theta - thetash)) - mul(bsh, Vsh, cos(theta - thetash)), self.vsh, self.v) dae.add_jac(Gy, mul(gsh, V, sin(theta - thetash)) - mul(bsh, V, cos(theta - thetash)), self.vsh, self.vsh) dae.add_jac(Gy, mul(gsh, V, Vsh, cos(theta - thetash)) + mul(bsh, V, Vsh, sin(theta - thetash)), self.vsh, self.a) dae.add_jac(Gy, -mul(gsh, V, Vsh, cos(theta - thetash)) - mul(bsh, V, Vsh, sin(theta - thetash)), self.vsh, self.ash) dae.add_jac(Gy, 0.5 * mul(self.u, 2*V - 2*mul(Vsh, cos(theta - thetash)), abs(iIsh), abs(iZsh) ** 2), self.Ish, self.v) dae.add_jac(Gy, 0.5 * mul(self.u, 2*Vsh - 2*mul(V, cos(theta - thetash)), abs(iIsh), abs(iZsh) ** 2), self.Ish, self.vsh) dae.add_jac(Gy, 0.5 * mul(self.u, 2*V, Vsh, sin(theta-thetash), abs(iIsh), abs(iZsh) ** 2), self.Ish, self.a) dae.add_jac(Gy, 0.5 * mul(self.u, 2*V, Vsh, - sin(theta - thetash), abs(iIsh), abs(iZsh) ** 2), self.Ish, self.ash) dae.add_jac(Gy, -2 * mul(self.u, self.k2, dae.y[self.Ish]), self.pdc, self.Ish) dae.add_jac(Gy, mul(2 * gsh, Vsh) - mul(gsh, V, cos(theta - thetash)) + mul(bsh, V, sin(theta - thetash)), self.pdc, self.vsh) dae.add_jac(Gy, -mul(gsh, Vsh, cos(theta - thetash)) + mul(bsh, Vsh, sin(theta - thetash)), self.pdc, self.v) dae.add_jac(Gy, mul(gsh, V, Vsh, sin(theta - thetash)) + mul(bsh, V, Vsh, cos(theta - thetash)), self.pdc, self.a) dae.add_jac(Gy, -mul(gsh, V, Vsh, sin(theta - thetash)) - mul(bsh, V, Vsh, cos(theta - thetash)), self.pdc, self.ash) for gidx, yidx in zip(self.glim, self.ylim): dae.set_jac(Gy, 0.0, [gidx] * dae.m, range(dae.m)) dae.set_jac(Gy, 1.0, [gidx], [yidx]) dae.set_jac(Gy, -1e-6, [gidx], [gidx])
# Figures 6.8-10, pages 313-314 # Quadratic smoothing. from math import pi from cvxopt import blas, lapack, matrix, sin, mul, normal try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 4000 t = matrix(list(range(n)), tc='d') ex = 0.5 * mul( sin(2*pi/n * t), sin(0.01 * t)) corr = ex + 0.05 * normal(n,1) if pylab_installed: pylab.figure(1, facecolor='w', figsize=(8,5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel('x[i]') pylab.xlabel('i') pylab.title('Original and corrupted signal (fig. 6.8)') pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel('xcor[i]') pylab.xlabel('i') # A = D'*D is an n by n tridiagonal matrix with -1.0 on the # upper/lower diagonal and 1, 2, 2, ..., 2, 2, 1 on the diagonal. Ad = matrix([1.0] + (n-2)*[2.0] + [1.0]) As = matrix(-1.0, (n-1,1))
if pylab_installed: pylab.figure(1, facecolor='w') pylab.plot(X[:, 0], X[:, 1], 'ko', X[:, 0], X[:, 1], '-k') # Ellipsoid in the form { x | || L' * (x-c) ||_2 <= 1 } L = +A lapack.potrf(L) c = +b lapack.potrs(L, c) # 1000 points on the unit circle nopts = 1000 angles = matrix([a * 2.0 * pi / nopts for a in range(nopts)], (1, nopts)) circle = matrix(0.0, (2, nopts)) circle[0, :], circle[1, :] = cos(angles), sin(angles) # ellipse = L^-T * circle + c blas.trsm(L, circle, transA='T') ellipse = circle + c[:, nopts * [0]] ellipse2 = 0.5 * circle + c[:, nopts * [0]] pylab.plot(ellipse[0, :].T, ellipse[1, :].T, 'k-') pylab.fill(ellipse2[0, :].T, ellipse2[1, :].T, facecolor='#F0F0F0') pylab.title('Loewner-John ellipsoid (fig 8.3)') pylab.axis('equal') pylab.axis('off') # Maximum volume enclosed ellipsoid # # minimize -log det B
# Tikhonov solution: minimize || A*x - b ||_2^2 + 0.1*||x||^2_2 xtik = A.T * b S = A.T * A S[::n + 1] += 0.1 lapack.posv(S, xtik) # Worst case solution xwc = wcls(A, Ap, b) notrials = 100000 r = sqrt(uniform(1, notrials)) theta = 2.0 * pi * uniform(1, notrials) u = matrix(0.0, (2, notrials)) u[0, :] = mul(r, cos(theta)) u[1, :] = mul(r, sin(theta)) # LS solution q = A * xls - b P = matrix(0.0, (m, 2)) P[:, 0], P[:, 1] = Ap[0] * xls, Ap[1] * xls r = P * u + q[:, notrials * [0]] resls = sqrt(matrix(1.0, (1, m)) * mul(r, r)) q = A * xtik - b P[:, 0], P[:, 1] = Ap[0] * xtik, Ap[1] * xtik r = P * u + q[:, notrials * [0]] restik = sqrt(matrix(1.0, (1, m)) * mul(r, r)) q = A * xwc - b P[:, 0], P[:, 1] = Ap[0] * xwc, Ap[1] * xwc
def gycall(self, dae): Zsh = self.rsh + 1j * self.xsh iZsh = div(1, abs(Zsh)) V = polar(dae.y[self.v], dae.y[self.a] * 1j) Vsh = polar(dae.y[self.vsh], dae.y[self.ash] * 1j) Ish = div(V - Vsh, Zsh) iIsh = div(1, Ish) gsh = div(1, Zsh).real() bsh = div(1, Zsh).imag() V = dae.y[self.v] theta = dae.y[self.a] Vsh = dae.y[self.vsh] thetash = dae.y[self.ash] Vdc = dae.y[self.v1] - dae.y[self.v2] dae.add_jac(Gy, -div(1, Vdc), self.v1, self.pdc) dae.add_jac(Gy, div(dae.y[self.pdc], mul(Vdc, Vdc)), self.v1, self.v1) dae.add_jac(Gy, -div(dae.y[self.pdc], mul(Vdc, Vdc)), self.v1, self.v2) dae.add_jac(Gy, div(1, Vdc), self.v2, self.pdc) dae.add_jac(Gy, -div(dae.y[self.pdc], mul(Vdc, Vdc)), self.v2, self.v1) dae.add_jac(Gy, div(dae.y[self.pdc], mul(Vdc, Vdc)), self.v2, self.v2) dae.add_jac( Gy, 2 * mul(gsh, V) - mul(gsh, Vsh, cos(theta - thetash)) - mul(bsh, Vsh, sin(theta - thetash)), self.ash, self.v) dae.add_jac( Gy, -mul(gsh, V, cos(theta - thetash)) - mul(bsh, V, sin(theta - thetash)), self.ash, self.vsh) dae.add_jac( Gy, mul(gsh, V, Vsh, sin(theta - thetash)) - mul(bsh, V, Vsh, cos(theta - thetash)), self.ash, self.a) dae.add_jac( Gy, -mul(gsh, V, Vsh, sin(theta - thetash)) + mul(bsh, V, Vsh, cos(theta - thetash)) + 1e-6, self.ash, self.ash) dae.add_jac( Gy, -2 * mul(bsh, V) - mul(gsh, Vsh, sin(theta - thetash)) + mul(bsh, Vsh, cos(theta - thetash)), self.vsh, self.v) dae.add_jac( Gy, -mul(gsh, V, sin(theta - thetash)) + mul(bsh, V, cos(theta - thetash)), self.vsh, self.vsh) dae.add_jac( Gy, -mul(gsh, V, Vsh, cos(theta - thetash)) - mul(bsh, V, Vsh, sin(theta - thetash)), self.vsh, self.a) dae.add_jac( Gy, mul(gsh, V, Vsh, cos(theta - thetash)) + mul(bsh, V, Vsh, sin(theta - thetash)), self.vsh, self.ash) dae.add_jac( Gy, 0.5 * mul(2 * V - 2 * mul(Vsh, cos(theta - thetash)), abs(iIsh), abs(iZsh), abs(iZsh)), self.Ish, self.v) dae.add_jac( Gy, 0.5 * mul(2 * Vsh - 2 * mul(V, cos(theta - thetash)), abs(iIsh), abs(iZsh), abs(iZsh)), self.Ish, self.vsh) dae.add_jac( Gy, 0.5 * mul(2 * V, Vsh, sin(theta - thetash), abs(iIsh), abs(iZsh), abs(iZsh)), self.Ish, self.a) dae.add_jac( Gy, 0.5 * mul(2 * V, Vsh, -sin(theta - thetash), abs(iIsh), abs(iZsh), abs(iZsh)), self.Ish, self.ash) dae.add_jac(Gy, -2 * mul(self.k2, dae.y[self.Ish]), self.pdc, self.Ish) dae.add_jac( Gy, -mul(2 * gsh, Vsh) + mul(gsh, V, cos(theta - thetash)) - mul(bsh, V, sin(theta - thetash)), self.pdc, self.vsh) dae.add_jac( Gy, mul(gsh, Vsh, cos(theta - thetash)) - mul(bsh, Vsh, sin(theta - thetash)), self.pdc, self.v) dae.add_jac( Gy, -mul(gsh, V, Vsh, sin(theta - thetash)) - mul(bsh, V, Vsh, cos(theta - thetash)), self.pdc, self.a) dae.add_jac( Gy, mul(gsh, V, Vsh, sin(theta - thetash)) + mul(bsh, V, Vsh, cos(theta - thetash)), self.pdc, self.ash) dae.add_jac(Gy, -self.R, self.pdc, self.v1) for gidx, yidx in zip(self.glim, self.ylim): dae.set_jac(Gy, 0.0, [gidx] * dae.m, range(dae.m)) dae.set_jac(Gy, 1.0, [gidx], [yidx]) dae.set_jac(Gy, 1e-6, [gidx], [gidx])
if pylab_installed: pylab.figure(1, facecolor='w') pylab.plot(X[:,0], X[:,1], 'ko', X[:,0], X[:,1], '-k') # Ellipsoid in the form { x | || L' * (x-c) ||_2 <= 1 } L = +A lapack.potrf(L) c = +b lapack.potrs(L, c) # 1000 points on the unit circle nopts = 1000 angles = matrix( [ a*2.0*pi/nopts for a in range(nopts) ], (1,nopts) ) circle = matrix(0.0, (2,nopts)) circle[0,:], circle[1,:] = cos(angles), sin(angles) # ellipse = L^-T * circle + c blas.trsm(L, circle, transA='T') ellipse = circle + c[:, nopts*[0]] ellipse2 = 0.5 * circle + c[:, nopts*[0]] pylab.plot(ellipse[0,:].T, ellipse[1,:].T, 'k-') pylab.fill(ellipse2[0,:].T, ellipse2[1,:].T, facecolor = '#F0F0F0') pylab.title('Loewner-John ellipsoid (fig 8.3)') pylab.axis('equal') pylab.axis('off') # Maximum volume enclosed ellipsoid #
from math import pi from cvxopt import blas, lapack, solvers from cvxopt import matrix, spmatrix, sin, mul, div, normal # solvers.options['show_progress'] = 0 try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 2000 t = matrix(list(range(n)), tc="d") ex = matrix(n // 4 * [1.0] + n // 4 * [-1.0] + n // 4 * [1.0] + n // 4 * [-1.0]) + 0.5 * sin(2.0 * pi / n * t) corr = ex + 0.1 * normal(n, 1) if pylab_installed: pylab.figure(1, facecolor="w", figsize=(8, 5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel("x[i]") pylab.xlabel("i") pylab.axis([0, 2000, -2, 2]) pylab.title("Original and corrupted signal (fig. 6.11)") pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel("xcor[i]") pylab.xlabel("i") pylab.axis([0, 2000, -2, 2])