Skip to main content

Kinetic Monte Carlo Simulation using Python (kMCpy)

Project description

Logo

GitHub release Documentation Status CI Status PyPI Version Requires Python 3.11+ Paper

Kinetic Monte Carlo Simulation with Python (kMCpy) is an open-source Python package for studying ion diffusion using the kinetic Monte Carlo (kMC) technique. It offers a comprehensive Python-based approach to compute kinetic properties, suitable for research, development, and prediction of new functional materials.

Key features include a local cluster expansion model toolkit, a rejection-free kinetic Monte Carlo (rf-kMC) solver, and tools to extract ion transport properties like diffusivities and conductivities. The local cluster expansion model toolkit facilitates model fitting from ab initio or empirical barrier calculations. Post-training, the local cluster expansion model can compute migration barriers in crystalline materials within the transition state theory.

Advantages of using kMCpy:

  1. Written entirely in Python with a modular design, promoting developer-centricity and easy feature addition.
  2. Cross-platform compatibility, supporting Windows, macOS, and Linux.
  3. Performance-optimized kMC routines using Numba, resulting in significant speed improvements.

[!WARNING] kMCpy is under active development.

APIs and serialized file formats may change before a stable release. Check the release notes and current documentation when updating existing workflows.

Release notes are maintained in the changelog.

Installation

Install with pip

You can install the latest PyPI release directly into a Python environment:

pip install kmcpy

Using a virtual environment is recommended:

python -m venv kmcpy-env
source kmcpy-env/bin/activate  # On Windows use `kmcpy-env\Scripts\activate`
pip install kmcpy

Install with uv pip

If you manage Python environments with uv, use the same PyPI package:

uv venv kmcpy-env
source kmcpy-env/bin/activate
uv pip install kmcpy

Install with Conda

Use Conda to create the Python environment, then install the PyPI package inside that environment:

conda create -n kmcpy python=3.11 pip
conda activate kmcpy
python -m pip install kmcpy

Install from source

You can install kMCpy from source using either pip or uv. First, clone the repository and navigate to its root directory.

To install normally with pip:

pip install .

For development with pip:

pip install -e ".[dev]"

For development with uv:

uv sync
uv sync --extra dev
uv pip install -e .

[!WARNING] Windows users

Windows users (not applicable to WSL) need to install Microsoft C++ build tools for pymatgen.

Build documentation

You can access the documentation at https://kmcpy.readthedocs.io/. However, if you want to build the documentation locally, you can do so by following these steps:

uv sync --extra doc
python scripts/build_doc.py

Quickstart

Run a minimal end-to-end simulation with bundled example files:

uv sync
uv run python -c "from kmcpy.simulator.config import Configuration; Configuration.help_fields()"
uv run python example/minimal_example.py

Configuration routes arguments into two groups:

  1. system fields define what you simulate after loader inputs have been read.
  2. runtime fields define how you simulate (temperature, passes, random seed).

If you pass an unknown keyword, kMCpy raises a clear error and points to Configuration.help_fields().

Unit conventions are explicit in code via kmcpy.units, Configuration.field_units(), and tracker.result_units. Common units are: barriers in meV, rates/probabilities in Hz, time in s, distance in Angstrom, diffusivity in cm^2/s, and conductivity in mS/cm.

Run kMCpy

API usage

You can run kMC through API. See the example directory for scripts and notebook workflows covering setup, event generation, and simulations.

For a one-call simulation:

from kmcpy import Configuration, run

config = Configuration.from_file("input.yaml")
tracker = run(config)

You can also attach custom property callbacks during a run:

from kmcpy.simulator.kmc import KMC

kmc = KMC.from_config(config)

def custom_property(state, step, sim_time):
    occupied = sum(1 for occ in state.occupations if occ < 0)
    return occupied / len(state.occupations)

kmc.attach(custom_property, interval=100, name="occupied_fraction")
kmc.set_property_enabled("conductivity", False)  # Optional: disable selected built-in fields
tracker = kmc.run()

# Stored custom callback records
records = tracker.get_property_records("occupied_fraction")

Command line usage

A wrapper is provided if you want to run kMCpy from the command line.

  1. Generate a commented template input file:
