pyMagCalc is a Python package for performing Linear Spin-Wave Theory (LSWT) calculations. It allows users to define a spin model (Hamiltonian, magnetic structure, lattice) and compute spin-wave dispersion relations and dynamic structure factors S(Q,ω).
- Diffraction Physics: Calculates spin-wave dispersion and neutron scattering intensity (S(Q,ω)) with magnetic form factor and polarization factor corrections.
- Energy Minimization: Numerically finds the classical magnetic ground state by minimizing the Hamiltonian energy, supporting both simple and complex (e.g., canted) structures.
- 3D Visualization: Visualizes the magnetic structure in 3D with scaled spins and correct aspect ratios.
- Modular Architecture: Separation of concerns with
MagCalccore logic, linear algebra utilities, and model definitions. - Symbolic Hamiltonian: Generates symbolic quadratic boson Hamiltonians using
SymPyfor arbitrary spin interactions. - Numerical Engine: Efficient numerical evaluation using
NumPyandmultiprocessingfor parallel q-point calculations. - Caching System: Caches computationally expensive symbolic Hamiltonian diagonalization to disk for faster re-runs.
- Centralized Outputs: Automatically saves all generated plots and data to structured directories (
examples/plots/andcache/data/). - Flexible Inputs: Supports declarative YAML configurations (validated against schema) or Python-based model definitions.
magcalc/: Core Python package.core.py: MainMagCalcclass and calculation logic.generic_model.py:GenericSpinModelfor YAML-based model loading.linalg.py: Matrix operations and Bogoliubov transformation utilities.config_loader.py: Utilities for loading and validating configurations.
scripts/: Executable scripts for running calculations and inspecting models (e.g.,run_magcalc.py).examples/: Sample data and scripts for various materials.KFe3J/: KFe3(OH)6(SO4)2 (Jarosite) - Kagome antiferromagnet.aCVO/: alpha-Cu2V2O7 - Honeycomb-like antiferromagnet with Dzyaloshinskii-Moriya interactions.plots/: Centralized directory where all example scripts save their output plots.
tests/: Unit and integration tests ensuring package reliability.
- Python (3.8+)
- NumPy
- SciPy
- SymPy
- Matplotlib
- tqdm
- PyYAML
- ASE (Atomistic Simulation Environment, used for CIF file reading)
- pytest (for testing)
- Clone the repository.
- Install the package in editable mode (recommended for development):
This installs the
pip install -e .magcalccommand-line tool and all dependencies.
The new command-line interface makes it easy to manage calculations.
Create a template configuration file:
magcalc init my_config.yamlCheck if your configuration file is valid:
magcalc validate my_config.yamlRun dispersion, S(Q,w), and plotting as defined in the config:
magcalc run my_config.yamlThe recommended way to run examples is via the CLI using the modern configuration files:
# Run the Jarosite (KFe3J) example
magcalc run examples/KFe3J/config_modern.yaml
# Run the CVO example
magcalc run examples/aCVO/config_modern.yamlPlots are automatically saved to examples/plots/. You can toggle on-screen display using the show_plot option in the config.
For custom workflows or parameter scans, you can use the library directly:
import magcalc as mc
from magcalc.generic_model import GenericSpinModel
import yaml
# 1. Load Model from YAML
with open("examples/KFe3J/config_modern.yaml") as f:
config = yaml.safe_load(f)
model = GenericSpinModel(config)
# 2. Initialize Calculator
calc = mc.MagCalc(spin_model_module=model, spin_magnitude=2.5, cache_mode='auto')
# 3. Minimize Energy
# Use a smart initial guess to avoid local minima
x0 = ...
min_res = calc.minimize_energy(x0=x0)
calc.sm.set_magnetic_structure(min_res.x)
# 4. Visualize
mc.plot_magnetic_structure(calc.sm.atom_pos(), min_res.x, show_plot=True)
# 5. Calculate Dispersion
# ...The core of pyMagCalc is the declarative YAML configuration (e.g., config_modern.yaml). It defines:
- Structure: Lattice vectors and atoms.
-
Interactions: Heisenberg (
$J$ ), DM ($D$ ), Single-Ion Anisotropy, etc. -
Minimization: Initial guess (
initial_configuration) and method. -
Plotting: Options like
show_plot,plot_structure, and axis limits.
examples/KFe3J/: Kagome Antiferromagnet (Jarosite).- Uses
config_modern.yamlfor a fully declarative workflow. - Demonstrates
initial_configurationfor handling complex ground states (120-degree structure).
- Uses
examples/aCVO/: 1D Chain / Honeycomb (Cu2V2O7).- Demonstrates handling of imaginary eigenvalues via correct ground state finding.
- Features complex Dzyaloshinskii-Moriya interactions.
Run the test suite using pytest from the project root:
pytest