! ============================================================
! TREEDEFS: include file with common definitions for TREECODE.
! ============================================================
 Include 'mpif.h'
! --------------------------------------------------------------
! Fundamental constants for treecode, not to be altered lightly.
! --------------------------------------------------------------
 
! NULL        value denoting pointer to nothing.
! NDIM        number of spatial dimensions.
! NSUBC       number of descendents per cell.
! NQUAD       number of independent quadrupole components.
 
        INTEGER :: NULL, NDIM, NSUBC, NQUAD
        PARAMETER(NULL = 0)
        PARAMETER(NDIM = 3)
        PARAMETER(NSUBC = 2**NDIM)
        PARAMETER(NQUAD = 2*NDIM - 1)
 
! -------------------------------------------------------
! Treecode control parameters, input from parameter file.
! -------------------------------------------------------
 
! HDLINE      identification string for the run.
! THETA       accuracy parameter.
! EPS         potential softening parameter.
! DTIME       N-body integration timestep.
! USQUAD      option to include quadrupole terms.
! NSTEPS      total number of timesteps in the simulation.
! NOUT        frequency of system record outputs.
 
        CHARACTER(32) :: HDLINE
        COMMON /TEXT/ HDLINE
 
        DOUBLE PRECISION :: THETA, EPS, DTIME
        LOGICAL :: USQUAD
        INTEGER :: NSTEPS, NOUT
        COMMON /PARAMS/ THETA, EPS, DTIME, USQUAD, NSTEPS, NOUT
 
! ----------------------------------
! System status and diagnostic data.
! ----------------------------------
        
! TNOW        value of current system time.
! MTOT        total mass of system.
! EKTOT       total kinetic energy.
! EPTOT       total potential energy.
! ETOT        sum of KE and PE.
! CMPOS       position of center of mass.
! CMVEL       velocity of center of mass.
! AMVEC       total angular momentum vector.
! CPUMKT      cpu time used in last tree construction.
! CPUACC      cpu time used in last force calculation.
! CPUT0       baseline for computing total cpu time.
! NBTOT       total number of body-body interactions.
! NCTOT       total number of body-cell interactions.
! NTMAX       maximum number of interactions for any body.
! NTAVG       average number of interactions per body.
 
        DOUBLE PRECISION :: TNOW, MTOT, EKTOT, EPTOT, ETOT, CMPOS(NDIM), &
             CMVEL(NDIM), AMVEC(NDIM), CPUMKT, CPUACC
        DOUBLE PRECISION :: CPUT0
        INTEGER :: NBTOT, NCTOT, NTMAX, NTAVG
        COMMON /STATUS/ TNOW, MTOT, EKTOT, EPTOT, ETOT, &
                       CMPOS, CMVEL, AMVEC, CPUMKT, CPUACC, &
                       CPUT0, NBTOT, NCTOT, NTMAX, NTAVG
 
! ---------------------------
! Tree structure data arrays.
! ---------------------------
 
! Bodies and cells are used to represent the tree.  The information for a
! given node is distributed across a number of arrays.  Quantities defined
! for both bodies and cells, such as mass and position, are stored in
! arrays with indicies running from 1 to MXNODE.  Quantities defined only
! for bodies are stored in arrays with indicies running from 1 to MXBODY,
! while those defined only for cells are stored in arrays with indicies
! running from INCELL to MXNODE.  With this convention, indicies can refer
! to either bodies or cells without conflict, and the type is deduced by
! comparing the index to the index of the first cell.  During tree
! construction, descendent indicies are stored in the SUBP arrays:
!
!          +-------------------------------------------------------+
! ROOT --> | CELL: MASS, POS, RCRIT2, QUAD, SUBP:[/,o,/,/,/,/,o,/] |
!          +----------------------------------------|---------|----+
!                                                   |         |
!     +---------------------------------------------+         |
!     |                                                       |
!     |    +--------------------------------+                 |
!     +--> | BODY: MASS, POS, VEL, ACC, PHI |                 |
!          +--------------------------------+                 |
!                                                             |
!     +-------------------------------------------------------+
!     |
!     |    +-------------------------------------------------------+
!     +--> | CELL: MASS, POS, RCRIT2, QUAD, SUBP:[o,/,/,o,/,/,o,/] |
!          +--------------------------------------|-----|-----|----+
!                                                etc   etc   etc
 
