double dodes(double initial_value, double start_time, double end_time, \
			int (*ode_function), char *solver_type, double nequs, double eps_abs, \
			double eps_rel, double step_size, int *params)
{
	double out = 0, t = 0;
	//int status;
	out = initial_value;
	t = start_time;
	//Setup ODE related parameters
	gsl_odeiv2_system sys = {ode_function, NULL, nequs, NULL};

	gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc (gsl_odeiv2_step_rkf45, nequs);
  	gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, eps_rel);
  	gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (nequs);
  
  	while(t < end_time)
  	{
  		gsl_odeiv2_evolve_apply_fixed_step (e, c, s, &sys, &t, step_size, &out);
	}

	gsl_odeiv2_evolve_free (e);
  	gsl_odeiv2_control_free (c);
  	gsl_odeiv2_step_free (s);

	return out;
	
}
Example #2
0
Integrator::~Integrator() {
	// Destructor

	// Release the GSL objects
	gsl_odeiv2_evolve_free(evolve);
	gsl_odeiv2_control_free(control);
	gsl_odeiv2_step_free(step);

}
Example #3
0
int main (void) 
{
  size_t neqs = 4;          /* number of equations */
  double eps_abs = 1.e-8, 
    eps_rel = 0.;           /* desired precision */
  double stepsize = 1e-6;   /* initial integration step */
  double R = 5.;            /* the aerodynamic efficiency */
  double t = 0., t1 = 240.; /* time interval */
  int status;
  /* 
   * Initial conditions 
   */
   
  double theta = -M_PI/3;
  
  for (theta = -M_PI/3; theta < +M_PI/3; theta +=M_PI/12)
  {
    double y[4] = { 2, theta, 0., 2. };   /* for -pi/3 <= theta <= pi/3 */
  /*
   * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. 
   * This method is a good general-purpose integrator.
   */
  	gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc 
                              (gsl_odeiv2_step_rkf45, neqs);
    gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, 
	  					    eps_rel);
    gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (neqs);
    
    gsl_odeiv2_system sys = {func, NULL, neqs, &R};
    
  /*
   * Evolution loop 
   */
    while ( (t < t1) && (y[3] > 0) )
    {
      status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t,
                                        t1, &stepsize, y);

      if (status != GSL_SUCCESS) {
        printf ("Troubles: % .5e  % .5e  % .5e  % .5e  % .5e\n", 
                t, y[0], y[1], y[2], y[3]);
        break;
      }

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

    gsl_odeiv2_evolve_free (e);
    gsl_odeiv2_control_free (c);
    gsl_odeiv2_step_free (s);
  }
  
  return 0;
}
Example #4
0
File: build.c Project: IlariaD/ssm
void ssm_calc_free(ssm_calc_t *calc, ssm_nav_t *nav)
{
    gsl_rng_free(calc->randgsl);

    if (nav->implementation == SSM_ODE  || nav->implementation == SSM_EKF){

        gsl_odeiv2_step_free(calc->step);
        gsl_odeiv2_evolve_free(calc->evolve);
        gsl_odeiv2_control_free(calc->control);

        if(nav->implementation == SSM_EKF){
            gsl_vector_free(calc->_pred_error);
            gsl_matrix_free(calc->_St);
            gsl_matrix_free(calc->_Stm1);
            gsl_matrix_free(calc->_Rt);
            gsl_matrix_free(calc->_Ht);
            gsl_matrix_free(calc->_Kt);
            gsl_matrix_free(calc->_Tmp_N_SV_N_TS);
            gsl_matrix_free(calc->_Tmp_N_TS_N_SV);
            gsl_matrix_free(calc->_Q);
            gsl_matrix_free(calc->_FtCt);
            gsl_matrix_free(calc->_Ft);
            gsl_vector_free(calc->_eval);
            gsl_matrix_free(calc->_evec);
            gsl_eigen_symmv_free(calc->_w_eigen_vv);
        }

    } else if (nav->implementation == SSM_SDE){
        free(calc->y_pred);
    } else if (nav->implementation == SSM_PSR){
        ssm_psr_free(calc);
    }

    free(calc->to_be_sorted);
    free(calc->index_sorted);

    if(calc->covariates_length){
        int k;
        for(k=0; k< calc->covariates_length; k++) {
            if(calc->spline[k]){
                gsl_spline_free(calc->spline[k]);
            }
            if(calc->acc[k]){
                gsl_interp_accel_free(calc->acc[k]);
            }
        }

        free(calc->spline);
        free(calc->acc);
    }

    free(calc);
}
Example #5
0
File: driver.c Project: CNMAT/gsl
void
gsl_odeiv2_driver_free (gsl_odeiv2_driver * state)
{
  if (state->c != NULL)
    {
      gsl_odeiv2_control_free (state->c);
    }

  gsl_odeiv2_evolve_free (state->e);
  gsl_odeiv2_step_free (state->s);
  free (state);
}
Example #6
0
IzhikevichBranch::subthreshold_regimeRegime_::~subthreshold_regimeRegime_() {
    
    // GSL structs only allocated by init_nodes_(),
    // so we need to protect destruction
    if ( s_ != NULL)
        gsl_odeiv2_step_free (s_);
    if ( c_ != NULL)
        gsl_odeiv2_control_free (c_);
    if ( e_ != NULL)
        gsl_odeiv2_evolve_free (e_);
    if ( u != NULL)
        free (u);
    if ( jac != NULL)
        free (jac);}
