|
| 1 | +# ruff: noqa: E402 |
| 2 | +""" |
| 3 | +The test data is obtained from the following paper: |
| 4 | +
|
| 5 | +Antoine Loew et al 2026 J. Phys. Mater. 9 015010 Universal machine learning potentials under pressure |
| 6 | +DOI 10.1088/2515-7639/ae2ba8 |
| 7 | +
|
| 8 | +We downsampled the original test set to 45 structures at each pressure point (25, 50, 75, 100, 125, 150 GPa) |
| 9 | +""" |
| 10 | + |
| 11 | +from ase.io import read |
| 12 | +from ase import Atoms |
| 13 | +from ase.calculators.calculator import Calculator |
| 14 | +from ase.optimize import FIRE |
| 15 | +from ase.filters import FrechetCellFilter |
| 16 | +from pathlib import Path |
| 17 | +from tqdm import tqdm |
| 18 | +from sklearn.metrics import root_mean_squared_error, mean_absolute_error |
| 19 | +from lambench.models.ase_models import ASEModel |
| 20 | +import logging |
| 21 | + |
| 22 | +KBAR_2_EVA3 = 6.2415e-4 |
| 23 | +GPA_2_KBAR = 10 |
| 24 | + |
| 25 | + |
| 26 | +def optimize(structure: Atoms, target_p: float, fmax: float, steps: int) -> Atoms: |
| 27 | + target_p = target_p * GPA_2_KBAR * KBAR_2_EVA3 # to eV/A3 |
| 28 | + cell_filter = FrechetCellFilter(structure, scalar_pressure=target_p) |
| 29 | + opt = FIRE(cell_filter) |
| 30 | + opt.run(fmax=fmax, steps=steps) |
| 31 | + return cell_filter.atoms |
| 32 | + |
| 33 | + |
| 34 | +def test_one( |
| 35 | + init: Atoms, |
| 36 | + final: Atoms, |
| 37 | + target_p: float, |
| 38 | + calc: Calculator, |
| 39 | + fmax: float, |
| 40 | + max_steps: int, |
| 41 | +) -> tuple[float, float]: |
| 42 | + init.calc = calc |
| 43 | + optimized = optimize(init, int(target_p), fmax, max_steps) |
| 44 | + natoms = len(init) |
| 45 | + return final.get_volume() / natoms, optimized.get_volume() / natoms |
| 46 | + |
| 47 | + |
| 48 | +def run_inference( |
| 49 | + model: ASEModel, |
| 50 | + test_data: Path, |
| 51 | + fmax: float, |
| 52 | + max_steps: int, |
| 53 | +) -> dict[str, float]: |
| 54 | + calc = model.calc |
| 55 | + all_labels = [] |
| 56 | + all_preds = [] |
| 57 | + num_samples = 0 |
| 58 | + num_fails = 0 |
| 59 | + |
| 60 | + for pressure in tqdm(["025", "050", "075", "100", "125", "150"]): |
| 61 | + init_traj = read(test_data / f"P{pressure}.traj", ":") |
| 62 | + final_traj = read(test_data / f"P{pressure}.traj", ":") |
| 63 | + for i in tqdm(range(len(init_traj))): |
| 64 | + init = init_traj[i] |
| 65 | + final = final_traj[i] |
| 66 | + assert init.get_chemical_formula() == final.get_chemical_formula() |
| 67 | + try: |
| 68 | + dft, lam = test_one(init, final, int(pressure), calc, fmax, max_steps) |
| 69 | + except Exception as e: |
| 70 | + logging.error( |
| 71 | + f"Error during test_one at pressure {pressure}, index {i}: {e}" |
| 72 | + ) |
| 73 | + dft, lam = None, None |
| 74 | + if dft is None or lam is None: |
| 75 | + num_fails += 1 |
| 76 | + continue |
| 77 | + num_samples += 1 |
| 78 | + all_labels.append(dft) |
| 79 | + all_preds.append(lam) |
| 80 | + |
| 81 | + return { |
| 82 | + "MAE": mean_absolute_error(all_labels, all_preds), # A3/atom |
| 83 | + "RMSE": root_mean_squared_error(all_labels, all_preds), # A3/atom |
| 84 | + "success_rate": (num_samples - num_fails) / num_samples, |
| 85 | + } |
0 commit comments