crystod.search#
Public Materials Project API of CrystOD (the crystod-search domain).
Search the Materials Project the way its website does and download the
structures, the Python form of crystod-search. A query is a formula
(SrTiO3), an anonymous formula (ABO3), a chemical system
(Sr-Ti-O), a list of elements (Sr,Ti,O) or material IDs
(mp-5229); the answer lists formula, space group, ID, band gap, energy
above the hull and number of sites, with the experimentally observed
materials flagged. An API key of the Materials Project is needed (free,
from https://next-gen.materialsproject.org/api), read from the
MP_API_KEY environment variable or from PMG_MAPI_KEY in the pymatgen
settings file, or passed as api_key=.
Searching
search_materials()– a query plus filters (experimentally observed, on the hull, energy above hull, band gap, number of sites, space group, excluded elements), as aSearchResultofMaterialrecords.format_table()– the tablecrystod-searchprints.interpret_query(),resolve_space_group(),normalize_material_id()– how queries, space groups and IDs are read (mp-aaaaahtdismp-5229).
Downloading structures
fetch_materials()– the structures of given IDs as pymatgenStructureobjects, in the standardized conventional cell (default) or the standardized primitive cell.standardize_cell()– the cell conversion on its own.write_poscar(),poscar_text(),poscar_filename()– the POSCAR filescrystod-search --getwrites.
Tables
SORT_KEYS,CELL_CHOICES– the names--sortand--cellaccept;POSCAR_PREFIXES– the file-name prefix of each cell (POSCAR,PPOSCAR).
Usage:
from crystod import search
result = search.search_materials("Sr-Ti-O", experimental=True)
for material in result.materials:
print(material.label, material.formula, material.space_group)
(sr2tio4,) = search.fetch_materials("mp-5532")
search.write_poscar(sr2tio4) # POSCAR_Sr2TiO4_I4mmm_mp-5532 (conventional)
(sr2tio4,) = search.fetch_materials("mp-5532", cell="primitive")
search.write_poscar(sr2tio4) # PPOSCAR_Sr2TiO4_I4mmm_mp-5532
Attributes resolve lazily (PEP 562): importing this module is instant and
requests and pymatgen load only on first use. Functions report bad input
as ValueError through this namespace (the implementation module raises
SystemExit, as the command line wants); a missing API key, a network
failure or an error of the server raise MaterialsProjectError.
- exception crystod.search.MaterialsProjectError[source]#
Bases:
RuntimeErrorThe Materials Project could not answer: no or bad API key, no network, or an error reported by the server.
- class crystod.search.Material(material_id, formula, space_group, space_group_number, crystal_system, band_gap, energy_above_hull, nsites, experimental, structure=None, cell=None, deprecated=False, cell_space_group_number=None)[source]#
Bases:
objectOne material of the Materials Project, as a row of the search table.
- Variables:
material_id (str) – The ID as the website writes it (
mp-5229).formula (str) – The reduced formula (
SrTiO3).space_group (str) – The Hermann-Mauguin symbol (
Pm-3m,P6_3/mmc).space_group_number (int) – 1-230.
crystal_system (str) –
Cubic,Tetragonal, …band_gap (float | None) – The computed band gap in eV (
Nonewhen not computed).energy_above_hull (float | None) – The energy above the convex hull in eV/atom.
nsites (int) – Number of sites of the stored (computed) cell.
experimental (bool) – True when the structure matches an experimentally observed one (an ICSD or other experimental entry); these are starred in the table.
structure (Any) – The downloaded pymatgen
Structure– set byfetch_materials()(in the cell it was asked for),Nonein search results.cell (str | None) – The cell
structureis in (seeCELL_CHOICES).deprecated (bool) – The Materials Project has deprecated this entry.
cell_space_group_number (int | None) – The space group spglib finds for
structure– set byfetch_materials(); it differs fromspace_group_numberonly when the standardization at the chosen tolerance lands on another group.
- classmethod from_document(doc)[source]#
A record from one document of the
materials/summaryendpoint.- Return type:
- band_gap: float | None#
- cell: str | None = None#
- cell_space_group_number: int | None = None#
- crystal_system: str#
- deprecated: bool = False#
- energy_above_hull: float | None#
- experimental: bool#
- formula: str#
- property label: str#
The ID with the star of an experimentally observed material.
- material_id: str#
- nsites: int#
- space_group: str#
- space_group_number: int#
- structure: Any = None#
- property url: str#
The page of the material on the Materials Project website.
- class crystod.search.SearchResult(query, description, materials, total, sort='ehull', filters=())[source]#
Bases:
objectThe answer to one search.
- Variables:
query (str) – The query as given.
description (str) – How it was read (
chemical system Sr-Ti-O, …).materials (list[crystod.mp_search.Material]) – The matches, in the order of
sort.total (int) – How many materials match; more than
len(materials)when the list was cut atmax_results.sort (str) – The key of
SORT_KEYSthe list is sorted by.filters (tuple[str, ...]) – One phrase per filter that was applied.
- table_lines()[source]#
The table of
format_table()for these materials.- Return type:
list[str]
- description: str#
- filters: tuple[str, ...] = ()#
- query: str#
- sort: str = 'ehull'#
- total: int#
- property truncated: bool#
True when more materials match than were listed.
- crystod.search.fetch_materials(material_ids, *, cell='conventional', tolerance=0.1, api_key=None)[source]#
Download the structures of the given materials.
- Parameters:
material_ids (str | list[str] | tuple[str, ...]) – One ID or a list (
mp-5229,mp-aaaaahtd, …).cell (str) –
"conventional"(default) or"primitive"(seestandardize_cell()).tolerance (float) – Symmetry tolerance of the standardization in Angstrom.
api_key (str | None) – The API key; by default read as
find_api_key()describes.
- Returns:
One
Materialper distinct material, in the order first given (a repeated ID, or the two spellingsmp-5229andmp-aaaaahtdof one ID, give one entry), withstructureandcell_space_group_numberset.- Raises:
SystemExit – A malformed ID, an ID the Materials Project does not have, or a tolerance that is not a positive number (
ValueErrorthroughcrystod.search).MaterialsProjectError – No API key, no network, or a server error.
- Return type:
list[Material]
- crystod.search.format_table(materials)[source]#
The search table as text lines, one header line and one line per material.
Formula, space group and ID are left-aligned, the numbers right-aligned; an experimentally observed material has
" *"(a space and a star) after its ID.- Return type:
list[str]
- crystod.search.interpret_query(query, subsystems=False)[source]#
Read a query the way the Materials Project website does.
- Returns:
a phrase for the header of the table and the filter parameters of the
materials/summaryendpoint.- Return type:
(description, criteria)- Raises:
SystemExit – The query cannot be read, or
subsystemsis set for a query that is not a single chemical system (ValueErrorthroughcrystod.search).
- crystod.search.normalize_material_id(text)[source]#
The website spelling of a Materials Project ID.
mp-aaaaahtd,MP-5229andmp-5229all givemp-5229; an ID pastLEGACY_ID_CUTOFFis given in the 8-letter alphabetical form (mp-3732049givesmp-aaaieiuj).- Raises:
SystemExit –
textis not a Materials Project ID (ValueErrorwhen called throughcrystod.search).- Return type:
str
- crystod.search.poscar_filename(material, directory=False)[source]#
The default file name of a fetched material’s POSCAR.
{prefix}_{formula}_{space group}_{ID}with the prefix ofPOSCAR_PREFIXESfor the cell:POSCAR_Sr2TiO4_I4mmm_mp-5532(conventional cell),PPOSCAR_Sr2TiO4_I4mmm_mp-5532(primitive cell). The/and_of the space-group symbol are dropped (P6_3/mmc->P63mmc). Withdirectory,{formula}_{space group}_{ID}/{prefix}(Sr2TiO4_I4mmm_mp-5532/POSCAR).- Return type:
str
- crystod.search.poscar_text(material)[source]#
The POSCAR of a fetched material; the comment line names its source.
- Return type:
str
- crystod.search.resolve_space_group(text)[source]#
(number, symbol)of a space group given by number or short symbol.The symbol is the Hermann-Mauguin short symbol as spglib and the Materials Project write it (
P6_3/mmc); the underscore of a screw axis may be left out (P63/mmc).- Raises:
SystemExit – Not a space group (
ValueErrorthrough the API).- Return type:
tuple[int, str]
- crystod.search.search_materials(query, *, experimental=False, stable=False, max_energy_above_hull=None, band_gap=None, nsites=None, space_group=None, exclude_elements=None, subsystems=False, sort='ehull', max_results=1000, api_key=None)[source]#
Search the Materials Project the way its website does.
- Parameters:
query (str) –
SrTiO3(formula),ABO3(anonymous formula),Sr-Ti-O(chemical system),Sr,Ti,O(elements) ormp-5229(IDs); seeinterpret_query().experimental (bool) – Only experimentally observed materials.
stable (bool) – Only materials on the convex hull.
max_energy_above_hull (float | None) – Upper bound in eV/atom.
band_gap (tuple[float, float] | None) –
(min, max)in eV.nsites (tuple[int, int] | None) –
(min, max)number of sites.space_group (str | int | None) – Number or short symbol.
exclude_elements (list[str] | tuple[str, ...] | None) – Element symbols the materials must not contain.
subsystems (bool) – With a chemical system, also every subsystem (
Sr-Ti-Oadds Sr, Ti, O, Sr-Ti, Sr-O and Ti-O).sort (str) – A key of
SORT_KEYS(default: energy above hull).max_results (int) – Cap on the number of materials listed. Up to 1000 of a longer list cost one request; more (or a list longer than 1000 sorted by space group, which the server cannot sort on) need every match fetched, which is refused beyond
MAX_FULL_FETCHmatches.api_key (str | None) – The API key; by default read as
find_api_key()describes.
- Raises:
SystemExit – Unreadable query or filter, or a list too long to fetch (
ValueErrorthroughcrystod.search).MaterialsProjectError – No API key, no network, or a server error.
- Return type:
- crystod.search.standardize_cell(structure, cell='conventional', tolerance=0.1)[source]#
structurein the requested cell."primitive"and"conventional"are the standardized cells of spglib (standardize_cellwith and withoutto_primitive, atoms moved onto their ideal positions), the setting CrystOD’s own analyses standardize to. The angle tolerance is 5 degrees, the value the Materials Project uses with itssymprec = 0.1.- Raises:
SystemExit – Unknown cell, a tolerance that is not a positive number, or a cell spglib cannot standardize (
ValueErrorthroughcrystod.search).
- crystod.search.write_poscar(material, path=None, *, directory=False, overwrite=False, rename_hint=True)[source]#
Write the POSCAR of a fetched material.
- Parameters:
material (Material) – A material from
fetch_materials().path (str | None) – File name; default
poscar_filename()(POSCAR_...for the conventional cell,PPOSCAR_...for the primitive one).directory (bool) – Write
{formula}_{space group}_{ID}/POSCAR(PPOSCARfor the primitive cell) instead.overwrite (bool) – Replace an existing file with different content.
rename_hint (bool) – Suggest
-oin the error about a file in the way (the command line turns it off where-ois not allowed).
- Returns:
(path, status)with status"wrote","kept"(an identical file was already there) or"replaced".- Raises:
SystemExit – A different file is in the way and
overwriteis False, the path is a directory, or the file cannot be written (ValueErrorthroughcrystod.search).- Return type:
tuple[str, str]
- crystod.search.SORT_KEYS#
Sort orders of
--sort: name -> (field the server sorts on, description). The server cannot sort on the space-group number (it silently ignores a nested field such assymmetry.number), sospgsorts locally. The default, the energy above the hull, is the order of the website’s table.
- crystod.search.CELL_CHOICES = ('conventional', 'primitive')#
Cells
--cellcan write: the standardized conventional cell (default) or the standardized primitive cell.
- crystod.search.POSCAR_PREFIXES = {'conventional': 'POSCAR', 'primitive': 'PPOSCAR'}#
File-name prefix of each cell:
POSCAR_SrTiO3_Pm-3m_mp-5229holds the conventional cell,PPOSCAR_...the primitive one (the P of phonopy’s PPOSCAR and of CrystOD’s221_PPOSCAR_*examples); with--directorythe prefix is the file name.