/****************************************************************************/
/* DEFS.H: include file for hierarchical force calculation routines.  The   */
/* definitions in this file are needed for load.c and grav.c; this file     */
/* does not provide definitions for other parts of the N-body code.         */
/*                                                                          */
/* Copyright (c) 1993 by Joshua E. Barnes, Honolulu, HI.                    */
/* It's free because it's yours.                                            */
/****************************************************************************/
/* adapted by Bernd Vollmer, CDS, Observatoire de Strasbourg 2004           */ 
/****************************************************************************/
#include "stdinc.h"
#include "real.h"
#include "vectmath.h"



/*
 * Body and cell data structures are used to represent the tree.  During
 * tree construction, descendent pointers are stored in the subp arrays:
 *
 *          +-------------------------------------------------------------+
 * root --> | CELL: mass, pos, next, rcrit2, more, subp:[/,o,/,/,/,/,o,/] |
 *          +----------------------------------------------|---------|----+
 *                                                         |         |
 *     +---------------------------------------------------+         |
 *     |                                                             |
 *     |    +--------------------------------------+                 |
 *     +--> | BODY: mass, pos, next, vel, acc, phi |                 |
 *          +--------------------------------------+                 |
 *                                                                   |
 *     +-------------------------------------------------------------+
 *     |
 *     |    +-------------------------------------------------------------+
 *     +--> | CELL: mass, pos, next, rcrit2, more, subp:[o,/,/,o,/,/,o,/] |
 *          +--------------------------------------------|-----|-----|----+
 *                                                      etc   etc   etc
 *
 * After the tree is complete, it is threaded to permit linear force
 * calculation, using the next and more pointers.  The storage used for
 * the subp arrays may be reused to store quadrupole moments.
 *
 *          +-----------------------------------------------+
 * root --> | CELL: mass, pos, next:/, rcrit2, more:o, quad |
 *          +---------------------------------------|-------+
 *                                                  |
 *     +--------------------------------------------+
 *     |                             
 *     |    +----------------------------------------+
 *     +--> | BODY: mass, pos, next:o, vel, acc, phi |
 *          +-----------------------|----------------+
 *                                  |
 *     +----------------------------+
 *     |
 *     |    +-----------------------------------------------+
 *     +--> | CELL: mass, pos, next:/, rcrit2, more:o, quad |
 *          +---------------------------------------|-------+
 *                                                 etc
 */




/*
 * NODE: data common to BODY and CELL structures.
 */
 
typedef struct _node {
  short type;                 /* code for node type */
  real mass;                  /* total mass of node */
  vector pos;                 /* position of node */
  real majaxis;
  real minaxis;
  real pa;
  struct _node *next;		/* link to next force-calc */ 
} node, *nodeptr;
 
#define Type(x) (((nodeptr) (x))->type)
#define Mass(x) (((nodeptr) (x))->mass)
#define Pos(x)  (((nodeptr) (x))->pos)
#define Next(x) (((nodeptr) (x))->next)
#define Majaxis(x) (((nodeptr) (x))->majaxis)
#define Minaxis(x) (((nodeptr) (x))->minaxis)
#define PA(x) (((nodeptr) (x))->pa)

/*
 * BODY: data structure used to represent particles.
 */
 
#define BODY 01                 /* type code for bodies */
 
typedef struct {
  node bodynode;              /* data common to all nodes */
  real frequency;
  real resolution;
  real raerror;
  real decerror;
  real flux;
  real fluxerror;
  real specindx;
  real absz;
  short resolved;
  short wflag;
  short cflag;
  short eflag;
  char name[22];
  short nparents;
  short nbrothers;
  short nsons;
  nodeptr parents[50];
  nodeptr brothers[50];
  nodeptr sons[50];
  bool done;
} body, *bodyptr;
 
#define Body    body

#define Frequency(x) (((bodyptr) (x))->frequency)
#define Resolution(x) (((bodyptr) (x))->resolution)
#define Raerror(x) (((bodyptr) (x))->raerror)
#define Decerror(x) (((bodyptr) (x))->decerror)
#define Flux(x) (((bodyptr) (x))->flux)
#define Fluxerror(x) (((bodyptr) (x))->fluxerror)
#define Specindx(x) (((bodyptr) (x))->specindx)
#define Absz(x) (((bodyptr) (x))->absz)
#define Resolved(x) (((bodyptr) (x))->resolved)
#define Wflag(x) (((bodyptr) (x))->wflag)
#define Cflag(x) (((bodyptr) (x))->cflag)
#define Eflag(x) (((bodyptr) (x))->eflag)
#define Name(x) (((bodyptr) (x))->name)
#define Nparents(x) (((bodyptr) (x))->nparents)
#define Nbrothers(x) (((bodyptr) (x))->nbrothers)
#define Nsons(x) (((bodyptr) (x))->nsons)
#define Parents(x) (((bodyptr) (x))->parents)
#define Brothers(x) (((bodyptr) (x))->brothers)
#define Sons(x) (((bodyptr) (x))->sons)
#define Done(x) (((bodyptr) (x))->done)

