Logo: Pymatgen

Pymatgen

Communautéit
jkitchin
pymatgen

Comprehensive guidance for using pymatgen (Python Materials Genomics) for computational materials science. Covers structure creation and manipulation, file I/O (CIF, POSCAR, XYZ), symmetry analysis, Materials Project API integration, phase diagrams, electronic structure analysis, and DFT input generation. Use when working with crystal structures, materials properties, computational chemistry calculations, or materials databases. Triggers include 'pymatgen', 'crystal structure', 'Materials Project', 'CIF file', 'POSCAR', 'band structure', 'phase diagram', or materials analysis tasks.

Iwwersiicht

Editeurjkitchin
Repositoryknowhow
Skill-Nummpymatgen
Stären
45
Forks
9
Matgeliwwert Dateien
11
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 11 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    jkitchin huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Pymatgen an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/jkitchin/knowhow/tree/main/skills/programming/pymatgen
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/jkitchin/knowhow.git /tmp/knowhow
mkdir -p .claude/skills
cp -r /tmp/knowhow/skills/programming/pymatgen .claude/skills/pymatgen
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Pymatgen an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Pymatgen automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Pymatgen net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Pymatgen Materials Analysis

Systematic guidance for using pymatgen to analyze crystal structures, access materials databases, and interface with computational chemistry codes.

Pymatgen Workflow

1. Identify Task Type

What do you want to do?

Structure Creation/Loading:

  • Create structure from scratch → references/core-objects.md
  • Read from file (CIF, POSCAR, XYZ) → references/file-io.md
  • Get from Materials Project → references/materials-project.md
  • Generate from symmetry → references/structure-analysis.md

Structure Analysis:

  • Analyze symmetry/space groups → references/structure-analysis.md
  • Compare structures → references/structure-analysis.md
  • Calculate properties → references/properties.md
  • Visualize structures → references/visualization.md

Structure Manipulation:

  • Create supercells/slabs → references/transformations.md
  • Substitute atoms → references/transformations.md
  • Apply symmetry operations → references/transformations.md
  • Interpolate for NEB → references/transformations.md

Materials Database:

  • Query Materials Project → references/materials-project.md
  • Retrieve structures/properties → references/materials-project.md
  • Build phase diagrams → references/phase-diagrams.md

Electronic Structure:

  • Parse band structures → references/electronic-structure.md
  • Plot DOS → references/electronic-structure.md
  • Analyze VASP outputs → references/vasp-integration.md

Input Generation:

  • Create VASP inputs → references/vasp-integration.md
  • Generate Gaussian inputs → references/file-io.md
  • Set up calculations → references/vasp-integration.md

2. Core Pymatgen Workflow

Basic pattern:

python
# 1. Import modules
from pymatgen.core import Structure, Lattice, Element

# 2. Load or create structure
structure = Structure.from_file("POSCAR")

# 3. Analyze or manipulate
print(f"Formula: {structure.composition.reduced_formula}")
print(f"Space group: {structure.get_space_group_info()}")

# 4. Transform if needed
supercell = structure * (2, 2, 1)  # 2x2x1 supercell

# 5. Write output
supercell.to(filename="POSCAR_supercell")

Quick Reference - Common Tasks

Load structure: Structure.from_file("file.cif") - Auto-detects format Create structure: See references/core-objects.md for Element, Lattice, Structure creation Analyze symmetry: SpacegroupAnalyzer(struct).get_space_group_symbol() Make supercell: structure * (2, 2, 1) or use transformations Query MP: MPRester(key).get_structure_by_material_id("mp-149") Generate VASP: MPRelaxSet(struct).write_input("dir") Plot bands/DOS: See references/electronic-structure.md

Detailed examples for all tasks in reference files.

Task Routing

Core Objects and Creation

Route to: references/core-objects.md

When:

  • Creating structures from scratch
  • Understanding Element, Site, Structure classes
  • Working with Lattice objects
  • Creating molecules
  • Composition analysis

Key classes:

  • Element, Species
  • Lattice
  • Site, PeriodicSite
  • Structure, Molecule
  • Composition

File Input/Output

Route to: references/file-io.md

When:

  • Reading CIF, POSCAR, XYZ files
  • Writing structures to files
  • Format conversion
  • Parsing calculation outputs
  • Working with multiple formats

Supported formats:

  • CIF (Crystallographic Information File)
  • POSCAR/CONTCAR (VASP)
  • XYZ (molecular coordinates)
  • JSON (serialization)
  • Many computational chemistry codes

Structure Analysis

Route to: references/structure-analysis.md

When:

  • Finding space groups
  • Symmetry operations
  • Comparing structures
  • Getting primitive/conventional cells
  • Neighbor analysis

Key tools:

  • SpacegroupAnalyzer
  • StructureMatcher
  • VoronoiAnalysis
  • Distance calculations

Structure Transformations

Route to: references/transformations.md

When:

  • Creating supercells
  • Making slabs/surfaces
  • Substituting elements
  • Perturbing structures
  • High-throughput workflows

Key modules:

  • pymatgen.transformations.standard_transformations
  • pymatgen.transformations.advanced_transformations
  • pymatgen.alchemy

Materials Project API

Route to: references/materials-project.md

When:

  • Querying materials database
  • Getting structures by formula
  • Retrieving calculated properties
  • Accessing experimental data
  • Building chemical systems

Key class:

  • MPRester for API access
  • Requires API key from materialsproject.org

Phase Diagrams

Route to: references/phase-diagrams.md

When:

  • Constructing phase diagrams
  • Analyzing stability
  • Finding decomposition products
  • Pourbaix diagrams
  • Grand potential diagrams

