Monte Carlo method applied on a 2D binary alloy using an Ising Model on Python | by Jonathan Leban | Towards Data Science

A material such as a piece of metal or a glass sample has physical properties such as electrical conductivity, magnetic susceptibility, thermal conductivity and many others. For industrial applications, these properties must be known in order to select the best material or alloy that will meet the requirements of the load cell. For example, the body of a car must be light, load and corrosion resistant. To meet all these needs, the three main materials used are: metals, polymers and aluminium alloys. But how do you determine the properties of an object? How is it possible to calculate the properties of a material composed of billions of billions of microstates?
Strictly speaking, we need to know the energy of each microstate in order to calculate the properties of a material. In this article, I will focus on four physical quantities: energy, magnetic susceptibility, magnetization, and heat capacity. These quantities are those that are interconnected and define a system well because they are fundamental physical properties although energy is the basis of all the relationships we define below. But one question remains: how to simulate the behavior of a material in order to calculate its properties?
As we are limited by the power of computers, we cannot model the complete behavior of the hardware because it would cost us too much time and lead us to ask a new question: how should the size of the system be a representation close to reality?
To answer these questions, I will consider a 2D binary alloy as the piece of material I want to examine. For all the properties I am interested in, I will compare the theoretical value and the experimental value (value given by the simulation) for different system sizes.
But first, we will look at the mathematics and physics that establish the relationships for the different properties we want to evaluate.
The probability of finding the system in a particular microstate with the energy E is given by the canonical distribution :
With β = 1/kT, k defined as the Boltzman constant and T as the temperature. Z is known as the repartition function and is given by:
With both equations, we can measure any experimental parameter but, in general, summation cannot be performed. Fortunately, the probability of finding the system in the micro state of energy E, P(E), is a precise function of the energy E. Thus, we can make an approximation of the number of microstates to be taken into account in the calculation of physical properties. In this paper, I will show the impact of the size of the system on the accuracy of the calculation of the physical properties.
As I said before, we are only interested in energy, magnetic susceptibility, magnetization and heat capacity. There are multiple ways to simulate these properties as Density Functional Theory (the most accurate one), Machine Learning or Monte Carlo method.
In this article, I decided to build a Monte Carlo simulation of Ising’s 2D model with H=0. Using this model, I was able to calculate the expectation values of the absolute value of spin magnetization for L xL spins systems with L=4, 8, 16 and 32 as a function of temperature (the Ising model is the representation of spins on a graph). The Monte Carlo method is based on the repetition of random sampling (changing a rotation from -1 to 1 or vice versa) to obtain a new energy value. If the value of the total energy decreases, we accept the new situation because it means that we are getting closer to the equilibrium situation where the energy is minimal.As it is more convenient to work in dimensionless units, I have therefore calculated all energies in J units. All temperatures have been measured in J/k units.
The expression for the Energy of the total system is :
Where σ is the spin and can be equal to -1 or 1. In addition, I computed the magnetic susceptibility per site χ, at each temperature.
Here M is the total magnetization of the system and T the temperature. The magnetization per site is given by:
And finally the heat capacity per spin C is the following:
with Tc the critical temperature defined by ln(1 + √2) = (2J)/(k*Tc). It can be seen that the heat capacity diverges logarithmically at the critical temperature. This divergence is not visible in a finite size system. In addition, I did not have an analytical solution for magnetic susceptibility, so I did not compare the simulation result with the exact result.
Now, as the mathematics are done, we can go into the computational part.
With Python, I built Monte Carlo simulation of the 2D Ising model with H=0 and I included uncertainty for each physical property.
def init(L):        state = 2 * np.random.randint(2, size=(L,L)) - 1        return state        def E_dimensionless(config,L):    total_energy = 0    for i in range(len(config)):        for j in range(len(config)):            S = config[i,j]            nb = config[(i+1)%L, j] + config[i, (j+1)%L] + config[(i-1)%L, j] + config[i, (j-1)%L]            total_energy += -nb * S    return (total_energy/4)def magnetization(config):    Mag = np.sum(config)        return Mag
I first initialized my system which is a square of LxL spins that can take the value -1 or 1. I created one function to calculate the energy and another one for the magnetization.
def MC_step(config, beta):    '''Monte Carlo move using Metropolis algorithm '''    L = len(config)    for i in range(L):        for j in range(L):            a = np.random.randint(0, L) # looping over i & j therefore use a & b            b = np.random.randint(0, L)            sigma =  config[a, b]            neighbors = config[(a+1)%L, b] + config[a, (b+1)%L] + config[(a-1)%L, b] + config[a, (b-1)%L]            del_E = 2*sigma*neighbors            if del_E < 0:                sigma *= -1            elif rand() < np.exp(-del_E*beta):                sigma *= -1            config[a, b] = sigma    return config
Then I used the Metropolis algorithm for my Monte Carlo simulation. Based on a random initial configuration, I performed a random spin change and recalculated the energy. If the energy of the system decreases, the new configuration is accepted as we approach equilibrium. Otherwise, if the new system has a higher energy, the configuration is accepted with a probability given by exp(−βΔE).
def calcul_energy_mag_C_X(config, L, eqSteps, err_runs):            # L is the length of the lattice            nt      = 100         #  number of temperature points    mcSteps = 1000        T_c = 2/math.log(1 + math.sqrt(2))            # the number of MC sweeps for equilibrium should be at least equal to the number of MC sweeps for equilibrium    # initialization of all variables    T = np.linspace(1., 7., nt);     E,M,C,X = np.zeros(nt), np.zeros(nt), np.zeros(nt), np.zeros(nt)    C_theoric, M_theoric = np.zeros(nt), np.zeros(nt)    delta_E,delta_M, delta_C, delta_X = np.zeros(nt), np.zeros(nt), np.zeros(nt), np.zeros(nt)    n1 = 1.0/(mcSteps*L*L)    n2 = 1.0/(mcSteps*mcSteps*L*L)    # n1 and n2 will be use to compute the mean value and the # by sites        # of E and E^2            Energies = []    Magnetizations = []    SpecificHeats = []    Susceptibilities = []    delEnergies = []    delMagnetizations = []    delSpecificHeats = []    delSusceptibilities = []        for t in range(nt):        # initialize total energy and mag        beta = 1./T[t]        # evolve the system to equilibrium        for i in range(eqSteps):            MC_step(config, beta)        # list of ten macroscopic properties        Ez = [], Cz = [], Mz = [], Xz = []         for j in range(err_runs):            E = E_squared = M = M_squared = 0            for i in range(mcSteps):                MC_step(config, beta)                           energy = E_dimensionless(config,L) # calculate the energy at time stamp                mag = abs(magnetization(config)) # calculate the abs total mag. at time stamp                # sum up total energy and mag after each time steps                E += energy                E_squared += energy**2                M += mag                M_squared += mag**2            # mean (divide by total time steps)            E_mean = E/mcSteps            E_squared_mean = E_squared/mcSteps            M_mean = M/mcSteps            M_squared_mean = M_squared/mcSteps            # calculate macroscopic properties (divide by # sites) and append            Energy = E_mean/(L**2)            SpecificHeat = beta**2 * (E_squared_mean - E_mean**2)/L**2            Magnetization = M_mean/L**2            Susceptibility = beta * (M_squared_mean - M_mean**2)/(L**2)            Ez.append(Energy); Cz.append(SpecificHeat); Mz.append(Magnetization); Xz.append(Susceptibility);        Energy = np.mean(Ez)        Energies.append(Energy)        delEnergy = np.std(Ez)        delEnergies.append(float(delEnergy))                Magnetization = np.mean(Mz)        Magnetizations.append(Magnetization)        delMagnetization = np.std(Mz)        delMagnetizations.append(delMagnetization)                SpecificHeat = np.mean(Cz)        SpecificHeats.append(SpecificHeat)        delSpecificHeat = np.std(Cz)        delSpecificHeats.append(delSpecificHeat)        Susceptibility = np.mean(Xz)        delSusceptibility = np.std(Xz)                Susceptibilities.append(Susceptibility)        delSusceptibilities.append(delSusceptibility)                                    if T[t] - T_c >= 0:            C_theoric[t] = 0        else:            M_theoric[t] = pow(1 - pow(np.sinh(2*beta), -4),1/8)                coeff = math.log(1 + math.sqrt(2))        if T[t] - T_c >= 0:            C_theoric[t] = 0        else:             C_theoric[t] = (2.0/np.pi) * (coeff**2) * (-math.log(1-T[t]/T_c) + math.log(1.0/coeff) - (1 + np.pi/4))             return (T,Energies,Magnetizations,SpecificHeats,Susceptibilities, delEnergies, delMagnetizations,M_theoric,             C_theoric, delSpecificHeats, delSusceptibilities)
For each system size (4x4, 8x8, 16x16, 32x32), I’ve calculated energy, magnetization, specific heat and magnetic susceptibility. Each graph corresponds to a property. The black line for magnetization and specific heat is the exact solution and for each property an error estimation has been taken into account.
After executing the script above, I have the following results:
The simulations illustrate the concepts of finite size scale. Using the exact results known for Tc and critical exponents, we can check whether the simulation results for large systems actually converge towards the thermodynamic limit behaviour. The most fundamental concept underlying critical phenomena theory is a correlation length, which is a measure of a typical system length scale. The correlation length can be defined in terms of a correlation function, which, in the case of the Ising model, is given by the following. The correlation function decreases exponentially over long distances and is given by the so-called Ornstein-Zernicke form:
C(r⃗ )=exp(−r/ξ)/r^(d-2/2)
where ξ is the correlation length. This expression is valid only when r>>ξ. The correlation length ξ corresponds to the typical size of the ordered domains in the system. In the Ising model above Tc there are on average equal number of ordered domains with spin up and down (the material has lost all, and their typical size corresponds to the correlation length). Below Tc the correlation length corresponds to the typical size of domains of spins in an ordered background of opposite directed spins.
As a critical point (continuous phase transition) is approached, the correlation length diverges, in the thermodynamic limit, according to a power-law : ξ∼t^(−ν) where t is the reduced temperature measuring the distance from the critical point : t=|T−Tc| and ν is an example of a critical exponent. Another important critical exponent is the one determining the onset of order at the critical point, the magnetization. The order parameter is zero above Tc , and below Tc it emerges as : <m>=(Tc−T)^β.
For the heat capacity the relation is given by the following formula C=t^(−α). The heat capacity presents a singularity as T gets close to Tc.
For the susceptibility, the relation is the following: χ=t^(−γ). It seems that χ presents a singularity as T gets close to Tc. Hence, the system becomes infinitely sensitive to a magnetic field h as T gets close to Tc.
The set of exponents ν,α,β,γ for different systems fall into universality classes. Systems belonging to the same universality class have the same exponents. The universality class does not depend on microscopic details related to the system constituents and their interactions (as long as the interactions are short-ranged; long-range interactions can change the universality class), only on the dimensionality of the system and the nature of the order parameter. In our cases, the size of the system is therefore L. This is why the magnetization profile, heat capacity and temperature susceptibility differ from each other as the size of the system changes. The basis of finite-size scaling is that deviations from the infinite-size critical behavior occurs when the correlation length χ becomes comparable with the system length L. The way these finite-size deviations affects other quantities can be studied by expressing their temperature dependencies using the correlation length as the variable. Thus, t∼ξ^(−1/ν)
For example, in the asymptotic power-law form of the magnetization <m>=Tc^β t^β=Tc^β ξ^(−β/ν). The maximum value of the magnetization for a given system size L should be <m>=Tc^β L^(−β/ν). The convergence of the magnetization toward zero depends on the dimension size of the system L. In the 2D Ising model, we also have the following relation β/ν=1/8.
Based on the following expression χ=t^(−γ), in the asymptotic power-law form of the susceptibility, the substitution of t gives χ=ξ^(γ/ν). The maximum value of the susceptibility for a given system size L should be χ=L^(γ/ν). With this expression we can see that the peak value of χ grows very rapidly with L and explains the difference of profile in the susceptibility plot of the different system sizes. For the 2D Ising model, we have γ/ν=7/4
Regarding the heat capacity, the relation is quite similar and it is the following one C=ξ^(α/ν) and the maximum value of the heat capacity for a given system size L should be C=ξ^(α/ν). Thus as for the magnetic susceptibility, the peak value of C grows very rapidly with L and explains the difference of profile in the heat capacity plot of the different system sizes.
From a more qualitative point of view, we notice that there is an inflection point for T=Tc for the energy profile but also that magnetization, thermal capacity and susceptibility fall to zero from this temperature. This means a change in the system behavior, which corresponds to the definition of the critical temperature. In fact, the critical temperature is the Curie temperature in our case, and the Curie temperature is the temperature above which the material loses all its permanent magnetic properties, which heralds a change in the behavior of the material. The permanent magnetic properties are caused by the alignment of the magnetic moments. Thus, above Tc, there are on average an equal number of domains ordered with upward and downward spin, which can be illustrated by the convergence of magnetic moment, specific heat and susceptibility towards zero.
Thus, the relationship between the accuracy of the results and the size of the system depends on the relevance we are examining. But the larger the size of the system, the higher the computational cost. Thus, since the cost of time is really important, we need to think about the trade-off: time/cost/precision. That’s why I wondered about the dependence of each property on the size of the system. What I understood is that for energy, we can perform our simulation with the smallest system size and the result will be usable because the dependence of energy on system size is exponential. This is no longer true for other properties where the larger the system size, the more accurate the results are. Nevertheless, we can still improve our results by increasing the number of steps needed to reach equilibrium for the 32x32 system size.
PS: I am currently a Master of Engineering Student at Berkeley, and if you want to discuss the topic, feel free to reach me. Here is my email.