Example #7
0
int
main (void)
{
    size_t neqs = 2;            /* number of equations */
    double eps_abs = 1.e-8, eps_rel = 0.;       /* desired precision */
    double stepsize = 1e-6;     /* initial integration step */
    double rho_c = 10.;         /* central density */
    double r = 1.e-3, r1 = 5.;  /* time interval */
    int status;
    /* 
     * Initial conditions 
     */
    double y[2] = { 0, rho_c };

    /*
     * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. 
     * This method is a good general-purpose integrator.
     */
    gsl_odeiv2_step *s = gsl_odeiv2_step_alloc (gsl_odeiv2_step_rkf45, neqs);
    gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs,
                                                      eps_rel);
    gsl_odeiv2_evolve *e = gsl_odeiv2_evolve_alloc (neqs);

    gsl_odeiv2_system sys = { dwarf_eqs, NULL, neqs, &rho_c };

    /*
     * Evolution loop 
     */
    while ((r < r1) && (y[1] > 0))
    {
        status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &r,
                                          r1, &stepsize, y);

        if (status != GSL_SUCCESS)
        {
            printf ("Troubles: % .5e  % .5e  % .5e\n", r, y[0], y[1]);
            break;
        }

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

    gsl_odeiv2_evolve_free (e);
    gsl_odeiv2_control_free (c);
    gsl_odeiv2_step_free (s);

    return 0;
}
int main (void) 
{
	size_t neqs = 2;					/* number of equations */
	double eps_abs = 1.e-8, 
		eps_rel = 0.;					/* desired precision */
	double stepsize = 1e-6;				/* initial integration step */
	int status;
	double t = 0., t1 = 100.;			/* time interval */
	
	double ommega = 140.;				/* ommega for UNstable oscillation */
	double y[2] = { 0.99 * PI, 0. };	/* initial conditions (phi, phy dot) */
	
	/*
	 * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. 
	 * This method is a good general-purpose integrator.
	 */
	gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc 
					(gsl_odeiv2_step_rkf45, neqs);
	gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, 
					eps_rel);
	gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (neqs);

	gsl_odeiv2_system sys = {func, NULL, neqs, &ommega};

	/*
	 * Evolution loop 
	 */
	while (t < t1)
	{
		status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t,
			t1, &stepsize, y);

		if (status != GSL_SUCCESS) {
			printf ("Troubles: % .5e  % .5e  % .5e\n", 
				t, y[0], y[1]);
			break;
		}

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

	gsl_odeiv2_evolve_free (e);
	gsl_odeiv2_control_free (c);
	gsl_odeiv2_step_free (s);

	return 0;
}
void dodea(double *initial_value, double start_time, double end_time, \
			int (*ode_function), char *solver_type, double nequs, double eps_abs, \
			double eps_rel, double step_size, int *params, double *out)
{
	double t = start_time;
	gsl_odeiv2_step_type *step_type;

	/*Initialise output to initial state*/
	int counter = 0;
	for (counter = 0; counter<nequs;counter++)
	{
		out[counter] = initial_value[counter];
	}

	/*Setup ODE related parameters*/
	gsl_odeiv2_system sys = {ode_function, NULL, nequs, params};

	/*Select step solver*/
	if (solver_type == "adams")
		step_type = gsl_odeiv2_step_msadams;
	if (solver_type == "stiff")
		step_type = gsl_odeiv2_step_msbdf;
	if (solver_type == "rk")
		step_type = gsl_odeiv2_step_rk4;
	if (solver_type == "rkf")
		step_type = gsl_odeiv2_step_rkf45;
	if (solver_type == "root")
		step_type = gsl_odeiv2_step_rkck;
	if (solver_type == "discrete")
		step_type = gsl_odeiv2_step_rk8pd;
	else 
		step_type = gsl_odeiv2_step_rkf45;
		
	gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc (step_type, nequs);
  	gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, eps_rel);
  	gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (nequs);
  
  	while(t < end_time)
  	{
  		gsl_odeiv2_evolve_apply_fixed_step (e, c, s, &sys, &t, step_size, out);
	}

	gsl_odeiv2_evolve_free (e);
  	gsl_odeiv2_control_free (c);
  	gsl_odeiv2_step_free (s);
}
Example #10
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;

}
int main (void) 
{
	size_t neqs = 4;          /* number of equations */
	double eps_abs = 1.e-8, eps_rel = 0.;           /* desired precision */
	double stepsize = 1e-6;   /* initial integration step */
	double R = 10;            /* the aerodynamic efficiency */
	double t = 0., t1 = 600. ; /* time interval */
	int status;
/* 
 * Initial conditions 
 */
//loop twentyish times increassing launch angle
	for(int i = 1; i <= 20; i++)
	{
		double y[4] = { 2.,-((double)PI / 3.), 0., 2. };
		y[1] += i * 0.1;
		double inangle = y[1];
/*
 * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. 
 * This method is a good general-purpose integrator.
 */
		gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc (gsl_odeiv2_step_rkf45, neqs);
		gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, eps_rel);
		gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (neqs);
		gsl_odeiv2_system sys = {func, NULL, neqs, &R};
/*
 * Evolution loop
 */
		while ( (t < t1) && (y[3] > 0) )
		{
			status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t, t1, &stepsize, y);
			if (status != GSL_SUCCESS) {
				printf ("Troubles: % .5e  % .5e  % .5e  % .5e  % .5e\n", t, y[0], y[1], y[2], y[3]);
				break;
			}
		}
