calculate_gradient_fields#
- BaseFom.calculate_gradient_fields(fdtd_session: FdtdSession, parametrization, symmetry_factors: list = None) ndarray#
Compute the gradient of the FOM with respect to permittivity using adjoint fields.
The implementation runs in two phases:
Forward-state phase: with the forward
.fsploaded, every entry’s scaling factor, forward fields, and any subclass-specific forward-side aux data are gathered (_collect_fwd_side_aux()). All entries share the same forward project, so this typically runs a singlefdtd.loadeven for multi-port FOMs.Adjoint-state phase: for every entry,
_compute_adjoint_fields_phased()loads that entry’s adjoint.fspand assembles the adjoint fields (consuming the per-entry aux gathered in phase 1). The per-wavelength gradient accumulation is then done in pure NumPy.
Keeping all forward-state reads in phase 1 and all adjoint-state reads in phase 2 makes the file-load pattern
fwd → adj_1 → adj_2 → ...rather thanfwd → adj_1 → fwd → adj_2 → .... Eachfdtd.loadcosts ~700 ms of deserialization, so avoiding the back-and-forth is a significant speed-up for multi-port FOMs. Combined with thegetresultcache onFdtdSession, repeat forward reads across entries become cache hits.- Parameters:
- fdtd_session
FdtdSession FDTD session providing forward and adjoint field data.
- parametrization
BaseParametrization Current geometry parametrization used to compute the scaling factor.
- symmetry_factors
listofint,optional Per-monitor symmetry factors (from
fom_symmetry_factors). When provided, the Jacobian of the FOM is divided by each monitor’s factor before accumulating gradient contributions, so that monitors placed entirely within the symmetric part of the simulation region are weighted correctly.
- fdtd_session
- Returns:
np.ndarrayReal-valued gradient field array of shape
(nx, ny, nz, 3)summed over all simulation results and wavelengths.