torchspin#
Simulate and fit EPR spectra in Python. torchspin is a PyTorch
reimplementation of EasySpin’s simulators: powder and
single-crystal CW EPR (pepper), solution and slow-motion spectra (garlic,
chili), ENDOR (salt), pulse EPR (saffron), magnetometry (curry) and
least-squares fitting (esfit), with EasySpin’s parameter names, units and
conventions. Because it is built on PyTorch, spectra are differentiable with
respect to the spin Hamiltonian parameters and can run on a GPU.
Start here#
Install:
pip install torchspin(details and extras in Getting started).Simulate a first spectrum: Solid-state (powder) spectra, or the quickstart tutorial.
Fit a measured spectrum: Fitting spectra.
Use gradients or a GPU: Gradients and differentiable simulation.
from torchspin import SpinSystem, Experiment, pepper
cu = SpinSystem(S=0.5, g=[2.05, 2.05, 2.25], Nucs='63Cu', A=[50, 50, 520], HStrain=[60, 60, 90])
B, spc = pepper(cu, Experiment(mwFreq=9.5, Range=[260, 360])) # field in mT, derivative spectrum
What is here#
User guide: short task pages (powder, solution and slow-motion, ENDOR and pulse, fitting, gradients, orientation grids, ligand-field tools) with runnable examples.
Tutorials: executed notebooks, from the quickstart to real-world fitting.
API reference: every public function and class with the parameters it accepts.
Troubleshooting and reference: pitfalls, known limitations per simulator, and the change log.
Tutorials#
Notebook |
What it shows |
|---|---|
Spin systems, Hamiltonians, powder ( |
|
|
|
|
|
MATLAB/EasySpin vs torchspin timings (threads, GPU, worker processes) from the 2026-09 campaign, plus live timing |
|
Bad starting guesses, loss-landscape slices, simplex traps vs global search, 5-parameter stress test |
|
Rhombic Cu(II) case study: simplex, Levenberg–Marquardt and global head to head |
|
Overlays against stored EasySpin outputs (pepper, garlic, chili, saffron HYSCORE, cardamom) with cosine similarities |
|
Simulate, add noise, fit with every |
|
Interactive panel ( |
Running on a workstation#
CPU: torchspin uses all cores through thread pools inside the simulators (
torch.set_num_threads(n)caps it). Population-based fits addFitOptions(n_workers='auto')(worker processes for the allocated cores, at most 32, about 0.5 GB of memory each and never more than half the free memory;pip install "torchspin[fit]"for models defined in notebooks).GPU:
Options(device='cuda'); worthwhile for autograd and batched fitting, not for single simulations.Thread and process budgets:
torchspin.parallel(cpu_budget(),thread_budget(n),native_thread_env(n)) for code that runs torchspin inside its own worker pools.Progress and early stop:
FitOptions(progress='text', max_time=…); a kernel interrupt returns the best fit so far.
Benchmarks and tests#
Benchmark report: MATLAB/EasySpin vs torchspin on nine workloads, before and after the 2026-09 optimizations. The verified matched-host campaign (five replicates per point, with a per-workload device audit) supersedes it wherever the two disagree.
python -m pytestruns the test suite (about 5800 tests, including comparisons against stored EasySpin/MATLAB references intests/data; no MATLAB installation needed).