Skip to content

Commit

Permalink
Add default callbacks for FMI 1.0 and 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer committed Mar 18, 2022
1 parent fdce32c commit 8273ae8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 58 deletions.
37 changes: 13 additions & 24 deletions fmpy/fmi1.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ def printLogMessage(component, instanceName, status, category, message):
print("[%s] %s" % (label, message.decode("utf-8")))


def allocateMemory(nobj, size):
return calloc(nobj, size)
defaultCallbacks = fmi1CallbackFunctions()
defaultCallbacks.logger = fmi1CallbackLoggerTYPE(printLogMessage)
defaultCallbacks.allocateMemory = fmi1CallbackAllocateMemoryTYPE(calloc)
defaultCallbacks.freeMemory = fmi1CallbackFreeMemoryTYPE(free)
defaultCallbacks.stepFinished = None


def freeMemory(obj):
free(obj)


def stepFinished(componentEnvironment, status):
pass
try:
from .logging import addLoggerProxy
addLoggerProxy(byref(defaultCallbacks))
except Exception as e:
print(f"Failed to add logger proxy function. {e}")


class FMICallException(Exception):
Expand Down Expand Up @@ -466,13 +467,7 @@ def instantiate(self, mimeType='application/x-fmu-sharedlibrary', timeout=0, vis
fmuLocation = pathlib.Path(self.unzipDirectory).as_uri()

if functions is None:
functions = fmi1CallbackFunctions()
functions.logger = fmi1CallbackLoggerTYPE(printLogMessage)
functions.allocateMemory = fmi1CallbackAllocateMemoryTYPE(allocateMemory)
functions.freeMemory = fmi1CallbackFreeMemoryTYPE(freeMemory)
functions.stepFinished = None

self.callbacks = functions
functions = defaultCallbacks

self.component = self.fmi1InstantiateSlave(self.instanceName.encode('UTF-8'),
self.guid.encode('UTF-8'),
Expand Down Expand Up @@ -623,17 +618,11 @@ def getTypesPlatform(self):
def instantiate(self, functions=None, loggingOn=False):

if functions is None:
functions = fmi1CallbackFunctions()
functions.logger = fmi1CallbackLoggerTYPE(printLogMessage)
functions.allocateMemory = fmi1CallbackAllocateMemoryTYPE(allocateMemory)
functions.freeMemory = fmi1CallbackFreeMemoryTYPE(freeMemory)
functions.stepFinished = None

self.callbacks = functions
functions = defaultCallbacks

self.component = self.fmi1InstantiateModel(self.instanceName.encode('UTF-8'),
self.guid.encode('UTF-8'),
self.callbacks,
functions,
fmi1True if loggingOn else fmi1False)

if self.component is None:
Expand Down
33 changes: 14 additions & 19 deletions fmpy/fmi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@
fmi2Terminated = 3


def allocateMemory(nobj, size):
return calloc(nobj, size)


def freeMemory(obj):
free(obj)


def stepFinished(componentEnvironment, status):
pass


class fmi2CallbackFunctions(Structure):

_fields_ = [('logger', fmi2CallbackLoggerTYPE),
Expand All @@ -77,6 +65,18 @@ class fmi2EventInfo(Structure):
('nextEventTime', fmi2Real)]


defaultCallbacks = fmi2CallbackFunctions()
defaultCallbacks.logger = fmi2CallbackLoggerTYPE(printLogMessage)
defaultCallbacks.allocateMemory = fmi2CallbackAllocateMemoryTYPE(calloc)
defaultCallbacks.freeMemory = fmi2CallbackFreeMemoryTYPE(free)

try:
from .logging import addLoggerProxy
addLoggerProxy(byref(defaultCallbacks))
except Exception as e:
print(f"Failed to add logger proxy function. {e}")


class _FMU2(_FMU):
""" Base class for FMI 2.0 FMUs """

Expand Down Expand Up @@ -241,18 +241,13 @@ def instantiate(self, visible=False, callbacks=None, loggingOn=False):
resourceLocation = pathlib.Path(self.unzipDirectory, 'resources').as_uri()

if callbacks is None:
callbacks = fmi2CallbackFunctions()
callbacks.logger = fmi2CallbackLoggerTYPE(printLogMessage)
callbacks.allocateMemory = fmi2CallbackAllocateMemoryTYPE(allocateMemory)
callbacks.freeMemory = fmi2CallbackFreeMemoryTYPE(freeMemory)

self.callbacks = callbacks
callbacks = defaultCallbacks

self.component = self.fmi2Instantiate(self.instanceName.encode('utf-8'),
kind,
self.guid.encode('utf-8'),
resourceLocation.encode('utf-8'),
byref(self.callbacks),
byref(callbacks),
fmi2True if visible else fmi2False,
fmi2True if loggingOn else fmi2False)

Expand Down
29 changes: 14 additions & 15 deletions fmpy/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,27 +786,26 @@ def instantiate_fmu(unzipdir, model_description, fmi_type=None, visible=False, d
is_fmi1 = model_description.fmiVersion == '1.0'
is_fmi2 = model_description.fmiVersion == '2.0'

if is_fmi1:
callbacks = fmi1CallbackFunctions()
callbacks.logger = fmi1CallbackLoggerTYPE(printLogMessage if logger is None else logger)
callbacks.allocateMemory = fmi1CallbackAllocateMemoryTYPE(allocateMemory)
callbacks.freeMemory = fmi1CallbackFreeMemoryTYPE(freeMemory)
callbacks.stepFinished = None
elif is_fmi2:
callbacks = fmi2CallbackFunctions()
callbacks.logger = fmi2CallbackLoggerTYPE(printLogMessage if logger is None else logger)
callbacks.allocateMemory = fmi2CallbackAllocateMemoryTYPE(allocateMemory)
callbacks.freeMemory = fmi2CallbackFreeMemoryTYPE(freeMemory)
else:
callbacks = None
if logger is not None:
if is_fmi1:
callbacks = fmi1CallbackFunctions()
callbacks.logger = fmi1CallbackLoggerTYPE(logger)
callbacks.allocateMemory = fmi1CallbackAllocateMemoryTYPE(calloc)
callbacks.freeMemory = fmi1CallbackFreeMemoryTYPE(free)
callbacks.stepFinished = None
elif is_fmi2:
callbacks = fmi2CallbackFunctions()
callbacks.logger = fmi2CallbackLoggerTYPE(logger)
callbacks.allocateMemory = fmi2CallbackAllocateMemoryTYPE(calloc)
callbacks.freeMemory = fmi2CallbackFreeMemoryTYPE(free)

if model_description.fmiVersion in ['1.0', '2.0']:
# add native proxy function that processes variadic arguments
try:
from .logging import addLoggerProxy
addLoggerProxy(byref(callbacks))
except Exception as e:
print("Failed to add logger proxy function. %s" % e)
else:
callbacks = None

if fmi_type in [None, 'CoSimulation'] and model_description.coSimulation is not None:

Expand Down

0 comments on commit 8273ae8

Please sign in to comment.