From d48d5cec5930b3e26afb574009485f5dcf36a1e9 Mon Sep 17 00:00:00 2001 From: Stephen Smith Date: Thu, 29 Aug 2024 15:38:44 +0100 Subject: [PATCH 1/2] correct string start value --- examples/demoslave.py | 5 ++++- pythonfmu3/tests/test_variables.py | 4 +++- pythonfmu3/variables.py | 20 +++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/examples/demoslave.py b/examples/demoslave.py index 6b8bc93..7744801 100644 --- a/examples/demoslave.py +++ b/examples/demoslave.py @@ -1,4 +1,4 @@ -from pythonfmu3 import Fmi3Causality, Fmi3Variability, Fmi3Slave, Float64 +from pythonfmu3 import Fmi3Causality, Fmi3Variability, Fmi3Slave, Float64, String class Resistor(Fmi3Slave): @@ -19,6 +19,7 @@ def __init__(self, **kwargs): self.delta_v = 10. self.i = 0.001 self.R = 10000. + self.stringVariable = "mine" self.register_variable(Float64("time", causality=Fmi3Causality.independent, variability=Fmi3Variability.continuous)) self.register_variable(Float64("R", causality=Fmi3Causality.parameter, variability=Fmi3Variability.tunable)) @@ -31,6 +32,8 @@ def __init__(self, **kwargs): self.register_variable(Float64("delta_v", causality=Fmi3Causality.local)) self.register_variable(Float64("i", causality=Fmi3Causality.local)) + self.register_variable(String("stringVariable", causality=Fmi3Causality.parameter)) + def do_step(self, current_time, step_size): self.delta_v = self.positive_pin_v - self.negative_pin_v self.i = i = self.delta_v / self.R diff --git a/pythonfmu3/tests/test_variables.py b/pythonfmu3/tests/test_variables.py index 7adfe39..a2805c3 100644 --- a/pythonfmu3/tests/test_variables.py +++ b/pythonfmu3/tests/test_variables.py @@ -295,7 +295,9 @@ def test_String_to_xml(name, start): r = String(name, start) xml = r.to_xml() if start is not None: - assert xml.attrib['start'] == str(start) + start_elements = xml.findall('.//Start') + assert len(start_elements) == 1 + assert start_elements[0].attrib['value'] == str(start) @pytest.mark.requirements("numpy") diff --git a/pythonfmu3/variables.py b/pythonfmu3/variables.py index b910b08..41a479b 100644 --- a/pythonfmu3/variables.py +++ b/pythonfmu3/variables.py @@ -132,6 +132,16 @@ def __repr__(self) -> str: f"causality={self.causality}, " \ f"variability={self.variability})" +class Start(object): + def __init__(self, startValue): + self.value = startValue + + def to_xml(self) -> Element: + attrib = dict() + attrib["value"] = self.value + return Element("Start", attrib) + + class Dimension(object): def __init__(self, start: str = "", valueReference: str = ""): if start and valueReference and any((start, valueReference)): @@ -320,16 +330,17 @@ def to_xml(self) -> Element: class String(ModelVariable): def __init__(self, name: str, start: Optional[Any] = None, **kwargs): super().__init__(name, **kwargs) - self.__attrs = {"start": start} + self.__attrs = dict() self._type = "String" + self._start = Start(start) @property def start(self) -> Optional[Any]: - return self.__attrs["start"] + return self._start.value @start.setter def start(self, value: float): - self.__attrs["start"] = value + self._start.value = value def to_xml(self) -> Element: attrib = dict() @@ -338,6 +349,9 @@ def to_xml(self) -> Element: attrib[key] = str(value) self._extras = attrib parent = super().to_xml() + + if self.start: + parent.append(self._start.to_xml()) return parent From 5395d715cd604278eefe798e8c4afb6304e25ba7 Mon Sep 17 00:00:00 2001 From: Stephen Smith Date: Thu, 29 Aug 2024 15:47:42 +0100 Subject: [PATCH 2/2] change check to is not none --- pythonfmu3/variables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonfmu3/variables.py b/pythonfmu3/variables.py index 41a479b..7bdcab2 100644 --- a/pythonfmu3/variables.py +++ b/pythonfmu3/variables.py @@ -350,7 +350,7 @@ def to_xml(self) -> Element: self._extras = attrib parent = super().to_xml() - if self.start: + if self.start is not None: parent.append(self._start.to_xml()) return parent