Skip to content

Commit 0efd5fb

Browse files
rdemariacoldfix
authored andcommitted
access and query beam list (#124)
1 parent a36c6c8 commit 0efd5fb

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/cpymad/clibmadx.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ cdef extern from "madX/mad_gvar.h" nogil:
238238
command_list* defined_commands # with base types, but no user elements
239239
int start_var # start of variables after predefined constants
240240
int_array* deco # temporary buffer for polished expressions
241+
command_list* beam_list # beam list
241242

242243

243244
# Function declarations:

src/cpymad/libmadx.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ __all__ = [
122122
'get_defined_command',
123123
'get_defined_command_names',
124124

125+
# defined commands
126+
'get_beam',
127+
'get_beam_names',
128+
125129
# imported from 'os' for convenience in madx.Madx and should not be
126130
# considered part of the public interface:
127131
'getcwd',
@@ -911,6 +915,17 @@ def get_defined_command_names() -> list:
911915
"""Return list of MAD-X command names."""
912916
return _name_list(clib.defined_commands.list)
913917

918+
def get_beam(beam_name: str) -> dict:
919+
"""Return MAD-X beam as dict of values."""
920+
cdef bytes _beam_name = _cstr(beam_name)
921+
cdef int index = clib.name_list_pos(_beam_name, clib.beam_list.list)
922+
if index == -1:
923+
raise ValueError("Invalid beam: {!r}".format(beam_name))
924+
return _parse_command(clib.beam_list.commands[index])
925+
926+
def get_beam_names() -> list:
927+
"""Return list of MAD-X beam names."""
928+
return _name_list(clib.beam_list.list)
914929

915930
def is_sequence_expanded(sequence_name: str) -> bool:
916931
"""

src/cpymad/madx.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def __init__(self, libmadx=None, command_log=None, stdout=None,
221221
self.elements = GlobalElementList(self)
222222
self.base_types = BaseTypeMap(self)
223223
self.sequence = SequenceMap(self)
224+
self.beams = BeamMap(self)
224225
self.table = TableMap(self._libmadx)
225226
self._enter_count = 0
226227
self._batch = None
@@ -606,6 +607,29 @@ def __call__(self):
606607
return None
607608

608609

610+
class BeamMap(_Mapping):
611+
"""Mapping of all beams (:class:`Beam`) in memory."""
612+
613+
def __init__(self, madx):
614+
self._madx = madx
615+
self._libmadx = madx._libmadx
616+
617+
def __iter__(self):
618+
return iter(self._libmadx.get_beam_names())
619+
620+
def __getitem__(self, name):
621+
try:
622+
return Command(self._madx, self._libmadx.get_beam(name))
623+
except ValueError:
624+
raise KeyError("Unknown beam: {!r}".format(name)) from None
625+
626+
def __contains__(self, name):
627+
return name in self._libmadx.get_beam_names()
628+
629+
def __len__(self):
630+
return len(self._libmadx.get_beam_names())
631+
632+
609633
class TableMap(_Mapping):
610634

611635
"""Mapping of all tables (:class:`Table`) in memory."""

test/test_madx.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,9 @@ def test_dframe_after_use(mad):
10321032
# this line. It does not represent desired behaviour!
10331033
assert mad.table.twiss.row_names() == \
10341034
['#s', '#e', 'dfd', 'mqd', 'dff', 'mqf']
1035+
1036+
1037+
def test_beam_list(mad, lib):
1038+
mad.beam(pc=100)
1039+
assert lib.get_beam('default_beam')['data']['pc'].value == 100
1040+
assert lib.get_beam_names() == ['default_beam']

0 commit comments

Comments
 (0)