Skip to main content

DKX

PyPI CI Docs License

Differentiable neoclassical transport for stellarators and tokamaks, in JAX.

DKX solves the radially local, linearized drift-kinetic equation on a flux surface and returns particle and heat fluxes, parallel flows, bootstrap current, transport matrices and ambipolar radial electric fields. It implements the SFINCS Fortran v3 model, including full Fokker–Planck collisions and Phi1, reads and writes SFINCS decks, runs on CPU or GPU, and every output is differentiable in every input.

W7-X standard configuration: |B| and parallel current density on the boundary, bootstrap current profile, ambipolar Er against Pablant et al. 2018

Install

pip install dkx                    # CPU
pip install -U "jax[cuda12]"       # add for NVIDIA GPUs

Run

import dkx

case = dkx.Case.from_mapping({           # analytic tokamak, teaching grid: seconds, not converged
    "schema": 1, "name": "tokamak", "run": {"workflow": "profile", "progress": False},
    "geometry": {"format": "analytic", "file": "tokamak", "surfaces": [0.16, 0.25, 0.36]},
    "species": [{"name": "deuterium", "charge": 1, "mass_amu": 2.014,
                 "density_m3": [8.0e19, 7.0e19, 6.0e19], "temperature_keV": [1.0, 0.8, 0.6]}],
    "physics": {"model": "full_local", "collisions": "pitch_angle_scattering", "magnetic_drifts": "dkes", "phi1": "off"},
    "electric_field": {"mode": "prescribed", "value_kV_m": 0.0},
    "resolution": {"theta": 9, "zeta": 1, "pitch": 8, "speed": 4}, "solver": {"method": "auto", "relative_tolerance": 1e-8},
})
result = dkx.run(case)
print("solver route:", result.metadata["solver_route"])
print("particle flux:", float(result.arrays["particle_flux_m2_s"][1, 0]))

From the shell: dkx run case.toml, dkx converge case.toml (check the resolution before trusting a number), dkx wout_w7x.nc, or dkx input.namelist for a SFINCS deck.

Gradients

import jax, jax.numpy as jnp, dkx

case = dkx.Case.from_file("examples/05_ambipolar_profile/case.toml")
problem = dkx.prepare_er_scan(case, surface_index=1)          # geometry, grids, collisions once

def bootstrap_current(er_kv_m):
    scan = dkx.batched_er_scan(problem, er_kv_m, differentiable=True, retain_full_state=True)
    return jnp.sum(scan.moments["FSABjHat"])

j, dj_der = jax.jit(jax.value_and_grad(bootstrap_current))(jnp.array([-0.2, 0.0, 0.2]))

The derivative passes through the linear solve by the implicit function theorem, and every returned state has satisfied the original kinetic equation. Profiles and geometry differentiate the same way: differentiability.

Choose a workflow

Calculation Start from
Tokamak profile with prescribed E_r examples/01_tokamak_profile
Stellarator from VMEC or Boozer files examples/02_vmec_stellarator, examples/03_boozer_stellarator
Monoenergetic transport scan examples/04_monoenergetic_scan
Ambipolar roots and branch selection examples/05_ambipolar_profile
Resolution study, gradient checks examples/06_convergence_certificate, examples/07_gradients
Geometry sensitivity and descent (analytic proxy) examples/08_vmex_optimization
Phi1 and impurities (expert path) examples/09_phi1_and_impurities

Why DKX

DKX SFINCS v3 MONKES yancc
Full linearized Fokker–Planck, multispecies
Analytic, VMEC, Boozer and lasym geometry
Phi1 quasineutrality; Tangential magnetic drifts; export_f
Ambipolar E_r root with retained branch evidence
Transport matrices (RHSMode 2/3) and SFINCS deck/HDF5 I/O
GPU, JIT-compiled scans, Krylov recycling
Exact gradients of any output w.r.t. any input adjoint branches claimed
Gradients verified against finite differences

Verified

DKX against SFINCS Fortran v3, MONKES and YANCC: scaled differences on matched full Fokker-Planck decks, and Beidler-normalized monoenergetic coefficients

