def test_slide_energy():
    energies = [
        DefectEnergy("Va_Mg1", [0], [0], [0]),
        DefectEnergy("Va_O1", [1], [0], [0]),
        DefectEnergy("Va_O2", [2], [0], [0]),
        DefectEnergy("Mg_i1", [0], [0], [0])
    ]
    actual = slide_energy(energies, 1.0)
    expected = [
        DefectEnergy("Va_Mg1", [0], [0], [0]),
        DefectEnergy("Va_O1", [1], [1.0], [0]),
        DefectEnergy("Va_O2", [2], [2.0], [0]),
        DefectEnergy("Mg_i1", [0], [0], [0])
    ]
    assert actual == expected
    """
Example #2
0
    def __init__(self,
                 title: str,
                 defect_energies: List[DefectEnergy],
                 vbm: float,
                 cbm: float,
                 supercell_vbm: float,
                 supercell_cbm: float,
                 supercell_edge: bool = False,
                 y_range: Optional[List[float]] = None,
                 vline_threshold: float = 0.02,
                 x_unit: Optional[str] = "eV",
                 y_unit: Optional[str] = "eV",
                 **plot_settings):

        self._title = title
        self._supercell_edge = supercell_edge
        self._base_level = vbm
        if supercell_edge:
            self._x_range = [supercell_vbm - vbm, supercell_cbm - vbm]
            self._vbm = vbm - self._base_level
            self._cbm = cbm - self._base_level

        else:
            self._x_range = [0, cbm - vbm]

        self._supercell_vbm = supercell_vbm - self._base_level
        self._supercell_cbm = supercell_cbm - self._base_level

        self._defect_energies = slide_energy(defect_energies, self._base_level)

        if y_range:
            self._y_range = y_range
        else:
            all_y = []
            for de in self._defect_energies:
                cp = de.cross_points(self._x_range[0], self._x_range[1])
                xs, ys = cp.t_all_sorted_points
                all_y.extend(ys)
            self._y_range = [min(all_y) - 0.2, max(all_y) + 0.2]

        self._vline_threshold = vline_threshold
        self._x_unit = x_unit
        self._y_unit = y_unit
Example #3
0
def make_defect_formation_energy(args):
    formula = args.perfect_calc_results.structure.composition.reduced_formula
    chem_pot_diag = ChemPotDiag.from_yaml(args.cpd_yaml)
    pcr = args.perfect_calc_results

    defects, defect_entries, corrections, edge_states = [], [], [], []
    for d in args.dirs:
        if args.skip_shallow:
            edge_states = BandEdgeStates.from_yaml(d / "band_edge_states.yaml")
            if edge_states.is_shallow:
                continue
        defects.append(loadfn(d / "calc_results.json"))
        defect_entries.append(loadfn(d / "defect_entry.json"))
        corrections.append(loadfn(d / "correction.json"))

    if args.web_gui:
        from crystal_toolkit.settings import SETTINGS
        import dash_html_components as html
        from crystal_toolkit.helpers.layouts import Column
        import crystal_toolkit.components as ctc
        import dash

        edge_states = []
        for d in args.dirs:
            edge_states.append(
                BandEdgeStates.from_yaml(d / "band_edge_states.yaml"))

        app = dash.Dash(__name__,
                        suppress_callback_exceptions=True,
                        assets_folder=SETTINGS.ASSETS_PATH,
                        external_stylesheets=[
                            'https://codepen.io/chriddyp/pen/bWLwgP.css'
                        ])

        cpd_plot_info = CpdPlotInfo(chem_pot_diag)
        cpd_e_component = CpdEnergyComponent(cpd_plot_info, pcr, defects,
                                             defect_entries, corrections,
                                             args.unitcell.vbm,
                                             args.unitcell.cbm, edge_states)
        my_layout = html.Div([Column(cpd_e_component.layout)])
        ctc.register_crystal_toolkit(app=app, layout=my_layout, cache=None)
        app.run_server(port=args.port)
        return

    abs_chem_pot = chem_pot_diag.abs_chem_pot_dict(args.label)
    title = " ".join([latexify(formula), "point", args.label])
    defect_energies = make_energies(pcr, defects, defect_entries, corrections,
                                    abs_chem_pot)

    if args.print:
        defect_energies = slide_energy(defect_energies, args.unitcell.vbm)
        print("         charge          E_f   correction    ")
        for e in defect_energies:
            print(e)
            print("")

        print("-- cross points -- ")
        for e in defect_energies:
            print(e.name)
            print(
                e.cross_points(ef_min=args.unitcell.vbm,
                               ef_max=args.unitcell.cbm,
                               base_ef=args.unitcell.vbm))
            print("")
        return

    plotter = DefectEnergyMplPlotter(title=title,
                                     defect_energies=defect_energies,
                                     vbm=args.unitcell.vbm,
                                     cbm=args.unitcell.cbm,
                                     supercell_vbm=pcr.vbm,
                                     supercell_cbm=pcr.cbm,
                                     y_range=args.y_range,
                                     supercell_edge=args.supercell_edge,
                                     label_line=args.label_line,
                                     add_charges=args.add_charges)

    plotter.construct_plot()
    plotter.plt.savefig(f"energy_{args.label}.pdf")