Skip to content
Open
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
26 changes: 14 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
"version": "0.2.0",
"configurations": [
{
"name": "Debug Unit Test",
"name": "Python Debugger: Virtac",
"type": "debugpy",
"request": "launch",
"justMyCode": false,
"program": "${file}",
"purpose": [
"debug-test"
],
"program": "/venv/bin/virtac",
"console": "integratedTerminal",
"args": [
"-v"
],
"env": {
// Enable break on exception when debugging tests (see: tests/conftest.py)
"PYTEST_RAISE": "1",
"EPICS_CA_SERVER_PORT": "8064",
"EPICS_CA_REPEATER_PORT": "8065",
},
},
{
"name": "Python Debugger: Virtac",
"name": "Python Debugger: ATIP",
"type": "debugpy",
"request": "launch",
"program": "/venv/bin/virtac",
"program": "/venv/bin/atip",
"console": "integratedTerminal",
"args": [
"-t"
],
"env": {
"EPICS_CA_SERVER_PORT": "7064",
"EPICS_CA_REPEATER_PORT": "7065",
"EPICS_CA_SERVER_PORT": "8064",
"EPICS_CA_REPEATER_PORT": "8065",
},
},
]
Expand Down
3 changes: 0 additions & 3 deletions INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ Initial Setup and Installation

Troubleshooting
---------------

Please note that for ATIP to function with Python 3.7 or later, you must
use Cothread>=2.16.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
("py:class", "obj"),
("py:class", "class"),
("py:class", "at.lattice.lattice_object.Lattice"),
("py:class", "cothread.Event"),
]

# Both the class’ and the __init__ method’s docstring are concatenated and
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dependencies = [
"scipy",
"pytac>=0.3.0",
"accelerator-toolbox>=0.2.0",
"cothread",
"softioc",
"aioca",
]

dynamic = ["version"]
Expand Down
43 changes: 41 additions & 2 deletions src/atip/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"""Interface for ``python -m atip``."""

import asyncio
import logging
import random
from argparse import ArgumentParser
from collections.abc import Sequence

import pytac

import atip

from . import __version__

__all__ = ["main"]


def main(args: Sequence[str] | None = None) -> None:
async def async_main(args: Sequence[str] | None = None) -> None:
"""Argument parser for the CLI."""
parser = ArgumentParser()
parser.add_argument(
Expand All @@ -17,7 +24,39 @@ def main(args: Sequence[str] | None = None) -> None:
action="version",
version=__version__,
)
parser.parse_args(args)
parser.add_argument(
"-t",
"--run-test",
help="Start an endless test of atip",
action="store_true",
)
args = parser.parse_args()

if args.run_test:
# Load the DIAD lattice from Pytac.
lat = await pytac.load_csv.load("DIAD")
await atip.load_sim.load_from_filepath(lat, "../atip/src/atip/rings/DIAD.mat")
# Use the sim by default.
lat.set_default_data_source(pytac.SIM)
# The initial beam position is zero.
print(await lat.get_value("x"))

# Get the first horizontal corrector magnet and set its current to 1A.
hcor1 = lat.get_elements("HSTR")[0]
while True:
kick: float = random.uniform(0, 2)
print(f"Applying x_kick of {kick}")
await hcor1.set_value("x_kick", kick, units=pytac.ENG)
# Now the x beam position has changed.
print(f"New data: {await lat.get_value('x')}")
await asyncio.sleep(1)


def main(args: Sequence[str] | None = None) -> None:
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
# Load the AT sim into the Pytac lattice.
asyncio.run(async_main(args))


if __name__ == "__main__":
Expand Down
12 changes: 7 additions & 5 deletions src/atip/load_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SIMULATED_FIELDS = {"a1", "b0", "b1", "b2", "x", "y", "f", "x_kick", "y_kick"}


def load_from_filepath(
async def load_from_filepath(
pytac_lattice, at_lattice_filepath, callback=None, disable_emittance=False
):
"""Load simulator data sources onto the lattice and its elements.
Expand All @@ -32,12 +32,12 @@ def load_from_filepath(
at_lattice = at.load.load_mat(
at_lattice_filepath,
name=pytac_lattice.name,
energy=pytac_lattice.get_value("energy"),
energy=await pytac_lattice.get_value("energy"),
)
return load(pytac_lattice, at_lattice, callback, disable_emittance)
return await load(pytac_lattice, at_lattice, callback, disable_emittance)


def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
async def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
"""Load simulator data sources onto the lattice and its elements.

Args:
Expand All @@ -58,7 +58,9 @@ def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
f"(AT:{len(at_lattice)} Pytac:{len(pytac_lattice)})."
)
# Initialise an instance of the ATSimulator Object.
atsim = ATSimulator(at_lattice, callback, disable_emittance)
atsim = await ATSimulator.create(
at_lattice, callback, disable_emittance
) # TODO: This feels like a wierd place to create the simulator?
# Set the simulator data source on the Pytac lattice.
pytac_lattice.set_data_source(ATLatticeDataSource(atsim), pytac.SIM)
# Load the sim onto each element.
Expand Down
12 changes: 6 additions & 6 deletions src/atip/sim_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def add_field(self, field):
else:
self._fields.append(field)

def get_value(self, field, handle=None, throw=True):
async def get_value(self, field, handle=None, throw=True):
"""Get the value for a field.

Args:
Expand All @@ -148,7 +148,7 @@ def get_value(self, field, handle=None, throw=True):
# Wait for any outstanding calculations to conclude, to ensure they are
# complete before a value is returned; if the wait times out then raise
# an error message or log a warning according to the value of throw.
if not self._atsim.wait_for_calculations():
if not await self._atsim.wait_for_calculations():
error_msg = "Check for completion of outstanding calculations timed out."
if throw:
raise ControlSystemException(error_msg)
Expand All @@ -160,7 +160,7 @@ def get_value(self, field, handle=None, throw=True):
else:
raise FieldException(f"No field {field} on AT element {self._at_element}.")

def set_value(self, field, value, throw=None):
async def set_value(self, field, value, throw=None):
"""Set the value for a field. The field and value go onto the queue of
changes on the ATSimulator to be passed to make_change when the queue
is emptied.
Expand All @@ -178,7 +178,7 @@ def set_value(self, field, value, throw=None):
"""
if field in self._fields:
if field in self._set_field_funcs.keys():
self._atsim.queue_set(self._make_change, field, value)
await self._atsim.queue_set(self._make_change, field, value)
else:
raise HandleException(
f"Field {field} cannot be set on element data source {self}."
Expand Down Expand Up @@ -413,7 +413,7 @@ def get_fields(self):
"""
return list(self._field_funcs.keys())

def get_value(self, field, handle=None, throw=True):
async def get_value(self, field, handle=None, throw=True):
"""Get the value for a field on the Pytac lattice.

Args:
Expand All @@ -438,7 +438,7 @@ def get_value(self, field, handle=None, throw=True):
# Wait for any outstanding calculations to conclude, to ensure they are
# complete before a value is returned; if the wait times out then raise
# an error message or log a warning according to the value of throw.
if not self._atsim.wait_for_calculations():
if not await self._atsim.wait_for_calculations():
error_msg = "Check for completion of outstanding calculations timed out."
if throw:
raise ControlSystemException(error_msg)
Expand Down
Loading
Loading