def get_density_profiles(mainpath, desired_list): #determine the location of the period datalist = [] path_maindensity = os.path.join(mainpath, 'density.dat') data = np.loadtxt(path_maindensity) fAmin = np.min(data[:, 1]) cs = CubicSpline(data[:, 0], data[:, 1] - fAmin) x = np.linspace(np.min(data[:, 0]), np.max(data[:, 0]), 1000) roots = cs.roots() loc = np.where((roots > np.min(data[:, 0])) & (roots < np.max(data[:, 0])))[0] domain_min = np.min(roots[loc[1]]) domain_max = np.max(roots[loc[3]]) loc = np.where((data[:, 0] > domain_min) & (data[:, 0] < domain_max))[0] data_domain = data[loc, :] data_domain[:, 0] = data_domain[:, 0] - np.min(data_domain[:, 0]) cs = CubicSpline(data_domain[:, 0] / np.max(data_domain[:, 0]), data_domain[:, 1]) integral = 1 #cs.integrate(0,1) x = np.linspace(0, 1, 10000) y = cs(x) / integral datalist.append(np.vstack((x, y)).transpose()) path_data = os.path.join(mainpath, 'density_chain0.dat') all_data = np.loadtxt(path_data) loc = np.where((all_data[:, 0] > domain_min) & (all_data[:, 0] < domain_max))[0] data = all_data[loc, :] data[:, 0] = data[:, 0] - np.min(data[:, 0]) shape = np.shape(data) for i in range(1, shape[1]): if i in desired_list: cs = CubicSpline(data[:, 0] / np.max(data[:, 0]), data[:, i]) integral = cs.integrate(0, 1) x = np.linspace(0, 1, 10000) y = cs(x) / integral datalist.append(np.vstack((x, y)).transpose()) return datalist
color = ['tab:blue','tab:orange','tab:red','tab:olive','tab:brown'] path = '/home/tquah/Projects/positions/backboneposition_LAM_lowchi/Bottlebrush/density_chain0.dat' data = np.loadtxt(path) shape = np.shape(data) plt.close('all') plt.figure() for i in range(1,shape[1]): if i<shape[1]-2: cs = CubicSpline(data[:,0]/np.max(data[:,0]),data[:,i]) integral = cs.integrate(0,1) x = np.linspace(0,1,1000) y = cs(x)/integral plt.plot(x,y,label = str(i)) # plt.legend() path = '/home/tquah/Projects/positions/backboneposition_LAM_lowchi/linear/density_chain0.dat' data = np.loadtxt(path) shape = np.shape(data) # plt.close('all') # plt.figure() for i in range(1,shape[1]): if i<shape[1]:
# print(integrand1(E1)) # plt.plot(E1,integrand1(E1),color='r') # plt.scatter(E1,a0_final*np.exp(-11.604*E1/T)*E1,alpha=.01,c='b') # plt.yscale('log') # plt.show() # intg = integrate.romberg(integrand1,min(energyList),max(energyList),divmax=40) # intg1 = integrate.quad(integrand1,min(energyList),max(energyList),limit=200) intg2 = 0 for ind,elem in enumerate(E1): if ind < len(E1): intg2 += a0_final[ind]*np.exp(-11.604*E1[ind]/T)*E1[ind]*dE intg3 = integrand1.integrate(min(E1),max(E1)) # intg4 = integrate.cumtrapz(a0_final*np.exp(-11.604*E1/T)*E1,E1)[-1] # intg5 = integrate.trapz(a0_final*np.exp(-11.604*E1/T)*E1,E1) intg6 = integrate.simps(a0_final*np.exp(-11.604*E1/T)*E1,E1) intg7 = np.trapz(a0_final*np.exp(-11.604*E1/T)*E1,E1) # rate = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg # rate1 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg1[0] rate2 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg2 rate3 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg3 # rate4 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg4 # rate5 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg5 rate6 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg6 rate7 = rxnRateCONST * mu**(-.5) * T**(-1.5) * intg7
class Density: alt_models = { 'photon': 'fit_models/Alt_photon_pol3_model.txt', 'muon': 'fit_models/Alt_muon_pol3_model.txt' } eff_photon = 0.0001 eff_muon = 0.5 __xmin__ = 1e-24 # km __xmax__ = 1e4 # km def __init__(self): self.alt = R.TF1('Alt_muon', Alt.fitexpr, Density.__xmin__, Density.__xmax__) def set_shower(self, mass_number, energy_eV, altitude_km): self.A = mass_number self.E = energy_eV self.H = altitude_km x = np.logspace(np.log10(Density.__xmin__), np.log10(Density.__xmax__), 10000) for i in range(4): self.alt.SetParameter( i, Coefficients.get(i, Density.alt_models['photon'], self.A, self.E, self.H)) y1 = np.zeros(x.size) y2 = np.zeros(x.size) for i in range(x.size): y1[i] = self.alt.Eval(x[i]) y2[i] = x[i] * y1[i] y2 = 2. * np.pi * 1e10 * y2 self.photon = CubicSpline(x, y1) self.photon2 = CubicSpline(x, y2) y3 = np.zeros(x.size) for i in range(x.size): y3[i] = self.photon2.integrate(0., x[i]) self.N_photons = y3[-1] y3 = y3 / y3[-1] cut = -1 while (y3[cut] == 1.): cut -= 1 cut += 2 self.photon_cdf = CubicSpline(y3[:cut], x[:cut]) for i in range(4): self.alt.SetParameter( i, Coefficients.get(i, Density.alt_models['muon'], self.A, self.E, self.H)) y1 = np.zeros(x.size) y2 = np.zeros(x.size) for i in range(x.size): y1[i] = self.alt.Eval(x[i]) y2[i] = x[i] * y1[i] y2 = 2. * np.pi * 1e10 * y2 self.muon = CubicSpline(x, y1) self.muon2 = CubicSpline(x, y2) y3 = np.zeros(x.size) for i in range(x.size): y3[i] = self.muon2.integrate(0., x[i]) self.N_muons = y3[-1] y3 = y3 / y3[-1] cut = -1 while (y3[cut] == 1.): cut -= 1 cut += 2 self.muon_cdf = CubicSpline(y3[:cut], x[:cut])
def densm(alt, d0, xm, tz, zn3, tn3, tgn3, zn2, tn2, tgn2, gsurf, re): rgas = 831.4 densm_tmp = d0 tz_tmp = tz mn3, mn2 = len(zn3), len(zn2) if alt > zn2[0]: if xm == 0: densm_tmp = tz return densm_tmp, tz_tmp else: densm_tmp = d0 return densm_tmp, tz_tmp # stratosphere/mesosphere temperature if alt > zn2[mn2 - 1]: z = alt else: z = zn2[mn2 - 1] mn = mn2 xs, ys = [np.zeros(mn) for i in range(2)] z1, z2 = zn2[0], zn2[mn - 1] t1, t2 = tn2[0], tn2[mn - 1] zg, zgdif = zeta(z, z1, re), zeta(z2, z1, re) # set up spline nodes for k in range(mn): xs[k] = zeta(zn2[k], z1, re) / zgdif ys[k] = 1 / tn2[k] yd1 = -tgn2[0] / t1**2 * zgdif yd2 = -tgn2[1] / t2**2 * zgdif * ((re + z2) / (re + z1))**2 # calculate spline coefficients cs = CubicSpline(xs, ys, bc_type=((1, yd1), (1, yd2))) x = zg / zgdif y = cs(x) # temperature at altitude tz_tmp = 1 / y if xm != 0: # calaculate stratosphere/mesospehere density glb = gsurf / (1 + z1 / re)**2 gamm = xm * glb * zgdif / rgas # Integrate temperature profile yi = cs.integrate(xs[0], x) expl = gamm * yi if expl > 50: expl = 50 # Density at altitude densm_tmp = densm_tmp * (t1 / tz_tmp) * np.exp(-expl) if alt > zn3[0]: if xm == 0: densm_tmp = tz_tmp return densm_tmp, tz_tmp else: return densm_tmp, tz_tmp # troposhere/stratosphere temperature z = alt mn = mn3 xs, ys = [np.zeros(mn) for i in range(2)] z1, z2 = zn3[0], zn3[mn - 1] t1, t2 = tn3[0], tn3[mn - 1] zg, zgdif = zeta(z, z1, re), zeta(z2, z1, re) # set up spline nodes for k in range(mn): xs[k] = zeta(zn3[k], z1, re) / zgdif ys[k] = 1 / tn3[k] yd1 = -tgn3[0] / t1**2 * zgdif yd2 = -tgn3[1] / t2**2 * zgdif * ((re + z2) / (re + z1))**2 # calculate spline coefficients cs = CubicSpline(xs, ys, bc_type=((1, yd1), (1, yd2))) x = zg / zgdif y = cs(x) # temperature at altitude tz_tmp = 1 / y if xm != 0: # calaculate tropospheric / stratosphere density glb = gsurf / (1 + z1 / re)**2 gamm = xm * glb * zgdif / rgas # Integrate temperature profile yi = cs.integrate(xs[0], x) expl = gamm * yi if expl > 50: expl = 50 # Density at altitude densm_tmp = densm_tmp * (t1 / tz_tmp) * np.exp(-expl) if xm == 0: densm_tmp = tz_tmp return densm_tmp, tz_tmp else: return densm_tmp, tz_tmp
def densu(alt, dlb, tinf, tlb, xm, alpha, tz, zlb, s2, zn1, tn1, tgn1, gsurf, re): ''' Calculate Temperature and Density Profiles for MSIS models ''' rgas = 831.4 densu_tmp = 1 mn1 = len(zn1) # joining altitudes of Bates and spline za = zn1[0] if alt > za: z = alt else: z = za # geopotential altitude difference from ZLB zg2 = zeta(z, zlb, re) # Bates temperature tt = tinf - (tinf - tlb) * np.exp(-s2 * zg2) ta = tz = tt densu_tmp = tz_tmp = tz if alt < za: # calculate temperature below ZA # temperature gradient at ZA from Bates profile dta = (tinf - ta) * s2 * ((re + zlb) / (re + za))**2 tgn1[0], tn1[0] = dta, ta if alt > zn1[mn1 - 1]: z = alt else: z = zn1[mn1 - 1] mn = mn1 xs, ys = [np.zeros(mn) for i in range(2)] z1, z2 = zn1[0], zn1[mn - 1] t1, t2 = tn1[0], tn1[mn - 1] # geopotental difference from z1 zg, zgdif = zeta(z, z1, re), zeta(z2, z1, re) # set up spline nodes for k in range(mn): xs[k] = zeta(zn1[k], z1, re) / zgdif ys[k] = 1 / tn1[k] # end node derivatives yd1 = -tgn1[0] / t1**2 * zgdif yd2 = -tgn1[1] / t2**2 * zgdif * ((re + z2) / (re + z1))**2 # calculate spline coefficients cs = CubicSpline(xs, ys, bc_type=((1, yd1), (1, yd2))) x = zg / zgdif y = cs(x) # temperature at altitude tz_tmp = 1 / y densu_tmp = tz_tmp if xm == 0: return densu_tmp, tz_tmp # calculate density above za glb = gsurf / (1 + zlb / re)**2 gamma = xm * glb / (s2 * rgas * tinf) expl = np.exp(-s2 * gamma * zg2) if expl > 50: expl = 50 if tt <= 0: expl = 50 # density at altitude densa = dlb * (tlb / tt)**(1 + alpha + gamma) * expl densu_tmp = densa if alt >= za: return densu_tmp, tz_tmp # calculate density below za glb = gsurf / (1 + z1 / re)**2 gamm = xm * glb * zgdif / rgas # integrate spline temperatures yi = cs.integrate(xs[0], x) expl = gamm * yi if expl > 50: expl = 50 if tz_tmp <= 0: expl = 50 # density at altitude densu_tmp = densu_tmp * (t1 / tz_tmp)**(1 + alpha) * np.exp(-expl) return densu_tmp, tz_tmp
#!/usr/bin/env python3 from scipy.interpolate import CubicSpline import numpy as np x = np.arange(21) * np.pi / 5.0 y = np.sin(x) y[0] = 0.01 y[-1] = y[0] xq = np.linspace(min(x), max(x), 101) cs_not_a_knot = CubicSpline(x, y, bc_type='not-a-knot') cs_clamped = CubicSpline(x, y, bc_type='clamped') cs_natural = CubicSpline(x, y, bc_type='natural') cs_periodic = CubicSpline(x, y, bc_type='periodic') yq_knot = cs_not_a_knot(xq) yq_clamped = cs_clamped(xq) yq_natural = cs_natural(xq) yq_period = cs_periodic(xq) ofl = open('cubicSplineInterpolation.txt', 'w') for i in range(len(xq)): ofl.write("%e, %e, %e, %e, %e\n" % (xq[i], yq_knot[i], yq_clamped[i], yq_natural[i], yq_period[i])) ofl.close() print(cs_not_a_knot.integrate(0, 1)) print(cs_natural.integrate(max(x), 0)) print(cs_clamped.integrate(4.08, 6)) print(cs_periodic.integrate(3.99, max(x))) print(cs_periodic.integrate(3.007729, 2.541008))