Skip to content
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

Handle the case where micro simulations written in C++ have the initialize() function #110

Merged
merged 2 commits into from
Jun 3, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 32 additions & 11 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down