diff --git a/micro_manager/adaptivity/global_adaptivity.py b/micro_manager/adaptivity/global_adaptivity.py index 15bc1ca..bf7b700 100644 --- a/micro_manager/adaptivity/global_adaptivity.py +++ b/micro_manager/adaptivity/global_adaptivity.py @@ -190,6 +190,22 @@ def get_full_field_micro_output( return micro_sims_output + def log_metrics(self, logger, adaptivity_data: list, n: int) -> None: + """ """ + is_sim_active = adaptivity_data[1] + global_active_sims = np.count_nonzero(is_sim_active) + global_inactive_sims = np.count_nonzero(is_sim_active == False) + + logger.log_info_one_rank( + "{},{},{},{},{}".format( + n, + np.mean(global_active_sims), + np.mean(global_inactive_sims), + np.max(global_active_sims), + np.max(global_inactive_sims), + ) + ) + def _communicate_micro_output( self, adaptivity_data: list, diff --git a/micro_manager/adaptivity/local_adaptivity.py b/micro_manager/adaptivity/local_adaptivity.py index 04e4aeb..c89f229 100644 --- a/micro_manager/adaptivity/local_adaptivity.py +++ b/micro_manager/adaptivity/local_adaptivity.py @@ -10,7 +10,7 @@ class LocalAdaptivityCalculator(AdaptivityCalculator): - def __init__(self, configurator) -> None: + def __init__(self, configurator, comm) -> None: """ Class constructor. @@ -18,10 +18,11 @@ def __init__(self, configurator) -> None: ---------- configurator : object of class Config Object which has getter functions to get parameters defined in the configuration file. - logger : object of logging - Logger defined from the standard package logging + comm : MPI.COMM_WORLD + Global communicator of MPI. """ super().__init__(configurator) + self._comm = comm def compute_adaptivity( self, @@ -143,6 +144,27 @@ def get_full_field_micro_output( return micro_sims_output + def log_metrics(self, logger, adaptivity_list: list, n: int) -> None: + """ """ + is_sim_active = adaptivity_list[1] + + # MPI Gather is necessary as local adaptivity only stores local data + local_active_sims = np.count_nonzero(is_sim_active) + global_active_sims = self._comm.gather(local_active_sims) + + local_inactive_sims = np.count_nonzero(is_sim_active == False) + global_inactive_sims = self._comm.gather(local_inactive_sims) + + logger.log_info_one_rank( + "{},{},{},{},{}".format( + n, + np.mean(global_active_sims), + np.mean(global_inactive_sims), + np.max(global_active_sims), + np.max(global_inactive_sims), + ) + ) + def _update_inactive_sims( self, similarity_dists: np.ndarray, diff --git a/micro_manager/config.py b/micro_manager/config.py index 26f99b3..58a72be 100644 --- a/micro_manager/config.py +++ b/micro_manager/config.py @@ -50,7 +50,7 @@ def __init__(self, config_file_name): self._adaptivity_refining_constant = 0.5 self._adaptivity_every_implicit_iteration = False self._adaptivity_similarity_measure = "L1" - self._adaptivity_output_n = 1 + self._adaptivity_output_n = -2 self._adaptivity_output_cpu_time = False self._adaptivity_output_mem_usage = False diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 02fa3f0..56b7023 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -121,8 +121,6 @@ def __init__(self, config_file: str) -> None: self._adaptivity_data_names = self._config.get_data_for_adaptivity() - self._adaptivity_output_n = self._config.get_adaptivity_output_n() - # Names of macro data to be used for adaptivity computation self._adaptivity_macro_data_names = dict() @@ -138,7 +136,9 @@ def __init__(self, config_file: str) -> None: self._config.is_adaptivity_required_in_every_implicit_iteration() ) self._micro_sims_active_steps = None - self._output_adaptivity_cpu_time = self._config.output_adaptivity_cpu_time() + + self._adaptivity_output_n = self._config.get_adaptivity_output_n() + self._output_adaptivity_cpu_time = self._config.output_adaptivity_cpu_time() # Define the preCICE Participant self._participant = precice.Participant( @@ -306,28 +306,15 @@ def solve(self) -> None: for sim in self._micro_sims: sim.output() - if self._is_adaptivity_on: - if self._adaptivity_type == "local": - # MPI Gather is necessary as local adaptivity only stores local data - local_active_sims = np.count_nonzero(is_sim_active) - global_active_sims = self._comm.gather(local_active_sims) - - local_inactive_sims = np.count_nonzero(is_sim_active == False) - global_inactive_sims = self._comm.gather(local_inactive_sims) - elif self._adaptivity_type == "global": - global_active_sims = np.count_nonzero(is_sim_active) - global_inactive_sims = np.count_nonzero(is_sim_active == False) - - if n % self._adaptivity_output_n == 0 and self._rank == 0: - self._adaptivity_logger.log_info_one_rank( - "{},{},{},{},{}".format( - n, - np.mean(global_active_sims), - np.mean(global_inactive_sims), - np.max(global_active_sims), - np.max(global_inactive_sims), - ) - ) + if ( + self._is_adaptivity_on + and n % self._adaptivity_output_n == 0 + and self._rank == 0 + ): + self._adaptivity_controller.log_metrics( + self._adaptivity_logger, adaptivity_data, n + ) + self._logger.log_info_one_rank("Time window {} converged.".format(n)) self._participant.finalize() @@ -466,7 +453,7 @@ def initialize(self) -> None: if self._is_adaptivity_on: if self._adaptivity_type == "local": self._adaptivity_controller: LocalAdaptivityCalculator = ( - LocalAdaptivityCalculator(self._config) + LocalAdaptivityCalculator(self._config, self._comm) ) self._number_of_sims_for_adaptivity = self._local_number_of_sims elif self._adaptivity_type == "global":