From 0fd6df3711846e220dc6ac5a7c6c3cf38e7621ab Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 10:33:13 +0200 Subject: [PATCH 1/6] Monitor example --- examples/monitor/000_example_quick_monitor.py | 30 +++++++++++++++++++ .../{000_monitor.py => old_000_monitor.py} | 0 ....py => old_001_monitor_as_beam_element.py} | 0 3 files changed, 30 insertions(+) create mode 100644 examples/monitor/000_example_quick_monitor.py rename examples/monitor/{000_monitor.py => old_000_monitor.py} (100%) rename examples/monitor/{001_monitor_as_beam_element.py => old_001_monitor_as_beam_element.py} (100%) diff --git a/examples/monitor/000_example_quick_monitor.py b/examples/monitor/000_example_quick_monitor.py new file mode 100644 index 000000000..9a97bc797 --- /dev/null +++ b/examples/monitor/000_example_quick_monitor.py @@ -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.racker.record_last_track.x[3, 5] for the particle having +# particle_id = 3 and for the turn number 5 diff --git a/examples/monitor/000_monitor.py b/examples/monitor/old_000_monitor.py similarity index 100% rename from examples/monitor/000_monitor.py rename to examples/monitor/old_000_monitor.py diff --git a/examples/monitor/001_monitor_as_beam_element.py b/examples/monitor/old_001_monitor_as_beam_element.py similarity index 100% rename from examples/monitor/001_monitor_as_beam_element.py rename to examples/monitor/old_001_monitor_as_beam_element.py From 539379ec54c2ae18d20703d18e0d5ce314b16f0e Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 11:15:01 +0200 Subject: [PATCH 2/6] Examples for monitor --- examples/monitor/000_example_quick_monitor.py | 6 ++-- .../monitor/001_example_custom_monitor.py | 35 +++++++++++++++++++ .../002_example_custom_monitor_multiframe.py | 35 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 examples/monitor/001_example_custom_monitor.py create mode 100644 examples/monitor/002_example_custom_monitor_multiframe.py diff --git a/examples/monitor/000_example_quick_monitor.py b/examples/monitor/000_example_quick_monitor.py index 9a97bc797..982cb6a69 100644 --- a/examples/monitor/000_example_quick_monitor.py +++ b/examples/monitor/000_example_quick_monitor.py @@ -25,6 +25,6 @@ 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.racker.record_last_track.x[3, 5] for the particle having -# particle_id = 3 and for the turn number 5 +# `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. diff --git a/examples/monitor/001_example_custom_monitor.py b/examples/monitor/001_example_custom_monitor.py new file mode 100644 index 000000000..763a0c771 --- /dev/null +++ b/examples/monitor/001_example_custom_monitor.py @@ -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 # 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 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 diff --git a/examples/monitor/002_example_custom_monitor_multiframe.py b/examples/monitor/002_example_custom_monitor_multiframe.py new file mode 100644 index 000000000..c048319d1 --- /dev/null +++ b/examples/monitor/002_example_custom_monitor_multiframe.py @@ -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 # 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 the selected turns, i.e. `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`. From 7227949ae76b20e6d186decd942018ead24dcc7e Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 11:55:18 +0200 Subject: [PATCH 3/6] Still on monitor examples --- examples/monitor/000_example_quick_monitor.py | 6 ++--- .../monitor/001_example_custom_monitor.py | 12 +++++----- .../002_example_custom_monitor_multiframe.py | 23 +++++++++++-------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/examples/monitor/000_example_quick_monitor.py b/examples/monitor/000_example_quick_monitor.py index 982cb6a69..08afe9ab0 100644 --- a/examples/monitor/000_example_quick_monitor.py +++ b/examples/monitor/000_example_quick_monitor.py @@ -25,6 +25,6 @@ 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. +# 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. diff --git a/examples/monitor/001_example_custom_monitor.py b/examples/monitor/001_example_custom_monitor.py index 763a0c771..dcf07978d 100644 --- a/examples/monitor/001_example_custom_monitor.py +++ b/examples/monitor/001_example_custom_monitor.py @@ -25,11 +25,11 @@ start_at_turn=5, stop_at_turn=15, num_particles=num_particles) tracker.track(particles, num_turns=num_turns, - turn_by_turn_monitor=monitor # enables all particles for all 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 +# 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 diff --git a/examples/monitor/002_example_custom_monitor_multiframe.py b/examples/monitor/002_example_custom_monitor_multiframe.py index c048319d1..506ccd734 100644 --- a/examples/monitor/002_example_custom_monitor_multiframe.py +++ b/examples/monitor/002_example_custom_monitor_multiframe.py @@ -20,16 +20,19 @@ nemitt_y=2.5e-6, sigma_z=9e-2) -num_turns = 30 +num_turns = 100 monitor = xt.ParticlesMonitor(_context=context, - start_at_turn=5, stop_at_turn=15, - num_particles=num_particles) + 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 # enables all particles for all 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, i.e. `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`. +# 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. From 3f2ab0a80f7984e2a2ce95f071a2f28dd9784c3c Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 13:05:39 +0200 Subject: [PATCH 4/6] More on examples --- examples/monitor/002_example_custom_monitor_multiframe.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/monitor/002_example_custom_monitor_multiframe.py b/examples/monitor/002_example_custom_monitor_multiframe.py index 506ccd734..f415e9916 100644 --- a/examples/monitor/002_example_custom_monitor_multiframe.py +++ b/examples/monitor/002_example_custom_monitor_multiframe.py @@ -27,9 +27,9 @@ 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 + 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, :, :] From af49f9d79269cb57ef2684927063c7bb88d4f9b4 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 13:06:41 +0200 Subject: [PATCH 5/6] Add files --- .../monitor/003_monitors_as_beam_elements.py | 39 +++++++++++++++++++ examples/monitor/004_monitor_standalone.py | 39 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 examples/monitor/003_monitors_as_beam_elements.py create mode 100644 examples/monitor/004_monitor_standalone.py diff --git a/examples/monitor/003_monitors_as_beam_elements.py b/examples/monitor/003_monitors_as_beam_elements.py new file mode 100644 index 000000000..279ac65b0 --- /dev/null +++ b/examples/monitor/003_monitors_as_beam_elements.py @@ -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. \ No newline at end of file diff --git a/examples/monitor/004_monitor_standalone.py b/examples/monitor/004_monitor_standalone.py new file mode 100644 index 000000000..81dced34b --- /dev/null +++ b/examples/monitor/004_monitor_standalone.py @@ -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. From 9407af6ba898bdf15362dd168678ce4116bb3bd6 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 2 Apr 2022 14:32:11 +0200 Subject: [PATCH 6/6] Removed old files --- examples/monitor/old_000_monitor.py | 80 ------------------- .../old_001_monitor_as_beam_element.py | 80 ------------------- 2 files changed, 160 deletions(-) delete mode 100644 examples/monitor/old_000_monitor.py delete mode 100644 examples/monitor/old_001_monitor_as_beam_element.py diff --git a/examples/monitor/old_000_monitor.py b/examples/monitor/old_000_monitor.py deleted file mode 100644 index 8258fe20b..000000000 --- a/examples/monitor/old_000_monitor.py +++ /dev/null @@ -1,80 +0,0 @@ -import json - -import numpy as np - -import xtrack as xt -import xpart as xp -import xobjects as xo - -#context = xo.ContextPyopencl() -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(_context=context) - -num_particles = 50 -particles0 = 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) -##################################### -# Save all turns starting from zero # -##################################### - -# (particles.at_turn must be 0 at the beginning) -particles = particles0.copy() -num_turns = 500 -tracker.track(particles, num_turns=num_turns, turn_by_turn_monitor=True) -# ---> tracker.record_last_track.x (contains the z x coordinates) - -mon = tracker.record_last_track - -###################################### -# Save 10 turns starting from turn 5 # -###################################### - -# Build a monitor -monitor = xt.ParticlesMonitor(_context=context, - start_at_turn=5, stop_at_turn=15, - num_particles=num_particles) - -# (particles.at_turn must be 0 at the beginning) -particles = particles0.copy() -num_turns = 500 -tracker.track(particles, num_turns=num_turns, turn_by_turn_monitor=monitor) - -####################### -# Multi-frame monitor # -####################### - -# Repeated frames -monitor_multiframe = xt.ParticlesMonitor(_context=context, - start_at_turn=5, stop_at_turn=15, - n_repetitions=3, - repetition_period=100, - num_particles=num_particles) - - -# (particles.at_turn must be 0 at the beginning) -particles = particles0.copy() -num_turns = 500 -tracker.track(particles, num_turns=num_turns, - turn_by_turn_monitor=monitor_multiframe) - -assert np.all(mon.x.shape == np.array([50, 500])) -assert np.all(mon.at_turn[3, :] == np.arange(0, 500)) -assert np.all(mon.particle_id[:, 3] == np.arange(0, num_particles)) - -assert np.all(monitor.x.shape == np.array([50, 10])) -assert np.all(monitor.at_turn[3, :] == np.arange(5, 15)) -assert np.all(monitor.particle_id[:, 3] == np.arange(0, num_particles)) - -assert np.all(monitor_multiframe.x.shape == np.array([3, 50, 10])) -assert np.all(monitor_multiframe.at_turn[1, 3, :] == np.arange(105, 115)) -assert np.all(monitor_multiframe.particle_id[2, :, 3] == np.arange(0, num_particles)) - diff --git a/examples/monitor/old_001_monitor_as_beam_element.py b/examples/monitor/old_001_monitor_as_beam_element.py deleted file mode 100644 index dbd7a4a5d..000000000 --- a/examples/monitor/old_001_monitor_as_beam_element.py +++ /dev/null @@ -1,80 +0,0 @@ -import json - -import xtrack as xt -import xpart as xp - -from cpymad.madx import Madx - - -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 -particles0 = 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) -# ##################################### -# # Save all turns starting from zero # -# ##################################### -# -# # (particles.at_turn must be 0 at the beginning) -# particles = particles0.copy() -# num_turns = 500 -# tracker.track(particles, num_turns=num_turns, turn_by_turn_monitor=True) -# # ---> tracker.record_last_track.x (contains the z x coordinates) - -###################################### -# Save 10 turns starting from turn 5 # -###################################### - -# Build a monitor -monitor = xt.ParticlesMonitor(start_at_turn=5, stop_at_turn=15, - num_particles=num_particles) - -# (particles.at_turn must be 0 at the beginning) -particles = particles0.copy() -num_turns = 500 -tracker.track(particles, num_turns=num_turns, turn_by_turn_monitor=monitor) - -##################################################### -# Save 10 turns starting from turn 5 in ip5 and ip8 # -##################################################### - -# # Build a monitor -# 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_with_monitor = line.copy() -# line_with_monitor.insert_element(index='ip5', element=monitor_ip5, name='mymon5') -# line_with_monitor.insert_element(index='ip8', element=monitor_ip8, name='mymon8') -# -# tracker_w_monitor = line_with_monitor.build_tracker() -# -# particles = particles0.copy() -# num_turns = 500 -# tracker_w_monitor.track(particles, num_turns=num_turns) -# -# ####################### -# # Multi-frame monitor # -# ####################### - -# Repeated frames -monitor_multiframe = xt.ParticlesMonitor(start_at_turn=5, stop_at_turn=15, - n_repetitions=2, - repetition_period=100, - num_particles=num_particles) - - -# (particles.at_turn must be 0 at the beginning) -particles = particles0.copy() -num_turns = 500 -tracker.track(particles, num_turns=num_turns, - turn_by_turn_monitor=monitor_multiframe) \ No newline at end of file