Spaces:
Running
Running
File size: 1,669 Bytes
9d1a2a5 7cbf186 9d1a2a5 7cbf186 9d1a2a5 7cbf186 9d1a2a5 7cbf186 9d1a2a5 7cbf186 9d1a2a5 |
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 |
from typing import Optional, Tuple
import numpy as np
import torch
from ase import Atoms
from ase.calculators.calculator import all_changes
from huggingface_hub import hf_hub_download
from torch_geometric.data import Data
from mlip_arena.models import MLIP, MLIPCalculator
# TODO: WIP
class CHGNet(MLIPCalculator):
def __init__(
self,
device: torch.device | None = None,
restart=None,
atoms=None,
directory=".",
**kwargs,
):
self.device = device or torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
super().__init__(
model=model, restart=restart, atoms=atoms, directory=directory, **kwargs
)
self.name: str = self.__class__.__name__
self.implemented_properties = ["energy", "forces", "stress"]
def calculate(
self, atoms: Atoms, properties: list[str], system_changes: list = all_changes
):
"""Calculate energies and forces for the given Atoms object"""
super().calculate(atoms, properties, system_changes)
output = self.forward(atoms)
self.results = {}
if "energy" in properties:
self.results["energy"] = output["energy"].item()
if "forces" in properties:
self.results["forces"] = output["forces"].cpu().detach().numpy()
if "stress" in properties:
self.results["stress"] = output["stress"].cpu().detach().numpy()
def forward(self, x: Data | Atoms) -> dict[str, torch.Tensor]:
"""Implement data conversion, graph creation, and model forward pass"""
# TODO
raise NotImplementedError
|