Skip to content

Commit

Permalink
Add math label for bunching
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherMayes committed Nov 8, 2023
1 parent 0cfaa64 commit b7f33ae
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 55 deletions.
98 changes: 83 additions & 15 deletions docs/examples/bunching.ipynb

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pmd_beamphysics/labels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pmd_beamphysics.units import pg_units
from pmd_beamphysics.units import pg_units, parse_bunching_str, nice_array


TEXLABEL = {
Expand Down Expand Up @@ -119,6 +119,11 @@ def texlabel(key: str):
tex1 = texlabel(subkeys[1])
return fr'\left<{tex0}, {tex1}\right>'

if key.startswith('bunching'):
wavelength = parse_bunching_str(key)
x, _, prefix = nice_array(wavelength)
return f'\mathrm{{bunching~at}}~{x:.1f}~\mathrm{{ {prefix}m }}'

return None


Expand Down
4 changes: 2 additions & 2 deletions pmd_beamphysics/particles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pmd_beamphysics.units import dimension, dimension_name, SI_symbol, pg_units, c_light
from pmd_beamphysics.units import dimension, dimension_name, SI_symbol, pg_units, c_light, parse_bunching_str

from pmd_beamphysics.interfaces.astra import write_astra
from pmd_beamphysics.interfaces.bmad import write_bmad
Expand Down Expand Up @@ -750,7 +750,7 @@ def __getitem__(self, key):
elif key.startswith('ptp_'):
return self.ptp(key[4:])
elif key.startswith('bunching_'):
wavelength = statistics.parse_bunching_str(key)
wavelength = parse_bunching_str(key)
return self.bunching(wavelength)

else:
Expand Down
36 changes: 0 additions & 36 deletions pmd_beamphysics/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,42 +569,6 @@ def bunching(z: np.ndarray, wavelength: float, weight: np.ndarray = None) -> flo
f = np.exp(1j * k * z)
return np.abs(np.sum(weight * f)) / np.sum(weight)

def parse_bunching_str(s):
"""
Parse a string of the on of the forms to extract the wavelength:
'bunching_1.23e-4'
'bunching_1.23e-4_nm'
Returns
-------
wavelength: float
"""
assert s.startswith('bunching_')

x = s.split('_')

wavelength = float(x[1])

if len(x) == 2:
factor = 1
elif len(x) == 3:
unit = x[2]
if unit == 'm':
factor = 1
elif unit == 'mm':
factor = 1e-3
elif unit == 'µm' or unit == 'um':
factor = 1e-6
elif unit == 'nm':
factor = 1e-9
else:
raise ValueError(f'Unparsable unit: {unit}')
else:
raise ValueError(f'Cannot parse {s}')


return wavelength * factor



48 changes: 47 additions & 1 deletion pmd_beamphysics/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def plottable_array(x, nice=True, lim=None):
PARTICLEGROUP_UNITS[k] = unit('eV/c')
for k in ['x', 'y', 'z', 'r', 'Jx', 'Jy']:
PARTICLEGROUP_UNITS[k] = unit('m')
for k in ['beta', 'beta_x', 'beta_y', 'beta_z', 'gamma']:
for k in ['beta', 'beta_x', 'beta_y', 'beta_z', 'gamma', 'bunching']:
PARTICLEGROUP_UNITS[k] = unit('1')
for k in ['theta']:
PARTICLEGROUP_UNITS[k] = unit('rad')
Expand Down Expand Up @@ -504,10 +504,53 @@ def pg_units(key):
return unit('V/m')
if key.startswith('magneticField'):
return unit('T')
if key.startswith('bunching_'):
return unit('1')


raise ValueError(f'No known unit for: {key}')


# -------------------------
# Special parsers

def parse_bunching_str(s):
"""
Parse a string of the on of the forms to extract the wavelength:
'bunching_1.23e-4'
'bunching_1.23e-4_nm'
Returns
-------
wavelength: float
"""
assert s.startswith('bunching_')

x = s.split('_')

wavelength = float(x[1])

if len(x) == 2:
factor = 1
elif len(x) == 3:
unit = x[2]
if unit == 'm':
factor = 1
elif unit == 'mm':
factor = 1e-3
elif unit == 'µm' or unit == 'um':
factor = 1e-6
elif unit == 'nm':
factor = 1e-9
else:
raise ValueError(f'Unparsable unit: {unit}')
else:
raise ValueError(f'Cannot parse {s}')


return wavelength * factor



# -------------------------
Expand Down Expand Up @@ -591,6 +634,9 @@ def write_dataset_and_unit_h5(h5, name, data, unit=None):

if unit:
write_unit_h5(h5[name], unit)






Expand Down

0 comments on commit b7f33ae

Please sign in to comment.