Skip to content

Commit

Permalink
Updates in Swap utility
Browse files Browse the repository at this point in the history
adding Swap_ method
  • Loading branch information
vickysharma0812 committed Jun 9, 2024
1 parent ade4b17 commit 4dd82f5
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 3 deletions.
160 changes: 158 additions & 2 deletions src/modules/Utility/src/SwapUtility.F90
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
!

MODULE SwapUtility
USE GlobalData
USE GlobalData, ONLY: INT8, INT16, INT32, INT64, REAL32, REAL64, &
DFPC, LGT, I4B

IMPLICIT NONE

PRIVATE

PUBLIC :: Swap
PUBLIC :: Swap_

!----------------------------------------------------------------------------
! Swap@SwapMethods
Expand Down Expand Up @@ -521,7 +526,51 @@ MODULE PURE SUBROUTINE Swap_index2(a, b, i1, i2)
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 2
END SUBROUTINE Swap_index2
END INTERFACE Swap
END INTERFACE Swap

!----------------------------------------------------------------------------
! Swap@SwapMethods
!----------------------------------------------------------------------------

!> author: Vikas Sharma, Ph. D.
! date: 2021-11-20
! summary: Swap the index, it is like taking transpose.
!
!# Introduction
!
! - This routine returns an matrix by chaning the dimensions of input matrix
! `b`.
! - This routine does not check the shape, so make sure the shape of
! `a` and `b` are appropriate,.
!

