|
20 | 20 | OutputVariableCategory, |
21 | 21 | OutputVariableDef, |
22 | 22 | ) |
| 23 | +from deepmd.infer.deep_density import ( |
| 24 | + DeepDensity, |
| 25 | +) |
23 | 26 | from deepmd.infer.deep_dipole import ( |
24 | 27 | DeepDipole, |
25 | 28 | ) |
@@ -440,6 +443,8 @@ def model_type(self) -> type["DeepEvalWrapper"]: |
440 | 443 | return DeepWFC |
441 | 444 | elif "population" in model_output_type: |
442 | 445 | return DeepPopulation |
| 446 | + elif "density" in model_output_type: |
| 447 | + return DeepDensity |
443 | 448 | elif self.get_var_name() in model_output_type: |
444 | 449 | return DeepProperty |
445 | 450 | else: |
@@ -552,6 +557,17 @@ def eval( |
552 | 557 | coords, atom_types, len(atom_types.shape) > 1 |
553 | 558 | ) |
554 | 559 | request_defs = self._get_request_defs(atomic) |
| 560 | + if "grid" in kwargs and kwargs["grid"] is not None: |
| 561 | + out = self._eval_func(self._eval_model_density, numb_test, natoms)( |
| 562 | + coords, |
| 563 | + cells, |
| 564 | + atom_types, |
| 565 | + np.array(kwargs["grid"]), |
| 566 | + fparam, |
| 567 | + aparam, |
| 568 | + request_defs, |
| 569 | + ) |
| 570 | + return {"density": out} |
555 | 571 | if "spin" not in kwargs or kwargs["spin"] is None: |
556 | 572 | out = self._eval_func(self._eval_model, numb_test, natoms)( |
557 | 573 | coords, cells, atom_types, fparam, aparam, request_defs, charge_spin |
@@ -916,6 +932,80 @@ def _eval_model_spin( |
916 | 932 | ) # this is kinda hacky |
917 | 933 | return tuple(results) |
918 | 934 |
|
| 935 | + def _eval_model_density( |
| 936 | + self, |
| 937 | + coords: np.ndarray, |
| 938 | + cells: np.ndarray | None, |
| 939 | + atom_types: np.ndarray, |
| 940 | + grid: np.ndarray, |
| 941 | + fparam: np.ndarray | None, |
| 942 | + aparam: np.ndarray | None, |
| 943 | + request_defs: list[OutputVariableDef], |
| 944 | + ) -> tuple[np.ndarray, ...]: |
| 945 | + model = self.dp.to(DEVICE) |
| 946 | + |
| 947 | + nframes = coords.shape[0] |
| 948 | + if len(atom_types.shape) == 1: |
| 949 | + natoms = len(atom_types) |
| 950 | + atom_types = np.tile(atom_types, nframes).reshape(nframes, -1) |
| 951 | + else: |
| 952 | + natoms = len(atom_types[0]) |
| 953 | + |
| 954 | + coord_input = torch.tensor( |
| 955 | + coords.reshape([nframes, natoms, 3]), |
| 956 | + dtype=GLOBAL_PT_FLOAT_PRECISION, |
| 957 | + device=DEVICE, |
| 958 | + ) |
| 959 | + type_input = torch.tensor(atom_types, dtype=torch.long, device=DEVICE) |
| 960 | + grid_input = torch.tensor( |
| 961 | + grid.reshape([nframes, -1, 3]), |
| 962 | + dtype=GLOBAL_PT_FLOAT_PRECISION, |
| 963 | + device=DEVICE, |
| 964 | + ) |
| 965 | + ngrid = grid_input.shape[1] |
| 966 | + if cells is not None: |
| 967 | + box_input = torch.tensor( |
| 968 | + cells.reshape([nframes, 3, 3]), |
| 969 | + dtype=GLOBAL_PT_FLOAT_PRECISION, |
| 970 | + device=DEVICE, |
| 971 | + ) |
| 972 | + else: |
| 973 | + box_input = None |
| 974 | + if fparam is not None: |
| 975 | + fparam_input = to_torch_tensor( |
| 976 | + fparam.reshape(nframes, self.get_dim_fparam()) |
| 977 | + ) |
| 978 | + else: |
| 979 | + fparam_input = None |
| 980 | + if aparam is not None: |
| 981 | + aparam_input = to_torch_tensor( |
| 982 | + aparam.reshape(nframes, natoms, self.get_dim_aparam()) |
| 983 | + ) |
| 984 | + else: |
| 985 | + aparam_input = None |
| 986 | + |
| 987 | + do_atomic_virial = any( |
| 988 | + x.category == OutputVariableCategory.DERV_C_REDU for x in request_defs |
| 989 | + ) |
| 990 | + batch_output = model( |
| 991 | + coord_input, |
| 992 | + type_input, |
| 993 | + grid=grid_input, |
| 994 | + box=box_input, |
| 995 | + do_atomic_virial=do_atomic_virial, |
| 996 | + fparam=fparam_input, |
| 997 | + aparam=aparam_input, |
| 998 | + ) |
| 999 | + if isinstance(batch_output, tuple): |
| 1000 | + batch_output = batch_output[0] |
| 1001 | + |
| 1002 | + results = [] |
| 1003 | + pt_name = "density" |
| 1004 | + density_shape = [nframes, ngrid] |
| 1005 | + out = batch_output[pt_name].reshape(density_shape).detach().cpu().numpy() |
| 1006 | + results.append(out) |
| 1007 | + return tuple(results) |
| 1008 | + |
919 | 1009 | def _get_output_shape( |
920 | 1010 | self, odef: OutputVariableDef, nframes: int, natoms: int |
921 | 1011 | ) -> list[int]: |
|
0 commit comments