From 7f651c44cbb557d82cdb8dcadffa4861a5b7ab36 Mon Sep 17 00:00:00 2001 From: David A Minton Date: Fri, 2 Dec 2022 16:40:57 -0500 Subject: [PATCH] Moved the resize storage method to symba_nbody_system so that it can be reallocated --- src/encounter/encounter_util.f90 | 39 ---------------------------- src/modules/encounter_classes.f90 | 8 +----- src/modules/symba_classes.f90 | 7 +++++ src/symba/symba_encounter_check.f90 | 1 + src/symba/symba_util.f90 | 40 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/encounter/encounter_util.f90 b/src/encounter/encounter_util.f90 index e6d70dd53..cf7dc3a71 100644 --- a/src/encounter/encounter_util.f90 +++ b/src/encounter/encounter_util.f90 @@ -192,45 +192,6 @@ module subroutine encounter_util_resize_list(self, nnew) end subroutine encounter_util_resize_list - module subroutine encounter_util_resize_storage(self, nnew) - !! author: David A. Minton - !! - !! Checks the current size of the encounter storage against the required size and extends it by a factor of 2 more than requested if it is too small. - !! Note: The reason to extend it by a factor of 2 is for performance. When there are many enounters per step, resizing every time you want to add an - !! encounter takes significant computational effort. Resizing by a factor of 2 is a tradeoff between performance (fewer resize calls) and memory managment - !! Memory usage grows by a factor of 2 each time it fills up, but no more. - implicit none - ! Arguments - class(encounter_storage(*)), allocatable, intent(inout) :: self !! Swiftest encounter list - integer(I4B), intent(in) :: nnew !! New size of list needed - ! Internals - type(encounter_storage(nframes=:)), allocatable :: tmp - integer(I4B) :: i, nold - logical :: lmalloc - - - lmalloc = allocated(self) - if (lmalloc) then - nold = self%nframes - else - nold = 0 - end if - - if (nnew > nold) then - allocate(encounter_storage(2 * nnew) :: tmp) - if (lmalloc) then - do i = 1, nold - if (allocated(self%frame(i)%item)) tmp%frame(i) = self%frame(i)%item - end do - deallocate(self) - end if - call move_alloc(tmp,self) - end if - - return - end subroutine encounter_util_resize_storage - - module subroutine encounter_util_spill_list(self, discards, lspill_list, ldestructive) !! author: David A. Minton !! diff --git a/src/modules/encounter_classes.f90 b/src/modules/encounter_classes.f90 index 585992384..1f213f84a 100644 --- a/src/modules/encounter_classes.f90 +++ b/src/modules/encounter_classes.f90 @@ -73,7 +73,7 @@ module encounter_classes !! A class that that is used to store simulation history data between file output type(encounter_io_parameters) :: nciu contains - procedure :: dump => encounter_io_dump_storage_list + procedure :: dump => encounter_io_dump_storage_list !! Dumps contents of encounter history to file end type encounter_storage type encounter_bounding_box_1D @@ -286,12 +286,6 @@ module subroutine encounter_util_resize_list(self, nnew) integer(I8B), intent(in) :: nnew !! New size of list needed end subroutine encounter_util_resize_list - module subroutine encounter_util_resize_storage(self, nnew) - implicit none - class(encounter_storage(*)), allocatable, intent(inout) :: self !! Swiftest encounter list - integer(I4B), intent(in) :: nnew !! New size of list needed - end subroutine encounter_util_resize_storage - module subroutine encounter_util_spill_list(self, discards, lspill_list, ldestructive) implicit none class(encounter_list), intent(inout) :: self !! Swiftest encounter list diff --git a/src/modules/symba_classes.f90 b/src/modules/symba_classes.f90 index 139b221e0..cd97b74bd 100644 --- a/src/modules/symba_classes.f90 +++ b/src/modules/symba_classes.f90 @@ -197,6 +197,7 @@ module symba_classes procedure :: recursive_step => symba_step_recur_system !! Step interacting planets and active test particles ahead in democratic heliocentric coordinates at the current recursion level, if applicable, and descend to the next deeper level if necessary procedure :: reset => symba_step_reset_system !! Resets pl, tp,and encounter structures at the start of a new step procedure :: dealloc => symba_util_dealloc_system !! Deallocates all allocatable arrays + procedure :: resize_storage => symba_util_resize_storage final :: symba_util_final_system !! Finalizes the SyMBA nbody system object - deallocates all allocatables end type symba_nbody_system @@ -700,6 +701,12 @@ module subroutine symba_util_resize_pl(self, nnew) integer(I4B), intent(in) :: nnew !! New size neded end subroutine symba_util_resize_pl + module subroutine symba_util_resize_storage(self, nnew) + implicit none + class(symba_nbody_system), intent(inout) :: self !! SyMBA nbody system object + integer(I4B), intent(in) :: nnew !! New size of list needed + end subroutine symba_util_resize_storage + module subroutine symba_util_resize_tp(self, nnew) implicit none class(symba_tp), intent(inout) :: self !! SyMBA massive body object diff --git a/src/symba/symba_encounter_check.f90 b/src/symba/symba_encounter_check.f90 index 1e900a94a..60febb3fe 100644 --- a/src/symba/symba_encounter_check.f90 +++ b/src/symba/symba_encounter_check.f90 @@ -88,6 +88,7 @@ module function symba_encounter_check_pl(self, param, system, dt, irec) result(l pl%nplenc(j) = pl%nplenc(j) + 1 end do ienc_frame = ienc_frame + 1 + call system%resize_storage(ienc_frame) encounter_history%frame(ienc_frame) = plplenc_list end if diff --git a/src/symba/symba_util.f90 b/src/symba/symba_util.f90 index 805addee8..6b3661abb 100644 --- a/src/symba/symba_util.f90 +++ b/src/symba/symba_util.f90 @@ -900,6 +900,46 @@ module subroutine symba_util_resize_pl(self, nnew) end subroutine symba_util_resize_pl + module subroutine symba_util_resize_storage(self, nnew) + !! author: David A. Minton + !! + !! Checks the current size of the encounter storage against the required size and extends it by a factor of 2 more than requested if it is too small. + !! Note: The reason to extend it by a factor of 2 is for performance. When there are many enounters per step, resizing every time you want to add an + !! encounter takes significant computational effort. Resizing by a factor of 2 is a tradeoff between performance (fewer resize calls) and memory managment + !! Memory usage grows by a factor of 2 each time it fills up, but no more. + implicit none + ! Arguments + class(symba_nbody_system), intent(inout) :: self !! Swiftest encounter list + integer(I4B), intent(in) :: nnew !! New size of list needed + ! Internals + type(encounter_storage(nframes=:)), allocatable :: tmp + integer(I4B) :: i, nold + logical :: lmalloc + + + lmalloc = allocated(self%encounter_history) + if (lmalloc) then + nold = self%encounter_history%nframes + else + nold = 0 + end if + + if (nnew > nold) then + allocate(encounter_storage(2 * nnew) :: tmp) + if (lmalloc) then + do i = 1, nold + if (allocated(self%encounter_history%frame(i)%item)) tmp%frame(i) = self%encounter_history%frame(i)%item + end do + deallocate(self%encounter_history) + end if + call move_alloc(tmp,self%encounter_history) + end if + + return + end subroutine symba_util_resize_storage + + + module subroutine symba_util_resize_tp(self, nnew) !! author: David A. Minton !!