Code Monkey home page Code Monkey logo

jmad-core's Introduction

Latest release Build Status License codecov Codacy code quality

JMad

JMad is a Java API for the MadX software, which is used at CERN and in many other accelerator labs to simulate particle accelerators. JMad plays a key role in CERNs accelerator control system as it is bridges numerical simulations with the control system of the real CERN accelerators.

Getting Started

In order to use JMad in your java projects add the following to your build files:

Maven:

<dependency>
    <groupId>io.jmad</groupId>
    <artifactId>jmad-core</artifactId>
    <version>X.Y.Z</version>
</dependency>

Gradle:

compile 'io.jmad:jmad-core:X.Y.Z'

IMPORTANT NOTE

As of version 0.5.0, the groupId of JMad has changed fromjmad to io.jmad!

Using a model

JMad comes with a concept called 'Model Definitions'. A model definition specifies which madx files have to be loaded for a certain accelerator and certain optics (for madx users: sequence files, strength files, etc...). Model definitions can be created programmatically or be loaded from different sources: zip files, git repos or jars in the classpath. For this example, we will use the latter one.

jmad-core comes with one model definition built in in its jar itself. To get a new instance of a model using this model definition we have to do the following:

/* create a new JMad service */
JMadService jmadService = JMadServiceFactory.createJMadService();

/* then find a model definition */
JMadModelDefinition modelDefinition = jmadService.getModelDefinitionManager()
                                                 .getModelDefinition("example");

/* create the model and initialize it */
JMadModel model = jmadService.createModel(modelDefinition);
model.init();

After this we can use the model. For example we can retrieve all the elements the sequence range:

/* get all the elements */
List<Element> elements = model.getActiveRange().getElements();

/* print name and type of each element */
for (Element element : elements) {
    System.out.println("name: " + element.getName() + "; type: " + JMadElementType.fromElement(element));
}

... we can retrieve the actual optics, change some quadrupole and recalculate the optics:

Range activeRange = model.getActiveRange();

/* retrieve an element by name (MONITOR) */
Monitor monitor = (Monitor) activeRange.getElement("BPMIH.22604");

/* retrieve the actual optics */
Optic optic = model.getOptics();

/* retrieve some optics values */
List<Double> betaxValues = optic.getValues(MadxTwissVariable.BETX, activeRange.getElements());

/* retrieve optics values for one element */
OpticPoint opticPoint = optic.getPoint(monitor);
double monX = opticPoint.getX();

/* change a quad strength by 10 percent */
Quadrupole aQuad = (Quadrupole) activeRange.getElement("MQIF.20400");
aQuad.setK1(aQuad.getK1() * 1.1);

/* IMPORTANT: refetch the optic since it has changed! */
optic = model.getOptics();

... or we can perform a custom twiss:

TfsResultRequestImpl request = new TfsResultRequestImpl();

/* a regexp for the elements we want to retrieve */
request.addElementFilter("BPM.*");

/* and the variables, which we want to get */
request.addVariable(MadxTwissVariable.NAME);
request.addVariable(MadxTwissVariable.X);
request.addVariable(MadxTwissVariable.Y);

/* run the twiss and get the results */
TfsResult result = model.twiss(request);

List<String> elementNames = result.getStringData(MadxTwissVariable.NAME);
List<Double> xValues = result.getDoubleData(MadxTwissVariable.X);
List<Double> yValues = result.getDoubleData(MadxTwissVariable.Y);

/* print the values */
for (String name : elementNames) {
    int index = result.getElementIndex(name);
    System.out.println(name + ": X=" + xValues.get(index) + "; Y=" + yValues.get(index) + ";");
}

To close a model we have to call:

model.cleanup();

Additional Components

  • There is also a Swing GUI available, which runs on top of jmad and allows to interactively change model parameters, plot optics functions and can be embedded easily in other GUIs.
  • To Retrieve model definitions from git repositories, the project jmad-modelpack-service can be used (works currently only with gitlab repos)
  • pyjmad is a wrapper around jmad with which all the features of jmad (including model definitions, GUI and retrieval from git repos) can be used as library in python.

Further Reading

Build Artifacts of the Latest Version

jmad-core's People

Contributors

andreacalia avatar delph74 avatar eothred avatar kaifox avatar michi42 avatar nowakseb avatar xbuffat avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jmad-core's Issues

Extract model files into kernel-dependant temp dir

To avoid concurrency issues, the temporary model files must not be shared between kernel instances. (Different kernels might operate on different versions of models ... so would overwrite each others files)

Take into account initial conditions for matching in lines

Currently matching in jmad does not work for lines, because the initial conditions are not taken into account. The mechanics should be exactly the same as in twiss: If there are initial conditions available, they should be used. Otherwise a closed orbit solution can be used...

MAD-X no longer provides 32 bit binaries

Upon upgrading the MAD-X binaries, I noticed that since version 5.04.00, MAD-X no longer provides 32 bit binaries: http://madx.web.cern.ch/madx/releases/5.04.00/

Probably this is a side-effect of changing to C++11 (according to the changelog).

For now, I've left in the old binaries for 32 bit, which is not ideal as they might not support some features and so the behaviour might differ between 32 bit and 64 bit OS. Do we still need to support 32 bit architectures in JMad?

Added/removed strengths from StrengthVarManager are not reflected in model

At the moment, adding or removing strengths to or from a StrengthVarManager in a model (using get StrengthVarManager().getStrengthVarSet(). addAllStrengths()) is not reflected in the model: the resulting strengths can be changed, but changes are never passed to MAD-X.

The reason for this behavior is that JMadModelImpl has to register its listener for each strength to be passed to MAD-X. The only happens on initialization but not when the contents of the StrengthVarManager are modified.

To fix this, we would probably have to provide a listener in StrengthVarSet to observe strengths added/removed, and use this in JMadModelImpl to register the listener on the newly added strenghts (or remove it from deleted strengths, respectively).
In a second step, observing and concentrating the changes for all contained strengths could be completely delegated to StrengthVarSet, and the model would only add a single listener to StrengthVarSet (strengthValueChanged(Strength, newValue)) instead of tracking each Strength individually.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.