Ejemplo n.º 1
0
void sled(double k, double q, double p, const QString& fileName)
{
  QFile f(fileName);
  f.open(QIODevice::WriteOnly);
  QTextStream stream(&f);
  auto i = new GslIntegrator();
  double y[] = {q, p};
  
  gsl_odeiv2_system sys = {fun_zoga, 0, 2, &k};
  gsl_odeiv2_driver* driver = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk4, 1e-4, 1e-3, 1e-3);
  double t = 0.0;
  while (t < 20.0)
  {
    int c = fmod(t, 2) > 1 ? 1 : 0;
    double q = y[0], p = y[1];
    omeji(&q, &p);
    stream << t << " " << q << " " << p << " " << c << endl;

    int status = gsl_odeiv2_driver_apply_fixed_step(driver, &t, 1e-4, 1, y);
    if (status != GSL_SUCCESS)
    {
      qDebug() << t << gsl_strerror(status);
      break;
    }
  }
  
  f.close();
}
Ejemplo n.º 2
0
int
main (void)
{
  double mu = 10;
  gsl_odeiv2_system sys = {func, jac, 2, &mu};

  gsl_odeiv2_driver * d = 
    gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd,
				  1e-6, 1e-6, 0.0);
  int i;
  double t = 0.0, t1 = 100.0;
  double y[2] = { 1.0, 0.0 };

  for (i = 1; i <= 100; i++)
    {
      double ti = i * t1 / 100.0;
      int status = gsl_odeiv2_driver_apply (d, &t, ti, y);

      if (status != GSL_SUCCESS)
	{
	  printf ("error, return value=%d\n", status);
	  break;
	}

      printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
    }

  gsl_odeiv2_driver_free (d);
  return 0;
}
int
main (void)
{
    FILE *fp;
    double mu = -4.0;
    gsl_odeiv2_system sys = {func, jac, 2, &mu};
    
    gsl_odeiv2_driver * d =
    gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd,
                                   1e-6, 1e-6, 0.0);
    int i;
    double x = 0.0, x1 = 4*3.1415926;
    double y[2] = { 10.0, 1.0 };
    fp = fopen( "output.txt", "w" );
    
    for (i = 1; i <= 100; i++)
    {
        double ti = i * x1 / 100.0;
        int status = gsl_odeiv2_driver_apply (d, &x, ti, y);
        
        if (status != GSL_SUCCESS)
        {
            printf ("error, return value=%d\n", status);
            break;
        }
        
        printf ("%.5e %.5e %.5e\n", x, y[0], y[1]);
        fprintf( fp, "%.5e %.5e %.5e\n", x, y[0], y[1]);
    }
    fclose( fp );
    
    gsl_odeiv2_driver_free (d);
    return 0;
}
Ejemplo n.º 4
0
int main(int argc, char **argv) {
    enum Constexpr {n_points = 1000};
    double mu = 1.0;
    gsl_odeiv2_system sys = {ode_func, ode_jac, 2, &mu};
    gsl_odeiv2_driver * d= gsl_odeiv2_driver_alloc_y_new(
        &sys,
        gsl_odeiv2_step_rk8pd,
        1e-6,
        1e-6,
        0.0
    );
    int i;
    double t = 0.0;
    double t1 = 100.0;
    /* Initial condition: f = 0 with speed 0. */
    double y[2] = {1.0, 0.0};
    double dt = t1 / n_points;
    double datax[n_points];
    double datay[n_points];

    for (i = 0; i < n_points; i++) {
        double ti = i * dt;
        int status = gsl_odeiv2_driver_apply(d, &t, ti, y);
        if (status != GSL_SUCCESS) {
            fprintf(stderr, "error, return value=%d\n", status);
            break;
        }

        /* Get output. */
        printf("%.5e %.5e %.5e\n", t, y[0], y[1]);
        datax[i] = y[0];
        datay[i] = y[1];
    }

    /* Cleanup. */
    gsl_odeiv2_driver_free(d);

    /* Plot. */
    if (argc > 1 && argv[1][0] == '1') {
        plsdev("xwin");
        plinit();
        plenv(
            gsl_stats_min(datax, 1, n_points),
            gsl_stats_max(datax, 1, n_points),
            gsl_stats_min(datay, 1, n_points),
            gsl_stats_max(datay, 1, n_points),
            0,
            1
        );
        plstring(n_points, datax, datay, "*");
        plend();
    }

    return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
int
integrate_ode_using_driver (double t, double t1, double y[], int n_steps,
			    double h_init, double h_max, double eps_abs,
			    double eps_rel, void *params, int print_values)
{
  int i; // Counter in macro-step loop
  int j; // Counter in print loop
  int status;
  size_t dim = NY;
  double ti;
  double dt = (t1-t)/(double)n_steps;
  const gsl_odeiv2_step_type * T = gsl_odeiv2_step_msbdf;
  gsl_odeiv2_step * s = gsl_odeiv2_step_alloc (T, dim);

  gsl_odeiv2_system sys = {func, jac, dim, params};

  gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_msbdf, h_init, eps_abs, eps_rel);
  gsl_odeiv2_step_set_driver(s, d);

  if (h_max > 0.0)
    {
      gsl_odeiv2_driver_set_hmax(d, h_max);
    }

  for (i = 0; i < n_steps; ++i)
    {
      // Macro-step loop
      ti = t + dt*(i+1);
      status = gsl_odeiv2_driver_apply (d, &t, ti, y);

      if (status != GSL_SUCCESS)
	{
	  printf ("error, return value=%d\n", status);
	  break;
	}
      if (print_values)
	{
	  printf(STRINGIFY(PRECISION), t);
	  for (j = 0; j < NY; ++j)
	    {
	      printf(" " STRINGIFY(PRECISION), y[j]);
	    }
	  printf("\n");
	}

    }

  gsl_odeiv2_driver_free (d);
  gsl_odeiv2_step_free (s);

  return status;
}
Ejemplo n.º 6
0
void inittransitionengine() {
  sys.function = &odesys;
  sys.jacobian = NULL;
  sys.dimension = xmax * ymax * nspecies;
  sys.params = NULL;
  stepper = gsl_odeiv2_step_alloc(gsl_odeiv2_step_rkf45, xmax * ymax * nspecies);
  driver = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkf45, timestep, 1e-6, 0.0);
  yt = malloc(sizeof(double) * xmax * ymax * nspecies);
  yerr = malloc(sizeof(double) * xmax * ymax * nspecies);
  for (int i = 0; i < xmax * ymax * nspecies; i++) {
    yerr[i] = 0;
  }
}
Ejemplo n.º 7
0
	void
	SirDeterministic::simulate(const double beta, const double alpha, const double gamma, const double I0)
	{
		
		iGraph_.clear();
		
		// Parameters
		double parms[3];
		parms[0] = beta / N_; // Density independent!
		parms[1] = alpha;
		parms[2] = gamma;
		
		double y[4] = {N_-I0,I0,0.0,0.0};
		
		gsl_odeiv2_system sys = {model, NULL, 4, parms};
		gsl_odeiv2_driver* driver = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkf45, 1e-4, 1e-4, 0.0);
		
		double t = 0.0;

		// Store initial conditions
		CurveVal tmp;
		tmp.value  = 1.0;
		tmp.integral = 0.0;
		iGraph_.insert(std::make_pair(0.0,tmp));

		// Run model forward
		while (y[2]>=0.5 or y[1]>=0.5)
		{
			
			int status = gsl_odeiv2_driver_apply (driver, &t, t+delta_, y);
			if(status != GSL_SUCCESS)
			{
				std::cerr << "Error, return value = " << status << std::endl;
			}
			
			double integral = (--iGraph_.end())->second.integral; // Integral at the previous time point
			integral += ( (--iGraph_.end())->second.value + y[2] )/2 * delta_;
	
			tmp.value = y[2];
			tmp.integral = integral;
			iGraph_.insert(std::make_pair(t,tmp));
		}
		
		gsl_odeiv2_driver_free(driver);

		// Cache max time
		maxT_ = (--iGraph_.end())->first;
		maxIntegral_ = (--iGraph_.end())->second.integral;
		
	}
Ejemplo n.º 8
0
void portret(double k, const QString& fileName)
{
  
  // TODO: Direktro uporabi GSL, da ne porablja toliko RAMa. 
  Interval i = qMakePair(0.0, 10.0);
  Integrator* integrator = new GslIntegrator;
  
  QImage image(QSize(800,800), QImage::Format_Indexed8);
  for (int j = 0; j < 256; ++j)
  {
    image.setColor(j, qRgb(j, j, j));
  }
  image.fill(255);
  
  gsl_odeiv2_system sys = {fun_zoga, 0, 2, &k};
  gsl_odeiv2_driver* driver = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk4, 1e-3, 1, 1);
  double y[2];
  double t;
    
  for (double q = 0; q < 1; q += 0.02)
  {
    qDebug() << "q = " << q;
    for (double p = -5; p < 5; p += 0.05)
    {
      y[0] = q;
      y[1] = p;
      t = 0.0;
      double y[] = {q, p};
      while (t < 100.0)
      {
        int status = gsl_odeiv2_driver_apply_fixed_step(driver, &t, 1e-3, 1000, y);
        if (status != GSL_SUCCESS)
        {
          qDebug() << t << gsl_strerror(status);
          break;
        }
        double qq = y[0];
        double pp = y[1];
        omeji(&qq, &pp);
        uchar& pix = image.scanLine( qBound<int>(0, (5+pp) * 80.0, 799) )[ qBound<int>(0, qq * 800.0, 799) ];
        if (pix > 100)
        {
          pix -= 100;
        }
      }
    }
  }
  image.save(fileName);
}
Ejemplo n.º 9
0
    functionSign System::reachabilityUsingSampling(Point* p1, Point* p2){
        functionSign result = undefined; 
        int samples=3;
        double* f = new double[2];
        
        Point* tmp = new Point(2);
        double p1x = p1->getData(0);
        double p1y = p1->getData(1);
        double p2x = p2->getData(0);
        double p2y = p2->getData(1);
        for(int i=1;i<samples+1;i++){
            //sample point
            double y[2] = { p1x + i*(p2x-p1x)/(samples+1), p1y + i*(p2y-p1y)/(samples+1) };
            //code 1: sampling using differential trajectory:
            //func(0, y, f, &mu);
            //y[0] += f[0];
            //y[1] += f[1];
            //code 2: sampling using simulation
            gsl_odeiv2_system sys = {func, jac, 2, &mu};
            gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, 1e-6, 1e-6, 0.0);
            double t = 0.0, ti = 1;
            gsl_odeiv2_driver_apply (d, &t, ti, y);
            gsl_odeiv2_driver_free (d);
            tmp->setData(0, y[0]);
            tmp->setData(1, y[1]);
            
            //positition of the trejectory w.r.t p1-p2 line
            int position1 = geometry::position(p1, p2, tmp) ;

            //compute the direction of vector flow at the sampling point
            switch (result) {
                case positive:
                    result = (position1==1)? positive: posnegative;
                    break;
                case negative:
                    result = (position1==1)? posnegative: negative;
                    break;
                case posnegative:
                    result = (position1==1)? posnegative: posnegative;
                    break;
                case undefined:
                    result = (position1==1)? positive: negative;
                    break;
            }
            
        }
        delete f;
        return result ;
    }
