Logo Pymatgen

Pymatgen

Cộng đồng
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.

Tổng quan

Nhà phát hànhjkitchin
Kho mãknowhow
Tên kỹ năngpymatgen
Sao
45
Bản fork
9
Tệp đi kèm
11
Giấy phépMIT
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 11 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được jkitchin công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Pymatgen trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/jkitchin/knowhow/tree/main/skills/programming/pymatgen
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

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
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Pymatgen trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Pymatgen ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Pymatgen với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

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

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Pymatgen làm được gì?

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.

Vì sao nên dùng Pymatgen trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Pymatgen là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Pymatgen trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/jkitchin/knowhow/tree/main/skills/programming/pymatgen. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Pymatgen?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Pymatgen với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Pymatgen với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Pymatgen có miễn phí không?

Có. Kỹ năng này được jkitchin phát hành trên GitHub theo giấy phép MIT. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