Key classes:

  • PhaseDiagram
  • PhaseDiagramError (for stability analysis)
  • PourbaixDiagram

Electronic Structure

Route to: references/electronic-structure.md

When:

  • Analyzing band structures
  • Plotting DOS
  • Finding band gaps
  • Analyzing orbital contributions
  • Electronic property calculations

Key modules:

  • pymatgen.electronic_structure.bandstructure
  • pymatgen.electronic_structure.dos
  • pymatgen.electronic_structure.plotter

VASP Integration

Route to: references/vasp-integration.md

When:

  • Generating VASP inputs
  • Parsing VASP outputs
  • Creating input sets
  • High-throughput VASP
  • Custom INCAR settings

Key classes:

  • MPRelaxSet, MPStaticSet, etc.
  • Vasprun, Outcar parsers
  • Poscar, Incar classes

Common Patterns

Pattern 1: Structure Analysis

  • Load → Analyze composition/symmetry → Get primitive/conventional
  • See examples/structure_analysis.py for complete workflow

Pattern 2: Materials Project to Calculation

  • Query MP → Get structure → Generate VASP inputs → Customize settings
  • See examples/mp_to_vasp.py

Pattern 3: High-Throughput Substitution

  • Load base structure → Apply transformations → Write outputs
  • See examples/substitution_study.py

Pattern 4: Electronic Structure Analysis

  • Parse vasprun.xml → Extract band structure/DOS → Plot and analyze
  • See examples/band_structure.py

All patterns detailed in examples/ directory with complete code.

Installation and Setup

Install Pymatgen

bash
pip install pymatgen
# Or with optional dependencies
pip install pymatgen[all]

Configure Materials Project API

bash
# Register at materialsproject.org to get API key
pmg config --add PMG_MAPI_KEY your_api_key_here

Set VASP Pseudopotential Path

bash
pmg config --add PMG_VASP_PSP_DIR /path/to/vasp/potentials

Configuration File

Located at ~/.pmgrc.yaml:

yaml
PMG_MAPI_KEY: your_api_key_here
PMG_VASP_PSP_DIR: /path/to/vasp/potentials
PMG_DEFAULT_FUNCTIONAL: PBE

Common Issues and Solutions

Issue: Structure not defined

Problem: NameError: name 'Structure' is not defined

Solution:

python
from pymatgen.core import Structure

Issue: API key not working

Problem: MPRester returns authentication error

Solution:

  1. Get API key from materialsproject.org (free account)
  2. Configure: pmg config --add PMG_MAPI_KEY your_key
  3. Or pass directly: MPRester("your_key")

Issue: POTCAR generation fails

Problem: Cannot write POTCAR files

Solution:

  1. Set PSP directory: pmg config --add PMG_VASP_PSP_DIR /path
  2. Ensure VASP pseudopotentials are properly installed
  3. Check directory structure matches expected format

Issue: Import errors for optional dependencies

Problem: ImportError for plotting or specialized modules

Solution:

bash
pip install pymatgen[all]  # Install all optional dependencies
# Or specific ones:
pip install matplotlib  # For plotting
pip install scipy      # For analysis tools

Best Practices

Object-Oriented Approach

  • Use Structure/Molecule objects, not raw coordinates
  • Leverage built-in methods (composition, symmetry, etc.)
  • Chain operations for clarity

Serialization

  • Use as_dict() / from_dict() for persistence
  • Prefer JSON over pickle for code evolution
  • Use MontyEncoder/MontyDecoder for complex objects

Transformations

  • Use Transformation classes for reproducibility
  • TransformedStructure tracks history
  • Alchemy framework for high-throughput

Input Sets

  • Use MPRelaxSet, MPStaticSet for standard calculations
  • Customize by modifying Incar after creation
  • Use InputGenerator for custom workflows

Materials Project

  • Use context manager: with MPRester(...) as mpr:
  • Batch queries when possible
  • Cache results to avoid repeated API calls

Examples Directory

See examples/ for complete workflows:

  • structure_analysis.py - Comprehensive structure analysis
  • mp_query.py - Materials Project queries
  • phase_diagram.py - Phase diagram construction
  • band_structure.py - Electronic structure analysis
  • vasp_workflow.py - VASP calculation setup
  • substitution_study.py - High-throughput substitutions

Reference Documentation

  • references/core-objects.md - Element, Structure, Lattice, Composition
  • references/file-io.md - Reading/writing all file formats
  • references/structure-analysis.md - Symmetry, comparison, neighbors
  • references/transformations.md - Supercells, substitutions, perturbations
  • references/materials-project.md - API usage and queries
  • references/phase-diagrams.md - Phase diagram construction
  • references/electronic-structure.md - Band structures and DOS
  • references/vasp-integration.md - VASP input/output handling
  • references/properties.md - Calculated properties
  • references/visualization.md - Structure visualization

External Resources

Matgeliwwert Dateien

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Pymatgen?

Comprehensive guidance for using pymatgen (Python Materials Genomics) for computational materials science. Covers structure creation and manipulation, file I/O (CIF, POSCAR, XYZ), symmetry analysis, Materials Project API integration, phase diagrams, electronic structure analysis, and DFT input generation. Use when working with crystal structures, materials properties, computational chemistry calculations, or materials databases. Triggers include 'pymatgen', 'crystal structure', 'Materials Project', 'CIF file', 'POSCAR', 'band structure', 'phase diagram', or materials analysis tasks.

Firwat Pymatgen op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Pymatgen besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Pymatgen an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/jkitchin/knowhow/tree/main/skills/programming/pymatgen an. TypingMind liest d'Datei SKILL.md, bëndelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Pymatgen benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Pymatgen benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Pymatgen domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Pymatgen gratis?

Jo. jkitchin huet de Skill ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