bluemira.equilibria.optimisation.constraint_funcs

Equilibrium optimisation constraint functions.

Constraint functions must be of the form:

class Constraint(ConstraintFunction):

    def f_constraint(self, vector: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
        return constraint_calc(vector)

    def df_constraint(self, vector: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
        return gradient_calc(vector)

The constraint function convention is such that c <= 0 is sought. I.e. all constraint values must be negative.

Note that the gradient (Jacobian) of the constraint function is of the form:

\[ \begin{align}\begin{aligned}\nabla \mathbf{c} = \begin{bmatrix} \dfrac{\partial c_{0}}{\partial x_0} & \dfrac{\partial c_{0}}{\partial x_1} & ...\\ \dfrac{\partial c_{1}}{\partial x_0} & \dfrac{\partial c_{1}}{\partial x_1} & ...\\ ... & ... & ...\\ \end{bmatrix}\end{aligned}\end{align} \]

If the df_constraint function is not provided, the constraint can still be used for derivative-free optimisation algorithms, but will need to be updated or approximated for use in derivative based algorithms, such as those utilising gradient descent.

Classes

ConstraintFunction

Override to define a numerical constraint for a coilset optimisation.

AxBConstraint

Constraint function of the form:

L2NormConstraint

Constrain the L2 norm of an Ax = b system of equations.

FieldConstraintFunction

Current optimisation poloidal field constraints at prescribed locations

CurrentMidplanceConstraint

Constraint function to constrain the inboard or outboard midplane

CoilForceConstraintFunctions

Constraint functions to constrain the force applied to the coils

CoilForceConstraint

Combined constraint function for:

Module Contents

class bluemira.equilibria.optimisation.constraint_funcs.ConstraintFunction

Bases: abc.ABC

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.ConstraintFunction

Override to define a numerical constraint for a coilset optimisation.

property name: str | None

The name of the constraint

Return type:

str | None

abstract f_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

The constraint function.

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

property constraint_type: Literal['inequality', 'equality']

The type of constraint

Return type:

Literal[‘inequality’, ‘equality’]

class bluemira.equilibria.optimisation.constraint_funcs.AxBConstraint(a_mat: numpy.typing.NDArray[numpy.float64], b_vec: numpy.typing.NDArray[numpy.float64], value: float, scale: float, name: str | None = None)

Bases: ConstraintFunction

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.AxBConstraint

Constraint function of the form:

A.x - b < value

Parameters:
  • a_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix

  • b_vec (numpy.typing.NDArray[numpy.float64]) – Target value vector

  • value (float) – Target constraint value

  • scale (float) – Current scale with which to calculate the constraints

  • name (str | None)

a_mat
b_vec
value
scale
name = None

The name of the constraint

f_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint function

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

df_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint derivative

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

class bluemira.equilibria.optimisation.constraint_funcs.L2NormConstraint(a_mat: numpy.typing.NDArray[numpy.float64], b_vec: numpy.typing.NDArray[numpy.float64], value: float, scale: float, name: str | None = None)

Bases: ConstraintFunction

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.L2NormConstraint

Constrain the L2 norm of an Ax = b system of equations.

||(Ax - b)||² < value

Parameters:
  • a_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix

  • b_vec (numpy.typing.NDArray[numpy.float64]) – Target value vector

  • value (float) – Target constraint value

  • scale (float) – Current scale with which to calculate the constraints

  • name (str | None)

a_mat
b_vec
value
scale
name = None

The name of the constraint

f_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint function

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

df_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint derivative

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

class bluemira.equilibria.optimisation.constraint_funcs.FieldConstraintFunction(ax_mat: numpy.typing.NDArray[numpy.float64], az_mat: numpy.typing.NDArray[numpy.float64], bxp_vec: numpy.typing.NDArray[numpy.float64], bzp_vec: numpy.typing.NDArray[numpy.float64], B_max: numpy.typing.NDArray[numpy.float64], scale: float, name: str | None = None, *, round_dp: int = 16)

Bases: ConstraintFunction

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.FieldConstraintFunction

Current optimisation poloidal field constraints at prescribed locations

Parameters:
  • ax_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix for Bx (active coil contributions)

  • az_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix for Bz (active coil contributions)

  • bxp_vec (numpy.typing.NDArray[numpy.float64]) – Background vector for Bx (passive coil contributions)

  • bzp_vec (numpy.typing.NDArray[numpy.float64]) – Background vector for Bz (passive coil contributions)

  • B_max (numpy.typing.NDArray[numpy.float64]) – Maximum fields inside the coils

  • scale (float) – Current scale with which to calculate the constraints

  • name (str | None) – name of constraint (used in error reporting)

  • round_dp (int) – round the output of the constraint (to maintain numerical stability)

ax_mat
az_mat
bxp_vec
bzp_vec
B_max
scale
name = None

The name of the constraint

_round_dp = 16
f_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint function

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

df_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint derivative

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

class bluemira.equilibria.optimisation.constraint_funcs.CurrentMidplanceConstraint(eq: bluemira.equilibria.equilibrium.Equilibrium, radius: float, scale: float, *, inboard: bool, name: str | None = None)

Bases: ConstraintFunction

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.CurrentMidplanceConstraint

Constraint function to constrain the inboard or outboard midplane of the plasma during optimisation.

Parameters:
  • eq (bluemira.equilibria.equilibrium.Equilibrium) – Equilibrium to use to fetch last closed flux surface from.

  • radius (float) – Toroidal radius at which to constrain the plasma midplane.

  • scale (float) – Current scale with which to calculate the constraints

  • inboard (bool) – Boolean controlling whether to constrain the inboard (if True) or outboard (if False) side of the plasma midplane.

  • name (str | None)

eq
radius
scale
inboard
name = None

The name of the constraint

f_constraint(vector: numpy.typing.NDArray[numpy.float64]) numpy.typing.NDArray[numpy.float64]

Constraint function

Parameters:

vector (numpy.typing.NDArray[numpy.float64])

Return type:

numpy.typing.NDArray[numpy.float64]

class bluemira.equilibria.optimisation.constraint_funcs.CoilForceConstraintFunctions(a_mat: numpy.typing.NDArray[numpy.float64], b_vec: numpy.typing.NDArray[numpy.float64], n_PF: int, n_CS: int, scale: float = 1000000.0)

Constraint functions to constrain the force applied to the coils

Parameters:
  • a_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix

  • b_vec (numpy.typing.NDArray[numpy.float64]) – Target value vector

  • n_PF (int) – Number of PF coils

  • n_CS (int) – Number of CS coils

  • scale (float) – Force scale with which to calculate the constraints

a_mat
b_vec
n_PF
n_CS
scale = 1000000.0
n_coils
_constraint
_grad
property constraint

Constraint

property grad

Constraint Gradient

calc_f_matx(currents)
Returns:

Force matrix

calc_df_matx(currents)
Returns:

Force Jacobian

cs_fz(f_matx)
Returns:

Vertical forces on CS coils.

pf_z_constraint(f_matx, max_value)

Constraint Function Absolute vertical force constraint on PF coils.

pf_z_constraint_grad(f_matx, df_matx)

Constraint Derivative: Absolute vertical force constraint on PF coils.

cs_z_constraint(f_matx, max_value)

Constraint Function: Absolute sum of vertical force constraint on entire CS stack.

cs_z_grad(f_matx, df_matx)

Constraint Derivative: Absolute sum of vertical force constraint on entire CS stack

cs_z_sep_constraint(f_matx, max_value)

Constraint Function: CS separation constraints.

cs_z_sep_grad(df_matx)

Constraint Derivative: CS separation constraints.

class bluemira.equilibria.optimisation.constraint_funcs.CoilForceConstraint(a_mat: numpy.typing.NDArray[numpy.float64], b_vec: numpy.typing.NDArray[numpy.float64], n_PF: int, n_CS: int, PF_Fz_max: float, CS_Fz_sum_max: float, CS_Fz_sep_max: float, scale: float, name: str | None = None, *, round_dp: int = 16)

Bases: ConstraintFunction, CoilForceConstraintFunctions

Inheritance diagram of bluemira.equilibria.optimisation.constraint_funcs.CoilForceConstraint

Combined constraint function for:
  • PF vertical force,

  • CS vertical force,

  • and CS separation.

Parameters:
  • a_mat (numpy.typing.NDArray[numpy.float64]) – Response matrix

  • b_vec (numpy.typing.NDArray[numpy.float64]) – Target value vector

  • n_PF (int) – Number of PF coils

  • n_CS (int) – Number of CS coils

  • PF_Fz_max (float) – The maximum force in the z direction for a PF coil

  • CS_Fz_sum_max (float) – The total maximum force in the z direction for the CS coils

  • CS_Fz_sep_max (float) – The individual maximum force in the z direction for the CS coils

  • scale (float) – Current scale with which to calculate the constraints

  • name (str | None) – name of constraint (used in error reporting)

  • round_dp (int) – round the output of the constraint (to maintain numerical stability)

PF_Fz_max
CS_Fz_sum_max
CS_Fz_sep_max
name = None

The name of the constraint

_round_dp = 16
f_constraint(vector)

Constraint function

Returns:

Coil force

df_constraint(vector)

Constraint derivative

Returns:

Derivative of the coil force