Skip to main content

Differentiable 1D tokamak plasma transport simulator in JAX.

Project description

Unittests

What is TORAX?

TORAX is a differentiable tokamak core transport simulator aimed for fast and accurate forward modelling, pulse-design, trajectory optimization, and controller design workflows. TORAX is written in Python using JAX, with the following motivations:

  • Open-source and extensible, aiding with flexible workflow coupling
  • JAX provides auto-differentiation capabilities and code compilation for fast runtimes. Differentiability allows for gradient-based nonlinear PDE solvers for fast and accurate modelling, and for sensitivity analysis of simulation results to arbitrary parameter inputs, enabling applications such as trajectory optimization and data-driven parameter identification for semi-empirical models. Auto-differentiability allows for these applications to be easily extended with the addition of new physics models, including ML-surrogates, or new parameter inputs, by avoiding the need to hand-derive Jacobians
  • Python-JAX is a natural framework for the coupling of ML-surrogates of physics models

For more comprehensive documentation, see our readthedocs page.

TORAX, at v1.0.0, has the following physics and numerics feature set:

  • Coupled PDEs of ion and electron heat transport, electron particle transport, and current diffusion
    • Finite-volume-method discretization
    • Multiple solver options: linear with Pereverzev-Corrigan terms and the predictor-corrector method, nonlinear with Newton-Raphson, nonlinear with optimization using the jaxopt library
    • Poloidal flux boundary conditions based on either total current or loop voltage at the last-closed-flux-surface
  • Ohmic power, ion-electron heat exchange, fusion power, Bremsstrahlung, impurity line radiation, an [ICRH ML-surrogate] (as-yet covering limited regimes),
  • Neoclassical bootstrap current and conductivity with the analytical Sauter model
  • Coupling to the [QLKNN_7_11] and QLKNN10D [van de Plassche et al, Phys. Plasmas 2020] QuaLiKiz neural network surrogates for physics-based turbulent transport
  • General geometry, provided via CHEASE, FBT, or EQDSK equilibrium files
    • For testing and demonstration purposes, a single CHEASE equilibrium file is available in the data/third_party/geo directory. It corresponds to an ITER hybrid scenario equilibrium based on simulations in [Citrin et al, Nucl. Fusion 2010], and was obtained from PINT. A PINT license file is available in data/third_party/geo
    • Time dependent geometry is supported by providing a time series of geometry files
  • Simple pedestal models using a local adaptive source to set internal boundary conditions
  • Sawtooth triggering and profile redistribution

Additional heating and current drive sources can be provided by user-provided analytical models, or user-provided prescribed data.

Model implementation was verified through direct comparison of simulation outputs to the RAPTOR [Felici et al, Plasma Phys. Control. Fusion 2012] tokamak transport simulator.

This is not an officially supported Google product.

Development roadmap

A development roadmap is outlined in our readthedocs pages.

Installation guide

Requirements

Install Python 3.11 or greater.

Make sure that tkinter is installed:

sudo apt-get install python3-tk

How to install

Prepare a virtual environment

Install Virtualenv (if not already installed):

pip install --upgrade pip
pip install virtualenv

Create and activate a virtual environment

python3 -m venv toraxvenv
source toraxvenv/bin/activate

Install from PyPI

The simplest way to use TORAX is to install it via PyPI:

pip install torax

You can check that everything runs as it should:

run_torax --config=examples/basic_config.py --quit

If you plan to help develop TORAX, you will need to clone the repository, see contributing and contribution_tips

Running an example

The following command will run TORAX using the default configuration file examples/basic_config.py.

run_torax --config='examples/basic_config.py'

Simulation progress is shown by a terminal progress bar indicating the current time and percentage completed.

To run more involved, ITER-inspired simulations, run:

run_torax --config='examples/iterhybrid_rampup.py'

and

run_torax --config='examples/iterhybrid_predictor_corrector.py'

Additional configuration is provided through flags which append the above run command, and environment variables. For example, for increased output verbosity, you can run with the --log_progress flag.

run_torax  --config='examples/iterhybrid_rampup.py' --log_progress

Set environment variables

Error checking

If true, error checking is enabled in internal routines. Used for debugging. Default is false since it is incompatible with the persistent compilation cache.

$ export TORAX_ERRORS_ENABLED=<True/False>
JAX Compilation and Cache

If false, JAX does not compile internal TORAX functions. Used for debugging. Default is False.

$ export JAX_DISABLE_JIT=<True/False>

