/* Applies a time stepping method in a predator-prey simulation.
 * Compile with gcc and -lm flag for the sqrt. */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* Set verbose to 1 to see more output. */
#define verbose 0

void prey ( double t, double v, double *x, double *y );
/*
 * DESCRIPTION :
 *   Returns the coordinates of the prey, moving at constant speed v,
 *   at time t.
 *
 * ON ENTRY :
 *   t        current time;
 *   v        speed of the prey.
 *
 * ON RETURN :
 *   x        x-coordinate of the prey;
 *   y        y-coordinate of the prey. */

void predator
 ( double s, double x, double y, double v, double *px, double *py );
/*
 * DESCRIPTION :
 *   Returns the coordinates of the predator, moving at constant speed v
 *   towards the location of the prey.  If the distance of the predator
 *   to the prey is less than 1.0e-1, then the prey is caught and 
 *   the values of px and py match the values of x and y on return.
 *
 * ON ENTRY :
 *   s        value of the time step;
 *   x        x-coordinate of the prey;
 *   y        y-coordinate of the prey;
 *   v        speed of the predator;
 *   px       current x-coordinate of the predator;
 *   py       current y-coordinate of the predator.
 *
 * ON RETURN :
 *   px       updated x-coordinate of the predator;
 *   py       updated y-coordinate of the predator. */
 
void trajectory
 ( double endt, int dim, double v,
   double *x, double *y, double *px, double *py,
   double *endlast, int *idxlast );
/*
 * DESCRIPTION :
 *   Computes the trajectories of prey and predator.
 *
 * ON ENTRY :
 *   endt     end time of the simulation;
 *   dim      number of discretization steps;
 *   v        constant speed of the predator;
 *   x        is an array of dim doubles, with x[0] initialized
 *            with the value of the x-coordinate of the prey at time zero;
 *   y        is an array of dim doubles, with y[0] initialized
 *            with the value of the y-coordinate of the prey at time zero;
 *   px       is an array of dim doubles, with px[0] initialized
 *            with the value of the x-coordinate of the predator at time zero;
 *   py       is an array of dim doubles, with py[0] initialized
 *            with the value of the y-coordinate of the predator at time zero.
 *
 * ON RETURN :
 *   x        computed x-coordinates for the trajectory of the prey;
 *   y        computed y-coordinates for the trajectory of the prey;
 *   px       computed x-coordinates for the trajectory of the predator;
 *   py       computed y-coordinates for the trajectory of the predator;
 *   endlast  the time where the simulation ended differs from endt
 *            if the predator caught the prey;
 *   idxlast  the index in the arrays x, y, px, py, for the coordinates
 *            of the location where the prey was caught,
 *            if idxlast > dim, then the prey escaped. */

int main ( int argc, char* argv[] )
{
   double *xprey, *yprey, *xpred, *ypred;
   const double endtime = 40.0;
   const int dim = 1000;
   double last;
   int idx;

   printf("A predator-prey simulation ...\n");

   xprey = (double*) calloc(dim+1, sizeof(double));
   yprey = (double*) calloc(dim+1, sizeof(double));
   xpred = (double*) calloc(dim+1, sizeof(double));
   ypred = (double*) calloc(dim+1, sizeof(double));

   xprey[0] = 0.0; yprey[0] = 0.0;
   xpred[0] = 0.0; ypred[0] = 10.0;

   trajectory(endtime, dim, 1.3, xprey, yprey, xpred, ypred, &last, &idx);

   if(idx > dim)
      printf("The prey escaped.\n");
   else
   {
      printf("Prey caught at time : %f, at index %d\n", last, idx);
      printf("xprey = %f, yprey = %f\n", xprey[idx], yprey[idx]);
      printf("xpred = %f, ypred = %f\n", xpred[idx], ypred[idx]);
   }
   return 0;
}

void prey ( double t, double v, double *x, double *y )
{
   *x = v*t;
   *y = 0.0;
}

void predator
 ( double s, double x, double y, double v, double *px, double *py )
{
   double xdir = x - *px;
   double ydir = y - *py;
   double nrm = sqrt(xdir*xdir + ydir*ydir);
   if(verbose == 1)
      printf("xdir = %f, ydir = %f, nrm = %f\n", xdir, ydir, nrm);

   if(nrm < 1.0e-1)
   {
      *px = x; *py = y; 
   }
   else
   {
      xdir = xdir/nrm;
      ydir = ydir/nrm;
      *px = *px + s*v*xdir;
      *py = *py + s*v*ydir;
   }
}

void trajectory
 ( double endt, int dim, double v,
   double *x, double *y, double *px, double *py,
   double *endlast, int *idxlast )
{
   double step = endt/dim;
   double timestep = 0.0;
   int k;

   *endlast = endt;
   *idxlast = dim+1;

   for(k=1; k<=dim; k++)
   {
      timestep = timestep + step;  
      prey(timestep, 1.0, &x[k], &y[k]);
      if(verbose == 1) printf("prey[%d] = %f, %f\n", k, x[k], y[k]);
      px[k] = px[k-1]; py[k] = py[k-1];
      predator(step, x[k], y[k], v, &px[k], &py[k]);
      if(verbose == 1) printf("predator[%d] = %f, %f\n", k, px[k], py[k]);
      if((x[k] == px[k]) && (y[k] == py[k]))
      {
         *endlast = timestep;
         *idxlast = k;
         break;
      }
   }
}
