#include "head.h"
#define exp1(seed) -log(rand1(seed))
#include "rand01.h"
#include "init.h"

void initialise(
		double sigma,       /* value of $\sigma$ */
		double tau,         /* value of $\tau$ */
		double lambda,      /* value of $\lambda$ */
		double gamma,       /* value of $\gamma$ */
		double *mG,         /* value of $m(G)$ */
		int length,         /* number of points in process */
		location points[],  /* all the info about the points */
		long seed,          /* Two arbitrarily chosen seeds */
		long seedtwo        /* for the process. */
		)

{
  double con=0.0;             /* con$=\tau/(2\sigma^2(\sigma^2+\tau))$ */
  double loglambda=0.0;       /* $\log\lambda$ for each */
                              /* \texttt{points[i].lambda} in turn */
  double sum;                 /* sum is used to generate Poisson */
                              /* random variables as a sum of */
                              /* Exponential random variables */
  int i,j;

  *mG = 9;                    /* CHANGE THIS!!! */
  con=tau/(2*sigma*sigma*(sigma*sigma+tau));
  for(i=0;i<length;++i){
    loglambda = log(lambda) + points[i].data*points[i].data * con;
/* $\lambda = e^{d^2\tau/(2\sigma^2(\sigma^2+\tau))}$.
   See thesis for details. If $\lambda$ is too big make the point 
   `always on'.  This is to prevent rounding errors and make the process 
   computationally feasible. */
    points[i].lambda = exp(loglambda);

    if(loglambda <= MAXLOGLAMBDA)
      points[i].continuous = 0;
    else{
      points[i].continuous = 1;
      if(loglambda <= MAXCONT){
	sum=0;
	for(j=0;sum<=points[i].lambda;++j)
	  sum+=exp1(&seed);
	points[i].life.max=points[i].life.min=j-1;
      }
      else
	points[i].life.max=points[i].life.min=1;
      points[i].events = 0;
    }

/* If point is not `always on' then... */
    if(points[i].continuous==0){

/* Find out how many points are initially alive */
      sum=0;
      for(j=0;sum<=points[i].lambda;++j)
	sum+=exp1(&seed);
      points[i].initlives=j-1;

/* Give each point which is not `always on' a random seed for marks */
      points[i].markseed=QDA*((unsigned long)seed)+QDC;
      rand1(&seedtwo);

/* Give each point which is not `always on' a random seed for life-length */
      points[i].timeseed=QDA*((unsigned long)seedtwo)+QDC;
    }

/* Give each point a label for later identification so that points at 
   different locations in the configuration may be treated differently. */
    points[i].label=i;

/* Calculate the value of pmin for each point in turn. */
    points[i].pmin = pow(gamma,(-*mG))*sqrt(sigma*sigma/(sigma*sigma+tau))*exp(-points[i].data*points[i].data * con);
  }
}
