Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
413fdde
Added methods for comparing beams and for printing out summary in the…
ben-c-2013 Mar 19, 2025
fd58b69
Added test for testing SourceBasic
ben-c-2013 Mar 19, 2025
e3ea280
Updates to test_sources.py
ben-c-2013 Mar 20, 2025
ea6b689
Added test_beam.py for testing the Beam class.
ben-c-2013 Mar 20, 2025
0e7d7d9
Small change in Beam.copy_particle_charge() to handle one of the beam…
ben-c-2013 Mar 20, 2025
9543833
Added more tests in test_beam.py.
ben-c-2013 Mar 20, 2025
5eae0e8
Added Beam class initiation tests to test_beam.py and added another t…
ben-c-2013 Mar 24, 2025
bcc9aa7
Edited __init__.py to add SourceCapsule.
ben-c-2013 Mar 24, 2025
18887a3
Added a function for setting up a SourceBasic in test_beam.py. Also a…
ben-c-2013 Mar 24, 2025
1e4f5c0
Saving some tests for beam coordinate rotation. Work in progress...
ben-c-2013 Mar 25, 2025
02a45a7
Added controls to the Beam class to ensure that energy, longitudinal …
ben-c-2013 Mar 25, 2025
0561094
Edited tests in test_beam.py to meet the requirement of energy, longi…
ben-c-2013 Mar 25, 2025
fcc2ce9
Moved a chunk of code (the rotation part) further down in beam.py. Al…
ben-c-2013 Mar 26, 2025
df96f88
Added several major tests for the beam coordinate system rotations.
ben-c-2013 Mar 26, 2025
00afa1f
Updates to the rotation tets in test_beam.py.
ben-c-2013 Mar 27, 2025
e9634d2
Changed proper_velocity2momentum() in relativity.py to calculate mome…
ben-c-2013 Mar 27, 2025
eb90604
Changed pxs(), pys() and pzs() to reflect the changes done to proper_…
ben-c-2013 Mar 27, 2025
95c8085
Added more tolerances to phase space tests and added a large amount o…
ben-c-2013 Mar 27, 2025
94ab1fe
Added tests for beam projection methods in test_beam.py.
ben-c-2013 Mar 28, 2025
74c8332
Minor change to Beam plotting methods and moved these further down in…
ben-c-2013 Mar 28, 2025
81cffab
Added tests for Beam plotting methods.
ben-c-2013 Mar 28, 2025
602c6d0
Added tests for Beam.accelerate().
ben-c-2013 Mar 28, 2025
ef6c2b5
Added controls to Beam.scale_norm_emittance_x() and Beam.scale_norm_e…
ben-c-2013 Mar 31, 2025
1904545
Added a flag calc_evolution to StageBaisc and edited StageBasic.track…
ben-c-2013 Mar 31, 2025
a8c26e0
Added tests for the Beam methods compress(), scale_to_length(), scale…
ben-c-2013 Mar 31, 2025
5362d2a
Added test_magnify_beta_function() to test_beam.py.
ben-c-2013 Mar 31, 2025
3b16ce5
Added test_apply_betatron_damping(), test_magnify_beta_function(), te…
ben-c-2013 Mar 31, 2025
ccd580d
Minor changes in Beam.apply_betatron_damping() and Beam.flip_transver…
ben-c-2013 Mar 31, 2025
7e9b64b
Changes to the default relative tolerance and absolute tolerance of B…
ben-c-2013 Mar 31, 2025
2437ed1
Added test_save_load() to test_beam.py.
ben-c-2013 Mar 31, 2025
e04994d
Added beam reference file for tests.
ben-c-2013 Apr 1, 2025
de2a3af
Changed the limit for minimum energy in the Beam class to gamma=10.
ben-c-2013 Apr 1, 2025
8f487ed
Added controls to weighted_mean(), weighted_std() and weighted_cov() …
ben-c-2013 Apr 1, 2025
e21c239
Edited the str operator and added controls for __init__() and reset_p…
ben-c-2013 Apr 1, 2025
7d2b100
Edited some tests to purposedly trigger exceptions in test_beam.py.
ben-c-2013 Apr 1, 2025
b3556f6
Added to test_rs(), test_deltas(), test_transverse_vector(), test_nor…
ben-c-2013 Apr 1, 2025
bb597f7
Added several more tests for beam statistics.
ben-c-2013 Apr 1, 2025
5934727
Added exception triggering to test_rotate_coord_sys_3D() under test_b…
ben-c-2013 Apr 1, 2025
d5b3336
Added test_density_map_diags() and test_print_summary() to test_beam.py.
ben-c-2013 Apr 1, 2025
9752890
Small correction in test_beam.py
ben-c-2013 Apr 1, 2025
027c598
Rearranged code in beam.py.
ben-c-2013 Apr 1, 2025
6d69d5a
Added checks for input types in Beam.set_phase_space() and implemente…
ben-c-2013 Apr 2, 2025
4ac5954
Added test_remove_halo_particles() to test_beam.py.
ben-c-2013 Apr 2, 2025
3a246fa
Added doc string to Beam.apply_betatron_motion().
ben-c-2013 Apr 3, 2025
40c5fe1
Added minor comment to stage_basic.py.
ben-c-2013 Apr 3, 2025
9bb7a73
Added controls in source_from_file.py to ensure valid file path in bo…
ben-c-2013 Apr 3, 2025
b903b82
Edited test_SourceFromFile2Beam() in test_sources.py to trigger excep…
ben-c-2013 Apr 3, 2025
04c5462
Revert "Rearranged code in beam.py."
kyrsjo Apr 3, 2025
5665394
Manually undo the moving of 'beam statistics' function from commit fc…
kyrsjo Apr 3, 2025
3542531
Manually undo the moving of plotting functions from commit 74c833294e…
kyrsjo Apr 3, 2025
95de216
Changed beam reference file path in test_SourceFromFile2Beam() using …
ben-c-2013 Apr 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions abel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from .classes.source.impl.source_combiner import *
from .classes.source.impl.source_from_file import *
from .classes.source.impl.source_flattop import *
from .classes.source.impl.source_capsule import *
from .classes.stage.impl.stage_basic import *
from .classes.stage.impl.stage_nonlinear_1d import *
from .classes.stage.impl.stage_hipace import *
Expand Down
391 changes: 359 additions & 32 deletions abel/classes/beam.py

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion abel/classes/source/impl/source_from_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
from abel import Source, Beam
import os

