def hull_data(self, fjson=False, remake=False): """ Args: fjson (str) - file name to write hull data to remake (bool) - if True, write json; if False, read json Returns: dict of {chemical space (str) : {formula (str) : {'E' : formation energy (float), 'amts' : {el (str) : fractional amt of el in formula (float) for el in space}} for all relevant formulas including elements} elements are automatically given formation energy = 0 chemical space is now in 'el1_el2_...' format to be jsonable """ if not fjson: fjson = 'hull_input_data.json' if (remake == True) or not os.path.exists(fjson): hull_data = {} hull_spaces = self.hull_spaces compounds = self.compounds compound_to_energy = self.compound_to_energy for space in hull_spaces: for el in space: compound_to_energy[el] = 0 relevant_compounds = [c for c in compounds if set(CompAnalyzer(c).els).issubset(set(space))] + list(space) hull_data['_'.join(list(space))] = {c : {'E' : compound_to_energy[c], 'amts' : {el : CompAnalyzer(c).fractional_amt_of_el(el=el) for el in space}} for c in relevant_compounds} return write_json(hull_data, fjson) else: return read_json(fjson)
def big_query(self, fjson, criteria, props, remove_polymorphs=True, remake=False, write_it=True): """ Args: fjson (str) - where to write data (if write_it=True) criteria (dict) - MongoDB-style query input (https://github.com/materialsproject/mapidoc) props (list) - list of queryable properties (str) remove_polymorphs - if True: filter data to only include ground-state structures remake - if True: rewrite json; else: read json write_it - if True: write json; else: return dictionary Returns: dictionary of MP data corresponding with query based on criteria and props """ if (remake == True) or not os.path.exists(fjson): if 'material_id' not in props: props += ['material_id'] data = {'criteria': criteria, 'properties': props} r = requests.post('https://materialsproject.org/rest/v2/query', headers={'X-API-KEY': self.api_key}, data={k: json.dumps(v) for k,v in data.items()}) list_of_dicts = r.json()['response'] id_key_dict = list_of_dicts_to_dict(list_of_dicts, 'material_id', props) if remove_polymorphs == True: id_key_dict = self.get_ground_states_from_MP(id_key_dict) if write_it == True: return write_json(id_key_dict, fjson) else: return id_key_dict else: return read_json(fjson)
def parallel_hull_data(compound_to_energy, hull_spaces, fjson=False, remake=False, Nprocs=4, verbose=False): import multiprocessing as mp if not fjson: fjson = 'hull_input_data.json' if (remake == True) or not os.path.exists(fjson): hull_data = {} compounds = sorted(list(compound_to_energy.keys())) pool = mp.Pool(processes=Nprocs) # argss = [(compound_to_energy, compounds, space, verbose) for space in hull_spaces] # results = pool.map(_hullin_from_space, argss) results = [ r for r in pool.starmap(_hullin_from_space, [( compound_to_energy, compounds, space, verbose) for space in hull_spaces]) ] keys = ['_'.join(list(space)) for space in hull_spaces] hull_data = dict(zip(keys, results)) return write_json(hull_data, fjson) else: return read_json(fjson)
def smallest_spaces(hullin, compounds, fjson=False, remake=False, Nprocs=4, verbose=False): """ Args: Nprocs (int) - processors to parallelize over remake (bool) - run this (True) or read this (False) Returns: {formula (str) : chemical space (str, '_'.join(elements), convex hull) that is easiest to compute} """ import multiprocessing as mp if not fjson: fjson = 'smallest_spaces.json' if not remake and os.path.exists(fjson): return read_json(fjson) pool = mp.Pool(processes=Nprocs) # argss = [(hullin, compound, verbose) for compound in compounds] # smallest = pool.map(_smallest_space, argss) smallest = [ r for r in pool.starmap(_smallest_space, [(hullin, compound, verbose) for compound in compounds]) ] data = dict(zip(compounds, smallest)) return write_json(data, fjson)
def parallel_hullout(hullin, smallest_spaces, compounds='all', fjson=False, remake=False, Nprocs=4, verbose=False): """ Args: Nprocs (int) - processors to parallelize over remake (bool) - run this (True) or read this (False) Returns: {formula (str) : {'Ef' : formation energy (float, eV/atom), 'Ed' : decomposition energy (float, eV/atom), 'rxn' : decomposition reaction (str), 'stability' : bool (True if on hull)} } """ import multiprocessing as mp if not fjson: fjson = 'hullout.json' if not remake and os.path.exists(fjson): return read_json(fjson) pool = mp.Pool(processes=Nprocs) if compounds == 'all': compounds = sorted(list(smallest_spaces.keys())) results = [r for r in pool.starmap(_compound_stability, [(smallest_spaces, hullin, compound, verbose) for compound in compounds])] data = dict(zip(compounds, results)) return write_json(data, fjson)
def hull_spaces(self, fjson=False, remake=False, write=False): """ Args: Returns: list of unique chemical spaces (set) that do define convex hull spaces """ if not fjson: fjson = 'hull_spaces.json' if not remake and os.path.exists(fjson): d = read_json(fjson) return d['hull_spaces'] chemical_spaces_and_subspaces = self.chemical_spaces_and_subspaces chemical_subspaces = self.chemical_subspaces d = {'hull_spaces' : [s for s in chemical_spaces_and_subspaces if s not in chemical_subspaces if len(s) > 1]} if write: d = write_json(d, fjson) return d['hull_spaces']
def make_data(remake=False): fjson = '_data_for_RxnEngr.json' if not remake and os.path.exists(fjson): return read_json(fjson) data_file = '/Users/chrisbartel/Dropbox/postdoc/projects/synthesis/paperdb/data/mp/MP_stability.json' data = read_json(data_file) my_els = ['Li', 'Co', 'Ba', 'Ti', 'Y', 'Ba', 'Cu', 'C', 'O'] cmpds = sorted(list(data['0'].keys())) relevant_cmpds = [ c for c in cmpds if set(CompAnalyzer(c).els).issubset(set(sorted(my_els))) ] d = {T: {} for T in data} for c in relevant_cmpds: for T in d: if c in data[T]: d[T][c] = data[T][c] return write_json(d, fjson)
def specific_query(self, fjson, tag, props, remove_polymorphs=True, remake=False, write_it=True): """ Args: fjson (str) - where to write data (if write_it=True) tag (str) - chemical system (el1-el2-...), formula (Al2O3), or ID (mp-1234) on which to query props (list) - list of queryable properties (str) remove_polymorphs - if True: filter data to only include ground-state structures remake - if True: rewrite json; else: read json write_it - if True: write json; else: return dictionary Returns: dictionary of MP data corresponding with query based on tag and props """ if (remake == True) or not os.path.exists(fjson): list_of_dicts = self.rester.get_data(tag, 'vasp', '') id_key_dict = list_of_dicts_to_dict(list_of_dicts, 'material_id', props) if remove_polymorphs == True: id_key_dict = self.get_ground_states_from_MP(id_key_dict) if write_it == True: return write_json(id_key_dict, fjson) else: return id_key_dict else: return read_json(fjson)
def specific_hull_query(self, fjson, elements, props, remove_polymorphs=True, remake=False, write_it=True, include_els=False): """ Args: fjson (str) - where to write data (if write_it=True) elements (list) - list of elements (str) that comprise desired chemical space props (list) - list of queryable properties (str) remove_polymorphs - if True: filter data to only include ground-state structures remake - if True: rewrite json; else: read json write_it - if True: write json; else: return dictionary include_els (bool) - if True, also retrieve the elemental phases; else: don't Returns: dictionary of MP data corresponding with stability-related query for chemical space defined by elements """ if (remake == True) or not os.path.exists(fjson): spaces = [list(combinations(elements, i)) for i in range(2, len(elements)+1)] spaces = [j for i in spaces for j in i] all_data = {} for space in spaces: space = '-'.join(sorted(list(space))) space_data = self.specific_query('blah.json', space, props, remove_polymorphs, remake=True, write_it=False) all_data[space] = space_data if include_els: for el in elements: all_data[el] = self.specific_query('blah.json', el, props, remove_polymorphs, True, False) id_key_dict = {} for k in all_data: for ID in all_data[k]: id_key_dict[ID] = all_data[k][ID] # if remove_polymorphs == True: # id_key_dict = self.get_ground_states_from_MP(id_key_dict) if write_it == True: return write_json(id_key_dict, fjson) else: return id_key_dict else: return read_json(fjson)