Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Added templates for append and copy_into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Jul 31, 2021
1 parent 19b6a87 commit 4bd3487
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
22 changes: 16 additions & 6 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ module swiftest_classes
procedure :: xv2el => orbel_xv2el_vec !! Convert position and velocity vectors to orbital elements
procedure :: setup => setup_body !! A constructor that sets the number of bodies and allocates all allocatable arrays
procedure :: accel_user => user_kick_getacch_body !! Add user-supplied heliocentric accelerations to planets
procedure :: copy => util_copy_body !! Copies elements from one structure to another
procedure :: append => util_append_body !! Appends elements from one structure to another
procedure :: copy_into => util_copy_into_body !! Copies elements from one Swiftest body object to another.
procedure :: fill => util_fill_body !! "Fills" bodies from one object into another depending on the results of a mask (uses the MERGE intrinsic)
procedure :: resize => util_resize_body !! Checks the current size of a Swiftest body against the requested size and resizes it if it is too small.
procedure :: set_ir3 => util_set_ir3h !! Sets the inverse heliocentric radius term (1/rh**3)
Expand Down Expand Up @@ -735,6 +736,14 @@ module subroutine user_kick_getacch_body(self, system, param, t, lbeg)
logical, intent(in) :: lbeg !! Optional argument that determines whether or not this is the beginning or end of the step
end subroutine user_kick_getacch_body

module subroutine util_append_body(self, source, param, lmask)
implicit none
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to append
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
logical, dimension(:), optional, intent(in) :: lmask !! Logical mask indicating which elements to append to
end subroutine util_append_body

module subroutine util_coord_b2h_pl(self, cb)
implicit none
class(swiftest_pl), intent(inout) :: self !! Swiftest massive body object
Expand All @@ -759,12 +768,13 @@ module subroutine util_coord_h2b_tp(self, cb)
class(swiftest_cb), intent(in) :: cb !! Swiftest central body object
end subroutine util_coord_h2b_tp

module subroutine util_copy_body(self, source, param)
module subroutine util_copy_into_body(self, source, param, lmask)
implicit none
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to copy
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
end subroutine util_copy_body
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to append
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
logical, dimension(:), optional, intent(in) :: lmask !! Logical mask indicating which elements to append to
end subroutine util_copy_into_body

module subroutine util_exit(code)
implicit none
Expand Down
24 changes: 24 additions & 0 deletions src/util/util_append.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
submodule (swiftest_classes) s_util_append
use swiftest
contains

module subroutine util_append_body(self, source, param, lmask)
!! author: David A. Minton
!!
!! Append components from one Swiftest body object to another.
!! This method will automatically resize the destination body if it is too small
implicit none
! Arguments
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to append
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
logical, dimension(:), optional, intent(in) :: lmask !! Logical mask indicating which elements to append to

associate(nold => self%nbody, nnew => source%nbody)
if (nnew > size(self%status)) call self%resize(nnew, param)

end associate
return
end subroutine util_append_body

end submodule s_util_append
24 changes: 18 additions & 6 deletions src/util/util_copy.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
use swiftest
contains

module subroutine util_copy_body(self, source, param)
module subroutine util_copy_into_body(self, source, param, lmask)
!! author: David A. Minton
!!
!! Non-destructively copy components from one Swiftest body object to another.
!! Copies elements from one Swiftest body object to another.
!! This method will automatically resize the destination body if it is too small
implicit none
! Arguments
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to copy
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(in) :: source !! Source object to append
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
logical, dimension(:), optional, intent(in) :: lmask !! Logical mask indicating which elements to append to
! Internals
integer(I4B) :: nnew

if (present(lmask)) then
nnew = count(lmask)
else
nnew = size(source%status)
end if
associate(nold => self%nbody)
if (nnew > size(self%status)) call self%resize(nnew, param)

end associate
return
end subroutine util_copy_body
end subroutine util_copy_into_body

end submodule s_util_copy
2 changes: 1 addition & 1 deletion src/util/util_resize.f90
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module subroutine util_resize_body(self, nrequested, param)
if (lmalloc) allocate(temp, source=self)
call self%setup(nrequested, param)
if (lmalloc) then
call self%copy(temp, param)
call self%copy_into(temp, param)
deallocate(temp)
end if
else
Expand Down

0 comments on commit 4bd3487

Please sign in to comment.