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