Beispiel #1
0
int main() {                                                    
  
  // http://cdiac.ornl.gov/pns/current_ghg.html
  const double c_co2 = 390;
  const double c_n2o = 0.32;
  const double c_co = 0.1;
  const double c_ch4 = 1.775;
  const double c_o2 = 20.9e+4;
  
  
  const double cp=1004;  /* J/kg K */
  const double deltat=3600.0*4.0;  /* s */
  const double Ra=287; /* J/kg K */

  int timesteps = 0;
  int ilev=0;
  int ilyr=0;
  const int nlyr=20;  /* Number of Layers */
  const int nlev=nlyr+1;
  int instabil=FALSE;

  int status;
  
  
  

  const double deltap=PSURF/nlyr;
  const double kappa=Ra/cp;
  
  double *p=calloc(nlev,sizeof(double));
  double *T=calloc(nlyr,sizeof(double));
  double *theta=calloc(nlev,sizeof(double));
  double *plyr=calloc(nlyr,sizeof(double));
  double *z=calloc(nlev,sizeof(double));
  double *zlyr=calloc(nlyr,sizeof(double));
  double *deltazlyr=calloc(nlyr,sizeof(double));
  double *deltazlev=calloc(nlev,sizeof(double));
  double *h2o=calloc(nlyr,sizeof(double));
  double *o3=calloc(nlyr,sizeof(double));
  
  double ***dtaumol;
  double **wgt;
  double *wavelength;
  int nbnd=0;
  int *nch;
  int iv;
  int iq;
  
  double *tmplev=calloc(nlev, sizeof(double)); /* temporary vector with length lev */
  double *tmplyr=calloc(nlyr, sizeof(double)); /* temporary vector with length lyr */
  
  double *eup=calloc(nlev, sizeof(double));
  double *edn=calloc(nlev, sizeof(double));
  double *euptmp=calloc(nlev, sizeof(double));
  double *edntmp=calloc(nlev, sizeof(double));
  double *edirtmp=calloc(nlev, sizeof(double));
  double *enet=calloc(nlyr, sizeof(double));

  double *deltaTday=calloc(nlyr, sizeof(double));
  double *deltaT=calloc(nlyr, sizeof(double));
  
  //------------- Define variables for Rodents---------------//
  
  const double gr_albedo=0.3; /* 1 */
  
  const int mu_counterlimit = (int)(24.0 * 3600 / deltat);
  double* const mu_0 = calloc(mu_counterlimit, sizeof(double));
  int mu_counter;

  // --- start mu_0
  {
  double mu_weight = 0;

  for(mu_counter=0; mu_counter < mu_counterlimit; mu_counter++) {
    mu_0[mu_counter] = cos( 2.0*M_PI / mu_counterlimit * (mu_counter + mu_counter+1) / 2.0 );
    if(mu_0[mu_counter] > 0)
      mu_weight += mu_0[mu_counter];
  }
  // weight needs to be adjusted such that it is 0.5 for half of the steps (day-only)
  mu_weight = mu_weight / (mu_counterlimit/2.0 * 0.5);

  for(mu_counter=0; mu_counter < mu_counterlimit; mu_counter++) {
    mu_0[mu_counter] = mu_0[mu_counter] / mu_weight;
    printf("mu_0[%d] = %e\n", mu_counter, mu_0[mu_counter]);
  }
  mu_counter = 0;
  
  }
  // --- end mu_0
  
  double* S_0;
  double* omega_0=calloc(nlyr,sizeof(double));
  double* gassy=calloc(nlyr,sizeof(double));
  double* f=calloc(nlyr,sizeof(double));
  
  
  for(ilyr=0; ilyr<nlyr; ilyr++) {
   omega_0[ilyr] = 0.f;
   gassy[ilyr] = 0; //0.85f;
   f[ilyr] = 0; //0.8f;
  }
  
  {
    double *temp1, *temp2, *temp3;
    int temp4;
      status=read_4c_file("mstrnx.data/solar.dat", &temp1, &temp2, &temp3, &S_0, &temp4);
      if (status !=0) {
      printf("Error reading Solar.dat\n");
      return EXIT_FAILURE;
      }
      
      free(temp1);
      free(temp2);
      free(temp3);
  } 
  
  //--------------------- Plot color -------------------------//

#ifndef _NOPLOT
  plscolbg (255, 255, 255);   /* background color white */
  plscol0  (15, 0, 0, 0);     /* set color 15 to black  */
  plsdev ("xwin"); /* if not called, the user is asked! */
  plinit ();
  
  plssub(2,2);
#endif
  
  //--------------------- Calculate pressure for layers and levels -------------//

  for(ilev=0; ilev<nlev; ilev++) {                              /* Calculation of the Pressure at the Levels p[ilev] */
    p[ilev]=PSURF*ilev/(nlev-1);
  }
    
  for(ilyr=0; ilyr<nlyr; ilyr++) {                            /* Calculation of the Pressure in the Layers*/
    plyr[ilyr]= 0.5*(p[ilyr]+p[ilyr+1]);
  }
   
  
  //----------------------- Reading afglus ------------------------//
  
  {
    
    double *ztemp;
    double *ptemp;
    double *Ttemp;
    double *h2otemp;
    double *o3temp;

    int status;
    int ntemp;
    int itemp;
    unsigned int mintemp = 0;
    unsigned int mintemp2 = 0;
    
    status = read_5c_file("mstrnx.data/afglus.atm", &ztemp, &ptemp, &Ttemp, &h2otemp, &o3temp, &ntemp);
    if (status !=0) {
      printf("Error reading Temperature profile\n");
      return EXIT_FAILURE;
    }

    /*
      for (itemp=0; itemp<ntemp; itemp++) {
      printf("itemp = %d, ztemp = %f, ptemp = %f, Ttemp = %f, h2otemp = %f, o3temp = %f\n", itemp, ztemp[itemp], ptemp[itemp], Ttemp[itemp], h2otemp[itemp], o3temp[itemp]);
      }
    */  
    
    //-------------------- Interpolate T, h2o and o3 to layers --------------//
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {
      // printf("ilev = %d, searching for %f\n", ilyr, plyr[ilyr]);
      for (itemp=0; itemp<ntemp; itemp++) {
	if ( abs(ptemp[mintemp]- plyr[ilyr])>abs(ptemp[itemp]-plyr[ilyr])) {
	  mintemp=itemp;
	}
      }
      if (ptemp[mintemp]<plyr[ilyr]) {
	mintemp2 = mintemp+1;
      }
      else {
	mintemp2 = mintemp-1;
      }
      // printf("\tmintemp  = %d,\tp = %f\n", mintemp, ptemp[mintemp]);
      // printf("\tmintemp2 = %d,\tp = %f\n", mintemp2, ptemp[mintemp2]);

      const double weight2 = abs(ptemp[mintemp]-plyr[ilyr]);
      const double weight1 = abs(ptemp[mintemp2]-plyr[ilyr]);
      const double norm = weight1 + weight2;
      
      /* T in levels?!?! */
      T[ilyr] = Ttemp[mintemp]*weight1 + Ttemp[mintemp2]*weight2;
      T[ilyr] /= norm;
      
      h2o[ilyr] = h2otemp[mintemp]*weight1 + h2otemp[mintemp2]*weight2;
      h2o[ilyr] /= norm;

      o3[ilyr] = o3temp[mintemp]*weight1 + o3temp[mintemp2]*weight2;
      o3[ilyr] /= norm;
    }
  }
  
  
  for (ilyr=0; ilyr<nlyr; ilyr++)  {
    printf("ilyr = %d, plyr = %f,\tT = %f,\th2o = %f,\to3 = %f\n", ilyr, plyr[ilyr], T[ilyr], h2o[ilyr], o3[ilyr]);
  }
  
  /* 
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("T[%d] = %f\n", ilyr, T[ilyr]);
     }
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("o3[%d] = %f\n", ilyr, o3[ilyr]);
     }
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("h2o[%d] = %f\n", ilyr, h2o[ilyr]);
     }
  */
  
   
  
  
  //--------------------------------- Starting while-loop ----------------------------------//
  //----------------------------------------------------------------------------------------//
  
  while (timesteps*deltat<TIME_MAX) {
    // printf("\nNew time %d: T = %f\n", (int)(timesteps*deltat), T[nlyr-1]);
    timesteps++;
  
    for(ilev=0; ilev<nlev; ilev++) {                    /* Reseting edn, eup and enet */
      edn[ilev] = 0;
      eup[ilev] = 0;
      if(ilev < nlyr) //enet is defined for ilyr only
        enet[ilev]=0;
    }
    
    
    //--------------------- Calculate z ----------------------//
    
    for (ilyr=0;ilyr<nlyr; ilyr++) {                         /* z for layers */
      deltazlyr[ilyr]=(Ra*T[ilyr]*deltap)/(plyr[ilyr]*g);  
    }
      
    zlyr[nlyr-1]=0; //check this one time
    for (ilyr=nlyr-2; ilyr >= 0; ilyr--) {
      zlyr[ilyr]=zlyr[ilyr+1]+deltazlyr[ilyr];
    }
      
     
    for (ilev=0;ilev<nlev-1; ilev++) {                      /* z for levels */
      deltazlev[ilev]=(Ra*T[ilev]*deltap)/(plyr[ilev]*g);  
    }
      
    z[nlev-1]=0;
    for (ilev=nlev-2; ilev >= 0; ilev--) {
      z[ilev]=z[ilev+1]+deltazlev[ilev];
     
    }
      
      
      
    //--------------------- Use k-distribution ---------------------//
      
    /* mu_0 is defined for a 8-part cycle of the earth, i.e. a timestep of 3 hours! */
    if(++mu_counter == mu_counterlimit)
      mu_counter = 0;

    //calculate it every two days!
    if((timesteps-1) % 10 == 0)
      //warning, wavelength is in nm!
      status = ck_mstrnx (z, plyr, T, h2o, o3, nlev, /*lyrflag*/ 1, c_co2, c_n2o, c_co, c_ch4, c_o2, &dtaumol, &wgt, &wavelength, &nbnd, &nch);
    
    for(iv=0; iv<nbnd; iv++) {
      //printf("iv = %d, wavelength = %e, S0 = %e\n", iv, wavelength[iv], S_0[iv]);
	  
      
      //------------- Use schwarzschild and sum edn eup --------//
	
      
      for(iq=0; iq<nch[iv]; iq++) {
	schwarzschild2(dtaumol[iv][iq], T, nlev, T[nlyr-1], edntmp, euptmp, wavelength[iv]*1e-6,wavelength[iv+1]*1e-6, tmplev, tmplyr);	

	for(ilev=0; ilev<nlev; ilev++) {
	  edn[ilev] += edntmp[ilev]*wgt[iv][iq];
	  eup[ilev] += euptmp[ilev]*wgt[iv][iq];
	  //printf("ilev = %d, iv = %d of %d, iq = %d of %d, wgt = %e, edntmp = %e, euptmp = %e\n", ilev, iv, nbnd, iq, nch[iv], wgt[iv][iq], edntmp[ilev], euptmp[ilev]);
	}
	
	//------------------- Include Rodents---------------------//  
	if(mu_0[mu_counter] > 0) {
	  rodents_solar(nlyr, dtaumol[iv][iq], omega_0, gassy, f, S_0[iv], mu_0[mu_counter], gr_albedo, edntmp, euptmp, edirtmp);
	  
	  for(ilev=0; ilev<nlev; ilev++) {
	    edn[ilev] += (edntmp[ilev]+edirtmp[ilev])*wgt[iv][iq];
	    eup[ilev] += euptmp[ilev]*wgt[iv][iq];
	    //printf("ilev = %d, iv = %d of %d, iq = %d of %d, wgt = %e, edntmp = %e, euptmp = %e, edirtmp = %e\n", ilev, iv, nbnd, iq, nch[iv], wgt[iv][iq], edntmp[ilev], euptmp[ilev], edirtmp[ilev]);
	  }
	}
      }
    }
     
    //---------------- Calculate enet -----------------------// 
    
    for(ilyr=0; ilyr<nlyr-1; ilyr++) {
      enet[ilyr] = eup[ilyr+1] + edn[ilyr] - eup[ilyr] - edn[ilyr+1];
     
    }

    for (ilev=0; ilev<nlev; ilev++) {
      //printf("ilev = %d, eup = %f, edn = %f\n", ilev, eup[ilev], edn[ilev]);
    }

    //---------------- Calculate temperature gain per layer -----------//
    
    for (ilyr=0; ilyr<nlyr-1; ilyr++)  {
      deltaT[ilyr]=(enet[ilyr]*g*deltat)/((deltap*100.0)*cp);
      T[ilyr] +=deltaT[ilyr];
      //printf("dT[ilyr] = %f\n", ilyr, deltaT[ilyr]);
    }
  
    //----------------Calculate Temperature gain for ground layer ---------//
    
    deltaT[nlyr-1]=((edn[nlyr-1]-eup[nlyr-1])*g*deltat)/((deltap*100.0)*cp);
    T[nlyr-1] += deltaT[nlyr-1];


    //---------- Convert T to theta ------------//
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {                           
      theta[ilyr]=pow(PSURF/plyr[ilyr], kappa)*T[ilyr];
      //printf("%d %f\n", ilyr, theta[ilyr]);
    }
    
    
    //------------ Convection -------------------//
    
    instabil = FALSE;
    
    for (ilyr=0; ilyr<nlyr;ilyr++) {                            /* Testing for Instability */
      if (theta[ilyr+1]>theta[ilyr]) {
	instabil = TRUE;
	break;
      }
    }

    if (instabil) {                                             /* Convection - Sorting of Layers according to theta */
      qsort (theta, nlyr, sizeof(double), sortfunc);
      //  printf ("Konvektion :-)\n");
    }
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {                           /* Conversion from theta to T */
      T[ilyr] = theta[ilyr] /  (pow(PSURF/plyr[ilyr], kappa));

      //printf("%d %f\n", ilyr, theta[ilyr]);
    }

    
    //-------------------- Print every x timestep ------------//
    
    if(timesteps % (int)(10) == 0) {
      printf ("timestep %d\n", timesteps);

      /* Printing time in readable format
       * In order to remove days, hours or minutes
       * just add // in front of the appropriate line
       */
      int time = timesteps*deltat;
      printf("time ");
      printf("%dd = ", (int)(time / 3600 / 24));
      printf("%dh = ", (int)(time / 3600 ));
      printf("%dmin = ", (int)(time / 60 ));
      printf("%ds\n", time);

      printf(" Tsurf=%f\n", T[nlyr-1]);

      for (ilyr=0; ilyr<nlyr; ilyr++) {
	deltaTday[ilyr]=deltaT[ilyr]*86.4/2;
      }

      plotall(nlyr, T, plyr, zlyr, deltaTday);

      /* for (ilyr=0; ilyr<nlyr; ilyr++) { */
      /*   printf("ilyr %d, z=%f,  plyr=%f,theta=%f, T=%f\n", ilyr, z[ilyr], plyr[ilyr],theta[ilyr], T[ilyr]); */
      /* } */

      for (ilyr=0; ilyr<nlyr; ilyr++) {
      	printf("p%f, edn%f, eup%f, enet%f\n", p[ilyr], edn[ilyr], eup[ilyr], enet[ilyr]);
      }
      printf("p%f, edn%f, eup%f\n", p[ilyr], edn[ilyr], eup[ilyr]);
    }
   
  }                                     

  free(tmplyr);
  free(tmplev);
  //----------------- End of while-loop---------------------//
  
  printf("\nTime %d: T = %f\n", (int)(timesteps*deltat), T[nlyr-1]);
  for (ilyr=0; ilyr<nlyr; ilyr++) {
    printf("%d %f\n", ilyr, T[ilyr]);
  }

  printf("\nTsurf=%f\n", T[nlyr-1]);

  return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
#if DEBUG
  feenableexcept(FE_DIVBYZERO| FE_INVALID|FE_OVERFLOW); // enable exceptions
#endif
  if(argc ^ 2){
    printf("Usage: ./sa sa.config\n");
    exit(1);
  }
  if(read_config(argv[1])){           // read config
    printf("READDATA: Can't process %s \n", argv[1]);
    return 1;
  }  
  T = T+SKIP;               // increase T by SKIP to go from 0 to T all the way in time units
  
  // precomputing  
  I_Gamma = 1.0/Gamma;
  I_Alpha = 1.0/Alpha;    
  Bo = B;    
  X0_Be = pow(X0, Beta);
  GaBe  = Gamma * Beta;

  // event probabilites scaling by G;
  PE[0] = PE[0]*(double)G;
  PE[1] = PE[1]*(double)G;
  PE[2] = PE[2]*(double)G;
  
  initrand(Seed);
  init();          // note: method is in init.c  // allocate memory at initial  
  
  int i, j, ev, k = 0; // m;
  unsigned long seed;
  double dt, ct, tt;      // tt: time of simulation; dt: delta time after which next event occurs; ct: counter time to check with skip time for stat calculation
  time_t now;   
 
  for( i = 0; i < Runs; i++){
    tt = 0.0, ct = 0.0;
    k = 0; //m = 0;
    if(Seed == 0){      
      now = time(0);
      seed = ((unsigned long)now);  
      seed = 1454011037;
      initrand(seed);
      printf("\n run: %d rand seed: %lu\n",i+1, seed);
    }
    set_init_xrvpi();   // sets initial strategies vector, roles, valuations and payoffs       
     
    calc_stat(0, i);
    
    for( j = 0; j < N; j++) printf("%.4lf  ", V[0][j]); printf("\n");
    
    // Gillespie's stochastic simulation 
    while(tt < T){     
      // calc lambda; lambda = summation of P[j]s
      for(Lambda = 0, j = EVENTS; j--;)
	Lambda += PE[j];
      // select time for next event
      dt = randexp(Lambda); //printf("dt: %.4lf\n", dt);
      // select next event
      ev = select_event();
      // play the event      
      play_event(ev);
      // update time
      tt += dt;
      // calc stat
      ct += dt;
      if(ct >= SKIP){
        calc_stat(++k, i); // calculates all the stats	
	ct = 0.0;
      }
      // update events associated probabilites if necessary
    }
#if ALLDATAFILE
    calc_stat(-1, -1);    // free file pointers for individual run data files
#if !CLUSTER
    plotallIndividualRun(i, 0);      // note: method is in dataplot.c  
#if GRAPHS
    plotallIndividualRun(i, 1);      // note: method is in dataplot.c
#endif
#endif
    // write traits of final state
    {
      int m, n;
      char tstr[200], str[100];
      sprintf(str, "traits%d.dat", i);
      sprintf(str, "traits%d.dat", i); prep_file(tstr, str);
      FILE *fp = fopen(tstr, "w");
      for(m = 0; m < G; m++){
	for( n = 0; n < GS[m]; n++){
	  fprintf(fp, "%.4lf %.4lf    ", dxi[m][n], dsi[m][n]);
	}
	fprintf(fp, "\n");
      }
      fclose(fp);
    }
#if PUNISH
#if DISP_MATRIX
    plotTraits(i, 0);   // note: method is in dataplot.c
#endif
#if GRAPHS
    plotTraits(i, 1);   // note: method is in dataplot.c
#endif
#endif
#endif
  }  
  // write effort and payoff data
  writefile_effort_fertility();              // note: method is in dataplot.c
  writefile_threshold_aggressiveness();      // note: method is in dataplot.c
  // plot data with graphics using gnuplot
  if(!CLUSTER){    
    plotall(0);                              // note: method is in dataplot.c
#if GRAPHS
    plotall(1);                              // note: method is in dataplot.c
#endif
  }
#if PUNISH
#if DISP_MATRIX
  displayMatrix();                           
#endif  
#endif
  
  cleanup();                                 // note: method is in init.c
  return 0;
}