Skip to content

Add the Boozer transform of the wout spectra with its exact tangent and adjoint - #32

Closed
krystophny wants to merge 1 commit into
base/upstream-main-8bac9c4ffrom
feat/boozer-output-adjoint
Closed

krystophny wants to merge 1 commit into
base/upstream-main-8bac9c4ffrom
feat/boozer-output-adjoint

Conversation

@krystophny

Copy link
Copy Markdown
Member

This draft lives in our fork on purpose. Upstream closed a Boozer transform PR (proximafusion#770) with the comment that VMEC++ is not the place for it, so whether to propose this upstream is left to Chris. The base branch base/upstream-main-8bac9c4f is proximafusion/vmecpp main at 8bac9c4, so the diff shows only this change.

What

vmecpp.boozer.BoozerTransform runs the stellarator-symmetric booz_xform algorithm in C++ (src/vmecpp/cpp/vmecpp/vmec/boozer) on the spectra of an in-memory VmecWOut. It uses the same angle grid, mode table and half-grid interpolation of R and Z as booz_xform, so the Boozer spectra of a solve need no wout file. Nothing is computed unless it is called, and the default solver output is unchanged.

The tangent (jvp) and the adjoint (vjp) with respect to rmnc, zmns, lmns, bmnc, bsubumnc, bsubvmnc, iotas are hand-written. The transform is a linear synthesis on the VMEC grid, then a pointwise map to the Boozer angles, then a quadrature against the Boozer harmonics, whose adjoint is the transposed sum. BoozerTransform.jax() wraps the C++ forward pass and VJP as a JAX custom VJP, so jax.grad composes it with the implicit adjoint of vmecpp.run (merged upstream in proximafusion#710 and proximafusion#906, released in 0.9.0 and 0.9.1, needs an Enzyme build) and pulls a cotangent on bmnc_b back to rbc, zbs.

Verification (local, faepmac1, Enzyme build with Homebrew LLVM 22)

  • Primal against booz_xform 0.1.0: max relative difference 1e-15 (CTH-like, W7-X with mboz=24, nboz=16), and at most 2e-14 on the Landreman-Paul QA/QH, NCSX li383 and circular tokamak wouts.
  • JVP against central differences: the error falls as h^2 down to 5e-10 relative at h=1e-6. Per-input JVP against FD stays below 5.3e-9 on the public cases.
  • Adjoint duality <v, J u> = <J^T v, u> holds to 1e-14 relative, and the JVP agrees with JAX autodiff of an independent implementation in the test to 1e-11.
  • Boundary gradient of a Boozer quasi-symmetry residual through vmecpp.run (cth_like, ns=25, ftol=1e-14) against re-solved central differences: 2.3e-6 relative error at a step of 1e-4, and between 7e-6 and 1e-5 per DOF for rbc(1,0), zbs(1,1) and rbc(2,-1).
  • Timing: W7-X, 98 surfaces, mnboz=776 takes 0.21 s forward, 0.34 s JVP and 0.33 s VJP with OpenMP over surfaces, against 0.87 s for booz_xform on one thread. The boundary gradient took 4.7 s, against 2.3 s for one solve.
  • pytest tests/test_boozer.py gives 10 passed. bazel build //vmecpp/vmec/boozer:boozer succeeds, and the pre-commit hooks pass.

Limits

  • Stellarator symmetry only, and lasym raises.
  • The derivatives are those of the discrete transform at fixed mboz, nboz and grid. They are smooth while the Jacobian of the angle map keeps its sign.
  • booz_xform reads bsubumnc for m != 0 and bsubvmnc only for m = 0, so the derivative with respect to bsubvmnc vanishes for the other modes. For a converged equilibrium both determine the same potential.
  • Boundary-level forward mode is not available, because the upstream solver layer is reverse-only. Full Jacobians come from jax.jacrev.

Chris&AI

…nd adjoint

vmecpp.boozer.BoozerTransform runs the stellarator-symmetric booz_xform
algorithm in C++ (vmecpp/vmec/boozer) on the spectra of an in-memory VmecWOut,
with the same grid, mode table and half-grid interpolation of R and Z, so the
Boozer spectra of a solve need no wout file. It is computed only on request.

The tangent and the adjoint with respect to rmnc, zmns, lmns, bmnc, bsubumnc,
bsubvmnc and iotas are hand-written: the transform is a linear synthesis on
the VMEC grid, a pointwise map to the Boozer angles and a quadrature against
the Boozer harmonics, whose adjoint is the transposed sum. BoozerTransform.jax()
wraps the C++ forward pass and VJP as a JAX custom VJP, so jax.grad composes it
with the implicit adjoint of vmecpp.run and pulls a cotangent on bmnc_b back to
the boundary coefficients.

tests/test_boozer.py checks the primal against booz_xform 0.1.0, the tangent
against central differences and JAX autodiff of an independent implementation,
the adjoint against the tangent, and on an Enzyme build the boundary gradient
of a Boozer quasi-symmetry residual against re-solved central differences.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Eigen::VectorXd ToDouble(const std::vector<int>& v) {
Eigen::VectorXd out(v.size());
for (std::size_t i = 0; i < v.size(); ++i) {
out[i] = v[i];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'std::size_t' (aka 'unsigned long') to signed type 'Index' (aka 'long') is implementation-defined [bugprone-narrowing-conversions]

    out[i] = v[i];
        ^

t.n_b_ = ToDouble(t.xn_b_);

t.fourier_factor_ = 2.0 / ((t.nu2_b_ - 1) * t.nzeta_);
t.normalization_b_.setConstant(t.xm_b_.size(), t.fourier_factor_);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'Index' (aka 'long') is implementation-defined [bugprone-narrowing-conversions]

  t.normalization_b_.setConstant(t.xm_b_.size(), t.fourier_factor_);
                                 ^

Eigen::VectorXd BoozerTransform::s_b() const {
VectorXd s(surfaces_.size());
for (std::size_t i = 0; i < surfaces_.size(); ++i) {
s[i] = (surfaces_[i] + 0.5) / (ns_ - 1.0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'std::size_t' (aka 'unsigned long') to signed type 'Index' (aka 'long') is implementation-defined [bugprone-narrowing-conversions]

    s[i] = (surfaces_[i] + 0.5) / (ns_ - 1.0);
      ^

g.lambda_z = -(cos_grid_ * n_.cwiseProduct(lambda));

// Periodic part of the scalar potential, booz_xform's transpmn.
VectorXd wmns = VectorXd::Zero(xm_nyq_.size());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'Index' (aka 'long') is implementation-defined [bugprone-narrowing-conversions]

  VectorXd wmns = VectorXd::Zero(xm_nyq_.size());
                                 ^

@krystophny

Copy link
Copy Markdown
Member Author

This change will not go upstream into VMEC++, so the Boozer transform with its JVP/VJP has moved into boozer-bench as the standalone package vmecpp_boozer (https://github.com/itpplasma/boozer-bench/tree/main/vmecpp_boozer, commit 396c410). It depends only on Eigen and pybind11, keeps the BoozerTransform API (__call__, jvp, vjp, jax) and chains with the implicit boundary adjoint of VMEC++ 0.9.1 through vmecpp_boozer.vmecpp_chain.boundary_vjp. The move also fixes the memory use at mboz = nboz = 48 (the dense Boozer harmonics per surface and thread are now generated in blocks): 38.8 GB became 1.8 GB on 32 surfaces. Closing this pull request in favour of the new location.

Chris&AI

@krystophny krystophny closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant