/* Metropolis Monte Carlo simulation of a 2D Ising system Cameron F. Abrams Written for the course CHE 800-002, Molecular Simulation Spring 0304 compile using "gcc -o ising ising.c -lm -lgsl" (assumes the GNU Scientific Library is installed) runs as "./ising -L -T \ -nc -fs \ -s " For example, to run a 20x20 system at T = 0.5 for 1e7 cycles sampling every 100 cycles, the command looks like ./ising -L 20 -T 0.5 -nc 1e7 -fs 100 The default values are shown in parentheses above. You must have the GNU Scientific Library installed; see the coursenotes to learn how to do this. The Hamiltonian is H = -J sum_ s_i * s_j, where "sum_" is the sum over all unique nearest neighbor pairs, s_i = +/- 1, and J is an arbitrary "coupling" parameter having units of energy. We work in a unit system where energy is measured in units of J and temperature is nondimensionalized as (k_B*T)/J. Drexel University, Department of Chemical Engineering Philadelphia (c) 2004 */ #include #include #include #include /* This function computes and returns the change in system energy when spin (i,j) is flipped. The modulo arithmetic (the % operator) ensures periodic boundaries. The syntax "i?(i-1):(L-1)" performs the following check: If i is non-zero, return i-1, otherwise return L-1. This also ensures periodic boundaries. */ int E ( int ** F, int L, int i, int j ) { return -2*(F[i][j])*(F[i?(i-1):(L-1)][j]+F[(i+1)%L][j]+ F[i][j?(j-1):(L-1)]+F[i][(j+1)%L]); } /* Sample the system; compute the average magnetization and the average energy per spin */ double samp ( int ** F, int L, double * s, double * e ) { int i,j; *s=0.0; *e=0.0; /* Visit each position (i,j) in the lattice */ for (i=0;i