Ejemplo n.º 10
0
void polysolve(double hstep, bool listmodel)
{
  double h, rpmw[4], w1, w2, t;
  gsl_odeiv2_system sys = { diffrpmw, NULL, 4, NULL };
  gsl_odeiv2_driver *drv;
  int stat;
 
  if (npol <= 0.5 || mpol <= -1.0)
    error("%s: illegal value for n or m\n", getprog());
  if (npol >= 3*mpol + 5)
    error("%s: model would have infinite radius\n", getprog());
  Kprime = K * (mpol+1) * pow(PI, -1.5) * pow(2.0, - (mpol+1.5)) *
           exp(lgamma(mpol+npol+1) - lgamma(mpol+2) - lgamma(npol-0.5));
  h = hstep;
  do {
    drv = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk4,
					h, 1.0, 1.0);
    rpmw[0] = 0.0;				// initialize radius
    rpmw[1] = PHI0;				// initialize potential
    rpmw[2] = 0.0;				// initialize enclosed mass
    rpmw[3] = 0.0;				// initialize binding energy
    nstep = 0;
    storestep(rpmw);
    asmpstep(rpmw, h);				// use asymp. approximation
    storestep(rpmw);
    while (rpmw[1] < 0.0) {			// while potential is neg.
      stat = gsl_odeiv2_driver_apply_fixed_step(drv, &t, h, 1, rpmw);
      if (stat)
	eprintf("[%s.polysolve: stat = %d]\n", getprog(), stat);
      storestep(rpmw);
    }
    fixsurface();
    w1 = rpmw[3] - 0.5 * rsqr(mtot) / rad1;
    w2 = - (2*mpol + 3) / (3*mpol - npol + 5) * rsqr(mtot) / rad1;
    eprintf("[%s.polysolve: nstep = %3d  W = %f  error = %f]\n",
	    getprog(), nstep, w2, (w1 - w2)/w2);
    gsl_odeiv2_driver_free(drv);
    h = 0.5 * h;				// refine step-size by 2
  } while (nstep < MAXSTEP/4);
  polyscale();
  pmspline = gsl_spline_alloc(gsl_interp_cspline, nstep);
  gsl_spline_init(pmspline, rtab, ptab, nstep);
  if (listmodel) {
    printf("#%11s %11s %11s\n", "radius", "mass", "potential");
    for (int i = 0; i < nstep - 1; i++)
      printf(" %11.6f %11.6f %11.6f\n", rtab[i], mtab[i], ptab[i]);
    printf(" %11.6f %11.6f %11.6f\n", rad1, mtot, phi1);
  }
}
Ejemplo n.º 11
0
void	evolution(double* S)	{	
	FILE* 	fp;
  int			info;
  double	bx_t,by_t,bz_k;
	//sum1 Skz//sum2 bz_k*Skx//sum3 bz_k^2*Sky
	double  sum1,sum2,sum3;
  double	Mu = 0;	//void par,will not use
  double	tau=0.005;
  double	t1,t;

	const	char* datafile="../data/driven_circular3.txt";
	
  cal_phys(S, &bx_t, &by_t, &sum1, &sum2, &sum3); 

  printf ("%lf %lf %lf %lf %lf %lf\n", t, bx_t,by_t,sum1,sum2, sum3);

	fp = fopen(datafile, "w");
  fprintf (fp,"%s %s %s %s %s %s\n", "t", "bx_t","by_t","sum1","sum2","sum3");

  //Initialize the evoling process
  gsl_odeiv2_system sys = {odefun, NULL, 3*g_dim, &Mu};
  gsl_odeiv2_driver * odedriver = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, tau, g_eps, g_eps);
  gsl_odeiv2_driver_set_hmax (odedriver, tau);

  //do the evolution
	t=0.;
  while(t<g_endtime)	{
  	t1=t+tau;
  	info = gsl_odeiv2_driver_apply (odedriver, &t, t1, S);

  	if (info != GSL_SUCCESS)	{
  		printf ("error: driver returned %d\n", info);
  		exit(1);
  	}

	 cal_phys(S, &bx_t, &by_t, &sum1, &sum2, &sum3);


   printf ("%lf %lf %lf %lf %lf %lf\n", t, bx_t,by_t,sum1,sum2, sum3);
   fprintf (fp,"%lf %lf %lf %lf %lf %lf\n", t, bx_t,by_t,sum1,sum2, sum3);
	 fflush(fp);
  }

	fclose(fp);
  gsl_odeiv2_driver_free (odedriver);
}
Ejemplo n.º 12
0
int
main () {
  double alpha = 2;
  double beta = 4;
  double gamma = 7; //3 and 7
  double gmax = 5;

  // you can use any stepper here
  const gsl_odeiv2_step_type * T = gsl_odeiv2_step_rk4imp;
  gsl_odeiv2_step * s    = gsl_odeiv2_step_alloc(T, 3);
  gsl_odeiv2_control * c = gsl_odeiv2_control_y_new(1e-6, 0.0);
  gsl_odeiv2_evolve * e  = gsl_odeiv2_evolve_alloc(3);
  predprey_params pars = {alpha,beta,gamma,gmax,0,0};     /* the parameters */
  gsl_odeiv2_system sys = {predprey, jac_predprey, 3, &pars};

  gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(&sys, T, 1e-6, 1e-6, 1e-6 );
  gsl_odeiv2_step_set_driver(s, d);
     
  double t = 0.0, t1 = 20.0;
  double h = 1e-6;
  double x[3] = { 1.0, 0.1, 3.0 };
  
  double t2 = t;
  double interval = 0.01;
  while (t < t1)
  {
    int status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t, t1, &h, x);
    
    if (status != GSL_SUCCESS)
      break;
    if(t > t2+interval) {
      printf ("%.5e %.5e %.5e %.5e\n", t, x[0], x[1], x[2]);
      t2 = t;
    }
  }
  
  gsl_odeiv2_evolve_free (e);
  gsl_odeiv2_control_free (c);
  gsl_odeiv2_step_free (s);
  fprintf(stderr,"Number of Jacobian evaluations = %d\n"
       "Number of Function evaluations = %d\n", pars.jac_count,
 pars.count);
  return 0;

}
Ejemplo n.º 13
0
void	evolution(double* y)	{	
	FILE* 	fp;
  int			info;
  double	Delta_Re,Delta_Im,Delta,Ne;
  double	Mu = 0;	//void par,will not use
  double	tau=0.005;
  double	t1,t;

	const	char* datafile="../data/quench_large.txt";
	
  cal_phys(y, &Delta_Re, &Delta_Im, &Delta, &Ne); 
    printf ("%lf %lf %lf %lf %lf\n", t, Delta_Re,Delta_Im,Delta,Ne);

	fp = fopen(datafile, "w");
  fprintf (fp,"%s %s %s %s %s\n", "t", "Delta_Re","Delta_Im","|Delta|","Ne");

  //Initialize the evoling process
  gsl_odeiv2_system sys = {odefun, NULL, 4*Ns, &Mu};
  gsl_odeiv2_driver * odedriver = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, tau, eps, eps);
  gsl_odeiv2_driver_set_hmax (odedriver, tau);

  //do the evolution
	t=0.;
  while(t<endtime)	{
  	t1=t+tau;
  	info = gsl_odeiv2_driver_apply (odedriver, &t, t1, y);

  	if (info != GSL_SUCCESS)	{
  		printf ("error: driver returned %d\n", info);
  		exit(1);
  	}

    cal_phys(y, &Delta_Re, &Delta_Im, &Delta, &Ne); 


    printf ("%lf %lf %lf %lf %lf\n", t, Delta_Re,Delta_Im,Delta,Ne);
    fprintf (fp,"%lf %lf %lf %lf %lf\n", t, Delta_Re,Delta_Im,Delta,Ne);
		fflush(fp);
  }

	fclose(fp);
  gsl_odeiv2_driver_free (odedriver);
}
Ejemplo n.º 14
0
 vector<pair<double, double> > System::simulate(){
     vector<pair<double, double> > trace ;
     gsl_odeiv2_system sys = {func, jac, 2, &mu};
     gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, 1e-6, 1e-6, 0.0);
     double t = 0.0, t1 = 20.0;
     double* state = getInitialState();
     double y[2] = { state[0], state[1] };
     for (int i = 1; i <= 1000; i++){
         double ti = i * t1 / 1000.0;
         int status = gsl_odeiv2_driver_apply (d, &t, ti, y);
         if (status != GSL_SUCCESS){
             printf ("error, return value=%d\n", status);
             break;
         }
         trace.push_back(make_pair(y[0], y[1]));
         //printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
     }
     delete state;
     gsl_odeiv2_driver_free (d);
     return trace;
 }    
