Madx¶
- class Madx(libmadx=None, command_log=None, stdout=None, history=None, prompt=None, **Popen_args)[source]¶
Bases:
object
Python interface for a MAD-X process.
For usage instructions, please refer to:
Communicates with a MAD-X interpreter in a background process.
The state of the MAD-X interpreter is controlled by feeding textual MAD-X commands to the interpreter.
The state of the MAD-X interpreter is accessed by directly reading the values from the C variables in-memory and sending the results pickled back over the pipe.
Data attributes:
- Variables:
command – Mapping of all MAD-X commands.
globals – Mapping of global MAD-X variables.
elements – Mapping of globally visible elements.
base_types – Mapping of MAD-X base elements.
sequence – Mapping of all sequences in memory.
table – Mapping of all tables in memory.
Attributes Summary
Get the current default beam.
Values of current options.
Get the MAD-X version.
Methods Summary
__call__
(text)Run any textual MAD-X input.
batch
()Collect input and send in a single batch when leaving context.
call
(file[, chdir])CALL a file in the MAD-X interpreter.
chdir
(dir)Change the directory of the MAD-X process (not the current python process).
eval
(expr)Evaluates an expression and returns the result as double.
exit
()Shutdown MAD-X interpreter and stop process.
expr_vars
(expr)Find all variable names used in an expression.
input
(text)Run any textual MAD-X input.
match
([constraints, vary, weight, method, ...])Perform a simple MATCH operation.
quit
()Shutdown MAD-X interpreter and stop process.
sectormap
(elems, **kwargs)Compute the 7D transfer maps (the 7'th column accounting for KICKs) for the given elements and return as Nx7x7 array.
sectortable
([name])Read sectormap + kicks from memory and return as Nx7x7 array.
sectortable2
([name])Read 2nd order sectormap T_ijk, return as Nx6x6x6 array.
survey
(**kwargs)Run SURVEY.
twiss
(**kwargs)Run TWISS.
use
([sequence, range])Run USE to expand a sequence.
verbose
([switch])Turn verbose output on/off.
Attributes Documentation
- beam¶
Get the current default beam.
- options¶
Values of current options.
- version¶
Get the MAD-X version.
Methods Documentation
- __call__(text)¶
Run any textual MAD-X input.
- batch()[source]¶
Collect input and send in a single batch when leaving context. This is useful to improve performance when issueing many related commands in quick succession.
Example:
>>> with madx.batch(): ... madx.globals.update(optic)
- chdir(dir)[source]¶
Change the directory of the MAD-X process (not the current python process).
- Parameters:
dir (
str
) – new path name- Return type:
- Returns:
a context manager that can change the directory back
It can be used as context manager for temporary directory changes:
with madx.chdir('/x/y/z'): madx.call('file.x') madx.call('file.y')
- exit()¶
Shutdown MAD-X interpreter and stop process.
- expr_vars(expr)[source]¶
Find all variable names used in an expression. This does not include element attribute nor function names.
- Return type:
- match(constraints=[], vary=[], weight=None, method=('lmdif', {}), knobfile=None, limits=None, **kwargs)[source]¶
Perform a simple MATCH operation.
For more advanced cases, you should issue the commands manually.
- Parameters:
- Return type:
- Returns:
final knob values
Example:
>>> from cpymad.madx import Madx >>> from cpymad.types import Constraint >>> m = Madx() >>> m.call('sequence.madx') >>> twiss_init = {'betx': 1, 'bety': 2, 'alfx': 3, 'alfy': 4} >>> m.match( ... sequence='mysequence', ... constraints=[ ... dict(range='marker1', ... betx=Constraint(min=1, max=3), ... bety=2) ... ], ... vary=['qp1->k1', ... 'qp2->k1'], ... **twiss_init, ... ) >>> tw = m.twiss('mysequence', **twiss_init)
- sectormap(elems, **kwargs)[source]¶
Compute the 7D transfer maps (the 7’th column accounting for KICKs) for the given elements and return as Nx7x7 array.
- sectortable(name='sectortable')[source]¶
Read sectormap + kicks from memory and return as Nx7x7 array.
- survey(**kwargs)[source]¶
Run SURVEY.
- Parameters:
sequence (str) – name of sequence
kwargs – keyword arguments for the MAD-X command
- twiss(**kwargs)[source]¶
Run TWISS.
- Parameters:
sequence (str) – name of sequence
kwargs – keyword arguments for the MAD-X command
Note that the kwargs overwrite any arguments in twiss_init.