-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add DipoleModel and PolarModel (#3309)
This PR is to provide model wrappers for `DipoleFittingNet` and `PolarFittingNet`, such that the saved model can be used directly in inference with `DeepDiole` and `DeepPolar`. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4956864
commit af14ba4
Showing
16 changed files
with
379 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Dict, | ||
Optional, | ||
) | ||
|
||
import torch | ||
|
||
from .dp_model import ( | ||
DPModel, | ||
) | ||
|
||
|
||
class DipoleModel(DPModel): | ||
model_type = "dipole" | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__(*args, **kwargs) | ||
|
||
def forward( | ||
self, | ||
coord, | ||
atype, | ||
box: Optional[torch.Tensor] = None, | ||
fparam: Optional[torch.Tensor] = None, | ||
aparam: Optional[torch.Tensor] = None, | ||
do_atomic_virial: bool = False, | ||
) -> Dict[str, torch.Tensor]: | ||
model_ret = self.forward_common( | ||
coord, | ||
atype, | ||
box, | ||
fparam=fparam, | ||
aparam=aparam, | ||
do_atomic_virial=do_atomic_virial, | ||
) | ||
if self.fitting_net is not None: | ||
model_predict = {} | ||
model_predict["dipole"] = model_ret["dipole"] | ||
model_predict["global_dipole"] = model_ret["dipole_redu"] | ||
if self.do_grad_r("dipole"): | ||
model_predict["force"] = model_ret["dipole_derv_r"].squeeze(-2) | ||
if self.do_grad_c("dipole"): | ||
model_predict["virial"] = model_ret["dipole_derv_c_redu"].squeeze(-2) | ||
if do_atomic_virial: | ||
model_predict["atom_virial"] = model_ret["dipole_derv_c"].squeeze( | ||
-3 | ||
) | ||
else: | ||
model_predict = model_ret | ||
model_predict["updated_coord"] += coord | ||
return model_predict | ||
|
||
def forward_lower( | ||
self, | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping: Optional[torch.Tensor] = None, | ||
fparam: Optional[torch.Tensor] = None, | ||
aparam: Optional[torch.Tensor] = None, | ||
do_atomic_virial: bool = False, | ||
): | ||
model_ret = self.forward_common_lower( | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping, | ||
fparam=fparam, | ||
aparam=aparam, | ||
do_atomic_virial=do_atomic_virial, | ||
) | ||
if self.fitting_net is not None: | ||
model_predict = {} | ||
model_predict["dipole"] = model_ret["dipole"] | ||
model_predict["global_dipole"] = model_ret["dipole_redu"] | ||
if self.do_grad_r("dipole"): | ||
model_predict["force"] = model_ret["dipole_derv_r"].squeeze(-2) | ||
if self.do_grad_c("dipole"): | ||
model_predict["virial"] = model_ret["dipole_derv_c_redu"].squeeze(-2) | ||
if do_atomic_virial: | ||
model_predict["atom_virial"] = model_ret["dipole_derv_c"].squeeze( | ||
-3 | ||
) | ||
else: | ||
model_predict = model_ret | ||
return model_predict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.