def get_pseudos_from_dict(structure, pseudos_uuids):
    """Return mapping of structure kind names onto `UpfData` instances defined per element by `pseudos_uuids`.

    The format of `pseudos_uuids` should have the following format:

        {
            'Al': '045a7a8d-feb1-4aeb-9d32-4e04b13bfc32',
            'C': '08ad7d53-b7cc-45d5-acb8-13530790b751',
        }

    i.e. it shoud associate a chemical element name to a UUID of a UpfData node in the database, and a structure, return
    a dictionary associating each kind name with its UpfData object.

    :param structure: a StructureData
    :param pseudos_uuids: a dictionary of UUIDs of UpfData for each chemical element, as specified above
    :raise MultipleObjectsError: if more than one UPF for the same element is found in the group.
    :raise NotExistent: if no UPF for an element in the group is found in the group.

    .. deprecated:: 4.0.0
        This functionality is now implemented in ``aiida-pseudo``.
    """
    from aiida.common import NotExistent
    from aiida.orm import load_node

    warnings.warn(
        'this function is deprecated and will be removed in `v4.0.0`.',
        AiidaDeprecationWarning)

    pseudo_list = {}
    for kind in structure.kinds:
        symbol = kind.symbol
        try:
            uuid = pseudos_uuids[symbol]
        except KeyError as exception:
            msg = f'No UPF for element {symbol} found in the provided pseudos_uuids dictionary'
            raise NotExistent(msg) from exception
        try:
            upf = load_node(uuid)
        except NotExistent as exception:
            raise NotExistent(
                'No node found associated to the UUID {} given for element {} '
                'in the provided pseudos_uuids dictionary'.format(
                    uuid, symbol)) from exception
        if not isinstance(upf, (LegacyUpfData, UpfData)):
            raise ValueError(f'Node with UUID {uuid} is not a UpfData')
        if upf.element != symbol:
            raise ValueError(
                f'Node<{uuid}> is associated to element {upf.element} and not to {symbol} as expected'
            )

        pseudo_list[kind.name] = upf

    return pseudo_list
예제 #2
0
def get_pseudos_from_dict(structure, pseudos_uuids):
    """
    Given a dictionary in the format::

        {
            'Al': '045a7a8d-feb1-4aeb-9d32-4e04b13bfc32',
            'C': '08ad7d53-b7cc-45d5-acb8-13530790b751',
        }

    (i.e., associating a chemical element name to a UUID of a UpfData node
    in the database), and a AiiDA structure, return a dictionary
    associating each kind name with its UpfData object.

    :param structure: a StructureData
    :param pseudos_uuids: a dictionary of UUIDs of UpfData for each chemical element, as specified above
    :raise MultipleObjectsError: if more than one UPF for the same element is
       found in the group.
    :raise NotExistent: if no UPF for an element in the group is
       found in the group.
    """
    from aiida.common import NotExistent
    from aiida.orm import load_node

    pseudo_list = {}
    for kind in structure.kinds:
        symbol = kind.symbol
        try:
            uuid = pseudos_uuids[symbol]
        except KeyError:
            raise NotExistent(
                'No UPF for element {} found in the provided pseudos_uuids dictionary'
                .format(symbol))
        try:
            upf = load_node(uuid)
        except NotExistent:
            raise NotExistent(
                'No node found associated to the UUID {} given for element {} '
                'in the provided pseudos_uuids dictionary'.format(
                    uuid, symbol))
        if not isinstance(upf, UpfData):
            raise ValueError('Node with UUID {} is not a UpfData'.format(uuid))
        if upf.element != symbol:
            raise ValueError(
                'Node<{}> is associated to element {} and not to {} as expected'
                .format(uuid, upf.element, symbol))

        pseudo_list[kind.name] = upf

    return pseudo_list
예제 #3
0
    def validate_table_existence(self):
        """Verify that the `DbSetting` table actually exists.

        :raises: `~aiida.common.exceptions.NotExistent` if the settings table does not exist
        """
        from django.db import connection
        if self.table_name not in connection.introspection.table_names():
            raise NotExistent('the settings table does not exist')
예제 #4
0
    def validate_table_existence(self):
        """Verify that the `DbSetting` table actually exists.

        :raises: `~aiida.common.exceptions.NotExistent` if the settings table does not exist
        """
        from sqlalchemy.engine import reflection
        inspector = reflection.Inspector.from_engine(get_scoped_session().bind)
        if self.table_name not in inspector.get_table_names():
            raise NotExistent('the settings table does not exist')
예제 #5
0
    def delete(self, key):
        """Delete the setting with the given key.

        :param key: the key identifying the setting
        :raises: `~aiida.common.exceptions.NotExistent` if the settings does not exist
        """
        from aiida.backends.djsite.db.models import DbSetting

        self.validate_table_existence()

        try:
            DbSetting.del_value(key=key)
        except KeyError:
            raise NotExistent(f'setting `{key}` does not exist') from KeyError
예제 #6
0
    def delete(self, key):
        """Delete the setting with the given key.

        :param key: the key identifying the setting
        :raises: `~aiida.common.exceptions.NotExistent` if the settings does not exist
        """
        from aiida.backends.sqlalchemy.models.settings import DbSetting
        self.validate_table_existence()

        try:
            setting = get_scoped_session().query(DbSetting).filter_by(
                key=key).one()
            setting.delete()
        except NoResultFound:
            raise NotExistent('setting `{}` does not exist'.format(key))
예제 #7
0
    def get(self, key):
        """Return the setting with the given key.

        :param key: the key identifying the setting
        :return: Setting
        :raises: `~aiida.common.exceptions.NotExistent` if the settings does not exist
        """
        from aiida.backends.djsite.db.models import DbSetting

        self.validate_table_existence()
        setting = DbSetting.objects.filter(key=key).first()

        if setting is None:
            raise NotExistent(f'setting `{key}` does not exist')

        return Setting(setting.key, setting.val, setting.description, setting.time)
예제 #8
0
    def get(self, key):
        """Return the setting with the given key.

        :param key: the key identifying the setting
        :return: Setting
        :raises: `~aiida.common.exceptions.NotExistent` if the settings does not exist
        """
        from aiida.backends.sqlalchemy.models.settings import DbSetting
        self.validate_table_existence()

        try:
            setting = get_scoped_session().query(DbSetting).filter_by(
                key=key).one()
        except NoResultFound:
            raise NotExistent(
                f'setting `{key}` does not exist') from NoResultFound

        return Setting(key, setting.getvalue(), setting.description,
                       setting.time)
예제 #9
0
def get_pseudos_from_structure(structure, family_name):
    """
    Given a family name (of UpfData or OTFGData) and a AiiDA
    structure, return a dictionary associating each kind name with its
    pseduopotential object.

    :raise MultipleObjectsError: if more than one UPF for the same element is
       found in the group.
    :raise NotExistent: if no UPF for an element in the group is
       found in the group.

    :returns: A dictionary maps kind to the psueodpotential node
    """

    # Try to get the pseudopotentials that are managed by aiida-pseudo
    result = _get_pseudos_from_aiida_pseudo(structure, family_name)
    if result:
        return result

    family_pseudos = {}

    try:
        family_upf = [UpfData.get_upf_group(family_name)]
    except NotExistent:
        family_upf = []
    try:
        family_otfg = [OTFGData.get_otfg_group(family_name)]
    except NotExistent:
        family_otfg = []

    all_families = family_otfg + family_upf
    if len(all_families) == 0:
        raise NotExistent(
            "Cannot find matching group among UspData, UpfData and OTFGData")

    if len(all_families) > 1:
        raise MultipleObjectsError(
            f"Multiple groups with label {family_name} detected")

    family = all_families[0]

    # Checking uniqueness for each element
    for node in family.nodes:
        if isinstance(node, (UpfData, UspData, OTFGData)):
            if node.element in family_pseudos:
                raise MultipleObjectsError(
                    "More than one pseudo for element {} found in "
                    "family {}".format(node.element, family_name))
            family_pseudos[node.element] = node

    pseudo_list = {}
    for kind in structure.kinds:
        symbol = kind.symbol
        if symbol in family_pseudos:
            pseudo_list[kind.name] = family_pseudos[symbol]
        elif 'LIBRARY' in family_pseudos:
            pseudo_list[kind.name] = family_pseudos['LIBRARY']

        else:
            raise NotExistent(
                "No pseudo for element {} found in family {}".format(
                    symbol, family_pseudos))

    return pseudo_list