Pymatgen Diffusion Analysis

2025.11.15 · active · verified Fri Apr 17

Pymatgen-analysis-diffusion is an add-on module for the Materials Project's `pymatgen` library, specializing in the analysis of diffusion in solid-state materials. It provides tools for calculating mean squared displacement (MSD), determining diffusion coefficients from ab-initio molecular dynamics (AIMD) simulations, and analyzing diffusion paths. The current version is 2025.11.15, and releases are typically date-based, often aligning with updates to the core `pymatgen` library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `MSDAnalyzer` to calculate the diffusivity of a species from a list of `pymatgen.core.Structure` objects representing a trajectory. It creates a mock trajectory, initializes the analyzer, and then computes the diffusion coefficient.

from pymatgen.core import Structure, Species, Lattice
from pymatgen_analysis_diffusion.aimd.msd import MSDAnalyzer
import numpy as np

# 1. Create dummy structures to simulate a trajectory
lattice = Lattice.cubic(10.0)
species_list = [Species("Li"), Species("O")] * 4 # 8 atoms total

# Generate a list of structures for the trajectory
structures = []
for i in range(5):
    # Slightly perturb coordinates for each step
    coords = np.random.rand(8, 3) + i * 0.05 # Simulate some movement
    s = Structure(lattice, species_list, coords)
    structures.append(s)

# Define simulation parameters
time_step = 2.0  # Time step between structures in picoseconds (ps)
step_skip = 1    # Use every step

# 2. Initialize MSDAnalyzer for a specific species (e.g., Li)
li_species = [Species("Li")]

msd_analyzer = MSDAnalyzer(
    structures=structures,
    species=li_species,
    time_step=time_step,
    step_skip=step_skip
)

# 3. Calculate diffusivity
temperature = 300 # Kelvin
diffusivity_data = msd_analyzer.get_diffusivity(temperature=temperature, initial_drift_correct=True)

print(f"Calculated diffusivity for Li at {temperature}K:")
print(f"  Diffusivity: {diffusivity_data['diffusivity']:.2e} cm^2/s")
print(f"  Error: {diffusivity_data['error']:.2e} cm^2/s")

# Optional: Get MSD data for plotting
# time_points, msd_values = msd_analyzer.get_msd_plot()
# print(f"First 5 MSD values: {msd_values[:5]}")

view raw JSON →