Skip to content

Endregb/tokamak-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Numerical Methods for Charged Particle Motion

TMA4215 – Numerical Mathematics | NTNU Fall 2025

GitHub Pages Python License

Symplectic integrators for simulating charged particle trajectories in magnetic fields, with applications to tokamak fusion reactor geometries.

🎬 Tokamak Trajectory Animations

Multistep Method (9th Order)

Multistep method animation

Symmetric 9-step multistep integrator with standard initial velocityβ€”showing the banana orbit trajectory used throughout the project


Runge-Kutta 4 Method

RK4 method animation

Fourth-order Runge-Kutta with enhanced initial velocity ($4\times v_2$ component)β€”complete toroidal orbit for visualization clarity

🍌 Guiding Center Banana Orbits

Guiding center trajectories

Guiding center trajectories showing characteristic banana orbits in tokamak geometry using standard initial velocity

These banana orbits represent the actual particle behavior studied throughout this project. The guiding center approximation reduces the 6D phase space to 4D by separating fast cyclotron motion from slow drift dynamics, enabling 100-200Γ— larger timesteps while capturing the essential physicsβ€”a key validation of our tokamak simulation accuracy.

🌐 Magnetic Field Visualization

Magnetic field visualization

Magnetic field structures for 2D system (top) and tokamak geometry (bottom)

πŸ“Š Numerical Method Performance

Energy Errors Over Time

Energy errors comparison

Comparison of energy errors for Boris, RK4, and Multistep methods in both 2D and tokamak systems

Key Observations:

  • Boris & Multistep (Symplectic): Bounded errors over long-term simulations
  • RK4: Higher accuracy short-term but growing errors over time
  • Periodic Behavior: All methods show oscillating errors due to trajectory periodicity

πŸ”¬ Project Overview

This project implements and analyzes numerical integration methods for charged particle motion in magnetic fields:

Implemented Methods

  1. Boris Method (2nd order, symplectic)

    • Specialized velocity-Verlet variant for Lorentz force
    • Excellent long-term energy conservation
    • Industry standard for plasma physics simulations
  2. Runge-Kutta 4 (4th order, non-symplectic)

    • High accuracy for smooth problems
    • Energy drift over long timescales
    • Reference method for comparison
  3. 9-Step Symmetric Multistep (4th order, symplectic)

    • Implicit multistep with analytical Jacobians
    • $\sim 10\times$ speedup vs autograd
    • Excellent energy conservation (validates Hairer-Lubich theorem 3.2)
  4. ABM4 Guiding Center (4th order predictor-corrector)

    • Adams-Bashforth-Moulton for reduced 4D guiding center equations
    • Enables $100\text{-}200\times$ larger timesteps
    • Exact energy conservation in tokamak ($U=0$)

Physical Systems

  • 2D Magnetic Field: Simplified model with analytical solutions
  • Tokamak Geometry: Realistic fusion reactor magnetic field configuration
  • Guiding Center Approximation: Reduced model separating fast gyromotion from slow drift

🎯 Key Results

Convergence Analysis

  • βœ… Boris: 2nd order convergence confirmed
  • βœ… RK4: 4th order convergence confirmed
  • βœ… Multistep: 4th order convergence confirmed
  • βœ… All methods validated against DOP853 reference solutions

Energy Conservation

  • Symplectic methods: Energy errors bounded within $O(h^p)$ over $O(h^{-p-2})$ timescales
  • RK4: Linear energy drift observed
  • Guiding center: Exact conservation in tokamak (theoretical prediction confirmed)

Performance

  • Multistep with analytical Jacobians: $\sim 10\times$ faster than autograd version
  • Guiding center: $100\text{-}200\times$ larger timesteps while maintaining accuracy
  • Work-precision diagrams show multistep as most efficient method

πŸš€ Getting Started

Installation

git clone https://github.com/Endregb/tma4215-project2.git
cd tma4215-project2
pip install -r requirements.txt

Running Simulations

# Run individual system examples
python examples/run_2d_system.py
python examples/run_tokamak.py

# Compare all methods
python examples/compare_methods.py

# Generate animations (saves to results/animations/)
python examples/create_animations.py

Jupyter Notebook

Open notebooks/notebook.ipynb for complete analysis including:

  • Trajectory visualizations
  • Error analysis and convergence studies
  • Energy conservation plots
  • Work-precision diagrams
  • Guiding center implementation

πŸ“ Project Structure

tma4215-project2/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ methods/          # Numerical integrators
β”‚   β”‚   β”œβ”€β”€ boris.py      # Boris method
β”‚   β”‚   β”œβ”€β”€ rk4.py        # Runge-Kutta 4
β”‚   β”‚   β”œβ”€β”€ multistep.py  # 9-step symmetric method
β”‚   β”‚   └── abm4.py       # ABM4 guiding center
β”‚   β”œβ”€β”€ systems/          # Physical systems
β”‚   β”‚   β”œβ”€β”€ system_2d.py       # 2D magnetic field
β”‚   β”‚   β”œβ”€β”€ tokamak.py         # Tokamak geometry
β”‚   β”‚   └── guiding_center.py  # Guiding center models
β”‚   └── utils/            # Analysis and plotting
β”‚       β”œβ”€β”€ analysis.py   # Error computation, reference solutions
β”‚       └── plotting.py   # Visualization functions
β”œβ”€β”€ examples/             # Example scripts
β”œβ”€β”€ notebooks/            # Jupyter notebooks
β”œβ”€β”€ tests/               # Unit tests
└── results/             # Generated figures and animations

πŸ§ͺ Testing

pytest tests/

Tests include:

  • Method accuracy validation
  • Energy conservation checks
  • Convergence order verification
  • Guiding center reference solutions

πŸ“š References

  • Hairer & Lubich (2017) - Energy conservation of symplectic methods
  • Qin & Guan (2008) - Symplectic methods for charged particle dynamics
  • Hoppe, Iantchenko & Stranberg (2015) - Guiding center dynamics in tokamaks

πŸ‘₯ Contributors

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Links

About

Simulation of charged particles in a tokamak.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published