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

Add static type hints (with prompts from Mypy) #134

Merged
merged 3 commits into from
Dec 11, 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
6 changes: 3 additions & 3 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
43 changes: 26 additions & 17 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import precice

from .micro_manager_base import MicroManager

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:
Expand Down Expand Up @@ -81,7 +84,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()
Expand All @@ -101,7 +104,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
Expand Down Expand Up @@ -364,7 +366,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():
Expand All @@ -389,7 +391,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
Expand All @@ -404,9 +406,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(
Expand All @@ -417,22 +422,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: 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 = GlobalAdaptivityCalculator(
self._config,
self._logger,
self._global_number_of_sims,
self._global_ids_of_local_sims,
self._rank,
self._comm,
self._adaptivity_controller: GlobalAdaptivityCalculator = (
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

Expand Down Expand Up @@ -652,7 +661,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
Expand Down
Loading