Skip to main content

CI CI-Large Coverage PyPI Python License

virtual_casing_jax

virtual_casing_jax is a JAX implementation of the virtual casing principle for computing magnetic-field contributions from plasma currents using high-order singular quadrature. It is based on the C++ reference implementation in hiddenSymmetries/virtual-casing and on the SIMSOPT virtual-casing interface in hiddenSymmetries/simsopt.

Documentation is available at virtual-casing-jax.readthedocs.io.

Installation

Install the latest release from PyPI:

pip install virtual-casing-jax

Or install from a local source checkout:

git clone https://github.com/uwplasma/virtual_casing_jax.git
cd virtual_casing_jax
pip install -e .

Basic Usage

The SIMSOPT-compatible wrapper can be used as a drop-in virtual-casing calculation when SIMSOPT is installed:

from virtual_casing_jax import VirtualCasing

vc = VirtualCasing.from_vmec(
    "wout_example.nc",
    src_nphi=32,
    trgt_nphi=32,
    trgt_ntheta=32,
    filename="auto",
)

B_external_normal = vc.B_external_normal

For lower-level JAX workflows, use VirtualCasingJAX directly after preparing surface coordinates and magnetic-field arrays:

from virtual_casing_jax import VirtualCasingJAX

vc_jax = VirtualCasingJAX()
vc_jax.setup(digits, nfp, stellsym, Nt, Np, gamma, Nt, Np, Nt, Np)
B_external = vc_jax.compute_external_B(B_total)

Differentiable in the surface geometry

compute_external_B / compute_internal_B are differentiable in the source field out of the box. They are also differentiable in the surface coordinates — useful for single-stage stellarator optimization, where the plasma boundary itself is a degree of freedom — once the adaptive precision is frozen. The precision auto-selection (quadrature grid size, singular-patch dimension) concretizes surface-derived values, so under jax.grad/jit of the surface you first pick it once from a concrete surface and pass it back:

plan = vc_jax.plan_precision(digits=4)          # concrete surface -> PrecisionPlan

def loss(surface_coord):                        # surface is the differentiated input
    vc = VirtualCasingJAX()
    vc.setup(digits, nfp, stellsym, Nt, Np, surface_coord, Nt, Np, Nt, Np)
    return objective(vc.compute_internal_B(B_total, precision=plan))

grad = jax.grad(loss)(surface_coord)            # finite (NaN-safe self-interaction)

precision=plan reproduces the auto-selected precision exactly (identical B), and the Laplace kernels use a NaN-safe self-interaction gradient, so the surface gradient is finite. plan_precision also accepts explicit quad_nt/quad_np.

Performance features:

  • Source/target tiling with auto-tuned chunk sizes.
  • Rematerialization hooks for GradB singular correction.
  • Optional target-scan mode to reduce GradB peak memory (scan_targets).
  • Mixed-precision POU/patch tables with float64 outputs.
  • Bundled Quas3/LHD/W7X geometry assets (converted from SCTL .mat).

SIMSOPT compatibility: The package ships a SIMSOPT-compatible VirtualCasing class that mirrors simsopt.mhd.virtual_casing.VirtualCasing while using the JAX backend. Import it as from virtual_casing_jax import VirtualCasing. See docs/using_simsopt.rst and the examples in examples/ for full scripts.

Bundled test data: To make the SIMSOPT-style examples and tests self-contained, the repo includes a small subset of SIMSOPT test assets under tests/test_files/ and the VMEC input examples/inputs/input.QH_finitebeta. These files originated from the SIMSOPT repository (SIMSOPT) and are used only for validation and example runs.

Docs

Sphinx documentation lives in docs/ and is configured for ReadTheDocs. It includes the equations, numerics, implementation details, and validation strategy. Run locally:

pip install -r docs/requirements.txt
sphinx-build -b html docs docs/_build/html

Profiling

Use the profiling harness to capture JAX traces and inspect performance:

