def step2(**kwargs): """ post process: get energies from the jobs in the previous step and generate the phase diagram """ chkfile = kwargs['checkpoint_files'][0] all_jobs = jobs_from_file(chkfile) entries = [] # add endpoint data Al = Composition("Al1O0") energy_al = -3.36 O = Composition("Al0O1") energy_o = -2.58 entries.append(PDEntry(Al, energy_al)) entries.append(PDEntry(O, energy_o)) # get data and create entries for job in all_jobs: comp = job.vis.mplmp.structure.composition energy = job.final_energy entries.append(PDEntry(comp, energy)) pd = PhaseDiagram(entries) plotter = PDPlotter(pd, show_unstable=True) plotter.write_image('Al_O_phasediagram.jpg') return None
def pd_plot(system='Ni-Nb'): object_file = pickle.load(open('form_en_gbsave_v25h', "rb")) output = [] l = system.split('-') comb = [] for i in range(len(l)): comb += itertools.combinations(l, i + 1) comb_list = [list(t) for t in comb] # comb_list=['Zn','O','Zn-O'] with MPRester("") as m: for i in comb_list: dd = '-'.join(i) print dd data = m.get_data(dd) for d in data: x = {} x['material_id'] = str(d['material_id']) structure = m.get_structure_by_material_id(x['material_id']) X = get_comp_descp(struct=structure) print "X", X pred = object_file.predict(X) print structure.composition.reduced_formula, pred[0], d[ 'formation_energy_per_atom'], str(d['material_id']) output.append( PDEntry(Composition(structure.composition), float(pred[0]))) pd = PhaseDiagram(output) print output plotter = PDPlotter(pd, show_unstable=True) name = str(system) + str('_phasediagram.png') plotter.write_image(name, image_format="png")
def setUp(self): (elements, entries) = PDEntryIO.from_csv(os.path.join(module_dir, "pdentries_test.csv")) self.pd = PhaseDiagram(entries) self.plotter = PDPlotter(self.pd, show_unstable=True) entrieslio = [e for e in entries if len(e.composition) < 3 and ("Fe" not in e.composition)] self.pd_formation = PhaseDiagram(entrieslio) self.plotter_formation = PDPlotter(self.pd_formation, show_unstable=True) entries.append(PDEntry("C", 0)) self.pd3d = PhaseDiagram(entries) self.plotter3d = PDPlotter(self.pd3d, show_unstable=True)
def setUp(self): (elements, entries) = PDEntryIO.from_csv( os.path.join(module_dir, "pdentries_test.csv")) self.pd = PhaseDiagram(entries) self.plotter = PDPlotter(self.pd, show_unstable=True) entrieslio = [ e for e in entries if len(e.composition) < 3 and ("Fe" not in e.composition) ] self.pd_formation = PhaseDiagram(entrieslio) self.plotter_formation = PDPlotter(self.pd_formation, show_unstable=True) entries.append(PDEntry("C", 0)) self.pd3d = PhaseDiagram(entries) self.plotter3d = PDPlotter(self.pd3d, show_unstable=True)
def get_phase_diagram_plot(self): """ Returns a phase diagram plot, as a matplotlib plot object. """ # set the font to Times, rendered with Latex plt.rc('font', **{'family': 'serif', 'serif': ['Times']}) plt.rc('text', usetex=True) # parse the composition space endpoints endpoints_line = self.lines[0].split() endpoints = [] for word in endpoints_line[::-1]: if word == 'endpoints:': break else: endpoints.append(Composition(word)) if len(endpoints) < 2: print('There must be at least 2 endpoint compositions to make a ' 'phase diagram.') quit() # parse the compositions and total energies of all the structures compositions = [] total_energies = [] for i in range(4, len(self.lines)): line = self.lines[i].split() compositions.append(Composition(line[1])) total_energies.append(float(line[2])) # make a list of PDEntries pdentries = [] for i in range(len(compositions)): pdentries.append(PDEntry(compositions[i], total_energies[i])) # make a CompoundPhaseDiagram compound_pd = CompoundPhaseDiagram(pdentries, endpoints) # make a PhaseDiagramPlotter pd_plotter = PDPlotter(compound_pd, show_unstable=100) return pd_plotter.get_plot(label_unstable=False)
def test_dim1(self): #Ensure that dim 1 PDs can eb generated. for el in ["Li", "Fe", "O2"]: entries = [ e for e in self.entries if e.composition.reduced_formula == el ] pd = PhaseDiagram(entries) self.assertEqual(len(pd.stable_entries), 1) a = PDAnalyzer(pd) for e in entries: decomp, ehull = a.get_decomp_and_e_above_hull(e) self.assertGreaterEqual(ehull, 0) plotter = PDPlotter(pd) lines, stable_entries, unstable_entries = plotter.pd_plot_data self.assertEqual(lines[0][1], [0, 0])
class PDPlotterTest(unittest.TestCase): def setUp(self): (elements, entries) = PDEntryIO.from_csv( os.path.join(module_dir, "pdentries_test.csv")) self.pd = PhaseDiagram(entries) self.plotter = PDPlotter(self.pd, show_unstable=True) entrieslio = [ e for e in entries if len(e.composition) < 3 and ("Fe" not in e.composition) ] self.pd_formation = PhaseDiagram(entrieslio) self.plotter_formation = PDPlotter(self.pd_formation, show_unstable=True) entries.append(PDEntry("C", 0)) self.pd3d = PhaseDiagram(entries) self.plotter3d = PDPlotter(self.pd3d, show_unstable=True) def test_pd_plot_data(self): (lines, labels, unstable_entries) = self.plotter.pd_plot_data self.assertEqual(len(lines), 22) self.assertEqual(len(labels), len(self.pd.stable_entries), "Incorrect number of lines generated!") self.assertEqual( len(unstable_entries), len(self.pd.all_entries) - len(self.pd.stable_entries), "Incorrect number of lines generated!") (lines, labels, unstable_entries) = self.plotter3d.pd_plot_data self.assertEqual(len(lines), 33) self.assertEqual(len(labels), len(self.pd3d.stable_entries)) self.assertEqual( len(unstable_entries), len(self.pd3d.all_entries) - len(self.pd3d.stable_entries)) (lines, labels, unstable_entries) = self.plotter_formation.pd_plot_data self.assertEqual(len(lines), 3) self.assertEqual(len(labels), len(self.pd_formation.stable_entries)) def test_get_plot(self): # Some very basic non-tests. Just to make sure the methods are callable. self.plotter.get_plot() self.plotter3d.get_plot() # self.plotter.get_plot(energy_colormap="Reds", process_attributes=True) # plt = self.plotter3d.get_plot(energy_colormap="Reds", process_attributes=True) # self.plotter.get_plot(energy_colormap="Reds", process_attributes=False) # plt = self.plotter3d.get_plot(energy_colormap="Reds", # process_attributes=False) self.plotter.get_chempot_range_map_plot([Element("Li"), Element("O")])
class PDPlotterTest(unittest.TestCase): def setUp(self): (elements, entries) = PDEntryIO.from_csv(os.path.join( module_dir, "pdentries_test.csv")) self.pd = PhaseDiagram(entries) self.plotter = PDPlotter(self.pd, show_unstable=True) entrieslio = [e for e in entries if len(e.composition) < 3 and ("Fe" not in e.composition)] self.pd_formation = PhaseDiagram(entrieslio) self.plotter_formation = PDPlotter(self.pd_formation, show_unstable=True) entries.append(PDEntry("C", 0)) self.pd3d = PhaseDiagram(entries) self.plotter3d = PDPlotter(self.pd3d, show_unstable=True) def test_pd_plot_data(self): (lines, labels, unstable_entries) = self.plotter.pd_plot_data self.assertEqual(len(lines), 22) self.assertEqual(len(labels), len(self.pd.stable_entries), "Incorrect number of lines generated!") self.assertEqual(len(unstable_entries), len(self.pd.all_entries) - len(self.pd.stable_entries), "Incorrect number of lines generated!") (lines, labels, unstable_entries) = self.plotter3d.pd_plot_data self.assertEqual(len(lines), 33) self.assertEqual(len(labels), len(self.pd3d.stable_entries)) self.assertEqual(len(unstable_entries), len(self.pd3d.all_entries) - len(self.pd3d.stable_entries)) (lines, labels, unstable_entries) = self.plotter_formation.pd_plot_data self.assertEqual(len(lines), 3) self.assertEqual(len(labels), len(self.pd_formation.stable_entries)) def test_get_plot(self): # Some very basic non-tests. Just to make sure the methods are callable. self.plotter.get_plot() self.plotter3d.get_plot() # self.plotter.get_plot(energy_colormap="Reds", process_attributes=True) # plt = self.plotter3d.get_plot(energy_colormap="Reds", process_attributes=True) # self.plotter.get_plot(energy_colormap="Reds", process_attributes=False) # plt = self.plotter3d.get_plot(energy_colormap="Reds", # process_attributes=False) self.plotter.get_chempot_range_map_plot([Element("Li"), Element("O")])
el1) and x['forcefield'] == ff: output.append( PDEntry(Composition(x['composition']), float(x['totenergy']))) return output ##Used for phase diagram entries = get_entries_from_json(key='search', value='Cu-H-O', out='energy', ff='ffield.CuOCH.comb3', json_f='data.json', PD=True) #entries=get_entries_from_json(key='search',value='Fe-H-O',out='energy',ff='ffield.reax.Fe_O_C_H',json_f='data.json',PD=True) #entries=get_entries_from_json(key='search',value='Al-Ni',out='energy',ff='Mishin-Ni-Al-2009.eam.alloy',json_f='data.json',PD=True) pd = PhaseDiagram(entries) plotter = PDPlotter(pd, show_unstable=False) #plotter = PDPlotter(pd, show_unstable=True) #print plotter.pd_plot_data print plotter.get_plot name = str('Cu-H-Ocomb') + str('_LMP_phasediagram.png') plotter.write_image(name, image_format="png") ## ##INDIVIDUAL #output=get_entries_from_json(key='mpid',value='mp-13',out='energy',ff='ffield.reax.Fe_O_C_H',json_f='data.json') get_entries_from_json(key='mpid', value='mp-134', out='Bv', json_f='data.json') ##
projection=projection, collection="compositions", contributor_only=False, )[0] # append spectra to output PDF plotly_urls = doc[cid]["LBNL"][str(cid)]["plotly_urls"] for plotly_url in plotly_urls: image_bytes = requests.get("{}.pdf".format(plotly_url)).content merger.append(StringIO(image_bytes)) # get phase diagram from MP and append to PDF chemsys = ["Co", "Fe", "V"] # ["Ni", "Fe", "Pt"] # alphabetic cmap = ["Reds", "Blues", "Greens"] entries = mpr.get_entries_in_chemsys(chemsys) pd = PhaseDiagram(entries) plotter = PDPlotter(pd) # grid gx, gy = [], [] n = 20 fn = float(n) for i in range(0, n, 1): for k in range(n - i + 1, 0, -1): j = n + 1 - i - k x0, x1, x2 = i / fn, k / fn, j / fn gx.append(x0 + x2 / 2.0) # NOTE x0 might need to be replace with x1 gy.append(x2 * math.sqrt(3.0) / 2.0) grid_triang = tri.Triangulation(gx, gy) fields_strings = [
def setUp(self): (elements, entries) = PDEntryIO.from_csv( os.path.join(module_dir, "pdentries_test.csv")) self.pd = PhaseDiagram(entries) self.plotter = PDPlotter(self.pd)
#for val in content: # if val != '' and val !='\n' and val !='\r\n': # list_el.append(val) # for i in range(0,len(list_el)): # if i!=0: # element_ff.append(list_el[i]) # print ff,' ',element_ff element_ff=['Ti','O','N'] with MPRester(MAPI_KEY) as m: data = m.get_entries_in_chemsys(element_ff,inc_structure='final', property_data=["unit_cell_formula","material_id","icsd_id","spacegroup","energy_per_atom","formation_energy_per_atom","pretty_formula","band_gap","total_magnetization","e_above_hull"]) if (len(element_ff)>1): try: entries = m.get_entries_in_chemsys(element_ff) pd = PhaseDiagram(entries) plotter = PDPlotter(pd, show_unstable=True) image=str(ff)+str("_DFT")+str(".jpg") plotter.write_image(image) except: pass structures=[] structures_cvn=[] icsd_arr=[] mp_arr=[] sg_arr=[] enp_arr=[] fenp_arr=[] pf_arr=[] ucf_arr=[] bg_arr=[] tm_arr=[]
doc[cid] = mpr.query_contributions( criteria=criteria, projection=projection, collection='compositions', contributor_only=False )[0] # append spectra to output PDF plotly_urls = doc[cid]['LBNL'][str(cid)]['plotly_urls'] for plotly_url in plotly_urls: image_bytes = requests.get('{}.pdf'.format(plotly_url)).content merger.append(StringIO(image_bytes)) # get phase diagram from MP and append to PDF chemsys = ["Co", "Fe", "V"]#["Ni", "Fe", "Pt"] # alphabetic cmap = ['Reds', 'Blues', 'Greens'] entries = mpr.get_entries_in_chemsys(chemsys) pd = PhaseDiagram(entries) plotter = PDPlotter(pd) # grid gx, gy = [], [] n = 20 fn = float(n) for i in range(0, n, 1): for k in range(n-i+1, 0, -1): j = n+1-i-k x0, x1, x2 = i/fn, k/fn, j/fn gx.append(x0+x2/2.) # NOTE x0 might need to be replace with x1 gy.append(x2*math.sqrt(3.)/2.) grid_triang = tri.Triangulation(gx, gy) fields_strings = [ 'xas normalization to min and max -> normalization factor',
Composition(formula).reduced_formula ] relevant_entries = sorted(relevant_entries, key=lambda e: e.energy_per_atom) return relevant_entries[0] def get_comp_entries(formula, all_entries): relevant_entries = [ entry for entry in all_entries if entry.composition.reduced_formula == Composition(formula).reduced_formula ] return relevant_entries def find_entry_index(formula, all_entries): entry_index = [ all_entries.index(entry) for entry in all_entries if entry.composition.reduced_formula == Composition( formula).reduced_formula ] return entry_index rester = MPRester( '5TxDLF4Iwa7rGcAl') #Generate your own key from materials project.. mp_entries = rester.get_entries_in_chemsys(["Li", "Fe", "O", "S"]) pd = PhaseDiagram(mp_entries) plotter = PDPlotter(pd) plotter.show()
#content=(lines[3]).split("' '|\n|\r\n") for val in content: if val != '' and val !='\n' and val !='\r\n': list_el.append(val) for i in range(0,len(list_el)): if i!=0: element_ff.append(list_el[i]) # print ff,' ',element_ff with MPRester(MAPI_KEY) as m: data = m.get_entries_in_chemsys(element_ff,inc_structure='final', property_data=["unit_cell_formula","material_id","icsd_id","spacegroup","energy_per_atom","formation_energy_per_atom","pretty_formula","band_gap","total_magnetization","e_above_hull"]) if (len(element_ff)>1): try: entries = m.get_entries_in_chemsys(element_ff) pd = PhaseDiagram(entries) plotter = PDPlotter(pd, show_unstable=True) image=str(ff)+str("_DFT")+str(".jpg") plotter.write_image(image) except: pass structures=[] structures_cvn=[] icsd_arr=[] mp_arr=[] sg_arr=[] enp_arr=[] fenp_arr=[] pf_arr=[] ucf_arr=[] bg_arr=[] tm_arr=[]
] return relevant_entries def find_entry_index(formula, all_entries): entry_index = [ all_entries.index(entry) for entry in all_entries if entry.composition.reduced_formula == Composition( formula).reduced_formula ] return entry_index rester = MPRester( '5TxDLF4Iwa7rGcAl') #Generate your own key from materials project.. mp_entries = rester.get_entries_in_chemsys(["Li", "Fe", "O", "S"]) pd = PhaseDiagram(mp_entries) plotter = PDPlotter(pd) analyzer = PDAnalyzer(pd) li_entries = [e for e in mp_entries if e.composition.reduced_formula == "Li"] uli0 = min(li_entries, key=lambda e: e.energy_per_atom).energy_per_atom el_profile = analyzer.get_element_profile(Element("Li"), Composition("Li2FeSO")) for i, d in enumerate(el_profile): voltage = -(d["chempot"] - uli0) print("Voltage: %s V" % voltage) print(d["reaction"]) print("")
try: # Create phase diagram! with warnings.catch_warnings(): warnings.simplefilter("ignore") phase = PhaseDiagram(Computed_entries) except Exception as e: result["error"] = str(e) json_dir = output_Path + "/" + "output.json" with open(json_dir, "w") as outfile: json.dump(result, outfile) print(result) exit() # Plot! plotter = PDPlotter(phase, show_unstable=True) #plotter.show() lines, stable, unstable = plotter.pd_plot_data ### NEED_TO_CHANGE ### plt = plotter.get_plot() f = plt.gcf() f.set_size_inches((15, 12)) fig_dir = output_Path + "/" + "phase.png" plt.savefig(fig_dir, image_format="png") # Analyze phase diagram! pda = PDAnalyzer(phase) # elements : enum Element to array