Skip to content
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
2 changes: 2 additions & 0 deletions pyaml/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def from_dict(config_dict: dict, ignore_external=False) -> "Accelerator":
if ignore_external:
# control systems are external, so remove controls field
config_dict.pop("controls", None)
# Ensure factory is clean before building a new accelerator
Factory.clear()
return Factory.depth_first_build(config_dict, ignore_external)

@staticmethod
Expand Down
10 changes: 3 additions & 7 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
from pyaml.arrays.bpm_array import BPMArray
from pyaml.arrays.cfm_magnet import CombinedFunctionMagnet
from pyaml.arrays.cfm_magnet import ConfigModel as CombinedFunctionMagnetConfigModel
from pyaml.arrays.cfm_magnet_array import CombinedFunctionMagnetArray
from pyaml.arrays.element_array import ElementArray
from pyaml.arrays.magnet import ConfigModel as MagnetArrayConfigModel
from pyaml.arrays.magnet import Magnet
from pyaml.arrays.magnet_array import MagnetArray
from pyaml.configuration.factory import Factory


@pytest.mark.parametrize(
Expand Down Expand Up @@ -205,8 +203,6 @@ def test_arrays(install_test_package):
bpmsLive = BPMArray("", sr.live.get_all_bpms())
bpmsLive.positions.get()

Factory.clear()

# Test dynamic arrays

sr: Accelerator = Accelerator.load(
Expand Down Expand Up @@ -244,7 +240,6 @@ def test_arrays(install_test_package):
v = sr.design.get_cfm_magnets("emptyCFM").strengths.get() # Ensure good attach
assert np.shape(v) == (0,)

Factory.clear()

@pytest.mark.parametrize(
"sr_file",
Expand All @@ -253,7 +248,9 @@ def test_arrays(install_test_package):
],
)
def test_serialized_magnets_arrays(sr_file):
sr: Accelerator = Accelerator.load(sr_file, use_fast_loader=True, ignore_external=True)
sr: Accelerator = Accelerator.load(
sr_file, use_fast_loader=True, ignore_external=True
)
the_serie = sr.design.get_serialized_magnets("series")
strength = the_serie.strengths.get()
assert len(strength) == 5
Expand All @@ -263,4 +260,3 @@ def test_serialized_magnets_arrays(sr_file):
assert len(hardwares) == 5
print(hardwares)
the_serie.hardwares.set([10])

9 changes: 0 additions & 9 deletions tests/test_bpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def test_simulator_bpm_tilt():
Expand All @@ -13,8 +12,6 @@ def test_simulator_bpm_tilt():
bpm.tilt.set(0.01)
assert bpm.tilt.get() == 0.01

Factory.clear()


def test_simulator_bpm_offset():
sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True)
Expand All @@ -28,8 +25,6 @@ def test_simulator_bpm_offset():
assert bpm.offset.get()[1] == 0.2
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand All @@ -45,8 +40,6 @@ def test_simulator_bpm_position(install_test_package):
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))
assert np.allclose(bpm_simple.positions.get(), np.array([0.0, 0.0]))

Factory.clear()


def test_simulator_bpm_position_with_bad_corrector_strength():
sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True)
Expand All @@ -60,5 +53,3 @@ def test_simulator_bpm_position_with_bad_corrector_strength():
for bpm in [bpm1, bpm_simple, bpm3]:
assert bpm.positions.get()[0] != 0.0
assert bpm.positions.get()[1] != 0.0

Factory.clear()
7 changes: 0 additions & 7 deletions tests/test_bpm_controlsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


@pytest.mark.parametrize(
Expand All @@ -19,8 +18,6 @@ def test_controlsystem_bpm_tilt(install_test_package):
bpm.tilt.set(0.01)
assert bpm.tilt.get() == 0.01

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand All @@ -38,8 +35,6 @@ def test_controlsystem_bpm_offset(install_test_package):
assert bpm.offset.get()[1] == 0.2
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand All @@ -65,5 +60,3 @@ def test_controlsystem_bpm_position_indexed(install_test_package):
bpm = sr.live.get_bpm("BPM_C01-04")

assert np.allclose(bpm.positions.get(), np.array([0.0, 1.0]))

Factory.clear()
5 changes: 0 additions & 5 deletions tests/test_chromaticity_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def test_simulator_chromaticity_monitor():
Expand All @@ -20,8 +19,6 @@ def test_simulator_chromaticity_monitor():
== sr.design.get_lattice().get_chrom()[1]
)

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand All @@ -44,5 +41,3 @@ def test_controlsystem_chromaticity_monitor(install_test_package):
ksi = np.abs(chromaticity_monitor.chromaticity.get())
assert abs(ksi[0]) < 1e-17
assert abs(ksi[1]) < 1e-17

