Skip to content

Commit

Permalink
Moving adaptivity metrics Logger creation inside the adaptivity base …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
IshaanDesai committed Dec 16, 2024
1 parent 4f9acf7 commit 38971ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
16 changes: 14 additions & 2 deletions micro_manager/adaptivity/adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
from math import exp
from typing import Callable
from warnings import warn
from micro_manager.tools.logging_wrapper import Logger

import numpy as np


class AdaptivityCalculator:
def __init__(self, configurator) -> None:
def __init__(self, configurator, rank) -> None:
"""
Class constructor.
Parameters
----------
configurator : object of class Config
Object which has getter functions to get parameters defined in the configuration file.
logger : Logger defined from the standard package logging
rank : int
Rank of the MPI communicator.
"""
self._refine_const = configurator.get_adaptivity_refining_const()
self._coarse_const = configurator.get_adaptivity_coarsening_const()
Expand All @@ -29,10 +31,20 @@ def __init__(self, configurator) -> None:
self._coarse_tol = 0.0
self._ref_tol = 0.0

self._rank = rank

self._similarity_measure = self._get_similarity_measure(
configurator.get_adaptivity_similarity_measure()
)

self._metrics_logger = Logger(
"Adaptivity", "adaptivity-metrics.csv", rank, csv_logger=True
)

self._metrics_logger.log_info_one_rank(
"Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive"
)

def _get_similarity_dists(
self, dt: float, similarity_dists: np.ndarray, data: dict
) -> np.ndarray:
Expand Down
13 changes: 9 additions & 4 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
comm : MPI.COMM_WORLD
Global communicator of MPI.
"""
super().__init__(configurator)
super().__init__(configurator, rank)
self._global_ids = global_ids
self._comm = comm
self._rank = rank
Expand Down Expand Up @@ -190,13 +190,18 @@ def get_full_field_micro_output(

return micro_sims_output

def log_metrics(self, logger, adaptivity_data: list, n: int) -> None:
""" """
def log_metrics(self, adaptivity_data: list, n: int) -> None:
"""
Log metrics for global adaptivity.
Parameters
----------
"""
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(
self._metrics_logger.log_info_one_rank(
"{},{},{},{},{}".format(
n,
np.mean(global_active_sims),
Expand Down
15 changes: 10 additions & 5 deletions micro_manager/adaptivity/local_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class LocalAdaptivityCalculator(AdaptivityCalculator):
def __init__(self, configurator, comm) -> None:
def __init__(self, configurator, rank, comm) -> None:
"""
Class constructor.
Expand All @@ -21,7 +21,7 @@ def __init__(self, configurator, comm) -> None:
comm : MPI.COMM_WORLD
Global communicator of MPI.
"""
super().__init__(configurator)
super().__init__(configurator, rank)
self._comm = comm

def compute_adaptivity(
Expand Down Expand Up @@ -144,8 +144,13 @@ def get_full_field_micro_output(

return micro_sims_output

def log_metrics(self, logger, adaptivity_list: list, n: int) -> None:
""" """
def log_metrics(self, adaptivity_list: list, n: int) -> None:
"""
Log metrics for local adaptivity.
Parameters
----------
"""
is_sim_active = adaptivity_list[1]

# MPI Gather is necessary as local adaptivity only stores local data
Expand All @@ -155,7 +160,7 @@ def log_metrics(self, logger, adaptivity_list: list, n: int) -> None:
local_inactive_sims = np.count_nonzero(is_sim_active == False)
global_inactive_sims = self._comm.gather(local_inactive_sims)

logger.log_info_one_rank(
self._metrics_logger.log_info_one_rank(
"{},{},{},{},{}".format(
n,
np.mean(global_active_sims),
Expand Down
12 changes: 1 addition & 11 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ def __init__(self, config_file: str) -> None:
self._is_adaptivity_on = self._config.turn_on_adaptivity()

if self._is_adaptivity_on:
self._adaptivity_logger = Logger(
"Adaptivity", "adaptivity-metrics.csv", self._rank, csv_logger=True
)

self._adaptivity_logger.log_info_one_rank(
"Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive"
)

self._number_of_sims_for_adaptivity: int = 0

self._data_for_adaptivity: Dict[str, np.ndarray] = dict()
Expand Down Expand Up @@ -311,9 +303,7 @@ def solve(self) -> None:
and n % self._adaptivity_output_n == 0
and self._rank == 0
):
self._adaptivity_controller.log_metrics(
self._adaptivity_logger, adaptivity_data, n
)
self._adaptivity_controller.log_metrics(adaptivity_data, n)

self._logger.log_info_one_rank("Time window {} converged.".format(n))

Expand Down

0 comments on commit 38971ef

Please sign in to comment.