:py:mod:`aiida_hubbard.workflows.hubbard`
=========================================

.. py:module:: aiida_hubbard.workflows.hubbard

.. autoapi-nested-parse::

   Turn-key solution to automatically compute the self-consistent Hubbard parameters for a given structure.



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

Classes
~~~~~~~

.. autoapisummary::

   aiida_hubbard.workflows.hubbard.SelfConsistentHubbardWorkChain



Functions
~~~~~~~~~

.. autoapisummary::

   aiida_hubbard.workflows.hubbard.get_separated_parameters
   aiida_hubbard.workflows.hubbard.validate_positive
   aiida_hubbard.workflows.hubbard.validate_inputs



Attributes
~~~~~~~~~~

.. autoapisummary::

   aiida_hubbard.workflows.hubbard.HubbardStructureData
   aiida_hubbard.workflows.hubbard.PwCalculation
   aiida_hubbard.workflows.hubbard.PwBaseWorkChain
   aiida_hubbard.workflows.hubbard.PwRelaxWorkChain
   aiida_hubbard.workflows.hubbard.HpWorkChain


.. py:data:: HubbardStructureData

   

.. py:data:: PwCalculation

   

.. py:data:: PwBaseWorkChain

   

.. py:data:: PwRelaxWorkChain

   

.. py:data:: HpWorkChain

   

.. py:function:: get_separated_parameters(hubbard_parameters: list[tuple[int, str, int, str, float, tuple[int, int, int], str]]) -> tuple[list, list]

   Return a tuple with onsites and intersites parameters separated.

   :return: tuple (list of onsites, list of intersites).


.. py:function:: validate_positive(value, _)

   Validate that the value is positive.


.. py:function:: validate_inputs(inputs, _)

   Validate the entire inputs.


