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

Commit

Permalink
Simplified sorting methods with direction variable
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Jul 28, 2021
1 parent 570717e commit f3ef56c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 121 deletions.
72 changes: 27 additions & 45 deletions src/symba/symba_util.f90
Original file line number Diff line number Diff line change
Expand Up @@ -77,50 +77,35 @@ module subroutine symba_util_sort_pl(self, sortby, ascending)
logical, intent(in) :: ascending !! Logical flag indicating whether or not the sorting should be in ascending or descending order
! Internals
integer(I4B), dimension(self%nbody) :: ind
integer(I4B) :: direction

if (ascending) then
direction = 1
else
direction = -1
end if

associate(pl => self, npl => self%nbody)
select case(sortby)
case("nplenc")
if (ascending) then
call util_sort(pl%nplenc(1:npl), ind(1:npl))
else
call util_sort(-pl%nplenc(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%nplenc(1:npl), ind(1:npl))
case("ntpenc")
if (ascending) then
call util_sort(pl%ntpenc(1:npl), ind(1:npl))
else
call util_sort(-pl%ntpenc(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%ntpenc(1:npl), ind(1:npl))
case("levelg")
if (ascending) then
call util_sort(pl%levelg(1:npl), ind(1:npl))
else
call util_sort(-pl%levelg(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%levelg(1:npl), ind(1:npl))
case("levelm")
if (ascending) then
call util_sort(pl%levelm(1:npl), ind(1:npl))
else
call util_sort(-pl%levelm(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%levelm(1:npl), ind(1:npl))
case("peri")
if (ascending) then
call util_sort(pl%peri(1:npl), ind(1:npl))
else
call util_sort(-pl%peri(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%peri(1:npl), ind(1:npl))
case("atp")
if (ascending) then
call util_sort(pl%atp(1:npl), ind(1:npl))
else
call util_sort(-pl%atp(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%atp(1:npl), ind(1:npl))
case default
call util_sort_pl(pl, sortby, ascending)
return
end select

call pl%rearrange(ind)

end associate
return
end subroutine symba_util_sort_pl
Expand All @@ -137,32 +122,29 @@ module subroutine symba_util_sort_tp(self, sortby, ascending)
logical, intent(in) :: ascending !! Logical flag indicating whether or not the sorting should be in ascending or descending order
! Internals
integer(I4B), dimension(self%nbody) :: ind
integer(I4B) :: direction

if (ascending) then
direction = 1
else
direction = -1
end if

associate(tp => self, ntp => self%nbody)
select case(sortby)
case("nplenc")
if (ascending) then
call util_sort(tp%nplenc(1:ntp), ind(1:ntp))
else
call util_sort(-tp%nplenc(1:ntp), ind(1:ntp))
end if
call util_sort(direction * tp%nplenc(1:ntp), ind(1:ntp))
case("levelg")
if (ascending) then
call util_sort(tp%levelg(1:ntp), ind(1:ntp))
else
call util_sort(-tp%levelg(1:ntp), ind(1:ntp))
end if
call util_sort(direction * tp%levelg(1:ntp), ind(1:ntp))
case("levelm")
if (ascending) then
call util_sort(tp%levelm(1:ntp), ind(1:ntp))
else
call util_sort(-tp%levelm(1:ntp), ind(1:ntp))
end if
call util_sort(direction * tp%levelm(1:ntp), ind(1:ntp))
case default
call util_sort_tp(tp, sortby, ascending)
return
end select

call tp%rearrange(ind)

end associate
return
end subroutine symba_util_sort_tp
Expand Down
118 changes: 42 additions & 76 deletions src/util/util_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,35 @@ module subroutine util_sort_body(self, sortby, ascending)
logical, intent(in) :: ascending !! Logical flag indicating whether or not the sorting should be in ascending or descending order
! Internals
integer(I4B), dimension(self%nbody) :: ind
integer(I4B) :: direction

if (ascending) then
direction = 1
else
direction = -1
end if

associate(body => self, n => self%nbody)
select case(sortby)
case("id")
if (ascending) then
call util_sort(body%id(1:n), ind(1:n))
else
call util_sort(-body%id(1:n), ind(1:n))
end if
call util_sort(direction * body%id(1:n), ind(1:n))
case("a")
if (ascending) then
call util_sort(body%a(1:n), ind(1:n))
else
call util_sort(-body%a(1:n), ind(1:n))
end if
call util_sort(direction * body%a(1:n), ind(1:n))
case("e")
if (ascending) then
call util_sort(body%e(1:n), ind(1:n))
else
call util_sort(-body%e(1:n), ind(1:n))
end if
call util_sort(direction * body%e(1:n), ind(1:n))
case("inc")
if (ascending) then
call util_sort(body%inc(1:n), ind(1:n))
else
call util_sort(-body%inc(1:n), ind(1:n))
end if
call util_sort(direction * body%inc(1:n), ind(1:n))
case("capom")
if (ascending) then
call util_sort(body%capom(1:n), ind(1:n))
else
call util_sort(-body%capom(1:n), ind(1:n))
end if
call util_sort(direction * body%capom(1:n), ind(1:n))
case("mu")
if (ascending) then
call util_sort(body%mu(1:n), ind(1:n))
else
call util_sort(-body%mu(1:n), ind(1:n))
end if
call util_sort(direction * body%mu(1:n), ind(1:n))
case default
write(*,*) 'Cannot sort structure by component '//trim(adjustl(sortby))
write(*,*) 'Cannot sort structure by component ' // trim(adjustl(sortby))
return
end select

call body%rearrange(ind)

end associate

return
Expand All @@ -74,56 +59,37 @@ module subroutine util_sort_pl(self, sortby, ascending)
logical, intent(in) :: ascending !! Logical flag indicating whether or not the sorting should be in ascending or descending order
! Internals
integer(I4B), dimension(self%nbody) :: ind
integer(I4B) :: direction

if (ascending) then
direction = 1
else
direction = -1
end if

associate(pl => self, npl => self%nbody)
select case(sortby)
case("Gmass","mass")
if (ascending) then
call util_sort(pl%Gmass(1:npl), ind(1:npl))
else
call util_sort(-pl%Gmass(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%Gmass(1:npl), ind(1:npl))
case("rhill")
if (ascending) then
call util_sort(pl%rhill(1:npl), ind(1:npl))
else
call util_sort(-pl%rhill(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%rhill(1:npl), ind(1:npl))
case("radius")
if (ascending) then
call util_sort(pl%radius(1:npl), ind(1:npl))
else
call util_sort(-pl%radius(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%radius(1:npl), ind(1:npl))
case("density")
if (ascending) then
call util_sort(pl%density(1:npl), ind(1:npl))
else
call util_sort(-pl%density(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%density(1:npl), ind(1:npl))
case("k2")
if (ascending) then
call util_sort(pl%k2(1:npl), ind(1:npl))
else
call util_sort(-pl%k2(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%k2(1:npl), ind(1:npl))
case("Q")
if (ascending) then
call util_sort(pl%Q(1:npl), ind(1:npl))
else
call util_sort(-pl%Q(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%Q(1:npl), ind(1:npl))
case("tlag")
if (ascending) then
call util_sort(pl%tlag(1:npl), ind(1:npl))
else
call util_sort(-pl%tlag(1:npl), ind(1:npl))
end if
call util_sort(direction * pl%tlag(1:npl), ind(1:npl))
case default
call util_sort_body(pl, sortby, ascending)
return
end select

call pl%rearrange(ind)

end associate

return
Expand All @@ -141,27 +107,27 @@ module subroutine util_sort_tp(self, sortby, ascending)
logical, intent(in) :: ascending !! Logical flag indicating whether or not the sorting should be in ascending or descending order
! Internals
integer(I4B), dimension(self%nbody) :: ind
integer(I4B) :: direction

if (ascending) then
direction = 1
else
direction = -1
end if

associate(tp => self, ntp => self%nbody)
select case(sortby)
case("peri")
if (ascending) then
call util_sort(tp%peri(1:ntp), ind(1:ntp))
else
call util_sort(-tp%peri(1:ntp), ind(1:ntp))
end if
call util_sort(direction * tp%peri(1:ntp), ind(1:ntp))
case("atp")
if (ascending) then
call util_sort(tp%atp(1:ntp), ind(1:ntp))
else
call util_sort(-tp%atp(1:ntp), ind(1:ntp))
end if
call util_sort(direction * tp%atp(1:ntp), ind(1:ntp))
case default
call util_sort_body(tp, sortby, ascending)
return
end select

call tp%rearrange(ind)

end associate

return
Expand Down
3 changes: 3 additions & 0 deletions src/whm/whm_util.f90
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module subroutine whm_util_sort_pl(self, sortby, ascending)
else
direction = -1
end if

associate(pl => self, npl => self%nbody)
select case(sortby)
case("eta")
Expand All @@ -143,7 +144,9 @@ module subroutine whm_util_sort_pl(self, sortby, ascending)
call util_sort_pl(pl, sortby, ascending)
return
end select

call pl%rearrange(ind)

end associate
return
end subroutine whm_util_sort_pl
Expand Down

0 comments on commit f3ef56c

Please sign in to comment.