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

Commit

Permalink
Started a polymorphic sorting method
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Jul 28, 2021
1 parent 9939662 commit ba99dd7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
22 changes: 7 additions & 15 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module swiftest_classes
public :: user_kick_getacch_body
public :: util_coord_b2h_pl, util_coord_b2h_tp, util_coord_h2b_pl, util_coord_h2b_tp, util_exit, util_fill_body, util_fill_pl, util_fill_tp, &
util_peri_tp, util_reverse_status, util_set_beg_end_pl, util_set_ir3h, util_set_msys, util_set_mu_pl, &
util_set_mu_tp, util_set_rhill, util_set_rhill_approximate, util_sort, util_sort_body, util_sort_pl, util_spill_body, util_spill_pl, util_spill_tp, util_valid, util_version
util_set_mu_tp, util_set_rhill, util_set_rhill_approximate, util_sort, util_sort_body, util_spill_body, util_spill_pl, util_spill_tp, util_valid, util_version

!********************************************************************************************************************************
! swiftest_parameters class definitions
Expand Down Expand Up @@ -229,7 +229,6 @@ module swiftest_classes
procedure, public :: b2h => util_coord_b2h_pl !! Convert massive bodies from barycentric to heliocentric coordinates (position and velocity)
procedure, public :: fill => util_fill_pl !! "Fills" bodies from one object into another depending on the results of a mask (uses the MERGE intrinsic)
procedure, public :: set_beg_end => util_set_beg_end_pl !! Sets the beginning and ending positions and velocities of planets.
procedure, public :: sort => util_sort_pl !! Sorts a body by an attribute (adds ability to sort by mass)
procedure, public :: spill => util_spill_pl !! "Spills" bodies from one object to another depending on the results of a mask (uses the PACK intrinsic)
end type swiftest_pl

Expand Down Expand Up @@ -884,23 +883,16 @@ end subroutine util_sort_index_dp
interface
module subroutine util_sort_body(self, sortby, reverse)
implicit none
class(swiftest_body), intent(inout) :: self !! Swiftest body object
character(*), optional, intent(in) :: sortby !! Sorting attribute
logical, optional, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
class(swiftest_body), intent(inout) :: self !! Swiftest body object
character(*), intent(in) :: sortby !! Sorting attribute
logical, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
end subroutine util_sort_body

module subroutine util_sort_pl(self, sortby, reverse)
implicit none
class(swiftest_pl), intent(inout) :: self !! Swiftest body object
character(*), optional, intent(in) :: sortby !! Sorting attribute
logical, optional, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
end subroutine util_sort_pl

module subroutine util_spill_body(self, discards, lspill_list)
implicit none
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(inout) :: discards !! Discarded object
logical, dimension(:), intent(in) :: lspill_list !! Logical array of bodies to spill into the discards
class(swiftest_body), intent(inout) :: self !! Swiftest body object
class(swiftest_body), intent(inout) :: discards !! Discarded object
logical, dimension(:), intent(in) :: lspill_list !! Logical array of bodies to spill into the discards
end subroutine util_spill_body

module subroutine util_spill_pl(self, discards, lspill_list)
Expand Down
56 changes: 42 additions & 14 deletions src/util/util_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,49 @@ module subroutine util_sort_body(self, sortby, reverse)
!! sortby is a string. The only valid input the body class takes is "id," which is also the default value.
!! Sort order is ascending order by default. Set reverse=.true. to sort in descending order.
implicit none
class(swiftest_body), intent(inout) :: self !! Swiftest body object
character(*), optional, intent(in) :: sortby !! Sorting attribute
logical, optional, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
end subroutine util_sort_body
! Arguments
class(swiftest_body), intent(inout) :: self !! Swiftest body object
character(*), intent(in) :: sortby !! Sorting attribute
logical, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
! Internals
class(swiftest_body), allocatable :: body_sorted !! Temporary holder for sorted body
integer(I4B), dimension(:), allocatable :: ind

module subroutine util_sort_pl(self, sortby, reverse)
!! author: David A. Minton
!!
!! sortby is a string. The only valid input the body class takes is "id," which is also the default value.
!! Sort order is ascending order by default. Set reverse=.true. to sort in descending order.
implicit none
class(swiftest_pl), intent(inout) :: self !! Swiftest body object
character(*), optional, intent(in) :: sortby !! Sorting attribute
logical, optional, intent(in) :: reverse !! Logical flag indicating whether or not the sorting should be in reverse (descending order)
end subroutine util_sort_pl
associate(n => self%nbody)
allocate(body_sorted, source=self)
allocate(ind(n))
select case(sortby)
case("id")
if (reverse) then
call util_sort(-self%id(1:n), ind(1:n))
else
call util_sort(self%id(1:n), ind(1:n))
end if
end select

self%id(1:n) = body_sorted%id(ind(1:n))
self%name(1:n) = body_sorted%name(ind(1:n))
self%status(1:n) = body_sorted%status(ind(1:n))
self%ldiscard(1:n) = body_sorted%ldiscard(ind(1:n))
self%xh(:,1:n) = body_sorted%xh(:,ind(1:n))
self%vh(:,1:n) = body_sorted%vh(:,ind(1:n))
self%xb(:,1:n) = body_sorted%xb(:,ind(1:n))
self%vb(:,1:n) = body_sorted%vb(:,ind(1:n))
self%ah(:,1:n) = body_sorted%ah(:,ind(1:n))
self%aobl(:,1:n) = body_sorted%aobl(:,ind(1:n))
self%atide(:,1:n) = body_sorted%atide(:,ind(1:n))
self%agr(:,1:n) = body_sorted%agr(:,ind(1:n))
self%ir3h(1:n) = body_sorted%ir3h(ind(1:n))
self%a(1:n) = body_sorted%a(ind(1:n))
self%e(1:n) = body_sorted%e(ind(1:n))
self%inc(1:n) = body_sorted%inc(ind(1:n))
self%capom(1:n) = body_sorted%capom(ind(1:n))
self%omega(1:n) = body_sorted%omega(ind(1:n))
self%capm(1:n) = body_sorted%capm(ind(1:n))
self%mu(1:n) = body_sorted%mu(ind(1:n))
end associate
return
end subroutine util_sort_body

module subroutine util_sort_dp(arr)
!! author: David A. Minton
Expand Down

0 comments on commit ba99dd7

Please sign in to comment.