class SourceFromFile(Source):

def __init__(self, length=0, charge=None, energy=None, accel_gradient=None, wallplug_efficiency=1, file=None, x_offset=0, y_offset=0, x_angle=0, y_angle=0, waist_shift_x=0, waist_shift_y=0):

if file is not None and not os.path.exists(file):
raise FileNotFoundError(f"Error: The file '{file}' was not found.")

self.file = file
self._file = file

super().__init__(length, charge, energy, accel_gradient, wallplug_efficiency, x_offset, y_offset, x_angle, y_angle, waist_shift_x, waist_shift_y)


@property
def file(self) -> str | None:
return self._file
@file.setter
def file(self, file_path : str):
if file_path is not None and not os.path.exists(file_path):
raise FileNotFoundError(f"Error: The file '{file_path}' was not found.")
else:
self._file = file_path


def track(self, _=None, savedepth=0, runnable=None, verbose=False):
Expand Down
10 changes: 8 additions & 2 deletions abel/classes/stage/impl/stage_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

class StageBasic(Stage):

def __init__(self, nom_accel_gradient=None, nom_energy_gain=None, plasma_density=None, driver_source=None, ramp_beta_mag=None, transformer_ratio=1):
def __init__(self, nom_accel_gradient=None, nom_energy_gain=None, plasma_density=None, driver_source=None, ramp_beta_mag=None, transformer_ratio=1, calc_evolution=False):

super().__init__(nom_accel_gradient=nom_accel_gradient, nom_energy_gain=nom_energy_gain, plasma_density=plasma_density, driver_source=driver_source, ramp_beta_mag=ramp_beta_mag)

