Skip to content

Commit

Permalink
Merge pull request #14 from StephenSmith25/fix_str_params
Browse files Browse the repository at this point in the history
correct string start value
  • Loading branch information
StephenSmith25 authored Aug 29, 2024
2 parents 8dbe870 + 5395d71 commit b610159
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/demoslave.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pythonfmu3 import Fmi3Causality, Fmi3Variability, Fmi3Slave, Float64
from pythonfmu3 import Fmi3Causality, Fmi3Variability, Fmi3Slave, Float64, String


class Resistor(Fmi3Slave):
Expand All @@ -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))
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pythonfmu3/tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 17 additions & 3 deletions pythonfmu3/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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()
Expand All @@ -338,6 +349,9 @@ def to_xml(self) -> Element:
attrib[key] = str(value)
self._extras = attrib
parent = super().to_xml()

if self.start is not None:
parent.append(self._start.to_xml())

return parent

Expand Down

0 comments on commit b610159

Please sign in to comment.