|
| 1 | +""" |
| 2 | +This file is part of the PIConGPU. |
| 3 | +
|
| 4 | +Copyright 2017-2018 PIConGPU contributors |
| 5 | +Authors: Sebastian Starke |
| 6 | +License: GPLv3+ |
| 7 | +""" |
| 8 | + |
| 9 | +from picongpu.plugins.energy_histogram import EnergyHistogram |
| 10 | +from picongpu.plugins.plot_mpl.base_visualizer import Visualizer as\ |
| 11 | + BaseVisualizer, plt |
| 12 | + |
| 13 | + |
| 14 | +class Visualizer(BaseVisualizer): |
| 15 | + """ |
| 16 | + Class for creation of histogram plots on a logscaled y-axis. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, run_directory): |
| 20 | + """ |
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + run_directory : string |
| 24 | + path to the run directory of PIConGPU |
| 25 | + (the path before ``simOutput/``) |
| 26 | + """ |
| 27 | + super(Visualizer, self).__init__(run_directory) |
| 28 | + |
| 29 | + def _create_data_reader(self, run_directory): |
| 30 | + """ |
| 31 | + Implementation of base class function. |
| 32 | + """ |
| 33 | + return EnergyHistogram(run_directory) |
| 34 | + |
| 35 | + def _create_plt_obj(self, ax): |
| 36 | + """ |
| 37 | + Implementation of base class function. |
| 38 | + Turns 'self.plt_obj' into a matplotlib.pyplot.plot object. |
| 39 | + """ |
| 40 | + bins, counts = self.data |
| 41 | + self.plt_obj = ax.semilogy(bins, counts, nonposy='clip')[0] |
| 42 | + |
| 43 | + def _update_plt_obj(self): |
| 44 | + """ |
| 45 | + Implementation of base class function. |
| 46 | + """ |
| 47 | + bins, counts = self.data |
| 48 | + self.plt_obj.set_data(bins, counts) |
| 49 | + |
| 50 | + def visualize(self, ax=None, **kwargs): |
| 51 | + """ |
| 52 | + Creates a semilogy plot on the provided axes object for |
| 53 | + the data of the given iteration using matpotlib. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + iteration: int |
| 58 | + the iteration number for which data will be plotted. |
| 59 | + ax: matplotlib axes object |
| 60 | + the part of the figure where this plot will be shown. |
| 61 | + kwargs: dictionary with further keyword arguments, valid are: |
| 62 | + species: string |
| 63 | + short name of the particle species, e.g. 'e' for electrons |
| 64 | + (defined in ``speciesDefinition.param``) |
| 65 | + iteration: int |
| 66 | + number of the iteration |
| 67 | + species_filter: string |
| 68 | + name of the particle species filter, default is 'all' |
| 69 | + (defined in ``particleFilters.param``) |
| 70 | +
|
| 71 | + """ |
| 72 | + ax = self._ax_or_gca(ax) |
| 73 | + # this already throws error if no species or iteration in kwargs |
| 74 | + super(Visualizer, self).visualize(ax, **kwargs) |
| 75 | + iteration = kwargs.get('iteration') |
| 76 | + species = kwargs.get('species') |
| 77 | + species_filter = kwargs.get('species_filter', 'all') |
| 78 | + |
| 79 | + if iteration is None or species is None: |
| 80 | + raise ValueError("Iteration and species have to be provided as\ |
| 81 | + keyword arguments!") |
| 82 | + |
| 83 | + ax.set_xlabel('Energy [keV]') |
| 84 | + ax.set_ylabel('Count') |
| 85 | + ax.set_title('Energy Histogram for species ' + |
| 86 | + species + ', filter = ' + species_filter + |
| 87 | + ', iteration ' + str(iteration)) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + |
| 92 | + def main(): |
| 93 | + import sys |
| 94 | + import getopt |
| 95 | + |
| 96 | + def usage(): |
| 97 | + print("usage:") |
| 98 | + print( |
| 99 | + "python", sys.argv[0], |
| 100 | + "-p <path to run_directory> -i <iteration>" |
| 101 | + " -s <particle species> -f <species_filter>") |
| 102 | + |
| 103 | + path = None |
| 104 | + iteration = None |
| 105 | + species = None |
| 106 | + filtr = None |
| 107 | + |
| 108 | + try: |
| 109 | + opts, args = getopt.getopt(sys.argv[1:], "hp:i:s:f:", [ |
| 110 | + "help", "path", "iteration", "species", "filter"]) |
| 111 | + except getopt.GetoptError as err: |
| 112 | + print(err) |
| 113 | + usage() |
| 114 | + sys.exit(2) |
| 115 | + |
| 116 | + for opt, arg in opts: |
| 117 | + if opt in ["-h", "--help"]: |
| 118 | + usage() |
| 119 | + sys.exit() |
| 120 | + elif opt in ["-p", "--path"]: |
| 121 | + path = arg |
| 122 | + elif opt in ["-i", "--iteration"]: |
| 123 | + iteration = int(arg) |
| 124 | + elif opt in ["-s", "--species"]: |
| 125 | + species = arg |
| 126 | + elif opt in ["-f", "--filter"]: |
| 127 | + filtr = arg |
| 128 | + |
| 129 | + # check that we got all args that we need |
| 130 | + if path is None or iteration is None: |
| 131 | + print("Path to 'run' directory and iteration have to be provided!") |
| 132 | + usage() |
| 133 | + sys.exit(2) |
| 134 | + if species is None: |
| 135 | + species = 'e' |
| 136 | + print("Particle species was not given, will use", species) |
| 137 | + if filtr is None: |
| 138 | + filtr = 'all' |
| 139 | + print("Species filter was not given, will use", filtr) |
| 140 | + |
| 141 | + fig, ax = plt.subplots(1, 1) |
| 142 | + Visualizer(path).visualize(ax, iteration=iteration, species=species, |
| 143 | + species_filter=filtr) |
| 144 | + plt.show() |
| 145 | + |
| 146 | + main() |
0 commit comments