Skip to content

Instantly share code, notes, and snippets.

@mducle
Last active March 16, 2026 22:13
Show Gist options
  • Select an option

  • Save mducle/d5eb536bc7f1b2128abacdd1e18cc17a to your computer and use it in GitHub Desktop.

Select an option

Save mducle/d5eb536bc7f1b2128abacdd1e18cc17a to your computer and use it in GitHub Desktop.
""" Antiferromagnetic chain example """
from multiprocessing.spawn import freeze_support
from pyspinw.coupling import HeisenbergCoupling
from pyspinw.hamiltonian import Hamiltonian
from pyspinw.interface import (generate_exchanges, filter, axis_anisotropies, generate_structure,
generate_helical_structure)
from pyspinw.path import Path, Path1D
from pyspinw.site import LatticeSite
from pyspinw.sample import Powder
from pyspinw.symmetry.supercell import TrivialSupercell, SummationSupercell, CommensuratePropagationVector
from pyspinw.symmetry.unitcell import UnitCell
from pyspinw.structures import Structure
from pyspinw.legacy.genmagstr import genmagstr
from math import sqrt
import sys
from pyspinw.debug_plot import debug_plot
import matplotlib.pyplot as plt
if __name__ == "__main__":
"""Reproduces Tutorial 2: https://spinw.org/tutorials/02tutorial"""
freeze_support()
########################################
# Parameters for all examples
use_rust = "py" not in sys.argv[1] if len(sys.argv) > 1 else True
use_rotating = False
########################################
unit_cell = UnitCell(3, 8, 8)
sites = generate_helical_structure(unit_cell, positions=[[0,0,0]], moments=[[0,1,0]],
perpendicular=[0,0,1], propagation_vector=[0.5, 0, 0], names=["MCu1"])
exchanges = generate_exchanges(sites=sites,
max_distance=3.1,
coupling_type=HeisenbergCoupling,
j=1)
hamiltonian = Hamiltonian(sites, exchanges)
hamiltonian.print_summary()
path = Path([[0,0,0], [1,0,0]])
hamiltonian.spaghetti_plot(path, scale='log', show=False, use_rust=use_rust, use_rotating=use_rotating)
""" Antiferromagnetic chain example with applied magnetic field """
unit_cell = UnitCell(4,6,6)
sites = generate_structure(unit_cell, positions=[[0,0,0], [0.5,0,0]], moments=[[0,0,1], [0,0,-1]], names=['X', 'Y'])
exchanges = generate_exchanges(sites=sites,
bond=1,
coupling_type=HeisenbergCoupling,
j=1,
direction_filter=filter([1,0,0], symmetric=True))
anisotropies = axis_anisotropies(sites, -0.1)
hamiltonian = Hamiltonian(sites, exchanges, anisotropies)
hamiltonian.print_summary()
path = Path([[0,0,0], [2,0,0]])
hamiltonian.spaghetti_plot(path, field=[0,0,7], show=False, use_rust=use_rust)
""" Ferromagnetic chain example """
unit_cell = UnitCell(1,1,1)
only_site = LatticeSite(0, 0, 0, 0,0,1, name="X")
s = Structure([only_site], unit_cell=unit_cell)
exchanges = generate_exchanges(sites=[only_site],
unit_cell=unit_cell,
max_distance=1.1,
coupling_type=HeisenbergCoupling,
j=-1,
direction_filter=filter([1,0,0]))
hamiltonian = Hamiltonian(s, exchanges)
path = Path([[0,0,0], [1,0,0]])
hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust)
""" Kagome Antiferromagnet example """
unit_cell = UnitCell(6, 6, 10, gamma=120)
s = generate_structure(unit_cell, positions=[[0.5,0,0], [0,0.5,0], [0.5,0.5,0]], moments=[[1,2,0], [-2,-1,0], [1,-1,0]],
names=['X','Y','Z'], magnitudes=[1,1,1], moments_unit='lu')
j1 = generate_exchanges(sites=s, bond=1, j=1)
j2 = generate_exchanges(sites=s, bond=2, j=0.11)
exchanges = j1 + j2
hamiltonian = Hamiltonian(s, exchanges)
hamiltonian.print_summary()
path = Path([[-0.5,0,0], [0,0,0], [0.5,0.5,0]])
hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust)
""" Kagome Ferromagnet example """
unit_cell = UnitCell(6,6,5, gamma=120)
x = LatticeSite(0.5, 0, 0, 0, 1, 0, name="X")
y = LatticeSite(0, 0.5, 0, 0, 1, 0, name="Y")
z = LatticeSite(0.5, 0.5, 0, 0, 1, 0, name="Z")
sites = [x, y, z]
s = Structure(sites, unit_cell=unit_cell, supercell=TrivialSupercell(scaling=(1,1,1)))
exchanges = generate_exchanges(sites=[x, y, z],
unit_cell=unit_cell,
max_distance=4.,
coupling_type=HeisenbergCoupling,
j=-1)
hamiltonian = Hamiltonian(s, exchanges)
hamiltonian.print_summary()
path = Path([[-0.5,0,0], [0,0,0], [0.5,0.5,0]])
hamiltonian.energy_plot(path, show=False, use_rust=use_rust)
#sample = Powder(hamiltonian)
#path1D = Path1D(0.1, 2.5, n_points=100)
#sample.show_spectrum(path1D, n_energy_bins=250)
""" Kagome 3x3 Antiferromagnet example """
unit_cell = UnitCell(6, 6, 40, gamma=120)
s = generate_helical_structure(unit_cell, positions=[[0.5,0,0], [0,0.5,0], [0.5,0.5,0]],
moments=[[0,1,0], [0,1,0], [-1,-1,0]], magnitudes=[1,1,1], names=['X', 'Y', 'Z'],
moments_unit='lu', perpendicular=[0,0,1], propagation_vector=[-1./3., -1./3., 0])
exchanges = generate_exchanges(sites=s,
bond=1,
coupling_type=HeisenbergCoupling,
j=1)
hamiltonian = Hamiltonian(s, exchanges)
hamiltonian.print_summary()
#from pyspinw.gui.viewer import show_hamiltonian
#show_hamiltonian(hamiltonian)
path = Path([[-0.5,0,0], [0,0,0], [0.5,0.5,0]])
fig = hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust, use_rotating=use_rotating)
fig.axes[0].set_ylim(0, 3)
fig.axes[1].set_ylim(0, 1)
#plt.show()
""" Square-lattice Antiferromagnet example """
unit_cell = UnitCell(3, 3, 9)
sites = [LatticeSite(0, 0, 0, 1, 0, 0, name="X")]
k = CommensuratePropagationVector(0.5, 0.5, 0)
s = Structure(sites, unit_cell, supercell=SummationSupercell(propagation_vectors=[k]))
j1 = generate_exchanges(sites, unit_cell, min_distance=0, max_distance=3.1, coupling_type=HeisenbergCoupling, j=59.65)
j2 = generate_exchanges(sites, unit_cell, min_distance=4, max_distance=4.3, coupling_type=HeisenbergCoupling, j=-3.75)
j3 = generate_exchanges(sites, unit_cell, min_distance=5, max_distance=6.1, coupling_type=HeisenbergCoupling, j=1)
exchanges = j1 + j2 + j3
hamiltonian = Hamiltonian(s, exchanges)
hamiltonian.print_summary()
path = Path([[3/4, 1/4, 0], [1/2, 1/2, 0], [1/2, 0, 0], [3/4, 1/4, 0], [1, 0, 0], [1/2, 0, 0]], n_points_per_segment=51)
fig = hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust)
fig.axes[0].set_ylim(0, 300)
fig.axes[1].set_ylim(0, 5)
#plt.show()
""" Triangular Antiferromagnet example """
unit_cell = UnitCell(3, 3, 4, gamma=120)
sites = generate_helical_structure(unit_cell, positions=[[0,0,0]], moments=[[0,1,0]], magnitudes=[3./2], names=['X'],
perpendicular=[0,0,1], propagation_vector=[1./3., 1./3., 0])
exchanges = generate_exchanges(sites=sites,
bond=1,
coupling_type=HeisenbergCoupling,
j=1)
anisotropies = axis_anisotropies(sites, 0.2)
hamiltonian = Hamiltonian(sites, exchanges, anisotropies)
hamiltonian.print_summary()
path = Path([[0,0,0], [1,1,0]], n_points_per_segment=401)
fig = hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust, use_rotating=use_rotating)
fig.axes[0].set_ylim(0, 4)
fig.axes[1].set_ylim(0, 3)
#plt.show()
""" Triangular Antiferromagnet example """
unit_cell = UnitCell(3, 3, 8, gamma=120)
sites = generate_helical_structure(unit_cell, positions=[[0, 0, 0], [0, 0, 0.5]], moments=[[0, 1, 0], [0, 1, 0]],
magnitudes=[3./2, 3./2], propagation_vector=[1./3, 1./3, 0], perpendicular=[0, 0, 1])
exchanges = generate_exchanges(sites=sites, bond=1, coupling_type=HeisenbergCoupling, j=1) \
+ generate_exchanges(sites=sites, bond=2, coupling_type=HeisenbergCoupling, j=-0.1)
anisotropies = axis_anisotropies(sites, 0.2)
hamiltonian = Hamiltonian(sites, exchanges, anisotropies)
hamiltonian.print_summary()
path = Path([[0,0,0], [1,1,0]], n_points_per_segment=401)
fig = hamiltonian.spaghetti_plot(path, show=False, use_rust=use_rust, use_rotating=use_rotating)
fig.axes[0].set_ylim(0, 7)
fig.axes[1].set_ylim(0, 5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment