Bragg Peaks in MATLAB

I thought I would post some code I just wrote that implements the Bethe-Bloch slowing formula in MATLAB. The main program is protonstopping.m, which shows how the underlying functions can be used. In particular, you can obtain PDG range and loss rates in several ways.

As always with research software, you should use this code at your own risk, i.e. you should check its validity against your particular needs.

Code is here (.zip archive).

Running MAD8 on OSX 10.8 Mountain Lion

If you’ve worked in accelerator physics in any significant way, then you will have heard of MAD (‘Methodical Accelerator Design’) written at CERN many, many years ago, but still widely used for optics design, matching, and various tracking and nonlinear work. Quite a few working accelerators have been designed using various versions of it, including the SRS, DIAMOND, ALICE, and some parts of CLIC; I know these examples because I used MAD for them!

MAD8, the older-style code written in Fortran, has given way to MADX (it’s not considered polite to ask what happened to MAD9, but its legacy lives on in several other codes), and support for MAD8 has effectively stopped, with a few legacy versions floating around. SLAC implemented linac-style acceleration into their version 8.23acc, and a long time ago I did an optimised (and slightly debugged) version of MAD8 for Windows called 8.23dl. (By the way, you can tell which version you’re using from the code version at the top of any plot, so I know who you are if you’re using my version!). There are slight differences, but more or less you can trust what comes out of them.

MADX has taken over, but there are still reasons to use MAD8 from time to time. Most people who have used MAD8 have a love/hate relationship with it: they’ve had to get used to the code and its idiosyncrasies, but the combination of the simple XSIF file format and the relatively nice command structure means you can ‘get stuff done’. This is particularly true if you have some old input files that you don’t want to bother to convert to MADX format (yes, I hate converting files).

Whilst there are installers for MAD8 on Windows (my old friend James Jones has a really nice integrated package), and Linux is basically no problem, us poor old Mac users tend to be the poor cousins, especially with new OSes like 10.8. But fear not, here is a recipe for getting MAD8 up and running on OSX 10.8 Mountain Lion.

The procedure is as follows:

That’s it! You should now be able to run MAD8. I’ve included an input file for the old Daresbury SRS called srs.mff, which you can get here: srs.mff.

You can now run MAD8 using e.g.:
./mad8s < srs.mff > echo.txt

This should create a mad.ps file that you can load, and a print file that you can see your output in. Plenty of tools are available to work these files.

Hope that helps!

Installing Python for scientific computing on OSX

Python is an excellent programming language, in that it has decent structure, is quite easy to learn, and is easy to read. It’s also available as a default install on Mac OSX. Most people apparently are using Python 2.6/2.7 rather than Python 3, and this seems to be particularly true for researchers doing scientific computing. However, the basic install of Python does not contain decent libraries that allow scientific computing out-of-the-box, and a number of packages need to be installed – this is likely to be confusing for the starting user.

Whilst there are a number of packages available that are useful for scientific computing, the ones you really ought to know about above all others are NumPy, SciPy, and matplotlib. These, together with a few others, make Python more-or-less have the same capabilities as integrated language environments such as Matlab. Actually, having used both Matlab and Python, I think that both have advantages and disadvantages. They’re both quirky, and thus confusing when starting out. With Python, the question is how to get up and running conveniently for scientific computing with the least amount of effort. This post is an attempt to do a walk-through of that process. In the following guide I am using OSX 10.8 Mountain Lion, but this should work also in 10.6 and 10.7.

  1. Obtain EPDFree from Enthought, and install it. EPD – the Enthought Python Distribution – replaces your default Python install with another one, that includes all the numerical packages you might ever need. When I downloaded it I used version version py25-4.0.30002.
  2. Download Eclipse Classic. This will be the IDE (Integrated Development Environment) for your programs. I used version 4.2.1, 32 Bit.
  3. Update Eclipse by doing
    Help>Install New Software>Work with: http://pydev.org/updates/
    Check only the PyDev box, and select ‘Next
    and install only PyDev for Eclipse
  4. Under Preferences>PyDev>Interpreter – Python>New…
    Interpreter Name: EPDPython
    Interpreter Executable: /Library/Frameworks/Python.framework/Versions/7.3/bin/python2.7
    Make sure you’ve clicked ‘Python‘ and not anything else.
    Click Ok to process a load of links….(use the default choices)

That should be it. To test the code, you can create a new project by doing:
File>New>Project…>PyDev>PyDevProject
Call it ‘HelloWorld‘ or something similar
New>File
Call it helloworld.py
Enter some Python code
Click the green ‘run/play’ button to run it direct from the IDE, and run as ‘Python Run

Update: you’ll also probably want to run IPython Notebooks in your browser. The standard EPDFree needs to be udpated to have the latest version of IPython. I followed the instructions here, and have copied them below:

  • sudo enpkg enstaller
  • sudo enpkg ipython
  • sudo enpkg pandas

That’s it! To run IPython in a browser notebook, just execute the following from the Terminal command line:

