nidn package

Subpackages

Module contents

class nidn.LayerBuilder(run_cfg)

Bases: object

build_circle_layer(grid_material, circle_material, radius=0.33, x_center=0.5, y_center=0.5)

Generates a square layer with a circular hole in the middle.

Parameters
  • grid_material (str) – Name of the grid material.

  • circle_material (str) – Name of the hole material.

  • a (float, optional) – Radius of the circle (0 to 0.5). Defaults to 0.33.

  • x_center (float, optional) – Center of the circle in the x-direction. Defaults to 0.5.

  • y_center (float, optional) – Center of the circle in the y-direction. Defaults to 0.5.

Returns

Tensor with epsilon values for the layer

Return type

torch.tensor

build_squared_layer(grid_material, square_material, a=0.33)

Generates a square layer with a square hole in the middle.

Parameters
  • grid_material (str) – Name of the grid material.

  • square_material (str) – Name of the hole material.

  • a (float, optional) – Size of the square hole (0 to 0.5). Defaults to 0.33.

Returns

Tensor with epsilon values for the layer.

Return type

torch.tensor

build_uniform_layer(grid_material)

Generates a uniform layer.

Parameters

grid_material (str) – The name of the material.

Returns

Tensor with epsilon values for the layer

Return type

torch.tensor

class nidn.MaterialCollection(target_frequencies, to_skip=None)

Bases: object

Database of materials included with NIDN

N_materials = 0
epsilon_matrix = None
material_names = []
nidn.compute_spectrum(eps_grid, cfg, validate_cfg=True)

Computes spectrum, using either TRCWA or FDTD simulations

Parameters
  • cfg (DotMap) – Configurations for the simulation

  • permittivity (Array[Nx][Ny][Nlayers][Nfrequencies]) – Matrix of permittivity tensors

  • validate_cfg (bool) – Whether to validate the config

Returns

Reflection spectrumand transmission spectrum

Return type

Array, Array

nidn.compute_target_frequencies(min_physical_wl: float, max_physical_wl: float, N_freq: int, distribution: str)

Computes the target frequencies for a given set of physical frequencies.

Parameters
  • min_physical_wl (float) – Minimum physical frequency in the target spectrum.

  • max_physical_wl (float) – Maximum physical frequency in the target spectrum.

  • N_freq (int) – The number of target frequencies.

  • distribution (str) – The distribution of the target frequencies. Either linear or log.

Returns

Corresponding target frequencies

Return type

np.array

nidn.fix_random_seeds(seed: int = 42)

This function sets the random seeds in torch and numpy to enable reproducible behavior.

Parameters

seed (int) – Seed to use.

nidn.freq_to_wl(freq)

Gives the wavelength in units of the lattice constants from the unitless frequency and vice versa. In TRCWA the magnitude is set to micrometres (um).

Parameters

freq (float) – The unitless frequency, or the wavelength in units of TRCWA_UNIT_MAGNITUDE.

Returns

The wavelength with units of TRCWA_UNIT_MAGNITUDE, or the unitless frequency.

Return type

float

nidn.get_frequency_points(run_cfg: DotMap)

Computes both the physical and normalized frequency points which will be used. This can be helpful to determine the points at which you need to define the target spectrum.

Parameters

run_cfg (DotMap) – Config for the run.

Returns

List of physical frequencies. target_frequencies (list): List of target frequencies.

Return type

physical_target_frequencies (list)

nidn.load_default_cfg()

Loads the default toml config file from the cfg folder.

nidn.load_run(filename)

Loads the saved DotMap run config.

Parameters

filename (str) – Path to file.

Returns

Run config

Return type

DotMap

nidn.model_to_eps_grid(model, run_cfg: DotMap)

Computes a 4D grid of epsilons for the given model.

Parameters
  • model (torch.model) – Trained neural network model. Should map one 4D input to a [real,imag] epsilon.

  • run_cfg (DotMap) – Configuration for the run.

Returns

Resulting 4D [real,imag] epsilon grid and tensor of the material_ids (None for regression)

Return type

torch.tensor, torch.tensor

nidn.phys_freq_to_phys_wl(freq)

Gives the physical frequency from the physical wavelength and vice versa.

Parameters

input (float) – The physical wavelength in metres or the physical frequency in Hz.

Returns

The physical frequency or the physical wavelength.

Return type

float

nidn.plot_eps_per_point(run_cfg, compare_to_material=None, save_path=None, legend=True, file_id=None)

This function plots the epsilon values of grid points against real materials. Optionally saves it.

Parameters
  • run_cfg (dict) – The run configuration.

  • compare_to_material (str or list) – Name(s) of the material to compare with. Available ones are in /materials/data.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

  • legend (bool, optional) – Whether to show layer legend

  • file_id (str, optional) – Whether to add a postfix string

