def pmg2dict(pmg: Structure, as_model=False) -> Union[dict, Crystal]: """ Converts a pymatgen.Structure to a matador document/Crystal. Parameters: pmg (pymatgen.Structure): the structure to convert. Keyword arguments: as_model (bool): if True, return a Crystal instead of a dict. Returns: Union[dict, Crystal]: the converted structure. """ from matador.utils.ase_utils import ase2dict return ase2dict(AseAtomsAdaptor.get_atoms(pmg), as_model=as_model)
def _ase_cif2dict(fname): """ Read cif file into ASE object, then convert ASE Atoms into matador document. Parameters: fname (str): cif filename Returns: (dict, bool): simple matador document with error status. """ import ase.io from matador.utils.ase_utils import ase2dict fname = fname.replace('.cif', '') atoms = ase.io.read(fname + '.cif') doc = ase2dict(atoms) return doc, True
def test_ase2dict(self): doc = ase2dict(self.ase_atoms) self.assertEqual(doc["atom_types"], sorted(["Si", "O", "Si", "O", "Si", "O", "Si", "O"])) self.assertListEqual(doc["lattice_cart"][0], [12, 0, 0]) self.assertListEqual(doc["lattice_cart"][1], [0, 12, 0]) self.assertListEqual(doc["lattice_cart"][2], [0, 0, 12]) self.assertListEqual(doc["lattice_abc"][0], [12, 12, 12]) self.assertListEqual(doc["lattice_abc"][1], [90, 90, 90]) self.assertAlmostEqual(doc["cell_volume"], 12**3) self.assertEqual(len(doc["positions_frac"]), len(doc["atom_types"])) self.assertListEqual(doc["positions_frac"][0], [0.5, 0.0, 0.0]) self.assertListEqual(doc["positions_frac"][1], [0.5, 0.5, 0.5]) self.assertListEqual(doc["positions_frac"][3], [0.0, 0.5, 0.0]) self.assertEqual(doc["num_atoms"], 8) self.assertEqual(doc["num_fu"], 4) self.assertListEqual(doc["stoichiometry"][0], ["O", 1.0]) self.assertListEqual(doc["stoichiometry"][1], ["Si", 1.0]) self.assertListEqual(sorted(list(doc["elems"])), ["O", "Si"]) self.assertEqual(doc["space_group"], "Fm-3m") self.assertDictEqual(doc["ase_info"], self.ase_atoms.info)
def mp2dict(response): """ Convert a response from pymatgen.MPRester into a matador document, via an ASE atoms object. Expects certain properties to be requested in order to construct a full matador document, e.g. `structure` & `input`. Parameters: response (dict): containing one item of the MPRester response. """ if 'structure' not in response: raise RuntimeError('`structure` key missing, nothing to scrape...') ase_struct = AseAtomsAdaptor.get_atoms(response['structure']) doc = ase2dict(ase_struct) doc['source'] = [] if 'material_id' in response: doc['_mp_id'] = response['material_id'] doc['source'].append('{}'.format(doc['_mp_id'])) if 'pf_ids' in response: if response['pf_ids']: doc['_mp_pf_ids'] = response['pf_ids'] for _id in doc['_mp_pf_ids']: doc['source'].append('pf-{}'.format(_id)) if 'icsd_ids' in response: if response['icsd_ids']: doc['icsd'] = response['icsd_ids'][0] doc['_mp_icsd_ids'] = response['icsd_ids'] if 'formation_energy_per_atom' in response: doc['formation_energy_per_atom'] = response['formation_energy_per_atom'] doc['enthalpy_per_atom'] = response['formation_energy_per_atom'] doc['enthalpy'] = doc['enthalpy_per_atom'] * doc['num_atoms'] doc['formation_energy'] = doc['formation_energy_per_atom'] * doc['num_atoms'] if 'e_above_hull' in response: doc['hull_distance'] = response['e_above_hull'] if 'doi' in response: doc['_mp_doi'] = response['doi'] if 'snl' in response: doc['_mp_references'] = response['snl'].references if 'input' in response: incar = response['input']['incar'] if not isinstance(incar, dict): incar = incar.as_dict() doc['cut_off_energy'] = incar['ENCUT'] doc['xc_functional'] = 'PBE' kpts = response['input']['kpoints'] doc['kpoints_mp_grid'] = kpts.kpts[0] doc['kpoints_mp_offset'] = kpts.kpts_shift doc['pressure'] = 0.0 doc['stress'] = 0.0 doc['kpoints_mp_spacing'] = calc_mp_spacing(doc['lattice_cart'], doc['kpoints_mp_grid']) doc['spin_polarized'] = bool(incar['ISPIN'] - 1) return doc
def from_ase(cls, atoms): from matador.utils.ase_utils import ase2dict return ase2dict(atoms, as_model=True)