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,ω).
- pyMagCalc Studio: Interactive modern web GUI for designing models from CIF files and symmetry-based bonding rules. Provides a seamless Design -> Save -> Run workflow.
-
Symmetry-Aware Mechanics: Automatically propagates Heisenberg (
$J$ ), DM ($D$ ), Anisotropic Exchange ($T$ ), and Kitaev ($K$ ) rules across the crystal using space-group symmetry operators (viapymatgenandspglib). - Robust CIF Import: Imports crystal structures from CIF files, automatically detecting symmetry and reducing to unique Wyckoff positions.
-
Diffraction Physics: Calculates spin-wave dispersion and dynamic structure factors
$S(Q,\omega)$ with magnetic form factor and polarization factor corrections. - Energy Minimization: Numerically finds the classical magnetic ground state by minimizing the Hamiltonian energy, supporting parallel multistart and early stopping for robust convergence.
- 3D Visualization: Visualizes the magnetic structure in 3D with scaled spins, DM vectors (arrows), and orientation guides.
-
Symbolic Engine: Generates symbolic quadratic boson Hamiltonians using
SymPyfor arbitrary spin interactions. -
Numerical Engine: Efficient numerical evaluation using
NumPyandmultiprocessingfor parallel q-point calculations. -
Flexible Caching: Supports disk caching (
auto,r,w) for expensive symbolic matrices, ornonefor purely in-memory execution. -
Data Export (CSV): Export results to
.csvor.npzfiles for external analysis. - Validated Configurations: Supports declarative YAML configurations validated against a robust Pydantic schema for immediate error feedback.
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.schema.py: Pydantic V2 models for robust configuration validation.
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 (>=1.20)
- SciPy (>=1.7.0)
- SymPy (>=1.9)
- Matplotlib (>=3.4.0)
- tqdm (>=4.60.0)
- PyYAML (>=5.4)
- ASE (>=3.22.0, for CIF file reading)
- pytest (>=7.0.0, 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 pyMagCalc Studio is a modern web application designed to simplify the creation of complex spin models using a "pure" (symmetry-based) approach.
- CIF Integration: Load crystal structures directly from CIF files.
- Symmetry-Aware Bonding: Define interaction rules (Heisenberg, DM, Anisotropic Exchange) that are automatically propagated by space group symmetry.
- Real-time Feedback: Export to YAML or save expanded configurations directly to your workspace.
The easiest way to start the application is using the One-Click Launcher:
./start_magcalc.shThis script will:
- Kill any existing processes on ports 8000/5173.
- Start the Python backend and React frontend.
- Automatically open your browser to http://localhost:5173/.
- Stop all services cleanly when you press
Ctrl+C.
Note: Symmetry-generated interactions are currently in active development and may not fully propagate as expected. Please double-check your expanded configuration.
The designer facilitates a seamless Design -> Save -> Run workflow, where the generated config_designer.yaml can be executed immediately using magcalc run.
You can also run pyMagCalc Studio as a standalone native application on macOS (wrapper around the web app).
-
Prerequisites:
- Build the frontend:
cd gui npm install npm run build cd ..
- Install additional Python dependencies:
pip install -r requirements.txt
- Build the frontend:
-
Launch: You can use the helper script:
./run_native.sh
Or run manually:
python gui/native_app.py
The 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='none')
# 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$ ), Anisotropic Exchange ($K, \Gamma, \Gamma'$ ), Kitaev, and Single-Ion Anisotropy (SIA) with arbitrary axes. -
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:
pytestThis codebase was developed with the assistance of Google Gemini, an advanced agentic AI coding assistant.