From c5a5218b5a2e04d0bd7081e1548ebe60375973b4 Mon Sep 17 00:00:00 2001 From: Jonas Kellerer Date: Tue, 6 Jul 2021 09:53:21 +0200 Subject: [PATCH] analyisis scripts --- .../currents/cross_velocity_to_csv.py | 34 +++++++++ .../currents/current_in_DeltaE.py | 38 ++++++++++ .../currents/current_to_csv.py | 42 +++++++++++ .../currents/current_to_h5.py | 16 +++-- .../currents/plot_current_vs_E.py | 58 +++++++++++++++ .../currents/plot_current_vs_r.py | 8 +-- .../currents/plot_current_vs_z.py | 8 +-- .../currents/velocity_to_csv.py | 53 ++++++++++++++ .../currents/velocity_to_h5.py | 62 ++++++++++++++++ .../densities/density_in_DeltaE.py | 38 ++++++++++ .../densities/density_to_csv.py | 70 +++++++++++++++++++ .../densities/density_to_h5.py | 24 ++++++- .../densities/plot_all_density_vs_r.py | 8 +-- .../densities/plot_all_density_vs_z.py | 8 +-- .../plot_electron_density_vs_energy.py | 39 +++++------ .../densities/plot_electron_density_vs_z.py | 4 +- .../densities/spectrum_to_csv.py | 46 ++++++++++++ .../densities/spectrum_to_h5.py | 61 ++++++++++++++++ .../functions/calculate_density_from_karl.py | 1 - 19 files changed, 568 insertions(+), 50 deletions(-) create mode 100644 src/tools/analysis_jkellerer/currents/cross_velocity_to_csv.py create mode 100644 src/tools/analysis_jkellerer/currents/current_in_DeltaE.py create mode 100644 src/tools/analysis_jkellerer/currents/current_to_csv.py create mode 100644 src/tools/analysis_jkellerer/currents/plot_current_vs_E.py create mode 100644 src/tools/analysis_jkellerer/currents/velocity_to_csv.py create mode 100644 src/tools/analysis_jkellerer/currents/velocity_to_h5.py create mode 100644 src/tools/analysis_jkellerer/densities/density_in_DeltaE.py create mode 100644 src/tools/analysis_jkellerer/densities/density_to_csv.py create mode 100644 src/tools/analysis_jkellerer/densities/spectrum_to_csv.py create mode 100644 src/tools/analysis_jkellerer/densities/spectrum_to_h5.py diff --git a/src/tools/analysis_jkellerer/currents/cross_velocity_to_csv.py b/src/tools/analysis_jkellerer/currents/cross_velocity_to_csv.py new file mode 100644 index 0000000..9a582c2 --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/cross_velocity_to_csv.py @@ -0,0 +1,34 @@ +import numpy as np + +from functions.calculate_currents_from_karl import * +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def crossVelocityToCsv( + filename_densities, + filename_currents, + filename_output, + key_density, + key_current): + + jz = np.sum(particleCurrentZ(filename_currents, filename_densities, key_current),2) + + total_density = getDensities(filename_densities, key_density) + density = np.transpose(np.sum(total_density,3)[:,:,0]) + + velocity = np.zeros_like(jz) + nr = velocity.shape[0] + nz = velocity.shape[1] + + velocity[:,0] = jz[:,0]/density[:,0] + velocity[:,nz-1] = jz[:,nz-1]/density[:,nz-2] + + velocity[:,1:nz-1] = 2*jz[:,1:nz-1]/(density[:,0:nz-2] + density[:,1:nz-1]) + si_to_cgs = 1E2 + + np.savetxt( + filename_output, + np.transpose(velocity*si_to_cgs), + delimiter=",", + header="velocity (cm/s), r to left, z to bottom") diff --git a/src/tools/analysis_jkellerer/currents/current_in_DeltaE.py b/src/tools/analysis_jkellerer/currents/current_in_DeltaE.py new file mode 100644 index 0000000..5d22ef4 --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/current_in_DeltaE.py @@ -0,0 +1,38 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +import functions.calculate_currents_from_karl as cc +import functions.plot_standards as pst + +def longitudinalCurrentInDeltaE( + filename_density, + filename_current, + Emin, Emax, + z_indexs): + + e_dict = {"key" : "/e-", "charge": -1.602E-19} + #T3_dict = {"key" : "/T3+", "linestyle": '--', "label": "T$_3^+$", "charge": 1.602E-19} + #T2_dict = {"key" : "/T2+", "linestyle": '--', "label": "T$_2^+$", "charge": 1.602E-19} + #T_dict = {"key" : "/T+", "linestyle": '--', "label": "T$^+$", "charge": 1.602E-19} + dicts = [e_dict] + + pst.setRcParams() + prop_cycle = plt.rcParams['axes.prop_cycle'] + colors = prop_cycle.by_key()['color'] + + for d in dicts: + E_bins = cc.binBordersE(filename_current, d["key"]) + z_bins = cc.binBordersZ(filename_current, d["key"]) + + E_bin_min = np.abs((E_bins-Emin)).argmin() + E_bin_max = np.abs((E_bins-Emax)).argmin() + + I_z = cc.absoluteCurrentZ(filename_current, filename_density, d["key"]) + A = (4.5E-2)**2*np.pi + for zi in z_indexs: + j_E = np.sum(np.sum(I_z,axis=0)[zi,E_bin_min:E_bin_max])/A + print("j_E: {:e} 1/(m^2*s)".format(j_E)) \ No newline at end of file diff --git a/src/tools/analysis_jkellerer/currents/current_to_csv.py b/src/tools/analysis_jkellerer/currents/current_to_csv.py new file mode 100644 index 0000000..8bb26ee --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/current_to_csv.py @@ -0,0 +1,42 @@ +import numpy as np + +from functions.calculate_currents_from_karl import * +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def currentToCsv(filename_densities, filename_currents, filename_output_prefix): + + si_to_cgs = 1E-4 + e_statC = 4.80320425E-10 + + T3 = { + "key": "/T3+", + "filename_suffix": "_T3.csv", + "header": "current T3+ (statC/cm^-2 s^-1), r to left, z to bottom"} + e = { + "key": "/e-", + "filename_suffix": "_e.csv", + "header": "current e- (statC/cm^-2 s^-1), r to left, z to bottom"} + T2 = { + "key": "/T2+", + "filename_suffix": "_T2.csv", + "header": "current T2+ (statC/cm^-2 s^-1), r to left, z to bottom"} + T = { + "key": "/T+", + "filename_suffix": "_T.csv", + "header": "current T+ (statC/cm^-2 s^-1), r to left, z to bottom"} + + dicts = [e, T, T2, T3] + + for d in dicts: + total_current = particleCurrentZ(filename_currents, filename_densities, d["key"]) + current = np.sum(total_current,2)*si_to_cgs*e_statC + + filename_output = filename_output_prefix + d["filename_suffix"] + np.savetxt( + filename_output, + np.transpose(current), + delimiter=",", + header=d["header"]) + diff --git a/src/tools/analysis_jkellerer/currents/current_to_h5.py b/src/tools/analysis_jkellerer/currents/current_to_h5.py index f5fcc16..5356748 100644 --- a/src/tools/analysis_jkellerer/currents/current_to_h5.py +++ b/src/tools/analysis_jkellerer/currents/current_to_h5.py @@ -30,26 +30,28 @@ def currentToH5(filename_densities, filename_currents, filename_output): total_current_T3 = particleCurrentZ(filename_currents, filename_densities, keys[3]) current_T3 = np.sum(total_current_T3,2) + si_to_cgs = 1E-4 + f = h5py.File(filename_output, 'a') key_electrons = "Current/electrons" if key_electrons in f: del f[key_electrons] - f.create_dataset(key_electrons, data=current_e, dtype=float) + f.create_dataset(key_electrons, data=current_e*si_to_cgs, dtype=float) - key_T = "Current/T+" + key_T = "Current/T" if key_T in f: del f[key_T] - f.create_dataset(key_T, data=current_T, dtype=float) + f.create_dataset(key_T, data=current_T*si_to_cgs, dtype=float) - key_T2 = "Current/T2+" + key_T2 = "Current/T2" if key_T2 in f: del f[key_T2] - f.create_dataset(key_T2, data=current_T2, dtype=float) + f.create_dataset(key_T2, data=current_T2*si_to_cgs, dtype=float) - key_T3 = "Current/T3+" + key_T3 = "Current/T3" if key_T3 in f: del f[key_T3] - f.create_dataset(key_T3, data=current_T3, dtype=float) + f.create_dataset(key_T3, data=current_T3*si_to_cgs, dtype=float) f.close() diff --git a/src/tools/analysis_jkellerer/currents/plot_current_vs_E.py b/src/tools/analysis_jkellerer/currents/plot_current_vs_E.py new file mode 100644 index 0000000..b2539c2 --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/plot_current_vs_E.py @@ -0,0 +1,58 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +import functions.calculate_currents_from_karl as cc +import functions.plot_standards as pst + +def makeBarPlotData(data, mins, maxs): + plot_bins = np.dstack((mins,maxs)).flatten() + plot_datas = np.dstack((data,data)).flatten() + return plot_bins, plot_datas + +def plotCurrentVsE( + filename_density, + filename_current, + z_indexs, + r_index, + filename_output): + + e_dict = {"key" : "/e-", "linestyle": '-', "label": "e$^-$", "charge": -1.602E-19} + T3_dict = {"key" : "/T3+", "linestyle": '--', "label": "T$_3^+$", "charge": 1.602E-19} + T2_dict = {"key" : "/T2+", "linestyle": '--', "label": "T$_2^+$", "charge": 1.602E-19} + T_dict = {"key" : "/T+", "linestyle": '--', "label": "T$^+$", "charge": 1.602E-19} + dicts = [e_dict] + + pst.setRcParams() + prop_cycle = plt.rcParams['axes.prop_cycle'] + colors = prop_cycle.by_key()['color'] + + fig, ax = plt.subplots(figsize=pst.set_size('thesis')) + + for d in dicts: + E_bins = cc.binBordersE(filename_current, d["key"]) + z_bins = cc.binBordersZ(filename_current, d["key"]) + j_z = cc.particleCurrentZ(filename_current, filename_density, d["key"]) + + I_z = cc.absoluteCurrentZ(filename_current, filename_density, d["key"]) + A = (4.5E-2)**2*np.pi + + for zi in z_indexs: + plot_data = np.sum(I_z[:,zi,:],axis=0)/A + ax.plot(E_bins, np.abs(plot_data)[:-1], label=str(z_bins[zi])) + + ax.set_xscale("log") + ax.set_yscale("log") + ax.set_xlabel(r"Energy $E$ (eV)") + ax.set_ylabel(r"Current $j_z$ (1/cm$^2$s)") + + ax.set_facecolor('#e7e7e7ff') + ax.grid(color='white') + + ax.legend(loc="lower right") + + plt.show() + fig.savefig(filename_output, format='pdf', bbox_inches='tight') \ No newline at end of file diff --git a/src/tools/analysis_jkellerer/currents/plot_current_vs_r.py b/src/tools/analysis_jkellerer/currents/plot_current_vs_r.py index 2fdec9b..323e23e 100644 --- a/src/tools/analysis_jkellerer/currents/plot_current_vs_r.py +++ b/src/tools/analysis_jkellerer/currents/plot_current_vs_r.py @@ -31,8 +31,8 @@ def plotCurrentRVsR(filename_current, filename_density, output_folder, z_pos): ax.plot(plot_x*100, plot_data*1E-4, linestyle=d["linestyle"], marker=d["marker"], label=d["label"]) - ax.set_xlabel(r"Radius $r$ [cm]") - ax.set_ylabel(r"Particle Current $j_r$ [$cm^{-2}s^{-1}$]") + ax.set_xlabel(r"Radius $r$ (cm)") + ax.set_ylabel(r"Particle Current $j_r$ ($cm^{-2}s^{-1}$)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') @@ -69,8 +69,8 @@ def plotCurrentZVsR(filename_current, filename_density, output_folder, z_pos): ax.plot(plot_bins, plot_datas, linestyle=d["linestyle"], label=d["label"]) - ax.set_xlabel(r"Radius $r$ [m]") - ax.set_ylabel(r"Particle Current $j_z$ [$m^{-2}s^{-1}$]") + ax.set_xlabel(r"Radius $r$ (m)") + ax.set_ylabel(r"Particle Current $j_z$ ($m^{-2}s^{-1}$)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') diff --git a/src/tools/analysis_jkellerer/currents/plot_current_vs_z.py b/src/tools/analysis_jkellerer/currents/plot_current_vs_z.py index ef3cf64..907bf4f 100644 --- a/src/tools/analysis_jkellerer/currents/plot_current_vs_z.py +++ b/src/tools/analysis_jkellerer/currents/plot_current_vs_z.py @@ -31,8 +31,8 @@ def plotTubeCurrentVsZ(filename_current, filename_density, output_folder): ax.plot(plot_bins - plot_bins[0], plot_datas, linestyle=d["linestyle"], label=d["label"]) - ax.set_xlabel(r"Distance to rear wall $z$ [m]") - ax.set_ylabel(r"Current $I_r$ [A]") + ax.set_xlabel(r"Distance to rear wall $z$ (m)") + ax.set_ylabel(r"Current $I_r$ (A)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') @@ -64,8 +64,8 @@ def plotLongitudinalCurrentVsZ(filename_current, filename_density, output_folder plot_data = np.sum(np.sum(j_total,2),0) * d["charge"] ax.plot(z_bins- z_bins[0], plot_data, linestyle=d["linestyle"], label=d["label"]) - ax.set_xlabel(r"Distance to rear wall $z$ [m]") - ax.set_ylabel(r"Current $I_z$ [A]") + ax.set_xlabel(r"Distance to rear wall $z$ (m)") + ax.set_ylabel(r"Current $I_z$ (A)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') diff --git a/src/tools/analysis_jkellerer/currents/velocity_to_csv.py b/src/tools/analysis_jkellerer/currents/velocity_to_csv.py new file mode 100644 index 0000000..ad5c7ca --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/velocity_to_csv.py @@ -0,0 +1,53 @@ +import numpy as np + +from functions.calculate_currents_from_karl import * +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def velocityToCsv(filename_densities, filename_currents, filename_output_prefix): + electrons = { + "key": "/e-", + "key_density": "/Populations/e-/e-", + "filename_suffix": "_e.csv", + "header": "velocity e- (cm/s), r to left, z to bottom"} + Ts = { + "key": "T+", + "key_density": "/Populations/T+/T+", + "filename_suffix": "_T.csv", + "header": "velocity T+ (cm/s), r to left, z to bottom"} + T2s = { + "key": "T2+", + "key_density": "/Populations/T2+/T2+", + "filename_suffix": "_T2.csv", + "header": "velocity T2+ (cm/s), r to left, z to bottom"} + T3s = { + "key": "T3+", + "key_density": "/Populations/T3+/T3+", + "filename_suffix": "_T3.csv", + "header": "velocity T3+ (cm/s), r to left, z to bottom"} + + particles = [electrons, T3s, T2s, Ts] + + for part in particles: + jz = np.sum(particleCurrentZ(filename_currents, filename_densities, part["key"]),2) + + total_density = getDensities(filename_densities, part["key_density"]) + density = np.transpose(np.sum(total_density,3)[:,:,0]) + + velocity = np.zeros_like(jz) + nr = velocity.shape[0] + nz = velocity.shape[1] + + velocity[:,0] = jz[:,0]/density[:,0] + velocity[:,nz-1] = jz[:,nz-1]/density[:,nz-2] + + velocity[:,1:nz-1] = 2*jz[:,1:nz-1]/(density[:,0:nz-2] + density[:,1:nz-1]) + si_to_cgs = 1E2 + + filename_output = filename_output_prefix + part["filename_suffix"] + np.savetxt( + filename_output, + np.transpose(velocity*si_to_cgs), + delimiter=",", + header=part["header"]) diff --git a/src/tools/analysis_jkellerer/currents/velocity_to_h5.py b/src/tools/analysis_jkellerer/currents/velocity_to_h5.py new file mode 100644 index 0000000..624aead --- /dev/null +++ b/src/tools/analysis_jkellerer/currents/velocity_to_h5.py @@ -0,0 +1,62 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +from functions.calculate_currents_from_karl import * +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def velocityToH5(filename_densities, filename_currents, filename_output): + electrons = { + "key": "/e-", + "key_density": "/Populations/e-/e-", + "label": "e-", + "output_key": "Velocity/vz/electrons" + } + Ts = { + "key": "T+", + "key_density": "/Populations/T+/T+", + "label": "T+", + "output_key": "Velocity/vz/T" + } + T2s = { + "key": "T2+", + "key_density": "/Populations/T2+/T2+", + "label": "T2+", + "output_key": "Velocity/vz/T2" + } + T3s = { + "key": "T3+", + "key_density": "/Populations/T3+/T3+", + "label": "T3+", + "output_key": "Velocity/vz/T3" + } + + f = h5py.File(filename_output, 'a') + + particles = [electrons, T3s, T2s, Ts] + for part in particles: + jz = np.sum(particleCurrentZ(filename_currents, filename_densities, part["key"]),2) + + total_density = getDensities(filename_densities, part["key_density"]) + density = np.transpose(np.sum(total_density,3)[:,:,0]) + + velocity = np.zeros_like(jz) + nr = velocity.shape[0] + nz = velocity.shape[1] + + velocity[:,0] = jz[:,0]/density[:,0] + velocity[:,nz-1] = jz[:,nz-1]/density[:,nz-2] + + velocity[:,1:nz-1] = 2*jz[:,1:nz-1]/(density[:,0:nz-2] + density[:,1:nz-1]) + si_to_cgs = 1E2 + + if part["output_key"] in f: + del f[part["output_key"]] + f.create_dataset(part["output_key"], data=velocity*si_to_cgs, dtype=float) + + f.close() diff --git a/src/tools/analysis_jkellerer/densities/density_in_DeltaE.py b/src/tools/analysis_jkellerer/densities/density_in_DeltaE.py new file mode 100644 index 0000000..3dd2fe9 --- /dev/null +++ b/src/tools/analysis_jkellerer/densities/density_in_DeltaE.py @@ -0,0 +1,38 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +import functions.calculate_density_from_karl as ck +import functions.plot_standards as pst + +def longitudinalDensityInDeltaE( + filename_density, + Emin, Emax, + z_indexs): + + e_dict = {"key" : "/Populations/e-/e-", "charge": -1.602E-19} + dicts = [e_dict] + + pst.setRcParams() + prop_cycle = plt.rcParams['axes.prop_cycle'] + colors = prop_cycle.by_key()['color'] + + for d in dicts: + E_bins = ck.getEBinsMin(filename_density, d["key"]) + + E_bin_min = np.abs((E_bins-Emin)).argmin() + E_bin_max = np.abs((E_bins-Emax)).argmin() + + particles = ck.getParticlesPerBin(filename_density, d["key"]) + volumes = ck.getVolumes(filename_density, d["key"]) + + for zi in z_indexs: + particles_at_z = np.sum(particles[zi,:,:,:], axis=(0,1)) # z, r, phi, E + volumes_longitudinal = np.sum(volumes, axis=(1,2)) # z, r, phi + + dndE = particles_at_z/volumes_longitudinal[zi] + n = np.sum(dndE[E_bin_min:E_bin_max]) + print("n: {:e} 1/(m^3)".format(n)) \ No newline at end of file diff --git a/src/tools/analysis_jkellerer/densities/density_to_csv.py b/src/tools/analysis_jkellerer/densities/density_to_csv.py new file mode 100644 index 0000000..8c32214 --- /dev/null +++ b/src/tools/analysis_jkellerer/densities/density_to_csv.py @@ -0,0 +1,70 @@ +import numpy as np + +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def densityToCsv(filename, filename_output_prefix): + si_to_cgs = 1E-6 + + T3 = { + "key": "/Populations/T3+/T3+", + "filename_suffix": "_T3.csv", + "header": "density T3+ (cm^-3), r to left, z to bottom"} + e = { + "key": "/Populations/e-/e-", + "filename_suffix": "_e.csv", + "header": "density e- (cm^-3), r to left, z to bottom"} + T2 = { + "key": "/Populations/T2+/T2+", + "filename_suffix": "_T2.csv", + "header": "density T2+ (cm^-3), r to left, z to bottom"} + T = { + "key": "/Populations/T+/T+", + "filename_suffix": "_T.csv", + "header": "density T+ (cm^-3), r to left, z to bottom"} + + dicts = [e, T, T2, T3] + + for d in dicts: + total_density = getDensities(filename, d["key"]) + density = np.sum(total_density,3)[:,:,0]*si_to_cgs + filename_output = filename_output_prefix + d["filename_suffix"] + np.savetxt( + filename_output, + density, + delimiter=",", + header=d["header"]) + +def densityToCsvEnergyBound( + filename, + filename_output_prefix, + Emin, + Emax): + + si_to_cgs = 1E-6 + e = { + "key": "/Populations/e-/e-", + "filename_suffix": "_e.csv", + "header": "density e- (cm^-3), r to left, z to bottom"} + + Emins = getEBinsMin(filename, e["key"]) + Emaxs = getEBinsMax(filename, e["key"]) + + Emin_index = np.where(Emins <= Emin) + if np.size(Emin_index) == 0: + Emin_index = 0 + else: + Emin_index = Emin_index[-1][-1] + + Emax_index = np.where(Emins <= Emax)[-1][-1] + + total_density = getDensities(filename, e["key"]) + density = np.sum(total_density[:,:,:,Emin_index:Emax_index+1],3)[:,:,0]*si_to_cgs + filename_output = filename_output_prefix + e["filename_suffix"] + + np.savetxt( + filename_output, + density, + delimiter=",", + header=e["header"]) \ No newline at end of file diff --git a/src/tools/analysis_jkellerer/densities/density_to_h5.py b/src/tools/analysis_jkellerer/densities/density_to_h5.py index dbd0896..044c1fc 100644 --- a/src/tools/analysis_jkellerer/densities/density_to_h5.py +++ b/src/tools/analysis_jkellerer/densities/density_to_h5.py @@ -28,7 +28,8 @@ def densityToH5(filename, filename_output): total_density_T3 = getDensities(filename, keys[3]) density_T3 = np.sum(total_density_T3,3)[:,:,0] - total_density = np.array((density_T + density_T2 + density_T3 -density_e)/1E6) + si_to_cgs = 1E-6 + total_density = np.array((density_T + density_T2 + density_T3 -density_e)*si_to_cgs) f = h5py.File(filename_output, 'a') key_density = "Density/total" @@ -39,11 +40,28 @@ def densityToH5(filename, filename_output): key_electrons = "Density/electrons" if key_electrons in f: del f[key_electrons] - f.create_dataset(key_electrons, data=np.transpose(density_e/1E6), dtype=float) + f.create_dataset(key_electrons, data=np.transpose(density_e*si_to_cgs), dtype=float) key_ions = "Density/ions" if key_ions in f: del f[key_ions] - f.create_dataset(key_ions, data=np.transpose((density_T + density_T2 + density_T3)/1E6), dtype=float) + f.create_dataset(key_ions, data=np.transpose((density_T + density_T2 + density_T3)*si_to_cgs), dtype=float) + + key_T3 = "Density/T3" + if key_T3 in f: + del f[key_T3] + f.create_dataset(key_T3, data=np.transpose((density_T3)*si_to_cgs), dtype=float) + + key_T2 = "Density/T2" + if key_T2 in f: + del f[key_T2] + f.create_dataset(key_T2, data=np.transpose((density_T2)*si_to_cgs), dtype=float) + + key_T = "Density/T" + if key_T in f: + del f[key_T] + f.create_dataset(key_T, data=np.transpose((density_T)*si_to_cgs), dtype=float) + + f.close() diff --git a/src/tools/analysis_jkellerer/densities/plot_all_density_vs_r.py b/src/tools/analysis_jkellerer/densities/plot_all_density_vs_r.py index 508e8d6..e4fa535 100644 --- a/src/tools/analysis_jkellerer/densities/plot_all_density_vs_r.py +++ b/src/tools/analysis_jkellerer/densities/plot_all_density_vs_r.py @@ -42,15 +42,15 @@ def plotAllDensityVsR(filename, filename_output, z_bin): plot_bins, plot_data = makeBarPlotData(total_density_T3, rmins, rmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, '--', label=r'T$^+_3$') - plot_bins, plot_data = makeBarPlotData(total_density_T, rmins, rmaxs) + plot_bins, plot_data = makeBarPlotData(total_density_T2, rmins, rmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, '-.', label=r'T$^+_2$') - plot_bins, plot_data = makeBarPlotData(total_density_T2, rmins, rmaxs) + plot_bins, plot_data = makeBarPlotData(total_density_T, rmins, rmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, ':', label=r'T$^+$') ax.set_yscale("log") - ax.set_xlabel(r"Radius $r$ [m]") - ax.set_ylabel(r"Density $n$ [$cm^{-3}$]") + ax.set_xlabel(r"Radius $r$ (m)") + ax.set_ylabel(r"Density $n$ ($cm^{-3}$)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') diff --git a/src/tools/analysis_jkellerer/densities/plot_all_density_vs_z.py b/src/tools/analysis_jkellerer/densities/plot_all_density_vs_z.py index 7af2dc0..9f6a27e 100644 --- a/src/tools/analysis_jkellerer/densities/plot_all_density_vs_z.py +++ b/src/tools/analysis_jkellerer/densities/plot_all_density_vs_z.py @@ -43,15 +43,15 @@ def plotAllDensityVsZ(filename, filename_output): plot_bins, plot_data = makeBarPlotData(total_density_T3, zmins, zmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, '--', label=r'T$^+_3$') - plot_bins, plot_data = makeBarPlotData(total_density_T, zmins, zmaxs) + plot_bins, plot_data = makeBarPlotData(total_density_T2, zmins, zmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, '-.', label=r'T$^+_2$') - plot_bins, plot_data = makeBarPlotData(total_density_T2, zmins, zmaxs) + plot_bins, plot_data = makeBarPlotData(total_density_T, zmins, zmaxs) ax.plot(plot_bins - plot_bins[0], plot_data/1E6, ':', label=r'T$^+$') ax.set_yscale("log") - ax.set_xlabel(r"Distance to rear wall $z$ [m]") - ax.set_ylabel(r"Density $n$ [$cm^{-3}$]") + ax.set_xlabel(r"Distance to rear wall $z$ (m)") + ax.set_ylabel(r"Density $n$ ($cm^{-3}$)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') diff --git a/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_energy.py b/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_energy.py index c3c6b80..ca12c7b 100644 --- a/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_energy.py +++ b/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_energy.py @@ -15,45 +15,42 @@ def makeBarPlotData(data, mins, maxs): def plotElectronDensityVsE(filename, z_rearwall, z_mid, z_dps, filename_output): + pos1 = {"z_pos": z_mid, "label": "Mid", "linestyle": "-"} + pos2 = {"z_pos": z_rearwall, "label": "Rearwall", "linestyle": "--"} + pos3 = {"z_pos": z_dps, "label": "DPS", "linestyle": "-."} + + dicts = [pos1, pos2, pos3] + keys = ["/Populations/e-/e-", "/Populations/T+/T+", "/Populations/T2+/T2+", "/Populations/T3+/T3+"] k = keys[0] - density = getDensities(filename, k) + zmins = getZBinsMin(filename, k) zmaxs = getZBinsMax(filename, k) Emins = getEBinsMin(filename, k) Emaxs = getEBinsMax(filename, k) - rw_z_index = np.where(zmins <= z_rearwall)[-1][-1] - mid_z_index = np.where(zmins <= z_mid)[-1][-1] - dps_z_index = np.where(zmins <= z_dps)[-1][-1] - - density_sum = np.sum(density,2) - density_sum = np.sum(density_sum,1) - - density_rw = density_sum[rw_z_index,:] - density_mid = density_sum[mid_z_index,:] - density_dps = density_sum[dps_z_index,:] + particles = getParticlesPerBin(filename, k) + volumes = getVolumes(filename, k) pst.setRcParams() prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] - fig, ax = plt.subplots(figsize=pst.set_size('thesis')) - plot_bins, plot_data = makeBarPlotData(density_mid, Emins, Emaxs) - ax.plot(plot_bins, plot_data, '-', label="Mid") + for d in dicts: + z_index = np.where(zmins<= d["z_pos"])[-1][-1] + particles_at_z = np.sum(particles[z_index,:,:,:], axis=(0,1)) # z, r, phi, E + volumes_longitudinal = np.sum(volumes, axis=(1,2)) # z, r, phi + dndE = particles_at_z/volumes_longitudinal[z_index] + plot_bins, plot_data = makeBarPlotData(dndE, Emins, Emaxs) + ax.plot(plot_bins, plot_data, d["linestyle"], label=d["label"]) - plot_bins, plot_data = makeBarPlotData(density_rw, Emins, Emaxs) - ax.plot(plot_bins, plot_data,'--', label="Rearwall") - - plot_bins, plot_data = makeBarPlotData(density_dps, Emins, Emaxs) - ax.plot(plot_bins, plot_data, '-.', label="DPS") ax.set_yscale("log") ax.set_xscale("log") - ax.set_xlabel(r"Energy $E$ [eV]") - ax.set_ylabel(r"Density $n_e$ [$m^{-3}$]") + ax.set_xlabel(r"Energy $E$ (eV)") + ax.set_ylabel(r"Diff. Density $\frac{\mathrm{d}n_e}{\mathrm{d}\log_{10}E}$ $\left(\frac{1}{\mathrm{m}^{3} \log_{10}(\mathrm{eV})}\right)$") ax.set_xlim(1E-5, 2E4) diff --git a/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_z.py b/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_z.py index 265c248..e1425f3 100644 --- a/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_z.py +++ b/src/tools/analysis_jkellerer/densities/plot_electron_density_vs_z.py @@ -72,8 +72,8 @@ def plotElectronDensityVsZ(filename, E_th_max, E_beta_min, filename_output): ax.set_yscale("log") - ax.set_xlabel(r"Distance to rear wall $z$ [m]") - ax.set_ylabel(r"Density $n_e$ [$cm^{-3}$]") + ax.set_xlabel(r"Distance to rear wall $z$ (m)") + ax.set_ylabel(r"Density $n_e$ ($cm^{-3}$)") ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') diff --git a/src/tools/analysis_jkellerer/densities/spectrum_to_csv.py b/src/tools/analysis_jkellerer/densities/spectrum_to_csv.py new file mode 100644 index 0000000..2256cbb --- /dev/null +++ b/src/tools/analysis_jkellerer/densities/spectrum_to_csv.py @@ -0,0 +1,46 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +import functions.calculate_density_from_karl as ck + + +def spectrumToCsv(filename, filename_output, z_pos, Emin, Emax): + + key = "/Populations/e-/e-" + + particles = ck.getParticlesPerBin(filename, key) + volumes = ck.getVolumes(filename, key) + + zmins = ck.getZBinsMin(filename, key) + zmaxs = ck.getZBinsMax(filename, key) + Emins = ck.getEBinsMin(filename, key) + Emaxs = ck.getEBinsMax(filename, key) + + z_index = np.where(zmins <= z_pos)[-1][-1] + + particles_at_z = np.sum(particles[z_index,:,:,:], axis=(0,1)) # z, r, phi, E + volumes_longitudinal = np.sum(volumes, axis=(1,2)) # z, r, phi + + dndE = particles_at_z/volumes_longitudinal[z_index] + Emins = ck.getEBinsMin(filename, key) + Emaxs = ck.getEBinsMax(filename, key) + Emids = (Emaxs + Emins)/2 + + Emin_index = np.where(Emins <= Emin)[-1][-1] + Emax_index = np.where(Emins <= Emax)[-1][-1] + + dndE_bound = dndE[Emin_index:Emax_index+1] + E_bound = Emins[Emin_index:Emax_index+1] + + dndE_bound = np.append(dndE_bound, 0) + E_bound = np.append(E_bound, Emaxs[Emax_index]) + + np.savetxt( + filename_output, + np.transpose(np.array([E_bound, dndE_bound])), + delimiter=",", + header="Energy, dndE") diff --git a/src/tools/analysis_jkellerer/densities/spectrum_to_h5.py b/src/tools/analysis_jkellerer/densities/spectrum_to_h5.py new file mode 100644 index 0000000..9c67e8c --- /dev/null +++ b/src/tools/analysis_jkellerer/densities/spectrum_to_h5.py @@ -0,0 +1,61 @@ +import numpy as np +import h5py as h5py +import os +import sys +import matplotlib +import matplotlib.pyplot as plt + +from functions.calculate_density_from_karl import * +import functions.plot_standards as pst + + +def spectrumToH5(filename, filename_output): + keys = ["/Populations/e-/e-", "/Populations/T+/T+", "/Populations/T2+/T2+", "/Populations/T3+/T3+"] + + print("Get Density: e-") + total_density_e = getDensities(filename, keys[0]) + density_e = total_density_e[:,:,0,:] + density_e = np.moveaxis(density_e,1,0) + + print("Get Density: T+") + total_density_T = getDensities(filename, keys[1]) + density_T = total_density_T[:,:,0,:] + density_T = np.moveaxis(density_T,1,0) + + print("Get Density: T2+") + total_density_T2 = getDensities(filename, keys[2]) + density_T2 = total_density_T2[:,:,0,:] + density_T2 = np.moveaxis(density_T2,1,0) + + print("Get Density: T3+") + total_density_T3 = getDensities(filename, keys[3]) + density_T3 = total_density_T3[:,:,0,:] + density_T3 = np.moveaxis(density_T3,1,0) + + + si_to_cgs = 1E-6 + + f = h5py.File(filename_output, 'a') + key_electrons = "Spectrum/electrons" + if key_electrons in f: + del f[key_electrons] + f.create_dataset(key_electrons, data=(density_e*si_to_cgs), dtype=float) + + key_T3 = "Spectrum/T3" + if key_T3 in f: + del f[key_T3] + f.create_dataset(key_T3, data=(density_T3*si_to_cgs), dtype=float) + + key_T2 = "Spectrum/T2" + if key_T2 in f: + del f[key_T2] + f.create_dataset(key_T2, data=(density_T2*si_to_cgs), dtype=float) + + key_T = "Spectrum/T" + if key_T in f: + del f[key_T] + f.create_dataset(key_T, data=(density_T*si_to_cgs), dtype=float) + + + + f.close() diff --git a/src/tools/analysis_jkellerer/functions/calculate_density_from_karl.py b/src/tools/analysis_jkellerer/functions/calculate_density_from_karl.py index ffc86c4..325fff1 100644 --- a/src/tools/analysis_jkellerer/functions/calculate_density_from_karl.py +++ b/src/tools/analysis_jkellerer/functions/calculate_density_from_karl.py @@ -90,7 +90,6 @@ def longitudinalDensity(filename, key): volumes_longitudinal = np.sum(volumes, axis=(1,2)) density = particles_longitudinal / volumes_longitudinal - return density def getVolumes(filename, key):