/* 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; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // qmc.c: Feynman path integral for ground state wave function #include #include #include // if you don't have drand48 uncomment the following two lines #define drand48 1.0/RAND_MAX*rand #define srand48 srand #define max 250000 // number of trials #define seed 68111 // seed for number generator main() { double change, newE, oldE, path [101]; int i, j, element, prop [101]; double energy( double array[]); // finds energy of path FILE *output; // save data in qmc.dat output = fopen("qmc.dat","w"); srand48(seed); // seed number generator for (j = 0; j<= 100; j++) path[j] = 0.0; // initial path for (j = 0; j<= 100; j++) prop[j] = 0; // initial probability oldE= energy(path); // find energy of path for (i = 0; ioldE) && (exp(-newE+oldE) <= drand48())) { path[element] -= change; } // reject // add up probabilities for (j = 0; j<= 100; j++) { element = path[j]*10+50; prop[element]++; } oldE= newE; } for (i = 0; i<= 100; i++) { fprintf(output, "%d\t%f\n", i-50, (double) prop[i]/max);} printf("data stored in qmc.dat\n"); fclose(output); } // end of main double energy (double array[]) { // energy of path configuration int i; double sum = 0.; for (i = 0; i<100; i++) { sum += pow(array[i+1]-array[i], 2.0) + array[i]*array[i]; } return (sum); }