Ejemplo n.º 15
0
// Calculate energy downshift:
double StopPow::Eout(double E, double x) throw(std::invalid_argument, std::domain_error)
{
	// sanity checking:
	if( E < get_Emin() || E > get_Emax() || x < 0 )
	{
		std::stringstream msg;
		msg << "Energies passed to StopPow::Eout are bad: " << E << "," << x;
		throw std::invalid_argument(msg.str());
	}
	
	// set up GSL ODE solver
	gsl_odeiv2_system sys = {Eout_func, NULL, 1, this};
	gsl_odeiv2_driver * d = 
		gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk4,
				  1e-6, 1e-6, 0.0);
	
	double x0 = 0.; double x1 = x; // range of ODE integration
	double y[1] = { E };
	int status;
	try
	{
		status = gsl_odeiv2_driver_apply(d, &x0, x1, y);
	}
	catch(std::invalid_argument e)
	{
		return 0; // got to the end of the range
	}

	// check for errors:
	if( status != GSL_SUCCESS )
	{
		throw std::domain_error("GSL RK4 ODE integration failed in StopPow::Eout!");
	}

	gsl_odeiv2_driver_free (d);

	// make sure we do not return a negative energy:
	return fmax( y[0] , 0.0 );
}
Ejemplo n.º 16
0
void SQuIDS::Evolve(double dt){
  if(AnyNumerics){
    int gsl_status = GSL_SUCCESS;

    // initial time
    
    // ODE system error control
    gsl_odeiv2_driver* d = gsl_odeiv2_driver_alloc_y_new(&sys,step,h,abs_error,rel_error);
    gsl_odeiv2_driver_set_hmin(d,h_min);
    gsl_odeiv2_driver_set_hmax(d,h_max);
    gsl_odeiv2_driver_set_nmax(d,0);
    
    double* gsl_sys = system.get();
    
    if(adaptive_step){
      gsl_status = gsl_odeiv2_driver_apply(d, &t, t+dt, gsl_sys);
    }else{
      gsl_status = gsl_odeiv2_driver_apply_fixed_step(d, &t, dt/nsteps , nsteps , gsl_sys);
    }
    
    gsl_odeiv2_driver_free(d);
    if( gsl_status != GSL_SUCCESS ){
      throw std::runtime_error("SQUIDS::Evolve: Error in GSL ODE solver ("
                               +std::string(gsl_strerror(gsl_status))+")");
    }
    
    //after evolving, make estate alias state again
    for(unsigned int ei = 0; ei < nx; ei++){
      for(unsigned int i=0;i<nrhos;i++)
        estate[ei].rho[i].SetBackingStore(&(system[ei*size_state+i*size_rho]));
      if(nscalars>0)
        estate[ei].scalar=&(system[ei*size_state+nrhos*size_rho]);
    }
  }else{
    t+=dt;
    PreDerive(t);
  }
}
Ejemplo n.º 17
0
int main(void)
{
	gsl_odeiv2_system sys = {func, NULL, 2, NULL};

	// prams: system, driver, initial step size, absolute error, relative error
	gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk4, 1e-6, 1e-6, 0.0);

	int i;
	double t = 0.0, t1 = 100.0;
	struct variable_vector *vv;
	
	vv = malloc(sizeof(struct variable_vector));
	
	vv->x1 = 1.0;
	vv->x2 = 0.0;

	for (i = 1; i <= 10000; i++)
	{
		double ti = i * t1 / 10000.0;
		int status = gsl_odeiv2_driver_apply (d, &t, ti, (double *)vv);

		if (status != GSL_SUCCESS)
		{
			printf ("error, return value=%d\n", status);
			break;
		}

		printf ("%.5e %.5e %.5e\n", t, vv->x1, vv->x2);
	}

	gsl_odeiv2_driver_free (d);

	free(vv);

	return 0;
}
Ejemplo n.º 18
0
int main(void) {

	FILE *fp;	// create file stream
	char filename[] = "outputfile.txt";	// file name here
	// open the file:

	if ((fp = fopen(filename, "w")) == NULL ) {
		printf("I can't open the file for data save !!!\n");
		exit(1);
	}

	/***************** the begining of the GSL part **************************/
	/*kappa_0, kappa_1, kappa_2,  c_0,                  c_H               k_1,     k_3,             k_4, 	                 rho_0 = \tilde{k}_4,                          d_0*/

	struct rparams p =

	{ 0.0, 0.0, 0.0, 0.0 * 0.000075, REDUCTOR_PREFACTOR * 0.001, 0.0 * 1.188,
			0.0387, STALA_DO_A_R * 7.53 * 100000, STALA_DO_B_R * 7.53 * 100
			* REDUCTOR_PREFACTOR, 0.000075 };

	gsl_odeiv2_system sys = { func, NULL, S + 4, &p }; /*gsl_odeiv2_step_rkf45 */
	gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(&sys,
			gsl_odeiv2_step_rk8pd, 1e-16, 1e-16, 0.0);

	int i;
	double t = 0.0;

	double y[S + 4]; /* tablica zmiennych zaleznych (stezenia, momenty) */

	long double stand_M_0;
	long double stand_M_1;
	long double stand_M_2;

	long double aux_stand_M_0;
	long double aux_stand_M_1;
	long double aux_stand_M_2;

	/* S + 4 variables: y[0] = c_p, y[i] = \xi_i, i = 1, ......., S-1, y[S] = M_0, y[S+1] = M_0, y[S+2] = M_2, y[S+3] = c_a */

	/************* zerowanie reczne elem.  ************************/

	for (i = 0; i < S + 4; i++) {
		y[i] = 0.0;

		/*	printf("# y[%d] = % .16Lf", i, y[i]); */
	}

	/*******inicjalizacja tablicy zmiennych ******************/

	y[0] = p.c4; /* c_p(0) = c_0 */
	y[S + 3] = p.c10; /* c_a(0) = d_0 */

	fprintf(fp,
			"# Values of the model parameters: \n#\n# Coagulation kernel parameters: kappa_0 = %.1f, kappa_1 = %.1f, kappa_2 = %.1f \n#\n# Precursors: c_0 = %.10f, d_0 = %.10f, Reductor: c_H = %.10f \n#\n# (exponential notation: c_0 = %.6e, d_0 = %.6e, c_H = %.6e)\n#\n# Reaction rate constants (bare!!): k_1 = %.10f, k_3 = %.10f, k_4 = %.10f, b_R = rho_0 = %.10f \n#\n# (exponential notation: k_1 = %.7e, k_3 = %.7e, k_4 = %.7e) \n#\n",
			p.c1, p.c2, p.c3, p.c4, p.c10, p.c5, p.c4, p.c10, p.c5, p.c6, p.c7,
			p.c8, p.c9, p.c6, p.c7, p.c8);

	fprintf(fp,
			"# A_R = %.1f, B_R = %.1f \n#\n# N = %d, t1 = %.0f s \n#\n# co_ile_krokow_wypisuje = %d  \n#\n# s = %d\n#\n#\n#\n#\n#\n#\n",
			STALA_DO_A_R, STALA_DO_B_R, N, t1, co_ile_krokow_wypisuje, S);

	fprintf(fp,
			"# State parameters: \n# \n#  t            c_p                c_a                 M_0                 M_1                  M_2               M_0^{S}	       M_1^{S}	          M_2^{S}        c_p + c_a + M_1      M_1/M_0     std_dev(size)           xi_1,             xi_2....\n#\n");

	for (i = 1; i <= N; i++) /*N = 100 in the orginal example */
	{

		double ti = i * t1 / N;
		int status = gsl_odeiv2_driver_apply(d, &t, ti, y);

		if (status != GSL_SUCCESS) {
			fprintf(fp,"error, return value=%d\n", status);
			break;
		}

		stand_M_0 = 0.0;
		stand_M_1 = 0.0;
		stand_M_2 = 0.0;

		aux_stand_M_0 = 0.0;
		aux_stand_M_1 = 0.0;
		aux_stand_M_2 = 0.0;

		for (k = 1; k < S; k++) {
			aux_stand_M_0 = aux_stand_M_0 + y[k];
			aux_stand_M_1 = aux_stand_M_1 + (long double) k * y[k];
			aux_stand_M_2 = aux_stand_M_2 + pow((long double) k, 2.0) * y[k];

		}
		stand_M_0 = aux_stand_M_0 + y[S];
		stand_M_1 = aux_stand_M_1 + y[S + 1];
		stand_M_2 = aux_stand_M_2 + y[S + 2];

		if (i % co_ile_krokow_wypisuje == 0) {

			fprintf(fp,"% .4f % .16f % .16f ", t, y[0], y[S + 3]);
			fprintf(fp,"% .16Lf % .16Lf % .16Lf ", stand_M_0, stand_M_1, stand_M_2);
			fprintf(fp,"% .16f % .16f % .16f ", y[S], y[S + 1], y[S + 2]);
			fprintf(fp,"% .12Lf ", y[0] + y[S + 3] + stand_M_1);
			fprintf(fp,"% .12Lf ", mean(stand_M_0, stand_M_1, stand_M_2));
			fprintf(fp,"% .12Lf ", std_deviation(stand_M_0, stand_M_1, stand_M_2));
			/*
			 for(k = 1; k < S ; k++)
			 {
			 printf("% .16f ", y[k]);
			 }
			 */

			for (k = 1; k < S; k = 2 * k) /*printf(" x[%d]=% .16f ", k, y[k]);*/
			{
				fprintf(fp,"% .16f ", y[k]);
			}
			fprintf(fp," \n");

		} /* od 'if' od krokow czasowych */

		if (i == N) {
			fprintf(fp,"# \n");
			fprintf(fp,
					"# Teraz wypisuje asymptotyczne wartosci wszystkich stezen klastrow \n");
			for (k = 1; k < S; k++) /*printf(" x[%d]=% .16f ", k, y[k]);*/
			{
				fprintf(fp,"#7# %d  % .16f \n", k, y[k]);
			}
			fprintf(fp,"#M_0=#  % .16f \n", y[S]);
		}

	} /* for po 'i', glowna petla (kroki czasowe) */

	gsl_odeiv2_driver_free(d);

	/***************** the end of the GSL part **************************/

	fprintf(fp,
			"# State parameters: \n# \n#  t            c_p                c_a                 M_0                 M_1                  M_2               M_0^{S}	       M_1^{S}	          M_2^{S}        c_p + c_a + M_1      M_1/M_0     std_dev(size)           xi_1,             xi_2....\n#\n");

	// close file:
	fclose(fp);
	return 0;
} /*koniec funkcji main*/
Ejemplo n.º 19
0
void	delta_evolution(double *retime, double *delta_re, double *delta_im, int *delta_loopmax)
{
//calculate the initial array
double	*y;
int	i,s,nk;
int	para=0;
double	k;
y=(double *)malloc(sizeof(double)*10*chainlen);
i=0;
for(nk=-halfchain;nk<=halfchain;nk++)
	{
	k=2.0*M_PI*nk/chainlen;
	para=2;
	y[i]=delta_int(k, &para, initdelta);	//Re v_{k,up}
//	printf("%lf ",y[i]);
	i++;
	para=3;
	y[i]=delta_int(k, &para, initdelta);	//Im v_{k,up}
//	printf("%lf ",y[i]);
	i++;
	para=4;
	y[i]=delta_int(k, &para, initdelta);	//n_{k,up}
//	printf("%lf ",y[i]);
	i++;
	para=6;
	y[i]=delta_int(k, &para, initdelta);	//Re tau_{k}
//	printf("%lf ",y[i]);
	i++;
	para=7;
	y[i]=delta_int(k, &para, initdelta);	//Im tau_{k}
//	printf("%lf ",y[i]);
	i++;
	para=8;
	y[i]=delta_int(k, &para, initdelta);	//Re sigma_k
//	printf("%lf ",y[i]);
	i++;
	para=9;
	y[i]=delta_int(k, &para, initdelta);	//Im sigma_k
//	printf("%lf ",y[i]);
	i++;
	para=14;
	y[i]=delta_int(k, &para, initdelta);	//Re v_{k,down}
//	printf("%lf ",y[i]);
	i++;
	para=15;
	y[i]=delta_int(k, &para, initdelta);	//Im v_{k,down}
	i++;
	para=16;
	y[i]=delta_int(k, &para, initdelta);	//n_{k,down}
	i++;
	}
//The initial value at t<0
int	halfhalfchain = (halfchain >> 1);
double	delta=0.0;
double	deltaimag=0.0;
double	totalnum=0.0;
double	hmhc1=0.0;
double	hmhc2=0.0;
double	tripup=0.0;
double	tripdown=0.0;
for(nk=0;nk<=chainlen-1;nk++)
	totalnum += y[nk*10+2]+y[nk*10+9];
totalnum = totalnum/chainlen;
//h-h_{c1} and h-h_{c2}
hmhc1=magh-pow((potentialmu-2.0)*(potentialmu-2.0)+initdelta*initdelta,0.5);
hmhc2=magh-pow((potentialmu+2.0)*(potentialmu+2.0)+initdelta*initdelta,0.5);
//triplet pairing |nu_up| and |nu_down| at k=-\pi/2
tripup=pow(y[halfhalfchain*10+0]*y[halfhalfchain*10+0]+y[halfhalfchain*10+1]*y[halfhalfchain*10+1],0.5);
tripdown=pow(y[halfhalfchain*10+7]*y[halfhalfchain*10+7]+y[halfhalfchain*10+8]*y[halfhalfchain*10+8],0.5);
printf ("%lf %lf %lf %lf %lf %lf %lf %lf\n", -10.0, initdelta,0.0,totalnum,hmhc1,hmhc2,tripup,tripdown);
printf ("%lf %lf %lf %lf %lf %lf %lf %lf\n", -0.01, initdelta,0.0,totalnum,hmhc1,hmhc2,tripup,tripdown);

double	mu = 0;
double	tau=0.005;
gsl_odeiv2_system sys = {odefunc, NULL, 10*chainlen, &mu};
gsl_odeiv2_driver * odedriver = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, tau, 1e-7, 1e-7);
gsl_odeiv2_driver_set_hmax (odedriver, tau);
double	t = 0.0;
double	t1;
int	loopmax = (int)(endtime/tau);
delta=deltaimag=totalnum=hmhc1=hmhc2=tripup=tripdown=0.0;
//do the evolution
//for (i = 0; i <= loopmax-2; i++)
i=0;
int	printcount=0;
while(t<=endtime)
	{
//	s = gsl_odeiv2_driver_apply_fixed_step (odedriver, &t, tau, 1, y);
	t1=t+tau;
	s = gsl_odeiv2_driver_apply (odedriver, &t, t1, y);
	if (s != GSL_SUCCESS)
		{printf ("error: driver returned %d\n", s);	exit(1);}
	delta=deltaimag=totalnum=hmhc1=hmhc2=0.0;
	for(nk=0;nk<=chainlen-1;nk++)
		{delta += y[nk*10+3];	deltaimag += y[nk*10+4];	totalnum += y[nk*10+2]+y[nk*10+9];}
	if(quenchtime<=0)
		delta = U_f*delta/chainlen;
	else
		delta = (t<=quenchtime) ? ((U_i+t*(U_f-U_i)/quenchtime)*delta/chainlen) : U_f*delta/chainlen;
	if(quenchtime<=0)
		deltaimag = -U_f*deltaimag/chainlen;
	else
		deltaimag = (t<=quenchtime) ? (-(U_i+t*(U_f-U_i)/quenchtime)*deltaimag/chainlen) : (-U_f*deltaimag/chainlen);
	totalnum = totalnum/chainlen;
	//h-h_{c1} and h-h_{c2}
	hmhc1=magh-pow((potentialmu-2.0)*(potentialmu-2.0)+delta*delta+deltaimag*deltaimag,0.5);
	hmhc2=magh-pow((potentialmu+2.0)*(potentialmu+2.0)+delta*delta+deltaimag*deltaimag,0.5);
	//triplet pairing |nu_up| and |nu_down| at k=-pi/2
	tripup=pow(y[halfhalfchain*10+0]*y[halfhalfchain*10+0]+y[halfhalfchain*10+1]*y[halfhalfchain*10+1],0.5);
	tripdown=pow(y[halfhalfchain*10+7]*y[halfhalfchain*10+7]+y[halfhalfchain*10+8]*y[halfhalfchain*10+8],0.5);
	printf ("%lf %lf %lf %lf %lf %lf %lf %lf\n", t, delta, deltaimag, totalnum,hmhc1,hmhc2,tripup,tripdown);
	if(printcount==0 && t > (endtime-10.0) )
		{
		for(nk=0;nk<=chainlen-1;nk++)
			printf("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",t,2.0*M_PI*(nk-halfchain)/chainlen,y[nk*10+0],y[nk*10+1],y[nk*10+2],y[nk*10+3],y[nk*10+4],y[nk*10+7],y[nk*10+8],y[nk*10+9]);
		printcount=1;
		}
	retime[i+1]=t;
	delta_re[i+1]=delta;
	delta_im[i+1]=deltaimag;
	i++;
	fflush(stdout);
	}
