import numpy as np
import matplotlib.pyplot as plt
from file_to_np import file_to_array

L = [40, 60, 80, 100]
T = np.linspace(2.24, 2.3, 30)

for i in range(len(L)):
    E_mean = file_to_array(f'Results/4e_E_L{L[i]}.dat')
    M_mean = file_to_array(f'Results/4e_M_L{L[i]}.dat')
    C_V = file_to_array(f'Results/4e_Cv_L{L[i]}.dat')
    chi = file_to_array(f'Results/4e_chi_L{L[i]}.dat')
    data = [E_mean, M_mean, C_V, chi]
    ylabels = [
        r'$\langle E\rangle$', r'$\langle |M|\rangle$', r'$C_V$', r'$\chi$'
    ]
    titles = [
        f'Mean energy during phase transition, L={L[i]}',
        f'Mean absolute magnetization during phase transition, L={L[i]}',
        f'Specific heat during phase transition, L={L[i]}',
        f'Susceptibility during phase transition, L={L[i]}'
    ]

    for j in range(len(data)):
        plt.plot(T, data[j], color="indigo")
        plt.xlabel("T [kT/J]", fontsize=12)
        plt.ylabel(ylabels[j], fontsize=12)
        plt.subplots_adjust(left=0.15, right=0.92)
        plt.title(titles[j], fontsize=14)
        plt.savefig(f'Figures/4e_L{L[i]}_{j}.png')
        plt.show()
import numpy as np
import matplotlib.pyplot as plt
from file_to_np import file_to_array
import os

os.system("g++ -std=c++11 -o 4c.x 4c.cpp ising_model.cpp -O3")
os.system("./4c.x")

M1 = file_to_array("Results/burn_in_M_4c_1.dat")
E1 = file_to_array("Results/burn_in_E_4c_1.dat")
acceptance1 = file_to_array("Results/4c_acceptance_1.dat")

M2 = file_to_array("Results/burn_in_M_4c_2.dat")
E2 = file_to_array("Results/burn_in_E_4c_2.dat")
acceptance2 = file_to_array("Results/4c_acceptance_3.dat")

M3 = file_to_array("Results/burn_in_M_4c_3.dat")
E3 = file_to_array("Results/burn_in_E_4c_3.dat")
acceptance3 = file_to_array("Results/4c_acceptance_2.dat")

M4 = file_to_array("Results/burn_in_M_4c_4.dat")
E4 = file_to_array("Results/burn_in_E_4c_4.dat")
acceptance4 = file_to_array("Results/4c_acceptance_4.dat")

plt.plot(M1, label="T=1.0", color="green")
plt.xlabel("Number of MC-cycles", fontsize=12)
plt.ylabel("M", fontsize=12)
plt.title("Time evolution of M from organized grid", fontsize=14)
plt.legend()
plt.savefig("Figures/4c_M1.png")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from file_to_np import file_to_array

L = np.array([40, 60, 80, 100])
T = np.linspace(2.24, 2.3, 30)

T_C_CV = np.zeros(len(L))
T_C_chi = np.zeros(len(L))

for i in range(len(L)):
    C_V = file_to_array(f'Results/4e_Cv_L{L[i]}.dat')
    chi = file_to_array(f'Results/4e_chi_L{L[i]}.dat')

    i_C_V = np.argmax(C_V)
    i_chi = np.argmax(chi)

    T_C_CV[i] = T[i_C_V]
    T_C_chi[i] = T[i_chi]

T_C_average = (T_C_CV + T_C_chi) / 2.

print("T_C_CV:", T_C_CV)
print("T_C_chi:", T_C_chi)
print("T_C_average:", T_C_average)

x = 1 / L
y = T_C_average
n = len(x)

# least squares
import numpy as np
import matplotlib.pyplot as plt
from file_to_np import file_to_array
import os

os.system("g++ -std=c++11 -o 4d.x 4d.cpp ising_model.cpp -O3")
os.system("./4d.x")

E1 = file_to_array("Results/4d_T1_E.dat")
E2 = file_to_array("Results/4d_T2_E.dat")

var1 = np.var(E1)
var2 = np.var(E2)

plt.hist(E1, bins=20, label=f'$\sigma_E^2$ = {var1:.2f}', color="firebrick")
plt.title("Distribution of energies, T=1.0", fontsize=14)
plt.xlabel("E", fontsize=12)
plt.ylabel("Number of configurations", fontsize=12)
plt.legend()
plt.savefig("Figures/4d_histogram_T1.png")
plt.show()

plt.hist(E2, bins=20, label=f'$\sigma_E^2$ = {var2:.2f}', color="firebrick")
plt.title("Distribution of energies, T=2.4", fontsize=14)
plt.xlabel("E", fontsize=12)
plt.ylabel("Number of configurations", fontsize=12)
plt.legend()
plt.savefig("Figures/4d_histogram_T2.png")
plt.show()
J = 1.

# Analytic expression
E_mean_a = (-8 * J * np.exp(beta * 8 * J) + 8 * J * np.exp(-beta * 8 * J)) / (
    np.exp(beta * 8 * J) + np.exp(-beta * 8 * J) + 6)
M_mean_abs_a = (4 * np.exp(8 * beta * J) + 2) / (np.exp(8 * beta * J) +
                                                 np.exp(-8 * beta * J) + 6)
C_V_a = (1. / (k_B * (T**2))) * (
    (64 * (J**2) * np.exp(beta * 8 * J) + 64 *
     (J**2) * np.exp(-beta * 8 * J)) /
    (np.exp(beta * 8 * J) + np.exp(-beta * 8 * J) + 6) - E_mean_a**2)
chi_a = beta * ((8 * np.exp(-beta * 8 * J) + 8 * np.exp(beta * 8 * J) + 4) /
                (np.exp(8 * beta * J) + np.exp(8 * beta * J) + 6))

# Numerical results
E_mean = file_to_array("Results/E_mean_4b.dat")
M_mean = file_to_array("Results/M_mean_4b.dat")
C_V = file_to_array("Results/C_V_4b.dat")
chi = file_to_array("Results/chi_4b.dat") / 4.

MC_cycles = np.logspace(1, n, n)

# Plots for comparison
plt.plot(MC_cycles, E_mean, label="Numerical", color="red")
plt.axhline(y=E_mean_a, label="Analytic", color="green")
plt.xscale("log")
plt.xlabel("Number of MC-cycles", fontsize=12)
plt.ylabel(r'$\langle E\rangle$', fontsize=12)
plt.title("Mean energy of 2x2 lattice", fontsize=14)
plt.legend()
plt.subplots_adjust(left=0.15)