MLResearchAtOSRAM / tmm_fast

tmm_fast is a lightweight package to speed up optical planar multilayer thin-film device computation. Developed by Alexander Luce (@Nerrror) in cooperation with Heribert Wankerl (@HarryTheBird).
MIT License
53 stars 22 forks source link

Calculation result different from tmm package #23

Closed basimon closed 6 months ago

basimon commented 10 months ago

Hi,

I would like to use tmm_fast for reflection calculation an optimisation expecially in a context like example 6 in tmm examples.

I took the example from the original tmm package and tried to reimplement the same calculation in tmm_fast. The outputs do not match.

import numpy as np
import matplotlib.pyplot as plt
import torch
## tmm

import tmm as tmm
"""
An example reflection plot with a surface plasmon resonance (SPR) dip.
Compare with http://doi.org/10.2320/matertrans.M2010003 ("Spectral and
Angular Responses of Surface Plasmon Resonance Based on the Kretschmann
Prism Configuration") Fig 6a
"""
# list of layer thicknesses in nm
d_list = [np.inf, 5, 30, np.inf]
# list of refractive indices
n_list = [1.517, 3.719+4.362j, 0.130+3.162j, 1]
# wavelength in nm
lam_vac = 633
# list of angles to plot
theta_list = np.linspace(30*np.pi/180, 60*np.pi/180, num=300)
# initialize lists of y-values to plot
Rp = []
for theta in theta_list:
    Rp.append(tmm.coh_tmm('p', n_list, d_list, theta, lam_vac)['R'])

## tmm_fast
from tmm_fast import coh_tmm

pol="p"
n_list= torch.tensor(n_list, dtype=torch.complex64)
d_list= np.array(d_list)

N = torch.zeros(1,4,1, dtype=torch.complex128)
N[0,:,0]= n_list

T=torch.Tensor(1,4)
T[0,:] = torch.Tensor(d_list)

theta = np.linspace(30*np.pi/180, 60*np.pi/180, num=300)
lambda_vacuum = torch.Tensor([633])
theta_incidence = torch.Tensor(theta)

tm = coh_tmm(pol,N,T,theta_incidence,lambda_vacuum)    

fig,ax = plt.subplots(1,2,figsize=(8,4))
ax[0].plot(theta_list/np.pi*180, Rp, 'blue')
ax[1].plot(theta_list/np.pi*180, np.array(tm['R']).reshape(-1,1), 'blue')
for a in ax:
    a.set_xlabel('theta (degree)')
    a.set_ylabel('Fraction reflected')
    a.set_xlim(30, 60)
    a.set_ylim(0, 1)
plt.suptitle('Reflection of p-polarized light with Surface Plasmon Resonance\n'
          'Compare with http://doi.org/10.2320/matertrans.M2010003 Fig 6a')
plt.show()

Which produces the attached output (left original tmm; right tmm_fast; the original tmm output is the expected outcome). tmm_fast_coh_vec_tmm_disp_mstack_result

Am I using the package incorrectly or is there some other issue? tmm_fast is in version 0.2.1. tmm in 0.1.8