//(*delta_loopmax)=loopmax-1;
(*delta_loopmax)=i;
gsl_odeiv2_driver_free (odedriver);

/*
const gsl_odeiv2_step_type * odetype = gsl_odeiv2_step_rk8pd;
gsl_odeiv2_step * odestep = gsl_odeiv2_step_alloc (odetype, 10*chainlen);
gsl_odeiv2_control * odecontrol = gsl_odeiv2_control_y_new (1e-9, 0.0);
gsl_odeiv2_evolve * odeevolve = gsl_odeiv2_evolve_alloc (10*chainlen);
double	mu = 0.0;
gsl_odeiv2_system sys = {odefunc, NULL, 10*chainlen, &mu};
double	t = 0.0;
double	t1 = endtime;
double 	h = 1e-3;
delta=deltaimag=totalnum=hmhc1=hmhc2=tripup=tripdown=0.0;
while (t < t1)
	{
	int status = gsl_odeiv2_evolve_apply (odeevolve, odecontrol, odestep, &sys, &t, t1, &h, y);
	if (status != GSL_SUCCESS)
		break;
	delta=deltaimag=totalnum=hmhc1=hmhc2=0.0;
	for(nk=0;nk<=chainlen-1;nk++)
		{delta += y[nk*10+3];	deltaimag += y[nk*10+4];	totalnum += y[nk*10+2]+y[nk*10+9];}
	delta = U_f*delta/chainlen;
	deltaimag = -U_f*deltaimag/chainlen;
	totalnum = totalnum/chainlen;
	//h-h_{c1} and h-h_{c2}
	hmhc1=magh-pow((potentialmu-2.0)*(potentialmu-2.0)+delta*delta+deltaimag*deltaimag,0.5);
	hmhc2=magh-pow((potentialmu+2.0)*(potentialmu+2.0)+delta*delta+deltaimag*deltaimag,0.5);
	//triplet pairing |nu_up| and |nu_down| at k=-pi/2
	tripup=pow(y[halfhalfchain*10+0]*y[halfhalfchain*10+0]+y[halfhalfchain*10+1]*y[halfhalfchain*10+1],0.5);
	tripdown=pow(y[halfhalfchain*10+7]*y[halfhalfchain*10+7]+y[halfhalfchain*10+8]*y[halfhalfchain*10+8],0.5);
	printf ("%la %la %la %la %la %la %la %la\n", t, delta, deltaimag, totalnum,hmhc1,hmhc2,tripup,tripdown);
	}

gsl_odeiv2_evolve_free (odeevolve);
gsl_odeiv2_control_free (odecontrol);
gsl_odeiv2_step_free (odestep);*/

