Skip to content

Commit

Permalink
+New variants of safe_alloc_ptr & safe_alloc_alloc
Browse files Browse the repository at this point in the history
  Added new overloaded variants of safe_alloc_ptr and safe_alloc_alloc to allow
the index range of the third (vertical) index to have an arbitrary starting
value.  All answers are bitwise identical.
  • Loading branch information
Hallberg-NOAA committed Feb 27, 2019
1 parent 6dd6f52 commit 9b7b127
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/framework/MOM_safe_alloc.F90
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ module MOM_safe_alloc

!> Allocate a pointer to a 1-d, 2-d or 3-d array
interface safe_alloc_ptr
module procedure safe_alloc_ptr_3d_2arg, safe_alloc_ptr_2d_2arg
module procedure safe_alloc_ptr_3d_3arg, safe_alloc_ptr_3d_6arg, safe_alloc_ptr_2d_2arg
module procedure safe_alloc_ptr_3d, safe_alloc_ptr_2d, safe_alloc_ptr_1d
end interface safe_alloc_ptr

!> Allocate a 2-d or 3-d allocatable array
interface safe_alloc_alloc
module procedure safe_alloc_allocatable_3d, safe_alloc_allocatable_2d
module procedure safe_alloc_allocatable_3d_6arg
end interface safe_alloc_alloc

! This combined interface might work with a later version of Fortran, but
Expand Down Expand Up @@ -57,7 +58,7 @@ subroutine safe_alloc_ptr_2d_2arg(ptr, ni, nj)
end subroutine safe_alloc_ptr_2d_2arg

!> Allocate a pointer to a 3-d array based on its dimension sizes
subroutine safe_alloc_ptr_3d_2arg(ptr, ni, nj, nk)
subroutine safe_alloc_ptr_3d_3arg(ptr, ni, nj, nk)
real, dimension(:,:,:), pointer :: ptr !< A pointer to allocate
integer, intent(in) :: ni !< The size of the 1st dimension of the array
integer, intent(in) :: nj !< The size of the 2nd dimension of the array
Expand All @@ -66,7 +67,7 @@ subroutine safe_alloc_ptr_3d_2arg(ptr, ni, nj, nk)
allocate(ptr(ni,nj,nk))
ptr(:,:,:) = 0.0
endif
end subroutine safe_alloc_ptr_3d_2arg
end subroutine safe_alloc_ptr_3d_3arg

!> Allocate a pointer to a 2-d array based on its index starting and ending values
subroutine safe_alloc_ptr_2d(ptr, is, ie, js, je)
Expand Down Expand Up @@ -95,6 +96,22 @@ subroutine safe_alloc_ptr_3d(ptr, is, ie, js, je, nk)
endif
end subroutine safe_alloc_ptr_3d

!> Allocate a pointer to a 3-d array based on its index starting and ending values
subroutine safe_alloc_ptr_3d_6arg(ptr, is, ie, js, je, ks, ke)
real, dimension(:,:,:), pointer :: ptr !< A pointer to allocate
integer, intent(in) :: is !< The start index to allocate for the 1st dimension
integer, intent(in) :: ie !< The end index to allocate for the 1st dimension
integer, intent(in) :: js !< The start index to allocate for the 2nd dimension
integer, intent(in) :: je !< The end index to allocate for the 2nd dimension
integer, intent(in) :: ks !< The start index to allocate for the 3rd dimension
integer, intent(in) :: ke !< The end index to allocate for the 3rd dimension
if (.not.associated(ptr)) then
allocate(ptr(is:ie,js:je,ks:ke))
ptr(:,:,:) = 0.0
endif
end subroutine safe_alloc_ptr_3d_6arg


!> Allocate a 2-d allocatable array based on its index starting and ending values
subroutine safe_alloc_allocatable_2d(ptr, is, ie, js, je)
real, dimension(:,:), allocatable :: ptr !< An allocatable array to allocate
Expand All @@ -109,6 +126,7 @@ subroutine safe_alloc_allocatable_2d(ptr, is, ie, js, je)
end subroutine safe_alloc_allocatable_2d

!> Allocate a 3-d allocatable array based on its index starting and ending values
!! and k-index size
subroutine safe_alloc_allocatable_3d(ptr, is, ie, js, je, nk)
real, dimension(:,:,:), allocatable :: ptr !< An allocatable array to allocate
integer, intent(in) :: is !< The start index to allocate for the 1st dimension
Expand All @@ -122,4 +140,19 @@ subroutine safe_alloc_allocatable_3d(ptr, is, ie, js, je, nk)
endif
end subroutine safe_alloc_allocatable_3d

!> Allocate a 3-d allocatable array based on its 6 index starting and ending values
subroutine safe_alloc_allocatable_3d_6arg(ptr, is, ie, js, je, ks, ke)
real, dimension(:,:,:), allocatable :: ptr !< An allocatable array to allocate
integer, intent(in) :: is !< The start index to allocate for the 1st dimension
integer, intent(in) :: ie !< The end index to allocate for the 1st dimension
integer, intent(in) :: js !< The start index to allocate for the 2nd dimension
integer, intent(in) :: je !< The end index to allocate for the 2nd dimension
integer, intent(in) :: ks !< The start index to allocate for the 3rd dimension
integer, intent(in) :: ke !< The end index to allocate for the 3rd dimension
if (.not.allocated(ptr)) then
allocate(ptr(is:ie,js:je,ks:ke))
ptr(:,:,:) = 0.0
endif
end subroutine safe_alloc_allocatable_3d_6arg

end module MOM_safe_alloc

0 comments on commit 9b7b127

Please sign in to comment.