deepmd.dpmodel.utils#
Submodules#
- deepmd.dpmodel.utils.batch_size
- deepmd.dpmodel.utils.env_mat
- deepmd.dpmodel.utils.env_mat_stat
- deepmd.dpmodel.utils.exclude_mask
- deepmd.dpmodel.utils.learning_rate
- deepmd.dpmodel.utils.neighbor_stat
- deepmd.dpmodel.utils.network
- deepmd.dpmodel.utils.nlist
- deepmd.dpmodel.utils.region
- deepmd.dpmodel.utils.safe_gradient
- deepmd.dpmodel.utils.seed
- deepmd.dpmodel.utils.serialization
- deepmd.dpmodel.utils.type_embed
- deepmd.dpmodel.utils.update_sel
Attributes#
Classes#
The unit operation of a native model. | |
Computes the type exclusion mask for atoms. | |
Computes the type exclusion mask for atom pairs. | |
Native representation of a layer. | |
A collection of networks for multiple elements. |
Functions#
| Aggregate rows in data by specifying the owners. |
| Get the index mapping for edge graph and angle graph, ready in aggregate or index_select. |
| |
| |
| |
| Input one neighbor list, and produce multiple neighbor lists with |
| Build neighbor list for a single frame. keeps nsel neighbors. |
Extend the coordinates of the atoms by appending peridoc images. | |
| |
| Given a nlist that does not distinguish atom types, return a nlist that |
| Convert internal(direct) coordinates to physical coordinates. |
| Apply PBC according to the atomic coordinates. |
| Convert physical coordinates to internal(direct) coordinates. |
| Compute the to-face-distance of the simulation cell. |
| Load a DP model from a file in the native format. |
| Save a DP model to a file in the native format. |
| Traverse a model dict and call callback on each variable. |
Package Contents#
- class deepmd.dpmodel.utils.EnvMat(rcut: float, rcut_smth: float, protection: float = 0.0, use_exp_switch: bool = False)[source]#
Bases:
deepmd.dpmodel.NativeOPThe unit operation of a native model.
- rcut#
- rcut_smth#
- protection = 0.0#
- use_exp_switch = False#
- call(coord_ext: deepmd.dpmodel.array_api.Array, atype_ext: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array, davg: deepmd.dpmodel.array_api.Array | None = None, dstd: deepmd.dpmodel.array_api.Array | None = None, radial_only: bool = False) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array][source]#
Compute the environment matrix.
- Parameters:
- nlist
The neighbor list. shape: nf x nloc x nnei
- coord_ext
The extended coordinates of atoms. shape: nf x (nallx3)
- atype_ext
The extended aotm types. shape: nf x nall
- davg
The data avg. shape: nt x nnei x (4 or 1)
- dstd
The inverse of data std. shape: nt x nnei x (4 or 1)
- radial_only
Whether to only compute radial part of the environment matrix. If True, the output will be of shape nf x nloc x nnei x 1. Otherwise, the output will be of shape nf x nloc x nnei x 4. Default: False.
- Returns:
env_matThe environment matrix. shape: nf x nloc x nnei x (4 or 1)
diffThe relative coordinate of neighbors. shape: nf x nloc x nnei x 3
switchThe value of switch function. shape: nf x nloc x nnei
- class deepmd.dpmodel.utils.AtomExcludeMask(ntypes: int, exclude_types: list[int] = [])[source]#
Computes the type exclusion mask for atoms.
- ntypes#
- exclude_types = []#
- type_mask#
- build_type_exclude_mask(atype: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Compute type exclusion mask for atoms.
- Parameters:
- atype
The extended aotm types. shape: nf x natom
- Returns:
maskThe type exclusion mask for atoms. shape: nf x natom Element [ff,ii] being 0 if type(ii) is excluded, otherwise being 1.
- class deepmd.dpmodel.utils.PairExcludeMask(ntypes: int, exclude_types: list[tuple[int, int]] = [])[source]#
Computes the type exclusion mask for atom pairs.
- ntypes#
- exclude_types#
- type_mask#
- build_type_exclude_mask(nlist: deepmd.dpmodel.array_api.Array, atype_ext: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Compute type exclusion mask for atom pairs.
- Parameters:
- nlist
The neighbor list. shape: nf x nloc x nnei
- atype_ext
The extended aotm types. shape: nf x nall
- Returns:
maskThe type exclusion mask for pair atoms of shape: nf x nloc x nnei. Element [ff,ii,jj] being 0 if type(ii), type(nlist[ff,ii,jj]) is excluded, otherwise being 1.
- class deepmd.dpmodel.utils.NativeLayer(num_in: int, num_out: int, bias: bool = True, use_timestep: bool = False, activation_function: str | None = None, resnet: bool = False, precision: str = DEFAULT_PRECISION, seed: int | list[int] | None = None, trainable: bool = True)[source]#
Bases:
deepmd.dpmodel.NativeOPNative representation of a layer.
- Parameters:
- w
Array,optional The weights of the layer.
- b
Array,optional The biases of the layer.
- idt
Array,optional The identity matrix of the layer.
- activation_function
str,optional The activation function of the layer.
- resnetbool,
optional Whether the layer is a residual layer.
- precision
str,optional The precision of the layer.
- seed
int,optional Random seed.
- trainablebool, default=True
Whether the layer is trainable.
- w
- trainable = True#
- precision = 'float64'#
- w#
- b#
- idt#
- activation_function = None#
- resnet = False#
- classmethod deserialize(data: dict) NativeLayer[source]#
Deserialize the layer from a dict.
- Parameters:
- data
dict The dict to deserialize from.
- data
- call(x)[source]#
Forward pass.
- Parameters:
- x
Array The input.
- x
- Returns:
np.ndarrayThe output.
- class deepmd.dpmodel.utils.NetworkCollection(ndim: int, ntypes: int, network_type: str = 'network', networks: list[NativeNet | dict] = [])[source]#
A collection of networks for multiple elements.
The number of dimensions for types might be 0, 1, or 2. - 0: embedding or fitting with type embedding, in () - 1: embedding with type_one_side, or fitting, in (type_i) - 2: embedding without type_one_side, in (type_i, type_j)
- Parameters:
- ndim#
- ntypes#
- network_type#
- _networks#
- check_completeness() None[source]#
Check whether the collection is complete.
- Raises:
RuntimeErrorIf the collection is incomplete.
- classmethod deserialize(data: dict) NetworkCollection[source]#
Deserialize the networks from a dict.
- Parameters:
- data
dict The dict to deserialize from.
- data
- deepmd.dpmodel.utils.aggregate(data, owners, average: bool = True, num_owner: int | None = None)[source]#
Aggregate rows in data by specifying the owners.
- Parameters:
- Returns:
- output: [
num_owner,feature_dim]
- output: [
- deepmd.dpmodel.utils.get_graph_index(nlist, nlist_mask, a_nlist_mask, nall: int, use_loc_mapping: bool = True)[source]#
Get the index mapping for edge graph and angle graph, ready in aggregate or index_select.
- Parameters:
- nlist
nfxnlocxnnei Neighbor list. (padded neis are set to 0)
- nlist_mask
nfxnlocxnnei Masks of the neighbor list. real nei 1 otherwise 0
- a_nlist_mask
nfxnlocxa_nnei Masks of the neighbor list for angle. real nei 1 otherwise 0
- nall
The number of extended atoms.
- use_loc_mapping
Whether to use local atom index mapping in training or non-parallel inference. When True, local indexing and mapping are applied to neighbor lists and embeddings during descriptor computation.
- nlist
- Returns:
- edge_index2
xn_edge - n2e_indexn_edge
Broadcast indices from node(i) to edge(ij), or reduction indices from edge(ij) to node(i).
- n_ext2e_indexn_edge
Broadcast indices from extended node(j) to edge(ij).
- angle_index3
xn_angle - n2a_indexn_angle
Broadcast indices from extended node(j) to angle(ijk).
- eij2a_indexn_angle
Broadcast indices from extended edge(ij) to angle(ijk), or reduction indices from angle(ijk) to edge(ij).
- eik2a_indexn_angle
Broadcast indices from extended edge(ik) to angle(ijk).
- edge_index2
- deepmd.dpmodel.utils.make_fitting_network(T_EmbeddingNet: type, T_Network: type, T_NetworkLayer: type) type[source]#
- deepmd.dpmodel.utils.build_multiple_neighbor_list(coord: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array, rcuts: list[float], nsels: list[int]) dict[str, deepmd.dpmodel.array_api.Array][source]#
Input one neighbor list, and produce multiple neighbor lists with different cutoff radius and numbers of selection out of it. The required rcuts and nsels should be smaller or equal to the input nlist.
- Parameters:
- Returns:
- deepmd.dpmodel.utils.build_neighbor_list(coord: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, nloc: int, rcut: float, sel: int | list[int], distinguish_types: bool = True) deepmd.dpmodel.array_api.Array[source]#
Build neighbor list for a single frame. keeps nsel neighbors.
- Parameters:
- coord
Array exptended coordinates of shape [batch_size, nall x 3]
- atype
Array extended atomic types of shape [batch_size, nall] type < 0 the atom is treat as virtual atoms.
- nloc
int number of local atoms.
- rcut
float cut-off radius
- sel
intorlist[int] maximal number of neighbors (of each type). if distinguish_types==True, nsel should be list and the length of nsel should be equal to number of types.
- distinguish_typesbool
distinguish different types.
- coord
- Returns:
- neighbor_list
Array Neighbor list of shape [batch_size, nloc, nsel], the neighbors are stored in an ascending order. If the number of neighbors is less than nsel, the positions are masked with -1. The neighbor list of an atom looks like |------ nsel ------| xx xx xx xx -1 -1 -1 if distinguish_types==True and we have two types |---- nsel[0] -----| |---- nsel[1] -----| xx xx xx xx -1 -1 -1 xx xx xx -1 -1 -1 -1 For virtual atoms all neighboring positions are filled with -1.
- neighbor_list
- deepmd.dpmodel.utils.extend_coord_with_ghosts(coord: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, cell: deepmd.dpmodel.array_api.Array | None, rcut: float) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array][source]#
Extend the coordinates of the atoms by appending peridoc images. The number of images is large enough to ensure all the neighbors within rcut are appended.
- Parameters:
- coord
Array original coordinates of shape [-1, nloc*3].
- atype
Array atom type of shape [-1, nloc].
- cell
Array simulation cell tensor of shape [-1, 9].
- rcut
float the cutoff radius
- coord
- Returns:
- extended_coord:
Array extended coordinates of shape [-1, nall*3].
- extended_atype:
Array extended atom type of shape [-1, nall].
- index_mapping:
Array mapping extended index to the local index
- extended_coord:
- deepmd.dpmodel.utils.nlist_distinguish_types(nlist: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, sel: list[int]) deepmd.dpmodel.array_api.Array[source]#
Given a nlist that does not distinguish atom types, return a nlist that distinguish atom types.
- deepmd.dpmodel.utils.inter2phys(coord: deepmd.dpmodel.array_api.Array, cell: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Convert internal(direct) coordinates to physical coordinates.
- deepmd.dpmodel.utils.normalize_coord(coord: deepmd.dpmodel.array_api.Array, cell: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Apply PBC according to the atomic coordinates.
- deepmd.dpmodel.utils.phys2inter(coord: deepmd.dpmodel.array_api.Array, cell: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Convert physical coordinates to internal(direct) coordinates.
- deepmd.dpmodel.utils.to_face_distance(cell: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
Compute the to-face-distance of the simulation cell.
- deepmd.dpmodel.utils.load_dp_model(filename: str) dict[source]#
Load a DP model from a file in the native format.
- deepmd.dpmodel.utils.save_dp_model(filename: str, model_dict: dict) None[source]#
Save a DP model to a file in the native format.
- deepmd.dpmodel.utils.traverse_model_dict(model_obj: Any, callback: Callable, is_variable: bool = False) Any[source]#
Traverse a model dict and call callback on each variable.
- Parameters:
- model_obj
object The model object to traverse.
- callback
callable() The callback function to call on each variable.
- is_variablebool,
optional Whether the current node is a variable.
- model_obj
- Returns:
objectThe model object after traversing.