Commits

Stefan Scherfke committed 3a47762

Some more work on the cim2bb algorithm.

  • Participants
  • Parent commits 7623df3

Comments (0)

Files changed (4)

File cim2busbranch/cim2bb.py

         *tnodes* (topological nodes) and *branches*.
 
         """
+        buses = self._create_buses(tnodes)
+
+    def _create_buses(self, tnodes):
+        buses = {}
+        swingbus = None
+
         for tnode, prim_equip in tnodes.items():
-            print tnode
-            print len(prim_equip), prim_equip
+            btype = topology.bus_types.PQ
+            cim_classes = []
+
+            base_voltage = self._get_base_voltage(tnode).nominalVoltage
+
+            for equip in prim_equip:
+                equip_cls = equip.__class__.__name__
+                cim_classes.append(equip_cls)
+
+                if (equip_cls is 'BusbarSection' and
+                        equip.aliasName is 'SwingBus'):
+                    if swingbus:
+                        raise RuntimeError('Only one swing bus is allowed. '
+                                'Found %s and %s.' % (swingbus, equip))
+
+                    swingbus = equip_cls
+                    btype = topology.bus_types.REF
+
+
+            # TODO: Check for loads and generators
+            buses[cn.mRID] = topology.Bus(
+                    name=(cn.name or cn.mRID),
+                    btype=btype,
+                    v_base=base_voltage.nominalVoltage,
+                    p_demand=0.0,
+                    q_demand=0.0)
+            buses[cn.mRID].pos = _get_position(cn)
+
+        assert swingbus, 'No swing bus found.'
+
+    def _get_base_voltage(self, tnode):
+        """
+        Tries to get a base voltage value from *tnode* (a topological node) or
+        from it’s connectivity nodes.
+
+        Raises a :class:`ValueError` if no base voltage can be found or if
+        the base voltages of the c. nodes deffer from each other.
+
+        """
+        if tnode.BaseVoltage:
+            return tnode.BaseVoltage
+
+        if (hasattr(tnode.ConnectivityNodeContainer, 'BaseVoltage') and
+                tnode.ConnectivityNodeContainer.BaseVoltage):
+            return tnode.ConnectivityNodeContainer.BaseVoltage
+
+        base_voltage = None
+        for cn in tnode.ConnectivityNodes:
+            bv = cn.ConnectivityNodeContainer.BaseVoltage
+            if not bv:
+                continue
+
+            if not base_voltage:
+                base_voltage = bv
+            elif bv != base_voltage:
+                raise ValueError('Base voltage %s of %s deffers from %s' %
+                        (bv, cn, base_voltage))
+
+        if base_voltage:
+            return base_voltage
+
+        raise ValueError('No base voltage found for topo. node (%s)' %
+                ', '.join(tnode.ConnectivityNodes))
 
     def _iterate_prim_eq(self, cim_objects):
         """

File cim2busbranch/test/data/TestCaseB.xml

 
     <cim:ConnectivityNode rdf:ID="Transformer20kV_C">
         <cim:IdentifiedObject.mRID>Transformer20kV_C</cim:IdentifiedObject.mRID>
-        <cim:IdentifiedObject.aliasName>SwingBus</cim:IdentifiedObject.aliasName>
         <cim:ConnectivityNode.ConnectivityNodeContainer rdf:resource="#VoltageLevel20kV"/>
         <cim:ConnectivityNode.Terminals rdf:resource="#Transformer20kV_T"/>
         <cim:ConnectivityNode.Terminals rdf:resource="#Industry_2_T"/>
     </cim:ConnectivityNode>
     <cim:BusbarSection rdf:ID="Transformer20kV">
         <cim:IdentifiedObject.mRID>Transformer20kV</cim:IdentifiedObject.mRID>
+        <cim:IdentifiedObject.aliasName>SwingBus</cim:IdentifiedObject.aliasName>
         <cim:ConductingEquipment.Terminals rdf:resource="#Transformer20kV_T"/>
     </cim:BusbarSection>
     <cim:Terminal rdf:ID="Transformer20kV_T">

File cim2busbranch/test/test_cim2bb.py

 def test_iterate_primary_equipment(converter, results):
     cim_objects = RDFXMLReader.cimread(converter.cim_file)
     base_power, tnodes, branches = converter._iterate_prim_eq(cim_objects)
+
     assert base_power == results['base_power']
+
     assert len(tnodes) == results['num_tnodes']
-    for tnode in tnodes:
+    for tnode, equip in tnodes.items():
         assert tnode.__class__.__name__ == 'TopologicalNode'
+        assert len(equip) >= 1
+
     assert len(branches) == results['num_branches']
     for branch in branches:
         assert branch.__class__.__name__ == 'ACLineSegment'

File cim2busbranch/topology.py

     """
     def __init__(self, name='', btype=bus_type.PQ, pd=0., qd=0.,
                 base_kv=100., vm=1., va=0., vm_max=1.1, vm_min=0.9,
-                gs=0., bs=0., area=1, zone=1):
+                gs=0., bs=0., area=1, zone=1, cim_classes=None, pos=None):
 
         self.name = name if name else str(id(self))
         """Bus name"""
         self.zone = zone
         """Loss zone (positive integer)"""
 
+        self.cim_classes = cim_classes if cim_classes else []
+        """Classes of CIM components that were connected to this bus."""
+
+        self.pos = pos
+        """Position (x, y) of that bus."""
+
         self._pd_orig = pd
         self._qd_orig = qd