Esempio n. 1
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;
}
Esempio n. 2
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);
}
Esempio n. 3
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);
}
Esempio n. 4
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);
  }
}
Esempio n. 5
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);
}
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;

}
Esempio n. 7
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);
}