I have a c++ function called fmi2NewDiscreteStates from a DLL, which I want to call inside a jitted python function. The function takes the argument fmi2EventInfo, which is a struct that is defined using ctypes like this:
class fmi2EventInfo(Structure):
_fields_ = [('newDiscreteStatesNeeded', fmi2Boolean),
('terminateSimulation', fmi2Boolean),
('nominalsOfContinuousStatesChanged', fmi2Boolean),
('valuesOfContinuousStatesChanged', fmi2Boolean),
('nextEventTimeDefined', fmi2Boolean),
('nextEventTime', fmi2Real)]
The datatypes (fmi2Bool, fmi2Real) are aliases for int and double, respectively.
Now the question:
How can I pass the pointer inside the jitted function? Basically, I want:
fmi2NewDiscreteStates(component, eventInfo_ptr)
I’ve tried to pass by a reference:
# Assuming I have "component" already
eventInfo = fmi2EventInfo()
eventInfo_ptr = ctypes.addressof(eventInfo)
@njit()
def fmi2NewDiscreteStates_jitted():
fmi2NewDiscreteStates(component, eventInfo_ptr)
But If I run this multiple times, I get segfault. So it seems I am accessing something that I shouldn’t. What is the correct way to do this?