/*
 * CELL: structure used to represent internal nodes of tree.
 */
 
#define CELL 02                 /* type code for cells */
 
#define NSUB (1 << NDIM)        /* subcells per cell */
 
typedef struct {
    node cellnode;              /* data common to all nodes */
    real rcrit2;                /* critical c-of-m radius^2 */
    nodeptr more;		/* link to first descendent */   
    nodeptr subp[NSUB];         /* descendents of cell */              
} cell, *cellptr;
 
#define Rcrit2(x) (((cellptr) (x))->rcrit2)
#define More(x)   (((cellptr) (x))->more)
#define Subp(x)   (((cellptr) (x))->subp)


typedef struct {
  real cfrequency;
  real cresolution;
  real cmaxstructure;
  char cname[20];
  char cacro[20];
  char cbib[19];
  int csnumber;
} catalog, *catalogueptr;

#define Cfrequency(x) (((catalogueptr) (x))->cfrequency)
#define Cresolution(x) (((catalogueptr) (x))->cresolution)
#define Cmaxstructure(x) (((catalogueptr) (x))->cmaxstructure)
#define Cname(x) (((catalogueptr) (x))->cname)
#define Cacro(x) (((catalogueptr) (x))->cacro)
#define Cbib(x) (((catalogueptr) (x))->cbib)

#define Csnumber(x) (((catalogueptr) (x))->csnumber)

typedef struct {
  real compfrequency;
  bodyptr compnum;
  int compi;
} comppare, *comppareptr;

#define Compfrequency(x) (((comppareptr) (x))->compfrequency)
#define Compnum(x) (((comppareptr) (x))->compnum)
#define Compi(x) (((comppareptr) (x))->compi)

#define NONE 03

#define PARENT 04
#define BROTHER 05
#define SON 06

#define SOURCE 10
#define SOB 11
#define POB 12
#define SOP 13
#define POP 14
#define SOS 15
#define POS 16


/*
 * Variables used in tree construction.
 */
 
global cellptr root;                    /* pointer to root cell             */
 
global real rsize;                      /* side-length of root cell         */
 
global int cellused;			/* count of cells in tree           */

global int maxlevel;			/* count of levels in tree          */

global bodyptr bodycoll;                /* points to array of bodies after collision */

global catalogueptr catalogue; 

/*
 * Parameters and results for gravitational calculation.
 */
 
global string options;                  /* various option keywords          */
 
global real theta;                      /* accuracy parameter: 0.0 => exact */
 
global bool usequad;		        /* use quadrupole corrections       */

global real epsgrav;                    /* potential softening parameter    */
 
global int n2bterm;                     /* number 2-body of terms evaluated */
 
global int nbcterm;                     /* num of body-cell terms evaluated */

global bool nextneigh;			/* change for search of next neighbor */

global vector offset;			/* root cell offset                 */
	
global int nbody;

global int pnumber;
global int bnumber;
global int snumber;
		
global int ncatalogue;

global char dateiname[20];

global int zaehler;

global int excor;

global int cmore; 

global int maxbrothers;
global int maxsons;
global int maxparents;

global int njoin;

global int ncross;

global int ncompress;

global int ndouble;

global int nspectra;

global int specrun;

global int nresolved;

global real minborder;

global real maxborder;

global int pole;

global real maxstr;

global real resol;

nodeptr store[500];

int ccount;

bool doedge;

int maketr;

/*
 * Utility routines used in load.c and grav.c.  These are defined in
 * util.c, which must be compiled with the same choice of precision.
 */


bool scanopt(string, string);			/* find option in string    */
real cputime(void);				/* return elapsed CPU time  */
void *allocate(int);				/* allocate and zero memory */
real distv(vector, vector);			/* distance between vectors */
void error(string, ...);			/* report error and exit    */
void eprintf(string, ...); 			/* printf to error stream   */







