Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ jobs:
pytest benchmarks/test_benchmarks.py --benchmark-json=benchmark_results.json

- name: Compare benchmark result
if: github.event_name == 'pull_request'
# Skip the result upload/compare for fork PRs: their GITHUB_TOKEN is
# read-only, so comment-on-alert/auto-push hit 'Resource not accessible
# by integration'. The benchmarks still run above; only the write-back
# is skipped for forks.
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
uses: benchmark-action/github-action-benchmark@v1.21.0
with:
tool: "pytest"
Expand Down
28 changes: 23 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,33 @@ jobs:
run: |
# install VMEC++ deps as well as VMEC2000 deps (we need to import VMEC2000 in a test)
sudo apt-get update && sudo apt-get install -y build-essential cmake libnetcdf-dev liblapack-dev libomp-dev
- name: Cache the VMEC2000 wheel
if: ${{ matrix.os == 'ubuntu-22.04' && matrix.python-version == '3.10' }}
uses: actions/cache@v4
with:
path: /tmp/vmec2000-wheel
# Keyed on the pinned VMEC2000 source commit: rebuild only when it changes.
key: vmec2000-728af8bd6c79-cp310-ubuntu22.04
- name: Also install VMEC2000 (only on Ubuntu 22.04)
if: ${{ matrix.os == 'ubuntu-22.04' && matrix.python-version == '3.10' }}
run: |
# mpi4py is needed for VMEC2000
sudo apt-get install -y libopenmpi-dev
sudo apt-get install -y libopenmpi-dev libnetcdff-dev libscalapack-mpi-dev \
libopenblas-dev ninja-build
python -m pip install mpi4py
# custom wheel for VMEC2000, needed for some VMEC++/VMEC2000 compatibility tests
# NOTE: this wheel is only guaranteed to work on Ubuntu 22.04
python -m pip install vmec@https://anaconda.org/eguiraud-pf/vmec/0.0.6/download/vmec-0.0.6-cp310-cp310-linux_x86_64.whl
if ! ls /tmp/vmec2000-wheel/vmec-*.whl >/dev/null 2>&1; then
# Build with the SYSTEM cmake + ninja; do NOT pip-install the
# cmake/ninja wheels (they shadow the system cmake that
# scikit-build-core's editable vmecpp rebuild records).
python -m pip install numpy scikit-build f90wrap setuptools wheel
git clone https://github.com/hiddenSymmetries/VMEC2000.git /tmp/VMEC2000
git -C /tmp/VMEC2000 checkout 728af8bd6c796b36a0aa85fe298e507791e57c6e
cp /tmp/VMEC2000/cmake/machines/ubuntu.json /tmp/VMEC2000/cmake_config_file.json
LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu \
python -m pip wheel /tmp/VMEC2000 --no-build-isolation -w /tmp/vmec2000-wheel
fi
python -m pip install /tmp/vmec2000-wheel/vmec-*.whl
# fail loudly here if the binding is still broken, not in the test step
python -c "import vmec; print('VMEC2000 import OK')"
- name: Install package
run: |
# on Ubuntu we would not need this, but on MacOS we need to point CMake to gfortran-14 and gcc-14
Expand Down
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ find_package(LAPACK REQUIRED)
FetchContent_Declare(
abseil-cpp
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
# 20260107.1 LTS: required for Clang >= 21, where the 2024 pin fails to
# compile (absl::Nonnull SFINAE in absl/strings/ascii.cc). Clang is the
# compiler used for the Enzyme autodiff build.
GIT_TAG 20260107.1
# 20260107.1 LTS: older abseil fails to compile under Clang >= 21 (the
# Enzyme build) on absl::Nonnull SFINAE in absl/strings/ascii.cc.
GIT_TAG 255c84dadd029fd8ad25c5efb5933e47beaa00c7
GIT_SHALLOW TRUE
)
FetchContent_Declare(
Expand Down
11 changes: 10 additions & 1 deletion src/vmecpp/cpp/vmecpp/vmec/ideal_mhd_model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ cc_test(
cc_library(
name = "ideal_mhd_model",
srcs = ["ideal_mhd_model.cc"],
hdrs = ["ideal_mhd_model.h", "dft_data.h"],
# The shared force-chain kernels and the autodiff composition/JVP header are
# header-only and included by ideal_mhd_model.cc; declare whatever exists on
# this branch so the bazel sandbox can see them. The Enzyme JVP entry point
# (exact_force_jvp.cc) is built only by the CMake VMECPP_ENABLE_ENZYME path,
# not by bazel; its use in ideal_mhd_model.cc is guarded by that define.
hdrs = ["ideal_mhd_model.h", "dft_data.h"] + glob([
"*_kernel.h",
"local_force_composition.h",
"exact_force_jvp.h",
]),
visibility = ["//visibility:public"],
deps = [
":dft_toroidal",
Expand Down
34 changes: 34 additions & 0 deletions src/vmecpp/cpp/vmecpp/vmec/ideal_mhd_model/bco_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2024-present Proxima Fusion GmbH
// <info@proximafusion.com>
//
// SPDX-License-Identifier: MIT
#ifndef VMECPP_VMEC_IDEAL_MHD_MODEL_BCO_KERNEL_H_
#define VMECPP_VMEC_IDEAL_MHD_MODEL_BCO_KERNEL_H_

namespace vmecpp {

// Lower the index on the contravariant field with the metric tensor:
// bsubu = guu B^u + guv B^v
// bsubv = guv B^u + gvv B^v
// In 2D guv is absent and drops out. Shared, allocation-free over flat buffers
// (n = number of half-grid points), between IdealMhdModel::computeBCo and the
// Enzyme autodiff path.
inline void ComputeBCo(const double* guu, const double* guv, const double* gvv,
const double* bsupu, const double* bsupv, bool lthreed,
int n, double* bsubu, double* bsubv) {
if (lthreed) {
for (int i = 0; i < n; ++i) {
bsubu[i] = guu[i] * bsupu[i] + guv[i] * bsupv[i];
bsubv[i] = guv[i] * bsupu[i] + gvv[i] * bsupv[i];
}
} else {
for (int i = 0; i < n; ++i) {
bsubu[i] = guu[i] * bsupu[i];
bsubv[i] = gvv[i] * bsupv[i];
}
}
}

} // namespace vmecpp

#endif // VMECPP_VMEC_IDEAL_MHD_MODEL_BCO_KERNEL_H_
16 changes: 6 additions & 10 deletions src/vmecpp/cpp/vmecpp/vmec/ideal_mhd_model/ideal_mhd_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "vmecpp/common/util/util.h"
#include "vmecpp/vmec/fourier_geometry/fourier_geometry.h"
#include "vmecpp/vmec/handover_storage/handover_storage.h"
#include "vmecpp/vmec/ideal_mhd_model/bco_kernel.h"
#include "vmecpp/vmec/ideal_mhd_model/bcontra_kernel.h"
#include "vmecpp/vmec/ideal_mhd_model/jacobian_kernel.h"
#include "vmecpp/vmec/ideal_mhd_model/metric_kernel.h"
Expand Down Expand Up @@ -1453,16 +1454,11 @@ void IdealMhdModel::computeBContra() {

// Compute covariant magnetic field components.
void IdealMhdModel::computeBCo() {
// bsubu = g * B^contra: index lowering via metric tensor
if (s_.lthreed) {
// 3D case: need all of guu, guv, gvv
bsubu = guu.cwiseProduct(bsupu) + guv.cwiseProduct(bsupv);
bsubv = guv.cwiseProduct(bsupu) + gvv.cwiseProduct(bsupv);
} else {
// 2D case: can ignore guv (not even allocated)
bsubu = guu.cwiseProduct(bsupu);
bsubv = gvv.cwiseProduct(bsupv);
}
// bsubu = g * B^contra (index lowering via the metric tensor). Shared,
// allocation-free kernel (bco_kernel.h) used by solver and autodiff.
ComputeBCo(guu.data(), guv.data(), gvv.data(), bsupu.data(), bsupv.data(),
s_.lthreed, static_cast<int>(bsupu.size()), bsubu.data(),
bsubv.data());
}

void IdealMhdModel::pressureAndEnergies() {
Expand Down
13 changes: 8 additions & 5 deletions src/vmecpp/cpp/vmecpp/vmec/ideal_mhd_model/jacobian_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ namespace vmecpp {
// half-grid; sqrtSH is indexed jH - nsMinH. The half-grid point jH sits between
// full-grid surfaces jH (inside) and jH + 1 (outside).
inline void ComputeHalfGridJacobian(
const double* r1e, const double* r1o, const double* z1e, const double* z1o,
const double* rue, const double* ruo, const double* zue, const double* zuo,
const double* sqrtSH, double deltaS, double dSHalfDsInterp, int nZnT,
int nsMinF1, int nsMinH, int nsMaxH, double* r12, double* ru12,
double* zu12, double* rs, double* zs, double* tau) {
const double* __restrict r1e, const double* __restrict r1o,
const double* __restrict z1e, const double* __restrict z1o,
const double* __restrict rue, const double* __restrict ruo,
const double* __restrict zue, const double* __restrict zuo,
const double* __restrict sqrtSH, double deltaS, double dSHalfDsInterp,
int nZnT, int nsMinF1, int nsMinH, int nsMaxH, double* __restrict r12,
double* __restrict ru12, double* __restrict zu12, double* __restrict rs,
double* __restrict zs, double* __restrict tau) {
for (int jH = nsMinH; jH < nsMaxH; ++jH) {
const double sH = sqrtSH[jH - nsMinH];
for (int kl = 0; kl < nZnT; ++kl) {
Expand Down
16 changes: 10 additions & 6 deletions src/vmecpp/cpp/vmecpp/vmec/ideal_mhd_model/metric_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ namespace vmecpp {
// IdealMhdModel::computeMetricElements and the Enzyme autodiff path. Same
// indexing conventions as jacobian_kernel.h. sqrtSF is indexed jF - nsMinF1.
inline void ComputeMetricElements(
const double* r1e, const double* r1o, const double* rue, const double* ruo,
const double* zue, const double* zuo, const double* rve, const double* rvo,
const double* zve, const double* zvo, const double* tau, const double* r12,
const double* sqrtSF, const double* sqrtSH, bool lthreed, int nZnT,
int nsMinF1, int nsMinH, int nsMaxH, double* gsqrt, double* guu,
double* guv, double* gvv) {
const double* __restrict r1e, const double* __restrict r1o,
const double* __restrict rue, const double* __restrict ruo,
const double* __restrict zue, const double* __restrict zuo,
const double* __restrict rve, const double* __restrict rvo,
const double* __restrict zve, const double* __restrict zvo,
const double* __restrict tau, const double* __restrict r12,
const double* __restrict sqrtSF, const double* __restrict sqrtSH,
bool lthreed, int nZnT, int nsMinF1, int nsMinH, int nsMaxH,
double* __restrict gsqrt, double* __restrict guu, double* __restrict guv,
double* __restrict gvv) {
for (int jH = nsMinH; jH < nsMaxH; ++jH) {
const double sF_i = sqrtSF[jH - nsMinF1] * sqrtSF[jH - nsMinF1];
const double sF_o = sqrtSF[jH + 1 - nsMinF1] * sqrtSF[jH + 1 - nsMinF1];
Expand Down
5 changes: 5 additions & 0 deletions tests/test_simsopt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ def test_ensure_vmec2000_input_from_vmecpp_input():
if varname[1:-1] == "axis_":
# these are called differently in VMEC2000, e.g. raxis_c -> raxis_cc
varname_vmec2000 = f"{varname[:-1]}c{varname[-1]}"
if not hasattr(vmec2000.indata, varname_vmec2000):
# vmecpp-only field (e.g. free_boundary_method) with no counterpart
# in the legacy VMEC2000 INDATA namelist; not part of the common
# subset under test.
continue
vmec2000_var = getattr(vmec2000.indata, varname_vmec2000)

if vmecpp_var is None:
Expand Down
Loading