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

MAD-X: Add TKICKER, HKICKER and VKICKER to accepted input #420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,13 @@ add_impactx_test(kicker_madx.py
examples/kicker/analysis_kicker.py
OFF # no plot script yet
)
# copy MAD-X lattice file
file(COPY ${ImpactX_SOURCE_DIR}/examples/kicker/hvkicker.madx
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/hvkicker_madx.py)
add_impactx_test(hvkicker_madx.py
examples/kicker/run_hvkicker_madx.py
OFF # ImpactX MPI-parallel
ON # ImpactX Python interface
examples/kicker/analysis_kicker.py
OFF # no plot script yet
)
8 changes: 8 additions & 0 deletions examples/kicker/hvkicker.madx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
beam, particle=electron, energy=2.0;

M1: MONITOR, L=0.0;
HK1: HKICKER, hkick=2.0e-3;
VK1: VKICKER, vkick=3.0e-3;

KICKLATTICE: Line=(M1,HK1,VK1,M1);
USE, SEQUENCE = KICKLATTICE;
3 changes: 2 additions & 1 deletion examples/kicker/kicker.madx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ beam, particle=electron, energy=2.0;

M1: MONITOR, L=0.0;
HK1: KICKER, hkick=2.0e-3, vkick=0.0;
VK1: KICKER, hkick=0.0, vkick=3.0e-3;
! TKICKER and KICKER are currently treated the exact same in ImpactX
VK1: TKICKER, hkick=0.0, vkick=3.0e-3;

KICKLATTICE: Line=(M1,HK1,VK1,M1);
USE, SEQUENCE = KICKLATTICE;
54 changes: 54 additions & 0 deletions examples/kicker/run_hvkicker_madx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
#
# Copyright 2022-2023 ImpactX contributors
# Authors: Chad Mitchell, Axel Huebl, Marco Garten
# License: BSD-3-Clause-LBNL
#
# -*- coding: utf-8 -*-


import amrex.space3d as amr
from impactx import ImpactX, RefPart, distribution, elements

sim = ImpactX()

# set numerical parameters and IO control
sim.particle_shape = 2 # B-spline order
sim.space_charge = False
# sim.diagnostics = False # benchmarking
sim.slice_step_diagnostics = True

# domain decomposition & space charge mesh
sim.init_grids()

# load a 2 GeV electron beam with an initial
# unnormalized rms emittance of nm
bunch_charge_C = 1.0e-9 # used without space charge
npart = 10000 # number of macro particles

# reference particle
ref = sim.particle_container().ref_particle().load_file("hvkicker.madx")

# particle bunch
distr = distribution.Waterbag(
sigmaX=4.0e-3,
sigmaY=4.0e-3,
sigmaT=1.0e-3,
sigmaPx=3.0e-4,
sigmaPy=3.0e-4,
sigmaPt=2.0e-3,
)
sim.add_particles(bunch_charge_C, distr, npart)

# add beam diagnostics
monitor = elements.BeamMonitor("monitor", backend="h5")

# design the accelerator lattice
sim.lattice.load_file("hvkicker.madx", nslice=1)

# run simulation
sim.evolve()

# clean shutdown
del sim
amr.finalize()
84 changes: 83 additions & 1 deletion src/python/impactx/MADXParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,18 @@ def __init__(self):
self.__kicker = {"name": "", "hkick": 0.0, "vkick": 0.0, "type": "kicker"}

self.__kicker_pattern = r"(.*):kicker,(.*)=(.*),(.*)=(.*);"
# equivalent to kicker
# http://mad.web.cern.ch/mad/madx.old/Introduction/tkickers.html
self.__tkicker_pattern = r"(.*):tkicker,(.*)=(.*),(.*)=(.*);"
# horizontal kicker without vkick
self.__hkicker_pattern = r"(.*):hkicker,(.*)=(.*);"
# vertical kicker without hkick
self.__vkicker_pattern = r"(.*):vkicker,(.*)=(.*);"

# don't count name and type --> len - 2
self.__nKicker = 2 * (len(self.__kicker) - 2)
self.__nHkicker = self.__nKicker - 2
self.__nVkicker = self.__nKicker - 2

self.beam = {
"energy": 0.0,
Expand Down Expand Up @@ -297,7 +306,7 @@ def parse(self, fn):

self.__elements.append(self.__dipedge.copy())

elif "kicker" in line:
elif re.search(r":\bkicker\b", line):
obj = re.match(self.__kicker_pattern, line)

# first tag is name
Expand All @@ -320,6 +329,79 @@ def parse(self, fn):

self.__elements.append(self.__kicker.copy())

elif re.search(r":\bhkicker\b", line):
obj = re.match(self.__hkicker_pattern, line)

# first tag is name
self.__kicker["name"] = obj.group(1)
self.__kicker["vkick"] = 0.0

for i in range(2, self.__nHkicker + 2, 2):
if obj.group(i) in self.__kicker:
self.__kicker[obj.group(i)] = float(obj.group(i + 1))
else:
raise MADXInputError(
"HKicker",
"Line "
+ str(nLine)
+ ": Parameter "
+ "'"
+ obj.group(i)
+ "'"
+ " does not exist for hkicker.",
)

self.__elements.append(self.__kicker.copy())

elif re.search(r":\bvkicker\b", line):
obj = re.match(self.__vkicker_pattern, line)

# first tag is name
self.__kicker["name"] = obj.group(1)
self.__kicker["hkick"] = 0.0

for i in range(2, self.__nVkicker + 2, 2):
if obj.group(i) in self.__kicker:
self.__kicker[obj.group(i)] = float(obj.group(i + 1))
else:
raise MADXInputError(
"VKicker",
"Line "
+ str(nLine)
+ ": Parameter "
+ "'"
+ obj.group(i)
+ "'"
+ " does not exist for vkicker.",
)

self.__elements.append(self.__kicker.copy())

# We treat TKICKER elements exactly like KICKER elements for now
# http://mad.web.cern.ch/mad/madx.old/Introduction/tkickers.html
elif re.search(r":\btkicker\b", line):
obj = re.match(self.__tkicker_pattern, line)

# first tag is name
self.__kicker["name"] = obj.group(1)

for i in range(2, self.__nKicker + 2, 2):
if obj.group(i) in self.__kicker:
self.__kicker[obj.group(i)] = float(obj.group(i + 1))
else:
raise MADXInputError(
"TKicker",
"Line "
+ str(nLine)
+ ": Parameter "
+ "'"
+ obj.group(i)
+ "'"
+ " does not exist for tkicker.",
)

self.__elements.append(self.__kicker.copy())

elif "marker" in line:
pass

Expand Down
2 changes: 1 addition & 1 deletion src/python/impactx/madx_to_impactx.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def lattice(parsed_beamline, nslice=1):
# Kicker, idealized thin element,
# MADX also defines length "L" and a roll angle around the longitudinal axis "TILT"
# https://mad.web.cern.ch/mad/webguide/manual.html#Ch11.S11
"KICKER": "Kicker",
"KICKER": "Kicker", # TKICKER, HKICKER and VKICKER become KICKER elements
# note: in MAD-X, this keeps track only of the beam centroid,
# "In addition it serves to record the beam position for closed orbit correction."
"MONITOR": "BeamMonitor", # drift + output diagnostics
Expand Down
Loading