JAX_ENABLE_X64=1 python tools/profile_vc.py --case case_vc --op B --jit \
  --repeat 5 --trace-dir /tmp/vc_trace

tensorboard --logdir /tmp/vc_trace

For the new tuning knobs:

JAX_ENABLE_X64=1 XLA_FLAGS="--xla_dump_to=/tmp/vc_xla --xla_dump_hlo_as_text" \
  python tools/profile_vc.py --case case_vc_large --op GradB --jit \
  --chunk-size auto --target-chunk-size auto --pou-dtype float32 --patch-dtype float32 \
  --interp-block-size auto --remat --donate \
  --repeat 2 --trace-dir /tmp/vc_trace_case_vc_large_GradB

tensorboard --logdir /tmp/vc_trace_case_vc_large_GradB

This writes JAX traces under /tmp/vc_trace_* and HLO dumps under /tmp/vc_xla_*. See docs/performance.rst for detailed interpretation.

VMEC Exterior Fields

virtual_casing_jax can wrap VMEC boundary data as an EXTENDER-like exterior field. The current downstream integration is VMEX (package vmex, the JAX VMEC formerly named vmec_jax), whose free-boundary module vmex.core.freeboundary_diff builds a VmecSurfaceFieldData directly from a wout file or a VMEX state:

from vmex import read_wout
from vmex.core.freeboundary_diff import surface_field_data_from_wout
from virtual_casing_jax import ExteriorFieldConfig, VirtualCasingExteriorField

wout = read_wout("wout_circular_tokamak.nc")
surface = surface_field_data_from_wout(wout, nphi=32, ntheta=32)
field = VirtualCasingExteriorField(surface, ExteriorFieldConfig(digits=8))

B_plasma = field.B_plasma_xyz([[1.8, 0.0, 0.0]])

The legacy bridge surface_field_from_vmec_jax (module virtual_casing_jax.vmec_jax_bridge) is kept only for backwards compatibility with the historical vmec_jax package name and requires that package to be importable.

For targets outside the VMEC boundary, the plasma-current contribution uses the internal virtual-casing branch by default because the plasma currents are inside the LCFS:

B_out(x) = B_coils(x) + B_plasma^VC(x)
         = B_coils(x) + B_internal^VC(x)

The external branch remains available for diagnostics and means currents outside the VMEC surface, not target points outside the VMEC surface. This is not a self-consistent SOL plasma equilibrium solver; it does not replace HINT, SIESTA, PIES, or M3D-C1 when islands, stochastic regions, pressure relaxation, edge currents, or resistive MHD response must be solved self-consistently.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

virtual_casing_jax-0.0.3.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

virtual_casing_jax-0.0.3-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

Details for the file virtual_casing_jax-0.0.3.tar.gz.

File metadata

  • Download URL: virtual_casing_jax-0.0.3.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for virtual_casing_jax-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fe2b803d9e75920f8b213d633bbd12295362aed69d1949ee9da52b6323617c1c
MD5 22939f23a45ae3e1f3b501a3bd14f964
BLAKE2b-256 52d7c5b237b21935e3195c46d15f0fd530a0f64916ab54ee5b2368e49991c8b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for virtual_casing_jax-0.0.3.tar.gz:

Publisher: publish.yml on uwplasma/virtual_casing_jax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file virtual_casing_jax-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for virtual_casing_jax-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 96333f2b98a051fae7623f940cc29451d50310cdfabed2557f746db7552cdca8
MD5 7551492e8a3ea5661381dc046a64138b
BLAKE2b-256 fbfb3f7aa821bfcec48a55c4608b5c9edc07f1918a8e16c9dbdca8c285ae580c

See more details on using hashes here.

Provenance

The following attestation bundles were made for virtual_casing_jax-0.0.3-py3-none-any.whl:

Publisher: publish.yml on uwplasma/virtual_casing_jax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.0.5

2 files

0.0.4

2 files

This release

0.0.3 This release

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page