Skip to content

Commit

Permalink
EquationCollections are now properly printed to dck file (#42)
Browse files Browse the repository at this point in the history
* Unit number positive for EquationCollection

* Fixes header information for EquationCollection

* fixes tests

* Skips when meta is None
  • Loading branch information
Samuel Letellier-Duchesne committed May 3, 2021
1 parent 422cd56 commit 691d801
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
13 changes: 0 additions & 13 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,6 @@ def constant_block(self):

yield c_block

def test_unit_number(self, equation_block):
"""Equation block unit numbers are negative."""
assert equation_block.unit_number < 0

def test_symbolic_expression(self, tank_type, fan_type):
from trnsystor.statement.constant import Constant

Expand Down Expand Up @@ -717,11 +713,6 @@ def test_equation_with_typevariable(self, fan_type):
equa1 = Equation("T_out", fan_type.outputs[0])
equa_block = EquationCollection([equa1])
assert str(equa1) == "T_out = [1, 1]"
assert (
str(equa_block) == '* EQUATIONS "None"\n\nEQUATIONS '
"1\nT_out = ["
"1, 1]"
)

def test_two_unnamed_equationcollection(self, fan_type):
"""Make sure objects with same name=None can be created."""
Expand Down Expand Up @@ -784,10 +775,6 @@ def test_eq_one_based_idx(self, equation_block):
for equation in equation_block.values():
assert equation.one_based_idx >= 1

def test_eq_unit_number(self, equation_block):
for equation in equation_block.values():
assert equation.unit_number < 1

def test_eq_is_connected(self, equation_block):
for equation in equation_block.values():
assert equation.is_connected is False
Expand Down
25 changes: 14 additions & 11 deletions trnsystor/collections/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ def size(self):
"""Return len(self)."""
return len(self)

@property
def unit_number(self):
"""Return the unit_number of self. Negative by design.
Hint:
Only :class:`TrnsysModel` objects have a positive unit_number.
"""
return self._unit * -1

@property
def unit_name(self):
"""Return ``name`` of self.
Expand All @@ -155,11 +146,23 @@ def _to_deck(self):
NAMEn = ... equation n ...
"""
header_comment = '* EQUATIONS "{}"\n\n'.format(self.name)
header_comment = '* EQUATIONS "{}"\n*\n'.format(self.name)
head = "EQUATIONS {}\n".format(len(self))
v_ = ((equa.name, "=", equa._to_deck()) for equa in self.values())
core = tabulate.tabulate(v_, tablefmt="plain", numalign="left")
return str(header_comment) + str(head) + str(core)

def _studio(self):
"""Return deck representation of self."""
unit_name = "*$UNIT_NAME {}".format(self.unit_name)
layer = "*$LAYER {}".format(" ".join(self.studio.layer))
position = "*$POSITION {} {}".format(
self.studio.position.x, self.studio.position.y
)
unit_number = "*UNIT_NUMBER {}".format(self.unit_number)
return "\n" + "\n".join([unit_name, layer, position, unit_number]) + "\n"

tail = _studio(self)
return str(header_comment) + str(head) + str(core) + str(tail)

def _get_inputs(self):
"""Sort by order number each time it is called."""
Expand Down
5 changes: 3 additions & 2 deletions trnsystor/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def _parse_logic(cls, cc, dck, dcklines, line, proforma_root):
dck.update_models(component)
else:
pass
if key in ("parameters", "inputs"):
if key in ("parameters", "inputs") and component._meta is not None:
if component._meta.variables:
n_vars = int(match.group(key).strip())
init_at = n_vars
Expand Down Expand Up @@ -549,7 +549,8 @@ def distance(a, b):
f"at {proforma_root}"
)
meta = MetaData.from_xml(xml)
component.update_meta(meta)
if isinstance(component, TrnsysModel):
component.update_meta(meta)

line = dcklines.readline()
return line
Expand Down

0 comments on commit 691d801

Please sign in to comment.