Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-bunch plotting capabilities #11

Open
wants to merge 37 commits into
base: master
Choose a base branch
from

Conversation

mattachu
Copy link
Contributor

@mattachu mattachu commented Apr 14, 2020

Description

Adds six new plots with multiple bunches:

  • Beam size (rms)
  • Emittance
  • Emittance growth
  • Energy
  • Combined energy (log-scale)
  • Combined phase space

Can include all bunches or only a selected number of bunches.

Beam size, emittance and emittance growth are plotted against time t or location z.
Each bunch is plotted as a dotted line, and a combined value for all bunches as a solid line.
Also plots experimental data for beam size alongside the simulation results.

Energy spectra are plotted as histograms, including a combined plot for all bunches combined.

Phase space plots are generated using Matplotlib's hist2d function, which is much faster than the custom method use for the single-file phase space plots.
Also added 1D histograms to the axes.

MultiBunchPlot.py can also be run as a script, outputting all files as PNG images, and can be run on a headless server.
The bunches to plot can be given as a command-line parameter.

Dependencies

This depends on a number of other pull requests:

These have already been merged into this branch, so this pull request should only be merged after the above have already been merged into the master branch. At that point, this current pull request should be rebased.

The commit with the actual multi-bunch plotting changes is fa6373b.

Example plots

Emittance

2020-03-31-1 0e-14-no-spacecharge-emittance

Energy

2020-04-14-energies-initial

Phase space

2020-04-14-phase-space-initial

Files were commited with CRLF (Windows-style) line endings. This stops them working on Linux. It's best to commit to Git with LF (Linux-style) line endings and use Git's own settings to make sure you get the right version when you check it out.

On Windows, set Git to automatically change line endings on checkout:

```
git config core.autocrlf true
```

On Linux, set Git to checkout as-is but commit as LF:

```
git config core.autocrlf input
```
* Add new parameter `file_offset` to keep the files separate
* Adjust output statements to use the new file numbers
* Adjust output statements to include z-location and correctly scale `xpx` values (as in `diagnostic1avg_Output`)
* Call with a different file offset each time to avoid file clash
* Call once at the start and then every 15 time steps
* Use a single plot for both single and multiple bunch plots
* Use a drop-down to select bunch rather than a message window
* Set the bunch selector based on number of bunches in simulation
* Use methods instead of attributes to pass data
Adjusted calculation of `localmax(5)` and `localmax(6)` to match the calculation in the average diagnostic output subroutines.

Previously would output maximum size in the z direction without first subtracting the beam centroid.
This deprecated class was removed in Matplotlib version 3.0.0
This deprecated method was also removed in Matplotlib version 3.0.0
Was failing to plot anything because of these inconsistencies.
Adds six new plots with multiple bunches:
* Beam size (rms)
* Emittance
* Emittance growth
* Energy
* Combined energy (log-scale)
* Combined phase space

Can include all bunches or only a selected number of bunches.

Beam size, emittance and emittance growth are plotted against time t or location z.
Each bunch is plotted as a dotted line, and a combined value for all bunches as a solid line.
Also plots experimental data for beam size alongside the simulation results.

Energy spectra are plotted as histograms, including a combined plot for all bunches combined.

Phase space plots are generated using Matplotlib's `hist2d` function, which is much faster than the custom method use for the single-file phase space plots.
Also added 1D histograms to the axes.

MultiBunchPlot.py can also be run as a script, outputting all files as PNG images, and can be run on a headless server.
The bunches to plot can be given as a command-line parameter.
@mattachu mattachu marked this pull request as ready for review April 14, 2020 21:34
* Return 'single bunch' rather than 'bunch 1' from `MultiBunchPlot.bunch_text()` if there is only one bunch in the simulation
* New method `MultiBunchPlot.check_bunch_list()` checks which bunches are valid and which are invalid
* Only plot valid bunches when run as a script
* Don't produce per-bunch plots when run as a script if there is only one bunch in the simulation
Sometimes an offset to the starting position is required to ensure no particles have negative starting position. In such cases, the z-location of particles should be adjusted to enable direct comparison of experiment and simulation.

Specifying a z-offset makes the plots as if the particles had started at z=0 rather than at the offset location.

Also changed titles for multi-bunch energy spectra plots, from e.g. "BPM 110" to "at z = 0.6 mm"
Previous code used the small-angle approximation tan x ~ x
Sometimes an IndexError is thrown when all particles are lost from a bunch.

Also fixed some Matplotlib warnings about inefficient or deprecated practices.
Previous code had grown over time so that there was lots of duplication for the three sections for initial, final and BPM plots. This change moves that code into a new common method, `plot_phase_spaces_and_energies()`.

It also adds some extra error handling to make sure the maximum amount of plots can be generated when run as a post-processing script.
When run as a script, the offset is loaded from file at the beginning of the `plot_all()` routine.

When run as a GUI, the z-offset is loaded at draw time as part of the `create_offset_selector()` method, and the `get_offset()` method is also updated to check the input file if there is no value set in the GUI itself.
Given a target range [min, max], the `plot_total_energy()` method will highlight this range in the energy spectrum, splitting the histogram into three sections for below, within and above the target range.

The `plot_all()` script will also create a "target" phase space plot for each output step, including only the particles in the target energy range.

A new method `select_by_energy()` is used to select the subset of data for a given energy range.
Now that energy range filter parameters have been added to the input file in branch `feature/filter-energy` (see commit 4781d70), the plot script can read these directly rather than needing them to be passed as a parameter to the plot function.
Total energy plot can highlight the target energy range, and the phase space plot can be filtered by energy range.
@mattachu
Copy link
Contributor Author

Added energy filtering functionality to the GUI – this works well with #16, but the changes in #16 are not required for this GUI filtering to work – filters can be applied to any multi-bunch phase space or total energy plots.

This allows plotting of the target range for cases when the integrator is run as usual but the target range values are still stored in the input file.

This could be useful when running single-bunch simulations extracted from a larger multi-bunch simulation.
Previous code would treat a single bunch as one in a set, plotting with a dashed line and creating an unnecessary legend.
It's useful to save beam size plots against both z and t
@qianglbl
Copy link
Collaborator

need to resolve the conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants