From e6815577e49944f43f57bfea336f9aea0de969d7 Mon Sep 17 00:00:00 2001 From: Lars Ivar Hatledal Date: Tue, 6 May 2025 23:23:43 +0200 Subject: [PATCH] fix default_experiment as classvar and fix test doing nothing --- pythonfmu/fmi2slave.py | 19 ++++++++++--------- .../tests/slaves/pythonslave_default_ex.py | 15 +++++++++++++++ pythonfmu/tests/test_default_experiment.py | 11 ++++++++--- 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 pythonfmu/tests/slaves/pythonslave_default_ex.py diff --git a/pythonfmu/fmi2slave.py b/pythonfmu/fmi2slave.py index 4b3c36d..edd75c1 100644 --- a/pythonfmu/fmi2slave.py +++ b/pythonfmu/fmi2slave.py @@ -107,16 +107,17 @@ def to_xml(self, model_options: Dict[str, str] = dict()) -> Element: ) ) - 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") 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)