Passed
Pull Request — dev (#821)
by Uwe
02:17
created

solph.buses._bus   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 35
dl 0
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Bus.__init__() 0 3 1
A Bus.constraint_group() 0 5 2
A BusBlock.__init__() 0 2 1
B BusBlock._create() 0 32 6
1
# -*- coding: utf-8 -*-
2
3
"""
4
solph version of oemof.network.bus
5
6
SPDX-FileCopyrightText: Uwe Krien <[email protected]>
7
SPDX-FileCopyrightText: Simon Hilpert
8
SPDX-FileCopyrightText: Cord Kaldemeyer
9
SPDX-FileCopyrightText: Stephan Günther
10
SPDX-FileCopyrightText: Patrik Schönfeldt
11
SPDX-FileCopyrightText: Birgit Schachler
12
SPDX-FileCopyrightText: jnnr
13
SPDX-FileCopyrightText: jmloenneberga
14
15
SPDX-License-Identifier: MIT
16
17
"""
18
19
from oemof.network import network as on
20
from pyomo.core import BuildAction
21
from pyomo.core import Constraint
22
from pyomo.core.base.block import ScalarBlock
23
24
25
class Bus(on.Bus):
26
    """A balance object. Every node has to be connected to BusBlock.
27
28
    Notes
29
    -----
30
    The following sets, variables, constraints and objective parts are created
31
     * :py:class:`~oemof.solph.buses.bus.BusBlock`
32
33
    """
34
35
    def __init__(self, *args, **kwargs):
36
        super().__init__(*args, **kwargs)
37
        self.balanced = kwargs.get("balanced", True)
38
39
    def constraint_group(self):
40
        if self.balanced:
41
            return BusBlock
42
        else:
43
            return None
44
45
46
class BusBlock(ScalarBlock):
47
    r"""Block for all balanced buses.
48
49
    **The following constraints are build:**
50
51
    BusBlock balance  :attr:`om.BusBlock.balance[i, o, t]`
52
      .. math::
53
        \sum_{i \in INPUTS(n)} flow(i, n, t) =
54
        \sum_{o \in OUTPUTS(n)} flow(n, o, t), \\
55
        \forall n \in \textrm{BUSES},
56
        \forall t \in \textrm{TIMESTEPS}.
57
    """
58
59
    def __init__(self, *args, **kwargs):
60
        super().__init__(*args, **kwargs)
61
62
    def _create(self, group=None):
63
        """Creates the balance constraints for the class:`BusBlock` block.
64
65
        Parameters
66
        ----------
67
        group : list
68
            List of oemof bus (b) object for which the bus balance is created
69
            e.g. group = [b1, b2, b3, .....]
70
        """
71
        if group is None:
72
            return None
73
74
        m = self.parent_block()
75
76
        ins = {}
77
        outs = {}
78
        for n in group:
79
            ins[n] = [i for i in n.inputs]
80
            outs[n] = [o for o in n.outputs]
81
82
        def _busbalance_rule(block):
83
            for t in m.TIMESTEPS:
0 ignored issues
show
introduced by
The variable m does not seem to be defined for all execution paths.
Loading history...
84
                for g in group:
85
                    lhs = sum(m.flow[i, g, t] for i in ins[g])
0 ignored issues
show
introduced by
The variable ins does not seem to be defined for all execution paths.
Loading history...
86
                    rhs = sum(m.flow[g, o, t] for o in outs[g])
0 ignored issues
show
introduced by
The variable outs does not seem to be defined for all execution paths.
Loading history...
87
                    expr = lhs == rhs
88
                    # no inflows no outflows yield: 0 == 0 which is True
89
                    if expr is not True:
90
                        block.balance.add((g, t), expr)
91
92
        self.balance = Constraint(group, m.TIMESTEPS, noruleinit=True)
93
        self.balance_build = BuildAction(rule=_busbalance_rule)
94