Skip to content

Commit 436a20b

Browse files
committed
Allow toggling of additional simulator behaviour
Allow selecting linopt function, disabling chromaticity aand disabling radiation
1 parent eed5656 commit 436a20b

File tree

3 files changed

+154
-29
lines changed

3 files changed

+154
-29
lines changed

src/atip/load_sim.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,29 @@
1313

1414

1515
def load_from_filepath(
16-
pytac_lattice, at_lattice_filepath, callback=None, disable_emittance=False
16+
pytac_lattice,
17+
at_lattice_filepath,
18+
linopt_function="linopt6",
19+
disable_emittance=False,
20+
disable_chromaticity=False,
21+
disable_radiation=False,
22+
callback=None,
1723
):
1824
"""Load simulator data sources onto the lattice and its elements.
1925
2026
Args:
2127
pytac_lattice (pytac.lattice.Lattice): An instance of a Pytac lattice.
2228
at_lattice_filepath (str): The path to a .mat file from which the
2329
Accelerator Toolbox lattice can be loaded.
30+
linopt_function (str): Which pyAT linear optics function to use: linopt2,
31+
linopt4, linopt6.
32+
disable_emittance (bool): Whether the emittance calculations should be
33+
disabled.
34+
disable_chromaticity (bool): Whether the chromaticity calculations should be
35+
disabled.
36+
disable_radiation (bool): Whether radiation calculations should be disabled.
2437
callback (typing.Callable): To be called after completion of each round of
25-
physics calculations.
26-
disable_emittance (bool): Whether the emittance should be calculated.
38+
physics calculations.
2739
2840
Returns:
2941
pytac.lattice.Lattice: The same Pytac lattice object, but now with a
@@ -34,19 +46,40 @@ def load_from_filepath(
3446
name=pytac_lattice.name,
3547
energy=pytac_lattice.get_value("energy", units=pytac.PHYS),
3648
)
37-
return load(pytac_lattice, at_lattice, callback, disable_emittance)
49+
return load(
50+
pytac_lattice,
51+
at_lattice,
52+
linopt_function,
53+
disable_emittance,
54+
disable_chromaticity,
55+
disable_radiation,
56+
callback,
57+
)
3858

3959

40-
def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
60+
def load(
61+
pytac_lattice,
62+
at_lattice,
63+
linopt_function="linopt6",
64+
disable_emittance=False,
65+
disable_chromaticity=False,
66+
disable_radiation=False,
67+
callback=None,
68+
):
4169
"""Load simulator data sources onto the lattice and its elements.
4270
4371
Args:
4472
pytac_lattice (pytac.lattice.Lattice): An instance of a Pytac lattice.
45-
at_lattice (at.lattice_object.Lattice): An instance of an Accelerator
46-
Toolbox lattice object.
73+
at_lattice (at.lattice_object.Lattice): An instance of an AT lattice object.
74+
linopt_function (str): Which pyAT linear optics function to use: linopt2,
75+
linopt4, linopt6.
76+
disable_emittance (bool): Whether the emittance calculations should be
77+
disabled.
78+
disable_chromaticity (bool): Whether the chromaticity calculations should be
79+
disabled.
80+
disable_radiation (bool): Whether radiation calculations should be disabled.
4781
callback (typing.Callable): To be called after completion of each round of
48-
physics calculations.
49-
disable_emittance (bool): Whether the emittance should be calculated.
82+
physics calculations.
5083
5184
Returns:
5285
pytac.lattice.Lattice: The same Pytac lattice object, but now with a
@@ -58,7 +91,14 @@ def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
5891
f"(AT:{len(at_lattice)} Pytac:{len(pytac_lattice)})."
5992
)
6093
# Initialise an instance of the ATSimulator Object.
61-
atsim = ATSimulator(at_lattice, callback, disable_emittance)
94+
atsim = ATSimulator(
95+
at_lattice,
96+
linopt_function,
97+
disable_emittance,
98+
disable_chromaticity,
99+
disable_radiation,
100+
callback,
101+
)
62102
# Set the simulator data source on the Pytac lattice.
63103
pytac_lattice.set_data_source(ATLatticeDataSource(atsim), pytac.SIM)
64104
# Load the sim onto each element.

