deepmd.dpmodel.utils package
- class deepmd.dpmodel.utils.AtomExcludeMask(ntypes: int, exclude_types: List[int] = [])[source]
Bases:
objectComputes the type exclusion mask for atoms.
Methods
build_type_exclude_mask(atype)Compute type exclusion mask for atoms.
- deepmd.dpmodel.utils.EmbeddingNet
alias of
EN
- class deepmd.dpmodel.utils.EnvMat(rcut, rcut_smth)[source]
Bases:
NativeOPMethods
__call__(*args, **kwargs)Forward pass in NumPy implementation.
call(coord_ext, atype_ext, nlist[, davg, ...])Compute the environment matrix.
deserialize
serialize
- call(coord_ext: ndarray, atype_ext: ndarray, nlist: ndarray, davg: Optional[ndarray] = None, dstd: Optional[ndarray] = None, radial_only: bool = False) ndarray[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)
switchThe value of switch function. shape: nf x nloc x nnei
- deepmd.dpmodel.utils.FittingNet
alias of
FN
- class deepmd.dpmodel.utils.NativeLayer(num_in, num_out, bias: bool = True, use_timestep: bool = False, activation_function: Optional[str] = None, resnet: bool = False, precision: str = 'float64')[source]
Bases:
NativeOPNative representation of a layer.
- Parameters
- w
np.ndarray,optional The weights of the layer.
- b
np.ndarray,optional The biases of the layer.
- idt
np.ndarray,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.
- w
Methods
__call__(*args, **kwargs)Forward pass in NumPy implementation.
call(x)Forward pass.
deserialize(data)Deserialize the layer from a dict.
Serialize the layer to a dict.
check_shape_consistency
check_type_consistency
dim_in
dim_out
- call(x: ndarray) ndarray[source]
Forward pass.
- Parameters
- x
np.ndarray The input.
- x
- Returns
np.ndarrayThe output.
- classmethod deserialize(data: dict) NativeLayer[source]
Deserialize the layer from a dict.
- Parameters
- data
dict The dict to deserialize from.
- data
- deepmd.dpmodel.utils.NativeNet
alias of
NN
- class deepmd.dpmodel.utils.NetworkCollection(ndim: int, ntypes: int, network_type: str = 'network', networks: ~typing.List[~typing.Union[~deepmd.dpmodel.utils.network.make_multilayer_network.<locals>.NN, dict]] = [])[source]
Bases:
objectA collection of networks for multiple elements.
The number of dimesions 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
Methods
Check whether the collection is complete.
deserialize(data)Deserialize the networks from a dict.
Serialize the networks to a dict.
- NETWORK_TYPE_MAP: ClassVar[Dict[str, type]] = {'embedding_network': <class 'deepmd.dpmodel.utils.network.make_embedding_network.<locals>.EN'>, 'fitting_network': <class 'deepmd.dpmodel.utils.network.make_fitting_network.<locals>.FN'>, 'network': <class 'deepmd.dpmodel.utils.network.make_multilayer_network.<locals>.NN'>}
- check_completeness()[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
- class deepmd.dpmodel.utils.PairExcludeMask(ntypes: int, exclude_types: List[Tuple[int, int]] = [])[source]
Bases:
objectComputes the type exclusion mask for atom pairs.
Methods
build_type_exclude_mask(nlist, atype_ext)Compute type exclusion mask for atom pairs.
- build_type_exclude_mask(nlist: ndarray, atype_ext: ndarray)[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.
- deepmd.dpmodel.utils.build_multiple_neighbor_list(coord: ndarray, nlist: ndarray, rcuts: List[float], nsels: List[int]) Dict[str, ndarray][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
- coord
np.ndarray exptended coordinates of shape [batch_size, nall x 3]
- nlist
np.ndarray Neighbor list of shape [batch_size, nloc, nsel], the neighbors should be stored in an ascending order.
- rcuts
List[float] list of cut-off radius in ascending order.
- nsels
List[int] maximal number of neighbors in ascending order.
- coord
- Returns
- nlist_dict
Dict[str,np.ndarray] A dict of nlists, key given by get_multiple_nlist_key(rc, nsel) value being the corresponding nlist.
- nlist_dict
- deepmd.dpmodel.utils.build_neighbor_list(coord1: ndarray, atype: ndarray, nloc: int, rcut: float, sel: Union[int, List[int]], distinguish_types: bool = True) ndarray[source]
Build neightbor list for a single frame. keeps nsel neighbors.
- Parameters
- coord1
np.ndarray exptended coordinates of shape [batch_size, nall x 3]
- atype
np.ndarray extended atomic types of shape [batch_size, nall]
- 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.
- coord1
- Returns
- neighbor_list
np.ndarray 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
- neighbor_list
- deepmd.dpmodel.utils.extend_coord_with_ghosts(coord: ndarray, atype: ndarray, cell: Optional[ndarray], rcut: float)[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
np.ndarray original coordinates of shape [-1, nloc*3].
- atype
np.ndarray atom type of shape [-1, nloc].
- cell
np.ndarray simulation cell tensor of shape [-1, 9].
- rcut
float the cutoff radius
- coord
- Returns
- extended_coord:
np.ndarray extended coordinates of shape [-1, nall*3].
- extended_atype:
np.ndarray extended atom type of shape [-1, nall].
- index_mapping:
np.ndarray maping extended index to the local index
- extended_coord:
- deepmd.dpmodel.utils.inter2phys(coord: ndarray, cell: ndarray) ndarray[source]
Convert internal(direct) coordinates to physical coordinates.
- Parameters
- coord
np.ndarray internal coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- coord
- Returns
- phys_coord:
np.ndarray the physical coordinates
- phys_coord:
- deepmd.dpmodel.utils.load_dp_model(filename: str) dict[source]
Load a DP model from a file in the native format.
- deepmd.dpmodel.utils.nlist_distinguish_types(nlist: ndarray, atype: ndarray, sel: List[int])[source]
Given a nlist that does not distinguish atom types, return a nlist that distinguish atom types.
- deepmd.dpmodel.utils.normalize_coord(coord: ndarray, cell: ndarray) ndarray[source]
Apply PBC according to the atomic coordinates.
- Parameters
- coord
np.ndarray orignal coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell shape [*, 3, 3].
- coord
- Returns
- wrapped_coord:
np.ndarray wrapped coordinates of shape [*, na, 3].
- wrapped_coord:
- deepmd.dpmodel.utils.phys2inter(coord: ndarray, cell: ndarray) ndarray[source]
Convert physical coordinates to internal(direct) coordinates.
- Parameters
- coord
np.ndarray physical coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- coord
- Returns
- inter_coord:
np.ndarray the internal coordinates
- inter_coord:
- 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.to_face_distance(cell: ndarray) ndarray[source]
Compute the to-face-distance of the simulation cell.
- Parameters
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- cell
- Returns
- dist:
np.ndarray the to face distances of shape [*, 3]
- dist:
- deepmd.dpmodel.utils.traverse_model_dict(model_obj, callback: callable, is_variable: bool = False)[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.
Submodules
deepmd.dpmodel.utils.batch_size module
- class deepmd.dpmodel.utils.batch_size.AutoBatchSize(initial_batch_size: int = 1024, factor: float = 2.0)[source]
Bases:
AutoBatchSizeAutomatic batch size for NumPy.
Methods
execute(callable, start_index, natoms)Excuate a method with given batch size.
execute_all(callable, total_size, natoms, ...)Excuate a method with all given data.
Check if GPU is available.
is_oom_error(e)Check if the exception is an OOM error.
deepmd.dpmodel.utils.env_mat module
- class deepmd.dpmodel.utils.env_mat.EnvMat(rcut, rcut_smth)[source]
Bases:
NativeOPMethods
__call__(*args, **kwargs)Forward pass in NumPy implementation.
call(coord_ext, atype_ext, nlist[, davg, ...])Compute the environment matrix.
deserialize
serialize
- call(coord_ext: ndarray, atype_ext: ndarray, nlist: ndarray, davg: Optional[ndarray] = None, dstd: Optional[ndarray] = None, radial_only: bool = False) ndarray[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)
switchThe value of switch function. shape: nf x nloc x nnei
deepmd.dpmodel.utils.exclude_mask module
- class deepmd.dpmodel.utils.exclude_mask.AtomExcludeMask(ntypes: int, exclude_types: List[int] = [])[source]
Bases:
objectComputes the type exclusion mask for atoms.
Methods
build_type_exclude_mask(atype)Compute type exclusion mask for atoms.
- class deepmd.dpmodel.utils.exclude_mask.PairExcludeMask(ntypes: int, exclude_types: List[Tuple[int, int]] = [])[source]
Bases:
objectComputes the type exclusion mask for atom pairs.
Methods
build_type_exclude_mask(nlist, atype_ext)Compute type exclusion mask for atom pairs.
- build_type_exclude_mask(nlist: ndarray, atype_ext: ndarray)[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.
deepmd.dpmodel.utils.neighbor_stat module
- class deepmd.dpmodel.utils.neighbor_stat.NeighborStat(ntypes: int, rcut: float, mixed_type: bool = False)[source]
Bases:
NeighborStatNeighbor statistics using pure NumPy.
- Parameters
Methods
get_stat(data)Get the data statistics of the training data, including nearest nbor distance between atoms, max nbor size of atoms.
iterator(data)Abstract method for producing data.
- class deepmd.dpmodel.utils.neighbor_stat.NeighborStatOP(ntypes: int, rcut: float, mixed_types: bool)[source]
Bases:
NativeOPClass for getting neighbor statics data information.
- Parameters
- ntypes
The num of atom types
- rcut
The cut-off radius
- mixed_typesbool,
optional If True, treat all types as a single type.
Methods
__call__(*args, **kwargs)Forward pass in NumPy implementation.
call(coord, atype, cell)Calculate the neareest neighbor distance between atoms, maximum nbor size of atoms and the output data range of the environment matrix.
- call(coord: ndarray, atype: ndarray, cell: Optional[ndarray]) Tuple[float, ndarray][source]
Calculate the neareest neighbor distance between atoms, maximum nbor size of atoms and the output data range of the environment matrix.
- Parameters
- coord
The coordinates of atoms.
- atype
The atom types.
- cell
The cell.
- Returns
floatThe minimal squared distance between two atoms
np.ndarrayThe maximal number of neighbors
deepmd.dpmodel.utils.network module
Native DP model format for multiple backends.
See issue #2982 for more information.
- class deepmd.dpmodel.utils.network.Counter[source]
Bases:
objectA callable counter.
Examples
>>> counter = Counter() >>> counter() 0 >>> counter() 1
Methods
__call__()Call self as a function.
- deepmd.dpmodel.utils.network.EmbeddingNet
alias of
EN
- deepmd.dpmodel.utils.network.FittingNet
alias of
FN
- class deepmd.dpmodel.utils.network.NativeLayer(num_in, num_out, bias: bool = True, use_timestep: bool = False, activation_function: Optional[str] = None, resnet: bool = False, precision: str = 'float64')[source]
Bases:
NativeOPNative representation of a layer.
- Parameters
- w
np.ndarray,optional The weights of the layer.
- b
np.ndarray,optional The biases of the layer.
- idt
np.ndarray,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.
- w
Methods
__call__(*args, **kwargs)Forward pass in NumPy implementation.
call(x)Forward pass.
deserialize(data)Deserialize the layer from a dict.
Serialize the layer to a dict.
check_shape_consistency
check_type_consistency
dim_in
dim_out
- call(x: ndarray) ndarray[source]
Forward pass.
- Parameters
- x
np.ndarray The input.
- x
- Returns
np.ndarrayThe output.
- classmethod deserialize(data: dict) NativeLayer[source]
Deserialize the layer from a dict.
- Parameters
- data
dict The dict to deserialize from.
- data
- deepmd.dpmodel.utils.network.NativeNet
alias of
NN
- class deepmd.dpmodel.utils.network.NetworkCollection(ndim: int, ntypes: int, network_type: str = 'network', networks: ~typing.List[~typing.Union[~deepmd.dpmodel.utils.network.make_multilayer_network.<locals>.NN, dict]] = [])[source]
Bases:
objectA collection of networks for multiple elements.
The number of dimesions 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
Methods
Check whether the collection is complete.
deserialize(data)Deserialize the networks from a dict.
Serialize the networks to a dict.
- NETWORK_TYPE_MAP: ClassVar[Dict[str, type]] = {'embedding_network': <class 'deepmd.dpmodel.utils.network.make_embedding_network.<locals>.EN'>, 'fitting_network': <class 'deepmd.dpmodel.utils.network.make_fitting_network.<locals>.FN'>, 'network': <class 'deepmd.dpmodel.utils.network.make_multilayer_network.<locals>.NN'>}
- check_completeness()[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.network.load_dp_model(filename: str) dict[source]
Load a DP model from a file in the native format.
- deepmd.dpmodel.utils.network.make_fitting_network(T_EmbeddingNet, T_Network, T_NetworkLayer)[source]
- deepmd.dpmodel.utils.network.save_dp_model(filename: str, model_dict: dict) None[source]
Save a DP model to a file in the native format.
- deepmd.dpmodel.utils.network.traverse_model_dict(model_obj, callback: callable, is_variable: bool = False)[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.
deepmd.dpmodel.utils.nlist module
- deepmd.dpmodel.utils.nlist.build_multiple_neighbor_list(coord: ndarray, nlist: ndarray, rcuts: List[float], nsels: List[int]) Dict[str, ndarray][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
- coord
np.ndarray exptended coordinates of shape [batch_size, nall x 3]
- nlist
np.ndarray Neighbor list of shape [batch_size, nloc, nsel], the neighbors should be stored in an ascending order.
- rcuts
List[float] list of cut-off radius in ascending order.
- nsels
List[int] maximal number of neighbors in ascending order.
- coord
- Returns
- nlist_dict
Dict[str,np.ndarray] A dict of nlists, key given by get_multiple_nlist_key(rc, nsel) value being the corresponding nlist.
- nlist_dict
- deepmd.dpmodel.utils.nlist.build_neighbor_list(coord1: ndarray, atype: ndarray, nloc: int, rcut: float, sel: Union[int, List[int]], distinguish_types: bool = True) ndarray[source]
Build neightbor list for a single frame. keeps nsel neighbors.
- Parameters
- coord1
np.ndarray exptended coordinates of shape [batch_size, nall x 3]
- atype
np.ndarray extended atomic types of shape [batch_size, nall]
- 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.
- coord1
- Returns
- neighbor_list
np.ndarray 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
- neighbor_list
- deepmd.dpmodel.utils.nlist.extend_coord_with_ghosts(coord: ndarray, atype: ndarray, cell: Optional[ndarray], rcut: float)[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
np.ndarray original coordinates of shape [-1, nloc*3].
- atype
np.ndarray atom type of shape [-1, nloc].
- cell
np.ndarray simulation cell tensor of shape [-1, 9].
- rcut
float the cutoff radius
- coord
- Returns
- extended_coord:
np.ndarray extended coordinates of shape [-1, nall*3].
- extended_atype:
np.ndarray extended atom type of shape [-1, nall].
- index_mapping:
np.ndarray maping extended index to the local index
- extended_coord:
deepmd.dpmodel.utils.region module
- deepmd.dpmodel.utils.region.inter2phys(coord: ndarray, cell: ndarray) ndarray[source]
Convert internal(direct) coordinates to physical coordinates.
- Parameters
- coord
np.ndarray internal coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- coord
- Returns
- phys_coord:
np.ndarray the physical coordinates
- phys_coord:
- deepmd.dpmodel.utils.region.normalize_coord(coord: ndarray, cell: ndarray) ndarray[source]
Apply PBC according to the atomic coordinates.
- Parameters
- coord
np.ndarray orignal coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell shape [*, 3, 3].
- coord
- Returns
- wrapped_coord:
np.ndarray wrapped coordinates of shape [*, na, 3].
- wrapped_coord:
- deepmd.dpmodel.utils.region.phys2inter(coord: ndarray, cell: ndarray) ndarray[source]
Convert physical coordinates to internal(direct) coordinates.
- Parameters
- coord
np.ndarray physical coordinates of shape [*, na, 3].
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- coord
- Returns
- inter_coord:
np.ndarray the internal coordinates
- inter_coord:
- deepmd.dpmodel.utils.region.to_face_distance(cell: ndarray) ndarray[source]
Compute the to-face-distance of the simulation cell.
- Parameters
- cell
np.ndarray simulation cell tensor of shape [*, 3, 3].
- cell
- Returns
- dist:
np.ndarray the to face distances of shape [*, 3]
- dist:
deepmd.dpmodel.utils.update_sel module
- class deepmd.dpmodel.utils.update_sel.UpdateSel[source]
Bases:
BaseUpdateSel- Attributes
- neighbor_stat
Methods
get_min_nbor_dist
get_nbor_stat
get_rcut
get_sel
get_type_map
hook
parse_auto_sel
parse_auto_sel_ratio
update_one_sel
wrap_up_4
- property neighbor_stat: Type[NeighborStat]