-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaketreeND.f90
96 lines (85 loc) · 2.53 KB
/
maketreeND.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
!!--------------------------------------------------------------------------
!! MaketreeND - v1.0 1/8/03
!!
!! Constructs the tree for the Barnes/Hut tree code
!! The descriptions are given in Barnes & Hut (1989) ApJS
!!
!! This tree code is designed to work in 1,2 and 3 dimensions
!! Each node in the tree has 2^ndim daughter nodes
!!
!! Written by: Daniel Price, Institute of Astronomy, Cambridge UK
!!--------------------------------------------------------------------------
SUBROUTINE maketree(x,ntot)
! USE debug
! USE loguns
IMPLICIT NONE ! define local variables
INTEGER, PARAMETER :: maxlevels = 8
INTEGER, PARAMETER :: maxdigits = 2**(maxlevels+1)-1, ndim = 3
INTEGER, INTENT(IN) :: ntot
INTEGER :: i,j,idim,inode,ilevel
INTEGER, DIMENSION(ndim) :: ixcoord,idaughter_node
REAL, DIMENSION(ndim) :: xmin,xmax,dxroot,xcoord
REAL, DIMENSION(ndim,ntot) :: x
!
!--allow for tracing flow
!
! IF (trace) WRITE(iprint,*) ' Entering subroutine maketree'
!
!--find boundaries of particle distribution
! (this is the size of the root cell)
!
DO j=1,ndim
xmin(j) = MINVAL(x(j,1:ntot))
xmax(j) = MAXVAL(x(j,1:ntot))
ENDDO
dxroot(:) = xmax(:) - xmin(:) ! size of root cell
!
!--loop over all particles
!
over_particles: DO i=1,ntot
!
!--we use a binary representation of the particle's coordinates to determine
! which node(s) the particle should be in
!
!--find particle co-ordinates scaled to [0,1) in the root cell
xcoord(:) = (x(:,i) - xmin(:))/dxroot(:)
!--convert this to an integer value
ixcoord = NINT(xcoord*maxdigits)
!--use the first two bits of this integer to work out which daughter node
! the particle should belong to
!
!
!
ilevel = 0
nparticles(ilevel) = ntot
DO WHILE (.NOT.empty)
!
!--shuffle the bits of the coordinates to work out which daughter node the
! particle should belong to
!
inode = 0
DO idim=1,ndim
CALL MVBITS(ixcoord(idim),maxlevels-ilevel,1,inode,ndim-idim)
ENDDO
inode = inode + 1
inode = 0
idaughter_node = IBITS(ixcoord(:),maxlevels-ilevel,1)
DO idim=1,ndim
inode = inode + 2**(ndim-idim)*idaughter_node(idim)
ENDDO
inode = inode + 1
PRINT*,' xcoord = ',xcoord,ixcoord,maxdigits
PRINT*,' bits of xcoord = ',BTEST(ixcoord(1),maxlevels)
PRINT*,' daughter node = ',idaughter_node,inode
READ*
!
!--check if this node is empty or not
!
IF (empty(ilevel,inode)) THEN
!--if empty point to this particle and finish
ELSE
!--if not empty point to a branch and desend tree
ENDIF
ENDDO over_particles
RETURN
END SUBROUTINE maketree