ising_mf.py

ising_mf.py

from __future__ import print_function

import numpy as np
from scipy import optimize

# scipy.optimize.root_scalar
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root_scalar.html#scipy.optimize.root_scalar


def func(m, zj, beta, h):
    return m - np.tanh(beta * (zj * m + h))


def find_solution(zj, t, h, m_min=1e-6, m_max=1, verbose=False):
    beta = np.inf if t==0 else 1./t
    sol = optimize.root_scalar(func, args=(zj, beta, h), method='brentq', bracket=(m_min, m_max))
    if verbose:
        print(sol)
    return sol.root


def calc_entropy(m):
    if abs(m)==1:
        return 0

    s = 0
    for x in [(1.0+m)/2.0, (1.0-m)/2.0]:
        s -= x * np.log(x)
    return s


def calc_specific_heat(m, zj, t):
    if abs(m)==1:
        return 0

    t_tc = t / zj  # T / T_c
    c = t_tc/4.0 * (1.0-m**2) / (t_tc - (1.0-m**2)) * np.log((1.0+m)/(1.0-m))**2
    return c


def main():
    t = 0.1
    zj = 1.0
    h = 0.0
    m = find_solution(zj, t, h, verbose=True)
    s = calc_entropy(m)
    c = calc_specific_heat(m, zj, t)
    print(f"zJ = {zj}")
    print(f"T  = {t}")
    print(f"h  = {h}")
    print(f"m  = {m}")
    print(f"S  = {s}")
    print(f"C  = {c}")


if __name__ == '__main__':
    main()