deepmd.dpmodel.model.spin_model#
Classes#
A spin model wrapper, with spin input preprocess and output split. |
Module Contents#
- class deepmd.dpmodel.model.spin_model.SpinModel(backbone_model: deepmd.dpmodel.atomic_model.dp_atomic_model.DPAtomicModel, spin: deepmd.utils.spin.Spin)[source]#
Bases:
deepmd.dpmodel.common.NativeOPA spin model wrapper, with spin input preprocess and output split.
This model extends a backbone DP model to handle magnetic spin degrees of freedom. Virtual atoms are created at positions offset from real atoms by their spin vectors:
\[\mathbf{r}_i^{\mathrm{virtual}} = \mathbf{r}_i^{\mathrm{real}} + s_i \cdot \boldsymbol{\sigma}_i,\]where \(s_i\) is a scaling factor and \(\boldsymbol{\sigma}_i\) is the spin vector.
The model then computes interactions between real atoms, virtual atoms, and between real and virtual atoms, enabling the prediction of spin-dependent properties.
The output forces on virtual atoms are converted to magnetic torques:
\[\boldsymbol{\tau}_i = \mathbf{F}_i^{\mathrm{virtual}} \times \boldsymbol{\sigma}_i.\]- _to_xp(arr: Any, xp: Any, ref_arr: Any) Any[source]#
Convert a numpy array to the same namespace as ref_arr.
- process_spin_input(coord: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, spin: deepmd.dpmodel.array_api.Array) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array][source]#
Generate virtual coordinates and types, concat into the input.
- Returns:
- coord_spin
Array Concatenated coordinates with shape (nframes, 2*nloc, 3).
- atype_spin
Array Concatenated atom types with shape (nframes, 2*nloc).
- coord_corr
Array Coordinate correction for virial with shape (nframes, 2*nloc, 3).
- coord_spin
- process_spin_input_lower(extended_coord: deepmd.dpmodel.array_api.Array, extended_atype: deepmd.dpmodel.array_api.Array, extended_spin: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array, mapping: deepmd.dpmodel.array_api.Array | None = None) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array | None, deepmd.dpmodel.array_api.Array][source]#
Add extended_spin into extended_coord to generate virtual atoms, and extend nlist and mapping.
- Returns:
- extended_coord_updated
Array Updated coordinates with virtual atoms, shape (nframes, 2*nall, 3).
- extended_atype_updated
Array Updated atom types with virtual atoms, shape (nframes, 2*nall).
- nlist_updated
Array Updated neighbor list including virtual atoms.
- mapping_updated
ArrayorNone Updated mapping indices, or None if input mapping is None.
- extended_coord_corr
Array Coordinate correction for virial with shape (nframes, 2*nall, 3).
- extended_coord_updated
Notes
The final extended_coord_updated with shape [nframes, nall + nall, 3] has the following order: - [:, :nloc]: original nloc real atoms. - [:, nloc: nloc + nloc]: virtual atoms corresponding to nloc real atoms. - [:, nloc + nloc: nloc + nall]: ghost real atoms. - [:, nloc + nall: nall + nall]: virtual atoms corresponding to ghost real atoms.
- process_spin_output(atype: deepmd.dpmodel.array_api.Array, out_tensor: deepmd.dpmodel.array_api.Array, add_mag: bool = True, virtual_scale: bool = True) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array][source]#
Split the output both real and virtual atoms, and scale the latter.
- process_spin_output_lower(extended_atype: deepmd.dpmodel.array_api.Array, extended_out_tensor: deepmd.dpmodel.array_api.Array, nloc: int, add_mag: bool = True, virtual_scale: bool = True) tuple[deepmd.dpmodel.array_api.Array, deepmd.dpmodel.array_api.Array][source]#
Split the extended output of both real and virtual atoms with switch, and scale the latter.
- static extend_nlist(extended_atype: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array) deepmd.dpmodel.array_api.Array[source]#
- static concat_switch_virtual(extended_tensor: deepmd.dpmodel.array_api.Array, extended_tensor_virtual: deepmd.dpmodel.array_api.Array, nloc: int) deepmd.dpmodel.array_api.Array[source]#
- static expand_aparam(aparam: deepmd.dpmodel.array_api.Array, nloc: int) deepmd.dpmodel.array_api.Array[source]#
Expand the atom parameters for virtual atoms if necessary.
- compute_or_load_stat(sampled_func: collections.abc.Callable[[], list[dict[str, Any]]], stat_file_path: Any | None = None, preset_observed_type: list[str] | None = None) None[source]#
Compute or load the statistics parameters of the model.
- Parameters:
- sampled_func
The lazy sampled function to get data frames from different data systems.
- stat_file_path
The dictionary of paths to the statistics files.
- preset_observed_type
The preset observed types.
- get_sel_type() list[int][source]#
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.
- is_aparam_nall() bool[source]#
Check whether the shape of atomic parameters is (nframes, nall, ndim). If False, the shape is (nframes, nloc, ndim).
- get_nnei() int[source]#
Returns the total number of selected neighboring atoms in the cut-off radius.
- get_nsel() int[source]#
Returns the total number of selected neighboring atoms in the cut-off radius.
- model_output_def() deepmd.dpmodel.output_def.ModelOutputDef[source]#
Get the output def for the model.
- _get_spin_sampled_func(sampled_func: collections.abc.Callable[[], list[dict]]) collections.abc.Callable[[], list[dict]][source]#
Get a spin-aware sampled function that transforms spin data for the backbone model.
- Parameters:
- sampled_func
A callable that returns a list of data dicts containing ‘coord’, ‘atype’, ‘spin’, etc.
- Returns:
CallableA cached callable that returns spin-preprocessed data dicts.
- change_out_bias(merged: collections.abc.Callable[[], list[dict]] | list[dict], bias_adjust_mode: str = 'change-by-statistic') None[source]#
Change the output bias of atomic model according to the input data and the pretrained model.
- Parameters:
- 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. Since the sampling process can be slow and memory-intensive, the lazy function helps by only sampling once.
- bias_adjust_mode
str The mode for changing output bias : [‘change-by-statistic’, ‘set-by-statistic’] ‘change-by-statistic’ : perform predictions on labels of target dataset,
and do least square on the errors to obtain the target shift as bias.
‘set-by-statistic’ : directly use the statistic output bias in the target dataset.
- merged
- change_type_map(type_map: list[str], model_with_new_type_stat: Any = None) None[source]#
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.
- get_dp_atomic_model() DPAtomicModel | None[source]#
Get the underlying DPAtomicModel by delegating to the backbone model.
- call_common(coord: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, spin: deepmd.dpmodel.array_api.Array, box: deepmd.dpmodel.array_api.Array | None = None, fparam: deepmd.dpmodel.array_api.Array | None = None, aparam: deepmd.dpmodel.array_api.Array | None = None, do_atomic_virial: bool = False, charge_spin: deepmd.dpmodel.array_api.Array | None = None) dict[str, deepmd.dpmodel.array_api.Array][source]#
Return model prediction with raw internal keys.
- Parameters:
- coord
The coordinates of the atoms. shape: nf x (nloc x 3)
- atype
The type of atoms. shape: nf x nloc
- spin
The spins of the atoms. shape: nf x (nloc x 3)
- box
The simulation box. shape: nf x 9
- fparam
frame parameter. nf x ndf
- aparam
atomic parameter. nf x nloc x nda
- do_atomic_virial
If calculate the atomic virial.
- Returns:
ret_dictThe result dict of type dict[str,np.ndarray]. The keys are defined by the ModelOutputDef.
- call(coord: deepmd.dpmodel.array_api.Array, atype: deepmd.dpmodel.array_api.Array, spin: deepmd.dpmodel.array_api.Array, box: deepmd.dpmodel.array_api.Array | None = None, fparam: deepmd.dpmodel.array_api.Array | None = None, aparam: deepmd.dpmodel.array_api.Array | None = None, do_atomic_virial: bool = False, charge_spin: deepmd.dpmodel.array_api.Array | None = None) dict[str, deepmd.dpmodel.array_api.Array][source]#
Return model prediction with translated user-facing keys.
- Parameters:
- coord
The coordinates of the atoms. shape: nf x (nloc x 3)
- atype
The type of atoms. shape: nf x nloc
- spin
The spins of the atoms. shape: nf x (nloc x 3)
- box
The simulation box. shape: nf x 9
- fparam
frame parameter. nf x ndf
- aparam
atomic parameter. nf x nloc x nda
- do_atomic_virial
If calculate the atomic virial.
- Returns:
ret_dictThe result dict with translated keys, e.g.
atom_energy,energy,force,force_mag.
- call_common_lower(extended_coord: deepmd.dpmodel.array_api.Array, extended_atype: deepmd.dpmodel.array_api.Array, extended_spin: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array, mapping: deepmd.dpmodel.array_api.Array | None = None, fparam: deepmd.dpmodel.array_api.Array | None = None, aparam: deepmd.dpmodel.array_api.Array | None = None, do_atomic_virial: bool = False, comm_dict: dict | None = None, charge_spin: deepmd.dpmodel.array_api.Array | None = None) dict[str, deepmd.dpmodel.array_api.Array][source]#
Return model prediction with raw internal keys. Lower interface that takes extended atomic coordinates, types and spins, nlist, and mapping as input, and returns the predictions on the extended region. The predictions are not reduced.
- Parameters:
- extended_coord
coordinates in extended region. nf x (nall x 3).
- extended_atype
atomic type in extended region. nf x nall.
- extended_spin
spins in extended region. nf x (nall x 3).
- nlist
neighbor list. nf x nloc x nsel.
- mapping
maps the extended indices to local indices. nf x nall.
- fparam
frame parameter. nf x ndf
- aparam
atomic parameter. nf x nloc x nda
- do_atomic_virial
whether calculate atomic virial
- Returns:
result_dictthe result dict, defined by the FittingOutputDef.
- call_lower(extended_coord: deepmd.dpmodel.array_api.Array, extended_atype: deepmd.dpmodel.array_api.Array, extended_spin: deepmd.dpmodel.array_api.Array, nlist: deepmd.dpmodel.array_api.Array, mapping: deepmd.dpmodel.array_api.Array | None = None, fparam: deepmd.dpmodel.array_api.Array | None = None, aparam: deepmd.dpmodel.array_api.Array | None = None, do_atomic_virial: bool = False, charge_spin: deepmd.dpmodel.array_api.Array | None = None) dict[str, deepmd.dpmodel.array_api.Array][source]#
Return model prediction with translated user-facing keys. Lower interface.
- Parameters:
- extended_coord
coordinates in extended region. nf x (nall x 3).
- extended_atype
atomic type in extended region. nf x nall.
- extended_spin
spins in extended region. nf x (nall x 3).
- nlist
neighbor list. nf x nloc x nsel.
- mapping
maps the extended indices to local indices. nf x nall.
- fparam
frame parameter. nf x ndf
- aparam
atomic parameter. nf x nloc x nda
- do_atomic_virial
whether calculate atomic virial
- Returns:
result_dictThe result dict with translated keys, e.g.
atom_energy,energy,extended_force,extended_force_mag.