Files
katrin-plasma/src/mcelectron-analysis/functions/archive/calc_histogram.py
T

121 lines
4.0 KiB
Python

import pandas as pd
import numpy as np
from h5_loadpart import load_part
# Histograms
me_eV = 0.511e6
c_ms = 3e8
A_m2 = np.pi*0.045**2
lower_edge = 1e-5
upper_edge = 2e4
def calc_histogram(dfs_part, simulation_time_s, number_points = 200, binning = 'log'):
if type(dfs_part) == dict:
if type(dfs_part[list(dfs_part.keys())[0]]) == dict:
hists = dict()
for species in dfs_part:
dfs_z = dfs_part[species]
z_dict = dict()
for z in dfs_z:
df = dfs_z[z]
hist = calc_histogram0(df, simulation_time_s, number_points, binning)
z_dict.update({z:hist})
hists.update({species:z_dict})
return hists
else:
hists = dict()
for z in dfs_part:
df = dfs_part[z]
hist = calc_histogram0(df, simulation_time_s, number_points, binning)
hists.update({z:hist})
return hists
else:
return calc_histogram0(dfs_part, simulation_time_s, number_points, binning)
def calc_histogram0(df, simulation_time_s, number_points = 200, binning = 'log'):
if binning == 'log':
bins = 10**np.linspace(np.log10(lower_edge), np.log10(upper_edge), number_points)
E_pot = -1.5
def calc_bin_mid(bin_edges):
return np.exp ( (np.log(bin_edges[1:]) + np.log(bin_edges[:-1]))/2)
if binning == 'linear':
bins = np.linspace(lower_edge, upper_edge, number_points)
E_pot = -0.5
def calc_bin_mid(bin_edges):
return (bin_edges[1:] + bin_edges[:-1])/2
dNdEscale, bin_edges = np.histogram(df.Ekin_eV, bins=bins)
bin_mids = calc_bin_mid(bin_edges)
const1 = np.sqrt(me_eV/2)/A_m2/simulation_time_s/c_ms
dn_dE_m = const1*pow(bin_mids, E_pot)*dNdEscale
res = pd.DataFrame({'lower_edge_eV' : bin_edges[0:-1], 'upper_edge_eV' : bin_edges[1:], 'bin_mid_eV' : bin_mids, 'dn_dE_m3eV' : dn_dE_m})
return res
def calc_simulation_time_s (number_decays, max_density_mid_m3 = 1e21):
r_m = 0.045
l_m = 10
decay_const_s = np.log(2)/3.88524e+8
V_m3 = np.pi*r_m**2*l_m
number_molecules = max_density_mid_m3*V_m3/2
activity_s = number_molecules*decay_const_s
return (number_decays/activity_s)
def plot_hist(hist, ax):
if type(hist) == dict:
for z in hist:
ax.plot(hist[z].bin_mid_eV, hist[z].dn_dE_m3eV, label = z)
ax.legend(loc = 'best')
else:
ax.plot(hist.bin_mid_eV, hist.dn_dE_m3eV)
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('energy (eV)')
ax.set_ylabel(r'density ($m^{-3}$)')
return
def plot_fermi(ax, scale):
me_eV = 0.511e6
decay_constant = 1.2072343416696292e-47
end_point_eV = 18.574e3
Z = 2
normation = 1.2078059173513103e-24
def beta_dist(E_eV):
if E_eV > end_point_eV:
return 0.
gamma = 1+E_eV/me_eV
beta = np.sqrt(1 - pow(gamma,-2))
eta =Z/beta/137.
fermi_function = 2*np.pi*eta/(1-np.exp(-2*np.pi*eta));
p_eV = np.sqrt(E_eV*E_eV + 2 * E_eV * me_eV)
return decay_constant*fermi_function*p_eV*(E_eV+me_eV)*pow(end_point_eV-E_eV,2)/normation
beta_dist = np.vectorize(beta_dist)
E_eV = bins = 10**np.linspace(np.log10(1e1), np.log10(2e4), 1000)
n = beta_dist(E_eV)*scale
ax.plot(E_eV, n, label = 'beta decay')
return
def calc_generations(df):
id_string = 'Id'
pid_string = 'ParentId'
level_string = 'Level'
Id_data = df[[id_string,pid_string]]
Id_data = Id_data.drop_duplicates()
Id_data = Id_data.reset_index(drop=True)
Id_data.insert(len(Id_data.columns),level_string,-1)
Id_data.loc[Id_data[id_string] == Id_data[pid_string],level_string] = 0
level = 0
while not( Id_data[Id_data[level_string] == level].empty):
parentsatlevel = Id_data[Id_data[level_string] == level][id_string]
Id_data.loc[(Id_data[pid_string].isin(parentsatlevel)) & (Id_data[pid_string] != Id_data[id_string]),level_string] = level + 1
level = level + 1
return Id_data