Exemple #1
0
 def test_node_registration(self):
     eq_(Node.registry, self.es)
     b1 = Bus(label='<B1>')
     eq_(self.es.entities[0], b1)
     b2 = Bus(label='<B2>')
     Transformer(label='<TF1>', inputs=[b1], outputs=[b2])
     ok_(isinstance(self.es.entities[2], Transformer))
Exemple #2
0
    def test_grouping_laziness(self):
        """ Energy system `groups` should be fully lazy.

        `Node`s added to an energy system should only be tested for and put
        into their respective groups right before the `groups` property of an
        energy system is accessed.
        """
        group = "Group"
        g = Nodes(key=group, filter=lambda n: getattr(n, "group", False))
        self.es = es.EnergySystem(groupings=[g])
        buses = [Bus("Grouped"), Bus("Ungrouped one"), Bus("Ungrouped two")]
        self.es.add(buses[0])
        buses[0].group = True
        self.es.add(*buses[1:])
        ok_(
            group in self.es.groups,
            "\nExpected to find\n\n  `{!r}`\n\nin `es.groups`.\nGot:\n\n  `{}`"
            .format(
                group,
                "\n   ".join(pformat(set(self.es.groups.keys())).split("\n")),
            ),
        )
        ok_(
            buses[0] in self.es.groups[group],
            "\nExpected\n\n  `{}`\n\nin `es.groups['{}']`:\n\n  `{}`".format(
                "\n   ".join(pformat(buses[0]).split("\n")), group,
                "\n   ".join(pformat(self.es.groups[group]).split("\n"))),
        )
Exemple #3
0
 def test_node_registration(self):
     eq_(Node.registry, self.es)
     b1 = Bus(label='<B1>')
     eq_(self.es.entities[0], b1)
     b2 = Bus(label='<B2>')
     eq_(self.es.entities[1], b2)
     t1 = Transformer(label='<TF1>', inputs=[b1], outputs=[b2])
     ok_(t1 in self.es.entities)
Exemple #4
0
 def test_entity_registration(self):
     bus = Bus(label='bus-uid', type='bus-type')
     eq_(self.es.nodes[0], bus)
     bus2 = Bus(label='bus-uid2', type='bus-type')
     Transformer(label='pp_gas', inputs=[bus], outputs=[bus2])
     ok_(isinstance(self.es.nodes[2], Transformer))
     self.es.timeindex = self.timeindex
     ok_(len(self.es.timeindex) == 5)
Exemple #5
0
    def test_that_nodes_do_not_get_undead_flows(self):
        """ Newly created nodes should only have flows assigned to them.

        A new node `n`, which re-used a previously used label `l`, retained the
        flows of those nodes which where labeled `l` before `n`. This incorrect
        behaviour is a problem if somebody wants to use different nodes with
        the same label in multiple energy systems. While this feature currently
        isn't used, it also lead to weird behaviour when running tests.

        This test ensures that new nodes only have those flows which are
        assigned to them on construction.
        """
        # We don't want a registry as we are re-using a label on purpose.
        # Having a registry would just throw and error generated by the DEFAULT
        # grouping in this case.
        Node.registry = None
        flow = object()
        old = Node(label="A reused label")
        bus = Bus(label="bus", inputs={old: flow})
        eq_(bus.inputs[old].flow, flow,
            ("\n  Expected: {}" +
             "\n  Got     : {} instead").format(flow, bus.inputs[old].flow))
        eq_(old.outputs[bus].flow, flow,
            ("\n  Expected: {}" +
             "\n  Got     : {} instead").format(flow, old.outputs[bus].flow))
        new = Node(label="A reused label")
        eq_(new.outputs, {},
            ("\n  Expected an empty dictionary of outputs." +
             "\n  Got: {} instead").format(new.outputs))
