From 5c9d59d7d170d1f938a2e61f32aa53597bf957f4 Mon Sep 17 00:00:00 2001 From: nmannall Date: Thu, 18 Jul 2024 17:17:16 +0100 Subject: [PATCH] Fix wrong grid coord when coordinate at edge of grid When the global coordinate was in the far edge of the grid, the returned grid coordinate would be one more than the maximum in at least one dimension resulting in comm.Get_cart_rank failing. --- gprMax/grid/mpi_grid.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gprMax/grid/mpi_grid.py b/gprMax/grid/mpi_grid.py index bb92c18d..6d39501d 100644 --- a/gprMax/grid/mpi_grid.py +++ b/gprMax/grid/mpi_grid.py @@ -116,14 +116,17 @@ class MPIGrid(FDTDGrid): def is_coordinator(self) -> bool: return self.rank == self.COORDINATOR_RANK - def get_rank_from_coordinate(self, coord: npt.NDArray) -> int: + def get_grid_coord_from_coordinate(self, coord: npt.NDArray) -> npt.NDArray[np.intc]: step_size = self.global_size // self.mpi_tasks overflow = self.global_size % self.mpi_tasks - grid_coord = np.where( + return np.where( (step_size + 1) * overflow >= coord, coord // (step_size + 1), - (coord - overflow) // np.maximum(step_size, 1), + np.minimum((coord - overflow) // np.maximum(step_size, 1), self.mpi_tasks - 1), ) + + def get_rank_from_coordinate(self, coord: npt.NDArray) -> int: + grid_coord = self.get_grid_coord_from_coordinate(coord) return self.comm.Get_cart_rank(grid_coord.tolist()) def global_to_local_coordinate(self, global_coord: npt.NDArray) -> npt.NDArray: