The inverse design of nonlocal metasurfaces requires the precise optimization of lattice geometry to engineer spatial dispersion and high-Q resonances. However, gradient-based optimization is frequently bottle-necked by the evaluation of the periodic Dyadic Green's Function (DGF), where traditional Finite Difference (FD) methods suffer from an inherent trade-off between truncation error and numerical instability near spectral singularities. In this work, we present an end-to-end Analytic Gradient Engine for 2D Bravais lattices. By mapping the spectral lattice sums of the Coupled Dipole Approximation (CDA) to the theory of Quasi-Modular Forms (QMF), we derive exact, closed-form expressions for the gradients of the interaction matrix with respect to the modular lattice parameter $τ$. Our framework explicitly handles conditionally convergent terms via regularization and addresses the non-holomorphic outlier $σ_4^{(2)}$ via a hybrid numerical strategy. We further introduce a robust evaluation scheme combining $SL(2, \mathbb{Z})$ domain reduction with automatic error certificates. Experimental validation demonstrates that our engine achieves machine-precision derivatives ($10^{-15}$) and a 6.5$\times$ speedup in optimization convergence compared to finite-difference baselines, enabling the robust design of giant anisotropy in regimes where traditional methods fail.
TorchGDM: A GPU-Accelerated Python Toolkit for Multi-Scale Electromagnetic Scattering with Automatic Differentiation
LAAS-CNRS, Universit\'e de Toulouse, Toulouse, France;CEMES-CNRS, Universit\'e de Toulouse, Toulouse, France;CRHEA-CNRS, Université Côte d’Azur, Sophia Antipolis, France;CNRS, Universit\'e Rennes, DYNACOM (Dynamical Control of Materials Laboratory) - IRL 2015, The University of Tokyo, 7-3-1 Hongo, Tokyo, 113-0033, Japan