pyaccelerator.constraints

Module Contents

Classes

BaseTarget

Base target.

TargetPhasespace

Target phase space coordinates.

TargetTwiss

Target twiss parameters.

TargetTwissSolution

Target periodic twiss solution, twiss parameters at the beginning and

TargetDispersion

Target dispersion function.

TargetGlobal

Target global lattice attribute.

FreeParameter

Constraint free parameter.

Constraints

Match a lattice to constraints.

class pyaccelerator.constraints.BaseTarget[source]

Base target.

abstract loss(self, lattice: Lattice)[source]

Compute the loss for this target.

class pyaccelerator.constraints.TargetPhasespace(element: Union[str, ‘BaseElement’], x: Optional[float] = None, x_prime: Optional[float] = None, y: Optional[float] = None, y_prime: Optional[float] = None, dp: Optional[float] = None, initial: Optional[Sequence[float]] = None)[source]

Target phase space coordinates.

Parameters
  • element – Element name pattern or element instance at which the value should be achieved.

  • value – Target phase space coordinates at the given element.

  • x (optional) – Target x coordinate.

  • x_prime (optional) – Target x_prime coordinate.

  • y (optional) – Target y coordinate.

  • y_prime (optional) – Target y_prime coordinate.

  • dp (optional) – Target dp coordinate (probably breaks everything).

  • initial (optional) – Initial phase space coordinates with which to start the transport. If None will use the close orbit solution.

loss(self, lattice: Lattice) → float[source]

Compute the loss for this target.

class pyaccelerator.constraints.TargetTwiss(element: Union[str, ‘BaseElement’], beta: Optional[float] = None, alpha: Optional[float] = None, gamma: Optional[float] = None, plane: str = 'h')[source]

Target twiss parameters.

Parameters
  • element – Element name pattern or element instance at which the target beta, alpha, gamma should be achieved.

  • beta (optional) – Target beta value at the location of the element.

  • alpha (optional) – Target alpha value at the location of the element.

  • gamma (optional) – Target gamma value at the location of the element.

  • plane – Plane of interest, either “h” or “v”.

loss(self, lattice: Lattice) → float[source]

Compute the loss for this target.

class pyaccelerator.constraints.TargetTwissSolution(beta: Optional[float] = None, alpha: Optional[float] = None, gamma: Optional[float] = None, plane: str = 'h')[source]

Target periodic twiss solution, twiss parameters at the beginning and end of the lattice.

Useful when a lattice needs some coaxing into having a periodic twiss solution i.e. when lattice.twiss fails to find a solution.

Note: Only one of the twiss arguments can be omitted.

Parameters
  • beta (optional) – Target beta value at beginning and end of lattice.

  • alpha (optional) – Target alpha value at beginning and end of lattice.

  • gamma (optional) – Target gamma value at beginning and end of lattice.

  • plane – Plane of interest, either “h” or “v”.

loss(self, lattice: Lattice) → float[source]

Compute the loss for this target.

class pyaccelerator.constraints.TargetDispersion(element: Union[str, ‘BaseElement’], value: float, plane: str = 'h', **solver_kwargs)[source]

Target dispersion function.

Parameters
  • element – Element name pattern or element instance at which the value should be achieved.

  • value – Target value of dispersion function at the given element.

  • plane – Plane of interest, either “h” or “v”.

loss(self, lattice: Lattice) → float[source]

Compute the loss for this target.

class pyaccelerator.constraints.TargetGlobal(method: str, value: float, **method_kwargs)[source]

Target global lattice attribute.

Parameters
  • method – lattice method to match.

  • value – Target value.

  • **method_kwargs – additional method kwargs.

Examples

Create a constraint on the horizontal tune value:

>>> TargetGlobal("tune", 0.23, plane='h', n_turns=512)
TargetGlobal(method='tune', value=0.23, method_kwargs={'plane': 'h', 'n_turns': 512})
loss(self, lattice: Lattice)[source]

Compute the loss for this target.

class pyaccelerator.constraints.FreeParameter(element: Union[str, ‘BaseElement’], attribute: str)[source]

