|
| 1 | +""" |
| 2 | +This file is part of ImpactX |
| 3 | +
|
| 4 | +Copyright 2025 ImpactX contributors |
| 5 | +Authors: Axel Huebl |
| 6 | +License: BSD-3-Clause-LBNL |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +def plot_survey( |
| 11 | + self, ref=None, ax=None, legend=True, legend_ncols=5, palette="cern-lhc" |
| 12 | +): |
| 13 | + """Plot over s of all elements in the KnownElementsList. |
| 14 | +
|
| 15 | + The signs of element strengths are determined by the sign of the charge of the reference particle. |
| 16 | + The projection of all element strengths is s-x ("vertical"). |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + self : ImpactXParticleContainer_* |
| 21 | + The KnownElementsList class in ImpactX |
| 22 | + ref : RefPart |
| 23 | + A reference particle, checked for the charge sign to plot focusing/defocusing strength directions properly. |
| 24 | + ax : matplotlib axes |
| 25 | + A plotting area in matplotlib (called axes there). |
| 26 | + legend: bool |
| 27 | + Plot a legend if true. |
| 28 | + legend_ncols: int |
| 29 | + Number of columns for lattice element types in the legend. |
| 30 | + palette: string |
| 31 | + Color palette. |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + Either populates the matplotlib axes in ax or creates a new axes containing the plot. |
| 36 | + """ |
| 37 | + from math import copysign |
| 38 | + |
| 39 | + import matplotlib.pyplot as plt |
| 40 | + import numpy as np |
| 41 | + from matplotlib.patches import Rectangle |
| 42 | + |
| 43 | + from .ElementColors import get_element_color |
| 44 | + |
| 45 | + charge_qe = 1.0 if ref is None else ref.charge_qe |
| 46 | + |
| 47 | + ax = ax or plt.subplot(111) |
| 48 | + |
| 49 | + element_lengths = [element.ds for element in self] |
| 50 | + |
| 51 | + # NumPy 2.1+ (i.e. Python 3.10+): |
| 52 | + # element_s = np.cumulative_sum(element_lengths, include_initial=True) |
| 53 | + # backport: |
| 54 | + element_s = np.insert(np.cumsum(element_lengths), 0, 0) |
| 55 | + |
| 56 | + ax.hlines(0, 0, element_s[-1], color="black", linestyle="--") |
| 57 | + |
| 58 | + # plot config |
| 59 | + skip_names = [ |
| 60 | + "Drift", |
| 61 | + "ChrDrift", |
| 62 | + "ExactDrift", |
| 63 | + "Empty", |
| 64 | + "Marker", |
| 65 | + "Source", |
| 66 | + ] |
| 67 | + |
| 68 | + handles = {} |
| 69 | + |
| 70 | + for i, element in enumerate(self): |
| 71 | + el_dict = element.to_dict() |
| 72 | + el_type = el_dict["type"] |
| 73 | + if el_type in skip_names: |
| 74 | + continue |
| 75 | + |
| 76 | + color = get_element_color(el_type, palette=palette) |
| 77 | + |
| 78 | + y0 = 0 # default start in y for unspecified elements |
| 79 | + height = 0.5 # default height for unspecified elements |
| 80 | + |
| 81 | + # note the sub-string matching for el_type |
| 82 | + if el_type == "BeamMonitor": |
| 83 | + y0 = -0.5 |
| 84 | + height = 1.0 |
| 85 | + if "Quad" in el_type: |
| 86 | + height = copysign(0.8, el_dict["k"] * charge_qe) |
| 87 | + if "Sbend" in el_type: |
| 88 | + if ref is None: |
| 89 | + height = copysign(0.8, element.rc(ref)) |
| 90 | + else: # guess |
| 91 | + height = copysign(0.8, el_dict["phi"]) |
| 92 | + # TODO: sign dependent, read m_p_scale |
| 93 | + # if el_type == "Kicker": |
| 94 | + # height = copysign(0.8, el_dict["xkick"]) |
| 95 | + |
| 96 | + # plot thin elements on top of thick elements |
| 97 | + zorder = 2 |
| 98 | + if element.ds == 0: |
| 99 | + zorder = 3 |
| 100 | + |
| 101 | + patch = Rectangle( |
| 102 | + (element_s[i], y0), |
| 103 | + element_lengths[i], |
| 104 | + height, |
| 105 | + color=color, |
| 106 | + alpha=0.8, |
| 107 | + zorder=zorder, |
| 108 | + ) |
| 109 | + ax.add_patch(patch) |
| 110 | + |
| 111 | + handles[el_type] = patch |
| 112 | + |
| 113 | + if legend: |
| 114 | + labels = list(handles.keys()) |
| 115 | + values = list(handles.values()) |
| 116 | + ax.legend( |
| 117 | + handles=values, |
| 118 | + labels=labels, |
| 119 | + bbox_to_anchor=(0.0, 1.02, 1.0, 0.102), |
| 120 | + loc="lower left", |
| 121 | + ncols=legend_ncols, |
| 122 | + mode="expand", |
| 123 | + borderaxespad=0.0, |
| 124 | + ) |
| 125 | + |
| 126 | + ax.set_xlabel(r"$s$ [m]") |
| 127 | + |
| 128 | + ax.set_ylim(-1, 1) |
| 129 | + ax.set_yticks([]) |
| 130 | + |
| 131 | + ax.set_aspect(1 / 1.618) # golden ratio |
| 132 | + |
| 133 | + return ax |
0 commit comments