Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support atomic virials #14

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion deepmd_mace/mace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BaseModel,
)
from deepmd.pt.model.model.transform_output import (
atomic_virial_corr,
communicate_extended_output,
)
from deepmd.pt.utils.nlist import (
Expand Down Expand Up @@ -564,7 +565,7 @@ def forward_lower_common(
mapping: Optional[torch.Tensor] = None,
fparam: Optional[torch.Tensor] = None,
aparam: Optional[torch.Tensor] = None,
do_atomic_virial: bool = False, # noqa: ARG002
do_atomic_virial: bool = False,
comm_dict: Optional[dict[str, torch.Tensor]] = None,
) -> dict[str, torch.Tensor]:
"""Forward lower common pass of the model.
Expand Down Expand Up @@ -714,6 +715,12 @@ def forward_lower_common(
) @ extended_coord_ff.unsqueeze(-2).to(
extended_coord_.dtype,
)
if do_atomic_virial:
extended_virial_corr = atomic_virial_corr(
extended_coord_ff.unsqueeze(0),
atom_energy.view(1, nloc, 1),
)
atomic_virial = atomic_virial + extended_virial_corr
force = force.view(1, nall, 3).to(extended_coord_.dtype)
virial = (
torch.sum(atomic_virial, dim=0).view(1, 9).to(extended_coord_.dtype)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def test_forward(self) -> None:
"box": cell,
"aparam": aparam,
"fparam": fparam,
"do_atomic_virial": True,
}
if test_spin:
input_dict["spin"] = spin
Expand All @@ -282,6 +283,7 @@ def test_forward(self) -> None:
"aparam": aparam,
"fparam": fparam,
"mapping": mapping_large,
"do_atomic_virial": True,
}
if test_spin:
input_dict_lower["extended_spin"] = spin_ext
Expand Down
Loading