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

beam

Get the current default beam.

options

Values of current options.

version

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.

Parameters:

text (str) – command text

Return type:

bool

Returns:

whether the command has completed without error

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)
call(file, chdir=False)[source]

CALL a file in the MAD-X interpreter.

Parameters:
  • file (str) – file name with path

  • chdir (bool) – temporarily change directory in MAD-X process

chdir(dir)[source]

Change the directory of the MAD-X process (not the current python process).

Parameters:

dir (str) – new path name

Return type:

ChangeDirectory

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')
eval(expr)[source]

Evaluates an expression and returns the result as double.

Parameters:

expr (str) – expression to evaluate.

Return type:

float

Returns:

numeric value of the expression

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:

list

input(text)[source]

Run any textual MAD-X input.

Parameters:

text (str) – command text

Return type:

bool

Returns:

whether the command has completed without error

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:
  • constraints (list) – constraints to pose during matching

  • vary (list) – knob names to be varied

  • weight (dict) – weights for matching parameters

  • knobfile (str) – file to write the knob values to

  • kwargs (dict) – keyword arguments for the MAD-X command

Return type:

dict

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)
quit()[source]

Shutdown MAD-X interpreter and stop process.

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.

sectortable2(name='sectortable')[source]

Read 2nd order sectormap T_ijk, return as Nx6x6x6 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.

use(sequence=None, range=None, **kwargs)[source]

Run USE to expand a sequence.

Parameters:

sequence (str) – sequence name

Returns:

name of active sequence

verbose(switch=True)[source]

Turn verbose output on/off.