Skip to content

Commit

Permalink
feat(ArrayHandlers): add 2D array expansion interface (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli authored Oct 6, 2023
1 parent 5388698 commit 2177e8d
Showing 1 changed file with 93 additions and 4 deletions.
97 changes: 93 additions & 4 deletions src/Utilities/ArrayHandlers.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ArrayHandlersModule
use SimVariablesModule, only: iout
use GenericUtilitiesModule, only: sim_message, stop_with_error
private
public :: ExpandArray, ExpandArrayWrapper, ExtendPtrArray
public :: ExpandArray, ExpandArray2D, ExpandArrayWrapper, ExtendPtrArray
public :: ConcatArray
public :: ifind
public :: remove_character
Expand All @@ -19,10 +19,17 @@ module ArrayHandlersModule
! IMPORTANT: Do not use pointers to elements of arrays when using
! ExpandArray to increase the array size! The locations of array
! elements in memory are changed when ExpandArray is invoked.
module procedure expand_integer, expand_double, &
expand_character !, expand_real
module procedure expand_integer, expand_double, expand_character
end interface ExpandArray

interface ExpandArray2D
! This interface is for use with ALLOCATABLE arrays.
! IMPORTANT: Do not use pointers to elements of arrays when using
! ExpandArray2D to increase the array size! The locations of array
! elements in memory are changed when ExpandArray2D is invoked.
module procedure expand_integer_2d, expand_double_2d
end interface ExpandArray2D

interface ExtendPtrArray
! This interface is for use with POINTERS to arrays.
module procedure extend_double, extend_integer, &
Expand Down Expand Up @@ -224,6 +231,88 @@ subroutine expand_character(array, increment)
return
end subroutine expand_character

! -- Specific procedures that implement ExtendArray2D

subroutine expand_integer_2d(array, increment1, increment2)
implicit none
! -- dummy
integer(I4B), allocatable, intent(inout) :: array(:, :)
integer(I4B), optional, intent(in) :: increment1
integer(I4B), optional, intent(in) :: increment2
! -- local
integer(I4B) :: inclocal1, inclocal2, isize1, isize2, newsize1, newsize2
integer(I4B), allocatable, dimension(:, :) :: array_temp
!
! -- initialize
if (present(increment1)) then
inclocal1 = increment1
else
inclocal1 = 1
end if
if (present(increment2)) then
inclocal2 = increment2
else
inclocal2 = 1
end if
!
! -- increase size of array by inclocal corresponding to each dim,
! retaining contained data
if (allocated(array)) then
isize1 = size(array, 1)
isize2 = size(array, 2)
newsize1 = isize1 + inclocal1
newsize2 = isize2 + inclocal2
allocate (array_temp(newsize1, newsize2))
array_temp(1:isize1, 1:isize2) = array
deallocate (array)
call move_alloc(array_temp, array)
else
allocate (array(inclocal1, inclocal2))
end if
!
return
end subroutine expand_integer_2d

subroutine expand_double_2d(array, increment1, increment2)
implicit none
! -- dummy
real(DP), allocatable, intent(inout) :: array(:, :)
integer(I4B), optional, intent(in) :: increment1
integer(I4B), optional, intent(in) :: increment2
! -- local
integer(I4B) :: inclocal1, inclocal2, isize1, isize2, newsize1, newsize2
real(DP), allocatable, dimension(:, :) :: array_temp
!
! -- initialize
if (present(increment1)) then
inclocal1 = increment1
else
inclocal1 = 1
end if
if (present(increment2)) then
inclocal2 = increment2
else
inclocal2 = 1
end if
!
! -- increase size of array by inclocal corresponding to each dim,
! retaining contained data
if (allocated(array)) then
isize1 = size(array, 1)
isize2 = size(array, 2)
newsize1 = isize1 + inclocal1
newsize2 = isize2 + inclocal2
allocate (array_temp(newsize1, newsize2))
array_temp(1:isize1, 1:isize2) = array
deallocate (array)
call move_alloc(array_temp, array)
else
allocate (array(inclocal1, inclocal2))
end if
!
return
end subroutine expand_double_2d

! -- Specific procedures that implement ExtendPtrArray for pointer arrays

subroutine extend_double(array, increment)
Expand Down Expand Up @@ -344,7 +433,7 @@ subroutine extend_integer(array, increment)

end subroutine extend_integer

!> @brief Grows or allocated the array with the passed increment,
!> @brief Grows or allocates the array with the passed increment,
!< the old value of the array pointer is rendered invalid
subroutine extend_string(array, increment)
character(len=*), dimension(:), pointer, contiguous :: array
Expand Down

0 comments on commit 2177e8d

Please sign in to comment.