From 49b0da9040a6f136bae0d41c30c1cb84ceb9ffff Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Tue, 10 Dec 2024 17:59:26 +0100 Subject: [PATCH 1/3] Add static types to variables --- micro_manager/adaptivity/global_adaptivity.py | 6 +-- micro_manager/micro_manager.py | 42 ++++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/micro_manager/adaptivity/global_adaptivity.py b/micro_manager/adaptivity/global_adaptivity.py index 90690ec7..e30b29b4 100644 --- a/micro_manager/adaptivity/global_adaptivity.py +++ b/micro_manager/adaptivity/global_adaptivity.py @@ -20,7 +20,7 @@ def __init__( self, configurator, logger, - global_number_of_sims: float, + global_number_of_sims: int, global_ids: list, rank: int, comm, @@ -34,8 +34,8 @@ def __init__( Object which has getter functions to get parameters defined in the configuration file. logger : object of logging Logger defined from the standard package logging - global_number_of_sims : float - List of booleans. True if simulation is on this rank, False otherwise. + global_number_of_sims : int + Total number of simulations in the macro-micro coupled problem. global_ids : list List of global IDs of simulations living on this rank. rank : int diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index cd5a5e88..5078e3e4 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -25,9 +25,13 @@ import precice from .micro_manager_base import MicroManager + +from .adaptivity.adaptivity import AdaptivityCalculator from .adaptivity.global_adaptivity import GlobalAdaptivityCalculator from .adaptivity.local_adaptivity import LocalAdaptivityCalculator + from .domain_decomposition import DomainDecomposer + from .micro_simulation import create_simulation_class try: @@ -81,7 +85,7 @@ def __init__(self, config_file: str) -> None: self._is_adaptivity_on = self._config.turn_on_adaptivity() if self._is_adaptivity_on: - self._number_of_sims_for_adaptivity = 0 + self._number_of_sims_for_adaptivity: int = 0 self._data_for_adaptivity: Dict[str, np.ndarray] = dict() self._adaptivity_type = self._config.get_adaptivity_type() @@ -101,7 +105,6 @@ def __init__(self, config_file: str) -> None: self._adaptivity_in_every_implicit_step = ( self._config.is_adaptivity_required_in_every_implicit_iteration() ) - self._micro_sims_active_steps = None # ************** # Public methods @@ -364,7 +367,7 @@ def initialize(self) -> None: self._comm.Allgatherv(np.array(self._local_number_of_sims), nms_all_ranks) # Get global number of micro simulations - self._global_number_of_sims = np.sum(nms_all_ranks) + self._global_number_of_sims: int = np.sum(nms_all_ranks) if self._is_adaptivity_on: for name, is_data_vector in self._adaptivity_data_names.items(): @@ -389,7 +392,7 @@ def initialize(self) -> None: self._global_ids_of_local_sims.append(sim_id) sim_id += 1 - self._micro_sims = [None] * self._local_number_of_sims # DECLARATION + # self._micro_sims = [None] * self._local_number_of_sims # DECLARATION # Setup for simulation crashes self._has_sim_crashed = [False] * self._local_number_of_sims @@ -404,9 +407,12 @@ def initialize(self) -> None: ) # Create micro simulation objects + self._micro_sims = [] for i in range(self._local_number_of_sims): - self._micro_sims[i] = create_simulation_class(micro_problem)( - self._global_ids_of_local_sims[i] + self._micro_sims.append( + create_simulation_class(micro_problem)( + self._global_ids_of_local_sims[i] + ) ) self._logger.info( @@ -417,22 +423,26 @@ def initialize(self) -> None: if self._is_adaptivity_on: if self._adaptivity_type == "local": - self._adaptivity_controller = LocalAdaptivityCalculator( - self._config, self._logger + self._adaptivity_controller: AdaptivityCalculator = ( + LocalAdaptivityCalculator(self._config, self._logger) ) self._number_of_sims_for_adaptivity = self._local_number_of_sims elif self._adaptivity_type == "global": - self._adaptivity_controller = GlobalAdaptivityCalculator( - self._config, - self._logger, - self._global_number_of_sims, - self._global_ids_of_local_sims, - self._rank, - self._comm, + self._adaptivity_controller: AdaptivityCalculator = ( + GlobalAdaptivityCalculator( + self._config, + self._logger, + self._global_number_of_sims, + self._global_ids_of_local_sims, + self._rank, + self._comm, + ) ) self._number_of_sims_for_adaptivity = self._global_number_of_sims - self._micro_sims_active_steps = np.zeros(self._local_number_of_sims) + self._micro_sims_active_steps = np.zeros( + self._local_number_of_sims + ) # DECLARATION self._micro_sims_init = False # DECLARATION From 49dd329ed335ed5416544b4a5b10811ba9e3b834 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Tue, 10 Dec 2024 18:13:07 +0100 Subject: [PATCH 2/3] Add type hints for lists of dicts --- micro_manager/micro_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 5078e3e4..e6366df5 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -662,7 +662,7 @@ def _solve_micro_simulations(self, micro_sims_input: list, dt: float) -> list: List of dicts in which keys are names of data and the values are the data of the output of the micro simulations. """ - micro_sims_output = [None] * self._local_number_of_sims + micro_sims_output: list[dict] = [None] * self._local_number_of_sims for count, sim in enumerate(self._micro_sims): # If micro simulation has not crashed in a previous iteration, attempt to solve it From b3dd6035a4de2d82187e9885e027ddb79317f11c Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Wed, 11 Dec 2024 11:02:58 +0100 Subject: [PATCH 3/3] Add more type hints --- micro_manager/micro_manager.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index e6366df5..62ad3d6f 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -26,7 +26,6 @@ from .micro_manager_base import MicroManager -from .adaptivity.adaptivity import AdaptivityCalculator from .adaptivity.global_adaptivity import GlobalAdaptivityCalculator from .adaptivity.local_adaptivity import LocalAdaptivityCalculator @@ -423,12 +422,12 @@ def initialize(self) -> None: if self._is_adaptivity_on: if self._adaptivity_type == "local": - self._adaptivity_controller: AdaptivityCalculator = ( + self._adaptivity_controller: LocalAdaptivityCalculator = ( LocalAdaptivityCalculator(self._config, self._logger) ) self._number_of_sims_for_adaptivity = self._local_number_of_sims elif self._adaptivity_type == "global": - self._adaptivity_controller: AdaptivityCalculator = ( + self._adaptivity_controller: GlobalAdaptivityCalculator = ( GlobalAdaptivityCalculator( self._config, self._logger,