Factory.clear()
6 changes: 0 additions & 6 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pyaml.accelerator import Accelerator
from pyaml.arrays.magnet_array import MagnetArray
from pyaml.common.exception import PyAMLConfigException, PyAMLException
from pyaml.configuration.factory import Factory


@pytest.mark.parametrize(
Expand All @@ -15,18 +14,15 @@ def test_tune(install_test_package):
with pytest.raises(PyAMLConfigException) as exc:
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_1.yaml")
assert "MagnetArray HCORR : duplicate name SH1A-C02-H @index 2" in str(exc)
Factory.clear()

with pytest.raises(PyAMLConfigException) as exc:
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_2.yaml")
assert "BPMArray BPM : duplicate name BPM_C04-06 @index 3" in str(exc)
Factory.clear()

with pytest.raises(PyAMLConfigException) as exc:
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_3.yaml")
assert "element BPM_C04-06 already defined" in str(exc)
assert "line 58, column 3" in str(exc)
Factory.clear()

sr: Accelerator = Accelerator.load("tests/config/EBSTune.yaml")
m1 = sr.live.get_magnet("QF1E-C04")
Expand All @@ -45,5 +41,3 @@ def test_tune(install_test_package):
with pytest.raises(PyAMLException) as exc:
m2 = sr.design.get_bpm("QF1A-C05XX")
assert "BPM QF1A-C05XX not defined" in str(exc)

Factory.clear()
4 changes: 0 additions & 4 deletions tests/test_ident_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory
from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet
from pyaml.magnet.hcorrector import HCorrector
from pyaml.magnet.model import MagnetModel
from pyaml.magnet.vcorrector import VCorrector


Expand Down Expand Up @@ -44,5 +42,3 @@ def test_cfm_magnets(magnet_file, install_test_package):
assert np.abs(o[1] - 3.39661431e-07) < 1e-10
assert np.abs(o[2] + 1.59928207e-06) < 1e-10
assert np.abs(o[3] + 1.74771216e-05) < 1e-10

Factory.clear()
2 changes: 0 additions & 2 deletions tests/test_load_quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
)
from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet
from pyaml.magnet.hcorrector import HCorrector
from pyaml.magnet.identity_model import IdentityMagnetModel
from pyaml.magnet.quadrupole import ConfigModel as QuadrupoleConfigModel
from pyaml.magnet.quadrupole import Quadrupole

# TODO: Generate JSON pydantic schema for MetaConfigurator
Expand Down
23 changes: 13 additions & 10 deletions tests/test_ranges_cfm_deviceaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from pyaml import PyAMLException
from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def _in_range(vmin, vmax) -> float:
Expand All @@ -21,7 +20,9 @@ def _out_of_range(vmin, vmax) -> float:
return float(vmax) + 0.1
if vmin is not None:
return float(vmin) - 0.1
raise RuntimeError("Unbounded range [None, None], cannot build an out-of-range value.")
raise RuntimeError(
"Unbounded range [None, None], cannot build an out-of-range value."
)


@pytest.mark.parametrize(
Expand All @@ -34,7 +35,9 @@ def _out_of_range(vmin, vmax) -> float:
],
indirect=["install_test_package"],
)
def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_test_package):
def test_cfm_ranges_from_yaml_are_propagated_and_enforced(
magnet_file, install_test_package
):
sr: Accelerator = Accelerator.load(magnet_file)
sr.design.get_lattice().disable_6d()

Expand All @@ -55,11 +58,13 @@ def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_t
assert got_ranges == expected_ranges

# Build an in-range current vector (3 values)
in_currents = np.array([
_in_range(*expected_ranges[0]),
_in_range(*expected_ranges[1]),
_in_range(*expected_ranges[2]),
])
in_currents = np.array(
[
_in_range(*expected_ranges[0]),
_in_range(*expected_ranges[1]),
_in_range(*expected_ranges[2]),
]
)

