Skip to content

Pass stop_time and tolerance to Python Environment #246

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 2 commits into from
Jul 3, 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
2 changes: 1 addition & 1 deletion pythonfmu/csvbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def find_indices(self, t, dt):
self.next_index += 1
next_t = self.times[self.next_index]

def setup_experiment(self, start_time: float):
def setup_experiment(self, start_time: float, stop_time, tolerance):
self.current_time = start_time
self.find_indices(start_time, 0)

Expand Down
2 changes: 1 addition & 1 deletion pythonfmu/fmi2slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def register_variable(self, var: ScalarVariable, nested: bool = True):
if var.setter is None and hasattr(owner, var.local_name) and var.variability != Fmi2Variability.constant:
var.setter = lambda v: setattr(owner, var.local_name, v)

def setup_experiment(self, start_time: float):
def setup_experiment(self, start_time: float, stop_time: Optional[float], tolerance: Optional[float]):
pass

def enter_initialization_mode(self):
Expand Down
11 changes: 9 additions & 2 deletions pythonfmu/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,15 @@ class PySlaveInstance : public SlaveInstance

void SetupExperiment(double startTime, std::optional<double> stop, std::optional<double> tolerance) override
{
py_safe_run([this, startTime](PyGILState_STATE gilState) {
auto f = PyObject_CallMethod(pInstance_, "setup_experiment", "(d)", startTime);
py_safe_run([this, startTime, stop, tolerance](PyGILState_STATE gilState) {
PyObject* pyStop = stop ? Py_BuildValue("d", *stop) : (Py_INCREF(Py_None), Py_None);
PyObject* pyTol = tolerance ? Py_BuildValue("d", *tolerance) : (Py_INCREF(Py_None), Py_None);

auto f = PyObject_CallMethod(pInstance_, "setup_experiment", "(dOO)", startTime, pyStop, pyTol);

Py_DECREF(pyStop);
Py_DECREF(pyTol);

if (f == nullptr) {
handle_py_exception("[setupExperiment] PyObject_CallMethod", gilState);
}
Expand Down