File size: 1,627 Bytes
fb5d76b
 
 
 
 
 
 
 
 
 
 
 
50064b5
fb5d76b
 
 
 
 
 
 
 
50064b5
 
 
75ac94f
 
 
 
 
 
 
 
54047c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50064b5
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path

import yaml
from mattersim.forcefield import MatterSimCalculator

from mlip_arena.models.utils import get_freer_device

with open(Path(__file__).parents[1] / "registry.yaml", encoding="utf-8") as f:
    REGISTRY = yaml.safe_load(f)


class MatterSim(MatterSimCalculator):
    def __init__(
        self,
        checkpoint=REGISTRY["MatterSim"]["checkpoint"],
        device=None,
        **kwargs,
    ):
        super().__init__(
            load_path=checkpoint, device=str(device or get_freer_device()), **kwargs
        )

    def get_potential_energy(self, atoms=None, force_consistent=False):
        return float(
            super().get_potential_energy(
                atoms=atoms,
                force_consistent=force_consistent,
            )
        ) # mattersim return numpy float instead of python float

    def __getstate__(self):
        state = self.__dict__.copy()

        # BUG: remove unpicklizable potential
        state.pop("potential", None)

        return state

    # def calculate(
    #     self,
    #     atoms: Atoms | None = None,
    #     properties: list | None = None,
    #     system_changes: list | None = None,
    # ):
    #     super().calculate(atoms, properties, system_changes)

        # # convert unpicklizable atoms back to picklizable atoms to avoid prefect pickling error
        # if isinstance(self.atoms, MSONAtoms):
        #     atoms = self.atoms.copy()
        #     strucutre = AseAtomsAdaptor().get_structure(atoms)
        #     self.atoms = AseAtomsAdaptor().get_atoms(strucutre, msonable=False)