/* University of Warwick -- CO904 Statistical Mechanics 2013 */ /* Ising model simulation based on code by Charo del Genio -- 2012 */ /* compile using: gcc Ising2d.c -o Ising2d */ /* run using: ./Ising2d [size] [initprob] [temperature] [number of sweeps] [seed] [output filename] */ /* BEGIN HEADER */ #include #include #include #include /* global variables */ unsigned int STATE[32]; unsigned long int seed; /* prototypes */ void parameters(int nump, char *param[], int *size, double *initprob, double *temperature, int *sweeps, FILE **p_f_output); inline int DeltaIndex(const int x, const int y, const int size, int **spin); /* END HEADER */ //============================= int main(int argc, char *argv[]) { char outputfile[80]; int size, sweeps, DeltaE, x, y, diff, square, **spin; register int i, j, E=0, M=0; double initprob, temperature, Factors[5]; FILE *p_f_output; FILE *spin_output; // computing |m| during simulation (bad practice -- demonstration only) double abs_av_m=0.0; int t_av; /* Check parameters */ parameters(argc, argv, &size, &initprob, &temperature, &sweeps, &p_f_output); square = size*size; // square lattice only spin = malloc(size*sizeof(int*)); // allocate lattice spin[0] = malloc(square*sizeof(int)); for (i=1; i drand48() ? 1 : -1; // Create initial state /* Boltzmann factors */ Factors[0]=exp(-8.0/temperature); // All spins are equal to the one seeded DeltaE = 8 Factors[1]=exp(-4.0/temperature); // 1 spin is different DeltaE = 4 Factors[2]=1.0; // 2 spins are different DeltaE = 0 Factors[3]=exp(4.0/temperature); // 3 spins are different DeltaE = -4 Factors[4]=exp(8.0/temperature); // All spins are different DeltaE = -8 /* record initial state */ for (x=0; x10000) { // discard initialisation -- equilibration time t_av = i-10000; abs_av_m = abs_av_m * (((double) (t_av-1))/((double) t_av)) + fabs(((double) M)/square)/((double) t_av); } } // end sweep fprintf(p_f_output, "%.10f\t%.10f\n",temperature,abs_av_m); printf("Average |m| is: %.10f\n",abs_av_m); // output the final spin configuration for visualisation spin_output = fopen("spins.dat","w"); for (x=0; x1.0) { printf("\nThe probability for each spin to initially point up must be between 0 and 1.\n\n"); exit(EXIT_FAILURE); } *temperature = atof(param[3]); *sweeps = atoi(param[4]); if (*sweeps<1) { printf("\nYou must run at least 1 sweep.\n\n"); exit(EXIT_FAILURE); } seed=strtoul(param[5],NULL,10); if (seed<=0) { printf("\nThe random number seed must be a positive integer.\n\n"); exit(EXIT_FAILURE); } // seedrng(); srand48(seed); /* if ( (*p_f_output = fopen(param[6],"r") ) != NULL ) { printf("\nError!\nOutput file %s already exists!\n\n",param[6]); exit(EXIT_FAILURE); } if ( (*p_f_output = fopen(param[6],"w") ) == NULL ) { printf("\nError creating output file %s.\n\n",param[6]); exit(EXIT_FAILURE); } */ if ( (*p_f_output = fopen(param[6],"a") ) == NULL ) { // currently I want to append to the file printf("\nError creating output file %s.\n\n",param[6]); exit(EXIT_FAILURE); } return; } // end parameters