.. py:class:: SelfConsistentHubbardWorkChain(inputs: dict | None = None, logger: logging.Logger | None = None, runner: aiida.engine.runners.Runner | None = None, enable_persistence: bool = True)


   Bases: :py:obj:`aiida.engine.WorkChain`, :py:obj:`aiida_quantumespresso.workflows.protocols.utils.ProtocolMixin`

   Workchain computing the self-consistent Hubbard parameters of a structure.

   It iteratively relaxes the structure (optional) with the ``PwRelaxWorkChain``
   and computes the Hubbard parameters through the ``HpWorkChain``,
   using the remote folder of an scf performed via the ``PwBaseWorkChain``,
   until the Hubbard values are converged within certain tolerance(s).

   The procedure in each step of the convergence cycle is slightly different depending on the electronic and
   magnetic properties of the system. Each cycle will roughly consist of three steps:

       * Relaxing the structure at the current Hubbard values (optional).
       * One or two DFT calculations depending whether the system is metallic or insulating, respectively.
       * A DFPT calculation of the Hubbard parameters, perturbing the ground-state of the last DFT run.

   The possible options for the set of DFT SCF calculations that have to be run in the second step look are:

       * Metals:
           - SCF with smearing.
       * Insulators
           - SCF with smearing.
           - SCF with fixed occupations; if magnetic, total magnetization and number of bands
               are fixed to the values found from the previous SCF calculation.

   When convergence is achieved a node will be returned containing the final converged
   :class:`~aiida_quantumespresso.data.hubbard_structure.HubbardStructureData`.

   .. py:attribute:: defaults

      

   .. py:method:: define(spec)
      :classmethod:

      Define the specifications of the process.


   .. py:method:: get_protocol_filepath()
      :classmethod:

      Return ``pathlib.Path`` to the ``.yaml`` file that defines the protocols.


   .. py:method:: get_builder_from_protocol(pw_code, hp_code, hubbard_structure, protocol=None, overrides=None, options_pw=None, options_hp=None, **kwargs)
      :classmethod:

      Return a builder prepopulated with inputs selected according to the chosen protocol.

      :param pw_code: the ``Code`` instance configured for the ``quantumespresso.pw`` plugin.
      :param hp_code: the ``Code`` instance configured for the ``quantumespresso.hp`` plugin.
      :param hubbard_structure: the ``HubbardStructureData`` instance containing the initialised Hubbard paramters.
      :param protocol: protocol to use, if not specified, the default will be used.
      :param overrides: optional dictionary of inputs to override the defaults of the protocol.
      :param options_pw: A dictionary of options that will be recursively set for the ``metadata.options``
          input of all the pw ``CalcJobs`` that are nested in this work chain.
      :param options_hp: A dictionary of options that will be recursively set for the ``metadata.options``
          input of all the hp ``CalcJobs`` that are nested in this work chain.
      :return: a process builder instance with all inputs defined ready for launch.


   .. py:method:: setup()

      Set up Context variables.


   .. py:method:: should_run_relax()

      Return whether a relax calculation needs to be run, which is true if `relax` is specified in inputs.


   .. py:method:: should_check_convergence()

      Return whether to check the convergence of Hubbard parameters.


   .. py:method:: should_run_iteration()

      Return whether a new process should be run.


   .. py:method:: update_iteration()

      Update the current iteration index counter.


   .. py:method:: is_insulator()

      Return whether the current structure is a metal.


   .. py:method:: is_magnetic()

      Return whether the current structure is magnetic.


   .. py:method:: get_inputs(cls, namespace)

      Return the inputs for one of the subprocesses whose inputs are exposed in the given namespace.

      :param cls: the process class of the subprocess
      :param namespace: namespace into which the inputs are exposed.
      :return: dictionary with inputs.


   .. py:method:: set_pw_parameters(inputs)

      Set the input parameters for a generic `quantumespresso.pw` calculation.

      :param inputs: AttributeDict of a ``PwBaseWorkChain`` builder input.


   .. py:method:: get_pseudos() -> dict

      Return the mapping of pseudos based on the current structure.

      .. note:: this is necessary because during the workchain the kind names of the structure can change, meaning the
          mapping of the pseudos that is to be passed to the subprocesses also may have to change, since the keys are
          based on the kind names of the structure.

      :return: dictionary of pseudos where the keys are the kindnames of ``self.ctx.current_hubbard_structure``.


   .. py:method:: relabel_hubbard_structure(workchain) -> None

      Relabel the Hubbard structure if new types have been detected.


   .. py:method:: run_relax()

      Run the PwRelaxWorkChain to run a relax PwCalculation.


   .. py:method:: inspect_relax()

      Verify that the PwRelaxWorkChain finished successfully.


   .. py:method:: run_scf_smearing()

      Run an scf `PwBaseWorkChain` with smeared occupations.

      This step is always needed since we do not a priori whether
      the material will be metallic or insulating.


   .. py:method:: run_scf_fixed()

      Run an scf `PwBaseWorkChain` with fixed occupations on top of the previous calculation.

      The nunmber of bands and total magnetization (if magnetic) are set according to those of the
      previous calculation that was run with smeared occupations.

      .. note: this will be run only if the material has been recognised as insulating.


   .. py:method:: inspect_scf()

      Verify that the scf PwBaseWorkChain finished successfully.


   .. py:method:: recon_scf()

      Verify that the scf PwBaseWorkChain finished successfully.


   .. py:method:: run_hp()

      Run the HpWorkChain restarting from the last completed scf calculation.


   .. py:method:: inspect_hp()

      Analyze the last completed HpWorkChain.

      We check the current Hubbard parameters and compare those with the values computed
      in the previous iteration. If the difference for all Hubbard sites is smaller than
      the tolerance(s), the calculation is considered to be converged.


   .. py:method:: check_convergence()

      Check the convergence of the Hubbard parameters.


   .. py:method:: run_results()

      Attach the final converged Hubbard U parameters and the corresponding structure.


   .. py:method:: should_clean_workdir()

      Whether to clean the work directories at each iteration.


   .. py:method:: clean_iteration()

      Clean all work directiories of the current iteration.



