def call_FPT_bias(k, plin): #power-law bias and zero padding nu = -2 n_pad = len(k) res = np.zeros((8, len(k))) # set the parameters for the power spectrum window and # Fourier coefficient window P_window = np.array([.2, .2]) C_window = .65 fastpt = FASTPT.FASTPT(k, nu, n_pad=n_pad, to_do=['dd_bias']) bias_fpt = fastpt.one_loop_dd_bias(plin, P_window=P_window, C_window=C_window) res[0:7, :] = np.asarray(bias_fpt[0:7]) res[7, 0] = bias_fpt[7] return np.asarray(res)
P = d_extend[:-1, 1] # use if you want to interpolate data #from scipy.interpolate import interp1d #power=interp1d(k,P) #k=np.logspace(np.log10(k[0]),np.log10(k[-1]),3000) #P=power(k) #print d[:,0]-k P_window = np.array([.2, .2]) C_window = .65 n_pad = 1000 # initialize the FASTPT class fastpt = FASTPT.FASTPT(k, to_do=['IA'], low_extrap=-6, high_extrap=4, n_pad=n_pad) t1 = time() IA_E, IA_B = fastpt.IA_tt(P, C_window=C_window) t2 = time() # print('execution time to make IA data'), t2-t1 print('To make a one-loop power spectrum for IA', k.size, ' grid points, using FAST-PT takes ', t2 - t1, 'seconds.') from matplotlib.ticker import ScalarFormatter, FormatStrFormatter #import matplotlib as mpl #mpl.use('Agg') import matplotlib.pyplot as plt
P=d[:,1] # use if you want to interpolate data #from scipy.interpolate import interp1d #power=interp1d(k,P) #k=np.logspace(np.log10(k[0]),np.log10(k[-1]),3000) #P=power(k) #print d[:,0]-k P_window=np.array([.2,.2]) C_window=.65 nu=-2; n_pad=1000 # initialize the FASTPT class fastpt=FASTPT.FASTPT(k,nu,low_extrap=-5,high_extrap=5,n_pad=n_pad) t1=time() P_spt=fastpt.one_loop(P,C_window=C_window) t2=time() print('time'), t2-t1 print('To make a one-loop power spectrum for ', k.size, ' grid points, using FAST-PT takes ', t2-t1, 'seconds.') import matplotlib.pyplot as plt from matplotlib.ticker import ScalarFormatter, FormatStrFormatter fig=plt.figure(figsize=(16,10))
def RG_STS(STS_params, name, k, P, d_lambda, max, n_pad, P_window, C_window): stage, mu, dlambda_CFL = STS_params #save name name = 'RG_STS_' + name print('save name=', name) # The spacing in log(k) Delta = np.log(k[1]) - np.log(k[0]) # This window function tapers the edge of the power spectrum. # It is applied to STS # You could change it here. W = p_window(k, P_window[0], P_window[1]) #W=1 t1 = time.time() # windowed initial power spectrum P_0 = P * W # standard 1-loop parameters nu = -2 fastpt = FASTPT.FASTPT(k, nu=nu, n_pad=n_pad) P_spt = fastpt.one_loop(P_0, C_window=C_window) P_spt = P_0 + P_spt # initial lambda Lambda = 0 d_out = np.zeros((3, k.size + 1)) d_out[0, :] = np.append(Lambda, k) d_out[2, :] = np.append(Lambda, P_0) d_out[1, :] = np.append(Lambda, P_spt) dt_j = np.zeros(stage) for j in range(stage): arg = np.pi * (2 * j - 1) / (2 * stage) dt_j[j] = (dlambda_CFL) * ((1. + mu) - (1. - mu) * np.cos(arg))**(-1.) i = 0 while (Lambda <= max): t_step = 0 for j in range(stage): K = fastpt.one_loop(P, C_window=C_window) K = K * W P = P + dt_j[j] * K t_step = t_step + dt_j[j] d_lambda = t_step # check for failure. if (np.any(np.isnan(P))): print( 'RG flow has failed. It could be that you have not chosen a step size well.' ) print('You may want to consider a smaller step size.') print('iteration number and lambda', i, Lambda) sys.exit() if (np.any(np.isinf(P))): print( 'RG flow has failed. It could be that you have not chosen a step size well.' ) print('You may want to consider a smaller step size.') print('iteration number and lambda', i, Lambda) sys.exit() #update lambda and the iteration i = i + 1 Lambda += d_lambda # break if the next step is already passed lambda max if (Lambda >= max): break #print('lambda', Lambda ) # update data for saving d_update = np.append(Lambda, P) d_out = np.row_stack((d_out, d_update)) # set to True to check at each step if (False): ax = plt.subplot(111) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('k') ax.plot(k, P) ax.plot(k, P_0, color='red') plt.grid() plt.show() ''' Since STS method computes its own Delta Lambda. So,this routine will not end exaclty at max. Therefore, take Euler steps to reach the end ''' last_step = np.absolute(Lambda - d_lambda - max) #print('Lambda', Lambda-d_lambda) #print('last step', last_step) k1 = fastpt.one_loop(P, C_window=C_window) k1 = k1 * W k2 = fastpt.one_loop(k1 * last_step / 2. + P, C_window=C_window) k2 = k2 * W k3 = fastpt.one_loop(k2 * last_step / 2. + P, C_window=C_window) k3 = k3 * W k4 = fastpt.one_loop(k3 * last_step + P, C_window=C_window) k4 = k4 * W # full step P = P + 1 / 6. * (k1 + 2 * k2 + 2 * k3 + k4) * last_step Lambda = Lambda - d_lambda + last_step # update data for saving d_update = np.append(Lambda, P) d_out = np.row_stack((d_out, d_update)) # save the data t2 = time.time() print('time to run seconds', t2 - t1) print('time to run minutes', (t2 - t1) / 60.) print('iteration', i) print('save name', name) print('shape', d_out.shape) np.save(name, d_out) print('number of iterations and output shape', i, d_out.shape) # return last step return P
11: bv^2 (constant ) ''' # example use of FASTPTII k = d[:, 0] P_lin = d[:, 1] import FASTPT from time import time C_window = .65 n_pad = 1000 t1 = time() # initialize the FASTPT class nu = -2 fastpt = FASTPT.FASTPT(k, -2, n_pad=n_pad) P_1loop = fastpt.one_loop(P_lin, C_window=C_window) _, Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig4 = fastpt.P_bias(P_lin, C_window=C_window) t2 = time() print('The time to make density-density type power spectra is ', t2 - t1, ' .') # plot the output import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt from matplotlib.ticker import ScalarFormatter, FormatStrFormatter gs = gridspec.GridSpec(2, 3, height_ratios=[2, 1.25]) fig = plt.figure(figsize=(16, 10))
P=d[:,1] # use if you want to interpolate data #from scipy.interpolate import interp1d #power=interp1d(k,P) #k=np.logspace(np.log10(k[0]),np.log10(k[-1]),3000) #P=power(k) #print d[:,0]-k P_window=np.array([.2,.2]) C_window=.65 nu=-2; n_pad=1000 # initialize the FASTPT class fastpt=FASTPT.FASTPT(k,nu,low_extrap=-5,high_extrap=5,n_pad=n_pad,verbose=True) t1=time() P_spt=fastpt.one_loop(P,C_window=C_window) t2=time() print('time'), t2-t1 print('To make a one-loop power spectrum for ', k.size, ' grid points, using FAST-PT takes ', t2-t1, 'seconds.') import matplotlib.pyplot as plt from matplotlib.ticker import ScalarFormatter, FormatStrFormatter fig=plt.figure(figsize=(16,10))
print('k-points: {}'.format(k.shape[0])) print('kmin = {}; kmax = {}'.format(np.min(k), np.max(k))) print('dk/k = {}'.format(np.max(np.diff(k) / k[:-1]))) P_window = np.array([.2, .2]) C_window = .65 nu = -2 n_pad = 1000 # initialize the FASTPT class log_kmin = -5.0 log_kmax = 3.0 # extrapolating too far increases noise in P_{1loop}, thus in P_gg fastpt = FASTPT.FASTPT(k, nu, low_extrap=log_kmin, high_extrap=log_kmax, n_pad=n_pad, verbose=True) P_lin, Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig4 = fastpt.P_bias( P, C_window=C_window) # **DO NOT** subtract asymptotic terms according to the user manual #Pd2d2 -= 2.0*sig4 #Pd2s2 -= (4./3.)*sig4 #Ps2s2 -= (8./9.)*sig4 # now add P_spt to P_lin *after* computing bias terms P_spt = fastpt.one_loop(P, C_window=C_window) def galaxy_power(b1, b2, bs):
def RG_RK4(name,k,P,d_lambda,max,n_pad,P_window,C_window): x=max/d_lambda-int(max/d_lambda) if ( x !=0.0 ): raise ValueError('You need to send a d_lambda step so that max/d_lambda has no remainder to reach Lambda=max') #save name name='RG_RK4_'+name # The spacing in log(k) Delta=np.log(k[1])-np.log(k[0]) # This window function tapers the edge of the power spectrum. # It is applied to each Runge-Kutta step. # You could change it here. W=p_window(k,P_window[0],P_window[1]) #W=1 t1=time.time() # windowed initial power spectrum P_0=P*W nu=-2 fastpt=FASTPT.FASTPT(k,nu,n_pad=n_pad) P_spt=fastpt.one_loop(P_0,C_window=C_window) P_spt=P_0+P_spt # initial lambda Lambda=0 d_out=np.zeros((3,k.size+1)) d_out[0,:]=np.append(Lambda,k) d_out[2,:]=np.append(Lambda,P_0) d_out[1,:]=np.append(Lambda,P_spt) i=0 while (Lambda <= max): #for i in range(N_steps): k1=fastpt.one_loop(P,C_window=C_window) k1=k1*W k2=fastpt.one_loop(k1*d_lambda/2.+ P,C_window=C_window) k2=k2*W k3=fastpt.one_loop(k2*d_lambda/2. +P,C_window=C_window) k3=k3*W k4=fastpt.one_loop(k3*d_lambda +P,C_window=C_window) k4=k4*W # full step P=P+1/6.*(k1+2*k2+2*k3+k4)*d_lambda # check for failure. if (np.any(np.isnan(P))): print('RG flow has failed. It could be that you have not chosen a step size well.') print('You may want to consider a smaller step size.') sys.exit() if (np.any(np.isinf(P))): print('RG flow has failed. It could be that you have not chosen a step size well.') print('You may want to consider a smaller step size.') sys.exit() #update lambda and the iteration i=i+1 Lambda+=d_lambda #print('lambda', Lambda ) # update data for saving d_update=np.append(Lambda,P) d_out=np.row_stack((d_out,d_update)) # set to True to check at each step if (False): ax=plt.subplot(111) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('k') ax.plot(k,P) ax.plot(k,P_0, color='red') plt.grid() plt.show() # save the data t2=time.time() print('time to run seconds', t2-t1) print('time to run minutes', (t2-t1)/60.) print('number of iterations and output shape', i, d_out.shape) np.save(name,d_out) # return last step return P
def RG_RK4_filt(name, k, P, d_lambda, max, n_pad, P_window, C_window): x = max / d_lambda - int(max / d_lambda) if (x != 0.0): raise ValueError( 'You need to send a d_lambda step so that max/d_lambda has no remainder to reach Lambda=max' ) #save name name = 'RG_RK4_filt_' + name # The spacing in log(k) Delta = np.log(k[1]) - np.log(k[0]) # This window function tapers the edge of the power spectrum. # It is applied to each Runge-Kutta step. # You could change it here. W = p_window(k, P_window[0], P_window[1]) #W=1 t1 = time.time() # windowed initial power spectrum P_0 = P * W nu = -2 fastpt = FASTPT.FASTPT(k, nu, n_pad=n_pad) P_spt = fastpt.one_loop(P_0, C_window=C_window) P_spt = P_0 + P_spt # initial lambda Lambda = 0 d_out = np.zeros((3, k.size + 1)) d_out[0, 1:] = k d_out[2, 1:] = P_0 d_out[1, 1:] = P_spt # filtering specific k_start = 1 k_end = 5 id1 = np.where(k > k_end)[0] id2 = np.where(k <= k_end)[0] id3 = np.where(k > k_start)[0] #id4=np.where( k <= k_start)[0] id4 = np.where((k > k_start) & (k <= k_end))[0] order = 6 wn = .1 B, A = butter(order, wn, btype='low', analog=False) theta = np.linspace(1, 0, id4.size) W_fad = theta - 1 / 2. / np.pi * np.sin(2 * np.pi * theta) filt_pad = id3.size # end filtering specific def filt_zp(k, P_filt): def zero_phase(sig): sig = np.pad(sig, (filt_pad, filt_pad), 'constant', constant_values=(0, 0)) #zi=lfilter_zi(B,A) #x,_=lfilter(B,A,sig, zi=zi*sig[0]) x = lfilter(B, A, sig) #y,_=lfilter(B,A,x,zi=zi*x[0]) y = lfilter(B, A, x[::-1]) y = y[::-1] #return y return y[filt_pad:id3.size + filt_pad] P_smoothed = zero_phase(P_filt[id3]) P_patch = P_filt[id4] * W_fad P_filt[id3] = P_smoothed P_filt[id4] = P_patch + (1 - W_fad) * P_filt[id4] return P_filt i = 0 while Lambda <= max: k1 = fastpt.one_loop(P, C_window=C_window) k1 = filt_zp(k, k1) k2 = fastpt.one_loop(k1 * d_lambda / 2. + P, C_window=C_window) k2 = filt_zp(k, k2) k3 = fastpt.one_loop(k2 * d_lambda / 2. + P, C_window=C_window) k3 = filt_zp(k, k3) k4 = fastpt.one_loop(k3 * d_lambda + P, C_window=C_window) k4 = filt_zp(k, k4) # full Runge-Kutta step P = P + 1 / 6. * (k1 + 2 * k2 + 2 * k3 + k4) * d_lambda # check for failure. if (np.any(np.isnan(P))): print( 'RG flow has failed. It could be that you have not chosen a step size well.' ) print('You may want to consider a smaller step size.') print('Failure at lambda =', Lambda) sys.exit() if (np.any(np.isinf(P))): print( 'RG flow has failed. It could be that you have not chosen a step size well.' ) print('You may want to consider a smaller step size.') print('Failure at lambda =', Lambda) sys.exit() #update lambda and the iteration i = i + 1 Lambda += d_lambda # update data for saving d_update = np.append(Lambda, P) d_out = np.row_stack((d_out, d_update)) if (False): ax = plt.subplot(111) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('k') ax.plot(k, P) ax.plot(k, P_0, color='red') plt.grid() plt.show() # save the data t2 = time.time() print('time to run seconds', t2 - t1) print('time to run minutes', (t2 - t1) / 60.) print('number of iterations and output shape', i, d_out.shape) np.save(name, d_out) return P