Skip to main content

Flexible high‑performance plasma physics solver in Python using JAX and NumPy

Project description

flexipde

CI Docs PyPI version codecov

flexipde is a flexible, high‑performance library for solving plasma physics equations in any number of dimensions. It combines the ease of writing Python with the speed of modern array libraries like NumPy and JAX. The library draws inspiration from—and goes beyond—established simulation frameworks to have a performant, simple to use and flexible library. Instead of low‑level C++ or Fortran, you write concise Python code that can automatically run on CPUs or GPUs via JAX, and you can differentiate simulations with respect to parameters to enable optimisation or machine‑learning pipelines.

Features

  • Unified grid and coordinate system: Create structured grids in 1D, 2D or 3D, including cylindrical coordinate systems with arbitrary metrics.
  • Multiple discretisation methods: Choose between Fourier spectral methods for periodic domains and finite difference methods for general boundaries. Additional schemes (e.g. finite volume/WENO) can be added easily.
  • Built‑in models: Includes linear advection, diffusion, ideal Alfvén waves, resistive MHD, two‑fluid plasmas, drift‑kinetic equations and a Vlasov–Poisson solver. Additional models can be implemented by subclassing a base class.
  • Custom equations: Write your own PDEs without modifying the library—see the Burgers and Hasegawa–Wakatani examples.
  • JAX acceleration and differentiability: When JAX is installed, simulations are JIT‑compiled and run on GPU/TPU. You can compute gradients of objectives with respect to simulation parameters and plug them into optimisation routines (via Optax).
  • CLI and configuration files: Run simulations from the command line using TOML configuration files. You can specify the filename without the .toml suffix for convenience.
  • Extensive documentation: A pedagogical guide introduces the mathematics and numerics behind each model, with worked examples and a gentle introduction for students and researchers.
  • Continuous integration and coverage: GitHub workflows run tests, build the docs, and report coverage to codecov.

Installation

pip install flexipde
# Or install with JAX support
pip install 'flexipde[jax]'

To install from source for development:

git clone https://github.com/uwplasma/flexipde.git
cd flexipde
pip install -e .[jax,testing,docs]

Models

The library comes with several built‑in models:

  • LinearAdvection: Advection of a scalar field with constant velocity. You can supply a velocity vector of arbitrary dimension.
  • Diffusion: Heat equation with configurable diffusivity.
  • IdealAlfvén: A toy model of shear Alfvén waves in 1D with two fields (velocity and magnetic field).
  • ResistiveMHD: A simplified resistive magnetohydrodynamics system with transverse velocity and magnetic field and resistivity parameter.
  • TwoFluid: Ion and electron densities advect independently with prescribed velocity vectors.
  • DriftKinetic: A drift–kinetic equation in 1D phase space (x,v) with a constant electric field.
  • VlasovTwoStream: A 1D Vlasov–Poisson solver modelling the two‑stream instability with Maxwellian streams. You can adjust the number of velocity grid points and thermal velocities.
  • Custom models: You can implement arbitrary PDE systems by subclassing :class:flexipde.models.base.PDEModel or by writing a simple Python function in a driver script. See the Hasegawa–Wakatani and Burgers examples for guidance.

Features

Run a 1D diffusion simulation from a TOML file:

flexipde examples/diffusion_1d
# automatically appends `.toml` if missing

Or run the same simulation from Python:

from flexipde.io import build_simulation

sim = build_simulation('examples/diffusion_1d.toml')
times, states = sim.run()

import matplotlib.pyplot as plt
plt.plot(sim.grid.coords[0], states[-1]['u'])
plt.show()

See the examples directory for more scripts illustrating custom models and optimisation tasks.

Documentation

The full documentation, including a tutorial introduction, API reference, and mathematical derivations, is available at https://uwplasma.github.io/flexipde. The docs are built with MkDocs and use MathJax to render equations.

Tests and coverage

To run the test suite with coverage:

pip install 'flexipde[testing]'
pytest --cov=flexipde tests

Manufactured solution tests are provided for each model to verify that spatial operators and PDE right‑hand sides are correct. We use functions like f(x,t)=cos(2x)*cos(3t) to ensure the gradients, divergences and Laplacians match analytic expressions.

Contributing

Contributions are welcome! Please open issues or pull requests on GitHub. We follow PEP8 coding style and maintain a high level of test coverage.

License

This project is licensed under the MIT license—see the LICENSE file for details.

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

flexipde-0.3.3.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

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

flexipde-0.3.3-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file flexipde-0.3.3.tar.gz.

File metadata

  • Download URL: flexipde-0.3.3.tar.gz
  • Upload date:
  • Size: 45.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flexipde-0.3.3.tar.gz
Algorithm Hash digest
SHA256 5f7336555ce3b1d403db71774a0a5f7db2a23d5a89b25dfec9613f953c5115eb
MD5 9cd0ae588cab2dee0b5007af6cd3d252
BLAKE2b-256 9a28b12ccf484a2d9c12b522b1bf53b6bd945a79e613ed2586fd579cf8d88609

See more details on using hashes here.

Provenance

The following attestation bundles were made for flexipde-0.3.3.tar.gz:

Publisher: release.yml on uwplasma/flexipde

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

File details

Details for the file flexipde-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: flexipde-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flexipde-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bc0a830ab8a2f0c3a8382a772638c3f32ef4f9d99baf78511f517d58f0eaa0e2
MD5 b9ffa201455bf5696242f31a3c66d865
BLAKE2b-256 703915d96b8f4605448459f72767cc4ab786457a1076756a42089b1c256c1d94

See more details on using hashes here.

Provenance

The following attestation bundles were made for flexipde-0.3.3-py3-none-any.whl:

Publisher: release.yml on uwplasma/flexipde

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

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