deepmd.dpmodel.output_def
=========================

.. py:module:: deepmd.dpmodel.output_def


Classes
-------

.. autoapisummary::

   deepmd.dpmodel.output_def.OutputVariableOperation
   deepmd.dpmodel.output_def.OutputVariableCategory
   deepmd.dpmodel.output_def.OutputVariableDef
   deepmd.dpmodel.output_def.FittingOutputDef
   deepmd.dpmodel.output_def.ModelOutputDef


Functions
---------

.. autoapisummary::

   deepmd.dpmodel.output_def.check_shape
   deepmd.dpmodel.output_def.check_var
   deepmd.dpmodel.output_def.model_check_output
   deepmd.dpmodel.output_def.fitting_check_output
   deepmd.dpmodel.output_def.get_reduce_name
   deepmd.dpmodel.output_def.get_deriv_name
   deepmd.dpmodel.output_def.get_deriv_name_mag
   deepmd.dpmodel.output_def.get_hessian_name
   deepmd.dpmodel.output_def.apply_operation
   deepmd.dpmodel.output_def.check_operation_applied
   deepmd.dpmodel.output_def.check_deriv
   deepmd.dpmodel.output_def.do_reduce
   deepmd.dpmodel.output_def.do_mask
   deepmd.dpmodel.output_def.do_derivative


Module Contents
---------------

.. py:function:: check_shape(shape: list[int], def_shape: list[int]) -> None

   
   Check if the shape satisfies the defined shape.
















   ..
       !! processed by numpydoc !!

.. py:function:: check_var(var: Any, var_def: Any) -> None

.. py:function:: model_check_output(cls: type) -> type

   
   Check if the output of the Model is consistent with the definition.

   Two methods are assumed to be provided by the Model:
   1. Model.output_def that gives the output definition.
   2. Model.__call__ that defines the forward path of the model.















   ..
       !! processed by numpydoc !!

.. py:function:: fitting_check_output(cls: type) -> type

   
   Check if the output of the Fitting is consistent with the definition.

   Two methods are assumed to be provided by the Fitting:
   1. Fitting.output_def that gives the output definition.
   2. Fitting.__call__ defines the forward path of the fitting.















   ..
       !! processed by numpydoc !!

.. py:class:: OutputVariableOperation

   Bases: :py:obj:`enum.IntEnum`


   
   Defines the operation of the output variable.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: _NONE
      :value: 0


      
      No operation.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: REDU
      :value: 1


      
      Reduce the output variable.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_R
      :value: 2


      
      Derivative w.r.t. coordinates.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_C
      :value: 4


      
      Derivative w.r.t. cell.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: _SEC_DERV_R
      :value: 8


      
      Second derivative w.r.t. coordinates.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: MAG
      :value: 16


      
      Magnetic output.
















      ..
          !! processed by numpydoc !!


.. py:class:: OutputVariableCategory

   Bases: :py:obj:`enum.IntEnum`


   
   Defines the category of the output variable.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: OUT

      
      Output variable. (e.g. atom energy)
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: REDU

      
      Reduced output variable. (e.g. system energy)
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_R

      
      Negative derivative w.r.t. coordinates. (e.g. force)
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_C

      
      Atomic component of the virial, see PRB 104, 224202 (2021)
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_C_REDU

      
      Virial, the transposed negative gradient with cell tensor times cell tensor, see eq 40 JCP 159, 054801 (2023).
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_R_DERV_R

      
      Hession matrix, the second derivative w.r.t. coordinates.
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_R_MAG

      
      Magnetic part of negative derivative w.r.t. coordinates. (e.g. magnetic force)
















      ..
          !! processed by numpydoc !!


   .. py:attribute:: DERV_C_MAG

      
      Magnetic part of atomic component of the virial.
















      ..
          !! processed by numpydoc !!


