3.5. wavesim.utilities
- add_absorption(m, boundary_widths, n_roi, n_dims)[source]
Add (weighted) absorption to the permittivity (refractive index squared)
- Parameters:
m – array (permittivity)
boundary_widths – Boundary widths
n_roi – Number of points in the region of interest
n_dims – Number of dimensions
- Returns:
m with absorption
- analytical_solution(n_size0, pixel_size, wavelength=None)[source]
Compute analytic solution for 1D case
- boundary_(x)[source]
Anti-reflection boundary layer (ARL). Linear window function
- Parameters:
x – Size of the ARL
- class chain
Bases:
objectchain(*iterables) –> chain object
Return a chain object whose .__next__() method returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.
- classmethod from_iterable(iterable, /)
Alternative chain() constructor taking a single iterable argument that evaluates lazily.
- check_input_dims(x)[source]
Expand arrays to 3 dimensions (e.g. permittivity (refractive index²) or source)
- Parameters:
x – Input array
- Returns:
x with 3 dimensions
- check_input_len(x, e, n_dims)[source]
Check the length of input arrays and expand them to 3 elements if necessary. Either repeat or add ‘e’
- Parameters:
x – Input array
e – Element to add
n_dims – Number of dimensions
- Returns:
Array with 3 elements
- combine(domains, device='cpu')[source]
Concatenates a 3-d array of 3-d tensors
- full_matrix(operator)[source]
Converts operator to a 2D square matrix of size np.prod(d) x np.prod(d)
- Parameters:
operator – Operator to convert to a matrix. This function must be able to accept a 0 scalar, and return a vector of the size and data type of the domain.
- get_dims(x)[source]
Get the number of dimensions of ‘x’
- Parameters:
x – Input array
- Returns:
Number of dimensions
- is_zero(x)[source]
Check if x is zero
Some functions allow specifying 0 or 0.0 instead of a torch tensor, to indicate that the array should be cleared. This function returns True if x is a scalar 0 or 0.0. It raises an error if x is a scalar that is not equal to 0 or 0.0, and returns False otherwise.
- list_to_array(input, depth)[source]
Convert a nested list of depth depth to a numpy object array
- max_abs_error(e, e_true)[source]
(Normalized) Maximum Absolute Error (MAE) ||e-e_true||_{inf} / ||e_true||
- Parameters:
e – Computed field
e_true – True field
- Returns:
(Normalized) MAE
- max_relative_error(e, e_true)[source]
Computes the maximum error, normalized by the rms of the true field
- Parameters:
e – Computed field
e_true – True field
- Returns:
(Normalized) Maximum Relative Error
- normalize(x, min_val=None, max_val=None, a=0, b=1)[source]
Normalize x to the range [a, b]
- Parameters:
x – Input array
min_val – Minimum value (of x)
max_val – Maximum value (of x)
a – Lower bound for normalization
b – Upper bound for normalization
- Returns:
Normalized x
- pad_boundaries(x, boundary_widths, boundary_post=None, mode='constant')[source]
Pad ‘x’ with boundaries in all dimensions using numpy pad (if x is np.ndarray) or PyTorch nn.functional.pad (if x is torch.Tensor). If boundary_post is specified separately, pad with boundary_widths (before) and boundary_post (after).
- Parameters:
x – Input array
boundary_widths – Boundary widths for padding before and after (or just before if boundary_post not None)
boundary_post – Boundary widths for padding after
mode – Padding mode
- Returns:
Padded array
- partition(array, n_domains)[source]
Split a 3-D array into a 3-D set of sub-arrays of approximately equal sizes.
- preprocess(permittivity, boundary_widths=10)[source]
Preprocess the input parameters for the simulation. Add absorption and boundaries to the permittivity (refractive index²), and return the preprocessed permittivity and boundaries in the format (ax0, ax1, ax2).
- Parameters:
permittivity – Refractive index²
boundary_widths – Boundary widths (in pixels)
- Returns:
Preprocessed permittivity (refractive index²) with boundaries and absorption
- relative_error(e, e_true)[source]
Relative error
⟨|e-e_true|^2⟩ / ⟨|e_true|^2⟩- Parameters:
e – Computed field
e_true – True field
- Returns:
Relative Error
- squeeze_(x)[source]
Squeeze the last dimension of ‘x’ if it is 1
- Parameters:
x – Input array
- Returns:
Squeezed array