Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes and relax some tolerances after scipy.constant changes #125

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 29 additions & 28 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@

def _check_consistency_energy_variables(particles):
# Check consistency between beta0 and gamma0
assert np.allclose(particles.gamma0, 1/np.sqrt(1 - particles.beta0**2),
xo.assert_allclose(particles.gamma0, 1/np.sqrt(1 - particles.beta0**2),
rtol=1e-14, atol=1e-14)

# Assert consistency of p0c
assert np.allclose(particles.p0c,
xo.assert_allclose(particles.p0c,
particles.mass0 * particles.beta0 * particles.gamma0,
rtol=1e-14, atol=1e-14)

# Check energy0 property (consistency of p0c and gamma0)
assert np.allclose(particles.energy0, particles.mass0 * particles.gamma0,
xo.assert_allclose(particles.energy0, particles.mass0 * particles.gamma0,
atol=1e-14, rtol=1e-14)

# Check consistency of rpp and delta
assert np.allclose(particles.rpp, 1./(particles.delta + 1),
xo.assert_allclose(particles.rpp, 1./(particles.delta + 1),
rtol=1e-14, atol=1e-14)

beta = particles.beta0 * particles.rvv
gamma = 1/np.sqrt(1 - beta**2)
pc = particles.mass0 * gamma * beta

# Check consistency of delta with rvv
assert np.allclose(particles.delta, (pc-particles.p0c)/(particles.p0c),
xo.assert_allclose(particles.delta, (pc-particles.p0c)/(particles.p0c),
rtol=1e-14, atol=1e-14)

# Check consistency of ptau with rvv
energy = particles.mass0 * gamma
assert np.allclose(particles.ptau, (energy - particles.energy0)/particles.p0c,
xo.assert_allclose(particles.ptau, (energy - particles.energy0)/particles.p0c,
rtol=1e-14, atol=1e-14)

# Check consistency of pzeta
energy = particles.mass0 * gamma
assert np.allclose(particles.pzeta, (energy - particles.energy0)/(particles.beta0 * particles.p0c),
xo.assert_allclose(particles.pzeta, (energy - particles.energy0)/(particles.beta0 * particles.p0c),
rtol=1e-14, atol=1e-14)


# Check energy property
assert np.allclose(particles.energy, energy, rtol=1e-14, atol=1e-14)
xo.assert_allclose(particles.energy, energy, rtol=1e-14, atol=1e-14)


@for_all_test_contexts
Expand Down Expand Up @@ -182,8 +182,8 @@ def test_particles_update_p0c_and_energy_deviations(test_context):

part.move(_context=xo.ContextCpu())
part.sort(interleave_lost_particles = True)
assert np.allclose(part.p0c, [2e12, 3e12, 2e12], rtol=0, atol=1e-14)
assert np.allclose(part.delta, [-0.5, 0.1, 0], rtol=0, atol=1e-14)
xo.assert_allclose(part.p0c, [2e12, 3e12, 2e12], rtol=0, atol=1e-14)
xo.assert_allclose(part.delta, [-0.5, 0.1, 0], rtol=0, atol=1e-14)


def test_sort():
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_python_add_to_energy(test_context):

expected_energy = energy_before + 3e6
particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.energy, expected_energy,
xo.assert_allclose(particles.energy, expected_energy,
atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)
Expand All @@ -308,7 +308,7 @@ def test_python_delta_setter(test_context):
particles.delta = -2e-3

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.delta, -2e-3, atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.delta, -2e-3, atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)

Expand Down Expand Up @@ -352,7 +352,7 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.energy, energy_before + 1e6,
xo.assert_allclose(particles.energy, energy_before + 1e6,
atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)
Expand All @@ -378,17 +378,17 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.energy, energy_before + 1e6,
xo.assert_allclose(particles.energy, energy_before + 1e6,
atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)

rpp_after = particles.copy(_context=xo.ContextCpu()).rpp
assert np.all(particles.gamma0 == gamma0_before)
assert np.all(particles.zeta == zeta_before)
assert np.allclose(particles.px, px_before*rpp_before/rpp_after,
xo.assert_allclose(particles.px, px_before*rpp_before/rpp_after,
atol=1e-14, rtol=1e-14)
assert np.allclose(particles.py, py_before*rpp_before/rpp_after,
xo.assert_allclose(particles.py, py_before*rpp_before/rpp_after,
atol=1e-14, rtol=1e-14)


Expand Down Expand Up @@ -423,7 +423,7 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.delta, -2e-3, atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.delta, -2e-3, atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)

Expand Down Expand Up @@ -464,7 +464,7 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.ptau, -2e-3, atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.ptau, -2e-3, atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)

Expand Down Expand Up @@ -506,7 +506,7 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose((particles.ptau - ptau_before)/particles.beta0,
xo.assert_allclose((particles.ptau - ptau_before)/particles.beta0,
-2e-3, atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)
Expand Down Expand Up @@ -538,6 +538,7 @@ class TestElement(xt.BeamElement):

particles = xp.Particles(_context=test_context, p0c=1.4e9, delta=[0, 1e-3],
px=[1e-6, -1e-6], py=[2e-6, 0], zeta=0.1)

_check_consistency_energy_variables(
particles.copy(_context=xo.ContextCpu()))
px_before = particles.copy(_context=xo.ContextCpu()).px
Expand All @@ -549,14 +550,14 @@ class TestElement(xt.BeamElement):
telem.track(particles)

particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.p0c, 1.5e9, atol=1e-14, rtol=1e-14)
assert np.allclose(particles.energy, energy_before, atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.p0c, 1.5e9, atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.energy, energy_before, atol=1e-14, rtol=1e-14)

_check_consistency_energy_variables(particles)

assert np.all(particles.zeta == zeta_before*particles.beta0/beta0_before)
assert np.all(particles.px == px_before*p0c_before/particles.p0c)
assert np.all(particles.py == py_before*p0c_before/particles.p0c)
xo.assert_allclose(particles.zeta, zeta_before*particles.beta0/beta0_before, atol=1e-14)
xo.assert_allclose(particles.px, px_before*p0c_before/particles.p0c, atol=1e-14)
xo.assert_allclose(particles.py, py_before*p0c_before/particles.p0c, atol=1e-14)



Expand Down Expand Up @@ -617,8 +618,8 @@ class KickAng(xt.BeamElement):
px=[1.0e-3, -1.0e-3], py=[2.0e-3, -1.2e-3], zeta=0.1)
line.track(particles)
particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.px, [24.0e-3, 18.021e-3], atol=1e-14, rtol=1e-14)
assert np.allclose(particles.py, [-1.82e-3, -4.73564e-3], atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.px, [24.0e-3, 18.021e-3], atol=1e-14, rtol=1e-14)
xo.assert_allclose(particles.py, [-1.82e-3, -4.73564e-3], atol=1e-14, rtol=1e-14)

class ScaleAngExact(xt.BeamElement):
_xofields={
Expand Down Expand Up @@ -675,5 +676,5 @@ class KickAngExact(xt.BeamElement):
px=[1.0e-3, -1.0e-3], py=[2.0e-3, -1.2e-3], zeta=0.1)
line.track(particles)
particles.move(_context=xo.ContextCpu())
assert np.allclose(particles.px, [23.99302e-3, 18.01805e-3], atol=1e-14)
assert np.allclose(particles.py, [-1.81976e-3, -4.73529e-3], atol=1e-14)
xo.assert_allclose(particles.px, [23.99302e-3, 18.01805e-3], atol=1e-14)
xo.assert_allclose(particles.py, [-1.81976e-3, -4.73529e-3], atol=1e-14, rtol=5e-7)
36 changes: 18 additions & 18 deletions tests/test_build_particles_normalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_build_particles_normalized(test_context):
nemitt_x=3e-6, nemitt_y=3e-6)

dct = particles.to_dict() # transfers it to cpu
assert np.allclose(dct['x'], [-0.0003883 , -0.0006076 , -0.00082689],
xo.assert_allclose(dct['x'], [-0.0003883 , -0.0006076 , -0.00082689],
rtol=0, atol=1e-7)
assert np.isclose(dct['ptau'][1], 1e-3, rtol=0, atol=1e-9)
assert np.isclose(1/(dct['rpp'][1]) - 1, 1e-3, rtol=0, atol=1e-10)
Expand All @@ -56,7 +56,7 @@ def test_build_particles_normalized(test_context):
px_norm=[0, 1, 0], # in sigmas
nemitt_x=3e-6, nemitt_y=3e-6)
dct = particles.to_dict() # transfers it to cpu
assert np.allclose(dct['x'], [-0.0003883, -0.0006076, -0.00082689],
xo.assert_allclose(dct['x'], [-0.0003883, -0.0006076, -0.00082689],
rtol=0, atol=1e-7)
assert np.isclose(dct['ptau'][1], 1e-3, rtol=0, atol=1e-9)
assert np.isclose(1/(dct['rpp'][1]) - 1, 1e-3, rtol=0, atol=1e-10)
Expand All @@ -76,7 +76,7 @@ def test_build_particles_normalized(test_context):
nemitt_x=3e-6, nemitt_y=3e-6)

dct = particles.to_dict() # transfers it to cpu
assert np.allclose(dct['x'], [-0.00038813 , -0.00060738 , -0.00082664],
xo.assert_allclose(dct['x'], [-0.00038813 , -0.00060738 , -0.00082664],
rtol=0, atol=1e-7)
assert np.isclose(dct['ptau'][1], 1e-3, rtol=0, atol=1e-9)
assert np.isclose(1/(dct['rpp'][1]) - 1, 1e-3, rtol=0, atol=1e-10)
Expand All @@ -102,11 +102,11 @@ def test_build_particles_normalized_ions(test_context):
nemitt_x=3e-6, nemitt_y=3e-6)

dct = particles.to_dict() # transfers it to cpu
assert np.allclose(dct['x'], [6.5404e-3, 1.21e-5, -6.5163e-3],
xo.assert_allclose(dct['x'], [6.5404e-3, 1.21e-5, -6.5163e-3],
rtol=0, atol=1e-7)
assert np.isclose(dct['ptau'][1], 9.906e-6, rtol=0, atol=1e-9)
assert np.isclose(1/(dct['rpp'][1]) - 1, 1e-5, rtol=0, atol=1e-10)
assert np.allclose(dct['p0c'], 1.4024063e+12, rtol=0, atol=1e3)
xo.assert_allclose(dct['p0c'], 1.4024063e+12, rtol=0, atol=1e3)


@for_all_test_contexts
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_build_particles_normalized_closed_orbit(test_context):
dct_co = particle_on_co.to_dict()

for nn in 'x px y py zeta delta ptau rvv rpp gamma0 beta0 p0c'.split():
assert np.allclose(dct[nn], dct_co[nn], atol=1e-15, rtol=0)
xo.assert_allclose(dct[nn], dct_co[nn], atol=1e-15, rtol=0)


@for_all_test_contexts
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_build_particles_normalized_match_at_s(test_context):
particles.move(_context=xo.context_default)
assert (np.unique(particles.at_element[particles.state>0])[0]
== line.element_names.index('match_at_s'))
assert np.allclose(particles.x, 0.02, atol=1e-20)
xo.assert_allclose(particles.x, 0.02, atol=1e-20)


@for_all_test_contexts
Expand All @@ -221,14 +221,14 @@ def test_build_perticles_dispersion(test_context):
particles, nemitt_x=3e-6, nemitt_y=3e-6)
particles.move(_context=xo.ContextCpu())

assert np.allclose(particles.x, [1e-3, -1e-3], atol=1e-10, rtol=0)
assert np.allclose((particles.x-tw.x[0])/particles.delta, tw.dx[0],
xo.assert_allclose(particles.x, [1e-3, -1e-3], atol=1e-10, rtol=0)
xo.assert_allclose((particles.x-tw.x[0])/particles.delta, tw.dx[0],
atol=5e-3, rtol=0)

assert np.allclose(norm_coords['x_norm'], 0, 1e-12)
assert np.allclose(norm_coords['px_norm'], 0, 1e-12)
assert np.allclose(norm_coords['y_norm'], 0, 1e-12)
assert np.allclose(norm_coords['py_norm'], 0, 1e-12)
xo.assert_allclose(norm_coords['x_norm'], 0, 1e-12)
xo.assert_allclose(norm_coords['px_norm'], 0, 1e-12)
xo.assert_allclose(norm_coords['y_norm'], 0, 1e-12)
xo.assert_allclose(norm_coords['py_norm'], 0, 1e-12)

particles = line.build_particles(nemitt_x=3e-6, nemitt_y=3e-6,
x=[1e-3, -1e-3], x_norm=[0.3, 0.4], px_norm=[0.5, 0.6],
Expand All @@ -239,9 +239,9 @@ def test_build_perticles_dispersion(test_context):
particles, nemitt_x=3e-6, nemitt_y=3e-6)
particles.move(_context=xo.ContextCpu())

assert np.allclose(particles.x, [1e-3, -1e-3], atol=1e-10, rtol=0)
xo.assert_allclose(particles.x, [1e-3, -1e-3], atol=1e-10, rtol=0)

assert np.allclose(norm_coords['x_norm'], [0.3, 0.4], 1e-12)
assert np.allclose(norm_coords['px_norm'], [0.5, 0.6], 1e-12)
assert np.allclose(norm_coords['y_norm'], [0.7, 0.8], 1e-12)
assert np.allclose(norm_coords['py_norm'], [0.9, 1.0], 1e-12)
xo.assert_allclose(norm_coords['x_norm'], [0.3, 0.4], 1e-12)
xo.assert_allclose(norm_coords['px_norm'], [0.5, 0.6], 1e-12)
xo.assert_allclose(norm_coords['y_norm'], [0.7, 0.8], 1e-12)
xo.assert_allclose(norm_coords['py_norm'], [0.9, 1.0], 1e-12)
6 changes: 3 additions & 3 deletions tests/test_pdg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_lead_208():
assert _mass_consistent(pdg_id, xp.Pb208_MASS_EV)
assert xp.pdg.get_element_name_from_Z(82) == 'Pb'
assert xp.pdg.get_element_full_name_from_Z(82) == 'Lead'
assert np.allclose(xp.pdg.get_mass_from_pdg_id(pdg_id), xp.Pb208_MASS_EV,
xo.assert_allclose(xp.pdg.get_mass_from_pdg_id(pdg_id), xp.Pb208_MASS_EV,
rtol=1e-10, atol=0)
assert xp.pdg.get_properties_from_pdg_id(pdg_id) == (82., 208, 82, 'Pb208')

Expand All @@ -52,5 +52,5 @@ def test_build_reference_from_pdg_id(test_context):
particle_ref_lead = xp.reference_from_pdg_id(pdg_id='Pb208',
_context=test_context)
particle_ref_lead.move(_context=xo.context_default)
assert np.allclose(particle_ref_lead.q0, 82.)
assert np.allclose(particle_ref_lead.mass0, xp.Pb208_MASS_EV)
xo.assert_allclose(particle_ref_lead.q0, 82.)
xo.assert_allclose(particle_ref_lead.mass0, xp.Pb208_MASS_EV)
4 changes: 2 additions & 2 deletions tests/test_pencil_with_absolute_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def test_pencil_with_absolute_cut(test_context):
w_norm = getattr(norm_coords, other_plane+'_norm')
pw_norm = getattr(norm_coords, 'p'+other_plane+'_norm')

assert np.allclose(w_in_sigmas, w_norm, 1e-12)
assert np.allclose(pw_in_sigmas, pw_norm, 1e-12)
xo.assert_allclose(w_in_sigmas, w_norm, 1e-12)
xo.assert_allclose(pw_in_sigmas, pw_norm, 1e-12)

assert(np.allclose(zeta, particles.zeta, atol=1e-12))
assert(np.allclose(delta, particles.delta, atol=1e-12))
Expand Down
5 changes: 3 additions & 2 deletions tests/test_phase_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

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

Expand Down Expand Up @@ -38,8 +39,8 @@ def test_phase_monitor(test_context):
line.track(particles)

tw = line.twiss()
assert np.allclose(phase_monitor.qx[:], np.mod(tw['qx'], 1),
xo.assert_allclose(phase_monitor.qx[:], np.mod(tw['qx'], 1),
rtol=0, atol=1e-3)
assert np.allclose(phase_monitor.qy[:], np.mod(tw['qy'], 1),
xo.assert_allclose(phase_monitor.qy[:], np.mod(tw['qy'], 1),
rtol=0, atol=1e-3)