Source code for structuretoolkit.build.materialsproject

from collections.abc import Generator, Iterable
from typing import Any

from ase.atoms import Atoms

from structuretoolkit.common.pymatgen import pymatgen_to_ase






[docs] def by_id( material_id: str | int, final: bool = True, conventional_unit_cell: bool = False, api_key=None, ) -> Atoms | list[Atoms]: """ Retrieve a structure by material id. This is how you would ask for the iron ground state: >>> structuretoolkit.build.materialsproject.by_id('mp-13') Fe: [0. 0. 0.] tags: spin: [(0: 2.214)] pbc: [ True True True] cell: Cell([[2.318956, 0.000185, -0.819712], [-1.159251, 2.008215, -0.819524], [2.5e-05, 0.000273, 2.459206]]) Usage is only possible with an API key obtained from the Materials Project. To do this, create an account with them, login and access `this webpage <https://next-gen.materialsproject.org/api#api-key>`. Once you have a key, either pass it as the `api_key` parameter or export an environment variable, called `MP_API_KEY`, in your shell setup. Args: material_id (str): the id assigned to a structure by the materials project api_key (str, optional): if your API key is not exported in the environment flag MP_API_KEY, pass it here final (bool, optional): if set to False, returns the list of initial structures, else returns the final structure. (Default is True) conventional_unit_cell (bool, optional): if set to True, returns the standard conventional unit cell. (Default is False) Returns: :class:`~.Atoms`: requested final structure if final is True list of :class:~.Atoms`: a list of initial (pre-relaxation) structures if final is False Raises: ValueError: material id does not exist """ from mp_api.client import MPRester rest_kwargs = { "include_user_agent": True, # send some additional software version info to MP } if api_key is not None: rest_kwargs["api_key"] = api_key with MPRester(**rest_kwargs) as mpr: if final: return pymatgen_to_ase( mpr.get_structure_by_material_id( material_id=material_id, final=final, conventional_unit_cell=conventional_unit_cell, ) ) else: return [ pymatgen_to_ase(mpr_structure) for mpr_structure in ( mpr.get_structure_by_material_id( material_id=material_id, final=final, conventional_unit_cell=conventional_unit_cell, ) ) ]