When using automatic differentiation, there is a subtle issue with automatically differentiating objective functions with respect to a variable which appears in conditional expressions.
For example, differentiating an objective function |Ey|(z) which calculates the y-component of the electric field as a function of z-position for a fixed simulation (layers, lattice, permittivity, etc.). At the z values of the layer boundaries, the derivative might not be well-defined due to inkstone conditionally selecting the right-most (larger z) layer as the layer in which the boundary z belongs. Since autodiff libraries don't know about the control flow (they only trace inside the if statements), they will still output a gradient without throwing errors, even though the gradient is ill-defined. Away from the layer boundaries, autodiff can differentiate through the control flow and the z-derivatives are consistent with finite difference.
To fix, some smoothing function could be used instead of the z-value-layer check in GetFieldsListPoints.
When using automatic differentiation, there is a subtle issue with automatically differentiating objective functions with respect to a variable which appears in conditional expressions.
For example, differentiating an objective function |Ey|(z) which calculates the y-component of the electric field as a function of z-position for a fixed simulation (layers, lattice, permittivity, etc.). At the z values of the layer boundaries, the derivative might not be well-defined due to inkstone conditionally selecting the right-most (larger z) layer as the layer in which the boundary z belongs. Since autodiff libraries don't know about the control flow (they only trace inside the
ifstatements), they will still output a gradient without throwing errors, even though the gradient is ill-defined. Away from the layer boundaries, autodiff can differentiate through the control flow and the z-derivatives are consistent with finite difference.To fix, some smoothing function could be used instead of the z-value-layer check in
GetFieldsListPoints.