TORCWA (torch + rcwa) is a PyTorch implementation of rigorous coupled-wave analysis (RCWA)
- GPU-accelerated simulation
- Supporting automatic differentiation for optimization
- Units: Lorentz-Heaviside units
- Speed of light: 1
- Permittivity and permeability of vacuum: both 1
- Notation: exp(-jωt)
Ensure you have the following installed:
- Python 3.7 or higher
- Required libraries (listed in
requirements.txt)
Clone the repository:
git clone https://github.com/Alexin-CH/TORCWA.git
cd TORCWAInstall the required dependencies:
makeImport the package and build a simulation. The setup order is:
- Create the
rcwasimulation object (frequency, Fourier orders, lattice constants) - Optionally add input/output layers
- Set the incident angle
- Add internal layers
- Set the incident field (plane wave or Fourier amplitudes)
- Solve the global S-matrix
- Query S-parameters or field distributions
import torch
import torcwa
# Wavelength 800 nm, 3x3 diffraction orders, 1000 nm lattice
sim = torcwa.rcwa(
freq=1.0 / 800.0,
order=(1, 1),
lattice=(1000.0, 1000.0),
)
sim.add_input_layer(eps=1.0) # free-space input
sim.add_output_layer(eps=1.0) # free-space output
sim.set_incident_angle(inc_ang=30.0 * torch.pi / 180, azi_ang=0.0)
# Homogeneous slab: thickness 100 nm, eps = 2.25 (n = 1.5)
sim.add_layer(thickness=100.0, eps=2.25, mu=1.0)
# Incident plane wave (x-polarized, forward direction)
sim.source_planewave(amplitude=[1.0, 0.0], direction="f")
# Solve the global S-matrix
sim.solve()
# Power-normalized S-parameters for the (0, 0) order
R = sim.s_parameters(orders=[0, 0], direction="forward",
port="reflection", polarization="xx")
T = sim.s_parameters(orders=[0, 0], direction="forward",
port="transmission", polarization="xx")
print(f"R = {R.item():.4f}, T = {T.item():.4f}")
# Field distribution in the XY plane inside the first layer (z = 0)
x = torch.linspace(-500.0, 500.0, 64)
y = torch.linspace(-500.0, 500.0, 64)
(Ex, Ey, Ez), (Hx, Hy, Hz) = sim.field_xy(0, x, y, z_prop=0.0)Supported polarizations for s_parameters: xy-notation 'xx', 'yx', 'xy',
'yy' and ps-notation 'pp', 'sp', 'ps', 'ss'.
To evaluate the compute time as a function of the number of orders, run torcwa/eval_orders.py as a script:
python torcwa/eval_orders.py