Table Of Contents

Previous topic


Next topic

Using an internal lattice definition: fodo_simple2

This Page

A first example: fodo_simple1

In this example, we will simulate a matched bunch passing through a FODO cell. The FODO cell is defined by the file, which uses Mad8 format.

focus := 7                   ! [m]     : focal length of equivalent
                             !         :   thin quad
sepn := 10                   ! [m]     : distance between quad centers
length := 2.0                ! [m]     : quadrupole length
strength := 1/(focus*length) ! [m**-2] : quadrupole strength
                             !         :   = B'/brho

beam, particle=proton, energy=1.5

o: drift, l=( sepn - length )
f: quadrupole, l=length, k1=strength
d: quadrupole, l=length, k1=(-strength)

fodo:  line=( f, o, d, o )

The simulation itself is defined by the Python script

# !/usr/bin/env synergia
import synergia

from import Four_momentum, Reference_particle, pconstants
from synergia.lattice import Mad8_reader, Lattice
from synergia.bunch import Bunch, Diagnostics_basic 
from synergia.simulation import Independent_stepper_elements, Bunch_simulator, \

# Define a lattice
#     Read the lattice named "fodo" from the Mad8 file ""
lattice = synergia.lattice.Mad8_reader().get_lattice("fodo", "")

# Define a set of simulation steps
map_order = 1
steps_per_element = 2
stepper = Independent_stepper_elements(lattice, map_order, steps_per_element)

# Define a bunch
x_emit = 1.0e-6  # m-rad, RMS
y_emit = 1.0e-6  # m-rad, RMS
z_std = 0.01  # m
dpop = 1.0e-4  # unitless, RMS \frac{\delta p}{p_{tot}}
real_particles = 1.2e12  # unitless, meaningless in this simulation
                         #           without collective effects
macro_particles = 50000
seed = 1415926  # random number seed; 0 for automatic calculation (GSL)
bunch = synergia.optics.generate_matched_bunch_transverse(
              x_emit, y_emit, z_std, dpop,
              real_particles, macro_particles,

# Define a bunch simulator
bunch_simulator = Bunch_simulator(bunch)

# Define a set of bunch diagnostics
#     Apply basic diagnostics every step
diagnostics = Diagnostics_basic("diagnostics.h5")

# Perform the simulation
propagator = Propagator(stepper)
turns = 1  # a single pass through the line, since this isn't a ring
max_turns = 0 # Number of turns to run before writing checkpoint and stopping
              # When max_turns is 0, the simulation continues until the end.
verbosity = 2  # Display information about each simulation step
propagator.propagate(bunch_simulator, turns, max_turns, verbosity)

Lattice definition

The synergia.lattice.Mad8_reader class reads lattice files in Mad8 format and produces an object of type Lattice, which contains Synergia’s description of the accelerator lattice. A MadX_reader class is also available. Synergia can be extended with new reader classes.

Simulation step definition

A Synergia simulation is defined by a set of steps. These steps are produced by a class derived from Stepper. This example uses the simplest stepper, Independent_stepper_elements, which performs a fixed number of steps per thick element and one step per thin element.

Bunch definition

A Synergia Bunch contains a set of macroparticles representing a given number of real particles.

This example uses synergia.optics.generate_matched_bunch_transverse(), which combines bunch generation with filling the bunch with a set of macroparticles matched to a simulation. This interface will be changed before the Synergia 2.1 final release.

Bunch simulator definition

Synergia uses the Bunch_simulator class to combine objects of type Bunch with corresponding diagnostics objects.

Bunch diagnostic definition

Synergia has contains a variety of measurements to be applied to bunches at various points in the simulation. The simulation applies measurements via classes derived from Diagnostics that have been added to the Bunch_simulator. In this example we have decided to use Diagnostics_basic, which creates a basic set of diagnostics including means and standard deviations in all six phase-space degrees of freedom. The diagnostics will be applied every step. Many more options are available. See the various classes derived from Diagnostics and the various add_per methods of Bunch_simulator.

Performing the simulation

The Propagator class actually performs the simulation. It applies the steps from the Stepper class a number of times given by the turns parameter. The Propagator class performs propagation, diagnostics, arbitrary actions and checkpointing. The detail of the output to the screen is determined by the value of the verbosity parameter. Simulation progress is always written to the file log.

Running the simulation

Run the simulation using the :code:synergia executable:


which produces the following output:

Propagator: starting turn 1
Propagator:     step 1/8, s=1.0000, macroparticles = 50000, time = 0.025s
Propagator:     step 2/8, s=2.0000, macroparticles = 50000, time = 0.022s
Propagator:     step 3/8, s=6.0000, macroparticles = 50000, time = 0.023s
Propagator:     step 4/8, s=10.0000, macroparticles = 50000, time = 0.019s
Propagator:     step 5/8, s=11.0000, macroparticles = 50000, time = 0.022s
Propagator:     step 6/8, s=12.0000, macroparticles = 50000, time = 0.025s
Propagator:     step 7/8, s=16.0000, macroparticles = 50000, time = 0.022s
Propagator:     step 8/8, s=20.0000, macroparticles = 50000, time = 0.023s
Propagator: turn 1/1, macroparticles = 50000, time = 0.18s

Viewing the results

The output of the simulation is contained in the HDF5 file diagnostics.h5. The HDF5 format is a widely-adopted standard; the data in diagnostics.h5 can be analyzed using a number of standard applications, or in end-user written code. For simple analysis, Synergia2 includes several scripts for analyzing the output of the various Diagnostics. The syndiagplot script can be used on diagnostics.h5, e.g.,

syndiagplot diagnostics.h5 --oneplot x_std y_std

which produces the following output: