|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import json |
3 | 4 | from collections import defaultdict
|
4 | 5 |
|
5 |
| -from emmet.core.phonon import PhononBSDOSDoc |
| 6 | +from monty.json import MontyDecoder |
| 7 | +from emmet.core.phonon import PhononBSDOSDoc, PhononBS, PhononDOS |
6 | 8 |
|
7 |
| -from mp_api.client.core import BaseRester |
| 9 | +from mp_api.client.core import BaseRester, MPRestError |
8 | 10 | from mp_api.client.core.utils import validate_ids
|
9 | 11 |
|
10 | 12 |
|
@@ -61,3 +63,74 @@ def search(
|
61 | 63 | fields=fields,
|
62 | 64 | **query_params,
|
63 | 65 | )
|
| 66 | + |
| 67 | + def get_bandstructure_from_material_id(self, material_id: str, phonon_method: str): |
| 68 | + """Get the phonon band structure pymatgen object associated with a given material ID and phonon method. |
| 69 | +
|
| 70 | + Arguments: |
| 71 | + material_id (str): Material ID for the phonon band structure calculation |
| 72 | + phonon_method (str): phonon method, i.e. pheasy or dfpt |
| 73 | +
|
| 74 | + Returns: |
| 75 | + bandstructure (PhononBS): PhononBS object |
| 76 | + """ |
| 77 | + decoder = MontyDecoder().decode if self.monty_decode else json.loads |
| 78 | + result = self._query_open_data( |
| 79 | + bucket="materialsproject-parsed", |
| 80 | + key=f"ph-bandstructures/{phonon_method}/{material_id}.json.gz", |
| 81 | + decoder=decoder, |
| 82 | + )[0] |
| 83 | + |
| 84 | + if not result or not result[0]: |
| 85 | + raise MPRestError("No object found") |
| 86 | + |
| 87 | + if self.use_document_model: |
| 88 | + return PhononBS(**result[0]) |
| 89 | + |
| 90 | + return result[0] |
| 91 | + |
| 92 | + def get_dos_from_material_id(self, material_id: str, phonon_method: str): |
| 93 | + """Get the phonon dos pymatgen object associated with a given material ID and phonon method. |
| 94 | +
|
| 95 | + Arguments: |
| 96 | + material_id (str): Material ID for the phonon dos calculation |
| 97 | + phonon_method (str): phonon method, i.e. pheasy or dfpt |
| 98 | +
|
| 99 | + Returns: |
| 100 | + dos (PhononDOS): PhononDOS object |
| 101 | + """ |
| 102 | + decoder = MontyDecoder().decode if self.monty_decode else json.loads |
| 103 | + result = self._query_open_data( |
| 104 | + bucket="materialsproject-parsed", |
| 105 | + key=f"ph-dos/{phonon_method}/{material_id}.json.gz", |
| 106 | + decoder=decoder, |
| 107 | + )[0] |
| 108 | + |
| 109 | + if not result or not result[0]: |
| 110 | + raise MPRestError("No object found") |
| 111 | + |
| 112 | + if self.use_document_model: |
| 113 | + return PhononDOS(**result[0]) |
| 114 | + |
| 115 | + return result[0] |
| 116 | + |
| 117 | + def get_forceconstants_from_material_id(self, material_id: str): |
| 118 | + """Get the force constants associated with a given material ID. |
| 119 | +
|
| 120 | + Arguments: |
| 121 | + material_id (str): Material ID for the force constants calculation |
| 122 | +
|
| 123 | + Returns: |
| 124 | + force constants (list[list[Matrix3D]]): PhononDOS object |
| 125 | + """ |
| 126 | + decoder = MontyDecoder().decode if self.monty_decode else json.loads |
| 127 | + result = self._query_open_data( |
| 128 | + bucket="materialsproject-parsed", |
| 129 | + key=f"ph-force-constants/{material_id}.json.gz", |
| 130 | + decoder=decoder, |
| 131 | + )[0] |
| 132 | + |
| 133 | + if not result or not result[0]: |
| 134 | + raise MPRestError("No object found") |
| 135 | + |
| 136 | + return result[0] |
0 commit comments