File size: 6,816 Bytes
e94bdab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | """Set up MLIP infrastructure for high-throughput migration barrier computation.
Installs and validates MLIP tools for nudged elastic band (NEB) calculations:
- CHGNet: universal crystal Hamiltonian Graph neural Network
- MACE-MP-0: MACE architecture trained on Materials Project trajectories
- M3GNet: universal potential from Materials Project
- Orb-v3: Orbital-based MLIP
This script:
1. Checks what's installed
2. Attempts installation of missing packages
3. Validates each potential on a test structure
4. Generates a configuration file for the NEB pipeline
Usage:
python scripts/setup_mlip_infrastructure.py
python scripts/setup_mlip_infrastructure.py --check-only
python scripts/setup_mlip_infrastructure.py --install
"""
import argparse, os, sys, subprocess, json, warnings
from pathlib import Path
MLIP_PACKAGES = {
"chgnet": "chgnet",
"mace": "mace-torch",
"matgl": "matgl",
"orb": "orb-models",
}
TEST_STRUCTURE = """
{
"@module": "pymatgen.core.structure",
"@class": "Structure",
"lattice": {"matrix": [[3.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 3.0]], "pbc": [true, true, true]},
"sites": [
{"species": [{"element": "Li", "occu": 1}], "abc": [0.0, 0.0, 0.0]},
{"species": [{"element": "Cl", "occu": 1}], "abc": [0.5, 0.5, 0.5]}
]
}
"""
def check_installed():
"""Check which MLIP packages are installed."""
results = {}
for name, pkg in MLIP_PACKAGES.items():
try:
__import__(name.replace("-", "_"))
results[name] = "installed"
except ImportError:
try:
__import__(pkg.replace("-", "_"))
results[name] = "installed"
except ImportError:
results[name] = "not found"
return results
def install_packages(packages):
"""Install MLIP packages via pip."""
for name, pkg in packages.items():
print(f" Installing {pkg}...")
result = subprocess.run(
[sys.executable, "-m", "pip", "install", pkg],
capture_output=True, text=True
)
if result.returncode == 0:
print(f" {name}: installed")
else:
print(f" {name}: failed — {result.stderr[-200:]}")
def validate_chgnet(structure_dict):
"""Validate CHGNet can predict on test structure."""
import json
from pymatgen.core import Structure
from chgnet.model import CHGNet
from chgnet.utils import write_structures_to_POSCAR
struct = Structure.from_dict(structure_dict)
model = CHGNet.load()
prediction = model.predict_structure(struct)
return {
"energy": float(prediction["e"]),
"forces_shape": list(prediction["f"].shape),
}
def validate_mace(structure_dict):
"""Validate MACE can predict on test structure."""
import torch
from mace.calculators import MACECalculator
from ase.io import read
from pymatgen.core import Structure
from pymatgen.io.ase import AseAtomsAdaptor
struct = Structure.from_dict(structure_dict)
atoms = AseAtomsAdaptor.get_atoms(struct)
calc = MACECalculator(model_path="medium", device="cpu")
atoms.set_calculator(calc)
energy = atoms.get_potential_energy()
forces = atoms.get_forces()
return {
"energy": float(energy),
"forces_shape": list(forces.shape),
}
def main():
parser = argparse.ArgumentParser(description="MLIP infrastructure setup")
parser.add_argument("--check-only", action="store_true",
help="Check installed packages only")
parser.add_argument("--install", action="store_true",
help="Install missing MLIP packages")
parser.add_argument("--validate", action="store_true",
help="Validate installed potentials on test structure")
args = parser.parse_args()
BASE_DIR = Path(__file__).resolve().parent.parent
print("=" * 60)
print(" MLIP INFRASTRUCTURE SETUP")
print(" High-throughput migration barrier computation pipeline")
print("=" * 60)
# Check installed packages
print("\n Checking installed MLIP packages...")
installed = check_installed()
for name, status in installed.items():
print(f" {name:12s}: {status}")
if args.install:
to_install = {k: v for k, v in MLIP_PACKAGES.items() if installed[k] == "not found"}
if to_install:
print(f"\n Installing {len(to_install)} packages...")
install_packages(to_install)
else:
print("\n All packages already installed.")
if args.validate:
print("\n Validating potentials...")
struct_dict = json.loads(TEST_STRUCTURE)
if installed.get("chgnet") == "installed":
try:
result = validate_chgnet(struct_dict)
print(f" CHGNet: OK (energy={result['energy']:.3f} eV)")
except Exception as e:
print(f" CHGNet: validation failed — {str(e)[:80]}")
if installed.get("mace") == "installed":
try:
result = validate_mace(struct_dict)
print(f" MACE: OK (energy={result['energy']:.3f} eV)")
except Exception as e:
print(f" MACE: validation failed — {str(e)[:80]}")
# Generate config file
if not args.check_only:
config = {
"potentials": installed,
"pipeline": {
"bvse_barrier_threshold": 0.5,
"mlip_neb_grid": [5, 5, 5],
"mlip_neb_spring_constant": 5.0,
"mlip_neb_fmax": 0.05,
"mlip_neb_steps": 500,
},
"target_subset": "gold_battery_li",
"description": "Li-containing Gold-tier battery-family entries",
}
config_path = BASE_DIR / "configs" / "mlip_pipeline.json"
print(f"\n Writing config to {config_path}...")
config_path.parent.mkdir(parents=True, exist_ok=True)
with open(config_path, "w") as f:
json.dump(config, f, indent=2)
# Print next steps
print(f"\n{'─' * 60}")
print(" NEXT STEPS")
print(f" {'─' * 60}")
print("""
1. Install MLIP packages:
pip install chgnet mace-torch matgl orb-models
2. Run BVSE pre-filter on Li/Na entries:
python scripts/compute_bvse_barriers.py --subset gold --limit 50000
3. Run MLIP-NEB on BVSE-filtered subset:
python scripts/run_mlip_neb_pipeline.py --input dataset/bvse_filtered.json
4. Update sse_candidate_score with full 5 gates:
python scripts/compute_sse_candidate_score.py
""")
print("=" * 60)
if __name__ == "__main__":
main()
|