-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
466 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import copy | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
List, | ||
Optional, | ||
) | ||
|
||
from deepmd.dpmodel.common import ( | ||
DEFAULT_PRECISION, | ||
) | ||
from deepmd.dpmodel.fitting.invar_fitting import ( | ||
InvarFitting, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from deepmd.dpmodel.fitting.general_fitting import ( | ||
GeneralFitting, | ||
) | ||
from deepmd.utils.version import ( | ||
check_version_compatibility, | ||
) | ||
|
||
|
||
@InvarFitting.register("dos") | ||
class DOSFittingNet(InvarFitting): | ||
def __init__( | ||
self, | ||
ntypes: int, | ||
dim_descrpt: int, | ||
neuron: List[int] = [120, 120, 120], | ||
resnet_dt: bool = True, | ||
numb_fparam: int = 0, | ||
numb_aparam: int = 0, | ||
numb_dos: int = 300, | ||
rcond: Optional[float] = None, | ||
trainable: Optional[List[bool]] = None, | ||
activation_function: str = "tanh", | ||
precision: str = DEFAULT_PRECISION, | ||
mixed_types: bool = False, | ||
exclude_types: List[int] = [], | ||
# not used | ||
seed: Optional[int] = None, | ||
): | ||
super().__init__( | ||
var_name="dos", | ||
ntypes=ntypes, | ||
dim_descrpt=dim_descrpt, | ||
dim_out=numb_dos, | ||
neuron=neuron, | ||
resnet_dt=resnet_dt, | ||
numb_fparam=numb_fparam, | ||
numb_aparam=numb_aparam, | ||
rcond=rcond, | ||
trainable=trainable, | ||
activation_function=activation_function, | ||
precision=precision, | ||
mixed_types=mixed_types, | ||
exclude_types=exclude_types, | ||
) | ||
|
||
@classmethod | ||
def deserialize(cls, data: dict) -> "GeneralFitting": | ||
data = copy.deepcopy(data) | ||
check_version_compatibility(data.pop("@version", 1), 1, 1) | ||
data.pop("var_name") | ||
data.pop("dim_out") | ||
data.pop("tot_ener_zero") | ||
data.pop("layer_name") | ||
data.pop("use_aparam_as_mask") | ||
data.pop("spin") | ||
data.pop("atom_ener") | ||
return super().deserialize(data) | ||
|
||
def serialize(self) -> dict: | ||
"""Serialize the fitting to dict.""" | ||
return { | ||
**super().serialize(), | ||
"type": "dos", | ||
} |
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,80 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Dict, | ||
Optional, | ||
) | ||
|
||
import torch | ||
|
||
from .dp_model import ( | ||
DPModel, | ||
) | ||
|
||
|
||
class DOSModel(DPModel): | ||
model_type = "dos" | ||
|
||
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.get_fitting_net() is not None: | ||
model_predict = {} | ||
model_predict["atom_dos"] = model_ret["dos"] | ||
model_predict["dos"] = model_ret["dos_redu"] | ||
|
||
if "mask" in model_ret: | ||
model_predict["mask"] = model_ret["mask"] | ||
else: | ||
model_predict = model_ret | ||
model_predict["updated_coord"] += coord | ||
return model_predict | ||
|
||
@torch.jit.export | ||
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.get_fitting_net() is not None: | ||
model_predict = {} | ||
model_predict["atom_dos"] = model_ret["dos"] | ||
model_predict["dos"] = model_ret["energy_redu"] | ||
|
||
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
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.