Skip to content

Commit

Permalink
Merge branch 'examples/monitors' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
giadarol committed Apr 2, 2022
2 parents b1ac632 + 9407af6 commit 717ec62
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 160 deletions.
30 changes: 30 additions & 0 deletions examples/monitor/000_example_quick_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json

import xtrack as xt
import xpart as xp
import xobjects as xo

context = xo.ContextCpu()

with open('../../test_data/hllhc15_noerrors_nobb/line_and_particle.json') as f:
dct = json.load(f)
line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

tracker = line.build_tracker()

num_particles = 50
particles = xp.generate_matched_gaussian_bunch(tracker=tracker,
num_particles=num_particles,
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
sigma_z=9e-2)

num_turns = 30
tracker.track(particles, num_turns=num_turns,
turn_by_turn_monitor=True # enables all particles for all turns
)
# tracker.record_last_track contains the measured data. For example,
# tracker.record_last_track.x contains the x coordinate for all particles
# and all turns, e.g. tracker.record_last_track.x[3, 5] for the particle
# having particle_id = 3 and for the turn number 5.
80 changes: 0 additions & 80 deletions examples/monitor/000_monitor.py

This file was deleted.

35 changes: 35 additions & 0 deletions examples/monitor/001_example_custom_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json

import xtrack as xt
import xpart as xp
import xobjects as xo

context = xo.ContextCpu()

with open('../../test_data/hllhc15_noerrors_nobb/line_and_particle.json') as f:
dct = json.load(f)
line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

tracker = line.build_tracker()

num_particles = 50
particles = xp.generate_matched_gaussian_bunch(tracker=tracker,
num_particles=num_particles,
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
sigma_z=9e-2)

num_turns = 30
monitor = xt.ParticlesMonitor(_context=context,
start_at_turn=5, stop_at_turn=15,
num_particles=num_particles)
tracker.track(particles, num_turns=num_turns,
turn_by_turn_monitor=monitor
)
# tracker.record_last_track contains the measured data. For example,
# tracker.record_last_track.x contains the x coordinate for all particles
# and the selected turns, e.g. tracker.record_last_track.x[3, 5] gives the
# x coordinates for the particle having particle_id = 3 and for the fifth
# recorded turn. The turn indeces that are recorded can be inspected in
# tracker.record_last_track.at_turn
80 changes: 0 additions & 80 deletions examples/monitor/001_monitor_as_beam_element.py

This file was deleted.

38 changes: 38 additions & 0 deletions examples/monitor/002_example_custom_monitor_multiframe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json

import xtrack as xt
import xpart as xp
import xobjects as xo

context = xo.ContextCpu()

with open('../../test_data/hllhc15_noerrors_nobb/line_and_particle.json') as f:
dct = json.load(f)
line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

tracker = line.build_tracker()

num_particles = 50
particles = xp.generate_matched_gaussian_bunch(tracker=tracker,
num_particles=num_particles,
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
sigma_z=9e-2)

num_turns = 100
monitor = xt.ParticlesMonitor(_context=context,
start_at_turn=5, stop_at_turn=10,
n_repetitions=3, # <--
repetition_period=20, # <--
num_particles=num_particles)
tracker.track(particles, num_turns=num_turns,
turn_by_turn_monitor=monitor)

# tracker.record_last_track contains the measured data. For all particles
# variables the first index provides the frame index.
# For example, tracker.record_last_track.x[0, :, :] contains the recorded
# x position for the turns 5 to 10, tracker.record_last_track.x[1, :, :]
# contains the recorded x position for the turns 25 to 30, etc.
# The turn indeces that are recorded can be inspected in
# tracker.record_last_track.at_turn.
39 changes: 39 additions & 0 deletions examples/monitor/003_monitors_as_beam_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json

import xtrack as xt
import xpart as xp
import xobjects as xo

context = xo.ContextCpu()

with open('../../test_data/hllhc15_noerrors_nobb/line_and_particle.json') as f:
dct = json.load(f)
line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

num_particles = 50
monitor_ip5 = xt.ParticlesMonitor(start_at_turn=5, stop_at_turn=15,
num_particles=num_particles)
monitor_ip8 = xt.ParticlesMonitor(start_at_turn=5, stop_at_turn=15,
num_particles=num_particles)
line.insert_element(index='ip5', element=monitor_ip5, name='mymon5')
line.insert_element(index='ip8', element=monitor_ip8, name='mymon8')

tracker = line.build_tracker()

particles = xp.generate_matched_gaussian_bunch(tracker=tracker,
num_particles=num_particles,
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
sigma_z=9e-2)

num_turns = 30
monitor = xt.ParticlesMonitor(_context=context,
start_at_turn=5, stop_at_turn=15,
num_particles=num_particles)
tracker.track(particles, num_turns=num_turns)

# monitor_ip5 contains the data recorded in before the element 'ip5', while
# monitor_ip8 contains the data recorded in before the element 'ip8'
# The element index at which the recording is made can be inspected in
# monitor_ip5.at_element.
39 changes: 39 additions & 0 deletions examples/monitor/004_monitor_standalone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json

import xtrack as xt
import xpart as xp
import xobjects as xo

context = xo.ContextCpu()

with open('../../test_data/hllhc15_noerrors_nobb/line_and_particle.json') as f:
dct = json.load(f)
line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

tracker = line.build_tracker()

num_particles = 50
particles = xp.generate_matched_gaussian_bunch(tracker=tracker,
num_particles=num_particles,
nemitt_x=2.5e-6,
nemitt_y=2.5e-6,
sigma_z=9e-2)

num_turns = 100
monitor = xt.ParticlesMonitor(_context=context,
start_at_turn=5, stop_at_turn=10,
n_repetitions=3, # <--
repetition_period=20, # <--
num_particles=num_particles)
for iturn in range(num_turns):
monitor.track(particles)
tracker.track(particles)

# monitor contains the measured data. For all particles
# variables the first index provides the frame index.
# For example, monitor.x[0, :, :] contains the recorded
# x position for the turns 5 to 10, monitor.x[1, :, :]
# contains the recorded x position for the turns 25 to 30, etc.
# The turn indeces that are recorded can be inspected in
# monitor.at_turn.

0 comments on commit 717ec62

Please sign in to comment.