Skip to content

Commit c5391d0

Browse files
authored
Merge pull request #3 from naik-aakash/add_lobster_integration
Add CohpAndDosComponen
2 parents a97a0c6 + d26ea7e commit c5391d0

File tree

5 files changed

+902
-4
lines changed

5 files changed

+902
-4
lines changed

.github/workflows/pull-request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
python${{ matrix.python-version }} -m pip install -r requirements/ubuntu-latest_py${{ matrix.python-version }}_extras.txt
4040
python${{ matrix.python-version }} -m pip install --upgrade pip
4141
python${{ matrix.python-version }} -m pip install --no-deps .[server,test]
42+
python${{ matrix.python-version }} -m pip install lobsterpy
4243
- name: Test modules
4344
run: python${{ matrix.python-version }} -m pytest --cov=crystal_toolkit --cov-report=xml tests
4445
- name: Test example apps
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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)

crystal_toolkit/components/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
BandstructureAndDosComponent,
55
BandstructureAndDosPanelComponent,
66
)
7+
8+
# from crystal_toolkit.components.transformations.rattle import (
9+
# MonteCarloRattleTransformationComponent,
10+
# )
11+
from crystal_toolkit.components.cohp import CohpAndDosComponent
712
from crystal_toolkit.components.diffraction import XRayDiffractionComponent
813
from crystal_toolkit.components.diffraction_tem import TEMDiffractionComponent
914
from crystal_toolkit.components.fermi_surface import FermiSurfaceComponent
@@ -33,10 +38,6 @@
3338
from crystal_toolkit.components.transformations.grainboundary import (
3439
GrainBoundaryTransformationComponent,
3540
)
36-
37-
# from crystal_toolkit.components.transformations.rattle import (
38-
# MonteCarloRattleTransformationComponent,
39-
# )
4041
from crystal_toolkit.components.transformations.slab import SlabTransformationComponent
4142
from crystal_toolkit.components.transformations.substitution import (
4243
SubstitutionTransformationComponent,

0 commit comments

Comments
 (0)