Skip to content

Commit

Permalink
Fix default_experiment (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored Sep 26, 2022
1 parent ae50251 commit 4524b3e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pythonfmu/default_experiment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class DefaultExperiment:

def __init__(self, start_time: float = None, stop_time: float = None, tolerance: float = None):
def __init__(self, start_time: float = None, stop_time: float = None, step_size: float = None, tolerance: float = None):
self.start_time = start_time
self.stop_time = stop_time
self.step_size = step_size
self.tolerance = tolerance
22 changes: 12 additions & 10 deletions pythonfmu/fmi2slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def to_xml(self, model_options: Dict[str, str] = dict()) -> Element:
)
)

if self.default_experiment is not None:
attrib = dict()
if self.default_experiment.start_time is not None:
attrib["startTime"] = str(self.default_experiment.start_time)
if self.default_experiment.stop_time is not None:
attrib["stopTime"] = str(self.default_experiment.stop_time)
if self.default_experiment.step_size is not None:
attrib["stepSize"] = str(self.default_experiment.step_size)
if self.default_experiment.tolerance is not None:
attrib["tolerance"] = str(self.default_experiment.tolerance)
SubElement(root, "DefaultExperiment", attrib)

variables = SubElement(root, "ModelVariables")
for v in self.vars.values():
if ScalarVariable.requires_start(v):
Expand All @@ -127,16 +139,6 @@ def to_xml(self, model_options: Dict[str, str] = dict()) -> Element:
if v.causality == Fmi2Causality.output:
SubElement(outputs_node, "Unknown", attrib=dict(index=str(i + 1)))

if self.default_experiment is not None:
attrib = dict()
if self.default_experiment.start_time is not None:
attrib["startTime"] = self.default_experiment.start_time
if self.default_experiment.stop_time is not None:
attrib["stopTime"] = self.default_experiment.stop_time
if self.default_experiment.tolerance is not None:
attrib["tolerance"] = self.default_experiment.tolerance
SubElement(root, "DefaultExperiment", attrib)

return root

def __apply_start_value(self, var: ScalarVariable):
Expand Down
4 changes: 3 additions & 1 deletion pythonfmu/tests/slaves/pythonslave_read_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Fmi2Variability, String
from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Fmi2Variability, String, DefaultExperiment


class PythonSlaveReadFile(Fmi2Slave):

default_experiment = DefaultExperiment(start_time=0)

def __init__(self, **kwargs):
super().__init__(**kwargs)

Expand Down
17 changes: 17 additions & 0 deletions pythonfmu/tests/test_default_experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from pathlib import Path

from pythonfmu.csvbuilder import FmuBuilder


def test_default_experiment(tmp_path):
fmpy = pytest.importorskip(
"fmpy", reason="fmpy is not available for testing the produced FMU"
)

script_file = Path(__file__).parent / "slaves/pythonslave_read_file.py"
project_file = Path(__file__).parent / "data/hello.txt"
fmu = FmuBuilder.build_FMU(script_file, project_files=[project_file], dest=tmp_path, needsExecutionTool="false")
assert fmu.exists()

model_description = fmpy.read_model_description(fmu)

0 comments on commit 4524b3e

Please sign in to comment.