/* 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 */ // QMC.java: Quantum MonteCarlo Feynman path integration import java.io.*; // Location of PrintWriter import java.util.*; // Location of Random import java.lang.*; // Location of Math public class QMC { public static void main(String[] argv) throws IOException, FileNotFoundException { PrintWriter q = new PrintWriter( // File output new FileOutputStream("QMC.DAT"), true); int N = 100, M = 101, Trials = 25000, seedTrials = 200; double path[] = new double[N], xscale = 10.; long prop[] = new long[M], seed = 10199435; // Begin Trials for ( int count = 0; count < seedTrials*10; count += 10) { Random randnum = new Random(seed + count); double change = 0., newE = 0., oldE = 0. ; // Initial path for ( int i=0; i < N; i++ ) path[i] = 0. ; oldE = energy(path); // Find E of path // Pick random element for ( int i=0; i < Trials; i++ ) { int element = randnum.nextInt(N); change = 1.8*(0.5 - randnum.nextDouble()); path[element] += change; // Change path newE = energy(path); // Find new E // Metroplis algorithm if ( newE > oldE && Math.exp(-newE + oldE) // Reject <= randnum.nextDouble() ) path[element]-=change; // Add probabilities for ( int j=0; j < N; j++ ) { element = (int)Math.round( (M-1)*(path[j]/xscale + .5) ); if (element < M && element>=0) prop[element]++ ; } oldE = newE; } // t loop } // Seed loop for ( int i=0; i < M; i++ ) q.println(xscale*(i-(M-1)/2) + " " + (double)prop[i]/((double)Trials*(double)seedTrials)); System.out.println(" "); System.out.println("QMC Program Complete."); System.out.println("Data stored in QMC.DAT"); System.out.println(" "); } public static double energy(double path[]) { int i = 0; double sum = 0. ; for ( i=0; i < path.length-2; i++ ) { sum += (path[i+1] - path[i])*(path[i+1] - path[i]) ; } sum += path[i+1]*path[i+1]; return sum; } } // End class