def get_maximum_valid_range(self): """find maximum spectral range that spans real and imaginary data. Checks both real and imaginary parts of spectral data and finds the maximum spectral range which is valid for both parts. Returns ------- 2x1 np.array the maximum valid range """ if not (self.data['name'] == 'nk' or self.data['name'] == 'eps'): raise RuntimeError("valid_range cannot be defined as " + "Material does not yet contain " + " a valid n/k or permittivity spectrum") if self.data['complex'] is None: real_range_std = self.data['real'].valid_range.standard_rep imag_range_std = self.data['imag'].valid_range.standard_rep real_lower = np.min(real_range_std) real_upper = np.max(real_range_std) imag_lower = np.min(imag_range_std) imag_upper = np.max(imag_range_std) lower = np.max([real_lower, imag_lower]) upper = np.min([real_upper, imag_upper]) else: lower = np.min(self.data['complex'].valid_range.values) upper = np.max(self.data['complex'].valid_range.values) max_range = np.array([lower, upper]) spec = Spectrum(max_range) return spec.convert_to(self.defaults['spectrum_type'], self.defaults['unit'])
def test_interpolation(): data = np.array([[500, 1.2], [800, 1.4]]) spec_data = Interpolation(data, unit='nanometer') spectrum = Spectrum(500e-9) assert np.isclose(spec_data.evaluate(spectrum), 1.2) spectrum = Spectrum(800e-9) assert np.isclose(spec_data.evaluate(spectrum), 1.4)
def test_contains(): spectrum1 = Spectrum([400., 800.], spectrum_type='wavelength', unit='nanometer') spectrum2 = Spectrum([500., 700.], spectrum_type='wavelength', unit='nanometer') assert spectrum1.contains(spectrum2)
def test_from_wavelength(): spectrum1 = Spectrum([500, 800], spectrum_type='wavelength', unit='nanometer') assert np.isclose(spectrum1.standard_rep[0], 5.e-7) assert np.isclose(spectrum1.standard_rep[1], 8.e-7) spectrum2 = Spectrum([0.5, 0.8], spectrum_type='wavelength', unit='micrometre') assert np.isclose(spectrum1.standard_rep[0], 5.e-7) assert np.isclose(spectrum1.standard_rep[1], 8.e-7)
def make_anti_reflex_layers(keys): mat1 = keys['layer_order'][keys['AntiReflex_at']] mat2 = keys['layer_order'][keys['AntiReflex_at'] + 1] nLayers = keys['AntiReflex_n_layers'] total_thickness = keys['height_AntiReflex'] spectrum = Spectrum(keys['vacuum_wavelength'], unit='m') nk1 = np.real(keys['mat_' + mat1].get_nk_data(spectrum)) nk2 = np.real(keys['mat_' + mat2].get_nk_data(spectrum)) max_id = 0 for key in keys['Domains']: if keys['Domains'][key] > max_id: max_id = keys['Domains'][key] max_id += 1 for ilayer, layer in enumerate(keys['layer_order']): if layer == mat1: start_layer = ilayer for step in range(1, nLayers + 1): n = nk1 + (nk2 - nk1) * (step) / (nLayers + 1) new_data = np.vstack([spectrum.values, n]).T mat_name = 'mat_AntiReflex_{}'.format(step) keys[mat_name] = Material(tabulated_n=new_data, unit='m') t = total_thickness / nLayers height_name = 'height_AntiReflex_{}'.format(step) keys[height_name] = t layer_name = "AntiReflex_{}".format(step) keys['Domains'][layer_name] = max_id + step keys['layer_order'].insert(start_layer + step, layer_name) return keys
def _prepare_plot_data(self, **kwargs): """internal function to prepare data for plotting""" plot_data = {} if 'spectrum_type' not in kwargs: plot_data['spectrum_type'] = self.defaults['spectrum_type'] else: plot_data['spectrum_type'] = kwargs['spectrum_type'] if 'unit' not in kwargs: plot_data['unit'] = self.defaults['unit'] else: plot_data['unit'] = kwargs['unit'] if 'values' not in kwargs: spectrum = self.get_sample_spectrum() values = spectrum.convert_to(plot_data['spectrum_type'], plot_data['unit']) else: values = kwargs['values'] if isinstance(values, (list, tuple)): values = np.array(values) spectrum = Spectrum(values, spectrum_type=plot_data['spectrum_type'], unit=plot_data['unit']) plot_data['spectrum'] = spectrum return plot_data
def get_sample_spectrum(self): """spectrum which covers the maximum valid range of the material data""" max_range = self.get_maximum_valid_range() if max_range[0] == 0.0 or max_range[1] == np.inf: values = np.geomspace(100, 2000, 1000) spectrum = Spectrum(values, spectrum_type='wavelength', unit='nm') else: values = np.geomspace(max_range[0], max_range[1], 1000) if values[0] < max_range[0]: values[0] = max_range[0] if values[-1] > max_range[1]: values[-1] = max_range[1] spectrum = Spectrum(values, spectrum_type=self.defaults['spectrum_type'], unit=self.defaults['unit']) return spectrum
def test_sellmeier(): model_parameters = [ 0, 0.6961663, 0.0684043, 0.4079426, 0.1162414, 0.8974794, 9.896161 ] spec_data = Sellmeier(model_parameters, valid_range=[0.21, 6.7], unit='um') spectrum = Spectrum(0.5876e-6) assert np.isclose(spec_data.evaluate(spectrum), 1.4585, atol=1e-3)
def test_drude(): model_parameters = [8.55, 18.4e-3] spec_data = Drude(model_parameters, valid_range=[0.0, np.inf], unit='ev', spectrum_type='energy') spectrum = Spectrum(0.5876e-6) spec_data.evaluate(spectrum)
def get_permittivity(self, spectrum_values, spectrum_type='wavelength', unit='meter'): ''' return complex permittivity for a given input spectrum. Parameters ---------- spectrum: np.array or Spectrum the spectral values to evaluate spectrum_type: str {'wavelength', 'frequency', 'energy'} type of spectrum unit: str {'meter', 'nanometer', 'micrometer', 'hertz', 'electronvolt'} unit of spectrum (must match spectrum type) Returns ------- np.complex128 the complex permittivity values (if input spectrum has size == 1) np.array with np.complex128 dtype the complex permittivity values (if input spectrum has size > 1) ''' if isinstance(spectrum_values, Spectrum): spectrum = spectrum_values else: spectrum = Spectrum(spectrum_values, spectrum_type=spectrum_type, unit=unit) if not (self.data['name'] == 'nk' or self.data['name'] == 'eps'): raise ValueError("data type {}".format(self.data['name']) + "cannot be converted to refractive index") if self.data['complex'] is None: real = self.data['real'].evaluate(spectrum) imag = 1j * self.data['imag'].evaluate(spectrum) complex_val = real + imag else: complex_val = self.data['complex'].evaluate(spectrum) if self.data['name'] == 'nk': complex_val = np.power(complex_val, 2) return complex_val
def test_from_energy(): spectrum = Spectrum([500, 800], spectrum_type='wavelength', unit='nanometer') vals = spectrum.convert_to('energy', 'ev')
def test_standardise(): spectrum = Spectrum([0.5, 1.0]) assert spectrum.standard_rep[0] == 0.5 assert spectrum.standard_rep[1] == 1.0
def test_from_energy(): spectrum = Spectrum([1.0, 2.0], spectrum_type='energy', unit='ev') assert np.isclose(spectrum.standard_rep[0], 1.23984198e-06) assert np.isclose(spectrum.standard_rep[1], 6.19920992e-07)
def test_from_frequency(): spectrum = Spectrum([1e15, 1e16], spectrum_type='frequency', unit='hz') assert np.isclose(spectrum.standard_rep[0], 2.99792458e-07) assert np.isclose(spectrum.standard_rep[1], 2.99792458e-08)
def test_from_ang_frequency(): spectrum = Spectrum([2e15, 3e16], spectrum_type='angularfrequency', unit='1/s') assert np.isclose(spectrum.standard_rep[0], 9.41825784e-07) assert np.isclose(spectrum.standard_rep[1], 6.27883856e-08)
def test_to_wavenumber(): spectrum = Spectrum([500, 800], spectrum_type='wavelength', unit='nanometer') vals = spectrum.convert_to('wavenumber', '1/cm')
def test_scalar_init(): spectrum = Spectrum(1.3)
def test_constant_init(): spec_data = Constant(1.2) spectrum = Spectrum([500e-9]) assert np.isclose(spec_data.evaluate(spectrum), 1.2)
def test_from_wavenumber(): spectrum = Spectrum([12500., 25000.], spectrum_type='wavenumber', unit='1/cm') assert np.isclose(spectrum.standard_rep[0], 5.02654825e-06) assert np.isclose(spectrum.standard_rep[1], 2.51327412e-06)
def test_to_frequency(): spectrum = Spectrum([500, 800], spectrum_type='wavelength', unit='nanometer') vals = spectrum.convert_to('frequency', 'hz')
def test_to_angfrequency(): spectrum = Spectrum([500, 800], spectrum_type='wavelength', unit='nanometer') vals = spectrum.convert_to('angularfrequency', '1/s')
import pytest import numpy as np import os from dispersion import Material from dispersion import Spectrum from dispersion import get_config spectrum = Spectrum(0.5, unit='um') root_path = "../data" def test_mat_init(): md = Material(fixed_n=1.0) n = md.get_nk_data(spectrum) assert np.isclose(np.real(n), 1.0) assert np.isclose(np.imag(n), 0.0) def test_from_yml_file(): relpath = os.path.join('RefractiveIndexInfo', 'data', 'main', 'Ag', 'Hagemann.yml') filepath = os.path.join(root_path, relpath) md = Material(file_path=filepath, spectrum_type='wavelength', unit='micrometer') n = md.get_nk_data(spectrum) assert np.isclose(np.real(n), 0.23805806451612901) assert np.isclose(np.imag(n), 3.126040322580645)
def test_list_init(): spectrum = Spectrum([0.5, 1.0])