forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdp_atomic_model.py
More file actions
371 lines (324 loc) · 12.2 KB
/
Copy pathdp_atomic_model.py
File metadata and controls
371 lines (324 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# SPDX-License-Identifier: LGPL-3.0-or-later
from collections.abc import (
Callable,
)
from typing import (
Any,
)
from deepmd.dpmodel.array_api import (
Array,
xp_take_first_n,
)
from deepmd.dpmodel.descriptor.base_descriptor import (
BaseDescriptor,
)
from deepmd.dpmodel.fitting.base_fitting import (
BaseFitting,
)
from deepmd.dpmodel.output_def import (
FittingOutputDef,
)
from deepmd.utils.path import (
DPPath,
)
from deepmd.utils.version import (
check_version_compatibility,
)
from .base_atomic_model import (
BaseAtomicModel,
)
@BaseAtomicModel.register("standard")
class DPAtomicModel(BaseAtomicModel):
r"""Model give atomic prediction of some physical property.
The atomic model computes atomic properties by first extracting a descriptor
from the atomic environment, then passing it through a fitting network:
.. math::
\mathcal{D}^i = \mathcal{D}(\mathbf{R}^i, \mathbf{R}_j, \alpha_j),
.. math::
\mathbf{y}^i = \mathcal{F}(\mathcal{D}^i),
where :math:`\mathcal{D}^i` is the descriptor for atom :math:`i`,
:math:`\alpha_j` is the atom type of neighbor :math:`j`,
:math:`\mathcal{F}` is the fitting network, and
:math:`\mathbf{y}^i` is the predicted atomic property (energy, dipole, etc.).
Parameters
----------
descriptor
Descriptor
fitting_net
Fitting net
type_map
Mapping atom type to the name (str) of the type.
For example `type_map[1]` gives the name of the type 1.
"""
def __init__(
self,
descriptor: BaseDescriptor,
fitting: BaseFitting,
type_map: list[str],
**kwargs: Any,
) -> None:
super().__init__(type_map, **kwargs)
self.descriptor = descriptor
self.fitting_net = fitting
if hasattr(self.fitting_net, "reinit_exclude"):
self.fitting_net.reinit_exclude(self.atom_exclude_types)
self.type_map = type_map
self.add_chg_spin_ebd: bool = getattr(
self.descriptor, "add_chg_spin_ebd", False
)
super().init_out_stat()
def fitting_output_def(self) -> FittingOutputDef:
"""Get the output def of the fitting net."""
return self.fitting_net.output_def()
def get_rcut(self) -> float:
"""Get the cut-off radius."""
return self.descriptor.get_rcut()
def get_sel(self) -> list[int]:
"""Get the neighbor selection."""
return self.descriptor.get_sel()
def set_case_embd(self, case_idx: int) -> None:
"""
Set the case embedding of this atomic model by the given case_idx,
typically concatenated with the output of the descriptor and fed into the fitting net.
"""
self.fitting_net.set_case_embd(case_idx)
def mixed_types(self) -> bool:
"""If true, the model
1. assumes total number of atoms aligned across frames;
2. uses a neighbor list that does not distinguish different atomic types.
If false, the model
1. assumes total number of atoms of each atom type aligned across frames;
2. uses a neighbor list that distinguishes different atomic types.
"""
return self.descriptor.mixed_types()
def has_message_passing(self) -> bool:
"""Returns whether the atomic model has message passing."""
return self.descriptor.has_message_passing()
def need_sorted_nlist_for_lower(self) -> bool:
"""Returns whether the atomic model needs sorted nlist when using `forward_lower`."""
return self.descriptor.need_sorted_nlist_for_lower()
def enable_compression(
self,
min_nbor_dist: float,
table_extrapolate: float = 5,
table_stride_1: float = 0.01,
table_stride_2: float = 0.1,
check_frequency: int = -1,
) -> None:
"""Call descriptor enable_compression().
Parameters
----------
min_nbor_dist
The nearest distance between atoms
table_extrapolate
The scale of model extrapolation
table_stride_1
The uniform stride of the first table
table_stride_2
The uniform stride of the second table
check_frequency
The overflow check frequency
"""
self.descriptor.enable_compression(
min_nbor_dist,
table_extrapolate,
table_stride_1,
table_stride_2,
check_frequency,
)
def forward_atomic(
self,
extended_coord: Array,
extended_atype: Array,
nlist: Array,
mapping: Array | None = None,
fparam: Array | None = None,
aparam: Array | None = None,
) -> dict[str, Array]:
"""Models' atomic predictions.
Parameters
----------
extended_coord
coordinates in extended region
extended_atype
atomic type in extended region
nlist
neighbor list. nf x nloc x nsel
mapping
mapps the extended indices to local indices. nf x nall
fparam
frame parameter. nf x ndf
aparam
atomic parameter. nf x nloc x nda
Returns
-------
result_dict
the result dict, defined by the `FittingOutputDef`.
"""
nframes, nloc, nnei = nlist.shape
atype = xp_take_first_n(extended_atype, 1, nloc)
# Handle default fparam if fitting net supports it
if (
hasattr(self.fitting_net, "get_dim_fparam")
and self.fitting_net.get_dim_fparam() > 0
and fparam is None
):
# use default fparam
from deepmd.dpmodel.array_api import (
array_api_compat,
)
default_fparam = self.fitting_net.get_default_fparam()
assert default_fparam is not None
xp = array_api_compat.array_namespace(extended_coord)
default_fparam_array = xp.asarray(
default_fparam,
dtype=extended_coord.dtype,
device=array_api_compat.device(extended_coord),
)
fparam_input_for_des = xp.tile(
xp.reshape(default_fparam_array, (1, -1)), (nframes, 1)
)
else:
fparam_input_for_des = fparam
descriptor, rot_mat, g2, h2, sw = self.descriptor(
extended_coord,
extended_atype,
nlist,
mapping=mapping,
fparam=fparam_input_for_des if self.add_chg_spin_ebd else None,
)
ret = self.fitting_net(
descriptor,
atype,
gr=rot_mat,
g2=g2,
h2=h2,
fparam=fparam,
aparam=aparam,
)
return ret
def compute_or_load_stat(
self,
sampled_func: Callable[[], list[dict]],
stat_file_path: DPPath | None = None,
compute_or_load_out_stat: bool = True,
preset_observed_type: list[str] | None = None,
) -> None:
"""Compute or load the statistics parameters of the model,
such as mean and standard deviation of descriptors or the energy bias of the fitting net.
Parameters
----------
sampled_func
The lazy sampled function to get data frames from different data systems.
stat_file_path
The path to the stat file.
compute_or_load_out_stat : bool
Whether to compute the output statistics.
If False, it will only compute the input statistics
(e.g. mean and standard deviation of descriptors).
"""
if stat_file_path is not None and self.type_map is not None:
stat_file_path /= " ".join(self.type_map)
wrapped_sampler = self._make_wrapped_sampler(sampled_func)
self.descriptor.compute_input_stats(wrapped_sampler, stat_file_path)
self.fitting_net.compute_input_stats(
wrapped_sampler, stat_file_path=stat_file_path
)
if compute_or_load_out_stat:
self.compute_or_load_out_stat(wrapped_sampler, stat_file_path)
self._collect_and_set_observed_type(
wrapped_sampler, stat_file_path, preset_observed_type
)
def change_type_map(
self, type_map: list[str], model_with_new_type_stat: Any | None = None
) -> None:
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
"""
super().change_type_map(
type_map=type_map, model_with_new_type_stat=model_with_new_type_stat
)
self.type_map = type_map
self.descriptor.change_type_map(
type_map=type_map,
model_with_new_type_stat=model_with_new_type_stat.descriptor
if model_with_new_type_stat is not None
else None,
)
self.fitting_net.change_type_map(type_map=type_map)
def compute_fitting_input_stat(
self,
sample_merged: Callable[[], list[dict]] | list[dict],
stat_file_path: DPPath | None = None,
) -> None:
"""Compute the input statistics (e.g. mean and stddev) for the fittings from packed data.
Parameters
----------
sample_merged : Union[Callable[[], list[dict]], list[dict]]
- list[dict]: A list of data samples from various data systems.
Each element, ``merged[i]``, is a data dictionary containing
``keys``: ``np.ndarray`` originating from the ``i``-th data system.
- Callable[[], list[dict]]: A lazy function that returns data samples
in the above format only when needed.
stat_file_path : Optional[DPPath]
The path to the stat file.
"""
self.fitting_net.compute_input_stats(
sample_merged,
protection=self.data_stat_protect,
stat_file_path=stat_file_path,
)
def serialize(self) -> dict:
dd = super().serialize()
dd.update(
{
"@class": "Model",
"type": "standard",
"@version": 2,
"type_map": self.type_map,
"descriptor": self.descriptor.serialize(),
"fitting": self.fitting_net.serialize(),
}
)
return dd
# for subclass overridden
base_descriptor_cls = BaseDescriptor
"""The base descriptor class."""
base_fitting_cls = BaseFitting
"""The base fitting class."""
@classmethod
def deserialize(cls, data: dict[str, Any]) -> "DPAtomicModel":
data = data.copy()
check_version_compatibility(data.pop("@version", 1), 2, 2)
data.pop("@class")
data.pop("type")
descriptor_obj = cls.base_descriptor_cls.deserialize(data.pop("descriptor"))
fitting_obj = cls.base_fitting_cls.deserialize(data.pop("fitting"))
data["descriptor"] = descriptor_obj
data["fitting"] = fitting_obj
obj = super().deserialize(data)
return obj
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return self.fitting_net.get_dim_fparam()
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return self.fitting_net.get_dim_aparam()
def has_default_fparam(self) -> bool:
"""Check if the model has default frame parameters."""
return self.fitting_net.has_default_fparam()
def get_default_fparam(self) -> list[float] | None:
"""Get the default frame parameters."""
return self.fitting_net.get_default_fparam()
def get_sel_type(self) -> list[int]:
"""Get the selected atom types of this model.
Only atoms with selected atom types have atomic contribution
to the result of the model.
If returning an empty list, all atom types are selected.
"""
return self.fitting_net.get_sel_type()
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).
If False, the shape is (nframes, nloc, ndim).
"""
return False