From 691d8018b40ca9fcc1b05511ec327947be9934a1 Mon Sep 17 00:00:00 2001 From: Samuel Letellier-Duchesne Date: Mon, 3 May 2021 08:58:19 -0400 Subject: [PATCH] EquationCollections are now properly printed to dck file (#42) * Unit number positive for EquationCollection * Fixes header information for EquationCollection * fixes tests * Skips when meta is None --- tests/test_xml.py | 13 ------------- trnsystor/collections/equation.py | 25 ++++++++++++++----------- trnsystor/deck.py | 5 +++-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/tests/test_xml.py b/tests/test_xml.py index 3da2bf3..0b0c4dd 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -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 @@ -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.""" @@ -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 diff --git a/trnsystor/collections/equation.py b/trnsystor/collections/equation.py index 4745240..5a9f828 100644 --- a/trnsystor/collections/equation.py +++ b/trnsystor/collections/equation.py @@ -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. @@ -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.""" diff --git a/trnsystor/deck.py b/trnsystor/deck.py index c67c50b..cca4dcb 100644 --- a/trnsystor/deck.py +++ b/trnsystor/deck.py @@ -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 @@ -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