Skip to content

FourierPlanarCoil still gives NaN gradient when axis is vertical #2276

Closed

Description

@dario-panici

I thought we had fixed this before #1456 but I guess not?

from desc.coils import FourierPlanarCoil
from desc.objectives import ObjectiveFunction, CoilLength
from desc.optimize import Optimizer

coil = FourierPlanarCoil(normal=[0,0,1])
opt = Optimizer("lsq-exact")

obj = ObjectiveFunction(CoilLength(coil))

# Works fine, no Nan
opt.optimize(coil,objective=obj,verbose=3,maxiter=2);


coil = FourierPlanarCoil(normal=[1e-18,1e-18,1])
opt = Optimizer("lsq-exact")

obj = ObjectiveFunction(CoilLength(coil))
# Nan Optimality when normal is close but not quite vertical
opt.optimize(coil,objective=obj,verbose=3,maxiter=2);
  • No NaN in forward mode
  • No NaN if the normal is [2e-8, 2e-8, 1], but NaN when the first two components are anything less than 2e-8
    • points to handling of the allclose here which depends on the dotproduct I guess? though these are not aligned with negative Z axis...

Our current tests dont catch this, if I add

      test([0, 1e-8, 1])  # nearly parallel
      test([0, 1e-8, -1])  # nearly antiparallel

to our test for this, these fail.

Activity

  1. YigitElma commented on Jul 29, 2026

    @YigitElma
    Collaborator

    I think this is due to round-off error during dot product. For [0, 1e-8, 1], dot product becomes 1*1 + 1e-8*1e-8= 1 + 1e-16 and this is rounded to simply 1 (due to float64). Maybe we can prune the very small terms from the dot product before actually computing it?

  2. added a commit that references this issue on Oct 29, 2026
    64bfefc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions