py4sci

Previous topic

cern.cpymad.madx

This Page

cern.cpymad.model

This module provides a model class which is conceptually derived from the jmad models, but uses cern.cpymad.madx.Madx as the backend. See, Model example.

Cython implementation of the model api.

Module author: Yngve Inntjore Levinsen <Yngve.Inntjore.Levinsen@cern.ch>

class cern.cpymad.model.Model(model, sequence='', optics='', histfile='', madx=None, logger=None)[source]

Model class implementation. the model spawns a madx instance in a separate process. this has the advantage that you can run separate models which do not affect each other.

To load a Model object using the default ModelLocator use the static constructor method Model.from_name. The default constructor should be invoked with a ModelData instance.

aperture(sequence='', madrange='', columns='name, l, s, n1, aper_1, aper_2, aper_3, aper_4', fname='', use=False)[source]

Get the aperture from the model.

Parameters:
  • sequence (string) – Sequence, if empty, using active sequence.
  • madrange (string) – Range, if empty, the full sequence is chosen.
  • columns (string) – Columns in the twiss table, can also be list of strings
  • fname (string) – Optionally, give name of file for tfs table.
  • use (bool) – Call use before aperture.
call(filepath)[source]

Call a file in Mad-X. Give either full file path or relative.

evaluate(expr)[source]

Evaluate a string expression and return the resulting float.

Parameters:expr (string) –
classmethod from_name(model, *args, **kwargs)[source]

Create a Model object from its name.

Parameters:
  • model (string) – Name of the model
  • args (tuple) – Positional parameters as needed by __init__
  • kwargs (dict) – Keyword parameters as needed by __init__

This constructor is provided for backward compatibility. To specify where your model data is loaded from you should create and use your own custom cern.cpymad.model_locator.ModelLocator.

get_beam(bname)[source]

Returns the beam definition in form of a dictionary.

You can then change parameters in this dictionary as you see fit, and use set_beam() to activate that beam.

get_sequence_names()[source]

Return list of all loaded sequences.

get_sequences()[source]

Returns a list of loaded sequences.

has_optics(optics)[source]

Check if model has the optics.

Parameters:optics (string) – Optics name to be checked.
has_sequence(sequence)[source]

Check if model has the sequence.

Parameters:sequence (string) – Sequence name to be checked.
list_beams()[source]

Returns an iterable of available beams

list_optics()[source]

Returns an iterable of available optics

list_ranges(sequence=None)[source]

Returns a list of available ranges for the sequence. If sequence is not given, returns a dictionary structured as {sequence1:[range1,range2,...],sequence2:...}

Parameters:sequence (string) – sequence name.
match(constraints, vary, weight=None, method=['lmdif'], sequence='', fname='')[source]

Perform a matching operation.

See cern.madx.match() for a description of the parameters.

set_beam(beam_dict)[source]

Set the beam from a beam definition (dictionary)

set_optic(optic)[source]

Set new optics.

Parameters:optics (string) – Optics name.
Raises KeyError:
 In case you try to set an optics not available in model.
set_range(madrange='')[source]

Sets the active range to madrange. Must be defined in the currently active sequence... If madrange is empty, sets the range to default-range unless another range is already set.

set_sequence(sequence='', madrange='')[source]

Set a new active sequence...

survey(sequence='', columns='name, l, s, angle, x, y, z, theta', madrange='', fname='', use=True)[source]

Run a survey on the model.

Parameters:
  • sequence (string) – Sequence, if empty, using active sequence.
  • columns (string) – Columns in the twiss table, can also be list of strings
  • fname (string) – Optionally, give name of file for tfs table.
  • use (bool) – Call use before survey.
twiss(sequence='', columns=['name', 's', 'betx', 'bety', 'x', 'y', 'dx', 'dy', 'px', 'py', 'mux', 'muy', 'l', 'k1l', 'angle', 'k2l'], pattern=['full'], madrange='', fname='', use=True)[source]

Run a TWISS on the model.

Warning for ranges: Currently TWISS with initial conditions is NOT implemented!

Parameters:
  • sequence (string) – Sequence, if empty, using active sequence.
  • columns (string) – Columns in the twiss table, can also be list of strings
  • madrange (string) – Optional, give name of a range defined for the model.
  • fname (string) – Optionally, give name of file for tfs table.
  • use (bool) – Call use before twiss.
cern.cpymad.model.save_model(model_def, filename)[source]

Saves the model definition defined by the dictionary model_def to file filename. The file is in YAML format (or JSON). We do not check that the model is valid in any way.

It is recommended that you rather use the modeldefs.model.save_model, once it is ready.