From a60bf76532da8230ea55bc314a3297eacee2fca5 Mon Sep 17 00:00:00 2001 From: Jesse Lentz Date: Tue, 23 Jun 2026 15:19:57 -0400 Subject: [PATCH 1/2] Move inverse_permutation to mpp_mod Move inverse_permutation from interpolator_mod to mpp_mod so it can be more cleanly reused by other parts of the codebase. --- interpolator/interpolator.F90 | 44 +++++++---------------------------- mpp/include/mpp_util.inc | 30 ++++++++++++++++++++++++ mpp/mpp.F90 | 2 +- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/interpolator/interpolator.F90 b/interpolator/interpolator.F90 index df8ec27f07..65ee570297 100644 --- a/interpolator/interpolator.F90 +++ b/interpolator/interpolator.F90 @@ -22,14 +22,15 @@ module interpolator_mod -use mpp_mod, only : mpp_error, & - FATAL, & - mpp_pe, & - mpp_init, & - mpp_exit, & - mpp_npes, & - WARNING, & - NOTE, & +use mpp_mod, only : inverse_permutation, & + mpp_error, & + FATAL, & + mpp_pe, & + mpp_init, & + mpp_exit, & + mpp_npes, & + WARNING, & + NOTE, & input_nml_file use mpp_domains_mod, only : mpp_domains_init, & mpp_update_domains, & @@ -767,33 +768,6 @@ function chomp(string) chomp = string(:len) end function chomp - -!> @brief Produce an inverse permutation. For example, transform [2, 3, 1, 4] into [3, 1, 2, 4]. -!! -!! @param [in] The original permutation vector -!! @param [out] The inverted permutation vector -subroutine inverse_permutation(x, y) - use fms_string_utils_mod, only: string - integer, intent(in) :: x(:) - integer, intent(out) :: y(size(x)) - integer :: i, n - - y = 0 - - n = size(x) - do i=1,n - if (x(i).ge.1 .and. x(i).le.n) then - y(x(i)) = i - else - call mpp_error(FATAL, "interpolator_mod: Invalid dim_order. Values must be in the range from 1 through " & - // string(n) // ".") - endif - enddo - - if (any(y.eq.0)) then - call mpp_error(FATAL, "interpolator_mod: Invalid dim_order. Values must be non-repeating.") - endif -end subroutine inverse_permutation ! !######################################################################## diff --git a/mpp/include/mpp_util.inc b/mpp/include/mpp_util.inc index 2a1d63fd5d..34af2421ac 100644 --- a/mpp/include/mpp_util.inc +++ b/mpp/include/mpp_util.inc @@ -1562,4 +1562,34 @@ end function rarray_to_char call mpp_broadcast(Content, LENGTH, from_pe, PELIST=PELIST) end subroutine read_ascii_file + + !> @brief Produce an inverse permutation. For example, transform [2, 3, 1, 4] to [3, 1, 2, 4]. + !! + !! The purpose of this subroutine is to convert between (memory dimension) -> (logical dimension) and + !! (logical dimension) -> (memory dimension) maps. + !! + !! @param [in] The original permutation vector + !! @param [out] The inverted permutation vector + subroutine inverse_permutation(x, y) + use fms_string_utils_mod, only: string + integer, intent(in) :: x(:) + integer, intent(out) :: y(size(x)) + integer :: i, n + + y = 0 + + n = size(x) + do i=1,n + if (x(i).ge.1 .and. x(i).le.n) then + y(x(i)) = i + else + call mpp_error(FATAL, "inverse_permutation: Invalid dim_order. Values must be in the range from 1 through " & + // string(n) // ".") + endif + enddo + + if (any(y.eq.0)) then + call mpp_error(FATAL, "inverse_permutation: Invalid dim_order. Values must be non-repeating.") + endif + end subroutine inverse_permutation !> @} diff --git a/mpp/mpp.F90 b/mpp/mpp.F90 index 1f19a92351..57ce35651d 100644 --- a/mpp/mpp.F90 +++ b/mpp/mpp.F90 @@ -211,7 +211,7 @@ module mpp_mod public :: read_ascii_file, read_input_nml, mpp_clock_begin, mpp_clock_end public :: get_ascii_file_num_lines, get_ascii_file_num_lines_and_length public :: mpp_record_time_start, mpp_record_time_end - public :: mpp_commID, mpp_comm + public :: mpp_commID, mpp_comm, inverse_permutation !--- public interface from mpp_comm.h ------------------------------ public :: mpp_chksum, mpp_max, mpp_min, mpp_sum, mpp_transmit, mpp_send, mpp_recv From bff7835ff3fe5faee2c3c18dbd4b4d64fad1a03b Mon Sep 17 00:00:00 2001 From: Jesse Lentz Date: Tue, 23 Jun 2026 15:56:06 -0400 Subject: [PATCH 2/2] Remove fms_string_utils_mod dependency --- mpp/include/mpp_util.inc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mpp/include/mpp_util.inc b/mpp/include/mpp_util.inc index 34af2421ac..eae66e6e29 100644 --- a/mpp/include/mpp_util.inc +++ b/mpp/include/mpp_util.inc @@ -1571,7 +1571,6 @@ end function rarray_to_char !! @param [in] The original permutation vector !! @param [out] The inverted permutation vector subroutine inverse_permutation(x, y) - use fms_string_utils_mod, only: string integer, intent(in) :: x(:) integer, intent(out) :: y(size(x)) integer :: i, n @@ -1583,8 +1582,13 @@ end function rarray_to_char if (x(i).ge.1 .and. x(i).le.n) then y(x(i)) = i else - call mpp_error(FATAL, "inverse_permutation: Invalid dim_order. Values must be in the range from 1 through " & - // string(n) // ".") + block + character(2) :: nstr + + write (nstr, "(I0)") n + call mpp_error(FATAL, "inverse_permutation: Invalid dimension map. & + Values must be in the range from 1 to " // trim(nstr) // ".") + end block endif enddo