Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Micro Manager changelog

## latest

- Allow `initialize()` to return data that is not used by the adaptivity [#261](https://github.com/precice/micro-manager/pull/261)

## v0.9.0

- Refactored `DomainDecomposer` class and added a new variant of non-uniform decomposition [#243](https://github.com/precice/micro-manager/pull/243)
Expand Down
48 changes: 29 additions & 19 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env python3micro_manager
"""
Micro Manager is a tool to initialize and adaptively control micro simulations and couple them via preCICE to a macro simulation.
This files the class MicroManager which has the following callable public methods:
Expand Down Expand Up @@ -742,6 +742,20 @@ def initialize(self) -> None:
self._micro_sims[i].initialize()
else: # Case where the initialize() method returns data
if self._is_adaptivity_on:
# Check for missing data
expected, provided = set(self._data_for_adaptivity.keys()), set(
initial_micro_output.keys()
)
if missing := expected - provided:
raise Exception(
"The initialize() method needs to return data which is required for the adaptivity calculation. "
f'Of the expected data {", ".join(expected)}, the following is missing: {", ".join(missing)}'
)
elif extra := provided - expected:
self._logger.log_warning_rank_zero(
f'The initialize() method of the Micro simulation returns extra initial data which isn\'t used by the adaptivity: {", ".join(extra)}'
)

for name in initial_micro_output.keys():
initial_micro_data[name] = [0] * self._local_number_of_sims
# Save initial data from first micro simulation as we anyway have it
Expand All @@ -750,12 +764,8 @@ def initialize(self) -> None:
# Save initial data from first micro simulation as we anyway have it
for name in initial_micro_output.keys():
if name in self._data_for_adaptivity:
self._data_for_adaptivity[name][
first_id
] = initial_micro_output[name]
else:
raise Exception(
"The initialize() method needs to return data which is required for the adaptivity calculation."
self._data_for_adaptivity[name][first_id] = (
initial_micro_output[name]
)

# Gather initial data from the rest of the micro simulations
Expand All @@ -765,17 +775,17 @@ def initialize(self) -> None:
initial_macro_data[i]
)
for name in self._adaptivity_micro_data_names:
self._data_for_adaptivity[name][
i
] = initial_micro_output[name]
self._data_for_adaptivity[name][i] = (
initial_micro_output[name]
)
initial_micro_data[name][i] = initial_micro_output[name]
else:
for i in micro_sims_to_init:
initial_micro_output = self._micro_sims[i].initialize()
for name in self._adaptivity_micro_data_names:
self._data_for_adaptivity[name][
i
] = initial_micro_output[name]
self._data_for_adaptivity[name][i] = (
initial_micro_output[name]
)
initial_micro_data[name][i] = initial_micro_output[name]
else: # If adaptivity is off, the returned initial data from the initialize() method will be ignored
self._logger.log_warning_rank_zero(
Expand Down Expand Up @@ -1045,9 +1055,9 @@ def _solve_micro_simulations_with_adaptivity(
# Mark the micro sim as active for export
micro_sims_output[lid]["Active-State"] = 1
gid = self._global_ids_of_local_sims[lid]
micro_sims_output[lid][
"Active-Steps"
] = self._micro_sims_active_steps[gid]
micro_sims_output[lid]["Active-Steps"] = (
self._micro_sims_active_steps[gid]
)

# If simulation crashes, log the error and keep the output constant at the previous iteration's output
except Exception as error_message:
Expand Down Expand Up @@ -1101,9 +1111,9 @@ def _solve_micro_simulations_with_adaptivity(
for inactive_lid in inactive_sim_lids:
micro_sims_output[inactive_lid]["Active-State"] = 0
gid = self._global_ids_of_local_sims[inactive_lid]
micro_sims_output[inactive_lid][
"Active-Steps"
] = self._micro_sims_active_steps[gid]
micro_sims_output[inactive_lid]["Active-Steps"] = (
self._micro_sims_active_steps[gid]
)

# Collect micro sim output for adaptivity calculation
for i in range(self._local_number_of_sims):
Expand Down