Metadata-Version: 2.4
Name: llgs-simulation
Version: 0.1.0
Summary: Spin-dynamics simulations using the Landau-Lifshitz-Gilbert-Slonczewski equation
Author: Ian Chang
License: MIT License
        
        Copyright (c) 2026 Ian Chang
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/ianchung621/LLGS-simulation
Project-URL: Repository, https://github.com/ianchung621/LLGS-simulation.git
Project-URL: Issues, https://github.com/ianchung621/LLGS-simulation/issues
Keywords: spintronics,LLGS,magnetism,spin dynamics,simulation
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: h5py
Requires-Dist: matplotlib
Requires-Dist: numba
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: tqdm
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Dynamic: license-file

# LLGS Simulation

[![Tests](https://github.com/ianchung621/LLGS-simulation/actions/workflows/test.yml/badge.svg)](https://github.com/ianchung621/LLGS-simulation/actions/workflows/test.yml)
[![Coverage](https://codecov.io/gh/ianchung621/LLGS-simulation/branch/main/graph/badge.svg)](https://codecov.io/gh/ianchung621/LLGS-simulation)
![Python](https://img.shields.io/badge/python-3.9--3.13-blue)
![OS](https://img.shields.io/badge/OS-Linux%20%7C%20macOS%20%7C%20Windows-lightgrey)

This package provides tools for simulating and analyzing spin dynamics using
the Landau-Lifshitz-Gilbert-Slonczewski (LLGS) equation on two-dimensional
lattices. It supports antiferromagnetic exchange, DMI, anisotropy, external
fields, and spin-orbit torque.

## Install

LLGS Simulation requires Python 3.9 or newer:

```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install llgs-simulation
```

## Example Usage

Below is a step-by-step demonstration of how to use the repository to simulate spin dynamics and visualize the results.

### 1. Creating a Lattice Object

The `lattice.py` module allows you to define a spin lattice. Here's an example of creating a hexagonal lattice:

```python
import numpy as np

from llgs import Lattice_2D

honeycomb = Lattice_2D(
    n_a=10,
    n_b=8,
    n_site=2,
    r_a=[np.sqrt(3), 0],
    r_b=[0.5 * np.sqrt(3), 1.5],
    r_site=[
        [1 / 3, 1 / 3],
        [2 / 3, 2 / 3],
    ],
)
honeycomb.plot(draw_unitcell=True)
```

![Honeycomb lattice](https://raw.githubusercontent.com/ianchung621/LLGS-simulation/main/doc/lattice.png)

### 2. Initializing Spins

Initialize the spins on the lattice to a desired configuration:

```python
zigzag_config = {
    "b % 2 == 0": np.array([1, 0, 0]),
    "b % 2 == 1": np.array([-1, 0, 0]),
}
honeycomb.initialize_spin(zigzag_config)
honeycomb.plot()
```

![Initialized spins](https://raw.githubusercontent.com/ianchung621/LLGS-simulation/main/doc/spin_initialized.png)

### 3. Setting Up Parameters for Simulation

Build the exchange matrix from exact lattice bonds, then configure the
simulation. Each bond is `(source_site, target_site, cell_offset, coupling)`;
the reverse bond is added automatically.

```python
from llgs import LLGS_Simulation_2D, build_exchange

J1 = -11.2  # exchange-matrix coefficient, Tesla
exchange_bonds = [
    (0, 1, (0, 0), J1),
    (1, 0, (0, 1), J1),
    (1, 0, (1, 0), J1),
]
H_E = build_exchange(honeycomb, exchange_bonds)
H_ext = np.array([5.0, 5.0, 0.0])

sim = LLGS_Simulation_2D(
    honeycomb,
    H_E=H_E,
    H_ext=H_ext,
    alpha=0.1,
    H_para=0.086,
    H_perp=1.812,
    io_foldername="Data",
    io_filename="results_RK4",
    method="RK4",
)
```

`H_E` may be a dense `(N, N)` array or a SciPy sparse matrix. For sparse DMI,
pass `H_DMI` as a sequence of three sparse `(N, N)` matrices, one for each
Cartesian component. Dense DMI remains a `(3, N, N)` array. Use
`matrix_type="sparse"` to convert both fields to CSR matrices,
`matrix_type="dense"` to convert them to NumPy arrays, or the default
`matrix_type="auto"` to preserve the supplied representation.

### 4. Running the Simulation

Run the simulation for a specified number of steps and save the results:

```python
sim.evolve(dt=2e-4, max_iters=1000)
```

### 5. Visualizing Spin Dynamics

The `read_result.py` module reads the simulation results and creates visualizations. Here is how you can animate the spin dynamics:

```python
from llgs import ReadResult

results = ReadResult("Data/results_RK4.h5")

results.animate(period=50, save_fn="Data/movie.gif")
```

### Example Output

Here is an example of how the animation might look:

![Spin dynamics animation](https://raw.githubusercontent.com/ianchung621/LLGS-simulation/main/doc/spin_animation.gif)

### Additional Details

For a comprehensive explanation of the methods, equations, and parameters,
see the [example notebook](https://github.com/ianchung621/LLGS-simulation/blob/main/example.ipynb).

This notebook includes:
- Detailed descriptions of lattice construction.
- Explanation of the exchange field matrix and LLGS simulation.
- Mathematical derivations and parameter setups.
