Advertisement
SepandMeenu

Phase transition in Ising model with ext. field

Feb 8th, 2018
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. # Python3
  2. # see: < https://physics.stackexchange.com/q/348917 >
  3.  
  4. import numpy as np
  5. from scipy.optimize import fsolve
  6. import matplotlib.pyplot as plt
  7. #==============================================================================80
  8.  
  9. #-- mean-field equation: m = tanh( Tc/T * (h + m) )
  10.  
  11. # m  := magnetic order parameter
  12. # h  := rescaled external magnetic field = h_ext / Tc
  13. # t  := T/Tc , rescaled temperature
  14. # HM := Tc/T * (h + m) = (h + m)/t
  15.  
  16. #-- MF solver
  17. def solveMF(hs, t):
  18.     # solves MF eq for the given h-values at a rescaled temperature t
  19.    
  20.     MFeq = lambda m, h: m - np.tanh( (m+h)/t )
  21.     ms = map(lambda h: fsolve(lambda m: MFeq(m, h), x0= -2 if h < 0 else +2), hs)
  22.  
  23.     return tuple(ms)
  24. #-------------------
  25.    
  26. #-- thermodynamic quantities
  27. MH = lambda m, h, t: (m + h)/t
  28.  
  29. # free energy, f(h, T; m) = F/N
  30. FE  = lambda m, h, t: 0.5 * m**2 - t * np.log( 2*np.cosh(MH(m,h,t)) )
  31.  
  32. # dm/dt
  33. dM_dT = lambda m, h, t: MH(m, h, t) / (1 - t/(1 - m**2))
  34.  
  35. # entropy, s(h, T; m) = S/N
  36. def S(m, h, t):
  37.     mh0 = MH(m, h, t)
  38.     return np.log( 2*np.cosh(mh0) ) - m * mh0
  39.  
  40. # specific heat at const. volume, c_V(h, T; m) = C_V/N
  41. CV = lambda m, h, t: -t * MH(m, h, t)**2 / (1 - t/(1 - m**2))
  42.  
  43. # magnetic susceptibility
  44. Xh = lambda m, h, t: -1 / (1 - t/(1 - m**2))
  45.  
  46. #==============================================================================80
  47.  
  48. #-- free energy and entropy difference for the case where a finite opposite h
  49. # is applied to a system prepared in an ordered phase (T < Tc)
  50.  
  51. t0 = 0.5  # T < Tc
  52. hs = (1e-3, -1)  # initial and final magnetic fields
  53. ms = solveMF(hs, t0)
  54. fs = tuple( map(lambda z: FE(z[0], z[1], t0), zip(ms, hs)) )
  55. ss = tuple( map(lambda z: S(z[0], z[1], t0) , zip(ms, hs)) )
  56. df = fs[1]-fs[0]
  57. ds = ss[1]-ss[0]
  58. print("free-energy change: f(h=%.3f) - f(h=%.3f) = %.3f" % (hs[1], hs[0], df) )
  59. print("entropy change    : s(h=%.3f) - s(h=%.3f) = %.3f" % (hs[1], hs[0], ds) )
  60. print("heat exchange     : dq = T ds = %.3f" % (ds/t0) )
  61.  
  62. #-- plots
  63.  
  64. #--- graphical solution of MF eq
  65. h0 = -0.1                       # rescaled magnetic field
  66. pTitle0  = r"$T < T_c$" if t0 < 1 else r"$T > T_c$" if t0 > 1 else r"$T = T_c$"
  67.  
  68. Mmax = 2                        # max of m
  69. ms = np.linspace(-Mmax, Mmax, 1<<10)  # m values
  70. RHSs = list( map(lambda m_: np.tanh( (m_+h0)/t0 ), ms) )  # RHS vals.
  71. fs = list(map(lambda m_: FE(m_, h=h0, t=t0), ms))  # free-energy vals.
  72.  
  73. plt.plot(ms, ms, color='green', label='LHS')
  74. plt.plot(ms, RHSs, color='orange', label='RHS')
  75. plt.plot(ms, fs, color='crimson', linestyle='-', lw = 1, label='f')
  76.  
  77. pTitle = "Graphical solution to MF eq\n" + pTitle0 + "\n" + r"$h_0$"+ "= %.3f" % h0
  78. plt.title(pTitle, fontsize=10)
  79. plt.xlabel("m")
  80. plt.grid()
  81. plt.legend()
  82. plt.show()
  83.  
  84. #--- thermodynamic quantities
  85. Hmax = 1                                           # max of h
  86. hs = np.linspace(-Hmax, Hmax, 1<<10, dtype=float)  # h-field vals.
  87. ms = solveMF(hs, t0)                               # solve for m
  88.  
  89. # m-h plot
  90. plt.plot(hs, ms,
  91.          color='blue', marker='.', ms=0.2, lw = 0, label=r'$m$')
  92.  
  93. plt.title(pTitle0)
  94. plt.xlabel("h")
  95. plt.ylabel("m")
  96. #plt.legend()
  97. plt.show()
  98.  
  99. # entropy
  100. ss = list( map(lambda z: S(z[0], z[1], t0), zip(ms, hs)) )
  101.  
  102. plt.plot(hs, ss,
  103.          color='red', lw = 1, label='s')
  104.  
  105. plt.title(pTitle0)
  106. plt.xlabel("h")
  107. plt.ylabel("s")
  108. #plt.legend()
  109. plt.show()
  110.  
  111. # magnetic susceptibility
  112. xhs = list( map(lambda z: Xh(z[0], z[1], t0), zip(ms, hs)) )
  113. plt.plot(hs, xhs,
  114.          color='orange', lw = 1, label=r'$\chi_h$')
  115.  
  116. plt.title(pTitle0)
  117. plt.xlabel("h")
  118. plt.ylabel(r'$\chi_h$')
  119. #plt.legend()
  120. plt.show()
  121.  
  122. # specific heat
  123. cvs = list( map(lambda z: CV(z[0], z[1], t0), zip(ms, hs)) )
  124. plt.plot(hs, cvs,
  125.          color='green', lw = 1, label=r'$c_V$')
  126.  
  127.  
  128. plt.title(pTitle0)
  129. plt.xlabel("h")
  130. plt.ylabel(r'$c_V$')
  131. #plt.legend()
  132. plt.show()
  133. #==============================================================================80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement