Commit 465864e2 authored by martin.moraga's avatar martin.moraga
Browse files

improve nv_powerflow_cim.py

parent 55a58520
......@@ -36,6 +36,8 @@ class System():
self.bX=[]
self.P=[]
self.Q=[]
self.Ymatrix = np.zeros((1, 1),dtype=np.complex)
self.Adjacencies = np.array([])
def load_cim_data(self, res):
#this function is used to fill the vectors node, branch, bR, bX, P and Q
......@@ -52,8 +54,6 @@ class System():
length=1.0
self.bR.append(value.r*length)
self.bX.append(value.x*length)
#startNode=res[value.startNodeID]
#endNode=res[value.endNodeID]
for i in range(len(self.nodes)):
if value.startNodeID==self.nodes[i].uuid:
startNode=self.nodes[i]
......@@ -66,8 +66,6 @@ class System():
elif value.__class__.__name__=="PowerTransformer":
self.bR.append(value.primaryConnection.r)
self.bX.append(value.primaryConnection.x)
#startNode=res[value.startNodeID]
#endNode=res[value.endNodeID]
for i in range(len(self.nodes)):
if value.startNodeID==self.nodes[i].uuid:
startNode=self.nodes[i]
......@@ -79,7 +77,22 @@ class System():
self.branches.append(Branch(value.primaryConnection.r, value.primaryConnection.x, startNode, endNode))
else:
continue
self.Ymatrix_calc()
def Ymatrix_calc(self):
nodes_num = len(self.nodes)
branches_num = len(self.branches)
self.Ymatrix = np.zeros((nodes_num, nodes_num),dtype=np.complex)
self.Adjacencies = [[] for _ in range(nodes_num)]
for branch in self.branches:
fr = branch.start_node.index
to = branch.end_node.index
self.Ymatrix[fr][to] -= branch.y
self.Ymatrix[to][fr] -= branch.y
self.Ymatrix[fr][fr] += branch.y
self.Ymatrix[to][to] += branch.y
self.Adjacencies[fr].append(to+1) #to + 1???
self.Adjacencies[to].append(fr+1) #fr + 1???
def load_python_data(nodes, branches, type):
system = System()
......@@ -93,7 +106,8 @@ def load_python_data(nodes, branches, type):
for branch_idx in range(0, branches.num):
system.branches.append(Branch(branches.R[branch_idx], branches.X[branch_idx],
system.nodes[branches.start[branch_idx]-1], system.nodes[branches.end[branch_idx]-1]))
system.Ymatrix_calc()
return system
def Ymatrix_calc(system):
......@@ -110,4 +124,4 @@ def Ymatrix_calc(system):
Ymatrix[to][to] += system.branches[index].y
Adjacencies[fr].append(to+1)
Adjacencies[to].append(fr+1)
return Ymatrix, Adjacencies
\ No newline at end of file
return Ymatrix, Adjacencies
......@@ -5,7 +5,7 @@ from network import Ymatrix_calc
from network import BusType
sys.path.append("../../../dataprocessing")
from dpsim_reader import read_data
from villas.dataprocessing.readtools import read_timeseries_dpsim
class PF_Results():
def __init__(self):
......@@ -41,10 +41,104 @@ class PowerflowBranch():
self.power = complex(0, 0)
class PowerflowResults():
def __init__(self):
def __init__(self, system):
self.nodes=[]
self.branches=[]
self.Ymatrix=system.Ymatrix
self.Adjacencies=system.Adjacencies
for node in system.nodes:
self.nodes.append(PowerflowNode(topo_node=node))
for branch in system.branches:
self.branches.append(PowerflowBranch(topo_branch=branch))
def read_data(self, file_name):
"""
read the voltages from a input file
"""
loadflow_results = read_data(loadflow_results_file)
for elem in range(len(self.nodes)):
elem.V = loadflow_results[elem.topology_node.uuid]
def load_voltages(self, V):
"""
load the voltages of V-array (result of powerflow_cim.solve)
"""
for index in range(len(V)):
for elem in self.nodes:
if elem.topology_node.index == index:
elem.voltage = V[index]
def calculateI(self):
"""
To calculate the branch currents
"""
for branch in self.branches:
fr = branch.topology_branch.start_node.index
to = branch.topology_branch.end_node.index
branch.current = - (self.nodes[fr].voltage - self.nodes[to].voltage)*self.Ymatrix[fr][to]
def calculateIinj(self):
"""
calculate current injections at a node
"""
for node in self.nodes:
to=complex(0, 0) #sum of the currents flowing to the node
fr=complex(0, 0) #sum of the currents flowing from the node
for branch in self.branches:
if node.topology_node.index==branch.topology_branch.start_node.index:
fr = fr + branch.current
if node.topology_node.index==branch.topology_branch.end_node.index:
to = to + branch.current
node.current = to - fr
def calculateSinj(self):
"""
calculate power injection at a node
"""
for node in self.nodes:
node.power = node.voltage*np.conj(node.current)
def get_voltages(self):
"""
get node voltages
for a test purpose
"""
voltages = np.zeros(len(self.nodes), dtype=np.complex_)
for node in self.nodes:
voltages[node.topology_node.index] = node.voltage
return voltages
def get_Iinj(self):
"""
get node currents
for a test purpose
"""
Iinj = np.zeros(len(self.nodes), dtype=np.complex_)
for node in self.nodes:
Iinj[node.topology_node.index] = node.current
return Iinj
def get_Sinj(self):
"""
get node power
for a test purpose
"""
Sinj = np.zeros(len(self.nodes), dtype=np.complex_)
for node in self.nodes:
Sinj[node.topology_node.index] = node.power
return Sinj
def getI(self):
"""
get branch currents
for a test purpose
"""
currents = np.zeros(len(self.branches), dtype=np.complex_)
for elem in self.branches:
voltages[elem.topology_node.index] = elem.voltage
return voltages
def solve(system):
"""It performs Power Flow by using rectangular node voltage state variables."""
......
import math
import sys
sys.path.append("../acs/state_estimation")
sys.path.append("../../acs/state_estimation")
import network
import py_95bus_network_data
from nv_powerflow_cim import Ymatrix_calc as Ymatrix_calc_cim
from nv_powerflow_cim import solve as solve_cim
import nv_powerflow_cim
from nv_powerflow import *
class PerUnit:
......@@ -24,18 +23,21 @@ slackV = 1.02
Base = PerUnit(S,V)
branch, node = py_95bus_network_data.Network_95_nodes(Base, slackV)
system=network.load_python_data(node, branch, node.type)
V, I, Iinj, S1, S2, Sinj, num_iter = solve_cim(system)
print("S1_cim:")
print(S1)
print("\n\n")
print("Sinj_cim:")
print(Sinj)
print("\n\n")
print("num_iter:" + str(num_iter))
print("\n\n")
V, I, Iinj, S1, S2, Sinj, num_iter = solve(branch, node)
system = network.load_python_data(node, branch, node.type)
res = nv_powerflow_cim.PF_Results()
res.V, res.num_iter = nv_powerflow_cim.solve(system)
res.I = nv_powerflow_cim.calculateI(system, res.V)
res.Iinj = nv_powerflow_cim.calculateInj(system, res.I)
res.S1 = nv_powerflow_cim.calculateS1(system, res.V, res.I)
res.S2 = nv_powerflow_cim.calculateS2(system, res.V, res.I)
res.Sinj = nv_powerflow_cim.calculateSinj(res.V, res.Iinj)
# Show numerical comparison
print("res.V==V? " + str(not np.any(res.V-V)))
print("res.I==I? " + str(not np.any(res.I-I)))
print("res.Iinj==Iinj? " + str(not np.any(res.Iinj-Iinj)))
print("res.S1==S1? " + str(not np.any(res.S1-S1)))
print("res.S2==S2? " + str(not np.any(res.S2-S2)))
print("res.Sinj==Sinj? " + str(not np.any(res.Sinj-Sinj)))
\ No newline at end of file
import numpy as np
import math
import sys
import copy
sys.path.append("../../acs/state_estimation")
import py_95bus_network_data
import py_95bus_meas_data
import nv_state_estimator
import nv_powerflow
import network
import nv_powerflow_cim
import cim_py_95bus_meas_data
import nv_state_estimator_cim
class PerUnit:
def __init__(self, S, V):
self.S = S
self.V = V
self.I = S/V
self.Z = S/(V**2)
class Measurements:
def __init__(self, index, unc):
self.index = index.astype(int)
self.unc = unc
self.num = len(index)
class Measurement_set:
def __init__(self, V, I, Sinj, S1, S2, Vpmu_mag, Vpmu_phase, Ipmu_mag, Ipmu_phase):
self.V = V
self.Sinj = Sinj
self.S1 = S1
self.S2 = S2
self.I = I
self.Vpmu_mag = Vpmu_mag
self.Vpmu_phase = Vpmu_phase
self.Ipmu_mag = Ipmu_mag
self.Ipmu_phase = Ipmu_phase
class Zdata_init:
def __init__(self, meas):
nmeas = meas.V.num + meas.I.num + 2*meas.Sinj.num + 2*meas.S1.num + 2*meas.S2.num + meas.Ipmu_mag.num + meas.Ipmu_phase.num + meas.Vpmu_mag.num + meas.Vpmu_phase.num
self.mtype = np.zeros(nmeas)
self.mval = np.zeros(nmeas)
self.mbranch = np.zeros(nmeas)
self.mfrom = np.zeros(nmeas)
self.mto = np.zeros(nmeas)
self.mstddev = np.zeros(nmeas)
""" Insert here per unit values of the grid for power and voltage """
S = 100*(10**6)
V = (11*(10**3))/math.sqrt(3)
slackV = 1.02
Base = PerUnit(S,V)
branch, node = py_95bus_network_data.Network_95_nodes(Base, slackV)
system = network.load_python_data(node, branch, node.type)
results = nv_powerflow_cim.PowerflowResults(system)
Vtrue, Itrue, Iinjtrue, S1true, S2true, Sinjtrue, num_iter = nv_powerflow.solve(branch, node)
Vtrue_cim, num_iter_cim = nv_powerflow_cim.solve(system)
results.load_voltages(Vtrue_cim)
results.calculateI()
results.calculateIinj()
results.calculateSinj()
print("Vtrue==Vtrue_cim?: " + str((results.get_voltages()==Vtrue).all()))
print("Iinj==Iinj_cim?: " + str((results.get_Iinj()==Iinjtrue).all()))
print("Sinj==Sinj_cim?: " + str((results.get_Sinj()==Sinjtrue).all()))
\ No newline at end of file
......@@ -8,7 +8,7 @@ import matplotlib.pyplot as plt
sys.path.append("../../../cimpy")
import cimpy
sys.path.append("./acs/state_estimation")
sys.path.append("../../acs/state_estimation")
import network
import nv_powerflow_cim
from measurement_generator import *
......
Supports Markdown
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