free(y);
}
Ejemplo n.º 20
0
void	evolution(double* y,double t)	{	
	FILE* 	fp;
  FILE*   bfp; //binary file to store y;
  int			info;
	long    count;
  double	Da_re,Da_im,Db_re,Db_im,Da,Db,Na,Nb;
  double	Mu = 0;	//void par,will not use
  double	tau=0.005;
  double	t1;

	const	char* readmefile="../data1/s12_readme.txt";
	const	char* datafile="../data1/s12.txt";
	const	char* yfile="../data1/s12_y.bin";
//	struct timespec time1={0,0};
//	struct timespec time2={0,0};
//	double timepass;
	
	fp=fopen(readmefile, "w");
	fprintf(fp,"eps=%e\nA=%lf\nomega=%lf\nU=%lf\nmu=%lf\ndelta=%f\nendtime=%f\nN=%d",\
							eps,A,omega,U,mu,delta,endtime,N);
	fclose(fp);
	

  cal_phys(y,&Da_re, &Da_im, &Db_re, &Db_im, &Da, &Db, &Na, &Nb); 

	fp = fopen(datafile, "w");
  printf ("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", -10.0, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);
  printf ("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", -0.01, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);
  fprintf (fp,"%s %s %s %s %s %s %s %s %s %s\n", "t", "ReDa","ImDa","ReDb","ImDb","Na","Nb","Na+Nb","|Da|","|Db|");
  fprintf (fp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", -10.0, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);
  fprintf (fp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", -0.01, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);

  //Initialize the evoling process
  gsl_odeiv2_system sys = {odefun, NULL, 16*Ns, &Mu};
  gsl_odeiv2_driver * odedriver = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, tau, eps, eps);
  gsl_odeiv2_driver_set_hmax (odedriver, tau);

  //do the evolution
  while(t<endtime)	{
	//	clock_gettime(CLOCK_REALTIME,&time1);
    count=count+1;
  	t1=t+tau;
  	info = gsl_odeiv2_driver_apply (odedriver, &t, t1, y);

  	if (info != GSL_SUCCESS)	{
  		printf ("error: driver returned %d\n", info);
  		exit(1);
  	}
    //record y,t and U every 200*tau time.
		if (count%200==0) {
      bfp=fopen(yfile,"wb");
      fwrite(&U,sizeof(double),1,bfp);
      fwrite(&t,sizeof(double),1,bfp);
      fwrite(y,sizeof(double),Ns*16,bfp);
      close(bfp);
		}

    cal_phys(y,&Da_re, &Da_im, &Db_re, &Db_im, &Da, &Db, &Na, &Nb); 

	//	clock_gettime(CLOCK_REALTIME,&time2);
	//	timepass=(double)(time2.tv_sec-time1.tv_sec)*1000+(double)(time2.tv_nsec-time1.tv_nsec)/1000000;
	//	printf("time passed is %f ms\n",timepass);

  	printf ("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", t, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);
  	fprintf (fp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", t, Da_re,Da_im,Db_re,Db_im,Na,Nb,Na+Nb,Da,Db);
		fflush(fp);
  }

	fclose(fp);
  gsl_odeiv2_driver_free (odedriver);
}
Ejemplo n.º 21
0
double * run_integration(double y[],double target_a[],double q,double ecc, int ntp,double eps,double * t0, double  tfin, int close_encounters_flag, int * active)
{
	if(close_encounters_flag <=0 ){
		tpxv=fopen("tp.xv","w");  star1=fopen("star1.xv","w");star2=fopen("star2.xv","w");
	}else{
		tpxv=fopen("tp.xv","a");  star1=fopen("star1.xv","a");star2=fopen("star2.xv","a");
		relv=fopen("relv.dat","w");
	}
	if ((tpxv==NULL) || (star1==NULL) ||(star2==NULL)) {
		printf("File handling error in run_integration!\n");
		exit(-1);
	}
	double del_theta = TWOPI/N_BINS; // Binning variables
#if 0
	double rms_bin_variance = 0;
	int theta_bin_count[N_BINS] = { 0 };
	double theta_bin_var[N_BINS] = { 0 } , theta_bin_avg[N_BINS] = { 0 }, theta_bin_er2[N_BINS] = { 0 };
#endif
	double star1_xv[4], star2_xv[4];
	if(close_encounters_flag<=0)
		set_target_a(target_a,y,ntp); //Only set for damping run, otherwise leave the same
	parameters params = {q,ecc,star1_xv, star2_xv,target_a,active}; // parameter to pass to dervis routine
	int tint = 0, lfirst=1; // Time/loop control variales
	double t,tnext,e0;
	double tfinal = tfin * period;
	double dt = eps * period;
	int tintfinal =  1 + (int) ceil((tfinal-*t0) / dt);
	gsl_odeiv2_system sys = {derivs, 0, NVAR, &params}; // GSL integrator variables 
	double hh = 1.0e-8;
	gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(&sys,gsl_odeiv2_step_rk8pd, hh,1.0e-9,0.0);
	double start_t=*t0;	
	for (tint = 0; tint < tintfinal; tint++)
	{
		t = tint * dt + start_t;
		tnext = (tint + 1) * dt + start_t;
		printf("Time: %g\n", t/period);
		int id;
		//if(tint%10==0 && t > 1.99e4 * period){
		for (id=0; id<ntp; id++) {
			if(active[id] > 0)
				check_eject(id,y,active);
				write_tpxv(tpxv,t, id,y,q,star1_xv,star2_xv);
		}//}
		if(close_encounters_flag > 0)
			rel_vel(y,ntp,active,relv);
		write_starxvdat(t,star1,star2,star1_xv,star2_xv);
		int status = gsl_odeiv2_driver_apply (d, &t,tnext , y);
		if (status != GSL_SUCCESS) exit(-1);
	}
	int tpid;
	for (tpid=0; tpid<ntp; tpid++){
		if(active[tpid] <= 0)
			printf("Particle %d was ejected\n",tpid );
	}
	fclose(star1);
	fclose(star2);
	fclose(tpxv);
	if(close_encounters_flag>0)
		fclose(relv);
	gsl_odeiv2_driver_free(d);
//	rms_bin_variance = rms_sum(theta_bin_var);
	*t0 = tnext;
	return y;
}
Ejemplo n.º 22
0
double  omega = 0.1;	/* Parameter fuer f */
gsl_odeiv2_system       system = { f, NULL, 2, &omega }; /* DG-System */
gsl_odeiv2_driver       *driver
        = gsl_odeiv2_driver_alloc_y_new(&system, gsl_odeiv2_step_rk8pd,
                1e-6, 1e-6, 0.0);
Ejemplo n.º 23
0
double *integration_eam_p(double *y, void *params, unsigned int S, int *stab, Params *p)
{

    char *buffer = (char*)malloc(100);
    sprintf(buffer, "%s/cycle%d.txt",p->folder,p->cycle_num);
    FILE *outname = fopen(buffer,"w");
    fprintf(outname,"\n%d\t",0);
    print_double_array(y,S,outname);
  

    // DEFINE AND INITIALIZE THE VARIABLES
    //------------------------------------
    int theend = 0;
    int theend2 = 0;
    double *biomass = (double*)malloc(S * sizeof(double));   // array for the temporal mean biomasses calculation
    double *minis = (double*)malloc(S * sizeof(double));     // array for the minimum densities
    double *maxis = (double*)malloc(S * sizeof(double));     // array for the maximum densities
    double *check1 = (double*)malloc(S * sizeof(double));    // array for the minimum densities
    double *check2 = (double*)malloc(S * sizeof(double));    // array for the maximum densities

    for(int i = 0 ; i < S ; ++i)
    {
	biomass[i] = 0.0;
    }
    memcpy(minis,biomass,S * sizeof(double));
    memcpy(maxis,biomass,S * sizeof(double));
    memcpy(check1,biomass,S * sizeof(double));
    memcpy(check2,biomass,S * sizeof(double));


    // PRINT THE INITIAL DENSITIES
    //----------------------------
    //display_densities(y,S,0);    

    // CALCULATION OF INTERACTIONS AND PREFERENCES
    //--------------------------------------------
    interaction((isll*)params,p);

    // DEFINE THE INTEGRATION SYSTEM WITH THE FUNCTION FOO_EAM
    //--------------------------------------------------------
    gsl_odeiv2_system sys = {foo_eam, NULL,S, params};
    //gsl_odeiv2_system sys = {foo_eam, jac_eam,S, params};

        // DEFINE THE SOLVER
    //------------------
    //gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk8pd, 1e-15, 1e-15,1e-20);
    gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkck, 1e-12, 1e-12,0);         // the faster
    //gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkf45, 1e-15, 1e-15,1e-20);    

    // STARTING TIME
    //--------------
    double t = 0.0;
    int a = EAM_TMAX - EAM_B;
    int b = EAM_B * 3;


    //-------------//
    // INTEGRATION //
    //------------ //

    // MODE WITH CHECKING TIME
    //------------------------
    if(EAM_EQ_MOD)  
    {
        // RUN UNTIL CHECKING TIME
        //------------------------
      runi_p(d,&t,y,1,(EAM_TE_START) ,S,outname);

        int run_cycles = (int)((a - EAM_TE_START) / b);                                                       // number of checking times
	int left_time = EAM_TMAX - (run_cycles * b);                                                          // time left to reach EAM_TMAX if not stopped before
	int index = EAM_TE_START + 1;
	for(int i = 0 ; i < run_cycles ; ++i)
	{
	    theend = index + EAM_B - 1;
	    runb_p(d,&t,y,check1,index, theend,S,outname);
	    theend2 = theend  + EAM_B;
	    runb_p(d,&t,y,check2,(theend + 1), theend2,S,outname);                                      

	    // IF STABILITY IS REACHED, STOP THE INTEGRATION 
	    //----------------------------------------------
	    if(compare_dd(check1,check2,S) == 1)
	    {
		p->time_eq = index + b - 1;
	        runb_plus_stab_p(d,&t,y,biomass,minis,maxis,(theend2 + 1),p->time_eq,S,stab,outname);

                // FREE THE MEMORY
                //----------------
		free(minis);
		free(maxis);
		free(check1);
		free(check2);
		gsl_odeiv2_driver_free(d);
		fclose(outname);
		free(buffer);
		return(biomass);
	    }
	    else
	      {
		runi_p(d,&t,y,(theend2 + 1),(index + b - 1),S,outname);
	      }
	    index += b;
	}

	// IF STABILITY IS NOT REACHED, FINISH THE INTEGRATION AND CALCULATE THE TEMPORAL BIOMASS
	//---------------------------------------------------------------------------------------
        if(left_time != 0)runi_p(d,&t,y,index,(a-1) ,S,outname);
	runb_p(d,&t,y,biomass,a,EAM_TMAX,S,outname);
	*stab = 0;
	p->time_eq =  EAM_TMAX;     
    }
    else  

    // MODE WITHOUT CHECKING TIME
    //---------------------------           
    {
        int index = EAM_TMAX - b;
        runi_p(d,&t,y,1,index,S,outname);
	theend = index + EAM_B;
	runb_p(d,&t,y,check1,(index + 1), theend,S,outname);
	theend2 = theend  + EAM_B;
	runb_p(d,&t,y,check2,(theend + 1), theend2,S,outname); 
	if(compare_dd(check1,check2,S) == 1)
	{
	    runb_plus_stab_p(d,&t,y,biomass,minis,maxis,(theend2 + 1),EAM_TMAX,S,stab,outname);
	}
	else
	{
	    runb_p(d,&t,y,biomass,(theend2 + 1),EAM_TMAX,S,outname);
	    *stab = 0;
	}
	p->time_eq =  EAM_TMAX;
    }
        // FREE THE MEMORY
        //----------------
        free(minis);
        free(maxis);
        free(check1);
        free(check2);
        gsl_odeiv2_driver_free(d);
	fclose(outname);
	free(buffer);
        return (biomass);

}
Ejemplo n.º 24
0
int ode(int method, double h, double eps_abs, double eps_rel,
        int f(double, int, const double*, int, double*),
        int jac(double, int, const double*, int, int, double*),
        KRVEC(xi), KRVEC(ts), RMAT(sol)) {

    const gsl_odeiv2_step_type * T;

    switch(method) {
        case 0 : {T = gsl_odeiv2_step_rk2; break; }
        case 1 : {T = gsl_odeiv2_step_rk4; break; }
        case 2 : {T = gsl_odeiv2_step_rkf45; break; }
        case 3 : {T = gsl_odeiv2_step_rkck; break; }
        case 4 : {T = gsl_odeiv2_step_rk8pd; break; }
        case 5 : {T = gsl_odeiv2_step_rk2imp; break; }
        case 6 : {T = gsl_odeiv2_step_rk4imp; break; }
        case 7 : {T = gsl_odeiv2_step_bsimp; break; }
        case 8 : {T = gsl_odeiv2_step_rk1imp; break; }
        case 9 : {T = gsl_odeiv2_step_msadams; break; }
        case 10: {T = gsl_odeiv2_step_msbdf; break; }
        default: ERROR(BAD_CODE);
    }

    Tode P;
    P.f = f;
    P.j = jac;
    P.n = xin;

    gsl_odeiv2_system sys = {odefunc, odejac, xin, &P};

    gsl_odeiv2_driver * d =
         gsl_odeiv2_driver_alloc_y_new (&sys, T, h, eps_abs, eps_rel);

    double t = tsp[0];

    double* y = (double*)calloc(xin,sizeof(double));
    int i,j;
    int status;
    for(i=0; i< xin; i++) {
        y[i] = xip[i];
        solp[i] = xip[i];
    }

       for (i = 1; i < tsn ; i++)
         {
           double ti = tsp[i];
           
           status = gsl_odeiv2_driver_apply (d, &t, ti, y);
     
           if (status != GSL_SUCCESS) {
         	  printf ("error in ode, return value=%d\n", status);
         	  break;
        	}

//           printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
           
           for(j=0; j<xin; j++) {
               solp[i*xin + j] = y[j];
           }
         }

    free(y);
    gsl_odeiv2_driver_free (d);
    
    return status;
}
Ejemplo n.º 25
0
void muk::muk_calc(){

	gsl_odeiv2_system muksys = {modfprime, nullptr, 4, mparams};
        gsl_odeiv2_driver* mukd = gsl_odeiv2_driver_alloc_y_new(&muksys, gsl_odeiv2_step_rkf45, 0.01, 0., 1.e-9); // perhaps make the tolerance adjustable later but OK for now

	double kmpc;
	double kc;
	double lna0muk;
	double lna1muk;

	double step_muk;
	double H_i;
	double a_i;
	double lna_i;
	int status;
	double z;
	double conv;

	double modf[4];

	std::vector<double> u1; 
	std::vector<double> u2; 
	std::vector<double> convtest;

	int indexlna0 = 0;
	int indexlna1 = 0;
	double etaminval;
	double etamaxval;

	int asize = mparams->a->size();

	//This is for chekcing only
	//FILE * u1modf;
	//FILE * u2modf;

	//end of checking
	int lenks = mparams->ks->size();

        for(int kin=0; kin <300; kin++){
                mparams->set_kindex(kin);
		// Make these depend on k but OK for now
		kmpc = (*(mparams->ks))[mparams->kindex];
		kc = kmpc;// * mpcconv; NO MPCCONV!!
                // lna0muk = log(kmpc*1.e20);
                // lna1muk = log(kmpc*1.e26);
                etaminval = 0.0001/kc;
		etamaxval = 1000./kc;
		for(int i=indexlna0; i!=asize; ++i){
			if( (*(mparams->eta))[i] < etamaxval ){ // use inheritance here, no need to derference
				indexlna0 = i;
				break;
			}
		}

		for(int i=indexlna1; i!=asize; ++i){
			if( (*(mparams->eta))[i] < etaminval ){
				indexlna1 = i;
				break;
			}
		}
		//Here I did not reset the values of indexlna becuse kc is always increasing but be careful otherwise; OK for now


		lna0muk = log((*(mparams->a))[indexlna0]);
		lna1muk = log((*(mparams->a))[indexlna1]);

		step_muk = (lna1muk-lna0muk)/nsteps; // change this since we changed the a steps near the feature;  OK for now
                H_i = mparams->interp_H(exp(lna0muk));
                a_i = exp(lna0muk); 

                modf[0] = u1init; modf[1] = u2init; modf[2] = u1pinit; modf[3] = u2pinit;

                u1.push_back(u1init);
                u2.push_back(u2init);


	/*	
		double factor;
		if( (mparams->sparams->d) >0.005){
			factor = 3.; 
		} else if ( (mparams->sparams->d) > 0.0005){
			factor = 4.; 
		} else {
			factor = 8.; 
		} // this is a bad solution but OK for now
	*/	
		//checking
		//if((kin > 130) && (kin < 140)){
		//	u1modf = fopen( ("modf/modf1_k" + std::to_string(kc) + ".dat").c_str() , "w");
		//	u2modf = fopen( ("modf/modf2_k" + std::to_string(kc) + ".dat").c_str() , "w");
		//}//end of checking


                for(int i=1; i<=nsteps; ++i){
			lna_i = lna0muk + step_muk;
		
	/* 
 			//Variable step
			if ( ( (mparams->interp_phi(exp(lna_i))) > (14.668 + (factor*(mparams->sparams->d))) ) || ( (mparams->interp_phi(exp(lna_i))) < (14.668 - (5.*(mparams->sparams->d)))) ) {
                        	lna_i = lna0muk + step_muk;
			} else {
				lna_i = lna0muk + (step_muk/30.);
			}
	*/
			
                        status = gsl_odeiv2_driver_apply(mukd, &lna0muk, lna_i, modf);
			

                        u1.push_back(modf[0]);
                        u2.push_back(modf[1]);

                        z = exp(lna0muk) * mparams->interp_phip(exp(lna0muk));
                        //double kc = (*(mparams->ks))[mparams->kindex] * mpcconv;
                        conv = ( (u1.back()*u1.back()/(2.*kc)) + (kc*u2.back()*u2.back()/(2.*a_i*a_i*H_i*H_i)) )/(z*z);

			//checking only
			//if((kin > 130) && (kin < 140)){
			//	fprintf(u1modf, "%.9e  %.9e  %.9e\n", lna0muk, modf[0], z);
			//	fprintf(u2modf, "%.9e  %.9e  %.9e\n", lna0muk, modf[1], z);
			//}
			//end of checking


                        convtest.push_back(conv);
                        //if((std::abs(convtest.back() - convtest.end()[-2]) < (0.00000001 * convtest.back()))){
                        //        break;
			//}
		} // end of modf integration loop

		//checking 
		//if((kin > 130) && (kin < 140)){
		//	std::cout << "kval = " << kc << std::endl;
		//	fclose(u1modf);
		//	fclose(u2modf);
		//}
		//end of checking


                //double kc = (*(mparams->ks))[mparams->kindex] * mpcconv;
                Delta2.push_back( (kc*kc*kc/(2*M_PI*M_PI))*convtest.back() );
                //std::cout << ((*(mparams->ks))[mparams->kindex]) << "  " << Delta2.back() << std::endl;

                std::vector<double>().swap(u1);
                std::vector<double>().swap(u2);
                std::vector<double>().swap(convtest);


	} // end of kindex loop


} // end of muk_calc
gsl_vector* EvolveNetwork(struct foodweb nicheweb, struct migration stochastic, gsl_rng* rng1, const gsl_rng_type* rng1_T, gsl_matrix* Dchoice, gsl_vector* result)
{	
	struct foodweb *params = &nicheweb; 									// Damit Holling2 auf das foodweb zugreifen kann

	int S 	 	= nicheweb.S;
	int Y 	     	= nicheweb.Y;
	int Rnum 	= nicheweb.Rnum;
	int Z 		= nicheweb.Z;
	double Bmigr 	= stochastic.Bmigr;
	printf("Bmigr ist %f\n", Bmigr);
	//int Tchoice 	= nicheweb.Tchoice;
	//double tcheck	= 7805; 
	double aussterbeSchwelle; 
	
	if(stochastic.Bmigr < 1e-5)
	{
	  aussterbeSchwelle = stochastic.Bmigr;
	}
	else
	{
	  aussterbeSchwelle = 1e-5;
	}
	
	printf("aussterbeSchwelle ist %f\n",aussterbeSchwelle);
	
	double Rsize = gsl_vector_get(nicheweb.network, (Rnum+S)*(Rnum+S)+Y*Y+2);
		
	double *y 	 = (double *)calloc((Rnum+S)*Y, sizeof(double));				// Ergebnis Array für den Lösungsalgorithmus

	int i, j   	 = 0;	
	int closezero= 1;			

//-- Ergebnis Variablen-----------------------------------------------------------------------------------------------------------------------------------	
	gsl_vector *y0		= gsl_vector_calloc((Rnum+S)*Y);						// Startwerte der Populationsgrößen
	gsl_vector *ymax	= gsl_vector_calloc((Rnum+S)*Y);						// Maximalwerte nach t2
	gsl_vector *ymin	= gsl_vector_calloc((Rnum+S)*Y);						// Minimalwerte nach t2
	gsl_vector *yavg	= gsl_vector_calloc((Rnum+S)*Y);						// Durchschnittswert nach t2

//--Zufallszahlengenerator für Populationsgrößen----------------------------------------------------------------------------------------------------------

// 	const gsl_rng_type *rng1_T;							// Für zufällige Populationsgröße der Spezies
// 	gsl_rng *rng1;   									
// 	gsl_rng_env_setup();   								
// 	rng1_T = gsl_rng_default;   						// default random number generator (so called mt19937)
// 	gsl_rng_default_seed = 0;							// default seed for rng
// 	//gsl_rng_default_seed=((unsigned)time(NULL));		// random starting seed for rng
// 	rng1 = gsl_rng_alloc(rng1_T);	

//--Erstelle y[] mit Startwerten für die Speziespopulationsgrößen---------------------------------------------------------------------------------------

	  for(j=0; j<Y; j++)								// set initial species size "RANDOM" in each patch
	  {

		for(i=0; i<Rnum; i++)
		 {
			y[j*(Rnum+S)+i] = Rsize;							// Ressourcen Größe pro Patch		
		 }

		for(i=Rnum; i<Rnum+S; i++)
		{
		  if(closezero == 1) y[j*(Rnum+S)+i] = 0.0000001 + (gsl_rng_uniform_pos(rng1)*0.1);
		  			    else y[j*(Rnum+S)+i] = 0.001     + (gsl_rng_uniform_pos(rng1)*0.1);

			//printf("y0 = %f\n", y[j*(Rnum+S)+i]);
		}
	  }

	 // printf("Eintrag 5 von y ist am Anfang %f\n",y[5]);
	printf("Spezies Anfangspopulationen erzeugt\n");

  	gsl_vector_view y_vec = gsl_vector_view_array(y, (Rnum+S)*Y);		
   					       	gsl_vector_memcpy(y0, &y_vec.vector);						//y0 enthält jetzt die so eben bestimmten Startwerte

//-------------------------------------------------------------------------------------------------------------------------------------------------------------
/*ODE: Ordinary Differential Equation  mittlerweile gibt es die odeiv2 systeme -> sollte man vielleicht upgraden
	Hilfe für die alte Version: http://www.inference.phy.cam.ac.uk/pjc51/local/gsl/manual/gsl-ref_25.html	
	Neue Version: https://www.gnu.org/software/gsl/manual/html_node/Ordinary-Differential-Equations.html#Ordinary-Differential-Equations
*/  
	const gsl_odeiv2_step_type *Solv		= gsl_odeiv2_step_rkf45;							// ODE Solver vom Typ RungeKutta 4/5 (siehe Dokumentation)
		  gsl_odeiv2_step *s		= gsl_odeiv2_step_alloc(Solv,(Rnum+S)*Y);			// Schrittfunktion
		  gsl_odeiv2_control *c		= gsl_odeiv2_control_y_new(1e-6, 1e-8);			// Kontrollfunktion zur Anpassung der Schrittgröße, um Genuigkeit zu gewährleisten
		  
		  gsl_odeiv2_evolve *e			= gsl_odeiv2_evolve_alloc((Rnum+S)*Y);			// 
		  gsl_odeiv2_system sys			= {Holling2, NULL, (size_t)(Rnum+S)*Y, params};			// ODE System struct -> dieses wird evolviert
		  gsl_odeiv2_driver *d 	= gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rkf45, 1e-5, 1e-6, 1e-8);
//   		  gsl_odeiv2_driver_set_hmax(d, 0.01);
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------- 
gsl_odeiv_system ist ein Datentyp, der ein allgemeines ODE system mit frei wählbaren Parametern enthält. 
Er wird definiert über vier Größen 

(1) eine int funktion f(double t, const double y[], double dydt[], void * params) 
    Sie sollte die Vektorelemente der zu lösenden Funktion in dydt[] speichern für die Argumente (t,y) und die Parameter params enthalten

	-> hier Hol_dynam(double t, const double y[], double ydot[], void *params)

(2) eine funktion "int (* jacobian) (double t, const double y[], double * dfdy, double dfdt[], void * params)", die die Jacobi-Matrix enthält. 
    Sie wird von weiter entwickelten Solvern benutzt. Für einfachere Solver muss sie nicht angegeben werden.
	-> hier weggelassen mit NULL

(3) eine Größe size_t dimension, die die Dimension des Gleichungssystems angibt
	-> hier (Rnum+S)*Y, da auf Y Patches jeweils S+Rnum Spezies leben

(4) void * params, einen Pointer auf die Parameter des Systems, diese werden hier über den *params übergeben
    FRAGE: Muss network erst in ein Array geschrieben werden?
*/

//--DGL lösen mit Holling II---------------------------------------------------------------------------------------------------------------------------------------------------
  double t		= 0.0; 				// start time
  double tend1 	= 7800; 
  double tend2	= 8000;				// endtime
  double h		= 1e-3;				// stepwidth

  double countsteps = 0;			// Schritte
  //double mu=0, nu=0, tau = 0;
  double tlast = -0.1;
  //int SpeciesNumber;
  unsigned long migrationEventNumber = 0;
  gsl_vector_set(nicheweb.migrPara,5, 0);
  double taulast = 0;
  //printf("Z ist %i\n",Z);
//  int docheck 		= 0;			
  int k=0;
//--Erster Abschnitt bis t1--------------------------------------------------------------------------------------------------------------  
	
	printf("\nStarte Lösen der Populationsdynamik\n\n");	
	
	
  if(Y>1)
  {
    stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
    //gsl_vector_set(nicheweb.migrPara, 0 , gsl_vector_get(nicheweb.migrPara, 0));
    printf("migrationereignis zum Zeipunkt %f\n",gsl_vector_get(nicheweb.migrPara, 0));
  }
  else
  {
    printf("Es gibt nur ein Patch, sodass keine Migration stattfinden kann\n");
  }	
  double hmean;
  double ytest;
  double ti;
//   int k;
  for(k = 1; k<78000;k++)					
  { 
    
    ti = k * tend1 / 78000.0;
    gsl_vector_set(nicheweb.migrPara, 4,tlast);	
	//for(i=0; i<Rnum+S; i++)// Startgrößen
    printf("t ist oben %f\n",t);
    printf("ti ist oben %f\n",ti);
    printf("k ist oben %i\n",k);
	//printf("mu ist %f\n", gsl_vector_get(nicheweb.migrPara, 1));
      //printf("nu ist %f\n", gsl_vector_get(nicheweb.migrPara, 2));	
    gsl_odeiv2_driver_set_hmax(d, 0.2);
    int status = gsl_odeiv2_driver_apply(d, &t, ti, y);
    printf("status ist %i\n",status);

    if(status != GSL_SUCCESS) {
		printf("Fehler beim Lösen der DGL!\n");		
		break;
	   }	  /*status = Ergebnis für einen Zeitschritt der DGL Lösung. 
	  HollingII wird mit t, y, params, aufgerufen, die Ergebnisse legt es dann in ydot ab. 
	  Die Werte in ydot werden dann als neue Werte für y verwendet.*/
	   
	   
//        printf("t ist %f\n",t);
//        printf("ti ist %f\n",ti);
//        printf("h ist %f\n",h);

    hmean += h;
    if(h>1)
    {
      printf("h ist %f\n",h);
    }

    
//     ytest= y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)];	
	//	if(status == GSL_SUCCESS)	printf("Status OK\n");
    
    while( (t > gsl_vector_get(nicheweb.migrPara, 0)) && (tlast < gsl_vector_get(nicheweb.migrPara, 0)) )
    {
//       printf("tlast ist %f und t ist %f\n", tlast,t);
      y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)] = y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]+Bmigr; 
      y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 1)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)] = y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 1)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]-Bmigr; 
      gsl_vector_set(nicheweb.migrPara,5, gsl_vector_get(nicheweb.migrPara,5)+Bmigr);
      
       for(i=0; i<(Rnum+S)*Y; i++)
       {
//  	printf("y[%i]= %f\n",i,y[i]);
		  if(y[i] < 1e-10) 
			 y[i] = 0;			// bei Populationsgrößen kleiner als 10^-5 gilt die Population als ausgestorben
      }
      
 	taulast = gsl_vector_get(nicheweb.migrPara, 0);
      //printf("y vorher ist %f\t und nachher %f\n",ytest, y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]);
	stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
      
	gsl_vector_set(nicheweb.migrPara, 0 , gsl_vector_get(nicheweb.migrPara, 0)+taulast);
