#from asyncio import open_unix_connection import numpy as np import h5py as h5py import os import sys import matplotlib import matplotlib.pyplot as plt import plot_standards as pst def getSpecies(filename): file = h5py.File(filename, 'r') return list(file.keys()) def outputPeriodKey(output_period): if (output_period == "LastSyncPeriod"): return "/LastSyncPeriod" else: return "/" + output_period def mass(filename, path_to_species): file = h5py.File(filename, 'r') path_to_mass = path_to_species + "/Mass" return np.array(file[path_to_mass])[0] def charge(filename, path_to_species): file = h5py.File(filename, 'r') path_to_charge = path_to_species + "/Charge" return np.array(file[path_to_charge])[0] def zBinsDensityField(filename, path_to_species): file = h5py.File(filename, 'r') path_to_border = path_to_species + "/DensityField/BinBorders/longitudinal" return np.array(file[path_to_border]) def energyBinsDensityfield(filename, path_to_species): file = h5py.File(filename, 'r') path_to_border = path_to_species + "/DensityField/BinBorders/energy" return np.array(file[path_to_border]) def binVolumes(filename, path_to_species): file = h5py.File(filename, 'r') path_to_volumes = path_to_species + "/DensityField/BinVolumes" return np.array(file[path_to_volumes]) def deletionsMaxInteractions(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path = path_to_species + "/DeletionLogger" + \ outputPeriodKey(output_period) + "/NumDeletions/MaxInteractions" return np.array(file[path])[0] def deletionsMaxTimeAlive(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path = path_to_species + "/DeletionLogger" + \ outputPeriodKey(output_period) + "/NumDeletions/MaxTimeAlive" return np.array(file[path])[0] def deletionsRecombination(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path = path_to_species + "/DeletionLogger" + \ outputPeriodKey(output_period) + "/NumDeletions/Recombination" return np.array(file[path])[0] def deletionsWall(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path = path_to_species + "/DeletionLogger" + \ outputPeriodKey(output_period) + "/NumDeletions/Wall" return np.array(file[path])[0] def timeInBin(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_time = path_to_species + "/DensityField/SimulationData" + \ outputPeriodKey(output_period) + "/TimeInBin" return np.array(file[path_to_time]) def physicalTime(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_time = path_to_species + "/DensityField/SimulationData" + \ outputPeriodKey(output_period) + "/PhysicalTime" return np.array(file[path_to_time])[0] def spectrum(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_spectrum = path_to_species + "/DensityField/SimulationData" + \ outputPeriodKey(output_period) + "/Spectrum" return np.array(file[path_to_spectrum]) def density(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_density = path_to_species + "/DensityField/SimulationData" + \ outputPeriodKey(output_period) + "/Density" return np.array(file[path_to_density]) def longitudinalCurrent(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_current = path_to_species + "/CurrentCollector/SimulationData" + \ outputPeriodKey(output_period) + "/CurrentInZ" return np.array(file[path_to_current]) def longitudinalNegativeCrossings(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_current = path_to_species + "/CurrentCollector/SimulationData" + \ outputPeriodKey(output_period) + "/NegativeCrossingsInZ" return np.array(file[path_to_current]) def longitudinalPositiveCrossings(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_current = path_to_species + "/CurrentCollector/SimulationData" + \ outputPeriodKey(output_period) + "/PositiveCrossingsInZ" return np.array(file[path_to_current]) def radialCurrent(filename, path_to_species, output_period): file = h5py.File(filename, 'r') path_to_current = path_to_species + "/CurrentCollector/SimulationData" + \ outputPeriodKey(output_period) + "/CurrentInR" return np.array(file[path_to_current]) def longitudinalAreas(filename, path_to_species): file = h5py.File(filename, 'r') path_to_areas = path_to_species + "/CurrentCollector/AreaInZ" return np.array(file[path_to_areas]) def radialAreas(filename, path_to_species): file = h5py.File(filename, 'r') path_to_areas = path_to_species + "/CurrentCollector/AreaInR" return np.array(file[path_to_areas]) def longitudinalCurrentVsZ(filename, path_to_species, output_period="LastSyncPeriod"): areas_in_z = longitudinalAreas(filename, path_to_species) current_in_z = longitudinalCurrent( filename, path_to_species, output_period) particles = current_in_z * areas_in_z return np.sum(particles, axis=(0, 1)) / np.sum(areas_in_z, axis=(0, 1)) def longitudinalNegativeCrossingsVsZ(filename, path_to_species, output_period="LastSyncPeriod"): areas_in_z = longitudinalAreas(filename, path_to_species) current_in_z = longitudinalCurrent( filename, path_to_species, output_period) particles = current_in_z * areas_in_z return np.sum(particles, axis=(0, 1)) / np.sum(areas_in_z, axis=(0, 1)) def tubeCurrentVsZ(filename, path_to_species, output_period="LastSyncPeriod"): areas_in_r = radialAreas(filename, path_to_species) current_in_r = radialCurrent(filename, path_to_species, output_period) particles = current_in_r * areas_in_r return (np.sum(particles, axis=(1)) / np.sum(areas_in_r, axis=(1)))[-2, :] def densityVsZ(filename, path_to_species, output_period="LastSyncPeriod"): bin_volumes = binVolumes(filename, path_to_species) densities = density(filename, path_to_species, output_period) particles = densities*bin_volumes return np.sum(particles, axis=(0, 1)) / np.sum(bin_volumes, axis=(0, 1)) def findClosestIndex(position, bin_borders): return (np.abs(bin_borders - position)).argmin() def spectrumAtZ(filename, path_to_species, z_position, output_period="LastSyncPeriod"): bin_volumes = binVolumes(filename, path_to_species) spectrums = spectrum(filename, path_to_species, output_period) particles = spectrums for i in np.arange(0, particles.shape[3]): particles[:, :, :, i] = spectrums[:, :, :, i]*bin_volumes z_index = findClosestIndex( z_position, zBinsDensityField(filename, path_to_species)) volume = np.sum(bin_volumes, axis=(0, 1))[z_index] return np.sum(particles, axis=(0, 1))[z_index, :]/volume 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 plotSpectrumAtZ(filename, path_to_species, z_position, plot_axis, output_period="LastSyncPeriod"): spectrum_at_z = spectrumAtZ( filename, path_to_species, z_position, output_period) E_bins = energyBinsDensityfield(filename, path_to_species) # included to get dn/dE E_bin_volumes = E_bins[1:] - E_bins[:-1] plot_bins, plot_datas = makeBarPlotData( spectrum_at_z[1:-1]/E_bin_volumes, E_bins[0:-1], E_bins[1:]) plot_axis.plot(plot_bins, plot_datas, label=str(path_to_species)) plot_axis.set_xscale("log") plot_axis.set_yscale("log") plot_axis.set_xlabel(r"Energy $E$ (eV)") plot_axis.set_ylabel( r"Diff. Density $\frac{\mathrm{d}n_e}{\mathrm{d}E}$ $\left(\frac{1}{\mathrm{m}^{3} \mathrm{eV}}\right)$") # plot_axis.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)$") def plotAllSpectrumAtZ(filename, z_position, output_period, specie_keys, output_folder): pst.setRcParams() fig, ax = plt.subplots(figsize=pst.set_size('thesis')) for key in specie_keys: plotSpectrumAtZ(filename, key, z_position, ax, output_period) ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') ax.legend() fig.savefig(output_folder + "/spectrum_at_z={}_at_period={}.pdf".format(z_position, output_period)) def plotDensityVsZ(filename, path_to_species, plot_axis, output_period="LastSyncPeriod"): density = densityVsZ(filename, path_to_species, output_period) z_bins = zBinsDensityField(filename, path_to_species) plot_bins, plot_datas = makeBarPlotData( density[1:-1], z_bins[0:-1], z_bins[1:]) plot_axis.plot(plot_bins, plot_datas, label=str(path_to_species)) plot_axis.set_yscale("log") plot_axis.set_xlabel(r"Distance to rear wall $z$ (m)") plot_axis.set_ylabel(r"Density $n$ ($m^{-3}$)") def plotAllDensityVsZ(filename, output_period, specie_keys, output_folder): pst.setRcParams() fig, ax = plt.subplots(figsize=pst.set_size('thesis')) for key in specie_keys: plotDensityVsZ(filename, key, ax, output_period) ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') ax.legend() fig.savefig(output_folder + "/density_vs_z_at_period={}.pdf".format(output_period), format='pdf', bbox_inches='tight') def plotLongitudinalParticleCurrentVsZ(filename, path_to_species, plot_axis, output_period="LastSyncPeriod"): current_in_z = longitudinalCurrentVsZ( filename, path_to_species, output_period) z_bins = zBinsDensityField(filename, path_to_species) plot_axis.plot(z_bins, current_in_z, label=str(path_to_species)) plot_axis.set_xlabel(r"Distance to rear wall $z$ (m)") plot_axis.set_ylabel(r"Particle current $j$ ($m^{-2}s^{-1}$)") def plotAllLongitudinalParticleCurrentVsZ(filename, output_period, specie_keys, output_folder): pst.setRcParams() fig, ax = plt.subplots(figsize=pst.set_size('thesis')) for key in specie_keys: plotLongitudinalParticleCurrentVsZ(filename, key, ax, output_period) ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') ax.legend() fig.savefig(output_folder + "/longitudinal_particle_current_vs_z_at_period={}.pdf".format(output_period), format='pdf', bbox_inches='tight') def plotLongitudinalElectricCurrentVsZ(filename, path_to_species, plot_axis, output_period="LastSyncPeriod"): current_in_z = longitudinalCurrentVsZ( filename, path_to_species, output_period)*charge(filename, path_to_species) z_bins = zBinsDensityField(filename, path_to_species) plot_axis.plot(z_bins, current_in_z) plot_axis.set_xlabel(r"Distance to rear wall $z$ (m)") plot_axis.set_ylabel(r"Electric current $I$ ($A$)") # unit should be wrong, think it should be A/m^2 def printDeletionLoggerStats(filename, path_to_species, output_period="LastSyncPeriod"): max_interactions = deletionsMaxInteractions( filename, path_to_species, output_period) max_time_alive = deletionsMaxTimeAlive( filename, path_to_species, output_period) recombinations = deletionsRecombination( filename, path_to_species, output_period) wall = deletionsWall(filename, path_to_species, output_period) print("Deletion Logger stats for: " + str(path_to_species)) print("MaxTimeAlive: {}".format(max_time_alive)) print("MaxInteractions: {}".format(max_interactions)) print("Recombinations: {}".format(recombinations)) print("Wall hits: {}".format(wall)) def printAllDeletionLoggerStats(filename, specie_keys, output_period): for key in specie_keys: printDeletionLoggerStats(filename, key, output_period) def zBinsPoissonSolver(filename, path_to_poisson_solver): file = h5py.File(filename, 'r') path_to_border = path_to_poisson_solver + "/BinBorders/longitudinal" return np.array(file[path_to_border]) def rBinsPoissonSolver(filename, path_to_poisson_solver): file = h5py.File(filename, 'r') path_to_border = path_to_poisson_solver + "/BinBorders/radial" return np.array(file[path_to_border]) def potential(filename, path_to_poisson_solver, output_period): file = h5py.File(filename, 'r') path_to_density = path_to_poisson_solver + "/SimulationData" + \ outputPeriodKey(output_period) + "/Potential" return np.array(file[path_to_density]) # returns potential averaged over r-direction; assumes only one azimuthal bin def potentialVsZ(filename, path_to_poisson_solver, output_period="LastSyncPeriod"): rbinBorders = rBinsPoissonSolver(filename, path_to_poisson_solver) deltar = rbinBorders[1] - rbinBorders[0] #assumption of linear binning weights = rbinBorders[1:]**2 - rbinBorders[:-1]**2 + deltar**2 weights = np.append(deltar**2/4, weights) potentialValues = potential(filename, path_to_poisson_solver, output_period) weightedPotential = potentialValues*weights[:, np.newaxis, np.newaxis] return np.sum(weightedPotential, axis=(0,1)) / np.sum(weights) def plotPotentialVsZ(filename, path_to_poisson_solver, plot_axis, output_period="LastSyncPeriod"): potential_ = potentialVsZ(filename, path_to_poisson_solver, output_period) z_bins = zBinsPoissonSolver(filename, path_to_poisson_solver) plot_bins, plot_datas = makeBarPlotData( potential_, z_bins, z_bins) plot_axis.plot(plot_bins, plot_datas) # plot_axis.set_yscale("log") plot_axis.set_xlabel(r"Distance to rear wall $z$ (m)") plot_axis.set_ylabel(r"Potential $\Phi$ ($V$)") def plotAllPotentialVsZ(filename, output_period, output_folder): pst.setRcParams() fig, ax = plt.subplots(figsize=pst.set_size('thesis')) key = "PoissonSolver" plotPotentialVsZ(filename, key, ax, output_period) ax.set_facecolor('#e7e7e7ff') ax.grid(color='white') fig.savefig(output_folder + "/potential_vs_z_at_period={}.pdf".format(output_period), format='pdf', bbox_inches='tight')