Skip to content

Commit 13fbacd

Browse files
authored
Fix default_experiment as classvar (#240)
1 parent a857713 commit 13fbacd

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

pythonfmu/fmi2slave.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ def to_xml(self, model_options: Dict[str, str] = dict()) -> Element:
102102
)
103103
)
104104

105-
if self.default_experiment is not None:
105+
def_ex = getattr(self.__class__, "default_experiment", getattr(self, "default_experiment", None))
106+
if def_ex is not None:
106107
attrib = dict()
107-
if self.default_experiment.start_time is not None:
108-
attrib["startTime"] = str(self.default_experiment.start_time)
109-
if self.default_experiment.stop_time is not None:
110-
attrib["stopTime"] = str(self.default_experiment.stop_time)
111-
if self.default_experiment.step_size is not None:
112-
attrib["stepSize"] = str(self.default_experiment.step_size)
113-
if self.default_experiment.tolerance is not None:
114-
attrib["tolerance"] = str(self.default_experiment.tolerance)
108+
if def_ex.start_time is not None:
109+
attrib["startTime"] = str(def_ex.start_time)
110+
if def_ex.stop_time is not None:
111+
attrib["stopTime"] = str(def_ex.stop_time)
112+
if def_ex.step_size is not None:
113+
attrib["stepSize"] = str(def_ex.step_size)
114+
if def_ex.tolerance is not None:
115+
attrib["tolerance"] = str(def_ex.tolerance)
115116
SubElement(root, "DefaultExperiment", attrib)
116117

117118
variables = SubElement(root, "ModelVariables")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Fmi2Variability, Integer, DefaultExperiment
2+
3+
4+
class PythonSlaveDefaultExperiment(Fmi2Slave):
5+
default_experiment = DefaultExperiment(start_time=1, stop_time=2, tolerance=1e-3, step_size=1e-3)
6+
7+
def __init__(self, **kwargs):
8+
super().__init__(**kwargs)
9+
10+
self.dummy = 1
11+
12+
self.register_variable(Integer("dummy", causality=Fmi2Causality.output, variability=Fmi2Variability.constant))
13+
14+
def do_step(self, current_time, step_size):
15+
return True

pythonfmu/tests/test_default_experiment.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ def test_default_experiment(tmp_path):
99
"fmpy", reason="fmpy is not available for testing the produced FMU"
1010
)
1111

12-
script_file = Path(__file__).parent / "slaves/pythonslave_read_file.py"
13-
project_file = Path(__file__).parent / "data/hello.txt"
14-
fmu = FmuBuilder.build_FMU(script_file, project_files=[project_file], dest=tmp_path, needsExecutionTool="false")
12+
script_file = Path(__file__).parent / "slaves/pythonslave_default_ex.py"
13+
fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
1514
assert fmu.exists()
1615

1716
model_description = fmpy.read_model_description(fmu)
17+
default_experiment = model_description.defaultExperiment
18+
19+
assert default_experiment.startTime == pytest.approx(1.0)
20+
assert default_experiment.stopTime == pytest.approx(2)
21+
assert default_experiment.tolerance == pytest.approx(1e-3)
22+
assert default_experiment.stepSize == pytest.approx(1e-3)

0 commit comments

Comments
 (0)