diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9770c..cf2ba50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## latest +- Handle calling `initialize()` function of micro simulations written in languages other than Python https://github.com/precice/micro-manager/pull/110 - Check if initial data returned from the micro simulation is the data that the adaptivity computation requires https://github.com/precice/micro-manager/pull/109 - Use executable `micro-manager-precice` by default, and stop using the script `run_micro_manager.py` https://github.com/precice/micro-manager/pull/105 - Make `initialize()` method of the MicroManager class public https://github.com/precice/micro-manager/pull/105 diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 449f5d1..6472b5f 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -468,18 +468,39 @@ def initialize(self) -> None: if hasattr(micro_problem, "initialize") and callable( getattr(micro_problem, "initialize") ): - self._micro_sims_init = True - - # Check if the initialize() method of the micro simulation has any arguments - argspec = inspect.getfullargspec(micro_problem.initialize) - if len(argspec.args) == 1: - is_initial_data_required = False - elif len(argspec.args) == 2: - is_initial_data_required = True - else: - raise Exception( - "The initialize() method of the Micro simulation has an incorrect number of arguments." + self._micro_sims_init = True # Starting value before setting + + try: # Try to get the signature of the initialize() method, if it is written in Python + argspec = inspect.getfullargspec(micro_problem.initialize) + if ( + len(argspec.args) == 1 + ): # The first argument in the signature is self + is_initial_data_required = False + elif len(argspec.args) == 2: + is_initial_data_required = True + else: + raise Exception( + "The initialize() method of the Micro simulation has an incorrect number of arguments." + ) + except TypeError: + self._logger.info( + "The signature of initialize() method of the micro simulation cannot be determined. Trying to determine the signature by calling the method." ) + # Try to get the signature of the initialize() method, if it is not written in Python + try: # Try to call the initialize() method without initial data + self._micro_sims[0].initialize() + is_initial_data_required = False + except TypeError: + self._logger.info( + "The initialize() method of the micro simulation has arguments. Attempting to call it again with initial data." + ) + try: # Try to call the initialize() method with initial data + self._micro_sims[0].initialize(initial_data[0]) + is_initial_data_required = True + except TypeError: + raise Exception( + "The initialize() method of the Micro simulation has an incorrect number of arguments." + ) if is_initial_data_required and not is_initial_data_available: raise Exception(