RCWA4D is an electromagnetic solver for layered structures whose layers have incommensurate periodicities, such as twisted bilayer (moiré) photonic crystal slabs. It extends rigorous coupled-wave analysis (RCWA) to compute transmission, reflection, diffraction efficiencies, and internal fields of such quasi-periodic stacks.
- Multilayer stacks where each layer follows one of two lattices rotated by an arbitrary twist angle
- Standard RCWA for untwisted stacks, and an extended ("4D") plane-wave basis for twisted ones
- Arbitrary 2D patterns: permittivity (and permeability) given as a real-space array per unit cell
- Total and per-order reflection/transmission for any incident angle and polarization
- Fourier coefficients of fields inside the stack and in the reflection and transmission regions
- Uniform superstrate and substrate media
- Pure NumPy/SciPy
pip install git+https://github.com/fancompute/rcwa4d.gitFor development (editable install with test dependencies):
git clone https://github.com/fancompute/rcwa4d.git
cd rcwa4d
pip install -e ".[dev]"
pytestimport numpy as np
from rcwa4d import RCWA
# One unit cell: dielectric slab (eps = 4) with a circular air hole of radius 0.25a
n = 256
x, y = np.meshgrid(np.linspace(-0.5, 0.5, n), np.linspace(-0.5, 0.5, n))
eps = np.where(x**2 + y**2 < 0.25**2, 1.0, 4.0)
# Two identical slabs separated by an air gap, the bottom one twisted by 5 degrees
sim = RCWA(
epsr_list=[eps, None, eps], # None marks a uniform air gap
thickness_list=[0.2, 0.1, 0.2], # in units of the lattice constant a
orientation_list=[1, 1, 2], # which lattice each layer follows (2 = twisted)
twist=np.deg2rad(5), # rotation of lattice 2
N=1, # Fourier orders -N..N along x
M=1, # Fourier orders -M..M along y
)
sim.set_freq_k(0.75, theta_phi=(0, 0)) # frequency in c/a, normal incidence
(R, T), (r0, t0) = sim.get_RT(pte=0, ptm=1) # TM-polarized plane wave
print(f"R = {R:.4f}, T = {T:.4f}")Units and conventions. Lengths are in units of the lattice constant a and frequencies
in units of c/a. Light comes in from the reflection half-space (e_r, m_r, vacuum by default),
passes through the layers in order, and exits into the transmission half-space (e_t, m_t).
Angles are in radians.
Convergence. The twisted solver uses [(2N+1)(2M+1)]² plane waves, so cost grows quickly
with N and M. Always check that your results converge as you increase the truncation
(see example 3).
RCWA(epsr_list, thickness_list, orientation_list, twist=..., N=..., M=..., e_r=..., e_t=...) |
Define the stack |
sim.set_freq_k(freq, theta_phi=(theta, phi)) or kxy_inc=(kx, ky) |
Set frequency and incidence |
sim.get_RT(pte, ptm) |
Total (R, T) and 0th-order complex amplitudes; per-order efficiencies in sim.R_orders, sim.T_orders |
sim.get_internal_field(layers, offsets) |
Field Fourier coefficients inside the stack (needs get_RT(..., storing_intermediate_Smats=True)) |
sim.get_RT_field() |
Field Fourier coefficients of the reflected and transmitted waves |
sim.Sg |
Global scattering matrix (dict with S11, S12, S21, S22) |
The examples/ folder contains Jupyter notebooks (pip install -e ".[examples]"):
| Notebook | Content |
|---|---|
example0-band-structure |
Transmission vs. frequency, in-plane wavevector, and twist angle |
example1-scattering-matrix |
Scattering matrices of single-layer and twisted bilayer photonic crystals |
example2-phase-inference |
Inferring the relative phase of two beams from twist-dependent intensity |
example3-convergence-check |
Convergence with respect to Fourier truncation |
example4-redundant-periods |
Sanity check using supercells with repeated periods |
If you use RCWA4D in your research, please cite the companion paper:
B. Lou and S. Fan, "RCWA4D: Electromagnetic solver for layered structures with incommensurate periodicities," Computer Physics Communications 306, 109356 (2025). doi:10.1016/j.cpc.2024.109356
BibTeX entries for this and all papers below are collected at the end of this section.
-
RCWA4D: Electromagnetic solver for layered structures with incommensurate periodicities
B. Lou and S. Fan
Computer Physics Communications 306, 109356 (2025) · doi:10.1016/j.cpc.2024.109356
The companion paper for this repository. -
Theory for twisted bilayer photonic crystal slabs
B. Lou, N. Zhao, M. Minkov, C. Guo, M. Orenstein, and S. Fan
Physical Review Letters 126, 136101 (2021) · doi:10.1103/PhysRevLett.126.136101
The first paper on the algorithm.
-
Tunable guided resonance in twisted bilayer photonic crystal
B. Lou, B. Wang, J. A. Rodríguez, M. Cappelli, and S. Fan
Science Advances 8, eadd4339 (2022) · doi:10.1126/sciadv.add4339
First experimental verification, at microwave frequencies. -
Experimental probe of twist angle–dependent band structure of on-chip optical bilayer photonic crystal
H. Tang, B. Lou, F. Du, M. Zhang, X. Ni, W. Xu, R. Jin, S. Fan, and E. Mazur
Science Advances 9, eadh8498 (2023) · doi:10.1126/sciadv.adh8498
First experimental verification at optical frequencies, with comprehensive comparisons.
-
Tunable frequency filter based on twisted bilayer photonic crystal slabs
B. Lou and S. Fan
ACS Photonics 9, 800–805 (2022) · doi:10.1021/acsphotonics.1c01263 -
Wide wavelength-tunable narrow-band thermal radiation from moiré patterns
C. Guo, Y. Guo, B. Lou, and S. Fan
Applied Physics Letters 118, 131111 (2021) · doi:10.1063/5.0047308 -
Three-dimensional reconfigurable optical singularities in bilayer photonic crystals
X. Ni, Y. Liu, B. Lou, M. Zhang, E. L. Hu, S. Fan, E. Mazur, and H. Tang
Physical Review Letters 132, 073804 (2024) · doi:10.1103/PhysRevLett.132.073804 -
On-chip multidimensional dynamic control of twisted moiré photonic crystal for smart sensing and imaging
H. Tang, B. Lou, F. Du, G. Gao, M. Zhang, X. Ni, E. Hu, A. Yacoby, Y. Cao, S. Fan, et al.
arXiv preprint (2023) · arXiv:2312.09089
@article{lou2025rcwa4d,
title = {{RCWA4D}: Electromagnetic solver for layered structures with incommensurate periodicities},
author = {Lou, Beicheng and Fan, Shanhui},
journal = {Computer Physics Communications},
volume = {306},
pages = {109356},
year = {2025},
doi = {10.1016/j.cpc.2024.109356}
}
@article{lou2021theory,
title = {Theory for Twisted Bilayer Photonic Crystal Slabs},
author = {Lou, Beicheng and Zhao, Nathan and Minkov, Momchil and Guo, Cheng and Orenstein, Meir and Fan, Shanhui},
journal = {Physical Review Letters},
volume = {126},
number = {13},
pages = {136101},
year = {2021},
doi = {10.1103/PhysRevLett.126.136101}
}
@article{lou2022tunable,
title = {Tunable guided resonance in twisted bilayer photonic crystal},
author = {Lou, Beicheng and Wang, Benjamin and Rodr{\'\i}guez, Jesse A. and Cappelli, Mark and Fan, Shanhui},
journal = {Science Advances},
volume = {8},
number = {48},
pages = {eadd4339},
year = {2022},
doi = {10.1126/sciadv.add4339}
}
@article{tang2023experimental,
title = {Experimental probe of twist angle--dependent band structure of on-chip optical bilayer photonic crystal},
author = {Tang, Haoning and Lou, Beicheng and Du, Fan and Zhang, Mingjie and Ni, Xueqi and Xu, Weijie and Jin, Rebekah and Fan, Shanhui and Mazur, Eric},
journal = {Science Advances},
volume = {9},
number = {28},
pages = {eadh8498},
year = {2023},
doi = {10.1126/sciadv.adh8498}
}
@article{lou2022filter,
title = {Tunable Frequency Filter Based on Twisted Bilayer Photonic Crystal Slabs},
author = {Lou, Beicheng and Fan, Shanhui},
journal = {ACS Photonics},
volume = {9},
number = {3},
pages = {800--805},
year = {2022},
doi = {10.1021/acsphotonics.1c01263}
}
@article{guo2021wide,
title = {Wide wavelength-tunable narrow-band thermal radiation from moir{\'e} patterns},
author = {Guo, Cheng and Guo, Yu and Lou, Beicheng and Fan, Shanhui},
journal = {Applied Physics Letters},
volume = {118},
number = {13},
pages = {131111},
year = {2021},
doi = {10.1063/5.0047308}
}
@article{ni2024three,
title = {Three-Dimensional Reconfigurable Optical Singularities in Bilayer Photonic Crystals},
author = {Ni, Xueqi and Liu, Yuan and Lou, Beicheng and Zhang, Mingjie and Hu, Evelyn L. and Fan, Shanhui and Mazur, Eric and Tang, Haoning},
journal = {Physical Review Letters},
volume = {132},
number = {7},
pages = {073804},
year = {2024},
doi = {10.1103/PhysRevLett.132.073804}
}
@article{tang2023chip,
title = {On-Chip Multidimensional Dynamic Control of Twisted Moir{\'e} Photonic Crystal for Smart Sensing and Imaging},
author = {Tang, Haoning and Lou, Beicheng and Du, Fan and Gao, Guangqi and Zhang, Mingjie and Ni, Xueqi and Hu, Evelyn and Yacoby, Amir and Cao, Yuan and Fan, Shanhui and others},
journal = {arXiv preprint arXiv:2312.09089},
year = {2023}
}Bug reports and feature requests are welcome on the issue tracker. For collaborations and other inquiries, contact Beicheng Lou at beichenglou@stanford.edu.
MIT © 2024 Fan Group
