#include #include #include #include #include /*** wanglandau.c * * * (c) 2004 cameron abrams * drexel university * department of chemical engineering * * philadelphia * * Implements the Wang-Landau flat histogram method for either * an Ising or Potts model in two dimensions, a la * Wang&Landau, PRL 86:2050 (2001) and PRE 64:056101 (2001). * * ***/ enum { POTTS, ISING, NULL_SCHEME }; char * scheme_name [NULL_SCHEME] = {"POTTS", "ISING"}; int get_ebin ( int e, int n, int Scheme ) { /* Both: e_0 = 2*n Ising has n-1 occupiable levels: -e0,-e0+8,-e0+12,...,-4,0,4,...e0-12,e0-8,e0 Potts has n occupiable levels: as in -e0,-e0+4,-e0+6,...,-6,-4,-2,0 */ int e_0 = 2*n; int ee; ee = e+e_0; if (ee) {ee/=2; if (Scheme==ISING) ee/=2; ee-=1;} if (Scheme==ISING && e==e_0) ee--; return ee; } int get_energy ( int i, int n, int Scheme ) { int e_0 = 2*n, e; if (!i) return -e_0; else { e=(i+1)*(Scheme==ISING?4:2)-e_0; if (Scheme==ISING && i==(n-2)) e=e_0; } return e; } int E ( int ** F, int L, int Scheme ) { int i,j; int energy=0; for (i=0;i=*nelevels) { fprintf(stderr,"# Error; file %s contains too many entries\n" "Expecting %i\n", idosfn,*nelevels); exit(-1); } (*lng)[i]=lng_i; } fclose(ifp); } else { fprintf(stderr,"# error: cannot read %s\n",idosfn); exit(-1); } } void write_data (char * fn, int segment, double lnf, int N, int Scheme, int * ehist, double * lng, int nelevels) { FILE * fp=fopen(fn,"w"); int i; if (fp) { fprintf(fp,"# wl segment %i f %.10le Scheme %s L %i\n", segment,exp(lnf),scheme_name[Scheme],sqrt(N)); for (i=0;i1.0) prob=1.0; // select a uniform random variate between 0 and 1 x=gsl_rng_uniform(r); if (x f_tol) keep_going=1; else keep_going=0; segment++; } if (tail_fp) { fprintf(tail_fp,"# end of data\n"); fclose(tail_fp); } fprintf(stderr,"# Program ends.\n"); }