ipython notebook –pylab=inline

 

RCUK Careers in Research Case Studies

A little while ago I took part in the RCUK Case Study of research careers in the UK. It’s interesting to read the stories of different people – I seem to be one of the older people who answered the survey questions! Anyway, you can look at the case studies, and even read mine if you want! I’ve copied it below.

Producing beams of high-energy radiation is increasingly important both for research and industry. Particle accelerators use electromagnetic fields to propel charged particles, such as protons or electrons at very high speeds to form these beams, which have a wide range of uses. This includes understanding the laws of physics and treating cancer. Hywel Owen describes how his career path owed a lot to chance.

“Research helps to ennoble man and to create the conditions in which one day there will be no hunger and where all our material needs will be met. If we reduce want, we lessen the chances of war.”

My work focuses on design, simulation and experimentation involving particle accelerators. I’m a Lecturer in the Accelerator Group at the University of Manchester’s School of Physics and Astronomy, and a member of the Cockcroft Institute for Accelerator Science. I teach Undergraduates and postgraduates, manage research grants and staff, and oversee safety. I have been closely involved in most of the particle accelerators constructed in the UK over the past 20 years and am an adviser for a range of programmes.

My parents are both teachers with strong beliefs in the benefit of education. I rebelled against them by going into physics instead of music, which they both taught. I still love music though. As a child, I had good scientific toys: luminous stars on the ceiling, a microscope, a chemistry set, a ZX81 computer and lots of books. I also had a passion for museums, and frequently visited the Science and Natural History Museums. In particular I liked the Foucault pendulum and computer exhibitions, which I still remember today. As a teenager, I had good social guidance from my local sports club and was mentored by a genetics lecturer who remains a friend. I was also very fortunate to go to a good school where it was assumed you would be academic. The teaching was superb, but the careers advice was less good – I would have benefited from more structured guidance both from school and my parents.

Personally, I don’t believe in the standard idea of a ‘career’ as a progression from one point to the next. But I do think it is a good idea to try out a number of things early on, build your experience and find out what path is best for you.

My path went from a first degree in Physics to a PhD in Physics, both in Manchester. When that finished, there were few industry jobs in my specialist area – liquid crystals – and limited postdoctoral positions in the UK, so I looked at other physics topics. By chance, I found a great post working for the Daresbury Laboratory on the design of the DIAMOND synchrotron, which was a hugely enjoyable project for seven years. After DIAMOND, there was a succession of projects which did not get off the ground, and I decided to look for an academic post, ending up in Manchester where I had first started my research career.

I love designing things that make the world a better place, and this forms the basis of my motivation in life. I probably differ from most academics, in that I don’t have a particular passion to ‘find out the truth’. I am quite happy not knowing it. But I do care if there is ‘a point’ to my work.

Installing Elegant on OSX 10.8 Mountain Lion

This is a short guide to installing the accelerator code Elegant on OSX 10.8 Mountain Lion, hopefully giving an error-free, step-by-step guide with a bit more information than is available from the Elegant website and forums.

OS Version: OSX 10.8 Mountain Lion
Elegant Version: 25.1.0 (Nov 7, 2012)

Installation Steps:

  1. Install XCode and Java if you don’t already have them installed (see finkers.wordpress.com for details); use the App Store to install XCode. You will probably want to disable Java in your browser after installation.
  2. Install Command Line Tools at XCode>Preferences>Downloads>Components
  3. Install fink binaries using
    http://finkers.wordpress.com/installing-fink/#install-fast.install
    fink-0.34.5.tar.gz
  4. Carry out the following commands:
    fink selfupdate-rsync
    fink index -f
    fink update-all
  5. Install gfortran using this installer:
    http://cran.r-project.org/bin/macosx/tools/gfortran-4.2.3.dmg
  6. Install elegant (darwin-x86 32/64 bit Nov 8 2012) and SDDS binaries (darwin-x86 Nov 7 2012) from the APS software website. Put everything into a folder in your home directory, i.e. in ~/bin/
  7. Download the defns.rpn file and put it in ~/bin/ as well.
  8. You will probably want the SDDS.m file as well if you’re going to be using Mathematica.
  9. Add this line to your .bash_profile:
    export RPN_DEFNS=”~bin/defns.rpn”
    (hint: cp .bash_profile vis.bash_profile to make it visible for editing, then copy back)
  10. Following the instructions on this site,
    http://www.slac.stanford.edu/~hogan/elegant.html,
    execute the following commands:
    fink install libpng3 <- see update below, this doesn’t seem to be needed now.
    fink install openmotif3
    fink install readline5
    fink install gd2      (…this one takes ages)
    fink install libtiff

That should be it! Quit and restart your Terminal.app, and you should be able to run elegant from the command line.

Update July 25th 2013: I recently re-installed everything, and noticed that fink-0.34.5 doesn’t seem to be able to find libpng3 when you run fink install libpng3. I tested running without this library, and elegant seems to be happily making png files without this library.

 

Accelerating through life