API Reference

PyMAD-NG Module contents

class pymadng.MAD(mad_path: str = None, py_name: str = 'py', debug: bool = False, num_temp_vars: int = 8, ipython_use_jedi: bool = False)

Bases: object

An object that allows communication with MAD-NG

py_name

A string indicating the name of the reference to python from MAD-NG, for communication from MAD-NG to Python.

__MAD_version__

A string indicating the version of MAD-NG being used. (Also accessible from pymadng.MAD().MAD.env.version)

MADX_env_send(input: str)

Open the MAD-X environment in MAD-NG and directly send code.

Parameters:

input (str) – The code that would like to be evaluated in the MAD-X environment in MAD-NG.

__init__(mad_path: str = None, py_name: str = 'py', debug: bool = False, num_temp_vars: int = 8, ipython_use_jedi: bool = False)

Create a MAD Object to interface with MAD-NG.

The modules MADX, elements, sequence, mtable, twiss, beta0, beam, survey, object, track, match are imported into the MAD-NG environment by default.

Parameters:
  • mad_path (str) – The path to the mad executable, for the default value of None, the one that comes with pymadng package will be used

  • py_name (str) – The name used to interact with the python process from MAD

  • debug (bool) – Sets debug mode on or off

  • num_temp_vars (int) – The number of unique temporary variables you intend to use, see Managing References

  • ipython_use_jedi (bool) – Allow ipython to use jedi in tab completion, will be slower and may result in MAD-NG throwing errors

Returns:

A MAD object, allowing for communication with MAD-NG

deferred(**kwargs)

Create a deferred expression object

For the deferred expression object, the kwargs are used as the deffered expressions, with = replaced with :=. To assign the returned object a name, then use mad['name'] = mad.deffered(kwargs).

Parameters:

**kwargs – A variable list of keyword arguments, keyword as the name of the deffered expression within the object and the value as a string that is sent directly to the MAD-NG environment.

Returns:

A reference to the deffered expression object.

eval(input: str)

Evaluate an expression in MAD and return the result.

Parameters:

input (str) – An expression that would like to be evaluated in MAD-NG.

Returns:

The evaluated result.

globals() List[str]

Retreive the list of names of variables in the environment of MAD

Returns:

A list of strings indicating the globals variables and modules within the MAD-NG environment

load(module: str, *vars: str)

Import modules into the MAD-NG environment

Retrieve the classes in MAD-NG from the module module, while only importing the classes in the variable length list vars. If no string is provided, it is assumed that you would like to import every class from the module.

For example, mad.load("MAD.gmath") imports all variables from the module MAD.gmath. But mad.load("MAD", "matrix", "cmatrix")`` only imports the modules matrix and cmatrix from the module MAD.gmath.

Parameters:
  • module (str) – The name of the module to import from.

  • *vars (str) – Variable length argument list of the variable(s) to import from module.

loadfile(path: str, *vars: str)

Load a .mad file into the MAD-NG environment.

If vars is not provided, this is equivalent to assert(loadfile(path)) in MAD-NG. If vars is provided, for each var in vars, this is equivalent to var = require(path).var in MAD-NG. Due to Lua querks, do not include the .mad extension in the path if you provide vars, as this implies you would like to load a module.

Parameters:
  • path (str) – The path to the file to import.

  • *vars (str) – Variable length argument list of the variable(s) to import from the file.

py_strs_to_mad_strs(input: str | List[str])

Add ‘ to either side of a string or each string in a list of strings

Parameters:

input (str/list[str]) – The string(s) that you would like to add ‘ either side to each string.

Returns:

A string or list of strings with ‘ placed at the beginning and the end of each string.

receive(varname: str = None) str | int | float | ndarray | bool | list

See recv()

recv(varname: str = None) str | int | float | ndarray | bool | list

Return received data from MAD-NG.

Parameters:

varname (str) – The name of the variable you are receiving (Only useful when receiving references)

Returns:

Data from MAD-NG with type str/int/float/ndarray/bool/list, depending what was asked from MAD-NG.

Raises:

TypeError – If you forget to give a name when receiving a reference,

recv_and_exec(env: dict = {}) dict

Receive a string from MAD-NG and execute it.

Note: The class numpy and the instance of this object are available during the execution as np and mad respectively

Parameters:

env (dict) – The environment you would like the string to be executed in.

Returns:

The updated environment after executing the string.

recv_vars(*names: str) Any

Receive variables from the MAD-NG process

Given a list of variable names, receive the variables from the MAD-NG process.

Parameters:

*names (str) – The name(s) of variables that you would like to receive from MAD-NG.

Returns:

See recv().

send(data: str | int | float | ndarray | bool | list)

Send data to MAD-NG.

Parameters:

data (str/int/float/ndarray/bool/list) – The data to send to MAD-NG.

Returns:

self (the instance of the mad object)

Raises:
  • TypeError – An unsupported type was attempted to be sent to MAD-NG.

  • AssertionError – If data is a np.ndarray, the matrix must be of two dimensions.

send_ctpsa(monos: ndarray, coefficients: ndarray)

Send the monomials and coefficients of a complex TPSA to MAD-NG

The combination of monomials and coefficients creates a table representing the complex TPSA object in MAD-NG.

Parameters:

Seesend_tpsa().

Raises:

Seesend_tpsa().

send_lrng(start: float, stop: float, size: int)

Send a numpy array as a logrange to MAD-NG, equivalent to np.geomspace, but in MAD-NG.

Parameters:
  • start (float) – The start of range

  • stop (float) – The end of range (inclusive)

  • size (float) – The length of range

send_rng(start: float, stop: float, size: int)

Send a range to MAD-NG, equivalent to np.linspace, but in MAD-NG.

Parameters:
  • start (float) – The start of range

  • stop (float) – The end of range (inclusive)

  • size (float) – The length of range

send_tpsa(monos: ndarray, coefficients: ndarray)

Send the monomials and coefficients of a TPSA to MAD

The combination of monomials and coefficients creates a table representing the TPSA object in MAD-NG.

Parameters:
  • monos (ndarray) – A list of monomials in the TPSA.

  • coefficients (ndarray) – A list of coefficients in the TPSA.

Raises:
  • AssertionError – The list of monomials must be a 2-D array (each monomial is 1-D).

  • AssertionError – The number of monomials and coefficients must be identical.

  • AssertionError – The monomials must be of type 8-bit unsigned integer

send_vars(**vars: str | int | float | ndarray | bool | list)

Send variables to the MAD-NG process.

Send the variables in vars with the names in names to MAD-NG.

Parameters:

**vars (str/int/float/ndarray/bool/list) – The variables to send with the assigned name in MAD-NG by the keyword argument.

Raises:

Seesend().

Useful functions for MAD References

pymadng.madp_classes.madhl_obj.to_df(self, columns: list = None)

Converts the object to a pandas dataframe.

This function imports pandas and tfs-pandas, if tfs-pandas is not installed, it will only return a pandas dataframe.

Parameters:

columns (list, optional) – List of columns to include in the dataframe. Defaults to None.

Returns:

The dataframe containing the object’s data.

Return type:

pandas.DataFrame or tfs.TfsDataFrame