! MXBODY      maximum number of bodies allowed.
! MXCELL      maximum number of cells allowed.
! INCELL      index of first cell in arrays.
! MXNODE      maximum number of nodes (bodies + cells).

! NBODY       number of bodies in the system.
! NCELL       number of cells currently in use.
! MASS        mass of each node.
! POS         position of each node.
! VEL         velocity of each body.
! ACC         acceleration of each body
! PHI         potential of each body.
! RCRIT2      critical distances**2 of each cell.
! QUAD        quad moments of each cell.
! SUBP        descendent of each cell.
! TPOS        current position time.
! TVEL        current velocity time.
! RSIZE       side-length of root cell.
! ROOT        index of cell representing root.
 
        INTEGER :: MXBODY, MXCELL, INCELL, MXNODE
!        PARAMETER(MXBODY = 4096)
!        PARAMETER(MXBODY = 8192)
        PARAMETER(MXBODY = 43802)
!        PARAMETER(MXBODY = 87604)
        PARAMETER(MXCELL = MXBODY)
        PARAMETER(INCELL = MXBODY + 1)
        PARAMETER(MXNODE = MXBODY + MXCELL)
 
        INTEGER :: NBODY, NCELL, IPRINT
        DOUBLE PRECISION :: MASS(MXNODE), POS(MXNODE,NDIM)
        DOUBLE PRECISION :: VEL(MXBODY,NDIM), ACC(MXBODY,NDIM), PHI(MXBODY)
        DOUBLE PRECISION :: RCRIT2(INCELL:MXNODE), QUAD(INCELL:MXNODE,NQUAD)
        INTEGER SUBP(INCELL:MXNODE,NSUBC), ROOT
        DOUBLE PRECISION :: TPOS, TVEL, RSIZE
        COMMON /NODES/ NBODY, NCELL, MASS, POS, VEL, ACC, PHI, &
             RCRIT2, QUAD, SUBP, TPOS, TVEL, RSIZE, ROOT, IPRINT
 
! These definitions apply only during tree construction.

! MID         geometric center of each cell [equiv to POS].
! CLSIZE      size of each cell [equiv to RCRIT2].

	    DOUBLE PRECISION :: MID(MXNODE,NDIM), CLSIZE(INCELL:MXNODE)
	    EQUIVALENCE (MID(1,1), POS(1,1))
	    EQUIVALENCE (CLSIZE(INCELL), RCRIT2(INCELL))



! --------------------------
! Input/output unit numbers.
! --------------------------
 
        INTEGER UTERM, UPARS, UBODI, UBODO, UBODF, ULOG
        PARAMETER(UTERM =  6, UPARS = 10, UBODI = 11)
        PARAMETER(UBODO = 12, UBODF = 13, ULOG  = 14)
        INTEGER, PARAMETER :: PROC_RECV = 50
        INTEGER, PARAMETER :: RAW_SEND = 60
        
        
!    -----------------------------------------------
!	 MPI Declarations
!    -----------------------------------------------        

INTEGER :: nodes,workers,ierr,status(MPI_STATUS_SIZE), my_rank, mpi_index
INTEGER :: dest, source
INTEGER, PARAMETER :: MPILOG = 25
INTEGER, PARAMETER :: MASTER = 0
INTEGER, PARAMETER :: params_tag = 1
INTEGER, PARAMETER :: ind_tag = 2
INTEGER, PARAMETER :: root_tag = 3
INTEGER, PARAMETER :: POS_tag = 4
INTEGER, PARAMETER :: subp_tag = 5
INTEGER, PARAMETER :: rcrit2_tag = 6
INTEGER, PARAMETER :: mass_tag = 7
INTEGER, PARAMETER :: nb_tag = 8
INTEGER, PARAMETER :: acc_tag = 9
INTEGEr, PARAMETER :: phi_tag = 10


INTEGER,DIMENSION(2),PARAMETER :: Workload = (/ 21901, 21901  /)

COMMON /MPITERMS/ nodes,workers,ierr,status, my_rank,mpi_index