|
| 1 | +""" |
| 2 | +
|
| 3 | +.. _demo-sbs-plotting: |
| 4 | +
|
| 5 | +========================== |
| 6 | +Segment-by-Segment Results |
| 7 | +========================== |
| 8 | +
|
| 9 | +This example shows how to use the modules in `pyhdtoolkit.plotting.sbs` and their various functions |
| 10 | +to easily visualize results of segment-by-segment runs. |
| 11 | +""" |
| 12 | +# sphinx_gallery_thumbnail_number = 2 |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import tfs |
| 15 | + |
| 16 | +from pyhdtoolkit.plotting.sbs import coupling, phase |
| 17 | +from pyhdtoolkit.utils import defaults |
| 18 | + |
| 19 | +defaults.config_logger(level="warning") |
| 20 | +plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial |
| 21 | +plt.rcParams.update({"text.usetex": True, "legend.fontsize": 16}) # for these specific plots |
| 22 | + |
| 23 | +############################################################################### |
| 24 | +# The functions in `pyhdtoolkit.plotting.sbs` modules usually need to be provided |
| 25 | +# different dataframes corresponding to specific components of segment-by-segment |
| 26 | +# results, which can be obtained by directly loading the output **TFS** files. |
| 27 | +# Let's load below the coupling results of a segment-by-segment run and related |
| 28 | +# model files. |
| 29 | + |
| 30 | +b1_model_tfs = tfs.read("sbs/b1_twiss_elements.dat") |
| 31 | +b2_model_tfs = tfs.read("sbs/b2_twiss_elements.dat") |
| 32 | + |
| 33 | +couple_b1_tfs = tfs.read("sbs/b1_sbscouple_IP1.out") |
| 34 | +couple_b2_tfs = tfs.read("sbs/b2_sbscouple_IP1.out") |
| 35 | + |
| 36 | +############################################################################### |
| 37 | +# One can now easily plot these results in a few lines with functions from the |
| 38 | +# `~.plotting.sbs.coupling` module. Here we will plot a single component of a |
| 39 | +# given coupling RDT through the segment. |
| 40 | +# |
| 41 | +# .. tip:: |
| 42 | +# Providing a dataframe with the model information is optional. If it is given, it is |
| 43 | +# used to determine the position of the IP point in the segment, and this position |
| 44 | +# will then be highlighted in the plot. |
| 45 | + |
| 46 | +# Here we plot the real part of the f1001 coupling resonance driving term (default rdt plotted) |
| 47 | +coupling.plot_rdt_component( |
| 48 | + couple_b1_tfs, |
| 49 | + couple_b2_tfs, |
| 50 | + b1_model_tfs, |
| 51 | + b2_model_tfs, |
| 52 | + ip=1, |
| 53 | + component="RE", |
| 54 | + figsize=(8, 8), |
| 55 | + b1_ylabel=r"$\mathrm{Beam\ 1}$ $\Re f_{1001}$", |
| 56 | + b2_ylabel=r"$\mathrm{Beam\ 2}$ $\Re f_{1001}$", |
| 57 | +) |
| 58 | +# We can set specific limits to the axes by accessing them through the returned Figure |
| 59 | +for ax in plt.gcf().axes: |
| 60 | + ax.set_ylim(-0.1, 0.1) |
| 61 | +plt.show() |
| 62 | + |
| 63 | +############################################################################### |
| 64 | +# One can plot all components of a given coupling RDT for both beams with the |
| 65 | +# `~.plotting.sbs.coupling.plot_full_ip_rdt` function. |
| 66 | +# |
| 67 | +# .. tip:: |
| 68 | +# Specific limits can be provided for different components of the RDT. At the |
| 69 | +# moment, these limits apply to the plots of both beams as they share their y |
| 70 | +# axis. Keyword arguments can be used to specify properties of the figure and |
| 71 | +# set the position of the legend. |
| 72 | + |
| 73 | + |
| 74 | +coupling.plot_full_ip_rdt( |
| 75 | + couple_b1_tfs, |
| 76 | + couple_b2_tfs, |
| 77 | + b1_model_tfs, |
| 78 | + b2_model_tfs, |
| 79 | + ip=1, |
| 80 | + figsize=(18, 9), |
| 81 | + abs_ylimits=(5e-3, 6.5e-2), |
| 82 | + real_ylimits=(-1e-1, 1e-1), |
| 83 | + imag_ylimits=(-1e-1, 1e-1), |
| 84 | + bbox_to_anchor=(0.535, 0.945), |
| 85 | +) |
| 86 | +plt.show() |
| 87 | + |
| 88 | +############################################################################### |
| 89 | +# Similarly, one can plot the phase results of a segment-by-segment run with the |
| 90 | +# functions in `~.plotting.sbs.phase`. The plotting logic is the same as above, |
| 91 | +# with the simplification that no component has to be chosen. Let's load data for |
| 92 | +# this example: one dataframe for each plane for Beam 2. |
| 93 | + |
| 94 | +sbs_phasex = tfs.read("sbs/b2sbsphasext_IP5.out") |
| 95 | +sbs_phasey = tfs.read("sbs/b2sbsphaseyt_IP5.out") |
| 96 | + |
| 97 | +phase.plot_phase_segment_one_beam( |
| 98 | + phase_x=sbs_phasex, |
| 99 | + phase_y=sbs_phasey, |
| 100 | + model=b2_model_tfs, |
| 101 | + ip=5, |
| 102 | + figsize=(9, 10), |
| 103 | +) |
| 104 | +plt.show() |
| 105 | + |
| 106 | + |
| 107 | +############################################################################### |
| 108 | +# Similarly to the coupling example, one can plot the results for both beams in |
| 109 | +# a single call with the `~.plotting.sbs.phase.plot_phase_segment_both_beams` |
| 110 | +# function, as demonstrated below. |
| 111 | + |
| 112 | +phase.plot_phase_segment_both_beams( |
| 113 | + b1_phase_x=sbs_phasex, |
| 114 | + b1_phase_y=sbs_phasey, |
| 115 | + b2_phase_x=sbs_phasex, |
| 116 | + b2_phase_y=sbs_phasey, |
| 117 | + b1_model=b2_model_tfs, |
| 118 | + b2_model=b2_model_tfs, |
| 119 | + ip=5, |
| 120 | + figsize=(18, 9), |
| 121 | + bbox_to_anchor=(0.535, 0.94), |
| 122 | +) |
| 123 | +plt.show() |
| 124 | + |
| 125 | +############################################################################# |
| 126 | +# |
| 127 | +# .. admonition:: References |
| 128 | +# |
| 129 | +# The use of the following functions, methods, classes and modules is shown |
| 130 | +# in this example: |
| 131 | +# |
| 132 | +# - `~.plotting.sbs.coupling`: `~.plotting.sbs.coupling.plot_rdt_component`, `~.plotting.sbs.coupling.plot_full_ip_rdt` |
| 133 | +# - `~.plotting.sbs.phase`: `~.plotting.sbs.phase.plot_phase_segment_one_beam`, `~.plotting.sbs.phase.plot_phase_segment_both_beams` |
0 commit comments