tolerancing.compensator

Compensator Module

This module contains a class for compensating perturbations in an optical system. The CompensatorOptimizer class is used to optimize a set of variables to compensate for perturbations in an optical system.

Kramer Harrison, 2024

Classes

CompensatorOptimizer([method, tol])

A class representing a compensator for a tolerancing problem.

class CompensatorOptimizer(method='generic', tol=1e-05)[source]

A class representing a compensator for a tolerancing problem. This class optimizes a set of variables to compensate for perturbations in an optical system.

Parameters:
  • method (str) – The type of optimizer to use. Default is ‘generic’. Other options: ‘least_squares’

  • tol (float) – The tolerance for the optimizer. Default is 1e-5.

method

The type of optimizer to use. Default is ‘generic’. Other options include ‘least_squares’.

Type:

str

tol

The tolerance for the optimizer. Default is 1e-5.

Type:

float

_optimizer_map

A mapping of optimizer types to their respective classes.

Type:

dict

add_operand(operand_type=None, target=None, min_val=None, max_val=None, weight=1, input_data=None)

Add an operand to the merit function

add_variable(optic, variable_type, scaler: Scaler = None, **kwargs)

Add a variable to the merit function

property batching_enabled: bool

Whether batched evaluation is currently active.

clear_operands()

Clear all operands from the merit function

clear_variables()

Clear all variables from the merit function

disable_batching()

Disable batched ray evaluation and use standard per-operand evaluation.

enable_batching()

Enable batched ray evaluation for faster optimization.

When batching is enabled, operands that require ray tracing are grouped by optic and wavelength so that redundant traces are eliminated. This can dramatically speed up merit-function evaluation for problems with many ray operands.

The evaluator is re-created whenever this method is called, so it always reflects the current set of operands.

fun_array()

Array of operand contribution terms for the merit function.

Each term is computed as:

effective_weight(op) * op.delta() ** 2

where effective_weight = operand.weight * field_weight * wl_weight. Field and wavelength weights are read from the optic stored in each operand’s input_data. Operands with an effective weight of zero are excluded from the result.

When batching is enabled, delegates to the BatchedRayEvaluator which minimises redundant ray traces.

Returns:

1-D array of per-operand contribution values. Returns [0.0] when there are no active operands.

Return type:

be.ndarray

get_optimizer()[source]
property has_variables

Check if the optimizer has variables. If no variables are present, then the optimizer will not run.

Returns:

True if the optimizer has variables, False otherwise.

Return type:

bool

info()

Print information about the optimization problem.

merit_info()

Print information about the merit function.

operand_info()

Print information about the operands in the merit function

residual_vector()

Vector of weighted operand deltas (unsquared).

Returns a 1-D array whose i-th element is weight_i * delta_i for each operand. This is the residual vector r needed by least-squares algorithms such as the Damped Least-Squares (Levenberg-Marquardt) optimizer.

Unlike fun_array(), the values are not squared, so the merit function equals sum(residual_vector() ** 2).

When batching is enabled, delegates to the BatchedRayEvaluator which minimises redundant ray traces.

Returns:

A 1-D array of length len(self.operands).

Return type:

be.ndarray

rss()

RSS of current merit function

run()[source]

Run the optimizer for compensation.

Returns:

The result of the optimizer run.

Return type:

scipy.optimize.OptimizeResult

sum_squared()

Calculate the sum of squared operand weighted deltas.

When batching is enabled, delegates to the BatchedRayEvaluator which minimises redundant ray traces.

update_optics()

Update all optics considered in the optimization problem

variable_info()

Print information about the variables in the merit function.

weight_breakdown() list[dict]

Return a list of dicts describing each operand’s effective weight.

The effective weight is the product of the operand’s own weight, the field weight (looked up from the optic via the operand’s input_data), and the wavelength weight. The formula used in the merit function is:

effective_weight × delta ** 2

Each returned dict contains:

  • operand_type (str): The operand type string.

  • field: The field index or coordinate from input_data (or None).

  • wavelength: The wavelength index or value from input_data (or None).

  • operand_weight (float): The user-set Operand.weight.

  • field_weight (float): The field’s weight from the optic (1.0 if not resolvable).

  • wl_weight (float): The wavelength’s weight from the optic (1.0 if not resolvable).

  • effective_weight (float): Product of the three weights above.

Returns:

One dict per operand in self.operands.

Return type:

list[dict]