|
| 1 | +import os |
| 2 | +import warnings |
| 3 | + |
| 4 | +import dash |
| 5 | +import numpy as np |
| 6 | +from pymatgen.core import Structure |
| 7 | +from pymatgen.electronic_structure.cohp import CompleteCohp |
| 8 | +from pymatgen.io.lobster.inputs import Lobsterin |
| 9 | +from pymatgen.io.lobster.outputs import ( |
| 10 | + Bandoverlaps, |
| 11 | + Charge, |
| 12 | + Doscar, |
| 13 | + Icohplist, |
| 14 | + Lobsterout, |
| 15 | + MadelungEnergies, |
| 16 | +) |
| 17 | +from pymatgen.io.vasp.outputs import Vasprun |
| 18 | + |
| 19 | +import crystal_toolkit.components as ctc |
| 20 | +from crystal_toolkit.helpers.layouts import H3, Container |
| 21 | +from crystal_toolkit.settings import SETTINGS |
| 22 | + |
| 23 | + |
| 24 | +class CustomVasprun(Vasprun): |
| 25 | + """Override final_energy property without unitized decorator""" |
| 26 | + |
| 27 | + def __init__(self, filename, **kwargs): |
| 28 | + super().__init__(filename, **kwargs) |
| 29 | + |
| 30 | + @property |
| 31 | + def final_energy(self) -> float: |
| 32 | + """Final energy from the VASP run.""" |
| 33 | + |
| 34 | + try: |
| 35 | + final_istep = self.ionic_steps[-1] |
| 36 | + total_energy = final_istep["e_0_energy"] |
| 37 | + |
| 38 | + # Fix a bug in vasprun.xml. |
| 39 | + # See https://www.vasp.at/forum/viewtopic.php?f=3&t=16942 |
| 40 | + final_estep = final_istep["electronic_steps"][-1] |
| 41 | + electronic_energy_diff = ( |
| 42 | + final_estep["e_0_energy"] - final_estep["e_fr_energy"] |
| 43 | + ) |
| 44 | + total_energy_bugfix = np.round( |
| 45 | + electronic_energy_diff + final_istep["e_fr_energy"], 8 |
| 46 | + ) |
| 47 | + if np.abs(total_energy - total_energy_bugfix) > 1e-7: |
| 48 | + return total_energy_bugfix |
| 49 | + |
| 50 | + return total_energy |
| 51 | + |
| 52 | + except (IndexError, KeyError): |
| 53 | + warnings.warn( |
| 54 | + "Calculation does not have a total energy. Possibly a GW or similar kind of run. Infinity is returned.", |
| 55 | + stacklevel=2, |
| 56 | + ) |
| 57 | + return float("inf") |
| 58 | + |
| 59 | + |
| 60 | +calc_dir = "path/to/your/lobster/output" # Replace with your actual path |
| 61 | + |
| 62 | +icohplist_obj = Icohplist( |
| 63 | + filename=f"{calc_dir}/ICOHPLIST.lobster.gz", are_cobis=False, are_coops=False |
| 64 | +) |
| 65 | + |
| 66 | +completecohp_obj = CompleteCohp.from_file( |
| 67 | + filename=f"{calc_dir}/COHPCAR.lobster.gz", |
| 68 | + structure_file=f"{calc_dir}/CONTCAR.gz", |
| 69 | + fmt="LOBSTER", |
| 70 | + are_cobis=False, |
| 71 | + are_coops=False, |
| 72 | +) |
| 73 | + |
| 74 | +charge_obj = Charge(filename=f"{calc_dir}/CHARGE.lobster.gz") |
| 75 | +madelung_obj = MadelungEnergies(filename=f"{calc_dir}/MadelungEnergies.lobster.gz") |
| 76 | +lob_dos = Doscar( |
| 77 | + doscar=f"{calc_dir}/DOSCAR.LSO.lobster.gz", structure_file=f"{calc_dir}/CONTCAR.gz" |
| 78 | +) |
| 79 | + |
| 80 | +vasprun_obj = CustomVasprun(filename=f"{calc_dir}/vasprun.xml.gz") |
| 81 | +structure_obj = Structure.from_file(f"{calc_dir}/CONTCAR.gz") |
| 82 | +lobsterin_obj = Lobsterin.from_file(f"{calc_dir}/lobsterin.gz") |
| 83 | +lobsterout_obj = Lobsterout(filename=f"{calc_dir}/lobsterout.gz") |
| 84 | +# Include band overlaps file if it exists available |
| 85 | +bandoverlaps_obj = ( |
| 86 | + Bandoverlaps(filename=f"{calc_dir}/bandOverlaps.lobster.gz") |
| 87 | + if os.path.exists(f"{calc_dir}/bandOverlaps.lobster.gz") |
| 88 | + else None |
| 89 | +) |
| 90 | + |
| 91 | +cohp_component = ctc.CohpAndDosComponent( |
| 92 | + density_of_states=lob_dos.completedos, |
| 93 | + charge_obj=charge_obj, |
| 94 | + icohplist_obj=icohplist_obj, |
| 95 | + completecohp_obj=completecohp_obj, |
| 96 | + madelung_obj=madelung_obj, |
| 97 | + vasprun_obj=vasprun_obj, |
| 98 | + structure_obj=structure_obj, |
| 99 | + lobsterin_obj=lobsterin_obj, |
| 100 | + lobsterout_obj=lobsterout_obj, |
| 101 | + bandoverlaps_obj=bandoverlaps_obj, |
| 102 | + mpid="mp-xxx", |
| 103 | + disable_callbacks=False, |
| 104 | +) |
| 105 | + |
| 106 | +# example layout to demonstrate capabilities of component |
| 107 | +layout = Container([H3("COHP and Density of States Example"), cohp_component.layout()]) |
| 108 | + |
| 109 | +app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH, prevent_initial_callbacks=True) # |
| 110 | + |
| 111 | +ctc.register_crystal_toolkit(app, layout=layout) |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + app.run(debug=True, port=8051) |
0 commit comments