Gaussian Smoother¶
- diffinytrace.gaussian_smoother.gaussian_func_1D(eval_points: Tensor, x_range, num_gauss_points: int, sigma: float, include_boundary=False) Tensor[source]¶
Gaussian function for 1D convolution.
- Parameters:
eval_points (torch.Tensor) – Points where the Gaussian function is evaluated.
x_range (tuple) – Range of the target plane.
num_gauss_points (int) – Number of Gaussian points.
sigma (float) – Standard deviation of the Gaussian function.
include_boundary (bool) – Whether to include the boundary points.
- Returns:
Evaluated Gaussian function.
- Return type:
torch.Tensor
- diffinytrace.gaussian_smoother.gaussian_func_2D(eval_points: Tensor, x_range, y_range, x_grid_size: int, y_grid_size: int, sigma: float | Tensor, val_multi: Tensor | None = None, summed: bool = True, include_boundary=False) Tensor[source]¶
Gaussian function for 2D convolution.
- Parameters:
eval_points (torch.Tensor) – Points where the Gaussian function is evaluated.
y_range (tuple) – Range of the target plane in the vertical direction.
x_range (tuple) – Range of the target plane in the horizontal direction.
y_grid_size (int) – Number of Gaussian points in the vertical direction.
x_grid_size (int) – Number of Gaussian points in the horizontal direction.
sigma (float) – Standard deviation of the Gaussian function.
val_multi (torch.Tensor|None) – Optional multiplier for the Gaussian function.
summed (bool) – Whether to sum the Gaussian function.
include_boundary (bool) – Whether to include the boundary points.
- Returns:
Evaluated Gaussian function.
- Return type:
torch.Tensor
- diffinytrace.gaussian_smoother.calc_smooth_desired_irradiance(desired_irradiance_fun: Callable, x_range: List[float], y_range: List[float], x_grid_size: int, y_grid_size: int, sigma: float, num_integration_points: int, num_splits=5, dtype=torch.float64, device=device(type='cpu')) Tensor[source]¶
Calculates the smoothed desired irradiance using Gaussian convolution.
- Parameters:
desired_irradiance_fun (Callable) – Function that computes the desired irradiance at given points.
x_range (List[float]) – Range of the target plane in the x direction [min, max].
y_range (List[float]) – Range of the target plane in the y direction [min, max].
x_grid_size (int) – Number of pixels in the x direction.
y_grid_size (int) – Number of pixels in the y direction.
sigma (float) – Standard deviation of the Gaussian kernel.
num_integration_points (int) – Number of integration points for numerical integration.
num_splits (int, optional) – Number of splits for integration to reduce memory usage. Defaults to 5.
dtype (torch.dtype, optional) – Data type for tensors. Defaults to torch.get_default_dtype().
device (torch.device, optional) – Device for computation. Defaults to torch.get_default_device().
- Returns:
Smoothed desired irradiance map.
- Return type:
torch.Tensor
- class diffinytrace.gaussian_smoother.GaussianSmoother(x_range: list, y_range: list, x_grid_size: int, y_grid_size: int, sigma: float, desired_irradiance_fun: Callable, smoothed_num_integration_points: int, smoothed_num_splits: int, dtype=torch.float64, device=device(type='cpu'))[source]¶
Bases:
objectThe GaussianSmoother class implements gaussian measurement functions but also computes smoothed desired irradiance distributions. For more information on this class please refer to the examples.
- Parameters:
x_range (list) – Range of the target plane in the x direction [min, max].
y_range (list) – Range of the target plane in the y direction [min, max].
x_grid_size (int) – Number of pixels in the x direction.
y_grid_size (int) – Number of pixels in the y direction.
sigma (float) – Standard deviation of the Gaussian kernel.
desired_irradiance_fun (Callable) – Function that computes the desired irradiance at given points.
smoothed_num_integration_points (int) – Number of integration points for smoothing.
smoothed_num_splits (int) – Number of splits for integration to reduce memory usage.
dtype (torch.dtype, optional) – Data type for tensors. Defaults to torch.get_default_dtype().
device (torch.device, optional) – Device for computation. Defaults to torch.get_default_device().
- x_grid_size¶
Number of pixels in the x direction.
- Type:
int
- y_grid_size¶
Number of pixels in the y direction.
- Type:
int
- sigma¶
Standard deviation of the Gaussian kernel.
- Type:
float
- include_boundary¶
Whether to include boundary points in the grid.
- Type:
bool
- x_range¶
Range of the target plane in the x direction.
- Type:
list
- y_range¶
Range of the target plane in the y direction.
- Type:
list
- discrete_desired_irradiance¶
Desired irradiance at pixel centers.
- Type:
torch.Tensor
- smoothed_desired_irradiance¶
Smoothed desired irradiance map.
- Type:
torch.Tensor
- smoothed_irradiance(points: Tensor, ray_multi: Tensor, x_range=None, y_range=None) Tensor[source]¶
Computes the smoothed irradiance at given points using a Gaussian kernel.
- Parameters:
points (torch.Tensor) – Array of points where the irradiance is evaluated, shape [N, 2].
ray_multi (torch.Tensor) – Multiplicative weights for each point, e.g., ray flux.
x_range (tuple, optional) – Range of the target plane in the x direction. Defaults to None.
y_range (tuple, optional) – Range of the target plane in the y direction. Defaults to None.
- Returns:
Smoothed irradiance values at the specified points.
- Return type:
torch.Tensor
- none_smoothed_irradiance(points: Tensor, ray_multi: Tensor) Tensor[source]¶
Computes the non-smoothed irradiance at given points by summing ray contributions in each grid cell.
- Parameters:
points (torch.Tensor) – Array of points where the irradiance is evaluated, shape [N, 2].
ray_multi (torch.Tensor) – Multiplicative weights for each point, e.g., ray flux.
- Returns:
Non-smoothed irradiance values at the specified grid cells.
- Return type:
torch.Tensor
- integrate_values(vals: Tensor, x_range=None, y_range=None) Tensor[source]¶
Integrates the provided values over the grid using midpoint rule.
- Parameters:
vals (torch.Tensor) – Values to integrate, typically irradiance or residuals, shape matching the grid.
- Returns:
The integrated sum over the grid.
- Return type:
torch.Tensor
- diffinytrace.gaussian_smoother.make_evaluation_function(optical_system: SequentialOpticalSystem, sequence: List, source: LightSource, detector, smoother: GaussianSmoother, num_splits: int = 10, num_rays_per_split: int = 100000, method_ray_tracing='monte_carlo', device=device(type='cpu')) Callable[source]¶
Creates an evaluation function for comparing simulated and desired irradiance.
- Parameters:
optical_system (SequentialOpticalSystem) – The optical system to be used for ray tracing.
sequence – The sequence of optical elements.
source (LightSource) – The light source for the simulation.
detector – The detector object.
smoother (GaussianSmoother) – Smoother object for irradiance comparison.
num_splits (int, optional) – Number of splits for ray tracing to reduce memory usage. Defaults to 10.
num_rays_per_split (int, optional) – Number of rays per split. Defaults to 1,000,000.
method_ray_tracing (str, optional) – Ray tracing method (‘monte_carlo’, etc.). Defaults to “monte_carlo”.
device (torch.device, optional) – Device for computation. Defaults to torch.get_default_device().
- Returns:
A function that computes the L2 error between simulated and desired irradiance.
- Return type:
Callable
- diffinytrace.gaussian_smoother.make_merit_function(optical_system: SequentialOpticalSystem, sequence: List, source: LightSource, detector, smoother: GaussianSmoother, num_rays: int, method_ray_tracing='sobol_pow2', use_desired_irradiance_smoothing=True, device=device(type='cpu'), T_margin=None) Callable[source]¶
Creates a merit function to obtain a desired irradiance distribution for the given optical system, source, and detector.
- Parameters:
optical_system (SequentialOpticalSystem) – The optical system to be used.
sequence – The sequence of elements in the optical system.
source (LightSource) – The light source to be used.
detector – The detector to be used.
num_rays (int) – Number of rays to be traced.
smoother (Smoother) – The smoother object for merit function calculation.
device – The device to be used for calculations.
method_ray_tracing (str) – Method for ray tracing (‘sobol’ or ‘midpoint’).
use_desired_irradiance_smoothing (bool) – Whether to use desired irradiance smoothing.
use_power_correction (bool) – Whether to use power correction.
save_last_eval (bool) – Whether to save the last evaluation.
T_margin (float|None) – Optional margin for integration domain if it is None the integration domain will not be adjusted on the fly.
- Returns:
A function that computes the merit value.
- Return type:
Callable
- class diffinytrace.gaussian_smoother.GaussianSmootherSquare(aperture_radius: list, grid_size: int, sigma: float, desired_irradiance_fun: Callable, smoothed_num_integration_points: int, smoothed_num_splits: int, dtype=torch.float64, device=device(type='cpu'))[source]¶
Bases:
GaussianSmootherThis class is a specialized version of GaussianSmoother for cases where the x and y ranges are identical, and the grid is square (same number of pixels in both directions).
- Parameters:
x_range (list) – Range of the target plane in both x and y directions [min, max].
x_grid_size (int) – Number of pixels in both x and y directions.
sigma (float) – Standard deviation of the Gaussian kernel.
desired_irradiance_fun (Callable) – Function that computes the desired irradiance at given points.
smoothed_num_integration_points (int) – Number of integration points for smoothing.
smoothed_num_splits (int) – Number of splits for integration to reduce memory usage.
dtype (torch.dtype, optional) – Data type for tensors. Defaults to torch.get_default_dtype().
device (torch.device, optional) – Device for computation. Defaults to torch.get_default_device().