Tutorial : ElegantCommand class

This tutorial shows basic usage of the ElegantCommand class, which is used by the ElegantRun class to construct the Elegant command file.

[2]:
from pyelegantsdds.elegant_command import ElegantCommandFile
[3]:
# filetype is either 0 for binary or 1 for ascii
ec = ElegantCommandFile('test.ele')

Properties

[4]:
ec.__dict__
[4]:
{'commandlist': [],
 'filename': 'test.ele',
 'history': {},
 '_count_iter': count(0)}

Methods

[5]:
from types import FunctionType
[x for x, y in ElegantCommandFile.__dict__.items() if (type(y) == FunctionType) and not x.startswith('_')]
[5]:
['checkCommand',
 'addCommand',
 'modifyCommand',
 'repeatCommand',
 'clearHistory',
 'clear',
 'remove_command',
 'write']

Examples

[6]:
ec.filename, ec.commandlist
[6]:
('test.ele', [])

Adding a command.

[7]:
ec.addCommand(
    "run_setup",
    lattice='FODO.lte',
    use_beamline='FODO',
    p_central_mev=1700.00,
    default_order= 1,
    concat_order= 3,
    rootname="temp",
    parameters="%s.params",
    semaphore_file="%s.done",
    magnets="%s.mag",  # for plotting profile
)

ec.commandlist
[7]:
[{'NAME': 'run_setup',
  'NOTE': '',
  'lattice': 'FODO.lte',
  'use_beamline': 'FODO',
  'p_central_mev': 1700.0,
  'default_order': 1,
  'concat_order': 3,
  'rootname': 'temp',
  'parameters': '%s.params',
  'semaphore_file': '%s.done',
  'magnets': '%s.mag'}]

The modifyCommand method allows to modify command in the command list. The mode argument allows to select which command to modify based on the command name (‘last’,’first’, index as int).

[8]:
ec.modifyCommand('run_setup', mode='last',lattice='partrack.lte')
[9]:
ec.commandlist
[9]:
[{'NAME': 'run_setup',
  'NOTE': '',
  'lattice': 'partrack.lte',
  'use_beamline': 'FODO',
  'p_central_mev': 1700.0,
  'default_order': 1,
  'concat_order': 3,
  'rootname': 'temp',
  'parameters': '%s.params',
  'semaphore_file': '%s.done',
  'magnets': '%s.mag'}]

Commands can be repeated.

[10]:
ec.repeatCommand('run_setup',mode='last')
[11]:
ec.commandlist
[11]:
[{'NAME': 'run_setup',
  'NOTE': '',
  'lattice': 'partrack.lte',
  'use_beamline': 'FODO',
  'p_central_mev': 1700.0,
  'default_order': 1,
  'concat_order': 3,
  'rootname': 'temp',
  'parameters': '%s.params',
  'semaphore_file': '%s.done',
  'magnets': '%s.mag'},
 {'NAME': 'run_setup',
  'NOTE': '',
  'lattice': 'partrack.lte',
  'use_beamline': 'FODO',
  'p_central_mev': 1700.0,
  'default_order': 1,
  'concat_order': 3,
  'rootname': 'temp',
  'parameters': '%s.params',
  'semaphore_file': '%s.done',
  'magnets': '%s.mag'}]

Or removed.

[12]:
ec.remove_command('run_setup',mode='last')
[13]:
ec.commandlist
[13]:
[{'NAME': 'run_setup',
  'NOTE': '',
  'lattice': 'partrack.lte',
  'use_beamline': 'FODO',
  'p_central_mev': 1700.0,
  'default_order': 1,
  'concat_order': 3,
  'rootname': 'temp',
  'parameters': '%s.params',
  'semaphore_file': '%s.done',
  'magnets': '%s.mag'}]

The write method is used to write the command file and update the history adn clear the command list.

[14]:
ec.write()
[15]:
with open(ec.filename,'r') as f:
    dat=f.read()

print(dat)
&run_setup
        lattice             = partrack.lte,
        use_beamline        = FODO,
        p_central_mev       = 1700.0,
        default_order       = 1,
        concat_order        = 3,
        rootname            = temp,
        parameters          = %s.params,
        semaphore_file      = %s.done,
        magnets             = %s.mag,
&end


[16]:
ec.history
[16]:
{0: [{'NAME': 'run_setup',
   'NOTE': '',
   'lattice': 'partrack.lte',
   'use_beamline': 'FODO',
   'p_central_mev': 1700.0,
   'default_order': 1,
   'concat_order': 3,
   'rootname': 'temp',
   'parameters': '%s.params',
   'semaphore_file': '%s.done',
   'magnets': '%s.mag'}],
 1: [{'NAME': 'run_setup',
   'NOTE': '',
   'lattice': 'partrack.lte',
   'use_beamline': 'FODO',
   'p_central_mev': 1700.0,
   'default_order': 1,
   'concat_order': 3,
   'rootname': 'temp',
   'parameters': '%s.params',
   'semaphore_file': '%s.done',
   'magnets': '%s.mag'}]}
[17]:
ec.commandlist
[17]:
[]