1. #include <iostream>
  2. //include for random numbers
  3. #include <cstdlib>
  4. //include for pow function
  5. #include <cmath>
  6. //include to write to file
  7. #include <fstream>
  8. //include to get the time for seed
  9. #include <time.h>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. //number of time steps N, total time T = Na
  15. const unsigned int N=400;
  16.  
  17. //time step a
  18. const float a=0.25;
  19.  
  20. const int m=1;
  21. const int w=1;
  22. const int hbar=1;
  23. const float delta=1.5;
  24.  
  25. float du, k, g;
  26. float accepted = 0;
  27.  
  28. //generates a random number between -k and k
  29. float randomNumber(int k) {
  30. return (rand()/(float)RAND_MAX)*2*k -k;
  31. }
  32.  
  33.  
  34.  
  35. //method to write to file
  36. int writeOutputToFile(char * filename, float x[]) {
  37. ofstream f;
  38. f.open(filename);
  39. for(int i=0; i<N+1; i++) {
  40. f << x[i] << " " << i << endl;
  41. }
  42. f.close();
  43. return 0;
  44. }
  45.  
  46.  
  47. //this infact computes partial S/a. Leave x a till the end to improve speed slightly?
  48. /*
  49. float partialS(float xi1, float xi) {
  50.   float s;
  51.   s = (m/2)* pow( (xi1 - xi)/a, 2 ) + 0.5 * m * pow(w*(xi1+xi)/2, 2);
  52.   return s;
  53. }
  54.  
  55. //calculate the total action
  56. float totalS(float x[]) {
  57.   float action = 0;
  58.  
  59.   //loop through all sites to get the total action by summing each partial action
  60.   for (int i=0; i<N+1; i++) {
  61.   action = action + partialS(x[i+1], x[i]);
  62.   }
  63.  
  64.   action = a * action;
  65.   return action;
  66. }
  67. */
  68.  
  69. //calculate the change in the action/hbar
  70. //x1 = u_{i-1}, x2= u_{i}, x3 = u_{i+1}
  71. float dAction(float x1, float x2, float x3) {
  72. return du*(du + 2*x2 - g*(x1 + x3));
  73. }
  74.  
  75. //metropolis algorithm
  76. int metropolis(float x[]) {
  77. float newx, newPartialS, oldPartialS, r;
  78. float dS;
  79. //I initially had i<N+1 but this changes x[0] = x[N+1] and I need end points fixed
  80.  
  81. for(int i=1; i<N; i++) {
  82.  
  83. //newx is the oldx +- randomNumber between delta and -delta
  84. newx = x[i] + randomNumber(delta);
  85.  
  86. //calculate the change in action/hbar
  87. dS = dAction(x[i-1], x[i], x[i+1]);
  88.  
  89.  
  90. //if the action is reduced with the new x, then set x[i] = newx;
  91. if (dS<0) {
  92. x[i] = newx;
  93. accepted++;
  94. //otherwise...
  95. } else {
  96.  
  97. //create a random number between 0..1 with uniform probability
  98. r = rand()/(float)RAND_MAX;
  99.  
  100.  
  101. //check to see if we accept newx based on the random number generated and the change in action
  102. if (exp(-1 * dS ) > r) {
  103. //if true, ACCEPT!
  104. x[i] = newx;
  105. accepted++;
  106. } else {
  107. //cout << "here" << endl;
  108.  
  109. }
  110. //cout << (accepted/(float)N) << endl;
  111. accepted = 0;
  112. }
  113.  
  114. return 0;
  115.  
  116. }
  117.  
  118. float monteCarlo(float x[]) {
  119. float integral =0;
  120.  
  121. //number of monte carlo interations
  122. int iter = 300;
  123. writeOutputToFile("output1.txt", x);
  124. //300 monte carlo iterations
  125. for(int i=0; i<iter; i++) {
  126.  
  127.  
  128.  
  129.  
  130. //calculate new path/configuration, a few times
  131. for(int j=0; j<50; j++) {
  132. metropolis(x);
  133. }
  134.  
  135. //compute correlation function
  136. integral = integral + x[N-1] * x[1];
  137.  
  138. }
  139.  
  140. writeOutputToFile("output.txt", x);
  141. return integral/(float)iter;
  142.  
  143. }
  144.  
  145.  
  146. //the main method!
  147. int main() {
  148.  
  149. //set up the variables a little
  150.  
  151. k = 0.25 * a*a * w*w;
  152. g = (1-k)/(1+k);
  153.  
  154. //using float because RAND_MAX is only 10^9, no point in using double
  155. //create an array of size N, since j=0..N-1, the Markov chain
  156. float u[N+1];
  157.  
  158. //set the seed for rand()
  159. srand(time(0));
  160.  
  161. //inputs random variables into sites, being between -5 and 5
  162. u[0] = 0;
  163. u[N] = 0;
  164. for(int i=1; i<N; i++) {
  165. u[i] = randomNumber(5);
  166. }
  167.  
  168. //monteCarlo(u);
  169. //cout << monteCarlo(u) << endl;
  170.  
  171. return 0;
  172.  
  173. }
  174.