.. py:class:: OutputVariableDef(name: str, shape: list[int], reducible: bool = False, r_differentiable: bool = False, c_differentiable: bool = False, atomic: bool = True, category: int = OutputVariableCategory.OUT.value, r_hessian: bool = False, magnetic: bool = False, intensive: bool = False)

   
   Defines the shape and other properties of the one output variable.

   It is assume that the fitting network output variables for each
   local atom. This class defines one output variable, including its
   name, shape, reducibility and differentiability.

   :Parameters:

       **name**
           Name of the output variable. Notice that the xxxx_redu,
           xxxx_derv_c, xxxx_derv_r are reserved names that should
           not be used to define variables.

       **shape**
           The shape of the variable. e.g. energy should be [1],
           dipole should be [3], polarizabilty should be [3,3].

       **reducible**
           If the variable is reduced.

       **r_differentiable**
           If the variable is differentiated with respect to coordinates
           of atoms. Only reducible variable are differentiable.
           Negative derivative w.r.t. coordinates will be calculated. (e.g. force)

       **c_differentiable**
           If the variable is differentiated with respect to the
           cell tensor (pbc case). Only reducible variable
           are differentiable.
           Virial, the transposed negative gradient with cell tensor times
           cell tensor, will be calculated, see eq 40 JCP 159, 054801 (2023).

       **atomic** : :ref:`bool <python:bltin-boolean-values>`
           If the variable is defined for each atom.

       **category** : :class:`python:int`
           The category of the output variable.

       **r_hessian** : :ref:`bool <python:bltin-boolean-values>`
           If hessian is required

       **magnetic** : :ref:`bool <python:bltin-boolean-values>`
           If the derivatives of variable have magnetic parts.

       **intensive** : :ref:`bool <python:bltin-boolean-values>`
           It indicates whether the fitting property is intensive or extensive.














   ..
       !! processed by numpydoc !!

   .. py:attribute:: name


   .. py:attribute:: shape


   .. py:attribute:: output_size
      :value: 1



   .. py:attribute:: atomic
      :value: True



   .. py:attribute:: reducible
      :value: False



   .. py:attribute:: r_differentiable
      :value: False



   .. py:attribute:: c_differentiable
      :value: False



   .. py:attribute:: intensive
      :value: False



   .. py:attribute:: category


   .. py:attribute:: r_hessian
      :value: False



   .. py:attribute:: magnetic
      :value: False



   .. py:property:: size
      :type: int



   .. py:method:: squeeze(dim: int) -> None


.. py:class:: FittingOutputDef(var_defs: list[OutputVariableDef])

   
   Defines the shapes and other properties of the fitting network outputs.

   It is assume that the fitting network output variables for each
   local atom. This class defines all the outputs.

   :Parameters:

       **var_defs**
           List of output variable definitions.














   ..
       !! processed by numpydoc !!

   .. py:attribute:: var_defs


   .. py:method:: __getitem__(key: str) -> OutputVariableDef


   .. py:method:: get_data() -> dict[str, OutputVariableDef]


   .. py:method:: keys()


.. py:class:: ModelOutputDef(fit_defs: FittingOutputDef)

   
   Defines the shapes and other properties of the model outputs.

   The model reduce and differentiate fitting outputs if applicable.
   If a variable is named by foo, then the reduced variable is called
   foo_redu, the derivative w.r.t. coordinates is called foo_derv_r
   and the derivative w.r.t. cell is called foo_derv_c.

   :Parameters:

       **fit_defs**
           Definition for the fitting net output














   ..
       !! processed by numpydoc !!

   .. py:attribute:: def_outp


   .. py:attribute:: def_redu


   .. py:attribute:: def_derv_c_redu


   .. py:attribute:: def_mask


   .. py:attribute:: var_defs
      :type:  dict[str, OutputVariableDef]


   .. py:method:: __getitem__(key: str) -> OutputVariableDef


   .. py:method:: get_data() -> dict[str, OutputVariableDef]


   .. py:method:: keys()


   .. py:method:: keys_outp()


   .. py:method:: keys_redu()


   .. py:method:: keys_derv_r()


   .. py:method:: keys_hess_r()


   .. py:method:: keys_derv_c()


   .. py:method:: keys_derv_c_redu()


.. py:function:: get_reduce_name(name: str) -> str

.. py:function:: get_deriv_name(name: str) -> tuple[str, str]

.. py:function:: get_deriv_name_mag(name: str) -> tuple[str, str]

.. py:function:: get_hessian_name(name: str) -> str

.. py:function:: apply_operation(var_def: OutputVariableDef, op: OutputVariableOperation) -> int

   
   Apply an operation to the category of a variable definition.


   :Parameters:

       **var_def** : :obj:`OutputVariableDef`
           The variable definition.

       **op** : :obj:`OutputVariableOperation`
           The operation to be applied.



   :Returns:

       :class:`python:int`
           The new category of the variable definition.




   :Raises:

       :obj:`ValueError`
           If the operation has been applied to the variable definition,
           and exceed the maximum limitation.







   ..
       !! processed by numpydoc !!

.. py:function:: check_operation_applied(var_def: OutputVariableDef, op: OutputVariableOperation) -> bool

   
   Check if a operation has been applied to a variable definition.


   :Parameters:

       **var_def** : :obj:`OutputVariableDef`
           The variable definition.

       **op** : :obj:`OutputVariableOperation`
           The operation to be checked.



   :Returns:

       :ref:`bool <python:bltin-boolean-values>`
           True if the operation has been applied, False otherwise.











   ..
       !! processed by numpydoc !!

.. py:function:: check_deriv(var_def: OutputVariableDef) -> bool

   
   Check if a variable is obtained by derivative.
















   ..
       !! processed by numpydoc !!

.. py:function:: do_reduce(def_outp_data: dict[str, OutputVariableDef]) -> dict[str, OutputVariableDef]

.. py:function:: do_mask(def_outp_data: dict[str, OutputVariableDef]) -> dict[str, OutputVariableDef]

.. py:function:: do_derivative(def_outp_data: dict[str, OutputVariableDef]) -> tuple[dict[str, OutputVariableDef], dict[str, OutputVariableDef]]

