Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 1, 2024
1 parent e423e68 commit 39da7ff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 49 deletions.
105 changes: 64 additions & 41 deletions deepmd/pt/model/model/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
from deepmd.model_format import (

Check warning on line 10 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L10

Added line #L10 was not covered by tests
FittingOutputDef,
)

from deepmd.pt.utils.nlist import (

Check warning on line 13 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L13

Added line #L13 was not covered by tests
build_multiple_neighbor_list,
)

from .atomic_model import (

Check warning on line 17 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L17

Added line #L17 was not covered by tests
AtomicModel,
)
from .dp_atomic_model import (

Check warning on line 20 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L20

Added line #L20 was not covered by tests
DPAtomicModel,
)
from .model import (

Check warning on line 23 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L23

Added line #L23 was not covered by tests
BaseModel,
)
from .pair_tab import (

Check warning on line 26 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L26

Added line #L26 was not covered by tests
PairTabModel,
)
from .dp_atomic_model import (
DPAtomicModel,
)

from deepmd.pt.utils.nlist import build_multiple_neighbor_list


class LinearModel(BaseModel, AtomicModel):

Check warning on line 31 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L31

Added line #L31 was not covered by tests
Expand All @@ -46,11 +46,13 @@ def __init__(
self.models = models
self.dp_model = models[0]
self.zbl_model = models[1]
assert isinstance(self.zbl_model, PairTabModel) and isinstance(self.dp_model, DPAtomicModel), "The provided models are not in the correct order `DPAtomicModel` + `PairTabModel`."
assert (

Check warning on line 49 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L45-L49

Added lines #L45 - L49 were not covered by tests
isinstance(self.zbl_model, PairTabModel)
and isinstance(self.dp_model, DPAtomicModel)
), "The provided models are not in the correct order `DPAtomicModel` + `PairTabModel`."
self.rcut = self.get_rcut()
self.sel = self.get_sel()

Check warning on line 54 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L53-L54

Added lines #L53 - L54 were not covered by tests


def get_fitting_output_def(self) -> FittingOutputDef:

Check warning on line 56 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L56

Added line #L56 was not covered by tests
"""Get the output def of the fitting net."""
return (

Check warning on line 58 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L58

Added line #L58 was not covered by tests
Expand All @@ -62,18 +64,25 @@ def get_fitting_output_def(self) -> FittingOutputDef:
def get_rcut(self) -> float:

Check warning on line 64 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L64

Added line #L64 was not covered by tests
"""Get the cut-off radius."""
return self.get_rcuts()[-1]

Check warning on line 66 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L66

Added line #L66 was not covered by tests

def get_rcuts(self) -> float:

Check warning on line 68 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L68

Added line #L68 was not covered by tests
"""Get the cut-off radius for each individual models in ascending order."""
return sorted([model.get_rcut() for model in self.models])

Check warning on line 70 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L70

Added line #L70 was not covered by tests

def get_sel(self) -> int:

Check warning on line 72 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L72

Added line #L72 was not covered by tests
"""Get the neighbor selection."""
"""Get the neighbor selection."""
return self.get_sels()[-1]

Check warning on line 74 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L74

Added line #L74 was not covered by tests

def get_sels(self) -> int:

Check warning on line 76 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L76

Added line #L76 was not covered by tests
"""Get the neighbor selection for each individual models in ascending order."""
return sorted([sum(model.get_sel()) if isinstance(model.get_sel(), list) else model.get_sel() for model in self.models])
"""Get the neighbor selection for each individual models in ascending order."""
return sorted(

Check warning on line 78 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L78

Added line #L78 was not covered by tests
[
sum(model.get_sel())
if isinstance(model.get_sel(), list)
else model.get_sel()
for model in self.models
]
)

def distinguish_types(self) -> bool:

Check warning on line 87 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L87

Added line #L87 was not covered by tests
"""If distinguish different types by sorting."""
Expand All @@ -84,10 +93,9 @@ def forward_atomic(
extended_coord,
extended_atype,
nlist,
ra: float,
rb: float,
ra: float,
rb: float,
alpha: Optional[float] = 0.1,

mapping: Optional[torch.Tensor] = None,
) -> Dict[str, torch.Tensor]:
"""Return atomic prediction.
Expand All @@ -96,7 +104,6 @@ def forward_atomic(
the weight is calculated based on this paper:
Appl. Phys. Lett. 114, 244101 (2019); https://doi.org/10.1063/1.5098061
Parameters
----------
extended_coord
Expand All @@ -107,65 +114,81 @@ def forward_atomic(
neighbor list. nf x nloc x nsel
mapping
mapps the extended indices to local indices
ra : float
inclusive lower boundary of the range in which the ZBL potential and the deep potential are interpolated.
rb : float
ra : float
inclusive lower boundary of the range in which the ZBL potential and the deep potential are interpolated.
rb : float
exclusive upper boundary of the range in which the ZBL potential and the deep potential are interpolated.
alpha : float
alpha : float
a tunable scale of the distances between atoms.
Returns
-------
result_dict
the result dict, defined by the fitting net output def.
"""

# the DPAtomicModel sel is always a List or Union[List, int]?
nlists = build_multiple_neighbor_list(extended_coord, nlist, self.get_rcuts(), self.get_sels())
nlists = build_multiple_neighbor_list(

Check warning on line 130 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L130

Added line #L130 was not covered by tests
extended_coord, nlist, self.get_rcuts(), self.get_sels()
)

zbl_nlist = nlists[str(self.zbl_model.rcut) + "_" + str(self.zbl_model.sel)]
dp_nlist = nlists[str(self.dp_model.rcut) + "_" + str(self.dp_model.sel)] # need to handle sel dtype.
dp_nlist = nlists[

Check warning on line 135 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L134-L135

Added lines #L134 - L135 were not covered by tests
str(self.dp_model.rcut) + "_" + str(self.dp_model.sel)
] # need to handle sel dtype.

zbl_nframe, zbl_nloc, zbl_nnei = zbl_nlist.shape
dp_nframe, dp_nloc, dp_nnei = dp_nlist.shape
zbl_atype = extended_atype[:, :zbl_nloc] # nframe, nloc should all be the same, only difference is nnei based on rcut and nlist.
zbl_atype = extended_atype[

Check warning on line 141 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L139-L141

Added lines #L139 - L141 were not covered by tests
:, :zbl_nloc
] # nframe, nloc should all be the same, only difference is nnei based on rcut and nlist.
dp_atype = extended_atype[:, :dp_nloc]

Check warning on line 144 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L144

Added line #L144 was not covered by tests

# which rr should I use? this rr should be (nfrmaes, nloc, nnei)
zbl_weight = self._compute_weight(rr, ra, rb)

Check warning on line 147 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L147

Added line #L147 was not covered by tests

dp_energy = self.dp_model.forward_atomic(extended_coord, dp_atype, nlist)["energy"]
zbl_energy = self.zbl_model.forward_atomic(extended_coord, zbl_atype, nlist)["energy"]
fit_ret = zbl_weight * zbl_energy + (1 - zbl_weight) * dp_energy # (nframes, nloc)

dp_energy = self.dp_model.forward_atomic(extended_coord, dp_atype, nlist)[

Check warning on line 149 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L149

Added line #L149 was not covered by tests
"energy"
]
zbl_energy = self.zbl_model.forward_atomic(extended_coord, zbl_atype, nlist)[

Check warning on line 152 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L152

Added line #L152 was not covered by tests
"energy"
]
fit_ret = (

Check warning on line 155 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L155

Added line #L155 was not covered by tests
zbl_weight * zbl_energy + (1 - zbl_weight) * dp_energy
) # (nframes, nloc)
return fit_ret

Check warning on line 158 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L158

Added line #L158 was not covered by tests

def _compute_weight(self, rr: torch.Tensor, ra: float, rb: float, alpha: Optional[float] = 0.1) -> torch.Tensor:
def _compute_weight(

Check warning on line 160 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L160

Added line #L160 was not covered by tests
self, rr: torch.Tensor, ra: float, rb: float, alpha: Optional[float] = 0.1
) -> torch.Tensor:
"""ZBL weight.
Parameters
----------
rr : torch.Tensor
rr : torch.Tensor
pairwise distance between atom i and atom j, (nframes, nloc, nnei).
ra : float
inclusive lower boundary of the range in which the ZBL potential and the deep potential are interpolated.
rb : float
ra : float
inclusive lower boundary of the range in which the ZBL potential and the deep potential are interpolated.
rb : float
exclusive upper boundary of the range in which the ZBL potential and the deep potential are interpolated.
alpha : float
alpha : float
a tunable scale of the distances between atoms.
Returns
-------
torch.Tensor
the atomic ZBL weight for interpolation. (nframes, nloc)
"""
assert rb > ra, "The upper boundary `rb` must be greater than the lower boundary `ra`."

sigma = torch.sum(rr*torch.exp(-rr/alpha),dim=-1)/torch.sum(torch.exp(-rr/alpha),dim=-1) # (nframes, nloc)
u = (sigma - ra)/(rb - ra)
assert (

Check warning on line 181 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L181

Added line #L181 was not covered by tests
rb > ra
), "The upper boundary `rb` must be greater than the lower boundary `ra`."

sigma = torch.sum(rr * torch.exp(-rr / alpha), dim=-1) / torch.sum(

Check warning on line 185 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L185

Added line #L185 was not covered by tests
torch.exp(-rr / alpha), dim=-1
) # (nframes, nloc)
u = (sigma - ra) / (rb - ra)
if sigma < ra:
return torch.ones_like(u)
elif ra <= sigma < rb:

Check warning

Code scanning / CodeQL

Redundant comparison Warning

Test is always true, because of
this condition
.
return -6 * u**5 + 15 * u**4 - 10 * u**3 + 1

Check warning on line 192 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L188-L192

Added lines #L188 - L192 were not covered by tests
else:
return torch.zeros_like(u)

Check warning on line 194 in deepmd/pt/model/model/linear_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/linear_model.py#L194

Added line #L194 was not covered by tests

10 changes: 3 additions & 7 deletions deepmd/pt/model/model/pair_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,10 @@ def get_sel(self) -> int:
def distinguish_types(self) -> bool:
# to match DPA1 and DPA2.
return False

def serialize(self) -> dict:
return {
"tab_file": self.tab_file,
"rcut": self.rcut,
"sel": self.sel
}

return {"tab_file": self.tab_file, "rcut": self.rcut, "sel": self.sel}

@classmethod
def deserialize(cls, data) -> "PairTabModel":
tab_file = data["tab_file"]
Expand Down
2 changes: 1 addition & 1 deletion source/tests/pt/test_pairtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_with_mask(self):

def test_jit(self):
model = torch.jit.script(self.model)

@patch("numpy.loadtxt")
def test_deserialize(self, mock_loadtxt):
file_path = "dummy_path"

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable file_path is not used.
Expand Down

0 comments on commit 39da7ff

Please sign in to comment.