INTERFACE Swap_
MODULE PURE SUBROUTINE Swap_index_1(a, b, i1, i2)
REAL(REAL32), INTENT(INOUT) :: a(:, :)
!! the returned array
REAL(REAL32), INTENT(IN) :: b(:, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 2
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 2
END SUBROUTINE Swap_index_1

MODULE PURE SUBROUTINE Swap_index_2(a, b, i1, i2)
REAL(REAL64), INTENT(INOUT) :: a(:, :)
!! the returned array
REAL(REAL64), INTENT(IN) :: b(:, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 2
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 2
END SUBROUTINE Swap_index_2
END INTERFACE Swap_

!----------------------------------------------------------------------------
! Swap@SwapMethods
Expand Down Expand Up @@ -575,6 +624,57 @@ END SUBROUTINE Swap_index3
! `a` and `b` are appropriate,.
!

INTERFACE Swap_
MODULE PURE SUBROUTINE Swap_index_3(a, b, i1, i2, i3)
REAL(REAL32), INTENT(INOUT) :: a(:, :, :)
!! the returned array
REAL(REAL32), INTENT(IN) :: b(:, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 3
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 3
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 3
END SUBROUTINE Swap_index_3

MODULE PURE SUBROUTINE Swap_index_4(a, b, i1, i2, i3)
REAL(REAL64), INTENT(INOUT) :: a(:, :, :)
!! the returned array
REAL(REAL64), INTENT(IN) :: b(:, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 3
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 3
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 3
END SUBROUTINE Swap_index_4
END INTERFACE Swap_

!----------------------------------------------------------------------------
! Swap@SwapMethods
!----------------------------------------------------------------------------

!> author: Vikas Sharma, Ph. D.
! date: 2021-11-20
! update: 2021-11-20
! summary: Swap the index, it is like taking transpose.
!
!# Introduction
!
! - This routine returns an matrix by chaning the dimensions of input matrix
! `b`.
! - This routine does not check the shape, so make sure the shape of
! `a` and `b` are appropriate,.
!

INTERFACE Swap
MODULE PURE SUBROUTINE Swap_index4(a, b, i1, i2, i3)
REAL(REAL64), ALLOCATABLE, INTENT(INOUT) :: a(:, :, :)
Expand Down Expand Up @@ -667,6 +767,62 @@ MODULE PURE SUBROUTINE Swap_index6(a, b, i1, i2, i3, i4)
END SUBROUTINE Swap_index6
END INTERFACE Swap

!----------------------------------------------------------------------------
! Swap@SwapMethods
!----------------------------------------------------------------------------

!> author: Vikas Sharma, Ph. D.
! date: 2021-11-20
! update: 2021-11-20
! summary: Swap the index, it is like taking transpose.
!
!# Introduction
!
! - This routine returns an matrix by chaning the dimensions of input matrix
! `b`.
! - This routine does not check the shape, so make sure the shape of
! `a` and `b` are appropriate,.

INTERFACE Swap_
MODULE PURE SUBROUTINE Swap_index_5(a, b, i1, i2, i3, i4)
REAL(REAL32), ALLOCATABLE, INTENT(INOUT) :: a(:, :, :, :)
!! the returned array
REAL(REAL32), INTENT(IN) :: b(:, :, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 4
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i4
!! index 4 is Swapped with index `i4`
!! make sure i4 is less than or equal to 4
END SUBROUTINE Swap_index_5

MODULE PURE SUBROUTINE Swap_index_6(a, b, i1, i2, i3, i4)
REAL(REAL64), ALLOCATABLE, INTENT(INOUT) :: a(:, :, :, :)
!! the returned array
REAL(REAL64), INTENT(IN) :: b(:, :, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 4
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i4
!! index 4 is Swapped with index `i4`
!! make sure i4 is less than or equal to 4
END SUBROUTINE Swap_index_6
END INTERFACE Swap_

!----------------------------------------------------------------------------
!
!----------------------------------------------------------------------------
Expand Down
110 changes: 109 additions & 1 deletion src/submodules/Utility/src/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_r64m
REAL(REAL32), DIMENSION(SIZE(a, 1), SIZE(a, 2)) :: dum
REAL(REAL64), DIMENSION(SIZE(a, 1), SIZE(a, 2)) :: dum
dum = a
a = b
b = dum
Expand Down Expand Up @@ -591,6 +591,38 @@
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_1
INTEGER(I4B) :: ij(2), s(2), i, j
!! main
s = SHAPE(b)
DO j = 1, s(2)
DO i = 1, s(1)
ij(1) = i; ij(2) = j
a(ij(i1), ij(i2)) = b(i, j)
END DO
END DO
END PROCEDURE swap_index_1

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_2
INTEGER(I4B) :: ij(2), s(2), i, j
!! main
s = SHAPE(b)
DO j = 1, s(2)
DO i = 1, s(1)
ij(1) = i; ij(2) = j
a(ij(i1), ij(i2)) = b(i, j)
END DO
END DO
END PROCEDURE swap_index_2

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index2
INTEGER(I4B) :: IJ(2), s(2), i, j
!! main
Expand Down Expand Up @@ -664,6 +696,42 @@
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_3
INTEGER(I4B) :: ijk(3), s(3), i, j, k
!! main
s = SHAPE(b)
DO k = 1, s(3)
DO j = 1, s(2)
DO i = 1, s(1)
ijk = [i, j, k]
a(ijk(i1), ijk(i2), ijk(i3)) = b(i, j, k)
END DO
END DO
END DO
END PROCEDURE swap_index_3

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_4
INTEGER(I4B) :: ijk(3), s(3), i, j, k
!! main
s = SHAPE(b)
DO k = 1, s(3)
DO j = 1, s(2)
DO i = 1, s(1)
ijk = [i, j, k]
a(ijk(i1), ijk(i2), ijk(i3)) = b(i, j, k)
END DO
END DO
END DO
END PROCEDURE swap_index_4

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index5
INTEGER(I4B) :: indx(4), s(4), i, j, k, l
!! main
Expand Down Expand Up @@ -702,6 +770,46 @@
END DO
END PROCEDURE swap_index6

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_5
INTEGER(I4B) :: indx(4), s(4), i, j, k, l
!! main
s = SHAPE(b)
DO l = 1, s(4)
DO k = 1, s(3)
DO j = 1, s(2)
DO i = 1, s(1)
indx = [i, j, k, l]
a(indx(i1), indx(i2), indx(i3), indx(i4)) = b(i, j, k, l)
END DO
END DO
END DO
END DO
END PROCEDURE swap_index_5

!----------------------------------------------------------------------------
! SWAP
!----------------------------------------------------------------------------

MODULE PROCEDURE swap_index_6
INTEGER(I4B) :: indx(4), s(4), i, j, k, l
!! main
s = SHAPE(b)
DO l = 1, s(4)
DO k = 1, s(3)
DO j = 1, s(2)
DO i = 1, s(1)
indx = [i, j, k, l]
a(indx(i1), indx(i2), indx(i3), indx(i4)) = b(i, j, k, l)
END DO
END DO
END DO
END DO
END PROCEDURE swap_index_6

!----------------------------------------------------------------------------
!
!----------------------------------------------------------------------------
Expand Down

0 comments on commit 4dd82f5

Please sign in to comment.