Skip to content

Commit

Permalink
update get_dos() with custom dos type
Browse files Browse the repository at this point in the history
  • Loading branch information
robinzyb committed Apr 15, 2024
1 parent 166f7fd commit f51643f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
87 changes: 68 additions & 19 deletions cp2kdata/pdos/pdos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glob
import os
from typing import Tuple

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -190,14 +191,48 @@ def __init__(self, file_name, parse_file_name=True):
self.project_name, self.spin, self.listidx, self.kind, self.timestep = pdos_name_parser(
self.file)

def get_dos(self,
sigma: float=1,
dos_type: str="total",
usecols: Tuple[int]=None):
"""
Get the density of states (DOS) from the CP2K DOS file.
Parameters:
- sigma (float): The standard deviation for Gaussian smoothing of the DOS.
- dos_type (str): The type of DOS to retrieve. Can be "total", "s", "p", "d", or "f".
- usecols (Tuple[int]): The columns to use for custom DOS. Only applicable if dos_type is "custom".
Returns:
- smth_dos (np.ndarray): The smoothed DOS.
- ener (np.ndarray): The energy values corresponding to the DOS.
"""
# smooth the dos data
dos, ener = self.get_raw_dos(dos_type=dos_type, usecols=usecols)
smth_dos = gaussian_filter1d(dos, sigma)
self.smth_dos = smth_dos

Check warning on line 213 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L211-L213

Added lines #L211 - L213 were not covered by tests

return smth_dos, ener

Check warning on line 215 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L215

Added line #L215 was not covered by tests

def read_dos_element(self):
"""
Reads the element from the first line of the file.
Returns:
str: The element extracted from the first line of the file.
"""
with open(self.file) as f:
first_line = f.readline()
element = first_line.split()[6]
# element = ''.join([i for i in self.kind if not i.isdigit()])
return element

def read_dos_fermi(self):
"""
Reads the Fermi energy from the file.
Returns:
float: The Fermi energy in eV.
"""
# this is fermi energy not fermi level!
# fermi energy is same as HOMO energy
with open(self.file) as f:
Expand All @@ -208,9 +243,15 @@ def read_dos_fermi(self):
return fermi

def read_dos_energies(self):
energies = np.loadtxt(self.file, usecols=1)
energies = energies * au2eV
return energies
"""
Reads the DOS energies from the file and converts them to electron volts (eV).
Returns:
numpy.ndarray: An array of DOS energies in eV.
"""
energies = np.loadtxt(self.file, usecols=1)
energies = energies * au2eV
return energies

Check warning on line 254 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L252-L254

Added lines #L252 - L254 were not covered by tests

@property
def occupation(self):
Expand All @@ -219,23 +260,37 @@ def occupation(self):
return occupation

def get_homo_ener(self):
homo_idx = np.where(self.occupation == 0)[0][0]-1
homo_ener = self.energies[homo_idx]
"""
Get the energy of the highest occupied molecular orbital (HOMO).
Returns:
float: The energy of the HOMO.
"""
homo_idx = np.where(self.occupation == 0)[0][0]-1
homo_ener = self.energies[homo_idx]

Check warning on line 270 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L269-L270

Added lines #L269 - L270 were not covered by tests

return homo_ener
return homo_ener

Check warning on line 272 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L272

Added line #L272 was not covered by tests

def get_lumo_ener(self):
lumo_ener = self.energies[self.occupation == 0][0]
return lumo_ener
"""
Get the energy of the lowest unoccupied molecular orbital (LUMO).
def get_raw_dos(self, dos_type="total", steplen=0.1):
Returns:
lumo_ener (float): The energy of the LUMO.
"""
lumo_ener = self.energies[self.occupation == 0][0]
return lumo_ener

Check warning on line 282 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L281-L282

Added lines #L281 - L282 were not covered by tests

def get_raw_dos(self, dos_type="total", steplen=0.1, usecols=None):

file = self.file
energies = self.energies
fermi = self.fermi
steplen = 0.1

if dos_type == "total":
#steplen = 0.1
if (dos_type == "custom") and (usecols is not None):
weights = np.loadtxt(file, usecols=usecols).sum(axis=1)
print("use customed columns")
elif dos_type == "total":

Check warning on line 293 in cp2kdata/pdos/pdos.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/pdos/pdos.py#L290-L293

Added lines #L290 - L293 were not covered by tests
tmp_len = len(np.loadtxt(file, usecols=2))
weights = np.ones(tmp_len)
elif dos_type == "s":
Expand All @@ -259,13 +314,7 @@ def get_raw_dos(self, dos_type="total", steplen=0.1):
self.ener = ener
return dos, ener

def get_dos(self, sigma=1, dos_type="total"):
# smooth the dos data
dos, ener = self.get_raw_dos(dos_type=dos_type)
smth_dos = gaussian_filter1d(dos, sigma)
self.smth_dos = smth_dos

return smth_dos, ener


PDOS_NAME_RE = re.compile(
Expand Down
12 changes: 11 additions & 1 deletion docs/pdos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
from cp2kdata import Cp2kPdos
dosfile = "Universality-ALPHA_k2-1_50.pdos"
mypdos = Cp2kPdos(dosfile)
dos, ener = mypdos.get_dos()
dos, ener = mypdos.get_dos(sigma=1, dos_type="total")
```

If DOS is parsed from LDOS files, `dos_type=custom` is preferred, which can be used together with `usecols`. `usecols` accepts tuple with `int` elements. For example, `usecols=(3, 4, 5)`

```python
dosfile = "water-list1-1_0.pdos
mypdos = Cp2kPdos(dosfile)
dos, ener = mypdos.get_dos(dos_type='custom', usecols=(3, 4, 5))
```



## Quickplot of PDOS Files in Single Point Energy Calculation

```python
Expand Down

0 comments on commit f51643f

Please sign in to comment.