void QwtHistogram::loadDataFromMatrix() { if (!d_matrix) return; int size = d_matrix->numRows() * d_matrix->numCols(); const double *data = d_matrix->matrixModel()->dataVector(); int n; gsl_histogram *h; if (d_autoBin) { double min, max; d_matrix->range(&min, &max); d_begin = floor(min); d_end = ceil(max); d_bin_size = 1.0; n = static_cast<int>(floor((d_end - d_begin) / d_bin_size)); if (!n) return; h = gsl_histogram_alloc(n); if (!h) return; gsl_histogram_set_ranges_uniform(h, floor(min), ceil(max)); } else { n = static_cast<int>((d_end - d_begin) / d_bin_size + 1); if (!n) return; h = gsl_histogram_alloc(n); if (!h) return; double *range = new double[n + 2]; for (int i = 0; i <= n + 1; i++) range[i] = d_begin + i * d_bin_size; gsl_histogram_set_ranges(h, range, n + 1); delete[] range; } for (int i = 0; i < size; i++) gsl_histogram_increment(h, data[i]); QVarLengthArray<double> X(n), Y(n); // stores ranges (x) and bins (y) for (int i = 0; i < n; i++) { Y[i] = gsl_histogram_get(h, i); double lower, upper; gsl_histogram_get_range(h, i, &lower, &upper); X[i] = lower; } setData(X.data(), Y.data(), n); d_mean = gsl_histogram_mean(h); d_standard_deviation = gsl_histogram_sigma(h); d_min = gsl_histogram_min_val(h); d_max = gsl_histogram_max_val(h); gsl_histogram_free(h); }
void QwtHistogram::initData(double *Y, int size) { if(size<2 || (size == 2 && Y[0] == Y[1])) {//non valid histogram data double x[2], y[2]; for (int i = 0; i<2; i++ ){ y[i] = 0; x[i] = 0; } setData(x, y, 2); return; } int n = 10;//default value QVarLengthArray<double> x(n), y(n);//double x[n], y[n]; //store ranges (x) and bins (y) gsl_histogram * h = gsl_histogram_alloc (n); if (!h) return; gsl_vector *v; v = gsl_vector_alloc (size); for (int i = 0; i<size; i++ ) gsl_vector_set (v, i, Y[i]); double min, max; gsl_vector_minmax (v, &min, &max); gsl_vector_free (v); d_begin = floor(min); d_end = ceil(max); gsl_histogram_set_ranges_uniform (h, floor(min), ceil(max)); for (int i = 0; i<size; i++ ) gsl_histogram_increment (h, Y[i]); for (int i = 0; i<n; i++ ){ y[i] = gsl_histogram_get (h, i); double lower, upper; gsl_histogram_get_range (h, i, &lower, &upper); x[i] = lower; } setData(x.data(), y.data(), n);//setData(x, y, n); d_bin_size = (d_end - d_begin)/(double)n; d_autoBin = true; d_mean = gsl_histogram_mean(h); d_standard_deviation = gsl_histogram_sigma(h); d_min = gsl_histogram_min_val(h); d_max = gsl_histogram_max_val(h); gsl_histogram_free (h); }
/* ==== */ static void output_dos(const gsl_histogram *x, const char T) { int i,fnlen; FILE *dos_fp=NULL; char *dos_fn=NULL, *lDoS_suffix="lDoS", *sDoS_suffix="sDoS"; char s[50]; double val,lo,hi; sprintf(s,"%li",steps); fnlen = strlen(out_prefix)+strlen(lDoS_suffix)+64; dos_fn = (char*)calloc(fnlen,sizeof(char)); strcpy(dos_fn, out_prefix); strcat(dos_fn, s); strcat(dos_fn,"."); switch (T){ case 'l': /* output logarithmic g, usually during the calculation */ strcat(dos_fn, lDoS_suffix); break; case 's': /* output scaled g, eg for in-process convergence checks */ strcat(dos_fn, sDoS_suffix); break; default: fprintf (stderr, "%s:%d output_dos(): No handler for type %c", __FILE__, __LINE__, T); exit(EXIT_FAILURE); } dos_fp = fopen(dos_fn, "w+"); fprintf(dos_fp, "# estimated DOS after %li steps\n",steps); fprintf(dos_fp, "# sampling range: %6.2f -- %6.2f\n", gsl_histogram_min(g),gsl_histogram_max(g)); fprintf(dos_fp, "# bin resolution: %g\n",wanglandau_opt.res); /* loop over histogram g */ for (i=0;i<=maxbin;i++){ val = gsl_histogram_get(x,i); if (val == 0.){continue;} gsl_histogram_get_range(x,i,&lo,&hi); fprintf(dos_fp,"%6.2f\t%20.6f\n",lo+(hi-lo)/2,val); } fclose(dos_fp); free(dos_fn); return; }
QVector<QPointF> DistanceToAtom::histogram(int bins) { QVector<QPointF> histogramVector; if(!m_isValid) { qFatal("DistanceToAtom is not valid. Run compute() first."); exit(1); } float minValue = 1e90; float maxValue = 0; for(const float &val : m_values) { if(val >= 0) { minValue = std::min(minValue, (float)sqrt(val)); maxValue = std::max(maxValue, (float)sqrt(val)); } } gsl_histogram *hist = gsl_histogram_alloc (bins); gsl_histogram_set_ranges_uniform (hist, minValue, maxValue); for(const float &value : m_values) { if(value >= 0) { gsl_histogram_increment (hist, sqrt(value)); } } histogramVector.resize(bins); for(int i=0; i<bins; i++) { double upper, lower; gsl_histogram_get_range(hist, i, &lower, &upper); float middle = 0.5*(upper+lower); histogramVector[i].setX(middle); histogramVector[i].setY(gsl_histogram_get(hist,i)); } gsl_histogram_free (hist); return histogramVector; }
bool FrequencyCountDialog::apply() { if (!d_col_values){ QMessageBox::critical(this, tr("QtiPlot - Error"), tr("Not enough data points, operation aborted!")); return false; } double from = boxStart->value(); double to = boxEnd->value(); if (from >= to){ QMessageBox::critical(this, tr("QtiPlot - Frequency input error"), tr("Please enter frequency limits that satisfy: From < To !")); boxEnd->setFocus(); return false; } int old_bins = d_bins; double bin_size = boxStep->value(); d_bins = int((to - from)/bin_size + 1); if (!d_bins) return false; ApplicationWindow *app = (ApplicationWindow *)parent(); if (!app) return false; if (!d_result_table){ d_result_table = app->newTable(30, 4, app->generateUniqueName(tr("Count"), true), tr("Frequency count of %1").arg(d_col_name)); d_result_table->setColName(0, tr("BinCtr")); d_result_table->setColName(1, tr("Count")); d_result_table->setColName(2, tr("BinEnd")); d_result_table->setColName(3, tr("Sum")); d_result_table->showMaximized(); } gsl_histogram *h = gsl_histogram_alloc(d_bins); if (!h) return false; double *range = (double *) malloc((d_bins + 2)*sizeof(double)); if (!range) return false; for (int i = 0; i <= d_bins + 1; i++) range[i] = from + i*bin_size; gsl_histogram_set_ranges (h, range, d_bins + 1); free(range); int dataSize = d_col_values->size; for (int i = 0; i < dataSize; i++ ) gsl_histogram_increment (h, gsl_vector_get(d_col_values, i)); if (d_bins > d_result_table->numRows()) d_result_table->setNumRows(d_bins); for(int i = d_bins; i < old_bins; i++){ d_result_table->setText(i, 0, ""); d_result_table->setText(i, 1, ""); d_result_table->setText(i, 2, ""); d_result_table->setText(i, 3, ""); } double sum = 0.0; for (int i = 0; i<d_bins; i++ ){ double aux = gsl_histogram_get (h, i); sum += aux; double lower, upper; gsl_histogram_get_range (h, i, &lower, &upper); d_result_table->setCell(i, 0, 0.5*(lower + upper)); d_result_table->setCell(i, 1, aux); d_result_table->setCell(i, 2, upper); d_result_table->setCell(i, 3, sum); } return true; }
void compute_global_autocorrelations(struct State *S) /* Compute population rate autocorrelation */ { struct Network *ntw = &S->ntw; int n_neurons_sample = 100; const size_t n_bins = 201; const double max_lag = 100; /* maximal lag in ms */ double bin_width = 2 * max_lag / (double) n_bins; /* 2*max_lag */ char filename[100]; gsl_histogram *h = gsl_histogram_alloc(n_bins); gsl_histogram_set_ranges_uniform(h, -max_lag, max_lag); FILE *f; struct Dynamic_Array poptrain; size_t num_spikes_total = 0; for (int i = 0; i < n_neurons_sample; i++) num_spikes_total += ntw->cell[i].spike_train.n; poptrain.data = emalloc(num_spikes_total * sizeof(double)); poptrain.n = 0; poptrain.size = num_spikes_total; report("Computing global (population rate) autocorrelation...\n"); fill_population_spike_train(S, &poptrain, n_neurons_sample); gsl_sort(poptrain.data, 1, num_spikes_total); sprintf(filename, "global_autocorrelation_%s", S->sim.suffix); double t_sp; f = fopen(filename, "w"); size_t l = 0; size_t r = 0; for (size_t j = 0; j < poptrain.n; j++) { t_sp = poptrain.data[j]; /* look for left index */ while (l < poptrain.n && poptrain.data[l] < t_sp - max_lag) l++; /* look for right index */ while (r < poptrain.n && poptrain.data[r] < t_sp + max_lag) r++; /* And feed the histogram with the relevant spikes */ for (size_t k = l; k < r; k++) gsl_histogram_increment(h, t_sp - poptrain.data[k]); } /* gsl_histogram_fprintf(f, h, "%g", "%g"); */ /* correct for boundary effects and substract mean */ double w; double T = S->sim.total_time - S->sim.offset; /* double nu = num_spikes_total / (T * (double) n_neurons_sample); */ double lower, upper, ac; int status; for (size_t j = 0; j < n_bins; j++) { w = T - fabs( (n_bins - 1.0) / 2.0 - j ) * bin_width; ac = gsl_histogram_get(h, j) / (w * n_neurons_sample); ac -= (pow(num_spikes_total / (T * n_neurons_sample), 2) * bin_width); /* ac /= pow(nu * bin_width, 2); [> Normalization <] */ status = gsl_histogram_get_range(h, j, &lower, &upper); if (status==0) { fprintf(f, "% 9.4f % 9.4f % 9.6f\n", lower, upper, ac); } else { report("Somehting wrong here.\n"); exit(2); } } fclose(f); free(poptrain.data); gsl_histogram_free(h); }
void compute_average_autocorrelations(struct State *S) /* Compute spike-time autocorrelation */ { struct Network *ntw = &S->ntw; struct Dynamic_Array *nrn_train; int n_neurons_sample = 1000; size_t num_spikes_total = 0; const size_t n_bins = 201; const double max_lag = 50; /* in ms */ double bin_width = 2 * max_lag / (double) n_bins; char filename[100]; gsl_histogram *h = gsl_histogram_alloc(n_bins); gsl_histogram_set_ranges_uniform(h, -max_lag, max_lag); FILE *f; report("Computing average autocorrelation...\n"); sprintf(filename, "autocorrelation_%s", S->sim.suffix); double t_sp; size_t l, r; f = fopen(filename, "w"); for (int i = 0; i < n_neurons_sample; i++) { l = 0; r = 0; nrn_train = &ntw->cell[i].spike_train; num_spikes_total += nrn_train->n; for (size_t j = 0; j < nrn_train->n; j++) { t_sp = nrn_train->data[j]; /* look for left index */ while (l < nrn_train->n && nrn_train->data[l] < t_sp - max_lag) l++; /* look for right index */ while (r < nrn_train->n && nrn_train->data[r] < t_sp + max_lag) r++; /* And feed the histogram with the relevant spikes */ for (size_t k = l; k < r; k++) gsl_histogram_increment(h, t_sp - nrn_train->data[k]); } } /* gsl_histogram_fprintf(f, h, "%g", "%g"); */ /* correct for boundary effects and substract mean */ double w; double T = S->sim.total_time - S->sim.offset; /* double nu = num_spikes_total / (T * (double) n_neurons_sample); */ double lower, upper, ac; int status; for (size_t j = 0; j < n_bins; j++) { w = T - fabs( (n_bins - 1.0) / 2.0 - j ) * bin_width; ac = gsl_histogram_get(h, j) / (w * n_neurons_sample); ac -= (pow(num_spikes_total / (T * n_neurons_sample), 2) * bin_width); /* ac /= pow(nu * bin_width, 2); [> Normalization <] */ status = gsl_histogram_get_range(h, j, &lower, &upper); if (status==0) { fprintf(f, "% 9.4f % 9.4f % 9.7f\n", lower, upper, ac); } else { report("Something wrong here.\n"); exit(2); } } fclose(f); gsl_histogram_free(h); }
void test1d_resample (void) { size_t i; int status = 0; gsl_histogram *h; gsl_ieee_env_setup (); h = gsl_histogram_calloc_uniform (10, 0.0, 1.0); gsl_histogram_increment (h, 0.1); gsl_histogram_increment (h, 0.2); gsl_histogram_increment (h, 0.2); gsl_histogram_increment (h, 0.3); { gsl_histogram_pdf *p = gsl_histogram_pdf_alloc (10); gsl_histogram *hh = gsl_histogram_calloc_uniform (100, 0.0, 1.0); gsl_histogram_pdf_init (p, h); for (i = 0; i < 100000; i++) { double u = urand(); double x = gsl_histogram_pdf_sample (p, u); gsl_histogram_increment (hh, x); } for (i = 0; i < 100; i++) { double y = gsl_histogram_get (hh, i) / 2500; double x, xmax; size_t k; double ya; gsl_histogram_get_range (hh, i, &x, &xmax); gsl_histogram_find (h, x, &k); ya = gsl_histogram_get (h, k); if (ya == 0) { if (y != 0) { printf ("%d: %g vs %g\n", (int) i, y, ya); status = 1; } } else { double err = 1 / sqrt (gsl_histogram_get (hh, i)); double sigma = fabs ((y - ya) / (ya * err)); if (sigma > 3) { status = 1; printf ("%g vs %g err=%g sigma=%g\n", y, ya, err, sigma); } } } gsl_histogram_pdf_free (p) ; gsl_histogram_free (hh); gsl_test (status, "gsl_histogram_pdf_sample within statistical errors"); } gsl_histogram_free (h); }
void test_normal() { gen g_uni[] = {builtin, devurandom, kalkulator, mersenne, #if N <= (1 << 12) randorg, #endif 0}; test t[] = {chisq, kolsmir, 0}; task* prob = task_new(0, g_uni, t, 0 ); gsl_histogram* h = gsl_histogram_alloc(B); gsl_histogram_set_ranges_uniform(h, 0, 1); gsl_histogram_shift(h, AVG); prob->density = h; gsl_histogram2d* hd = gsl_histogram2d_alloc(B2, B2); gsl_histogram2d_set_ranges_uniform(hd, 0, 1, 0, 1); gsl_histogram2d_shift(hd, AVG2/2); prob->density2d = hd; prob->min = 0; prob->max = 1; task_perform(prob); task_free(prob); gen g_gauss[] = {gauss_bm, gauss_zig, gauss_ratio, 0}; prob = task_new(2, g_gauss, t, 0); prob->max = 3; prob->min = -prob->max; h = gsl_histogram_alloc(B); gsl_histogram_set_ranges_uniform(h, prob->min, prob->max); int j; for (j=0; j<B; ++j) { double min, max; gsl_histogram_get_range(h, j, &min, &max); double x = (max + min)/2; gsl_histogram_accumulate(h, x, N*gsl_ran_ugaussian_pdf(x) / B * (prob->max - prob->min)); } hd = gsl_histogram2d_alloc(B2, B2); gsl_histogram2d_set_ranges_uniform(hd, prob->min, prob->max, prob->min, prob->max); int k; for (j=0; j < B2; ++j) for (k=0; k<B2; ++k) { double min, max; gsl_histogram2d_get_xrange(hd, j, &min, &max); double x = (max + min)/2; gsl_histogram2d_get_yrange(hd, k, &min, &max); double y = (max + min)/2; gsl_histogram2d_accumulate(hd, x, y, gsl_ran_ugaussian_pdf(x) * gsl_ran_ugaussian_pdf(y)); } gsl_histogram2d_scale( hd, N/2/gsl_histogram2d_sum(hd) ); prob->density = h; prob->density2d = hd; task_perform(prob); task_free(prob); }
void QwtHistogram::loadData() { if (d_matrix){ loadDataFromMatrix(); return; } int r = abs(d_end_row - d_start_row) + 1; QVarLengthArray<double> Y(r); int ycol = d_table->colIndex(title().text()); int size = 0; for (int i = 0; i<r; i++ ){ QString yval = d_table->text(i, ycol); if (!yval.isEmpty()){ bool valid_data = true; Y[size] = ((Graph *)plot())->locale().toDouble(yval, &valid_data); if (valid_data) size++; } } if(size < 2 || (size==2 && Y[0] == Y[1])){//non valid histogram double X[2]; Y.resize(2); for (int i = 0; i<2; i++ ){ Y[i] = 0; X[i] = 0; } setData(X, Y.data(), 2); return; } int n; gsl_histogram *h; if (d_autoBin){ n = 10; h = gsl_histogram_alloc (n); if (!h) return; gsl_vector *v = gsl_vector_alloc (size); for (int i = 0; i<size; i++ ) gsl_vector_set (v, i, Y[i]); double min, max; gsl_vector_minmax (v, &min, &max); gsl_vector_free (v); d_begin = floor(min); d_end = ceil(max); d_bin_size = (d_end - d_begin)/(double)n; gsl_histogram_set_ranges_uniform (h, floor(min), ceil(max)); } else { n = int((d_end - d_begin)/d_bin_size + 1); h = gsl_histogram_alloc (n); if (!h) return; double *range = new double[n+2]; for (int i = 0; i<= n+1; i++ ) range[i] = d_begin + i*d_bin_size; gsl_histogram_set_ranges (h, range, n+1); delete[] range; } for (int i = 0; i<size; i++ ) gsl_histogram_increment (h, Y[i]); #ifdef Q_CC_MSVC QVarLengthArray<double> X(n); //stores ranges (x) and bins (y) #else double X[n]; //stores ranges (x) and bins (y) #endif Y.resize(n); for (int i = 0; i<n; i++ ){ Y[i] = gsl_histogram_get (h, i); double lower, upper; gsl_histogram_get_range (h, i, &lower, &upper); X[i] = lower; } #ifdef Q_CC_MSVC setData(X.data(), Y.data(), n); #else setData(X, Y.data(), n); #endif d_mean = gsl_histogram_mean(h); d_standard_deviation = gsl_histogram_sigma(h); d_min = gsl_histogram_min_val(h); d_max = gsl_histogram_max_val(h); gsl_histogram_free (h); }
void test1d_trap (void) { gsl_histogram *h; double result, lower, upper; size_t i; gsl_set_error_handler (&my_error_handler); gsl_ieee_env_setup (); status = 0; h = gsl_histogram_calloc (0); gsl_test (!status, "gsl_histogram_calloc traps zero-length histogram"); gsl_test (h != 0, "gsl_histogram_calloc returns NULL for zero-length histogram"); status = 0; h = gsl_histogram_calloc_uniform (0, 0.0, 1.0); gsl_test (!status, "gsl_histogram_calloc_uniform traps zero-length histogram"); gsl_test (h != 0, "gsl_histogram_calloc_uniform returns NULL for zero-length histogram"); status = 0; h = gsl_histogram_calloc_uniform (10, 1.0, 1.0); gsl_test (!status, "gsl_histogram_calloc_uniform traps equal endpoints"); gsl_test (h != 0, "gsl_histogram_calloc_uniform returns NULL for equal endpoints"); status = 0; h = gsl_histogram_calloc_uniform (10, 2.0, 1.0); gsl_test (!status, "gsl_histogram_calloc_uniform traps invalid range"); gsl_test (h != 0, "gsl_histogram_calloc_uniform returns NULL for invalid range"); h = gsl_histogram_calloc_uniform (N, 0.0, 1.0); status = gsl_histogram_accumulate (h, 1.0, 10.0); gsl_test (status != GSL_EDOM, "gsl_histogram_accumulate traps x at xmax"); status = gsl_histogram_accumulate (h, 2.0, 100.0); gsl_test (status != GSL_EDOM, "gsl_histogram_accumulate traps x above xmax"); status = gsl_histogram_accumulate (h, -1.0, 1000.0); gsl_test (status != GSL_EDOM, "gsl_histogram_accumulate traps x below xmin"); status = gsl_histogram_increment (h, 1.0); gsl_test (status != GSL_EDOM, "gsl_histogram_increment traps x at xmax"); status = gsl_histogram_increment (h, 2.0); gsl_test (status != GSL_EDOM, "gsl_histogram_increment traps x above xmax"); status = gsl_histogram_increment (h, -1.0); gsl_test (status != GSL_EDOM, "gsl_histogram_increment traps x below xmin"); result = gsl_histogram_get (h, N); gsl_test (result != 0, "gsl_histogram_get traps index at n"); result = gsl_histogram_get (h, N + 1); gsl_test (result != 0, "gsl_histogram_get traps index above n"); status = gsl_histogram_get_range (h, N, &lower, &upper); gsl_test (status != GSL_EDOM, "gsl_histogram_get_range traps index at n"); status = gsl_histogram_get_range (h, N + 1, &lower, &upper); gsl_test (status != GSL_EDOM, "gsl_histogram_get_range traps index above n"); status = 0; gsl_histogram_find (h, -0.01, &i); gsl_test (status != GSL_EDOM, "gsl_histogram_find traps x below xmin"); status = 0; gsl_histogram_find (h, 1.0, &i); gsl_test (status != GSL_EDOM, "gsl_histogram_find traps x at xmax"); status = 0; gsl_histogram_find (h, 1.1, &i); gsl_test (status != GSL_EDOM, "gsl_histogram_find traps x above xmax"); gsl_histogram_free (h); }
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); }
/* ==== */ static void initialize_wl(void) { int bins,fnlen=512; char *res_string=NULL; double low,high,lo,hi,hmin,hmax,erange,*range = NULL; srand(time(NULL)); if(wanglandau_opt.verbose){ printf("[[initialize_wl()]]\n"); } /* assign function pointers */ initialize_model = initialize_RNA; /* for RNA */ pre_process_model = pre_process_RNA; post_process_model = post_process_RNA; /* set energy paramters for current model; compute mfe */ initialize_model(wanglandau_opt.sequence); range = (double*)calloc((wanglandau_opt.bins+1), sizeof(double)); assert(range!=NULL); /* initialize histograms */ hmin=mfe; if(wanglandau_opt.res_given){ /* determine histogram ranges manually */ int i; range[0]=mfe; for(i=1;i<=wanglandau_opt.bins;i++){ range[i]=range[i-1]+wanglandau_opt.res; } if(wanglandau_opt.verbose){ /* info output */ fprintf(stderr,"#allocating %lu bins of width %g\n", wanglandau_opt.bins,wanglandau_opt.res); /* fprintf(stderr,"#histogram ranges:\n #"); for(i=0;i<=wanglandau_opt.bins;i++){ fprintf(stderr, "%6.2f ",range[i]); } fprintf(stderr,"\n"); */ } if(wanglandau_opt.max_given){ hmax=wanglandau_opt.max; } else{ wanglandau_opt.max=range[wanglandau_opt.bins]; /* the last element */ hmax=wanglandau_opt.max; } h = gsl_histogram_alloc(wanglandau_opt.bins); g = gsl_histogram_alloc(wanglandau_opt.bins); s = gsl_histogram_alloc(wanglandau_opt.bins); gsl_histogram_set_ranges(h,range,(wanglandau_opt.bins+1)); gsl_histogram_set_ranges(g,range,(wanglandau_opt.bins+1)); gsl_histogram_set_ranges(s,range,(wanglandau_opt.bins+1)); } else{ /* determine histogram ranges automatically */ if(wanglandau_opt.max_given){ hmax = wanglandau_opt.max; } else{ hmax=20*fabs(mfe); } h = ini_histogram_uniform(wanglandau_opt.bins,hmin,hmax); g = ini_histogram_uniform(wanglandau_opt.bins,hmin,hmax); s = ini_histogram_uniform(wanglandau_opt.bins,hmin,hmax); } fprintf (stderr, "# sampling energy range is %6.2f - %6.2f\n", hmin,hmax); free(range); /* get the energy range up to which we will compute true DOS via RNAsubopt */ gsl_histogram_get_range(s,0,&lo,&hi); low = lo; gsl_histogram_get_range(s,(wanglandau_opt.truedosbins-1),&lo,&hi); high = hi; wanglandau_opt.erange=(float)fabs(mfe-high+0.01); if(wanglandau_opt.verbose){ printf("Using true DOS for bins 0-%d: (%6.3g -- %6.3g) wl_opt.erange=%6.3f\n", (wanglandau_opt.truedosbins-1),low,high,wanglandau_opt.erange); } /* prepare gsl random-number generation */ (void) clock_gettime(CLOCK_REALTIME, &ts); if(wanglandau_opt.seed_given){ seed = wanglandau_opt.seed; } else { seed = ts.tv_sec ^ ts.tv_nsec; } fprintf(stderr, "initializing random seed: %d\n",seed); gsl_rng_env_setup(); r = gsl_rng_alloc (gsl_rng_mt19937); gsl_rng_set( r, seed ); /* end gsl */ /* make prefix for output */ out_prefix = (char*)calloc(fnlen, sizeof(char)); res_string = (char*)calloc(16, sizeof(char)); sprintf(res_string,"%3.1f", wanglandau_opt.res); strcpy(out_prefix, wanglandau_opt.basename); strcat(out_prefix, ".res"); strcat(out_prefix, res_string); strcat(out_prefix, "."); free(res_string); return; }
double InformacionMalla::getLimiteMayorHistograma(int bin) { double resultado; double aux; gsl_histogram_get_range(h,bin,&aux,&resultado); return resultado; }