Exemple #6
0
    def test_modifying_inputs_after_construction(self):
        """ One should be able to add and delete inputs of a node.
        """
        node = Node("N1")
        bus = Bus("N2")
        flow = "flow"

        eq_(node.inputs, {},
            ("\n  Expected an empty dictionary of inputs." +
             "\n  Got: {} (== {}) instead").format(
                node.inputs,
                dict(node.inputs)))
        node.inputs[bus] = flow
        eq_(node.inputs, {bus: flow},
            ("\n  Expected {} as `node.inputs`." +
             "\n  Got    : {} (== {}) instead").format(
                {bus: flow}, node.inputs, dict(node.inputs)))
        eq_(node.inputs[bus], flow,
            ("\n  Expected {} as `node.inputs[bus]`." +
             "\n  Got    : {} instead").format(flow, node.inputs[bus]))
        del node.inputs[bus]
        eq_(node.inputs, {},
            ("\n  Expected an empty dictionary of inputs." +
             "\n  Got: {} (== {}) instead").format(
                node.inputs,
                dict(node.inputs)))
    def test_bus_to_sink_outputs_in_results_dataframe(self):
        bus = Bus(uid="bus")
        source = FS(
            label="source",
            outputs={bus: Flow(nominal_value=1, actual_value=0.5, fixed=True)})
        sink = Sink(label="sink", inputs={bus: Flow(nominal_value=1)})

        es = self.es
        om = OM(es)
        es.results = om.results()
        es.results[bus][sink] = [0.7]
        rdf = RDF(energy_system=es)
        try:
            eq_(
                rdf.loc[(slice(None), slice(None), slice(None),
                         "sink"), :].val[0], 0.7,
                "Output from bus to sink does not have the correct value.")
        except KeyError:
            self.failed = True
        if self.failed:
            ok_(
                False,
                "Output from bus to sink does not appear in results dataframe."
            )

        es.results[bus][bus] = [-1]
        rdf = RDF(energy_system=es)
        try:
            eq_(
                rdf.loc[(slice(None), slice(None), slice(None),
                         "sink"), :].val[0], 0.7,
                "Output from bus to sink does not have the correct value.")
        except KeyError:
            self.failed = True
        if self.failed:
            ok_(
                False, "Output from bus (with duals) to sink " +
                "does not appear in results dataframe.")
Exemple #8
0
def test_adding_subnodes():
    """ `Facade` subclasses correctly `connect` to and handle `add`.
    """
    es = EnergySystem()
    reservoir = Reservoir(
        label="r",
        bus=Bus("bus"),
        storage_capacity=1000,
        capacity=50,
        inflow=[1, 2, 6],
        loss_rate=0.01,
        initial_storage_level=0,
        max_storage_level=0.9,
        efficiency=0.93,
        carrier="carrier",
        tech="tech",
        profile="profile",
    )
    for sn in reservoir.subnodes:
        assert sn.label not in es.groups
    es.add(reservoir)
    for sn in reservoir.subnodes:
        assert sn.label in es.groups
    def test_issue_74(self):
        Storage.optimization_options.update({'investment': True})
        bus = Bus(uid="bus")
        store = Storage(uid="store",
                        inputs=[bus],
                        outputs=[bus],
                        c_rate_out=0.1,
                        c_rate_in=0.1)
        sink = Sink(uid="sink", inputs=[bus], val=[1])

        es = self.es
        om = OM(es)
        om.objective.set_value(-1)
        es.results = om.results()

        try:
            es.dump()
        except AttributeError as ae:
            self.failed = ae
        if self.failed:
            ok_(
                False,
                "EnergySystem#dump should not raise `AttributeError`: \n" +
                " Error message: " + str(self.failed))
Exemple #10
0
 def test_that_nodes_is_a_proper_alias_for_entities(self):
     b1, b2 = Bus(label="B1"), Bus(label="B2")
     eq_(self.es.nodes, [b1, b2])
     empty = []
     self.es.nodes = empty
     ok_(self.es.entities is empty)
Exemple #11
0
 def test_entity_grouping_on_construction(self):
     bus = Bus(label="test bus")
     ES = es.EnergySystem(entities=[bus])
     ok_(ES.groups[bus.label] is bus)