Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(int argc, char *argv[])
{
int N, i = 0, j = 0, trial, to = 0, dis = 0, maxstep = 0, nstep = 0;
double tao, tau, dx, counter = 0, u = 0, a = 0, dt = 0, t = 0, g = 0;
double *array, *b, *times;
if(argc != 7)
{
printf("Error!\nUsage: N(int), tao(double), a(double), dx(double), to(int), g(double)\n");
return 0;
}
else
{
sscanf( argv[1], "%d", &N);
}
sscanf( argv[2], "%lf", &tao);
sscanf( argv[3], "%lf", &a);
sscanf( argv[4], "%lf", &dx);
sscanf( argv[5], "%d", &to);
sscanf( argv[6], "%lf", &g);
tau = tao * (1 + g * t);
dt = -log(a) * tau;
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
gsl_rng_set (r, time(NULL));
array = (double*) malloc (to * to * sizeof(double));
for(i = 0; i < to * to; i++)
{
array[i] = 0.0;
}
while(t < to)
{
tau = tao * (1 + g * t);
dt = -log(a) * tau;
t = t + dt;
nstep++;
}
maxstep = nstep;
b = (double *) malloc (maxstep * maxstep * sizeof(double));
times = (double *) malloc (maxstep *sizeof(double));
for(i = 0; i < maxstep * maxstep; i++)
{
b[i] = 0.0;
}
for(trial = 0; trial < N; trial++)
{
nstep = 0;
t = 0.0;
dis = 0;
counter = 0.0;
while(t < to)
{
tau = tao * (1 + g * t);
dt = -log(a) * tau;
u = gsl_ran_flat (r, 0.0, 1.0);
b[nstep * maxstep + dis]++;
if(u > a)
{
dis++;
}
t = t + dt;
nstep++;
times[nstep] = t; //Segmentation Fault...why?
counter = counter + nstep * b[nstep * maxstep + dis]/N;
fprintf(stdout, "%f %f\n", times[nstep], counter);
}
}
for(i = 0; i < maxstep; i++)
{
for(j = 0; j < maxstep; j++)
{
fprintf(stderr, "%lf ", b[j * maxstep + i]);
}
fprintf(stderr, "\n");
}
free(array);
free(b);
return 0;
}