// 	printf("tau+t ist %f\n", gsl_vector_get(nicheweb.migrPara, 0));
      //printf("ydotmigr ist %f\n", gsl_vector_get(nicheweb.migrPara, 5));
// 	printf("t+tau ist %f\n", gsl_vector_get(nicheweb.migrPara, 0));
      //printf("nu ist %f\n", gsl_vector_get(nicheweb.migrPara, 2));
	migrationEventNumber++;
// 	printf("migrationEventNumber ist %i\n",migrationEventNumber);
      }


    

    for(i=0; i<(Rnum+S)*Y; i++)
     {
		  if(y[i] < aussterbeSchwelle) 
			 y[i] = 0;			// bei Populationsgrößen kleiner als 10^-5 gilt die Population als ausgestorben
     }
     
     
    tlast = t;
    
    if(t > gsl_vector_get(nicheweb.migrPara, 0)&& migrationEventNumber < Z)
    {
 	taulast = gsl_vector_get(nicheweb.migrPara, 0);
//       printf("y vorher ist %f\t und nachher %f\n",ytest, y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]);
	stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
      
	gsl_vector_set(nicheweb.migrPara, 0 , gsl_vector_get(nicheweb.migrPara, 0)+taulast);
// 	printf("tau+t ist %f\n", gsl_vector_get(nicheweb.migrPara, 0));
//       printf("ydotmigr ist %f\n", gsl_vector_get(nicheweb.migrPara, 5));
//       printf("t+tau ist %f\n", gsl_vector_get(nicheweb.migrPara, 0));
//       printf("nu ist %f\n", gsl_vector_get(nicheweb.migrPara, 2));
	migrationEventNumber++;
//       printf("migrationEventNumber ist %i\n",migrationEventNumber);
//       if(migrationEventNumber!=0.00001*gsl_vector_get(nicheweb.migrPara,5))
//       {
// 	int l;
// 	for(l = 0; l<100;l++)
// 	{
// 	  printf("Hier\t");
// 	}
//       }
      }
    
  }

  for(i=0; i < (Rnum+S)*Y; i++)		// Referenzwerte in min und max schreiben = Wert von y nach t = 7800 
  {
    gsl_vector_set(ymax, i, y[i]);
    gsl_vector_set(ymin, i, y[i]);
  }
 
