| import time, torch, numpy as np |
| from ase.build import fcc100, add_adsorbate, molecule |
| from ase.optimize import LBFGS |
|
|
| |
| import os |
| _REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| _JD_PATH = os.path.join(_REPO_ROOT, "weight", "Jd.pt") |
| if os.path.isfile(_JD_PATH): |
| os.environ.setdefault("ONESCIENCE_UMA_JD_PATH", _JD_PATH) |
|
|
| from onescience.utils.uma.units.mlip_unit import load_predict_unit |
| from onescience.utils.uma.calculate.ase_calculator import FAIRChemCalculator |
|
|
| |
| def _sync(): |
| if torch.cuda.is_available(): |
| torch.cuda.synchronize() |
|
|
| def clear_cache(atoms): |
| |
| if hasattr(atoms, "calc") and hasattr(atoms.calc, "results"): |
| atoms.calc.results.clear() |
|
|
| |
| predictor = load_predict_unit( |
| "../weight/uma-s-1p1_converted.pt", |
| device="cuda" |
| ) |
| calc = FAIRChemCalculator(predictor, task_name="oc20") |
|
|
| |
| slab = fcc100("Cu", (3, 3, 3), vacuum=8, periodic=True) |
| adsorbate = molecule("CO") |
| add_adsorbate(slab, adsorbate, 2.0, "bridge") |
| slab.calc = calc |
|
|
| |
| _ = slab.get_potential_energy(); _sync() |
| _ = slab.get_forces(); _sync() |
|
|
| |
| t0 = time.perf_counter() |
| opt = LBFGS(slab, logfile=None) |
| opt.run(fmax=0.05, steps=100) |
| _sync() |
| lbfgs_total = time.perf_counter() - t0 |
| steps_done = getattr(opt, "nsteps", None) or 100 |
|
|
| |
| clear_cache(slab); t0 = time.perf_counter() |
| energy = slab.get_potential_energy(); _sync() |
| t_energy = time.perf_counter() - t0 |
|
|
| clear_cache(slab); t0 = time.perf_counter() |
| forces = slab.get_forces(); _sync() |
| t_forces = time.perf_counter() - t0 |
|
|
| |
| print(f"Predicted energy: {energy}") |
| print(f"Predicted forces shape: {forces.shape}") |
| |
| |
|
|
| print(f"[E] latency: {t_energy*1e3:.2f} ms/call") |
| print(f"[F] latency: {t_forces*1e3:.2f} ms/call") |
| print(f"[LBFGS] total: {lbfgs_total:.3f} s, per-step: {lbfgs_total/steps_done*1e3:.2f} ms/step") |
|
|