powerflow/volts.py

00001 #!/usr/bin/python
00002 
00003 import sys
00004 from math import *
00005 from xml.dom.minidom import parse
00006 from xml.dom import Node
00007 
00008 
00009 class Object:
00010     def __init__(self, **props):
00011         self.__dict__ = props
00012 
00013 
00014 def quadrant(x, y):
00015     if x >= 0:
00016         if y >= 0: return 0
00017         else: return 3
00018     else:
00019         if y >= 0: return 1
00020         else: return 2
00021 
00022 # convert line-to-line voltage/angle to cartesian phase (line-to-neutral) voltage
00023 def CPV(V, angle):
00024     rads = radians(angle)
00025     return (V / sqrt(3.0)) * cos(rads), (V / sqrt(3.0)) * sin(rads)
00026 
00027 # convert cartesian voltage to angular voltage
00028 def AV(x, y):
00029     if x:
00030         angle = atan(y / x)
00031     else:
00032         angle = 0.0
00033     V = sqrt(x**2 + y**2)
00034     return (V, [0, 180, -180, 0][quadrant(x, y)] + degrees(angle))
00035 
00036 power = lambda kW, pf: complex(kW * 1000.0, tan(acos(pf)) * kW * 1000.0)
00037 
00038 def stringify(val):
00039     if not val:
00040         return ''
00041     else:
00042         return str(val)
00043 
00044 def get_nodes(path):
00045     objs = []
00046     doc = parse(path)
00047     nodes = [e for e in doc.getElementsByTagName('object') if str(e.getAttribute('type')) in ('node', 'load')]
00048     for node in nodes:
00049         name = node.getAttribute('name')
00050         props = dict([(str(n.tagName), stringify(n.firstChild.nodeValue)) for n in node.childNodes if n.nodeType == Node.ELEMENT_NODE and n.firstChild and n.firstChild.nodeType == Node.TEXT_NODE])
00051         for k in props:
00052             if k.endswith('_V'):
00053                 if props[k]:
00054                     props[k] = complex(props[k])
00055                 else:
00056                     props[k] = complex(0.0)
00057         objs.append(Object(name=name, **props))
00058     objs.sort(cmp=lambda x, y: cmp(y.rank, x.rank))
00059     return objs
00060 
00061 def main():
00062     objs = get_nodes(sys.argv[1])
00063     i = 1
00064     for obj in objs:
00065         if 'N' in obj.phases:
00066             V1 = obj.phaseA_V
00067             V2 = obj.phaseB_V
00068             V3 = obj.phaseC_V
00069         else:
00070             V1 = (obj.phaseA_V) - (obj.phaseB_V)
00071             V2 = (obj.phaseB_V) - (obj.phaseC_V)
00072             V3 = (obj.phaseC_V) - (obj.phaseA_V)
00073         print 'Node %s   '%i, obj.name
00074         print '     V1   %0.1f/%0.1f'%AV(V1.real, V1.imag)
00075         print '     V2   %0.1f/%0.1f'%AV(V2.real, V2.imag)
00076         print '     V3   %0.1f/%0.1f'%AV(V3.real, V3.imag)
00077         i += 1
00078 
00079 if __name__ == '__main__':
00080     main()

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy