コード例 #1
0
ファイル: i2c.c プロジェクト: useche/btstats
void i2c_print_results(const void *data)
{
	DECL_ASSIGN_I2C(i2c,data);
	double p;
	double avg = 0;
	__u32 i;
	__u64 tot_time = 0;

	for (i = 0; i <= i2c->maxouts; i++) {
		tot_time += i2c->oio[i].time;
	}

	for (i = 0; i <= i2c->maxouts; i++) {
		p = ((double)i2c->oio[i].time)/((double)tot_time);

		if(i2c->oio_hist_f)
			fprintf(i2c->oio_hist_f, "%u\t%.2lf\n",i,100*p);

		avg += p*i;
	}

	/* print all histograms */
	if(i2c->oio_hist_f) {
		for (i = 1; i <= i2c->maxouts; i++) {
			fprintf(i2c->oio_hist_f, "\nread: %d\n",i);
			gsl_histogram_fprintf(i2c->oio_hist_f,
					i2c->oio[i].op[READ], "%g", "%g");
			fprintf(i2c->oio_hist_f, "\nwrite: %d\n",i);
			gsl_histogram_fprintf(i2c->oio_hist_f,
					i2c->oio[i].op[WRITE], "%g", "%g");
		}
	}

	printf("I2C Max. OIO: %u, Avg: %.2lf\n",i2c->maxouts,avg);
}
コード例 #2
0
ファイル: kde.c プロジェクト: gulkhan007/kde
int histc(double *data, int length, double *xmesh, int n , double *bins)
{
	XML_IN;

	printf("-1\n");

	gsl_histogram * h = gsl_histogram_alloc(n-1);

	printf("0\n");

	gsl_histogram_set_ranges (h, xmesh, n);
	
	double h_max = gsl_histogram_max(h);
	double h_min = gsl_histogram_min(h);

	printf("h_min: %g h_max: %g\n",h_min,h_max);

	for (int i=0; i<length; i++)
		gsl_histogram_increment (h, data[i]);

	printf("2\n");

	for (int i=0;i<n-1;i++)
		bins[i] = gsl_histogram_get (h, i);

	printf("3\n");

	gsl_histogram_fprintf (stdout, h, "%g", "%g");
	/*...*/

	gsl_histogram_free(h);

	XML_OUT;
	return 0;
}
コード例 #3
0
ファイル: wanglandau.c プロジェクト: mtw/RNAwl
/* ==== */
static void
initialize_dos_estimate(void)
{
  int i;
  const size_t n = s->n; /* nr of bins */

  if(wanglandau_opt.verbose){
    fprintf(stderr, "[[initialize_dos_estimate()]]\n");
  }
  
  if (wanglandau_opt.truedosbins_given){
    /* initialize the first n bins of g with true DOS as computed by
       RNAsubopt */
    if(wanglandau_opt.verbose){
      fprintf(stderr, "initializing the lowest %d bins of DOS estimate g with true DOS values from subopt:\n",
	      wanglandau_opt.truedosbins);
    }
    for (i=0;i<wanglandau_opt.truedosbins;i++){
      g->bin[i]=log(s->bin[i]);  /* get corresponding value from s histogram */ }
    for(i=wanglandau_opt.truedosbins;i<n;i++){
      g->bin[i]=g->bin[wanglandau_opt.truedosbins-1];
    }
  }
  else{
    /* initialize g uniformly with 0 */
    if (wanglandau_opt.verbose){
      fprintf(stderr, "initializing DOS estimate g with zeros:\n");
    }
  }
  if (wanglandau_opt.verbose){
    gsl_histogram_fprintf(stderr,g,"%6.2f","%30.6f");
    fprintf(stderr,"+++\ndone initializing\n");
  }
}
コード例 #4
0
ファイル: demo1.c プロジェクト: BrianGladman/gsl
int
main (void)
{
  struct data ntuple_row;
  int i;

  gsl_ntuple *ntuple = gsl_ntuple_open ("test.dat", &ntuple_row,
                                        sizeof (ntuple_row));

  gsl_histogram *h = gsl_histogram_calloc_uniform (100, 0., 10.);

  gsl_ntuple_select_fn S;
  gsl_ntuple_value_fn V;

  double scale = 1.5;

  S.function = &sel_func;
  S.params = &scale;

  V.function = &val_func;
  V.params = 0;

  gsl_ntuple_project (h, ntuple, &V, &S);

  gsl_histogram_fprintf (stdout, h, "%f", "%f");

  gsl_histogram_free (h);

  gsl_ntuple_close (ntuple);
}
コード例 #5
0
int main(void) {
	struct data ntuple_row;

	gsl_ntuple *ntuple
	= gsl_ntuple_open ("test.dat", &ntuple_row,
	sizeof (ntuple_row));
	double lower = 1.5;

	gsl_ntuple_select_fn S;
	gsl_ntuple_value_fn V;

	gsl_histogram *h = gsl_histogram_alloc (100);
	gsl_histogram_set_ranges_uniform(h, 0.0, 10.0);

	S.function = &sel_func;
	S.params = &lower;

	V.function = &val_func;
	V.params = 0;

	gsl_ntuple_project (h, ntuple, &V, &S);

	gsl_histogram_fprintf (stdout, h, "%f", "%f");
	gsl_histogram_free (h);
	gsl_ntuple_close (ntuple);

	return 0;
}
コード例 #6
0
int
main (int argc, char **argv)
{
  double a = 0.0, b = 1.0;
  size_t n = 10;

  if (argc != 3 && argc !=4)
    {
      printf ("Usage: gsl-histogram xmin xmax [n]\n");
      printf (
"Computes a histogram of the data on stdin using n bins from xmin to xmax.\n"
"If n is unspecified then bins of integer width are used.\n");
      exit (0);
    }

  a = atof (argv[1]);
  b = atof (argv[2]);

  if (argc == 4) 
    {
      n = atoi (argv[3]);
    }
  else
    {
      n = (int)(b - a) ;
    }

  {
    double x;
    gsl_histogram *h = gsl_histogram_alloc (n);

    gsl_histogram_set_ranges_uniform (h, a, b);

    while (fscanf(stdin, "%lg", &x) == 1)
      {
        gsl_histogram_increment(h, x);
      }

#ifdef DISPLAY_STATS
    {
      double mean = gsl_histogram_mean (h);
      double sigma = gsl_histogram_sigma (h);
      fprintf (stdout, "# mean = %g\n", mean);
      fprintf (stdout, "# sigma = %g\n", sigma);
    }
#endif

    gsl_histogram_fprintf (stdout, h, "%g", "%g");

    gsl_histogram_free (h);
  }

  return 0;
}
コード例 #7
0
/** @short Markov Chain Monte Carlo 
		@param rng the GSL random number generator 
		@param p the vector of parameters being searched over 
		@param x the vector of observations */
void mcmc(const gsl_rng *rng, gsl_vector *p, const gsl_vector *x)
{
	gsl_vector * p_test = gsl_vector_alloc(p->size);
	size_t i;
	
	gsl_histogram * hist = gsl_histogram_alloc(nbins);
	gsl_histogram_set_ranges_uniform(hist, min, max);

	for(i=0; i < chain_length; i++)
	{
		gsl_vector_memcpy(p_test,p);
		propose(rng, p_test);
		double lnLikelihoodRatio = ln_lik_fn(p_test, x) - ln_lik_fn(p, x);
		if (take_step(rng, lnLikelihoodRatio))
			p = p_test;
		gsl_histogram_increment(hist, p->data[1]);
	}
	gsl_vector_free(p_test);
	gsl_histogram_fprintf(stdout, hist, "%g", "%g");
	gsl_histogram_free(hist);
}
コード例 #8
0
ファイル: genome.cpp プロジェクト: nnoll523/IDB-Sim
void write_histogram(gsl_histogram* h, const char* s){
    FILE *stream = fopen(s,"w");
    gsl_histogram_fprintf(stream,h,"%g","%g");
    fclose(stream);
}
コード例 #9
0
ファイル: genome.cpp プロジェクト: nnoll523/IDB-Sim
void population::writeBlockHist(const char* s){
    FILE *stream = fopen(s,"w");
    gsl_histogram_fprintf(stream,blockHist,"%g","%g");
    fclose(stream);
}
コード例 #10
0
main (int argc,char *argv[])
{
    int ia,ib,ic,id,it,inow,ineigh,icont;
    int in,ia2,ia3,irun,icurrent,ORTOGONALFLAG;
    int RP, P,L,N,NRUNS,next,sweep,SHOWFLAG;
    double u,field1,field2,field0,q,aux1,aux2;
    double alfa,aux,Q1,Q2,QZ,RZQ,rho,R;
    double pm,D,wmax,mQ,wx,wy,h_sigma,h_mean;	
    double TOL,MINLOGF,E;
    double DELTA;
    double E_new,Ex,DeltaE,ER;
    double EW,meanhist,hvalue,wE,aratio;
    double logG_old,logG_new,lf;	
    size_t  i_old,i_new;	
    long seed;
    double lGvR,lGv,DlG;
    size_t iL,iR,i1,i2;
    int I_endpoint[NBINS];
    double lower,upper;
    size_t i0;		
	
    FILE * wlsrange; 
    FILE * dos;
    FILE * thermodynamics;
    FILE * canonical; 
    FILE * logfile;
    //FILE * pajek;
    	     	
//***********************************
// Help
//*********************************** 
   if (argc<15){
	   help();
	   return(1);
   }
   else{
    	DELTA = atof(argv[1]);
   	P = atoi(argv[2]);
        RP = atoi(argv[3]);
	L = atoi(argv[4]); 
	N = atoi(argv[5]);
	TOL = atof(argv[6]);
	MINLOGF = atof(argv[7]);
   }  	  
   wlsrange=fopen(argv[8],"w");	
   dos=fopen(argv[9],"w");  
   thermodynamics=fopen(argv[10],"w");
   canonical=fopen(argv[11],"w");
   logfile=fopen(argv[12],"w");	
   SHOWFLAG = atoi(argv[13]);
   ORTOGONALFLAG = atoi(argv[14]);

   if ((ORTOGONALFLAG==1) && (P>L)) P=L; 
   //maximum number of orthogonal issues 

   if (SHOWFLAG==1){
  	printf("# parameters are DELTA=%1.2f P=%d ",DELTA,P);
        printf("D=%d L=%d M=%d TOL=%1.2f MINLOGF=%g \n",L,N,RP,TOL,MINLOGF);
   }

  fprintf(logfile,"# parameters are DELTA=%1.2f P=%d D=%d",DELTA,P,L);
  fprintf(logfile,"L=%d M=%d TOL=%1.2f MINLOGF=%g\n",L,RP,TOL,MINLOGF);

//**********************************************************************
// Alocate matrices                                              
//**********************************************************************
    gsl_matrix * sociedade = gsl_matrix_alloc(SIZE,L);
    gsl_matrix * issue = gsl_matrix_alloc(P,L);
    gsl_vector * current_issue = gsl_vector_alloc(L);
    gsl_vector * v0 = gsl_vector_alloc(L);
    gsl_vector * v1 = gsl_vector_alloc(L);
    gsl_vector * Z = gsl_vector_alloc(L);  
    gsl_vector * E_borda = gsl_vector_alloc(NBINS);  	
     

//**********************************************************************
// Inicialization                                                
//**********************************************************************
    const gsl_rng_type * T;
    gsl_rng * r; 
  
    gsl_rng_env_setup();   
    T = gsl_rng_default;
    r=gsl_rng_alloc (T);

    seed = time (NULL) * getpid();
    //seed = 13188839657852;
    gsl_rng_set(r,seed);
   	
    igraph_t graph;
    igraph_vector_t neighbors;
    igraph_vector_t result;
    igraph_vector_t dim_vector;
    igraph_real_t res;
    igraph_bool_t C;

    igraph_vector_init(&neighbors,1000); 
    igraph_vector_init(&result,0);
    igraph_vector_init(&dim_vector,DIMENSION);
    for(ic=0;ic<DIMENSION;ic++) VECTOR(dim_vector)[ic]=N;

    gsl_histogram * HE = gsl_histogram_alloc (NBINS);
    gsl_histogram * logG = gsl_histogram_alloc (NBINS);
    gsl_histogram * LG = gsl_histogram_alloc (NBINS);

  //********************************************************************
  // Social Graph
  //********************************************************************
   //Barabasi-Alberts network
    igraph_barabasi_game(&graph,SIZE,RP,&result,1,0);

    /* for (inow=0;inow<SIZE;inow++){
         igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT);
         printf("%d ",inow);
         for(ic=0;ic<igraph_vector_size(&neighbors);ic++)
         {
                ineigh=(int)VECTOR(neighbors)[ic];
                printf("%d ",ineigh);
         }
          printf("\n");
     }*/

     //pajek=fopen("graph.xml","w");
    // igraph_write_graph_graphml(&graph,pajek);

     //igraph_write_graph_pajek(&graph, pajek);
     //fclose(pajek);


//**********************************************************************
//Quenched issues set and Zeitgeist
//**********************************************************************	 
    gsl_vector_set_zero(Z);  
    gera_config(Z,issue,P,L,r,1.0);
     
    if (ORTOGONALFLAG==1) gsl_matrix_set_identity(issue);
   	 
    for (ib=0;ib<P;ib++)
    {
	gsl_matrix_get_row(current_issue,issue,ib);
	gsl_blas_ddot(current_issue,current_issue,&Q1);
	gsl_vector_scale(current_issue,1/sqrt(Q1));	
	gsl_vector_add(Z,current_issue);
     }
     gsl_blas_ddot(Z,Z,&QZ);
     gsl_vector_scale(Z,1/sqrt(QZ));			  
	
//**********************************************************************
// Ground state energy
//**********************************************************************
     double E0; 	
     gera_config(Z,sociedade,SIZE,L,r,0);  									
     E0 = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph);
    
     double EMIN=E0;	
     double EMAX=-E0; 	
     double E_old=E0;
     		
     gsl_histogram_set_ranges_uniform (HE,EMIN,EMAX);
     gsl_histogram_set_ranges_uniform (logG,EMIN,EMAX); 
       
     if (SHOWFLAG==1) printf("# ground state: %3.0f\n",E0);
     fprintf(logfile,"# ground state: %3.0f\n",E0); 

//**********************************************************************		      	
//  Find sampling interval
//**********************************************************************    
     //printf("#finding the sampling interval...\n");

     lf=1;
     sweep=0;
     icont=0;	
     int iflag=0;
     int TMAX=NSWEEPS;	

     while(sweep<=TMAX){
	if (icont==10000) {
			//printf("%d sweeps\n",sweep);
			icont=0;
	}	
	for(it=0;it<SIZE;it++){

                        igraph_vector_init(&neighbors,SIZE);
                        
                        //choose a  random site
                        do{
              		 	inow=gsl_rng_uniform_int(r,SIZE);
			 }while((inow<0)||(inow>=SIZE)); 
	      		 gsl_matrix_get_row(v1,sociedade,inow);
	      		 igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT); 
		
	      		 //generates  a random vector  v1
	      		 gsl_vector_memcpy(v0,v1);	
			 gera_vetor(v1,L,r);

			 //calculates energy change when v0->v1
			 // in site inow
			 DeltaE=variacaoE(v0,v1,inow,sociedade,
					  issue,N,L,P,DELTA,graph,neighbors);	      		
			 E_new=E_old+DeltaE;
			
			 //WL: accepts in [EMIN,EMAX]
			 if ((E_new>EMIN) && (E_new<EMAX))
	      		 {
		   		gsl_histogram_find(logG,E_old,&i_old);
		   		logG_old=gsl_histogram_get(logG,i_old);
		   		gsl_histogram_find(logG,E_new,&i_new);
		   		logG_new=gsl_histogram_get(logG,i_new); 	
		  		wE = GSL_MIN(exp(logG_old-logG_new),1);		
		   		if (gsl_rng_uniform(r)<wE){
					E_old=E_new;
					gsl_matrix_set_row(sociedade,inow,v1);
		   		}
	      		 }
			 //WL: update histograms
			 gsl_histogram_increment(HE,E_old); 
	     		 gsl_histogram_accumulate(logG,E_old,lf); 
                         igraph_vector_destroy(&neighbors);
		 }	
		sweep++;
		icont++;	
     }		 
     	
     gsl_histogram_fprintf(wlsrange,HE,"%g","%g");
    
     double maxH=gsl_histogram_max_val(HE);
     	
     //printf("ok\n");
     Ex=0;
     hvalue=maxH;		
     while((hvalue>TOL*maxH)&&(Ex>EMIN)){
	gsl_histogram_find(HE,Ex,&i0);
	hvalue=gsl_histogram_get(HE,i0);
	Ex-=1;
	if(Ex<=EMAX)TMAX+=10000;
     }		
     EMIN=Ex;
	
     Ex=0;	
     hvalue=maxH;	
     while((hvalue>TOL*maxH)&&(Ex<EMAX)) {
	gsl_histogram_find(HE,Ex,&i0);
	hvalue=gsl_histogram_get(HE,i0);
	Ex+=1;
	if(Ex>=EMAX)TMAX+=10000;
     }		
     EMAX=Ex;	   
     EMAX=GSL_MIN(10.0,Ex);
     if (SHOWFLAG==1) 
       printf("# the sampling interval is [%3.0f,%3.0f] found in %d sweeps \n\n"
	                                                      ,EMIN,EMAX,sweep);

     fprintf(logfile,
	"# the sampling interval is [%3.0f,%3.0f] found in %d sweeps \n\n"
	                                                      ,EMIN,EMAX,sweep);
     
     gsl_histogram_set_ranges_uniform (HE,EMIN-1,EMAX+1);
     gsl_histogram_set_ranges_uniform (logG,EMIN-1,EMAX+1); 
     gsl_histogram_set_ranges_uniform (LG,EMIN-1,EMAX+1); 		
     
//**********************************************************************		      	
// WLS
//**********************************************************************		
     int iE,itera=0;
     double endpoints[NBINS];		
     double w = WINDOW; //(EMAX-EMIN)/10.0;	
     //printf("W=%f\n",w);	
     lf=1;

//RESOLUTION ---->                                <------RESOLUTION*****            
    do{
	int iverify=0,iborda=0,flat=0; 
	sweep=0;
	Ex=EMAX; 
	EW=EMAX;
	E_old=EMAX+1;
	iE=0;
	endpoints[iE]=EMAX;
	iE++;
	gsl_histogram_reset(LG);		

//WINDOWS -->                                          <--WINDOWS*******           
	while((Ex>EMIN)&&(sweep<MAXSWEEPS)){	 
	   //initial config
	   gera_config(Z,sociedade,SIZE,L,r,0);
           E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph);
	   while( (E_old<EMIN+1)||(E_old>Ex) ){
		//printf("%d %3.1f\n",E_old);

		do{
              		inow=gsl_rng_uniform_int(r,SIZE);
		  }while((inow<0)||(inow>=SIZE)); 	
                gsl_matrix_get_row(v0,sociedade,inow);
	   	gera_vetor(v1,L,r); 	
		gsl_matrix_set_row(sociedade,inow,v1);					
                E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph);
                if (E_old>Ex){
                    gsl_matrix_set_row(sociedade,inow,v0);
                    E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph);
                }
                //printf("%3.1f %3.1f %3.1f\n",EMIN+1,E_old, Ex);
	   }
	   
	   if (SHOWFLAG==1){
		printf("# sampling [%f,%f]\n",EMIN,Ex);
	   	printf("# walking from E=%3.0f\n",E_old);
	   }
	   
	   fprintf(logfile,"# sampling [%f,%f]\n",EMIN,Ex);
	   fprintf(logfile,"# walking from E=%3.0f\n",E_old);

	   do{	//FLAT WINDOW------>                 <------FLAT WINDOW*****
//MC sweep ---->                                 <------MC sweep********	
		
	    	for(it=0;it<SIZE;it++){
                         igraph_vector_init(&neighbors,SIZE);
			 //escolhe sítio aleatoriamente
			 do{
              		 	inow=gsl_rng_uniform_int(r,SIZE);
			 }while((inow<0)||(inow>=SIZE)); 
	      		 gsl_matrix_get_row(v1,sociedade,inow);
	      		 igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT); 
		
	      		 //gera vetor aleatorio v1
	      		 gsl_vector_memcpy(v0,v1);	
			 gera_vetor(v1,L,r);

			 //calculates energy change when
			 //v0->v1 in site inow
			 DeltaE=variacaoE(v0,v1,inow,sociedade,issue,
					  N,L,P,DELTA,graph,neighbors);	      		
			 E_new=E_old+DeltaE;
			
			 //WL: accepts in [EMIN,Ex]
			 if ((E_new>EMIN) && (E_new<Ex))
	      		 {
		   		gsl_histogram_find(logG,E_old,&i_old);
		   		logG_old=gsl_histogram_get(logG,i_old);
		    		gsl_histogram_find(logG,E_new,&i_new);
		   		logG_new=gsl_histogram_get(logG,i_new); 	
		  		wE = GSL_MIN(exp(logG_old-logG_new),1);		
		   		if (gsl_rng_uniform(r)<wE){
					E_old=E_new;
					gsl_matrix_set_row(sociedade,inow,v1);
		   		}
	      		 }
			 //WL: updates histograms
			 gsl_histogram_increment(HE,E_old); 
	     		 gsl_histogram_accumulate(logG,E_old,lf); 
			 itera++;
                         igraph_vector_destroy(&neighbors);
		 }
//MC sweep ---->                                   <--------MC sweep**** 
		sweep++; iverify++;   

		if( (EMAX-EMIN)<NDE*DE ) {
			EW=EMIN;
		}else{	    
	   		EW=GSL_MAX(Ex-w,EMIN);
		}	

	   	if (iverify==CHECK){//Verify flatness 
			if (SHOWFLAG==1)  
			    printf(" #verificando flatness em [%f,%f]\n",EW,Ex);
	
			fprintf(logfile," #verificando flatness em [%f,%f]\n"
				                                        ,EW,Ex);
	   		iverify=0;
			flat=flatness(HE,EW,Ex,TOL,itera,meanhist,hvalue);
			if (SHOWFLAG==1) 
			    printf("#minH= %8.0f\t k<H>=%8.0f\t %d sweeps\t ",
				                hvalue,TOL*meanhist,sweep,flat);

			fprintf(logfile,
				"#minH= %8.0f\t k<H>=%8.0f\t %d sweeps\t ",
			                        hvalue,TOL*meanhist,sweep,flat);
	   	}

	    }while(flat==0);//                      <------FLAT WINDOW******	 	  
            flat=0;
  
	    //Find ER
            //printf("# EMAX=%f EMIN = %f Ex =%f\n",EMAX, EMIN, Ex);
	    if( (EMAX-EMIN)<NDE*DE ) {
		Ex=EMIN;
		endpoints[iE]=EMIN;
	    } 
	    else {		
	    	if (EW>EMIN){
			 ER=flatwindow(HE,EW,TOL,meanhist);
			 if (SHOWFLAG==1)  
			      printf("# extending flatness to[%f,%f]\n",ER,Ex);

			 fprintf(logfile,
				"# extending flatness to [%f,%f]\n",ER,Ex);

			 if((ER-EMIN)<1){
				ER=EMIN;
				Ex=EMIN;
				endpoints[iE]=EMIN;
		 	}else{
		 		endpoints[iE]=GSL_MIN(ER+DE,EMAX);
				Ex=GSL_MIN(ER+2*DE,EMAX);
			}
	     	}
	     	else{
			 endpoints[iE]=EMIN;
			 Ex=EMIN;	
			 ER=EMIN;	   			
	    	} 	 	
	    }	    
	   
	    if (SHOWFLAG==1) 
		 printf("# window %d [%3.0f,%3.0f] is flat after %d sweeps \n",
					iE,endpoints[iE],endpoints[iE-1],sweep);

	  fprintf(logfile,"# window %d [%3.0f,%3.0f] is flat after %d sweeps\n",
			iE,endpoints[iE],endpoints[iE-1],sweep);	
	   		
	  	     
	    //saves histogram
	    if (iE==1){
		gsl_histogram_find(logG,endpoints[iE],&i1);
		gsl_histogram_find(logG,endpoints[iE-1],&i2);
		for(i0=i1;i0<=i2;i0++){
			lGv=gsl_histogram_get(logG,i0);
			gsl_histogram_get_range(logG,i0,&lower,&upper);
			E=0.5*(upper+lower);
			gsl_histogram_accumulate(LG,E,lGv);
		}				
	    }else{
		gsl_histogram_find(logG,endpoints[iE],&i1);
		gsl_histogram_find(logG,endpoints[iE-1],&i2);
		lGv=gsl_histogram_get(logG,i2);
		lGvR=gsl_histogram_get(LG,i2);
		DlG=lGvR-lGv;
	  //printf("i1=%d i2=%d lGv=%f lGvR=%f DlG=%f\n",i1,i2,lGv,lGvR,DlG);
		for(i0=i1;i0<i2;i0++){
			lGv=gsl_histogram_get(logG,i0);
			lGv=lGv+DlG;
			gsl_histogram_get_range(logG,i0,&lower,&upper);
			E=(upper+lower)*0.5;
			//printf("i0=%d E=%f lGv=%f\n",i0,E,lGv);
			gsl_histogram_accumulate(LG,E,lGv);
		}		
	    }	
				 
	    //printf("#########################################\n");		
	    //gsl_histogram_fprintf(stdout,LG,"%g","%g");		
	    //printf("#########################################\n");			

	    iE++;
            if((Ex-EMIN)>NDE*DE) {
		if (SHOWFLAG==1) 
		     printf("# random walk is now restricted to [%3.0f,%3.0f]\n"
			    					      ,EMIN,Ex);
	    fprintf(logfile,"# random walk is now restricted to [%3.0f,%3.0f]\n"
			                                              ,EMIN,Ex);
	    }
	    gsl_histogram_reset(HE);
	   		     	 		
      }  
//WINDOWS -->

     if(sweep<MAXSWEEPS){	
     	if (SHOWFLAG==1) 
		  printf("# log(f)=%f converged within %d sweeps\n\n",lf,sweep);	
	fprintf(logfile,"# log(f)=%f converged within %d sweeps\n\n",lf,sweep);		 	
     	lf=lf/2.0;	 
     	gsl_histogram_reset(HE);
     	gsl_histogram_memcpy(logG,LG);
     }else {
	if (SHOWFLAG==1) 
		printf("# FAILED: no convergence has been attained.");
	fprintf(logfile,
           "# FAILED: no convergence has been attained. Simulation ABANDONED.");
	return(1);
     }	 			
	     
    }while(lf>MINLOGF); 
//RESOLUTION -->                                    <-----RESOLUTION****

     //***************************************************************** 	
     //Density of states	     	
     //*****************************************************************
     double minlogG=gsl_histogram_min_val(logG);	
     gsl_histogram_shift(logG,-minlogG);	
     gsl_histogram_fprintf(dos,logG,"%g","%g");	

     //***************************************************************** 
     //Thermodynamics    	
     //***************************************************************** 
     double beta,A,wT,Zmin_beta;
     double lGvalue,maxA,betaC,CTMAX=0;	
     double Z_beta,U,U2,CT,F,S;
     
     for (beta=0.01;beta<=30;beta+=0.01)
     {
	//****************************************************************** 	
	//Energy, free-energy, entropy, specific heat and Tc
 	//****************************************************************** 
	maxA=0;
	for (ia2=0; ia2<NBINS;ia2++)
	{
		lGvalue=gsl_histogram_get(logG,ia2);
		gsl_histogram_get_range(logG,ia2,&lower,&upper);
		E=(lower+upper)/2;
		A=lGvalue-beta*E;
		if (A>maxA) maxA=A;
	}       

        gsl_histogram_find(logG,EMIN,&i0); 

	Z_beta=0;U=0;U2=0;
	for (ia2=0; ia2<NBINS;ia2++)
	{
			lGvalue=gsl_histogram_get(logG,ia2);
			gsl_histogram_get_range(logG,ia2,&lower,&upper);
			E=(lower+upper)/2;
			A=lGvalue-beta*E-maxA;
			Z_beta+=exp(A);  
			U+=E*exp(A);
			U2+=E*E*exp(A);  
			if(ia2==i0) Zmin_beta=exp(A);
	}	
	wT=Zmin_beta/Z_beta;

	F=-log(Z_beta)/beta - maxA/beta; 
	U=U/Z_beta;
	S= (U-F)*beta;
	U2=U2/Z_beta;
	CT=(U2-U*U)*beta*beta;	
			
	if(CT>CTMAX){
		CTMAX=CT;
		betaC=beta;
	}

	fprintf(thermodynamics,"%f %f %f %f %f %f %f \n"
		,beta,1/beta,F/(double)(SIZE),S/(double)(SIZE),
		 U/(double)(SIZE),CT/(double)(SIZE),wT);
     }
     
     if (SHOWFLAG==1) printf("# BETAc: %f  Tc:%f \n",betaC,1/betaC); 
     fprintf(logfile,"# BETAc: %f  Tc:%f \n",betaC,1/betaC); 	
		
    //******************************************************************	
    //canonical distribuition at Tc
    //******************************************************************	
     beta=betaC; 
     double distr_canonica;	
     
      maxA=0;
      for (ia2=0; ia2<NBINS;ia2++)
      {
		lGvalue=gsl_histogram_get(logG,ia2);
		gsl_histogram_get_range(logG,ia2,&lower,&upper);
		E=(lower+upper)/2;
		A=lGvalue-beta*E;
		if (A>maxA) maxA=A;
      }       

      for (ia2=0; ia2<NBINS;ia2++)
      {
	  lGvalue=gsl_histogram_get(logG,ia2);
	  gsl_histogram_get_range(logG,ia2,&lower,&upper);
	  E=(lower+upper)/2;
	  A=lGvalue-beta*E-maxA;
	  distr_canonica=exp(A);
	  fprintf(canonical,"%f %f %f\n",
		  E/(double)(SIZE),distr_canonica,A);  
      }			

     //*****************************************************************
     // Finalization                                                    
     //*****************************************************************
     igraph_destroy(&graph);
     igraph_vector_destroy(&neighbors);
     igraph_vector_destroy(&result);  
     gsl_matrix_free(issue);
     gsl_vector_free(current_issue);
     gsl_vector_free(v1);
     gsl_vector_free(v0);
     gsl_matrix_free(sociedade);     	   	
     gsl_rng_free(r);
	
     fclose(wlsrange);
     fclose(dos);
     fclose(thermodynamics);
     fclose(canonical);   
     fclose(logfile);   	
   
     return(0);
}
コード例 #11
0
ファイル: wanglandau.c プロジェクト: mtw/RNAwl
/* ==== */
static void
wl_montecarlo(char *struc)
{
  short *pt=NULL;
  move_str m;
  int e,enew,emove,eval_me,status,debug=1;
  long int crosscheck=1000000; /* used for convergence checks */
  long int crosscheck_limit = 100000000000000000;
  double g_b1,g_b2,prob,lnf = 1.;  /* log modification parameter f */
  size_t b1,b2;                    /* indices in g/h corresponding to
				      old/new energies */
  gsl_histogram *gcp=NULL; /* clone of g used during crosscheck output */ 

  eval_me = 1; /* paranoid checking of neighbors against RNAeval */
  if (wanglandau_opt.verbose){
    printf("[[wl_montecarlo()]]\n");
  }
  pt = vrna_pt_get(struc);
  //mtw_dump_pt(pt);
  //char *str = vrna_pt_to_db(pt);
  //printf(">%s<\n",str);
  e = vrna_eval_structure_pt(wanglandau_opt.sequence,pt,P);
  
  /* determine bin where the start structure goes */
  status = gsl_histogram_find(g,(float)e/100,&b1);
  if (status) {
    if (status == GSL_EDOM){
      printf ("error: %s\n", gsl_strerror (status));
    }
    else {fprintf(stderr, "GSL error: gsl_errno=%d\n",status);}
    exit(EXIT_FAILURE);
  }
  printf("%s\n", wanglandau_opt.sequence);
  print_str(stderr,pt);
  printf(" (%6.2f) bin:%d\n",(float)e/100,b1);
  if (wanglandau_opt.verbose){
    fprintf(stderr,"\nStarting MC loop ...\n");
  }
  while (lnf > wanglandau_opt.ffinal) {
    if(wanglandau_opt.debug){
      fprintf(stderr,"\n==================\n");
      fprintf(stderr,"in while: lnf=%8.6f\n",lnf);
      fprintf(stderr,"steps: %d\n",steps);
      fprintf(stderr,"current histogram g:\n");
      gsl_histogram_fprintf(stderr,g,"%6.2f","%30.6f");
      fprintf(stderr,"\n");
      print_str(stderr,pt);
      fprintf(stderr, " (%6.2f) bin:%d\n",(float)e/100,b1);
      /*  mtw_dump_pt(pt); */
    }
    /* make a random move */
    m = get_random_move_pt(wanglandau_opt.sequence,pt);
    /* compute energy difference for this move */
    emove = vrna_eval_move_pt(pt,s0,s1,m.left,m.right,P);
    /* evaluate energy of the new structure */
    enew = e + emove;
    if(wanglandau_opt.debug){
      fprintf(stderr,
	      "random move: left %i right %i enew(%6.4f)=e(%6.4f)+emove(%6.4f)\n",
	      m.left,m.right,(float)enew/100,(float)e/100,(float)emove/100);
    }

    /* ensure the new energy is within sampling range */
    if ((float)enew/100 >= wanglandau_opt.max){
      fprintf(stderr,
	      "New structure has energy %6.2f >= %6.2f (upper energy bound)\n",
	      (float)enew/100,wanglandau_opt.max);
      fprintf(stderr,"Please increase --bins or adjust --max! Exiting ...\n");
      exit(EXIT_FAILURE);
    }
    /* determine bin where the new structure goes */
    status = gsl_histogram_find(g,(float)enew/100,&b2);
    if (status) {
      if (status == GSL_EDOM){
	printf ("error: %s\n", gsl_strerror (status));
      }
      else {fprintf(stderr, "GSL error: gsl_errno=%d\n",status);}
      exit(EXIT_FAILURE);
    }

    steps++;  /* # of MC steps performed so far */

    /* lookup current values for bins b1 and b2 */
    g_b1 = gsl_histogram_get(g,b1);
    g_b2 = gsl_histogram_get(g,b2);
      
    /* core MC steps */
    prob = MIN2(exp(g_b1 - g_b2), 1.0);
    rnum =  gsl_rng_uniform (r);
    
    if ((prob == 1 || (rnum <= prob)) ) { /* accept & apply the move */
      apply_move_pt(pt,m);
      if(wanglandau_opt.debug){
	print_str(stderr,pt);
	fprintf(stderr, " %6.2f bin:%d [A]\n", (float)enew/100,b2);
      }
      b1 = b2;
      e = enew;
    }
    else { /* reject the move */
      if(wanglandau_opt.debug){
	print_str(stderr,pt);
	fprintf(stderr, " (%6.2f) bin:%d [R]\n", (float)enew/100,b2);
       }
    }
    
    /* update histograms g and h */
    if(wanglandau_opt.truedosbins_given && b2 <= wanglandau_opt.truedosbins){
      /* do not update if b2 <= truedosbins, i.e. keep true DOS values
	 in those bins */
      if (wanglandau_opt.debug){
	fprintf(stderr, "NOT UPDATING bin %d\n",b1);
      }
    } else{
      if(wanglandau_opt.debug){
	fprintf(stderr, "UPDATING bin %d\n",b1); 
      }
      status = gsl_histogram_increment(h,(float)e/100);
      status = gsl_histogram_accumulate(g,(float)e/100,lnf);
    }
    maxbin = MAX2(maxbin,(int)b1);
   
    // stuff that can be skipped 
    /*
      printf ("performed move l:%4d r:%4d\t Energy +/- %6.2f\n",m.left,m.right,(float)emove/100);
      print_str(stderr,pt);printf(" %6.2f bin:%d\n",(float)enew/100,b2);
      e = vrna_eval_structure_pt(wanglandau_opt.sequence,pt,P);
      if (eval_me == 1 && e != enew){
      fprintf(stderr, "energy evaluation against vrna_eval_structure_pt() mismatch... HAVE %6.2f != %6.2f (SHOULD BE)\n",(float)enew/100, (float)e/100);
      exit(EXIT_FAILURE);
      }
      print_str(stderr,pt);printf(" %6.2f\n",(float)e/100);
    */
    // end of stuff that can be skipped

    /* output DoS every x*10^(1/4) steps, starting with x=10^6 (we
       used this fopr comparing perfomance and convergence of
       different DoS sampling methods */
    if((steps % crosscheck == 0) && (crosscheck <= crosscheck_limit)){
      fprintf(stderr,"# crosscheck reached %li steps ",crosscheck);
      gcp = gsl_histogram_clone(g);
      if(wanglandau_opt.verbose){
	fprintf(stderr,"## gcp before scaling\n");
	gsl_histogram_fprintf(stderr,gcp,"%6.2f","%30.6f");
      }
      scale_dos(gcp); /* scale estimated g; make ln(g[0])=0 */
      if(wanglandau_opt.verbose){
	fprintf(stderr,"## gcp after scaling\n");
	gsl_histogram_fprintf(stderr,gcp,"%6.2f","%30.6f");
      }
      double Z = partition_function(gcp);
      output_dos(gcp,'s');
      fprintf(stderr, "Z=%10.4g\n", Z);
      crosscheck *= (pow(10, 1.0/4.0));
      gsl_histogram_free(gcp);
      fprintf(stderr,"->  new crosscheck will be performed at %li steps\n", crosscheck);
    }
    
    if(steps % wanglandau_opt.checksteps == 0) {
      if( histogram_is_flat(h) ) {
	lnf /= 2;
	fprintf(stderr,"# steps=%20li | f=%12g | histogram is FLAT\n",
		steps,lnf);
	gsl_histogram_reset(h);
      }
      else {
	fprintf(stderr, "# steps=%20li | f=%12g | histogram is NOT FLAT\n",
		steps,lnf);
      }
      output_dos(g,'l');
    }
    
    /* stop criterion */
    if(steps % wanglandau_opt.steplimit == 0){
      fprintf(stderr,"maximun number of MC steps (%li) reached, exiting ...",
	      wanglandau_opt.steplimit);
      break;
    }

  } /* end while */
  free(pt); 
  return;
}