Skip to content

Fix default_experiment as classvar and fix test doing nothing #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pythonfmu/fmi2slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions pythonfmu/tests/slaves/pythonslave_default_ex.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 8 additions & 3 deletions pythonfmu/tests/test_default_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)