anderkve / FYS3150

https://anderkve.github.io/FYS3150
26 stars 14 forks source link

Energy per spin #81

Closed JohanCarlsen closed 1 year ago

JohanCarlsen commented 1 year ago

Hi! So I've implemented code to calculate the energy per spin after $n$ MCMC cycles. After 100.000 cycles, the energy is very close to the analytical expectation value. This is also the case for susceptibility.

When I try to plot this to visualize the convergence toward the analytical values, it seems the values per cycle are linear. I would think this should not be the case, but I can't figure out why it is this way. The code computing the values is as follows

for (int n = 0; n < nCycles; n++)
  {
    mcmc(lattice, generator, L, E, M, T);

    sumE += E;
    sumEE += (E * E);
    sumM += fabs(M);
    sumMM += (M * M);

    avgEps = sumE / (nCycles * N);
    avgEps_sqrd = sumEE / (nCycles * N * N);
    avgM = sumM / (nCycles * N);
    avgM_sqrd = sumMM / (nCycles * N * N);

    outfile << setw(width) << n + 1
            << setw(width) << avgEps
            << setw(width) << avgM
            << setw(width) << N * (avgEps_sqrd - avgEps * avgEps) / (T * T)
            << setw(width) << N * (avgM_sqrd - avgM * avgM) / T
            << endl;
  }

Is it clear why this is linear? Link to full repo: https://github.com/Madssb/FYS3150_H22_4_pers/tree/johan/Project_4

anderkve commented 1 year ago

Hi! Writing from my phone, so will keep it short.

I think you are dividing by the wrong sample (cycle) count. After having done n cycles your average is a sum over the n (not nCycles) samples you have collected so far.

JohanCarlsen commented 1 year ago

Thank you! That fixed it! 👍🏽