//-- Lösen von t1 bis t2-----------------------------------------------------------------------------------------------------------------------   

//  docheck = 1;			// triggert, dass in HollingII nach Fixpunkt Attraktoren gesucht wird???

// int testf0, testf1, testf2  	= 1;	

	//printf("t=%f\n", t);	

  //double migrationWerte[4];
  printf("Komme in zweite Schleife");
  for(k = 0; k<2000;k++)					
  { 
    ti = k * (tend1-tend2)/2000.0 + tend1;
    printf("t ist oben %f\n",t);
    printf("ti ist oben %f\n",ti);
    printf("k ist oben %i\n",k);
    gsl_vector_set(nicheweb.migrPara, 4,tlast);	
    //printf("SpeciesNumber %f\n", gsl_vector_get(nicheweb.migrPara,Z+3));
    //printf("t=%f\n", t);
    countsteps++;
    //printf("y=%f\n", y[1]);
    gsl_odeiv2_driver_set_hmax(d, 0.2);
    int status = gsl_odeiv2_driver_apply(d, &t, tend1, y);		// Hier werden fixp Variablen benutzt

//     printf("h ist %f\n",h);
    hmean += h;
//     k++;
    
    if(status != GSL_SUCCESS)
      break;
    
    while( (t > gsl_vector_get(nicheweb.migrPara, 0)) && (tlast < gsl_vector_get(nicheweb.migrPara, 0)))
    {
      y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)] = y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 2)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]+Bmigr; 
      y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 1)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)] = y[(Rnum+S)*(int)gsl_vector_get(nicheweb.migrPara, 1)+Rnum+(int)gsl_vector_get(nicheweb.migrPara, 3)]-Bmigr; 
      gsl_vector_set(nicheweb.migrPara,5, gsl_vector_get(nicheweb.migrPara,5)+Bmigr);
      
      for(i=0; i<(Rnum+S)*Y; i++)
      {
		  if(y[i] < aussterbeSchwelle) 
		  y[i] = 0;			// bei Populationsgrößen kleiner als 10^-5 gilt die Population als ausgestorben
      }
      taulast = gsl_vector_get(nicheweb.migrPara, 0);
      stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
      gsl_vector_set(nicheweb.migrPara, 0 , gsl_vector_get(nicheweb.migrPara, 0)+taulast);
