/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // Ising.java: 1-D Ising model with Metropolis algorithm import java.io.*; // Location of PrintWriter import java.util.*; // Location of Random public class Ising { public static int N = 1000; // Number of atoms public static double B = 1.; // Magnetic field public static double mu = .33; // Magnetic moment of atom public static double J = .20; // Exchange energy public static double k = 1.; // Boltzman constant public static double T = 100000000.; public static void main(String[] argv) throws IOException, FileNotFoundException { Random randnum = new Random(500767); // Seed random generator PrintWriter q = new PrintWriter(new FileOutputStream ("ising.dat"), false); int i, j, M = 5000; // Number of spin flips double[] state = new double[N]; double[] test = state; double ES = energy(state), p, ET; // State, test's energy // Set initial state for ( i=0 ; i < N ; i++ ) state[i] = -1.; // Change state and test for ( j=1 ; j <= M ; j++ ) { test = state; i = (int)(randnum.nextDouble()*(double)N); // Flip random atom test[i] *= -1.; ET = energy(test); p = Math.exp((ES-ET)/(k*T)); // Test trial state if (p >= randnum.nextDouble()) { state = test; ES = ET; } q.println(ES); // Output energy to file } q.close(); } public static double energy (double[] S) { // Method calc energy double FirstTerm = 0., SecondTerm = 0. ; int i; // Sum of energy for ( i=0 ; i <= (N-2) ; i++ ) FirstTerm += S[i]*S[i + 1]; FirstTerm *= -J; for ( i=0 ; i <= (N-1) ; i++ ) SecondTerm += S[i]; SecondTerm *= -B*mu; return (FirstTerm + SecondTerm); } }