Skip to content

lomips/cpymad

 
 

Repository files navigation

CPyMAD

Latest Version Downloads License Supported Python versions

CPyMAD is a Cython binding to MAD-X.

MAD-X is a software package to simulate particle accelerators and is used at CERN and all around the world. It has its own proprietary scripting language and is usually launched from the command line.

This version of CPyMAD is tested with MAD-X 5.02.08. Other MAD-X versions (and immediate revisions) might work too, but are more likely to cause problems.

The installable wheel archives that are provided for some versions of python to simplify the installation on windows contain a precompiled version of CPyMAD that is statically linked against MAD-X 5.02.08.

Disclaimer

This is a heavily modified fork of the cern-cpymad package. The fork is not authored or maintained by CERN members.

CPyMAD links against an unofficial build of MAD-X that is not supported by CERN, i.e. this binary may have problems that the official binary does not have and vice versa. This means:

  • Only report issues to CERN that can be reproduced with their official command line client.
  • Only report issues here if they cannot be reproduced with their official command line client.

See Reporting issues.

Project pages

Usage

The Madx class provides a basic binding to the MAD-X interpreter:

from cpymad.madx import Madx

# Start a MAD-X interpretor. All MAD-X commands issued via cpymad will
# be logged to `command_log`:
madx = Madx(command_log="log.madx")

# Show the version of MAD-X that is actually loaded:
print(madx.version)

# Execute one of your predefined MAD-X files:
madx.call('/path/to/some/input_file.madx')

# Only a handful of MAD-X methods are exposed as methods. For others,
# you can use the `command` attribute. For example, to set a beam:
madx.command.beam(sequence='myseq1', particle='PROTON')

# Calculate TWISS parameters:
twiss = madx.twiss(sequence='LEBT',
                   betx=0.1, bety=0.1,
                   alfx=0.1, alfy=0.1)

# Your own analysis below:
from matplotlib import pyplot as plt
plt.plot(twiss['s'], twiss['betx'])
plt.show()

There are alternative syntaxes for the extreme cases where you need more fine grained control over the command string composition or where command fails to do the right thing:

# can't use `global` as attribute, since it's a python keyword:
madx.command.global_(sequence='cassps', Q1=26.58)

# can't use `: ` as attribute:
madx.command('QP: QUADRUPOLE', AT=2, L=1)

# issue a plain text command, don't forget the semicolon!
madx.input('FOO, BAR=[baz], QUX=<NORF>;')

See http://hibtc.github.io/cpymad for further documentation.

Known issues

On windows with python3.3, there is currently no satisfying way to close file handles in the MAD-X process or prevent them from being inherited by default. You have to make sure on your own that you close all file handles before creating a new cpymad.madx.Madx instance!

Hacking

Try to be consistent with the PEP8 guidelines. Add unit tests for all non-trivial functionality. Dependency injection is a great pattern to keep modules testable.

Commits should be reversible, independent units if possible. Use descriptive titles and also add an explaining commit message unless the modification is trivial. See also: A Note About Git Commit Messages.

Reporting issues

If you have a problem with a sequence file, first try to check if that problem remains when using the MAD-X command line client distributed by CERN, then:

  • Report the issue to CERN only if it be reproduced with their official command line client.
  • Report the issue here only if it cannot be reproduced with their official command line client.

For issues regarding the cpymad code itself or usage information, I'm happy to answer. Just keep in mind to be precise, specific, concise and provide all the necessary information.

See also:

Tests

Currently, tests run on:

  • The Travis CI service is mainly used to check that the unit tests for pymad itself execute on several python versions. Python{2.7,3.3} are supported. The tests are executed on any update of an upstream branch. The Travis builds use a unofficial precompiled libmadx-dev package to avoid having to rebuild the entire MAD-X library on each invocation.

    Build Status Coverage

Packages

No packages published

Languages

  • Python 100.0%