solph._groupings   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 39
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A constraint_grouping() 0 28 2
A _invest_non_convex_grouping() 0 5 4
A _nonconvex_grouping() 0 5 4
A _investment_grouping() 0 5 4
1
# -*- coding: utf-8 -*-
2
3
"""Groupings needed on an energy system for it to work with solph.
4
5
If you want to use solph on an energy system, you need to create it with these
6
groupings specified like this:
7
8
    .. code-block:: python
9
10
        from oemof.network import EnergySystem
11
        import oemof.solph as solph
12
13
        energy_system = EnergySystem(groupings=solph.GROUPINGS)
14
15
SPDX-FileCopyrightText: Uwe Krien <[email protected]>
16
SPDX-FileCopyrightText: Simon Hilpert
17
SPDX-FileCopyrightText: Cord Kaldemeyer
18
SPDX-FileCopyrightText: Stephan Günther
19
SPDX-FileCopyrightText: Saeed Sayadi
20
SPDX-FileCopyrightText: Johannes Kochems
21
22
SPDX-License-Identifier: MIT
23
"""
24
25
from oemof.network import groupings as groupings
26
27
from oemof.solph.flows._invest_non_convex_flow_block import (
28
    InvestNonConvexFlowBlock,
29
)
30
from oemof.solph.flows._investment_flow_block import InvestmentFlowBlock
31
from oemof.solph.flows._non_convex_flow_block import NonConvexFlowBlock
32
from oemof.solph.flows._simple_flow_block import SimpleFlowBlock
33
34
35
def constraint_grouping(node, fallback=lambda *xs, **ks: None):
36
    """Grouping function for constraints.
37
38
    This function can be passed in a list to :attr:`groupings` of
39
    :class:`oemof.solph.network.EnergySystem`.
40
41
    Parameters
42
    ----------
43
    node : :class:`Node <oemof.network.Node`
44
        The node for which the figure out a constraint group.
45
    fallback : callable, optional
46
        A function of one argument. If `node` doesn't have a `constraint_group`
47
        attribute, this is used to group the node instead. Defaults to not
48
        group the node at all.
49
    """
50
    # TODO: Refactor this for looser coupling between modules.
51
    # This code causes an unwanted tight coupling between the `groupings` and
52
    # `network` modules, resulting in having to do an import at runtime in the
53
    # init method of solph's `EnergySystem`. A better way would be to add a
54
    # method (maybe `constraints`, `constraint_group`, `constraint_type` or
55
    # something like that) to solph's node hierarchy, which gets overridden in
56
    # each subclass to return the appropriate value. Then we can just call the
57
    # method here.
58
    # This even gives other users/us the ability to customize/extend how
59
    # constraints are grouped by overriding the method in future subclasses.
60
61
    cg = getattr(node, "constraint_group", fallback)
62
    return cg()
63
64
65
standard_flow_grouping = groupings.FlowsWithNodes(constant_key=SimpleFlowBlock)
66
67
68
def _investment_grouping(stf):
69
    if hasattr(stf[2], "investment"):
70
        if stf[2].investment is not None and stf[2].nonconvex is None:
71
            return True
72
    return False
73
74
75
investment_flow_grouping = groupings.FlowsWithNodes(
76
    constant_key=InvestmentFlowBlock,
77
    # stf: a tuple consisting of (source, target, flow), so stf[2] is the flow.
78
    filter=_investment_grouping,
79
)
80
81
82
def _nonconvex_grouping(stf):
83
    if hasattr(stf[2], "nonconvex"):
84
        if stf[2].nonconvex is not None and stf[2].investment is None:
85
            return True
86
    return False
87
88
89
nonconvex_flow_grouping = groupings.FlowsWithNodes(
90
    constant_key=NonConvexFlowBlock, filter=_nonconvex_grouping
91
)
92
93
94
def _invest_non_convex_grouping(stf):
95
    if hasattr(stf[2], "nonconvex"):
96
        if stf[2].investment is not None and stf[2].nonconvex is not None:
97
            return True
98
    return False
99
100
101
invest_non_convex_flow_grouping = groupings.FlowsWithNodes(
102
    constant_key=InvestNonConvexFlowBlock, filter=_invest_non_convex_grouping
103
)
104
105
GROUPINGS = [
106
    constraint_grouping,
107
    investment_flow_grouping,
108
    standard_flow_grouping,
109
    nonconvex_flow_grouping,
110
    invest_non_convex_flow_grouping,
111
]
112