//       printf("ydotmigr ist %f\n", gsl_vector_get(nicheweb.migrPara, 5));
      //printf("mu ist %f\n", gsl_vector_get(nicheweb.migrPara, 1));
      //printf("nu ist %f\n", gsl_vector_get(nicheweb.migrPara, 2));
      migrationEventNumber++;
      
      
    }
    
    
    for(i=0; i<(Rnum+S)*Y; i++)
      {
		  if(y[i]< aussterbeSchwelle)				// wieder Aussterbe-Kriterium
		   y[i]= 0;
				
      }
    //printf("test");
    tlast = t;
//     if(t > gsl_vector_get(nicheweb.migrPara, 0))
//     {
//       stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
//       gsl_vector_set(nicheweb.migrPara, 0 , gsl_vector_get(nicheweb.migrPara, 0)+t);
// //       printf("ydotmigr ist %f\n", gsl_vector_get(nicheweb.migrPara, 5));
//       //printf("mu ist %f\n", gsl_vector_get(nicheweb.migrPara, 1));
//       //printf("nu ist %f\n", gsl_vector_get(nicheweb.migrPara, 2));
//       migrationEventNumber++;
//     }
    
    //printf("Migrationsereignis #%i\n",migrationEventNumber);
    for(i=0; i<(Rnum+S)*Y; i++)
      {
		  if(y[i] > gsl_vector_get(ymax, i))				// Checken ob y größer geworden ist
			 		gsl_vector_set(ymax, i, y[i]); 	

		  if(y[i] < gsl_vector_get(ymin, i))				// Checken ob y kleiner geworden ist
			 		gsl_vector_set(ymin, i, y[i]); 	

		  gsl_vector_set(yavg, i, ((gsl_vector_get(yavg, i)*(countsteps-1)+y[i])/countsteps));
      }
      
      
//--Zum Testen, ob im Mittel das Gleiche wie bei deterministischen Migration rauskommt; sonst auskommentieren!!!---------------------------------------------      
//       if(t> tcheck )
//       {
// 	//printf("Im Test\n");
// 	int j,k;
// 	//printf("migrationEventNumber ist %i\n", migrationEventNumber);
// 	//printf("y ist %f\n",y[1]);
// 	for(k = 0; k < migrationEventNumber; k++)
// 	{
// 	    //printf("stochastic.AllMus ist %f\n",gsl_vector_get(stochastic.AllMus,k));
// 	    gsl_vector_set(stochastic.AllMus, k,0);
// 	    gsl_vector_set(stochastic.AllNus, k,0);
// 	    gsl_vector_set(stochastic.SpeciesNumbers, k,0);
// 	    gsl_vector_set(stochastic.Biomass_SpeciesNumbers, k,0);
// 	    //printf("stochastic.AllMus ist nachher %f\n",gsl_vector_get(stochastic.AllMus,k));
// 	}
// 	migrationEventNumber = 0;
// 	for(j = 0 ; j < Z; j++)
// 	{
// 	  
// 	  stochMigration(nicheweb, stochastic, y, rng1, rng1_T, migrationEventNumber, Dchoice);
// 	  migrationEventNumber++;
// 	
// 	}
// 	
// 	createOutputBiomass(nicheweb, y);
// 	
// 	t = tend2;
//      }
//--Ende Test-----------------------------------------------------------------------------------------------------------------
	// if(status == GSL_SUCCESS)	printf("Status OK\n");

  	
  	//testf0 	= testf0*fixp0;				Holling verwendet nur fix0, 1, 2 und fixp 0, 1,2
	//testf1	= testf1*fixp1;				Da testf0,1,2 vorher 1 sind stehen in test0,1,2 die Werte von fixp0,1,2 
    //testf2	= testf2*fixp2;
   
  }
  
//   hmean = hmean/k;
//   printf("hmean ist %f\n",hmean);
  
  gsl_vector_set(nicheweb.migrPara, 6, migrationEventNumber);
  printf("migrationEventNumber ist %i\n", migrationEventNumber);
  if(migrationEventNumber==Z)
  {
    printf("\n\n\n HIER \n\n\n");
  }
  printf("Letztes Migrationsereignis zum Zeitpunkt %f\n", gsl_vector_get(nicheweb.migrPara, 0));
  printf("Es migrieren %f \n",gsl_vector_get(nicheweb.migrPara,5));
//--Ergebnis zusammen fassen--------------------------------------------------------------------------------------------------------------------   

  for(i=0; i<(Rnum+S)*Y; i++)
 	gsl_vector_set(result, 0*Y*(Rnum+S)+i, y[i]);						//y[Ende] + y0 + ymax + ymin + yavg + fixp + TL
	 			 
  for(i=0; i<(Rnum+S)*Y; i++)
	gsl_vector_set(result, 1*Y*(Rnum+S)+i, gsl_vector_get(y0, i));	

  for(i=0; i<(Rnum+S)*Y; i++)
	gsl_vector_set(result, 2*Y*(Rnum+S)+i, gsl_vector_get(ymax, i));	

  for(i=0; i<(Rnum+S)*Y; i++)
	gsl_vector_set(result, 3*Y*(Rnum+S)+i, gsl_vector_get(ymin, i));	

  for(i=0; i<(Rnum+S)*Y; i++)
	gsl_vector_set(result, 4*Y*(Rnum+S)+i, gsl_vector_get(yavg, i));

  for(i=0; i < S; i++)
	gsl_vector_set(result, 5*Y*(Rnum+S)+i, gsl_vector_get(nicheweb.network, (Rnum+S)*(Rnum+S)+1+Y*Y+1+(Rnum+S)+i));
	

  // Fixpunkte mit übergeben, die sollen in .out datei
	gsl_vector_set(result, 5*Y*(Rnum+S)+S+0, gsl_vector_get((&nicheweb)->fixpunkte, 3));	
	gsl_vector_set(result, 5*Y*(Rnum+S)+S+1, gsl_vector_get((&nicheweb)->fixpunkte, 4));
	gsl_vector_set(result, 5*Y*(Rnum+S)+S+2, gsl_vector_get((&nicheweb)->fixpunkte, 5));


	free(y);
//	free(params);

	gsl_vector_free(y0);
	gsl_vector_free(ymax);
	gsl_vector_free(ymin);
	gsl_vector_free(yavg);
	gsl_odeiv2_step_free(s);
	
 //	gsl_rng_free(rng1);

	gsl_odeiv2_control_free(c);
  	gsl_odeiv2_evolve_free(e);
	gsl_odeiv2_driver_free(d);
	
	
  return result;

}