import random
from math import floor
def find_idx_from_table(x_in, nx, x):
x_value = x_in
i1 = 0
i2 = nx-1
xdif1 = x[i1] - x_value
xdif2 = x[i2] - x_value
if(xdif1 * xdif2 < 0):
# Use bisection to find the nearest cell
while True:
im = floor( (i1 + i2) / 2 )
xdifm = x[im] - x_value
if(xdif1 * xdifm < 0):
i2 = im
else:
i1 = im
xdif1 = xdifm
if(i2 - i1 == 1):
break
# Interpolate in x
fx = (x_value - x[i1]) / (x[i2] - x[i1])
else:
# if(warning and rank == 0):
# print('*** WARNING ***')
# print('Argument to "find_idx_from_table" outside the range of the table.')
# print('Using truncated value. No more warnings will be issued.')
# warning = False
if(xdif1 >= 0):
fx = 0.0
else:
fx = 1.0
return [fx,i1,i2]
def find_value_from_table_1d(x_in, nx, x):
fx,i1,i2 = find_idx_from_table(x_in, nx, x[:,0])
return (1.0 - fx) * x[i1,1] + fx * x[i2,1]
def find_value_from_table_2d(x_in, p_value, nx,ny, x,y, values):
value_interp = np.array([0.0 for i in range(3)])
fx, ix1, ix2 = find_idx_from_table(x_in,nx,x)
fy, iy1, iy2 = find_idx_from_table(p_value,ny,values[ix1,:])
value_interp[0] = (1.0 - fy) * y[iy1] + fy * y[iy2]
fy, iy1, iy2 = find_idx_from_table(p_value,ny,values[ix2,:])
value_interp[1] = (1.0 - fy) * y[iy1] + fy * y[iy2]
value_interp[2] = (1.0 - fx) * value_interp[0] + fx * value_interp[1]
return value_interp[2]
drdw = np.loadtxt('./c0.table')
print(drdw.shape)
ETA = np.linspace(-3,2,100)
OMEGA = np.linspace(-4,-0.00001,500)
eta_log = 1.
p_values = np.array([random.random() for i in range(100000)])
w_check = np.array([0.0 for i in range(100000)])
for i in range(100000):
w_check[i] = find_value_from_table_2d(eta_log, p_values[i], 100, 500, ETA, OMEGA, drdw)
H, edges = np.histogram(10**w_check,bins=np.linspace(0,1.0,100))
drdw_ana = np.array([odWdw(w,10**eta_log) for w in 10**OMEGA])
plt.plot(10**OMEGA,drdw_ana,'k-',label='analytical')
plt.plot(edges[:-1],H/100*np.pi,'r--',label='MC sampling')
plt.yscale('log')
plt.xlabel(r'$\omega$')
plt.ylabel(r'$d^2W/d\omega dt$')
plt.legend()
plt.show()