Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/echo/models/agnostic/tellegen/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from attrs import define, field
from pyomo.core.expr import EqualityExpression

from echo.exceptions import validate
from echo.configuration import Flows, Units
from echo.exceptions import ConfigurationError, validate
from echo.models.base import Node, Port
from echo.models.scenario import EchoConcreteModel
from echo.validators import (
Expand Down Expand Up @@ -32,5 +33,30 @@ def verify_node(self) -> None:

validate(
len(self.ports) >= 2,
f"A tellegen node must have at least two ports. Offending node has the name: {self.node_name}",
f"A TellegenNode(id={self.uid}, name={self.node_name}) must have at least two ports.",
)

# We can't have all import or all export ports on a tellegennode
flows = {port.flows for port in self.ports.values()}
validate(
flows != {Flows.Import} and flows != {Flows.Export},
f"TellgenNode(id={self.uid}, name={self.node_name}) must have a mixture "
f"of import and export ports. Got: {flows}",
)

@property
def units(self) -> Units | None:
if self.ports:
first_port = list(self.ports.values())[0]
return first_port.units
return None

# Override parent's 'add_port' method so we can perform checks necessary to tellegennodes.
def add_port(self, name: str, port: Port) -> None:
if self.units and self.units != port.units:
raise ConfigurationError(
f"Port with units '{port.units}' must match "
f"TellegenNode(id={self.uid}, name={self.node_name}) units, '{self.units}'."
)

super().add_port(name=name, port=port)
10 changes: 6 additions & 4 deletions src/echo/models/base/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def inflow(self) -> str:
return f"inflow_{self.node_name}"

def add_port(self, name: str, port: Port) -> None:
if self.ports.get(name) is None:
self.ports[name] = port
else:
raise ConfigurationError(f"Port with name {name} is already defined on node {self.node_name}")
if self.ports.get(name) is not None:
raise ConfigurationError(
f"Port with name '{name}' is already defined on TellegenNode(id={self.uid}, name={self.node_name})."
)

self.ports[name] = port

def add_ports_from_list(self, names: Iterable[str], port_type: type[Port], **kwargs) -> None:
"""Creates a set of ports (using port_type) and adds them to this Node. The ports will be constructed
Expand Down
47 changes: 41 additions & 6 deletions tests/unit/models/agnostic/tellegen/test_tellegen_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,44 @@ def test_tellegennode_validation():
node = TellegenNode()
node.ports = ports

# This should raise an error due to mixing of different units on ports
# There is a corresponding [github issue](https://github.com/bsgip/echo/issues/126)
# with pytest.raises(ConfigurationError):
# node = TellegenNode()
# node.add_ports_from_list(["electrical1", "electical2", "electrical3"], Port, units=Units.KW)
# node.add_ports_from_list(["gas1", "gas2", "gas3"], Port, units=Units.JPS)

def test_tellegennode_add_ports_from_list_validates_port_units():
with pytest.raises(ConfigurationError):
node = TellegenNode()

# We can add the first three ports (all have same units)
node.add_ports_from_list(["electrical1", "electical2", "electrical3"], Port, units=Units.KW)
assert len(node.ports) == 3

# This fails because we are mixing units
node.add_ports_from_list(["gas1", "gas2", "gas3"], Port, units=Units.JPS)


def test_tellgennode_verify_node_validates_port_flow_direction():
# Flows all import not allowed
with pytest.raises(ConfigurationError):
node = TellegenNode()
node.add_ports_from_list(
["electrical1", "electical2", "electrical3"],
Port,
units=Units.KW,
flows=Flows.Import,
import_constraint=FlowConstraint.Fixed,
import_constraint_value=0,
flow_type=OptimisationType.Parameter,
)
node.verify_node()

# Flows all export not allowed
with pytest.raises(ConfigurationError):
node = TellegenNode()
node.add_ports_from_list(
["electrical1", "electical2", "electrical3"],
Port,
units=Units.KW,
flows=Flows.Export,
export_constraint=FlowConstraint.Fixed,
export_constraint_value=0,
flow_type=OptimisationType.Parameter,
)
node.verify_node()
Loading