src/atip/simulator.py

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class LatticeData:
2424
def calculate_optics(
2525
at_lattice: at.lattice_object.Lattice,
2626
refpts: ArrayLike,
27+
linopt_function: str = "linopt6",
2728
disable_emittance: bool = False,
29+
disable_chromaticity: bool = False,
30+
disable_radiation: bool = False,
2831
) -> LatticeData:
2932
"""Perform the physics calculations on the lattice.
3033
@@ -43,21 +46,61 @@ def calculate_optics(
4346
LatticeData: The calculated lattice data.
4447
"""
4548
logging.debug("Starting physics calculations.")
46-
47-
orbit0, _ = at_lattice.find_orbit6()
48-
logging.debug("Completed orbit calculation.")
49-
50-
_, beamdata, twiss = at_lattice.linopt6(
51-
refpts=refpts, get_chrom=True, orbit=orbit0, keep_lattice=True
49+
logging.debug(
50+
f"Using simulation params: {linopt_function}, disable_emittance="
51+
f"{disable_emittance}, disable_chromaticity={disable_chromaticity}, "
52+
f"disable_radiation={disable_radiation}"
5253
)
54+
if linopt_function == "linopt6":
55+
orbit0, _ = at_lattice.find_orbit6()
56+
logging.debug("Completed orbit calculation.")
57+
58+
_, beamdata, twiss = at_lattice.linopt6(
59+
refpts=refpts,
60+
get_chrom=not disable_chromaticity,
61+
orbit=orbit0,
62+
keep_lattice=True,
63+
)
64+
elif linopt_function == "linopt4":
65+
orbit0, _ = at_lattice.find_orbit4()
66+
logging.debug("Completed orbit calculation.")
67+
68+
_, beamdata, twiss = at_lattice.linopt6(
69+
refpts=refpts,
70+
get_chrom=not disable_chromaticity,
71+
orbit=orbit0,
72+
keep_lattice=True,
73+
)
74+
elif linopt_function == "linopt2":
75+
orbit0, _ = at_lattice.find_orbit()
76+
logging.debug("Completed orbit calculation.")
77+
78+
_, beamdata, twiss = at_lattice.linopt2(
79+
refpts=refpts,
80+
get_chrom=not disable_chromaticity,
81+
orbit=orbit0,
82+
keep_lattice=True,
83+
)
84+
else:
85+
raise ValueError(
86+
f"Error. Invalid linopt function selected: {linopt_function}. Simulation "
87+
"data not calculated."
88+
)
89+
5390
logging.debug("Completed linear optics calculation.")
5491

5592
if not disable_emittance:
5693
emitdata = at_lattice.ohmi_envelope(orbit=orbit0, keep_lattice=True)
5794
logging.debug("Completed emittance calculation")
5895
else:
5996
emitdata = ()
60-
radint = at_lattice.get_radiation_integrals(twiss=twiss)
97+
98+
if not disable_radiation:
99+
radint = at_lattice.get_radiation_integrals(twiss=twiss)
100+
logging.debug("Completed radiation calculation")
101+
else:
102+
radint = ()
103+
61104
logging.debug("All calculation complete.")
62105
return LatticeData(twiss, beamdata.tune, beamdata.chromaticity, emitdata, radint)
63106

@@ -98,7 +141,15 @@ class ATSimulator:
98141
physics data upon a change.
99142
"""
100143

101-
def __init__(self, at_lattice, callback=None, disable_emittance=False):
144+
def __init__(
145+
self,
146+
at_lattice,
147+
linopt_function="linopt6",
148+
disable_emittance=False,
149+
disable_chromaticity=False,
150+
disable_radiation=False,
151+
callback=None,
152+
):
102153
"""
103154
.. Note:: To avoid errors, the physics data must be initially
104155
calculated here, during creation, otherwise it could be accidentally
@@ -107,12 +158,16 @@ def __init__(self, at_lattice, callback=None, disable_emittance=False):
107158
the thread.
108159
109160
Args:
110-
at_lattice (at.lattice_object.Lattice): An instance of an AT
111-
lattice object.
112-
callback (typing.Callable): Optional, if passed it is called on completion
113-
of each round of physics calculations.
114-
disable_emittance (bool): Whether or not to perform the beam
115-
envelope based emittance calculations.
161+
at_lattice (at.lattice_object.Lattice): An instance of an AT lattice object.
162+
linopt_function (str): Which pyAT linear optics function to use: linopt2,
163+
linopt4, linopt6.
164+
disable_emittance (bool): Whether the emittance calculations should be
165+
disabled.
166+
disable_chromaticity (bool): Whether the chromaticity calculations should be
167+
disabled.
168+
disable_radiation (bool): Whether radiation calculations should be disabled.
169+
callback (typing.Callable): To be called after completion of each round of
170+
physics calculations.
116171
117172
**Methods:**
118173
"""
@@ -122,12 +177,22 @@ def __init__(self, at_lattice, callback=None, disable_emittance=False):
122177
)
123178
self._at_lat = at_lattice
124179
self._rp = numpy.ones(len(at_lattice) + 1, dtype=bool)
180+
self._linopt_function = linopt_function
125181
self._disable_emittance = disable_emittance
126-
self._at_lat.radiation_on()
182+
self._disable_chromaticity = disable_chromaticity
183+
self._disable_radiation = disable_radiation
184+
185+
if not self._disable_radiation:
186+
self._at_lat.radiation_on()
127187

128188
# Initial phys data calculation.
129189
self._lattice_data = calculate_optics(
130-
self._at_lat, self._rp, self._disable_emittance
190+
self._at_lat,
191+
self._rp,
192+
self._linopt_function,
193+
self._disable_emittance,
194+
self._disable_chromaticity,
195+
self._disable_radiation,
131196
)
132197

133198
# Threading stuff initialisation.
@@ -196,7 +261,12 @@ def _recalculate_phys_data(self, callback):
196261
if bool(self._paused) is False:
197262
try:
198263
self._lattice_data = calculate_optics(
199-
self._at_lat, self._rp, self._disable_emittance
264+
self._at_lat,
265+
self._rp,
266+
self._linopt_function,
267+
self._disable_emittance,
268+
self._disable_chromaticity,
269+
self._disable_radiation,
200270
)
201271
except Exception as e:
202272
warn(at.AtWarning(e), stacklevel=1)

src/atip/utils.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ def load_at_lattice(mode="I04", **kwargs):
2929
return at_lattice
3030

3131

32-
def loader(mode="I04", callback=None, disable_emittance=False):
32+
def loader(
33+
mode="I04",
34+
linopt_function="linopt6",
35+
disable_emittance=False,
36+
disable_chromaticity=False,
37+
disable_radiation=False,
38+
callback=None,
39+
):
3340
"""Load a unified lattice of the specifed mode.
3441
3542
.. Note:: A unified lattice is a Pytac lattice where the corresponding AT
@@ -52,7 +59,15 @@ def loader(mode="I04", callback=None, disable_emittance=False):
5259
periodicity=1,
5360
energy=pytac_lattice.get_value("energy", units=pytac.PHYS),
5461
)
55-
lattice = atip.load_sim.load(pytac_lattice, at_lattice, callback, disable_emittance)
62+
lattice = atip.load_sim.load(
63+
pytac_lattice,
64+
at_lattice,
65+
linopt_function,
66+
disable_emittance,
67+
disable_chromaticity,
68+
disable_radiation,
69+
callback,
70+
)
5671
return lattice
5772

5873

0 commit comments

Comments
 (0)