Constraint free parameter.

Parameters
  • element – Element name pattern or element instance for which the provided attribute will be considered a free parameter.

  • attribute – attribute of element.

class pyaccelerator.constraints.Constraints(lattice: Lattice)[source]

Match a lattice to constraints.

Parameters

latticeLattice instance on which to match.

Examples

Compute Drift length to reach a x coord of 10 meters:

>>> lat = Lattice([Drift(1)])
>>> lat.constraints.add_free_parameter(element="drift", attribute="l")
>>> target = TargetPhasespace("drift", x=10, initial=[0, 1, 0, 0, 0])
>>> lat.constraints.add_target(target)
>>> matched_lat, _ = lat.constraints.match()
>>> matched_lat
Lattice([Drift(length=10, name='drift_0')])

Compute Drift length to reach a x coord of 5 meters after the first Drift:

>>> lat = Lattice([Drift(1), Drift(1)])
>>> lat.constraints.add_free_parameter("drift_0", "l")
>>> target = TargetPhasespace("drift_0", x=5, initial=[0, 1, 0, 0, 0])
>>> lat.constraints.add_target(target)
>>> matched_lat, _ = lat.constraints.match()
>>> matched_lat
Lattice([Drift(length=5, name='drift_0'), Drift(length=1, name='drift_1')])

Compute Drift length to reach a x coord of 5 meters after the second Drift with equal lengths of both Drifts:

>>> lat = Lattice([Drift(1), Drift(1)])
>>> lat.constraints.add_free_parameter("drift", "l")
>>> target = TargetPhasespace("drift_1", x=5, initial=[0, 1, 0, 0, 0])
>>> lat.constraints.add_target(target)
>>> matched_lat, _ = lat.constraints.match()
>>> matched_lat
Lattice([Drift(length=2.5, name='drift_0'), Drift(length=2.5, name='drift_1')])

Compute the Quadrupole strengths of a FODO cell to achieve a minimum beta of 0.5 meters:

>>> lat = Lattice([QuadrupoleThin(1.6, name='quad_f'), Drift(1), QuadrupoleThin(-0.8, name='quad_d'),
... Drift(1), QuadrupoleThin(1.6, name='quad_f')])
>>> lat.constraints.add_free_parameter("quad_f", "f")
>>> lat.constraints.add_free_parameter("quad_d", "f")
>>> target = TargetTwiss("quad_d", beta=0.5, plane="h")
>>> lat.constraints.add_target(target)
>>> matched_lat, _ = lat.constraints.match()
>>> matched_lat
Lattice([QuadrupoleThin(f=1.319, name='quad_f'), Drift(length=1, name='drift_0'),
         QuadrupoleThin(f=-0.918, name='quad_d'), Drift(1, name='drift_1'),
         QuadrupoleThin(f=1.319, name='quad_f')])
add_target(self, target: BaseTarget)[source]

Add a constraint target.

Parameters

target – An instance of either TargetPhasespace, TargetDispersion, TargetTwiss or TargetGlobal.

add_free_parameter(self, element: str, attribute: str)[source]

Add a free parameter.

Parameters
  • element – Element name pattern or element instance for which the provided attribute will be considered a free parameter.

  • attribute – attribute of element.

Examples

Setting a Drift’s length as a free parameters:

>>> drift = Drift(1)
>>> lat = Lattice([drift])
>>> lat
Lattice([Drift(l=1, name='drift_0')])
>>> lat.constraints.add_free_parameter("drift_0", "l")
... # or lat.constraints.add_free_parameter(drift, "l")
clear(self)[source]

Clear the targets and free parameters.

match(self, *args, **kwargs) → Tuple[‘Lattice’, ‘OptimizeResult’][source]

Match lattice properties to constraints using scipy.optimize.minimize.

Parameters
  • *args – Passed to scipy.optimize.minimize.

  • **kwargs – Passed to scipy.optimize.minimize.

Raises

ValueError – If no targets or free parameters specified.

Returns

New matched Lattice instance and scipy.optimize.OptmizeResult.