Repository navigation
Add the Boozer transform of the wout spectra with its exact tangent and adjoint - #32
krystophny wants to merge 1 commit into
Conversation
…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.
| 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]; |
There was a problem hiding this comment.
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_); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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());
^|
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 Chris&AI |
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-8bac9c4fis proximafusion/vmecpp main at 8bac9c4, so the diff shows only this change.What
vmecpp.boozer.BoozerTransformruns the stellarator-symmetric booz_xform algorithm in C++ (src/vmecpp/cpp/vmecpp/vmec/boozer) on the spectra of an in-memoryVmecWOut. 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 tormnc, zmns, lmns, bmnc, bsubumnc, bsubvmnc, iotasare 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, sojax.gradcomposes it with the implicit adjoint ofvmecpp.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 onbmnc_bback torbc, zbs.Verification (local, faepmac1, Enzyme build with Homebrew LLVM 22)
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).pytest tests/test_boozer.pygives 10 passed.bazel build //vmecpp/vmec/boozer:boozersucceeds, and the pre-commit hooks pass.Limits
lasymraises.bsubumncfor m != 0 andbsubvmnconly for m = 0, so the derivative with respect tobsubvmncvanishes for the other modes. For a converged equilibrium both determine the same potential.jax.jacrev.Chris&AI