Skip to content

Commit

Permalink
replace unnecessary single-item list extend with append
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Jun 13, 2024
1 parent 4c0bb04 commit 8e25238
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions pymatgen/io/lobster/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,13 @@ def __init__(
else:
n_bonds = len(data_without_orbitals)

labels, atoms1, atoms2, lens, translations, nums, icohps = [], [], [], [], [], [], []
labels: list[str] = []
atoms1: list[str] = []
atoms2: list[str] = []
lens: list[float] = []
translations: list[tuple[int, int, int]] = []
nums: list[int] = []
icohps: list[dict[Spin, float]] = []

for bond in range(n_bonds):
line_parts = data_without_orbitals[bond].split()
Expand All @@ -437,7 +443,7 @@ def __init__(
atom2 = str(line_parts[2])
length = float(line_parts[3])

icohp = {}
icohp: dict[Spin, float] = {}
if version == "2.2.1":
icohp[Spin.up] = float(line_parts[4])
num = int(line_parts[5])
Expand All @@ -453,13 +459,13 @@ def __init__(
if self.is_spin_polarized:
icohp[Spin.down] = float(data_without_orbitals[bond + n_bonds + 1].split()[7])

labels += [label]
atoms1 += [atom1]
atoms2 += [atom2]
lens += [length]
translations += [translation]
nums += [num]
icohps += [icohp]
labels.append(label)
atoms1.append(atom1)
atoms2.append(atom2)
lens.append(length)
translations.append(translation)
nums.append(num)
icohps.append(icohp)

list_orb_icohp: list[dict] | None = None
if self.orbitalwise:
Expand All @@ -479,7 +485,7 @@ def __init__(
icohp[Spin.down] = float(data_orbitals[n_orbs + i_data_orb].split()[7])

if len(list_orb_icohp) < int(label):
list_orb_icohp += [{orb_label: {"icohp": icohp, "orbitals": orbitals}}]
list_orb_icohp.append({orb_label: {"icohp": icohp, "orbitals": orbitals}})
else:
list_orb_icohp[int(label) - 1][orb_label] = {"icohp": icohp, "orbitals": orbitals}

Expand Down Expand Up @@ -561,7 +567,7 @@ def __init__(self, filename: str | None = "NcICOBILIST.lobster"): # LOBSTER < 4
data_without_orbitals = []
for line in data:
if "_" not in str(line.split()[3:]) and "s]" not in str(line.split()[3:]):
data_without_orbitals += [line]
data_without_orbitals.append(line)
else:
data_without_orbitals = data

Expand Down Expand Up @@ -592,11 +598,11 @@ def __init__(self, filename: str | None = "NcICOBILIST.lobster"): # LOBSTER < 4
if self.is_spin_polarized:
ncicobi[Spin.down] = float(data_without_orbitals[bond + n_bonds + 1].split()[2])

self.list_labels += [label]
self.list_n_atoms += [n_atoms]
self.list_ncicobi += [ncicobi]
self.list_interaction_type += [interaction_type]
self.list_num += [num]
self.list_labels.append(label)
self.list_n_atoms.append(n_atoms)
self.list_ncicobi.append(ncicobi)
self.list_interaction_type.append(interaction_type)
self.list_num.append(num)

# TODO: add functions to get orbital resolved NcICOBIs

Expand Down Expand Up @@ -677,7 +683,7 @@ def _parse_doscar(self):
for nd in range(1, ndos):
line = file.readline().split()
cdos[nd] = np.array(line)
dos += [cdos]
dos.append(cdos)
doshere = np.array(dos[0])
if len(doshere[0, :]) == 5:
self._is_spin_polarized = True
Expand All @@ -699,7 +705,7 @@ def _parse_doscar(self):
for orb_num, j in enumerate(range(1, ncol)):
orb = orbitals[atom + 1][orb_num]
pdos[orb][spin] = data[:, j]
pdoss += [pdos]
pdoss.append(pdos)
else:
tdensities[Spin.up] = doshere[:, 1]
tdensities[Spin.down] = doshere[:, 2]
Expand All @@ -717,7 +723,7 @@ def _parse_doscar(self):
pdos[orb][spin] = data[:, j]
if j % 2 == 0:
orb_num += 1
pdoss += [pdos]
pdoss.append(pdos)

self._efermi = efermi
self._pdos = pdoss
Expand Down Expand Up @@ -814,8 +820,8 @@ def __init__(
line = data[atom].split()
self.atomlist += [line[1] + line[0]]
self.types += [line[1]]
self.mulliken += [float(line[2])]
self.loewdin += [float(line[3])]
self.mulliken.append(float(line[2]))
self.loewdin.append(float(line[3]))

def get_structure_with_charges(self, structure_filename):
"""Get a Structure with Mulliken and Loewdin charges as site properties
Expand Down Expand Up @@ -1087,9 +1093,9 @@ def _get_spillings(data, number_of_spins):
splitrow = row.split()
if len(splitrow) > 2 and splitrow[2] == "spilling:":
if splitrow[1] == "charge":
charge_spilling += [np.float64(splitrow[3].replace("%", "")) / 100.0]
charge_spilling.append(np.float64(splitrow[3].replace("%", "")) / 100.0)
if splitrow[1] == "total":
total_spilling += [np.float64(splitrow[3].replace("%", "")) / 100.0]
total_spilling.append(np.float64(splitrow[3].replace("%", "")) / 100.0)

if len(charge_spilling) == number_of_spins and len(total_spilling) == number_of_spins:
break
Expand Down Expand Up @@ -1156,7 +1162,7 @@ def _get_warning_orthonormalization(data):
for row in data:
splitrow = row.split()
if "orthonormalized" in splitrow:
orthowarning += [" ".join(splitrow[1:])]
orthowarning.append(" ".join(splitrow[1:]))
return orthowarning

@staticmethod
Expand All @@ -1165,7 +1171,7 @@ def _get_all_warning_lines(data):
for row in data:
splitrow = row.split()
if len(splitrow) > 0 and splitrow[0] == "WARNING:":
ws += [" ".join(splitrow[1:])]
ws.append(" ".join(splitrow[1:]))
return ws

@staticmethod
Expand All @@ -1174,7 +1180,7 @@ def _get_all_info_lines(data):
for row in data:
splitrow = row.split()
if len(splitrow) > 0 and splitrow[0] == "INFO:":
infos += [" ".join(splitrow[1:])]
infos.append(" ".join(splitrow[1:]))
return infos


Expand Down Expand Up @@ -1257,7 +1263,7 @@ def __init__(
filenames = "."
for name in os.listdir(filenames):
if fnmatch.fnmatch(name, "FATBAND_*.lobster"):
filenames_new += [os.path.join(filenames, name)]
filenames_new.append(os.path.join(filenames, name))
filenames = filenames_new
if len(filenames) == 0:
raise ValueError("No FATBAND files in folder or given")
Expand Down Expand Up @@ -1310,7 +1316,7 @@ def __init__(
linenumbers = []
for iline, line in enumerate(contents[1 : self.nbands * 2 + 4]):
if line.split()[0] == "#":
linenumbers += [iline]
linenumbers.append(iline)

if ifilename == 0:
self.is_spinpolarized = len(linenumbers) == 2
Expand Down Expand Up @@ -1360,7 +1366,7 @@ def __init__(
]
)
if ifilename == 0:
kpoints_array += [KPOINT]
kpoints_array.append(KPOINT)

linenumber = 0
iband = 0
Expand Down Expand Up @@ -1471,7 +1477,7 @@ def _read(self, contents: list, spin_numbers: list):
kpoint_array = []
for kpointel in kpoint:
if kpointel not in {"at", "k-point", ""}:
kpoint_array += [float(kpointel)]
kpoint_array.append(float(kpointel))

elif "maxDeviation" in line:
if spin not in self.band_overlaps_dict:
Expand All @@ -1483,16 +1489,16 @@ def _read(self, contents: list, spin_numbers: list):
if "matrices" not in self.band_overlaps_dict[spin]:
self.band_overlaps_dict[spin]["matrices"] = []
maxdev = line.split(" ")[2]
self.band_overlaps_dict[spin]["max_deviations"] += [float(maxdev)]
self.band_overlaps_dict[spin]["max_deviations"].append(float(maxdev))
self.band_overlaps_dict[spin]["k_points"] += [kpoint_array]
self.max_deviation += [float(maxdev)]
self.max_deviation.append(float(maxdev))
overlaps = []

else:
rows = []
for el in line.split(" "):
if el != "":
rows += [float(el)]
rows.append(float(el))
overlaps += [rows]
if len(overlaps) == len(rows):
self.band_overlaps_dict[spin]["matrices"] += [np.matrix(overlaps)]
Expand Down Expand Up @@ -1603,7 +1609,7 @@ def __init__(self, filename: str = "GROSSPOP.lobster", list_dict_grosspop: list[
small_dict["Mulliken GP"][cleanline[0]] = float(cleanline[1])
small_dict["Loewdin GP"][cleanline[0]] = float(cleanline[2])
if "total" in cleanline[0]:
self.list_dict_grosspop += [small_dict]
self.list_dict_grosspop.append(small_dict)

def get_structure_with_total_grosspop(self, structure_filename: str) -> Structure:
"""Get a Structure with Mulliken and Loewdin total grosspopulations as site properties
Expand Down Expand Up @@ -1664,9 +1670,9 @@ def _parse_file(filename):
splitline = line.split()
if len(splitline) >= 6:
points += [[float(splitline[0]), float(splitline[1]), float(splitline[2])]]
distance += [float(splitline[3])]
real += [float(splitline[4])]
imaginary += [float(splitline[5])]
distance.append(float(splitline[3]))
real.append(float(splitline[4]))
imaginary.append(float(splitline[5]))

if len(real) != grid[0] * grid[1] * grid[2] or len(imaginary) != grid[0] * grid[1] * grid[2]:
raise ValueError("Something went wrong while reading the file")
Expand Down Expand Up @@ -1712,9 +1718,9 @@ def set_volumetric_data(self, grid, structure):
"coordinates 0.0 0.0 0.0 coordinates 1.0 1.0 1.0 box bandlist 1 "
)

new_x += [x_here]
new_y += [y_here]
new_z += [z_here]
new_x.append(x_here)
new_y.append(y_here)
new_z.append(z_here)

new_real += [self.real[runner]]
new_imaginary += [self.imaginary[runner]]
Expand Down Expand Up @@ -1909,8 +1915,8 @@ def __init__(
line = data[atom].split()
self.atomlist += [line[1] + str(line[0])]
self.types += [line[1]]
self.sitepotentials_mulliken += [float(line[2])]
self.sitepotentials_loewdin += [float(line[3])]
self.sitepotentials_mulliken.append(float(line[2]))
self.sitepotentials_loewdin.append(float(line[3]))

self.madelungenergies_mulliken = float(data[self.num_atoms + 1].split()[3])
self.madelungenergies_loewdin = float(data[self.num_atoms + 1].split()[4])
Expand Down Expand Up @@ -2098,21 +2104,21 @@ def _parse_matrix(file_data, pattern, e_fermi):
for idx, line in enumerate(file_data):
line = line.strip()
if "Real parts" in line:
start_inxs_real += [idx + 1]
start_inxs_real.append(idx + 1)
if idx == 1: # ignore the first occurrence as files start with real matrices
pass
else:
end_inxs_imag += [idx - 1]
end_inxs_imag.append(idx - 10)
matches = re.search(pattern, file_data[idx - 1])
if matches and len(matches.groups()) == 2:
k_point = matches.group(2)
complex_matrices[k_point] = {}
if "Imag parts" in line:
end_inxs_real += [idx - 1]
start_inxs_imag += [idx + 1]
end_inxs_real.append(idx - 1)
start_inxs_imag.append(idx + 1)
# explicitly add the last line as files end with imaginary matrix
if idx == len(file_data) - 1:
end_inxs_imag += [len(file_data)]
end_inxs_imag.append(len(file_data))

# extract matrix data and store diagonal elements
matrix_real = []
Expand All @@ -2133,13 +2139,13 @@ def _parse_matrix(file_data, pattern, e_fermi):

matches = re.search(pattern, file_data[start_inx_real - 2])
if matches and len(matches.groups()) == 2:
spin = Spin.up if matches.group(1) == "1" else Spin.down
k_point = matches.group(2)
spin = Spin.up if matches[1] == "1" else Spin.down
k_point = matches[2]
complex_matrices[k_point].update({spin: comp_matrix})
elif matches and len(matches.groups()) == 1:
k_point = matches.group(1)
k_point = matches[1]
complex_matrices.update({k_point: comp_matrix})
matrix_diagonal_values += [comp_matrix.real.diagonal() - e_fermi]
matrix_diagonal_values.append(comp_matrix.real.diagonal() - e_fermi)

# extract elements basis functions as list
elements_basis_functions = [
Expand Down

0 comments on commit 8e25238

Please sign in to comment.