Skip to content

Commit f39ff11

Browse files
authored
Pass stop_time and tolerance to Python Environment (#246)
1 parent 2926cca commit f39ff11

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

pythonfmu/csvbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def find_indices(self, t, dt):
150150
self.next_index += 1
151151
next_t = self.times[self.next_index]
152152
153-
def setup_experiment(self, start_time: float):
153+
def setup_experiment(self, start_time: float, stop_time, tolerance):
154154
self.current_time = start_time
155155
self.find_indices(start_time, 0)
156156

pythonfmu/fmi2slave.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def register_variable(self, var: ScalarVariable, nested: bool = True):
172172
if var.setter is None and hasattr(owner, var.local_name) and var.variability != Fmi2Variability.constant:
173173
var.setter = lambda v: setattr(owner, var.local_name, v)
174174

175-
def setup_experiment(self, start_time: float):
175+
def setup_experiment(self, start_time: float, stop_time: Optional[float], tolerance: Optional[float]):
176176
pass
177177

178178
def enter_initialization_mode(self):

pythonfmu/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,15 @@ class PySlaveInstance : public SlaveInstance
226226

227227
void SetupExperiment(double startTime, std::optional<double> stop, std::optional<double> tolerance) override
228228
{
229-
py_safe_run([this, startTime](PyGILState_STATE gilState) {
230-
auto f = PyObject_CallMethod(pInstance_, "setup_experiment", "(d)", startTime);
229+
py_safe_run([this, startTime, stop, tolerance](PyGILState_STATE gilState) {
230+
PyObject* pyStop = stop ? Py_BuildValue("d", *stop) : (Py_INCREF(Py_None), Py_None);
231+
PyObject* pyTol = tolerance ? Py_BuildValue("d", *tolerance) : (Py_INCREF(Py_None), Py_None);
232+
233+
auto f = PyObject_CallMethod(pInstance_, "setup_experiment", "(dOO)", startTime, pyStop, pyTol);
234+
235+
Py_DECREF(pyStop);
236+
Py_DECREF(pyTol);
237+
231238
if (f == nullptr) {
232239
handle_py_exception("[setupExperiment] PyObject_CallMethod", gilState);
233240
}

0 commit comments

Comments
 (0)