nidn.plot_epsilon_grid(eps, run_cfg, save_path=None)

Plots the absolute value of the epsilon over all frequencies for each 3-D grid point. Optionally saves it.

Parameters
  • eps (torch.tensor) – The epsilon tensor to plot.

  • run_cfg (dict) – The run configuration.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

nidn.plot_losses(run_cfg, save_path=None)

Plots the loss over training, optionally saving it.

Parameters
  • run_cfg (DotMap) – Run configuration.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

nidn.plot_material_grid(run_cfg, save_path=None, eps=None, plot_error=False, to_skip=None)

Plots the materials closest to the used ones for each grid point. Optionally saves it.

Parameters
  • run_cfg (dict) – The run configuration.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

  • eps (torch.tensor, optional) – The epsilon tensor. Defaults to None, then the epsilon tensor will be computed from the run_cfg.

  • plot_error (bool, optional) – If True, the error will be plotted. Defaults to False.

  • to_skip (int, optional) – Material to skip. Defaults to None.

nidn.plot_model_grid(run_cfg, save_path=None)

Plots the absolute value of the epsilon over all frequencies for each 3-D grid point. Optionally saves it. :param run_cfg: The run configuration. :type run_cfg: dict :param save_path: Folder to save the plot in. Defaults to None, then the plot will not be saved. :type save_path: str, optional

nidn.plot_model_grid_per_freq(run_cfg, freq_idx=[0, 1, 2, 3], save_path=None)

Plots the real and imaginary part of the permittivity in two separate plots for frequencies in target_frequencies defined by freq_idx. Optionally saves it.

Parameters
  • run_cfg (dict) – The run configuration.

  • freq_idx (list of int) – Which of the frequency indices in target_frequencies we want to plot. Defaults to [0, 1, 2, 3].

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

nidn.plot_spectra(run_cfg, save_path=None, prod_R_spectrum=None, prod_T_spectrum=None, markers=True, filename=None, ylim=[[0.0, 1.0], [0.0, 1.0]])

Plots the produced RTA spectra together with the target spectra. Optionally saves it.

Parameters
  • run_cfg (dict) – The run configuration.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

  • prod_R_spectrum (torch.tensor, optional) – The produced reflection spectrum. Defaults to None, then will compute from model.

  • prod_T_spectrum (torch.tensor, optional) – The produced transmission spectrum. Defaults to None, then will compute from model.

  • markers (bool) – Whether to plot markers for the target and produced spectra.

  • filename (str, optional) – Filename to save the plot in. Defaults to None, then the plot will be saved with the name “spectra.png”.

  • ylim (list) – The y-limits of the plot for the two spectra. Defaults to [[0.0, 1.0],[0.0, 1.0]].

nidn.plot_spectrum(run_cfg, R_spectrum, T_spectrum, markers=True, save_path=None, filename=None, show_absorption=False)

Plots the produced RTA spectra. Optionally saves it.

Parameters
  • run_cfg (dict) – The run configuration.

  • R_spectrum (torch.tensor) – The reflection spectrum.

  • T_spectrum (torch.tensor) – The transmission spectrum.

  • markers (bool) – Whether to plot markers for the target and produced spectra.

  • save_path (str, optional) – Folder to save the plot in. Defaults to None, then the plot will not be saved.

  • filename (str, optional) – Filename to save the plot in. Defaults to None, then the plot will be saved with the name “spectrum.png”.

  • show_absorption (bool, optional) – Whether to show the absorption spectrum. Defaults to False.

nidn.print_cfg(cfg: DotMap)

Prints the config in a more readable way.

Parameters

cfg (DotMap) – Config to print.

nidn.run_training(run_cfg: DotMap)

Runs a training run with the passed config.

Parameters

run_cfg (DotMap) – Run configuration.

Returns

The loss results of the training run

Return type

DotMap

nidn.save_all_plots(run_cfg, save_path)

Creates all plots for the passed run_config and save them in the passed folder.

Parameters
  • run_cfg (DotMap) – Run configuration (incl. trained model).

  • save_path (str) – Folder to save the plots to.

nidn.save_run(run_cfg, subfolder='')

Saves results of a run to a file.

Parameters
  • run_cfg (DotMap) – Run config.

  • subfolder (str) – Subfolder to save to. Defaults to “”.

nidn.set_log_level(log_level: str)

Set the log level for the logger.

Parameters

log_level (str) – The log level to set. Options are ‘TRACE’,’DEBUG’, ‘INFO’, ‘SUCCESS’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’.

nidn.wl_to_phys_wl(wl)

Gives the physical wavelength from the wavelength in units of TRCWA_UNIT_MAGNITUDE.

Parameters

wl (float) – The wavelength in units of TRCWA_UNIT_MAGNITUDE.

Returns

The physical wavelength in metres.

Return type

float