self.transformer_ratio = transformer_ratio
self.calc_evolution = calc_evolution


def track(self, beam_incoming, savedepth=0, runnable=None, verbose=False):
Expand Down Expand Up @@ -85,7 +86,12 @@ def track(self, beam_incoming, savedepth=0, runnable=None, verbose=False):


# ========== Betatron oscillations ==========
beam.apply_betatron_motion(self.length_flattop, self.plasma_density, self.nom_energy_gain_flattop, x0_driver=driver0.x_offset(), y0_driver=driver0.y_offset())
deltaEs = np.full(len(beam.Es()), self.nom_energy_gain_flattop) # Homogeneous energy gain for all macroparticles.
if self.calc_evolution:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pull the beam.apply_betatron_motion outside of the if? Its called by the same parameters anyway, the only difference is whether self.evolution.beam is set or not.

Also, why is it called? Are there side effects to beam when this is called (the function has no docstring)?

Copy link
Copy Markdown
Collaborator Author

@ben-c-2013 ben-c-2013 Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is inside of if self.calc_evolution because it only tracks the beam parameter evolution when self.calc_evolution=True. And this is the function that actually evolves the beam by solving Hill's equation. I will add docstrings to this (
commit 3a246fa).

_, evol = beam.apply_betatron_motion(self.length_flattop, self.plasma_density, deltaEs, x0_driver=driver0.x_offset(), y0_driver=driver0.y_offset(), calc_evolution=self.calc_evolution)
self.evolution.beam = evol
else:
beam.apply_betatron_motion(self.length_flattop, self.plasma_density, deltaEs, x0_driver=driver0.x_offset(), y0_driver=driver0.y_offset())


# ========== Accelerate beam with homogeneous energy gain ==========
Expand Down
6 changes: 4 additions & 2 deletions abel/utilities/relativity.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def proper_velocity2gamma(u):
return np.sign(u) * np.sqrt(1+(u/SI.c)**2)

# convert proper velocity to momentum [kg m/s]
def proper_velocity2momentum(u):
return gamma2momentum(proper_velocity2gamma(u))
# def proper_velocity2momentum(u):
# return gamma2momentum(proper_velocity2gamma(u))
def proper_velocity2momentum(u, m=SI.m_e):
return m*u
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is u = gamma*v?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


# convert proper velocity to energy
def proper_velocity2energy(u, unit=defaultUnitE, m=SI.m_e):
Expand Down
10 changes: 8 additions & 2 deletions abel/utilities/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ def prct_clean2d(xs, ys, enable=True, cut_percentile=5):
return x[mask], y[mask]

def weighted_mean(values, weights, clean=False, cut_percentile=5):
mask = clean_mask(values, enable=clean, cut_percentile=5)
if np.sum(weights) == 0.0:
raise ZeroDivisionError("Weights sum to zero, can't be normalized.")
mask = clean_mask(values, enable=clean, cut_percentile=cut_percentile)
return np.average(values[mask], weights=weights[mask])

def weighted_std(values, weights, clean=False, cut_percentile=5):
mask = clean_mask(values, enable=clean, cut_percentile=5)
if np.sum(weights) == 0.0:
raise ZeroDivisionError("Weights sum to zero, can't be normalized.")
mask = clean_mask(values, enable=clean, cut_percentile=cut_percentile)
return np.sqrt(np.cov(values[mask], aweights=weights[mask]))

def weighted_cov(xs, ys, weights, clean=False, cut_percentile=5):
if np.sum(weights) == 0.0:
raise ZeroDivisionError("Weights sum to zero, can't be normalized.")
mask_x = clean_mask(xs, enable=clean, cut_percentile=cut_percentile/2)
mask_y = clean_mask(ys, enable=clean, cut_percentile=cut_percentile/2)
mask = np.logical_and(mask_x, mask_y)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ addopts = [
markers = [
"core: Test core functionality",
"stageGeometry: Test stage geometry calculation functionality",
"sources: Test the various Source subclasses generate beams with desired properties",
"beam: Tests for the Beam class",
]
Binary file not shown.
Loading