Commits

Stefan Scherfke committed c6774db

Obtaining the voltage levels works.

Comments (0)

Files changed (2)

cim2busbranch/cim2bb.py

 
         """
         cim_objects = RDFXMLReader.cimread(self.cim_file)
-        base_power, tnodes, branches = self._iterate_prim_eq(cim_objects)
-        self._create_bb_topology(base_power, tnodes, branches)
+        base_power, tnodes, lines = self._iterate_prim_eq(cim_objects)
+        self._create_bb_topology(base_power, tnodes, lines)
 
-    def _create_bb_topology(self, base_power, tnodes, branches):
+    def _create_bb_topology(self, base_power, tnodes, lines):
         """
         Creates a :class:`topology.Case` from the identified *base_power*,
-        *tnodes* (topological nodes) and *branches*.
+        *tnodes* (topological nodes) and *lines*.
 
         """
-        buses = self._create_buses(tnodes)
+        buses, generators, twindings = self._create_buses(tnodes)
+        branches = self._create_branches(lines, twindings)
+
+        assert isinstance(buses, list)  # TODO: Remove
+        return topology.Case(base_power, buses, generators, branches)
+
+    def _create_branches(self, lines, twindings):
+        """
+        Creates and returns :class:`topology.Branch` instances for the *lines*
+        and Transformers (*twindings*).
+
+        """
+        return []
 
     def _create_buses(self, tnodes):
+        """
+        Creates and returns a :class:`topology.Bus` for each topologial node in
+        *tnodes* and handels all connected primary equipment to parameterize
+        the bus and/or creat :class:`topology.Generator` instances.
+
+        Transformer windings are ignored and handled by
+        :meth:`_create_branches`.
+
+        """
         buses = {}
         swingbus = None
 
             btype = topology.bus_types.PQ
             cim_classes = []
 
-            base_voltage = self._get_base_voltage(tnode).nominalVoltage
+            base_voltage = self._get_base_voltage(tnode)
 
             for equip in prim_equip:
                 equip_cls = equip.__class__.__name__
                     swingbus = equip_cls
                     btype = topology.bus_types.REF
 
+                # Todo: Check other equipment classes
 
             # TODO: Check for loads and generators
             buses[cn.mRID] = topology.Bus(
 
         """
         if tnode.BaseVoltage:
-            return tnode.BaseVoltage
+            return tnode.BaseVoltage.nominalVoltage
 
         if (hasattr(tnode.ConnectivityNodeContainer, 'BaseVoltage') and
                 tnode.ConnectivityNodeContainer.BaseVoltage):
-            return tnode.ConnectivityNodeContainer.BaseVoltage
+            return tnode.ConnectivityNodeContainer.BaseVoltage.nominalVoltage
 
         base_voltage = None
         for cn in tnode.ConnectivityNodes:
-            bv = cn.ConnectivityNodeContainer.BaseVoltage
+            if not cn.ConnectivityNodeContainer or not hasattr(
+                    cn.ConnectivityNodeContainer, 'BaseVoltage'):
+                continue
+
+            bv = cn.ConnectivityNodeContainer.BaseVoltage.nominalVoltage
             if not bv:
                 continue
 
             return base_voltage
 
         raise ValueError('No base voltage found for topo. node (%s)' %
-                ', '.join(tnode.ConnectivityNodes))
+                ', '.join(cn.mRID for cn in tnode.ConnectivityNodes))
 
     def _iterate_prim_eq(self, cim_objects):
         """
         """
         processed = set()  # Contain all proccessed connectivity nodes
         tnode_equipment = {}  # Top. nodes and adjacent equipment
-        branches = set()  # Contains all branches
+        lines = set()  # Contains all lines
         base_power = None
 
         BasePower = self._get_cls('BasePower')
             elif isinstance(obj, self._prim_twot):
                 assert len(obj.Terminals) == 2, ('%s has %d terminals, '
                         'but should only have 2.' % (obj, len(obj.Terminals)))
-                branches.add(obj)
+                lines.add(obj)
 
             # else: continue
 
-        return base_power, tnode_equipment, branches
+        return base_power, tnode_equipment, lines
 
     def _process_cnode(self, tnode, src_terminal, processed):
         """

cim2busbranch/test/test_cim2bb.py

     ]
 
 
+def test_get_base_voltage(converter, results):
+    get_cls = converter._get_cls
+    TopologicalNode = get_cls('TopologicalNode')
+    ConnectivityNode = get_cls('ConnectivityNode')
+    VoltageLevel = get_cls('VoltageLevel')
+    BaseVoltage = get_cls('BaseVoltage')
+
+    base_voltage = BaseVoltage(nominalVoltage=0.4)
+    voltage_level = VoltageLevel(BaseVoltage=base_voltage)
+
+    cnodes = [ConnectivityNode() for i in range(3)]
+    tnode = TopologicalNode(ConnectivityNodes=cnodes)
+
+    pytest.raises(ValueError, converter._get_base_voltage, tnode)
+
+    tnode.BaseVoltage = base_voltage
+    assert converter._get_base_voltage(tnode) == base_voltage.nominalVoltage
+    tnode.BaseVoltage = None
+
+    tnode.ConnectivityNodeContainer = voltage_level
+    assert converter._get_base_voltage(tnode) == base_voltage.nominalVoltage
+    tnode.ConnectivityNodeContainer = None
+
+    for cn in cnodes:
+        cn.ConnectivityNodeContainer = voltage_level
+    assert converter._get_base_voltage(tnode) == base_voltage.nominalVoltage
+
+    cnodes[0].ConnectivityNodeContainer = VoltageLevel(
+            BaseVoltage=BaseVoltage(nominalVoltage=20))
+    pytest.raises(ValueError, converter._get_base_voltage, tnode)
+
+
 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 branch.__class__.__name__ == 'ACLineSegment'
 
 
-def test__process_cnode():
-    pytest.skip()
+def test_process_cnode():
+    # _process_cnode() is covered by test_iterate_primary_equipment()
+    assert True
 
 
 def test_get_equipment_cls(converter):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.