kmcpy init --output input_template.yaml
  1. Edit the required file paths and simulation settings in input_template.yaml.

  2. Run the simulation:

run_kmc --input input_template.yaml

To print out all arguments, you can run:

run_kmc --help

Build local barrier models

For direct barrier logic, use LocalBarrierModel. It supports constant barriers, count rules, species-count rules, wildcard patterns, and exact catalog-style matches. Barrier values are in meV.

Constant barrier:

from kmcpy.models import LocalBarrierModel

model = LocalBarrierModel.constant_barrier(300.0)
model.to("model.json")

Species-count rule, for example "more than 3 Si in the local environment":

model = LocalBarrierModel(
    default_barrier=300.0,
    site_species={
        1: {-1: "P", 1: "Si"},
        2: {-1: "Si", 1: "P"},
        3: {-1: "Si", 1: "P"},
        4: {-1: "Al", 1: "Si"},
    },
)
model.add_species_count_rule(
    name="si_rich",
    sites="local_env",
    species="Si",
    min_count=4,
    barrier=420.0,
)
model.to("model.json")

Exact catalog-style match:

model = LocalBarrierModel.from_exact_entries([
    {
        "mobile_ion_indices": [0, 1],
        "local_env_indices": [1, 2, 3],
        "occupations": [1, -1, 1, -1],
        "properties": {"barrier": 250.0},
    }
])

Use the generated model file in simulation config by pointing to it:

model_file: "model.json"

Exact catalog-style data should be represented as LocalBarrierModel exact rules rather than a separate catalog model.

Citation

If you use kMCpy in your research, please cite it as follows:

@article{deng2022fundamental,
          title={Fundamental investigations on the sodium-ion transport properties of mixed polyanion solid-state battery electrolytes},
          author={Deng, Zeyu and Mishra, Tara P and Mahayoni, Eunike and Ma, Qianli and Tieu, Aaron Jue Kang and Guillon, Olivier and Chotard, Jean-No{\"e}l and Seznec, Vincent and Cheetham, Anthony K and Masquelier, Christian and Gautam, Gopalakrishnan Sai and Canepa, Pieremanuele},
          journal={Nature Communications},
          volume={13},
          number={1},
          pages={1--14},
          year={2022},
          publisher={Nature Publishing Group}
        }
@article{deng2023kmcpy,
          title = {kMCpy: A python package to simulate transport properties in solids with kinetic Monte Carlo},
          journal = {Computational Materials Science},
          volume = {229},
          pages = {112394},
          year = {2023},
          issn = {0927-0256},
          doi = {https://doi.org/10.1016/j.commatsci.2023.112394},
          author = {Zeyu Deng and Tara P. Mishra and Weihang Xie and Daanyal Ahmed Saeed and Gopalakrishnan Sai Gautam and Pieremanuele Canepa},
          }

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kmcpy-0.3.0.tar.gz (169.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kmcpy-0.3.0-py3-none-any.whl (144.9 kB view details)

Uploaded Python 3

File details

Details for the file kmcpy-0.3.0.tar.gz.

File metadata

  • Download URL: kmcpy-0.3.0.tar.gz
  • Upload date:
  • Size: 169.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kmcpy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8a4351d0b118bf0913f7208ac6e4ddba186320573c4679243fb25b1dd03b2b96
MD5 c85c17b33b02d8b4a8c6e4f4f6c9f5cd
BLAKE2b-256 4ea24082c1b7d927cf4e8705468f70e9a7a6e00ba0d9f30f4fe7554a1b3c704d

See more details on using hashes here.

Provenance

The following attestation bundles were made for kmcpy-0.3.0.tar.gz:

Publisher: publish-to-pypi.yml on caneparesearch/kMCpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kmcpy-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: kmcpy-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 144.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kmcpy-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f315136a7389c83a499360b8f9e2132d25f7df7c4de1923e455b8634651df59
MD5 6997af806efacabcc6d752a30149820a
BLAKE2b-256 7deb606c0d45794307a69e31bc7b35125590a525259977afab62f8545f5bd472

See more details on using hashes here.

Provenance

The following attestation bundles were made for kmcpy-0.3.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on caneparesearch/kMCpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page