Repository: NTNU-IHB/PythonFMU
Pull request #240 (2025-05-06 21:24:22)

Fix default_experiment as classvar and fix test doing nothing


Patch:
diff --git a/pythonfmu/fmi2slave.py b/pythonfmu/fmi2slave.py
index e9842ba..6657b7f 100644
--- a/pythonfmu/fmi2slave.py
+++ b/pythonfmu/fmi2slave.py
@@ -102,16 +102,17 @@ class Fmi2Slave(ABC):
                     )
                 )
 
-        if self.default_experiment is not None:
+        def_ex = getattr(self.__class__, "default_experiment", getattr(self, "default_experiment", None))
+        if def_ex 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)
+            if def_ex.start_time is not None:
+                attrib["startTime"] = str(def_ex.start_time)
+            if def_ex.stop_time is not None:
+                attrib["stopTime"] = str(def_ex.stop_time)
+            if def_ex.step_size is not None:
+                attrib["stepSize"] = str(def_ex.step_size)
+            if def_ex.tolerance is not None:
+                attrib["tolerance"] = str(def_ex.tolerance)
             SubElement(root, "DefaultExperiment", attrib)
 
         variables = SubElement(root, "ModelVariables")


Test patch:
diff --git a/pythonfmu/tests/slaves/pythonslave_default_ex.py b/pythonfmu/tests/slaves/pythonslave_default_ex.py
new file mode 100644
index 0000000..f82fadd
--- /dev/null
+++ b/pythonfmu/tests/slaves/pythonslave_default_ex.py
@@ -0,0 +1,15 @@
+from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Fmi2Variability, Integer, DefaultExperiment
+
+
+class PythonSlaveDefaultExperiment(Fmi2Slave):
+    default_experiment = DefaultExperiment(start_time=1, stop_time=2, tolerance=1e-3, step_size=1e-3)
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+        self.dummy = 1
+
+        self.register_variable(Integer("dummy", causality=Fmi2Causality.output, variability=Fmi2Variability.constant))
+
+    def do_step(self, current_time, step_size):
+        return True
diff --git a/pythonfmu/tests/test_default_experiment.py b/pythonfmu/tests/test_default_experiment.py
index 7c5a1df..98160fe 100644
--- a/pythonfmu/tests/test_default_experiment.py
+++ b/pythonfmu/tests/test_default_experiment.py
@@ -9,9 +9,14 @@ def test_default_experiment(tmp_path):
         "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")
+    script_file = Path(__file__).parent / "slaves/pythonslave_default_ex.py"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
     assert fmu.exists()
 
     model_description = fmpy.read_model_description(fmu)
+    default_experiment = model_description.defaultExperiment
+
+    assert default_experiment.startTime == pytest.approx(1.0)
+    assert default_experiment.stopTime == pytest.approx(2)
+    assert default_experiment.tolerance == pytest.approx(1e-3)
+    assert default_experiment.stepSize == pytest.approx(1e-3)