# Convert currents -> strengths (vector size 3)
in_strengths = m.model.compute_strengths(in_currents)
Expand All @@ -73,5 +78,3 @@ def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_t

with pytest.raises(PyAMLException, match="out of range"):
m.strengths.set(out_strengths)

Factory.clear()
7 changes: 0 additions & 7 deletions tests/test_rf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from pyaml.accelerator import Accelerator
from pyaml.common.exception import PyAMLException
from pyaml.configuration.factory import Factory


def test_rf():
Expand Down Expand Up @@ -33,8 +32,6 @@ def test_rf():
assert np.isclose(RF.frequency.get(), 3.523721693993786e8)
assert np.isclose(RF.voltage.get(), 6.5e6)

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand Down Expand Up @@ -85,8 +82,6 @@ def test_rf_multi(install_test_package):
assert np.isclose(RF2.voltage.get(), 2e6)
assert np.isclose(RFTRA_HARMONIC.voltage.get(), 3e5)

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand Down Expand Up @@ -116,5 +111,3 @@ def test_rf_multi_notrans(install_test_package):

# Check that frequency and voltage has been applied on the masterclock device
assert np.isclose(RF.frequency.get(), 3.523e8)

Factory.clear()
6 changes: 3 additions & 3 deletions tests/test_serialized_magnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def check_no_diff(array: list[np.float64]) -> bool:
Expand All @@ -24,7 +23,9 @@ def check_no_diff(array: list[np.float64]) -> bool:
],
)
def test_config_load(sr_file):
sr: Accelerator = Accelerator.load(sr_file, use_fast_loader=True, ignore_external=True)
sr: Accelerator = Accelerator.load(
sr_file, use_fast_loader=True, ignore_external=True
)
assert sr is not None
magnets = [
sr.design.get_element("QF8B-C04"),
Expand All @@ -46,4 +47,3 @@ def test_config_load(sr_file):
currents = [magnet.hardware.get() for magnet in magnets]
assert check_no_diff(strengths)
assert check_no_diff(currents)
Factory.clear()
3 changes: 0 additions & 3 deletions tests/test_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def test_tune():
Expand Down Expand Up @@ -51,5 +50,3 @@ def test_tune():
strs = quadForTuneLive.strengths.get()
strs += np.matmul(correctionmat, [0.1, 0.05]) # Ask for correction [dqx,dqy]
quadForTuneLive.strengths.set(strs)

Factory.clear()
6 changes: 3 additions & 3 deletions tests/test_tune_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def test_tune():
sr: Accelerator = Accelerator.load("tests/config/EBSTune.yaml", ignore_external=True)
sr: Accelerator = Accelerator.load(
"tests/config/EBSTune.yaml", ignore_external=True
)
sr.design.get_lattice().disable_6d()

quadForTuneDesign = sr.design.get_magnets("QForTune")
Expand Down Expand Up @@ -42,4 +43,3 @@ def test_tune():
assert np.abs(currents[0] - 88.04522942) < 1e-8
assert np.abs(currents[1] - 88.26677735) < 1e-8
assert units[0] == "A" and units[1] == "A"
Factory.clear()
9 changes: 3 additions & 6 deletions tests/test_tune_monitor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import pytest

from pyaml.accelerator import Accelerator
from pyaml.configuration.factory import Factory


def test_simulator_tune_monitor():
sr: Accelerator = Accelerator.load("tests/config/tune_monitor.yaml", ignore_external=True)
sr: Accelerator = Accelerator.load(
"tests/config/tune_monitor.yaml", ignore_external=True
)
sr.design.get_lattice().disable_6d()
tune_monitor = sr.design.get_betatron_tune_monitor("BETATRON_TUNE")
assert tune_monitor.tune.get()[0] == sr.design.get_lattice().get_tune()[0]
assert tune_monitor.tune.get()[1] == sr.design.get_lattice().get_tune()[1]

Factory.clear()


@pytest.mark.parametrize(
"install_test_package",
Expand All @@ -24,5 +23,3 @@ def test_controlsystem_tune_monitor(install_test_package):
tune_monitor = sr.live.get_betatron_tune_monitor("BETATRON_TUNE")
assert tune_monitor.tune.get()[0] == 0.0
assert tune_monitor.tune.get()[1] == 0.0

Factory.clear()
Loading
Loading