The following implements the JAX persistent cache and will cause jax to store compiled programs to the filesystem, reducing recompilation time in some cases:

$ export JAX_COMPILATION_CACHE_DIR=<path of your choice, such as ~/jax_cache>
$ export JAX_PERSISTENT_CACHE_MIN_ENTRY_SIZE_BYTES=-1
$ export JAX_PERSISTENT_CACHE_MIN_COMPILE_TIME_SECS=0.0

Set flags

log_progress - output simulation time, dt, and number of solver iterations (dt backtracking with nonlinear solver) carried out at each timestep.

run_torax \
   --config=examples/iterhybrid_predictor_corrector.py \
   --log_progress

output_dir - overrides the default output directory

run_torax \
   --config=examples/iterhybrid_predictor_corrector.py \
   --output_dir=/path/to/output/dir

Running Tests with Full Assertion Checks

To run the pytest test suite with all assertion checks enabled, you can use the following command:

TORAX_JAXTYPING=True TORAX_ERRORS_ENABLED=True pytest . -n auto

Post-simulation

Once complete, the time history of a simulation state and derived quantities is written to a timestamped file of the format state_history_%Y%m%d_%H%M%S.nc. The output directory is user configurable, with a default /tmp/torax_results.

To take advantage of the in-memory (non-persistent) cache, the process does not end upon simulation termination. It is possible to modify the runtime_params, toggle the log_progress and plot_progress flags, and rerun the simulation. The following modifications are examples which will trigger a recompilation:

  • Grid resolution
  • Evolved variables (equations being solved)
  • Changing internal functions used, e.g. transport models, time_step_calculator. source models, solver model, pedestal model

Cleaning up

You can exit the Python virtual env by deactivating it:

deactivate

(Optional) Install QLKNN-hyper

The default transport model in TORAX is QLKNN_7_11, installed as a TORAX dependency. An alternative to QLKNN_7_11 is to use QLKNN-hyper-10D, also known as QLKNN10D K.L. van de Plassche PoP 2020. QLKNN_7_11 is based on QuaLiKiz 2.8.1 which has an improved collision operator compared to the QLKNN10D training set. QLKNN_7_11 training data includes impurity density gradients as an input feature and has better coverage of the near-LCFS region compared to QLKNN-hyper-10D. However, using QLKNN10D may still be useful for specific use-cases, such as in benchmarks with other simulators, or comparisons with QLKNN_7_11.

Download QLKNN-hyper dependencies:

git clone https://gitlab.com/qualikiz-group/qlknn-hyper.git

To use QLKNN10D, set model_path in the transport section of your TORAX config to the path of the cloned repository.

Simulation tutorials

See tutorials section in our readthedocs pages.

Citing TORAX

A TORAX paper is available on arXiv. Cite this paper to cite TORAX:

@article{torax2024arxiv,
  title={{TORAX: A Fast and Differentiable Tokamak Transport Simulator in JAX}},
  author={Citrin, Jonathan and Goodfellow, Ian and Raju, Akhil and Chen, Jeremy
  and Degrave, Jonas and Donner, Craig and Felici, Federico and Hamel, Philippe
  and Huber, Andrea and Nikulin, Dmitry and Pfau, David and Tracey, Brendan, and
  Riedmiller, Martin and Kohli, Pushmeet},
  journal={arXiv preprint arXiv:2406.06718},
  year={2024}
}

Project details


Download files

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

Source Distribution

torax-1.3.1.tar.gz (897.3 kB view details)

Uploaded Source

Built Distribution

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

torax-1.3.1-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file torax-1.3.1.tar.gz.

File metadata

  • Download URL: torax-1.3.1.tar.gz
  • Upload date:
  • Size: 897.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for torax-1.3.1.tar.gz
Algorithm Hash digest
SHA256 c4d131840686af0a0beff2bd93ddb6f313b74704292f520d86642edc24c50417
MD5 ae02e51025bcc592ec586c6e79d3281a
BLAKE2b-256 66fe55e623c3f71c287a9a163fd4414c45716ffd5a906edb6361072afac107fd

See more details on using hashes here.

File details

Details for the file torax-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: torax-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for torax-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d9baf58ed828b6cf161034c15c76c7808103a8c93213c30bf1ce726e9f2f2f
MD5 c6f27ddffda1b41c8111407613eee2db
BLAKE2b-256 64cc0a8aee4d4c0c131f04aeee67ee227c886bfb70760713304a0cb4eb8bdcd7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page