@@ -542,6 +542,7 @@ void PySlaveInstance::GetContinuousStates(cppfmu::FMIFloat64* continuousStates,
542542 PyObject* item = PyList_GetItem (f, i);
543543 continuousStates[i] = static_cast <cppfmu::FMIFloat64>(PyFloat_AsDouble (item));
544544 }
545+ Py_DECREF (f);
545546 clearLogBuffer ();
546547 });
547548}
@@ -551,13 +552,15 @@ void PySlaveInstance::GetContinuousStateDerivatives(cppfmu::FMIFloat64* continuo
551552 py_safe_run ([this , &continuousStateDerivatives, nStates](PyGILState_STATE gilState) {
552553 auto f = PyObject_CallMethod (pInstance_, " get_continuous_state_derivatives" , nullptr );
553554 if (f == nullptr ) {
555+ PyErr_Print ();
554556 handle_py_exception (" [get_continuous_state_derivatives] PyObject_CallMethod" , gilState);
555557 }
556558 // Assuming f is a list of floats
557559 for (std::size_t i = 0 ; i < nStates; i++) {
558560 PyObject* item = PyList_GetItem (f, i);
559561 continuousStateDerivatives[i] = static_cast <cppfmu::FMIFloat64>(PyFloat_AsDouble (item));
560562 }
563+ Py_DECREF (f);
561564 clearLogBuffer ();
562565 });
563566}
@@ -602,9 +605,36 @@ void PySlaveInstance::GetNumberOfContinuousStates(std::size_t& nStates) const
602605
603606void PySlaveInstance::GetNumberOfEventIndicators (std::size_t & nIndicators) const
604607{
605- nIndicators= 0u ;
608+ py_safe_run ([this , &nIndicators](PyGILState_STATE gilState) {
609+ auto f = PyObject_CallMethod (pInstance_, " get_number_of_event_indicators" , nullptr );
610+ if (f == nullptr ) {
611+ handle_py_exception (" [getNumberOfEventIndicators] PyObject_CallMethod" , gilState);
612+ }
613+ nIndicators = static_cast <std::size_t >(PyLong_AsLong (f));
614+ Py_DECREF (f);
615+ clearLogBuffer ();
616+ });
617+ }
618+
619+ void PySlaveInstance::GetEventIndicators (cppfmu::FMIFloat64* eventIndicators, std::size_t nIndicators) const
620+ {
621+ py_safe_run ([this , &eventIndicators, nIndicators](PyGILState_STATE gilState) {
622+ auto f = PyObject_CallMethod (pInstance_, " get_event_indicators" , nullptr );
623+ if (f == nullptr ) {
624+ handle_py_exception (" [getEventIndicators] PyObject_CallMethod" , gilState);
625+ }
626+ // Assuming f is a list of floats
627+ for (std::size_t i = 0 ; i < nIndicators; i++) {
628+ PyObject* item = PyList_GetItem (f, i);
629+ eventIndicators[i] = static_cast <cppfmu::FMIFloat64>(PyFloat_AsDouble (item));
630+ }
631+ Py_DECREF (f);
632+ clearLogBuffer ();
633+ });
634+
606635}
607636
637+
608638void PySlaveInstance::SetTime (cppfmu::FMIFloat64 time)
609639{
610640 py_safe_run ([this , time](PyGILState_STATE gilState) {
@@ -625,12 +655,46 @@ void PySlaveInstance::UpdateDiscreteStates(
625655 cppfmu::FMIBoolean* nextEventTimeDefined,
626656 cppfmu::FMIFloat64* nextEventTime)
627657{
628- *discreteStatesNeedUpdate = false ;
629- *terminateSimulation = false ;
630- *nominalContinuousStatesChanged = false ;
631- *valuesOfContinuousStatesChanged = false ;
632- *nextEventTimeDefined = false ;
633- *nextEventTime = 0.0 ;
658+ py_safe_run ([this , discreteStatesNeedUpdate, terminateSimulation, nominalContinuousStatesChanged, valuesOfContinuousStatesChanged, nextEventTimeDefined, nextEventTime](PyGILState_STATE gilState) {
659+ auto f = PyObject_CallMethod (pInstance_, " update_discrete_states" , nullptr );
660+ if (f == nullptr ) {
661+ handle_py_exception (" [updateDiscreteStates] PyObject_CallMethod" , gilState);
662+ }
663+
664+ if (PyObject_HasAttrString (f, " discreteStatesNeedUpdate" )) {
665+ PyObject * pyDiscreteStatesNeedUpdate = PyObject_GetAttrString (f, " discreteStatesNeedUpdate" );
666+ *discreteStatesNeedUpdate = static_cast <bool >(PyObject_IsTrue (pyDiscreteStatesNeedUpdate));
667+ Py_DECREF (pyDiscreteStatesNeedUpdate);
668+ }
669+ if (PyObject_HasAttrString (f, " terminateSimulation" )) {
670+ PyObject* pyTerminateSimulation = PyObject_GetAttrString (f, " terminateSimulation" );
671+ *terminateSimulation = static_cast <bool >(PyObject_IsTrue (pyTerminateSimulation));
672+ Py_DECREF (pyTerminateSimulation);
673+ }
674+ if (PyObject_HasAttrString (f, " nominalContinuousStatesChanged" )) {
675+ PyObject* pyNominalContinuousStatesChanged = PyObject_GetAttrString (f, " nominalContinuousStatesChanged" );
676+ *nominalContinuousStatesChanged = static_cast <bool >(PyObject_IsTrue (pyNominalContinuousStatesChanged));
677+ Py_DECREF (pyNominalContinuousStatesChanged);
678+ }
679+ if (PyObject_HasAttrString (f, " valuesOfContinuousStatesChanged" )) {
680+ PyObject* pyValuesOfContinuousStatesChanged = PyObject_GetAttrString (f, " valuesOfContinuousStatesChanged" );
681+ *valuesOfContinuousStatesChanged = static_cast <bool >(PyObject_IsTrue (pyValuesOfContinuousStatesChanged));
682+ Py_DECREF (pyValuesOfContinuousStatesChanged);
683+ }
684+ if (PyObject_HasAttrString (f, " nextEventTimeDefined" )) {
685+ PyObject* pyNextEventTimeDefined = PyObject_GetAttrString (f, " nextEventTimeDefined" );
686+ *nextEventTimeDefined = static_cast <bool >(PyObject_IsTrue (pyNextEventTimeDefined));
687+ Py_DECREF (pyNextEventTimeDefined);
688+ }
689+ if (nextEventTimeDefined && *nextEventTimeDefined) {
690+ PyObject* pyNextEventTime = PyObject_GetAttrString (f, " nextEventTime" );
691+ *nextEventTime = PyFloat_AsDouble (pyNextEventTime);
692+ Py_DECREF (pyNextEventTime);
693+ }
694+
695+ Py_DECREF (f);
696+ clearLogBuffer ();
697+ });
634698}
635699
636700void PySlaveInstance::GetFMUstate (fmi3FMUState& state)
0 commit comments