Against SFINCS Fortran v3 on 38 upstream decks with the same discretization, DKX agrees to solver tolerance: median 4e-6, full Fokker–Planck decks to 1e-8. Against the independent codes MONKES and YANCC, the four Beidler-normalized monoenergetic coefficients agree within 6 percent and D33 within 0.1 percent on three configurations. Gradients agree with central finite differences over a step window on every shipped derivative example. Details, tolerances and scope: validation matrix.

Fast

Runtime and peak memory, DKX against SFINCS Fortran v3, on the 744k-unknown HSX PAS case

HSX_PASCollisions_DKESTrajectories, RHSMode=1, 744,610 unknowns, one machine, against the PETSc 3.23 / MUMPS 5.8.2 build of SFINCS v3. Warm is the second solve in a process, after XLA has compiled.

Configuration Warm solve Peak RSS
DKX, Nxi-for-x ramp 27.2 s 0.93 GB
DKX, uniform Nxi 44.3 s 1.16 GB
DKX, RTX A4000 GPU 45.0 s
SFINCS Fortran v3, 1 rank 463.6 s 3.98 GB
SFINCS Fortran v3, 2 ranks (its best) 229.5 s 2.86 GB
Cold versus warm, M3 Max CPU Unknowns Cold Warm
HSX PAS reduced 40,584 1.72 s 0.12 s
HSX PAS, 25x51x100x5 744,610 23.6 s 20.0 s

That is one measured 744k-unknown HSX PAS case, chosen because DKX has an exact structured solver for it. Across all 38 upstream decks: structured route faster on 9 of 9; Krylov route faster on 7 of 23, six not completed. Every deck, hardware string and method: performance.

Measured parity envelopes of DKX against SFINCS Fortran v3

Documentation

Tutorial: first W7-X result · How-to: resolution, wout files, SFINCS decks · Reference: schema, CLI, API · Explanation: physics models, solver routes, limitations

Cite

@software{dkx,
  author = {Jorge, Rogerio and contributors},
  title  = {DKX: differentiable drift-kinetic neoclassical transport in JAX},
  url    = {https://github.com/uwplasma/DKX},
  year   = {2026}
}

Please also cite SFINCS (Landreman, Smith, Mollén & Helander, Phys. Plasmas 21, 042503, 2014) when you use its decks or model. Metadata: CITATION.cff.

Examples · Research plan · Contributing · SOLVAX owns the reusable solver algorithms · LICENSE

Download files

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

Source Distribution

dkx-2.4.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

dkx-2.4.0-py3-none-any.whl (598.0 kB view details)

Uploaded Python 3

File details

Details for the file dkx-2.4.0.tar.gz.

File metadata

  • Download URL: dkx-2.4.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dkx-2.4.0.tar.gz
Algorithm Hash digest
SHA256 3ce1251b6cfe3dc36840471b04a6e3b8c89f812a9362fc82ffdd3814c07c9aea
MD5 eefa87d464a8e4d9bba6ac1226bf91c4
BLAKE2b-256 22e7705ae61d1ca6f05c5025f165fb73595024bff8f0f5bc494803db28edd3cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dkx-2.4.0.tar.gz:

Publisher: publish-pypi.yml on uwplasma/DKX

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

File details

Details for the file dkx-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: dkx-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 598.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dkx-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a913322db31b7060db9abdcc2bec3afaaf9723efccd58ff22a78213e677a5f36
MD5 ddb532c1d6907d29660a0d8409c98efc
BLAKE2b-256 2fca14372e84e93969f6ff39b9693d9296e63f7d35dd38dc18570de076a45c7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dkx-2.4.0-py3-none-any.whl:

Publisher: publish-pypi.yml on uwplasma/DKX

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

Release history Release notifications | RSS feed

This release

2.4.0 This release

2 files

2.3.1

2 files

2.3.0

2 files

2.2.0

2 files

2.1.1

2 files

2.1.0

2 files

2.0.0

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