Skip to content

Commit fbabe15

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

File tree

3 files changed

+111
-15
lines changed

3 files changed

+111
-15
lines changed

src/atip/load_sim.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def load_from_filepath(
3737
return load(pytac_lattice, at_lattice, callback, disable_emittance)
3838

3939

40-
def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
40+
def load(
41+
pytac_lattice,
42+
at_lattice,
43+
linopt_function="linopt6",
44+
disable_emittance=False,
45+
disable_chromaticity=False,
46+
disable_radiation=False,
47+
callback=None,
48+
):
4149
"""Load simulator data sources onto the lattice and its elements.
4250
4351
Args:
@@ -58,7 +66,14 @@ def load(pytac_lattice, at_lattice, callback=None, disable_emittance=False):
5866
f"(AT:{len(at_lattice)} Pytac:{len(pytac_lattice)})."
5967
)
6068
# Initialise an instance of the ATSimulator Object.
61-
atsim = ATSimulator(at_lattice, callback, disable_emittance)
69+
atsim = ATSimulator(
70+
at_lattice,
71+
linopt_function,
72+
disable_emittance,
73+
disable_chromaticity,
74+
disable_radiation,
75+
callback,
76+
)
6277
# Set the simulator data source on the Pytac lattice.
6378
pytac_lattice.set_data_source(ATLatticeDataSource(atsim), pytac.SIM)
6479
# Load the sim onto each element.

src/atip/simulator.py

Lines changed: 77 additions & 11 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+
logging.error(
86+
f"Error. Invalid linopt function selected: {linopt_function}. Simulation "
87+
"not computed."
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
@@ -122,12 +173,22 @@ def __init__(self, at_lattice, callback=None, disable_emittance=False):
122173
)
123174
self._at_lat = at_lattice
124175
self._rp = numpy.ones(len(at_lattice) + 1, dtype=bool)
176+
self._linopt_function = linopt_function
125177
self._disable_emittance = disable_emittance
126-
self._at_lat.radiation_on()
178+
self._disable_chromaticity = disable_chromaticity
179+
self._disable_radiation = disable_radiation
180+
181+
if not self._disable_radiation:
182+
self._at_lat.radiation_on()
127183

128184
# Initial phys data calculation.
129185
self._lattice_data = calculate_optics(
130-
self._at_lat, self._rp, self._disable_emittance
186+
self._at_lat,
187+
self._rp,
188+
self._linopt_function,
189+
self._disable_emittance,
190+
self._disable_chromaticity,
191+
self._disable_radiation,
131192
)
132193

133194
# Threading stuff initialisation.
@@ -196,7 +257,12 @@ def _recalculate_phys_data(self, callback):
196257
if bool(self._paused) is False:
197258
try:
198259
self._lattice_data = calculate_optics(
199-
self._at_lat, self._rp, self._disable_emittance
260+
self._at_lat,
261+
self._rp,
262+
self._linopt_function,
263+
self._disable_emittance,
264+
self._disable_chromaticity,
265+
self._disable_radiation,
200266
)
201267
except Exception as e:
202268
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)