//printf moved outside while loop to get final vaules
		printf ("% .5e  % .5e  % .5e  % .5e  % .5e\n", t, y[0], inangle, y[2], y[3]);
		gsl_odeiv2_evolve_free (e);
		gsl_odeiv2_control_free (c);
		gsl_odeiv2_step_free (s);
	}
	return 0;
}
Example #12
0
/* 
 *      FUNCTION  
 *         Name:  red_evol
 *  Description:  
 * 
 */
int red_evol ( void* params, const double r[], double time_end, double step,
		const gsl_vector* req_red, gsl_matrix* red_m )
{
	struct f_params* pars = (struct f_params*) params ;

	unsigned int i ;                        /* counter for the for loops */

	/* 
	 *
	 *  evolving the systems
	 * 
	 */
	double t = 0 ; 

	/* setting the initial vector */
	gsl_vector* init_red = gsl_vector_calloc(4) ;
	for ( i = 0 ; i < 4 ; i++ )
		gsl_vector_set ( init_red, i, r[i] ) ;

	/* 
	 *
	 * Initializing the system for Redfield dynamics
	 *
	 */
	gsl_odeiv2_system red_sys = { generator, jac, 4, (void*) red_m } ;

	/* Choosing the step function type: Runge-Kutta-Fehlberg (4,5) */	
	/* gsl_odeiv2_step* s = gsl_odeiv2_step_alloc ( gsl_odeiv2_step_rkf45 , 4 ) ; */

	/* Choosing the step function type: Runge-Kutta Cash-Karp (4,5) */	
	gsl_odeiv2_step* r_s = gsl_odeiv2_step_alloc ( gsl_odeiv2_step_rkck , 4 ) ;

	/* Setting the step control: abserr=1e-9, relerr=1e-3 */
	gsl_odeiv2_control* r_c = gsl_odeiv2_control_standard_new ( 1e-9, 1e-3, 1, 1 ) ;

	/* Allocating the space for evolution function */
	gsl_odeiv2_evolve* r_e = gsl_odeiv2_evolve_alloc ( 4 ) ;

	/* opening the files */
	FILE* f_red = fopen ( "RED-EVOLUTION.dat", "w" ) ;
	FILE* g_red = fopen ( "RED-ENTROPY-PROD.dat", "w" ) ;
	FILE* h_red = fopen ( "RED-CURRENT.dat", "w" ) ;
	FILE* i_red = fopen ( "RED-ENTROPY.dat", "w" ) ;


	/* writing data */
	while ( t < t_end )
	{
		evol ( t, init_red, step, r_e, r_c, r_s, &red_sys ) ;
		fprintf ( f_red, "%.2f %.9f %.9f %.9f %.9f\n", t,
				VECTOR(init_red,1), VECTOR(init_red,2),
				VECTOR(init_red,3),
				gsl_hypot3(VECTOR(init_red,1),VECTOR(init_red,2),
					VECTOR(init_red,3)) ) ;
		fprintf ( g_red, "%.2f %.9f\n",
				t, entropy_production( init_red, req_red, red_m )) ;
		fprintf ( h_red, "%.2f %.9f\n",	t, tot_current(init_red, pars) ) ;
		fprintf ( i_red, "%.2f %.9f\n",
				t, entropy_of_state(init_red) ) ;
		t += step ;
	}
	
	/* final entropy */
	printf("Final entropy: %g\n", entropy_of_state(init_red) ) ;

	/*  close the files */
	fclose (f_red) ;
	fclose (g_red) ;
	fclose (h_red) ;
	fclose (i_red) ;

	/* free memory for evolution */
	gsl_odeiv2_evolve_free (r_e) ;
	gsl_odeiv2_control_free (r_c) ;
	gsl_odeiv2_step_free (r_s) ;

	return 0;
}		/* -----  end of function red_evol  ----- */
Example #13
0
int main (void) 
{
  size_t neqs = 4;          /* number of equations */
  double eps_abs = 1.e-10, 
    eps_rel = 0.;           /* desired precision */
  double stepsize = 1e-6;   /* initial integration step */
  double R = 10.;           /* the aerodynamic efficiency */
  double t, t1;             /* time interval */
  int status;
  
  int i, np = 50;           /* number of points */
  double alt1 = 0.1, alt2 = 5., altitude;
  double step;
  
  step = (alt2 - alt1)/(np - 1);
  
  for (i = 0; i < np; i++)
  {
    t = 0.;
    t1 = 520.;              /* Code stops before hitting maximum time step */
    altitude = alt1 + i*step;
    /* 
     * Initial conditions 
     */
    double y[4] = { 2., 0., 0., altitude };   /* for res3 */

    /*
     * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. 
     * This method is a good general-purpose integrator.
     */
    gsl_odeiv2_step    *s = gsl_odeiv2_step_alloc 
                            (gsl_odeiv2_step_rkf45, neqs);
    gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, 
						    eps_rel);
    gsl_odeiv2_evolve  *e = gsl_odeiv2_evolve_alloc (neqs);
    
    gsl_odeiv2_system sys = {func, NULL, neqs, &R};
    
    /*
     * Evolution loop 
     */
    while ( (t < t1) && (y[3] > 0) )            /* ends loop before max time step */
    {
      status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t,
                                      t1, &stepsize, y);

      if (status != GSL_SUCCESS) {
        printf ("Troubles: % .5e  % .5e  % .5e  % .5e  % .5e\n", 
              t, y[0], y[1], y[2], y[3]);
        break;
      }
    }
    printf ("% .5e  % .5e  % .5e  % .5e  % .5e  % .5e \n", 	      
            t, y[0], y[1], y[2], y[3], altitude);
  
    gsl_odeiv2_evolve_free (e);
    gsl_odeiv2_control_free (c);
    gsl_odeiv2_step_free (s);

  }
  return 0;
}
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;

}