Advertisement
Mr-A

Correlation Length Calculation From Susceptibility Grid

Jul 11th, 2022 (edited)
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. import numpy as np
  2. import h5py
  3. from scipy.optimize import curve_fit    
  4.  
  5. def get_max_with_coords(vals2D, lenx):
  6.     maxcoord = (None, None)
  7.     maxval = -999999999999999
  8.     for x in range(lenx):
  9.         for y in range(lenx):
  10.             if vals2D[x, y] > maxval:
  11.                 maxcoord = (x, y)
  12.                 maxval = vals2D[x, y]
  13.     return (maxval, maxcoord)
  14.  
  15. def oz_func_idx(q_idx, A, chi, centerxy_idx, momgrid, momdimx):
  16.     omega = 0
  17.     gamma = 1
  18.    
  19.     qx = np.array([momgrid[int(qidx)][0] for qidx in q_idx])
  20.     qy = np.array([momgrid[int(qidx)][1] for qidx in q_idx])
  21.  
  22.     center = momgrid[centerxy_idx[1]*momdimx + centerxy_idx[0]]
  23.    
  24.     return A/(np.power(qx - center[0], 2) + np.power(qy - center[1], 2) + pow(chi, -2) + abs(omega)/gamma)
  25.  
  26. # for fit_window_in_percent: 0.5 is 50%
  27. def fit_to_lorentzian(momgrid, susc, fit_window_in_percent):
  28.     momdimx = int(np.sqrt(susc.shape[1]))
  29.     #assuming square lattice
  30.     susc2D = (susc[list(f["/susc_func/bgrid"]).index(0), :, 0, 0, 0, 0, 0, 0].reshape(momdimx, momdimx))
  31.     max_susc, found_at = get_max_with_coords(susc2D, momdimx)
  32.     suscs_to_fit = []
  33.     ks_to_fit = []
  34.    
  35.     fit_interval_size = int(momdimx * fit_window_in_percent/2)
  36.    
  37.     kx_interval_vals = []
  38.     ky_interval_vals = []
  39.     for ix in np.linspace(0, 2*fit_interval_size, 2 * fit_interval_size + 1) - fit_interval_size:
  40.         kx_interval_vals.append(int(found_at[0] + ix))
  41.         ky_interval_vals.append(int(found_at[1] + ix))
  42.        
  43.     ks_2d_to_fit = []
  44.     ks_to_fit = []
  45.     #iy = int(found_at[1])
  46.     for ix in kx_interval_vals:
  47.         for iy in ky_interval_vals:
  48.             ks_2d_to_fit.append((ix, iy))
  49.             ks_to_fit.append((ix + 10*momdimx)%momdimx + ((iy + 10*momdimx)%momdimx)*momdimx)
  50.            
  51.     suscs_to_fit = np.array([susc2D[k[0], k[1]] for k in ks_2d_to_fit])
  52.    
  53.     oz_func_to_fit = lambda q_idx, A, chi: oz_func_idx(q_idx, A, chi, found_at, momgrid, momdimx)
  54.    
  55.    
  56.     popt, pcov = curve_fit(oz_func_to_fit, np.array(ks_to_fit), np.array(suscs_to_fit))
  57.     return popt, pcov
  58.  
  59.  
  60. ##########################################
  61. ##########################################
  62. ############## example####################
  63. ##########################################
  64.  
  65. f = h5py.File("dat_U-5_Beta7p5_TP0_Mu0_C5_KDIM16_FFT80_REFINE0_TSTART0_HUB_2D_INTFL_2LOOP_1SELOOP_SU2_ALLSYMM_LOCAL_SELFEN_FLOW_SDE_DIVERGENT.h5","r")
  66. lam = np.array(f["Flow_obs/LAM"])[-1]
  67. # the division by lambda^2 depends on version.. At some later point, this was added in the flowing susceptibility. But if your runs are old (e.g. from 2021, you probably need it)
  68. susc = f["/susc_func/RE_SC"]/lam/lam
  69. momgrid = np.array(f["/susc_func/momgrid"])
  70.  
  71. fit_window = 1.0 #percent of BZ to fit against. It seems like 1.0 (100%) gives the least error (the square root of the (1,1) component of covariance matrix) of the correlation length  
  72.  
  73. ampl_corr_pair, covariance_matrix = fit_to_lorentzian(momgrid, susc, fit_window)
  74.  
  75. print("Amplitude of the lorenzian:", ampl_corr_pair[0])
  76. print("Width of the lorenzian (the correlation length):", ampl_corr_pair[1])
  77. print("Error in fitting the correlation length: ", np.sqrt(covariance_matrix[1][1])) # the error in fitting the correlation length
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement