Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions dpdata/plugins/ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def from_labeled_system(self, atoms: ase.Atoms, **kwargs) -> dict:
"""Convert ase.Atoms to a LabeledSystem. Energies and forces
are calculated by the calculator.

Note that this method will try to load virials from either virial field or converted from stress tensor.

Parameters
----------
atoms : ase.Atoms
Expand Down Expand Up @@ -94,13 +96,19 @@ def from_labeled_system(self, atoms: ase.Atoms, **kwargs) -> dict:
"energies": np.array([energies]),
"forces": np.array([forces]),
}
try:
stress = atoms.get_stress(voigt=False)
except PropertyNotImplementedError:
pass
else:
virials = np.array([-atoms.get_volume() * stress])
info_dict["virials"] = virials

# try to get virials from different sources
virials = atoms.info.get("virial")
if virials is None:
try:
stress = atoms.get_stress(voigt=False)
except PropertyNotImplementedError:
pass
else:
virials = -atoms.get_volume() * stress
if virials is not None:
info_dict["virials"] = np.array([virials])

return info_dict

def from_multi_systems(
Expand Down Expand Up @@ -166,7 +174,6 @@ def to_labeled_system(self, data, *args, **kwargs) -> list[ase.Atoms]:

structures = []
species = [data["atom_names"][tt] for tt in data["atom_types"]]

for ii in range(data["coords"].shape[0]):
structure = Atoms(
symbols=species,
Expand Down