Skip to content

Commit

Permalink
Merge adding subnodes via signal into master
Browse files Browse the repository at this point in the history
  • Loading branch information
gnn committed Jul 8, 2019
2 parents a0a4729 + 311a32e commit 580742d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
48 changes: 30 additions & 18 deletions src/oemof/tabular/facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
SPDX-License-Identifier: BSD-3-Clause
"""
from collections import deque

from oemof.energy_system import EnergySystem
from oemof.network import Node
from oemof.solph import Bus, Flow, Investment, Sink, Source, Transformer
from oemof.solph.components import ExtractionTurbineCHP, GenericStorage
from oemof.solph.custom import ElectricalBus, ElectricalLine, Link
from oemof.solph.plumbing import sequence


def add_subnodes(n, **kwargs):
deque((kwargs["EnergySystem"].add(sn) for sn in n.subnodes), maxlen=0)


class Facade(Node):
"""
Parameters
Expand All @@ -47,7 +54,12 @@ def __init__(self, *args, **kwargs):
required = kwargs.pop("_facade_requires_", [])

super().__init__(*args, **kwargs)

self.subnodes = []
EnergySystem.signals[EnergySystem.add].connect(
add_subnodes, sender=self
)

for r in required:
if r in kwargs:
setattr(self, r, kwargs[r])
Expand Down Expand Up @@ -85,7 +97,8 @@ def _investment(self):
maximum=getattr(
self,
"storage_capacity_potential",
float("+inf")),
float("+inf"),
),
minimum=getattr(
self, "minimum_storage_capacity", 0
),
Expand All @@ -99,7 +112,7 @@ def _investment(self):
maximum=getattr(
self, "capacity_potential", float("+inf")
),
existing=getattr(self, "capacity", 0)
existing=getattr(self, "capacity", 0),
)
else:
self.investment = None
Expand Down Expand Up @@ -242,8 +255,7 @@ def build_solph_components(self):
self.outputs.update(
{
self.bus: Flow(
nominal_value=self.capacity,
**self.output_parameters
nominal_value=self.capacity, **self.output_parameters
)
}
)
Expand Down Expand Up @@ -454,8 +466,9 @@ class Volatile(Source, Facade):
"""

def __init__(self, *args, **kwargs):
kwargs.update({"_facade_requires_": ["bus", "carrier", "tech",
"profile"]})
kwargs.update(
{"_facade_requires_": ["bus", "carrier", "tech", "profile"]}
)
super().__init__(*args, **kwargs)

self.profile = kwargs.get("profile")
Expand All @@ -472,7 +485,7 @@ def __init__(self, *args, **kwargs):

self.output_parameters = kwargs.get("output_parameters", {})

self.fixed = bool(kwargs.get('fixed', True))
self.fixed = bool(kwargs.get("fixed", True))

self.build_solph_components()

Expand Down Expand Up @@ -802,7 +815,7 @@ def build_solph_components(self):
{
self.electricity_bus: Flow(
nominal_value=self._nominal_value(),
investment=self._investment()
investment=self._investment(),
),
self.heat_bus: Flow(),
}
Expand Down Expand Up @@ -1088,10 +1101,12 @@ def __init__(self, *args, **kwargs):
self.storage_capacity_cost = kwargs.get("storage_capacity_cost")

self.storage_capacity_potential = kwargs.get(
"storage_capacity_potential", float("+inf"))
"storage_capacity_potential", float("+inf")
)

self.capacity_potential = kwargs.get(
"capacity_potential", float("+inf"))
"capacity_potential", float("+inf")
)

self.expandable = bool(kwargs.get("expandable", False))

Expand Down Expand Up @@ -1120,9 +1135,7 @@ def build_solph_components(self):
if self.investment:
self.invest_relation_input_output = 1

for attr in [
"invest_relation_input_output",
]:
for attr in ["invest_relation_input_output"]:
if getattr(self, attr) is None:
raise AttributeError(
(
Expand All @@ -1135,7 +1148,7 @@ def build_solph_components(self):
investment=Investment(
ep_costs=self.capacity_cost,
maximum=self.capacity_potential,
existing=self.capacity
existing=self.capacity,
),
**self.input_parameters
)
Expand All @@ -1149,8 +1162,8 @@ def build_solph_components(self):
self._invest_group = True
else:
fi = Flow(
nominal_value=self._nominal_value(),
**self.input_parameters)
nominal_value=self._nominal_value(), **self.input_parameters
)
fo = Flow(
nominal_value=self._nominal_value(),
variable_costs=self.marginal_cost,
Expand Down Expand Up @@ -1243,8 +1256,7 @@ def build_solph_components(self):
investment=investment,
),
self.to_bus: Flow(
nominal_value=self._nominal_value(),
investment=investment
nominal_value=self._nominal_value(), investment=investment
),
}
)
Expand Down
29 changes: 28 additions & 1 deletion tests/test_oemof_tabular.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from oemof.network import Bus
from oemof.energy_system import EnergySystem
import oemof

from oemof.tabular.facades import Reservoir
import oemof.tabular


Expand All @@ -13,5 +16,29 @@ def test_version_specification():
def test_project_name():
""" `oemof.tabular`'s project name is importable and correct.
"""
assert (oemof.tabular.__project__ == 'oemof.tabular')
assert oemof.tabular.__project__ == "oemof.tabular"


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

0 comments on commit 580742d

Please sign in to comment.