[fdtd.phasor] add accumulate_phasor*

This commit is contained in:
Jan Petykiewicz 2026-04-18 12:10:14 -07:00
commit d99ef96c96
5 changed files with 708 additions and 7 deletions

View file

@ -151,7 +151,7 @@ def main():
# Source parameters and function
source_phasor, _delay = gaussian_packet(wl=wl, dwl=100, dt=dt, turn_on=1e-5)
source_phasor, delay = gaussian_packet(wl=wl, dwl=100, dt=dt, turn_on=1e-5)
aa, cc, ss = source_phasor(numpy.arange(max_t))
srca_real = aa * cc
src_maxt = numpy.argwhere(numpy.diff(aa < 1e-5))[-1]
@ -160,7 +160,8 @@ def main():
Jph = numpy.zeros_like(epsilon, dtype=complex)
Jph[1, *(grid.shape // 2)] = epsilon[1, *(grid.shape // 2)]
Eph = numpy.zeros_like(Jph)
omega = 2 * numpy.pi / wl
eph = numpy.zeros((1, *epsilon.shape), dtype=complex)
# #### Run a bunch of iterations ####
output_file = h5py.File('simulation_output.h5', 'w')
@ -169,6 +170,7 @@ def main():
update_E(ee, hh, epsilon)
if tt < src_maxt:
# This codebase uses E -= dt * J / epsilon for electric-current injection.
ee[1, *(grid.shape // 2)] -= srca_real[tt]
update_H(ee, hh)
@ -185,17 +187,26 @@ def main():
print(f'saving E-field at iteration {tt}')
output_file[f'/E_t{tt}'] = ee[:, :, :, ee.shape[3] // 2]
Eph += (cc[tt] - 1j * ss[tt]) * phasor_norm * ee
fdtd.accumulate_phasor(
eph,
omega,
dt,
ee,
tt,
# The pulse is delayed relative to t=0, so the readout needs the same phase shift.
offset_steps=0.5 - delay / dt,
# accumulate_phasor() already includes dt, so undo the dt in phasor_norm here.
weight=phasor_norm / dt,
)
omega = 2 * numpy.pi / wl
Eph *= numpy.exp(-1j * dt / 2 * omega)
Eph = eph[0]
b = -1j * omega * Jph
dxes_fdfd = copy.deepcopy(dxes)
for pp in (-1, +1):
for dd in range(3):
stretch_with_scpml(dxes_fdfd, axis=dd, polarity=pp, omega=omega, epsilon_effective=n_air ** 2, thickness=pml_thickness)
A = e_full(omega=omega, dxes=dxes, epsilon=epsilon)
residual = norm(A @ vec(ee) - vec(b)) / norm(vec(b))
A = e_full(omega=omega, dxes=dxes_fdfd, epsilon=epsilon)
residual = norm(A @ vec(Eph) - vec(b)) / norm(vec(b))
print(f'FDFD residual is {residual}')