Commit 6055c5f6 authored by Markus Mirz's avatar Markus Mirz
Browse files

remove complex workarounds

parent 3db666e1
......@@ -67,23 +67,22 @@ def BC_power_flow(branches, nodes):
V = np.ones(nodes.num) + 1j* np.zeros(nodes.num)
num_iter = 0
State = np.zeros(2*branches.num)
State = np.ones(2*branches.num)
State = np.concatenate((np.array([1,0]),State),axis=0)
while diff > epsilon:
for k in range(1,nodes.num+1):
i = k-1
m = 2*i
t = nodes.type[i]
if t == 'slack':
if nodes.type[i] == 'slack':
h[m] = np.inner(H[m],State)
h[m+1] = np.inner(H[m+1],State)
elif t == 'PQ':
elif nodes.type[i] == 'PQ':
z[m] = (nodes.pwr_flow_values[1][i]*V[i].real + nodes.pwr_flow_values[2][i]*V[i].imag)/(np.abs(V[i])**2)
z[m+1] = (nodes.pwr_flow_values[1][i]*V[i].imag - nodes.pwr_flow_values[2][i]*V[i].real)/(np.abs(V[i])**2)
h[m] = np.inner(H[m],State)
h[m+1] = np.inner(H[m+1],State)
elif t == 'PV':
elif nodes.type[i] == 'PV':
z[m] = (nodes.pwr_flow_values[1][i]*V[i].real + nodes.pwr_flow_values[2][i]*V[i].imag)/(np.abs(V[i])**2)
h[m] = np.inner(H[m],State)
h[m+1] = np.abs(V[i])
......@@ -126,9 +125,7 @@ def BC_power_flow(branches, nodes):
Sinj_rx = np.multiply(V, np.conj(Iinj))
Sinj = np.real(Sinj_rx) + 1j * np.imag(Sinj_rx)
S1_rx = np.multiply(V[branchs.start-1], np.conj(I))
S2_rx = np.multiply(V[branchs.end-1], np.conj(I))
S1 = np.real(S1_rx) + 1j * np.imag(S1_rx)
S2 = np.real(S2_rx) + 1j * np.imag(S2_rx)
S1 = np.multiply(V[branchs.start-1], np.conj(I))
S2 = np.multiply(V[branchs.end-1], np.conj(I))
return V, I, Iinj, S1, S2, Sinj, num_iter
\ No newline at end of file
import numpy
import numpy as np
import math
class Real_to_all:
def __init__(self,Ar, Ax):
self.real = Ar
self.imag = Ax
self.complex = Ar + 1j*Ax
self.mag = numpy.absolute(self.complex)
self.phase = numpy.angle(self.complex)
def Ymatrix_calc(branch, node):
Ymatrix = numpy.zeros((node.num,node.num),dtype=numpy.complex)
Ymatrix = np.zeros((node.num,node.num),dtype=np.complex)
Adjacencies = [[] for _ in range(node.num)]
for index in range(branch.num):
fr = branch.start[index] - 1
......@@ -28,9 +20,9 @@ def NV_power_flow(branch, node):
Ymatrix, Adj = Ymatrix_calc(branch,node)
z = numpy.zeros(2*(node.num))
h = numpy.zeros(2*(node.num))
H = numpy.zeros((2*(node.num),2*(node.num)))
z = np.zeros(2*(node.num))
h = np.zeros(2*(node.num))
H = np.zeros((2*(node.num),2*(node.num)))
for k in range(1,node.num+1):
i = k-1
......@@ -45,92 +37,86 @@ def NV_power_flow(branch, node):
H[m][i] = 1
H[m+1][i2] = 1
elif t == 'PQ':
H[m][i] = - numpy.real(Ymatrix[i][i])
H[m][i2] = numpy.imag(Ymatrix[i][i])
H[m+1][i] = - numpy.imag(Ymatrix[i][i])
H[m+1][i2] = - numpy.real(Ymatrix[i][i])
idx1 = numpy.subtract(Adj[i],1)
H[m][i] = - np.real(Ymatrix[i][i])
H[m][i2] = np.imag(Ymatrix[i][i])
H[m+1][i] = - np.imag(Ymatrix[i][i])
H[m+1][i2] = - np.real(Ymatrix[i][i])
idx1 = np.subtract(Adj[i],1)
idx2 = idx1 + node.num
H[m][idx1] = - numpy.real(Ymatrix[i][idx1])
H[m][idx2] = numpy.imag(Ymatrix[i][idx1])
H[m+1][idx1] = - numpy.imag(Ymatrix[i][idx1])
H[m+1][idx2] = - numpy.real(Ymatrix[i][idx1])
H[m][idx1] = - np.real(Ymatrix[i][idx1])
H[m][idx2] = np.imag(Ymatrix[i][idx1])
H[m+1][idx1] = - np.imag(Ymatrix[i][idx1])
H[m+1][idx2] = - np.real(Ymatrix[i][idx1])
elif t == 'PV':
z[m+1] = node.pwr_flow_values[2][i]
H[m][i] = - numpy.real(Ymatrix[i][i])
H[m][i2] = numpy.imag(Ymatrix[i][i])
idx1 = numpy.subtract(Adj[i],1)
H[m][i] = - np.real(Ymatrix[i][i])
H[m][i2] = np.imag(Ymatrix[i][i])
idx1 = np.subtract(Adj[i],1)
idx2 = idx1 + node.num
H[m][idx1] = - numpy.real(Ymatrix[i][idx1])
H[m][idx2] = numpy.imag(Ymatrix[i][idx1])
H[m][idx1] = - np.real(Ymatrix[i][idx1])
H[m][idx2] = np.imag(Ymatrix[i][idx1])
epsilon = 5
Vr = numpy.ones(node.num)
Vx = numpy.zeros(node.num)
V = Real_to_all(Vr, Vx)
epsilon = 10**(-10)
diff = 5
V = np.ones(nodes.num) + 1j* np.zeros(nodes.num)
num_iter = 0
StateVr = numpy.ones(node.num)
StateVx = numpy.zeros(node.num)
State = numpy.concatenate((StateVr,StateVx),axis=0)
State = np.ones(2*branches.num)
State = np.concatenate((np.array([1,0]),State),axis=0)
while epsilon>10**(-10):
while diff > epsilon:
for k in range(1,node.num+1):
i = k-1
m = 2*i
i2 = i + node.num
t = node.type[i]
if t == 'slack':
h[m] = numpy.inner(H[m],State)
h[m+1] = numpy.inner(H[m+1],State)
elif t == 'PQ':
if nodes.type[i] == 'slack':
h[m] = np.inner(H[m],State)
h[m+1] = np.inner(H[m+1],State)
elif nodes.type[i] == 'PQ':
z[m] = (node.pwr_flow_values[1][i]*V.real[i] + node.pwr_flow_values[2][i]*V.imag[i])/(V.mag[i]**2)
z[m+1] = (node.pwr_flow_values[1][i]*V.imag[i] - node.pwr_flow_values[2][i]*V.real[i])/(V.mag[i]**2)
h[m] = numpy.inner(H[m],State)
h[m+1] = numpy.inner(H[m+1],State)
elif t == 'PV':
h[m] = np.inner(H[m],State)
h[m+1] = np.inner(H[m+1],State)
elif nodes.type[i] == 'PV':
z[m] = (node.pwr_flow_values[1][i]*V.real[i] + node.pwr_flow_values[2][i]*V.imag[i])/(V.mag[i]**2)
h[m] = numpy.inner(H[m],State)
h[m] = np.inner(H[m],State)
h[m+1] = V.mag[i]
H[m+1][i] = numpy.cos(V.phase[i])
H[m+1][i2] = numpy.sin(V.phase[i])
H[m+1][i] = np.cos(V.phase[i])
H[m+1][i2] = np.sin(V.phase[i])
r = numpy.subtract(z,h)
Hinv = numpy.linalg.inv(H)
Delta_State = numpy.inner(Hinv,r)
r = np.subtract(z,h)
Hinv = np.linalg.inv(H)
Delta_State = np.inner(Hinv,r)
State = State + Delta_State
epsilon = numpy.amax(numpy.absolute(Delta_State))
diff = np.amax(np.absolute(Delta_State))
V.real = State[:node.num]
V.imag = State[node.num:]
V = Real_to_all(V.real, V.imag)
V = State[:node.num] + 1j * State[node.num:]
num_iter = num_iter+1
Irx = numpy.zeros((branch.num),dtype=numpy.complex)
Irx = np.zeros((branch.num),dtype=np.complex)
for idx in range(branch.num):
fr = branch.start[idx]-1
to = branch.end[idx]-1
Irx[idx] = - (V.complex[fr] - V.complex[to])*Ymatrix[fr][to]
Ir = numpy.real(Irx)
Ix = numpy.imag(Irx)
Irx[idx] = - (V[fr] - V[to])*Ymatrix[fr][to]
Ir = np.real(Irx)
Ix = np.imag(Irx)
I = Real_to_all(Ir,Ix)
Iinj_r = numpy.zeros(node.num)
Iinj_x = numpy.zeros(node.num)
I = Ir + 1j*Ix
Iinj_r = np.zeros(node.num)
Iinj_x = np.zeros(node.num)
for k in range(1,node.num+1):
to = numpy.where(branch.end==k)
fr = numpy.where(branch.start==k)
Iinj_r[k-1] = numpy.sum(I.real[to[0]]) - numpy.sum(I.real[fr[0]])
Iinj_x[k-1] = numpy.sum(I.imag[to[0]]) - numpy.sum(I.imag[fr[0]])
to = np.where(branch.end==k)
fr = np.where(branch.start==k)
Iinj_r[k-1] = np.sum(I[to[0]].real) - np.sum(I[fr[0]].real)
Iinj_x[k-1] = np.sum(I[to[0]].imag) - np.sum(I[fr[0]].imag)
Iinj = Real_to_all(Iinj_r,Iinj_x)
Sinj_rx = numpy.multiply(V.complex,numpy.conj(Iinj.complex))
Sinj = Real_to_all(numpy.real(Sinj_rx),numpy.imag(Sinj_rx))
S1_rx = numpy.multiply(V.complex[branch.start-1],numpy.conj(I.complex))
S2_rx = - numpy.multiply(V.complex[branch.end-1],numpy.conj(I.complex))
S1 = Real_to_all(numpy.real(S1_rx),numpy.imag(S1_rx))
S2 = Real_to_all(numpy.real(S2_rx),numpy.imag(S2_rx))
Iinj = Iinj_r + 1j * Iinj_x
Sinj_rx = np.multiply(V, np.conj(Iinj))
Sinj = np.real(Sinj_rx) + 1j * np.imag(Sinj_rx)
S1 = np.multiply(V[branchs.start-1], np.conj(I))
S2 = - np.multiply(V[branchs.end-1], np.conj(I))
return V, I, Iinj, S1, S2, Sinj, num_iter
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment