#!/usr/bin/env python
#"""Writen by Niraj K. Nepal, Ph.D."""
"""Module to plot atom-projected phonon band dispersion"""
import os
import sys
import numpy as np
from ase.io import espresso
np.set_printoptions(threshold=100000,linewidth=1000,suppress=True, formatter={'float_kind':'{:0.2f}'.format})
MSG="""
Format of phonproj.in file
_____________________________________________
plot.eig #Eigenval file
2 111 #number of atom type and number of kpoints.
*
Mg 0 # first ion and its position in input file
O 1 # second ion and its position in input file
*
_____________________________________________
"""
[docs]
def write_phonproj_in(comp,nkpoint,filename,outfile='phonproj.in'):
"""
Function to write the 'phonproj.in' file required for atomic projection.
This function generates the 'phonproj.in' file based on the provided compound name,
the number of k-points, and the QE input scf.in file.
Parameters:
-----------
comp : str
Compound name.
nkpoint : int
Number of k-points.
filename : str
Path to the QE input scf.in file.
outfile : str, optional
Output file name for 'phonproj.in'. Default is 'phonproj.in'.
Returns:
--------
None
"""
struc = espresso.read_espresso_in(filename)
symbol_dict = struc.symbols.indices()
with open(outfile, "w") as write_phonproj:
write_phonproj.write("{}.eig".format(comp)+ "\n")
nat = len(symbol_dict.keys())
write_phonproj.write("{} {}".format(nat,nkpoint) + "\n")
write_phonproj.write("*\n")
for _,elm in enumerate(symbol_dict.keys()):
value = symbol_dict[elm]
write_phonproj.write(elm + " ")
for j,ind in enumerate(value):
if j < len(value) - 1:
write_phonproj.write(str(ind) + " ")
else:
write_phonproj.write(str(ind) + "\n")
write_phonproj.write("*\n")
[docs]
def projection(pol_q_mode,ion,indices):
"""
Function to calculate atomic projection for mode nu and wavevector q.
This function computes the atomic projection for a given mode nu and wavevector q.
Parameters:
-----------
pol_q_mode : numpy.ndarray
Array obtained from parse_input function containing eigenvectors.
ion : str
Type of atom.
indices : list
Indices of an ion in the scf.in input file.
Returns:
--------
proj_array : numpy.ndarray
Array of projections of size (nq, 3*nat).
"""
nqpt = pol_q_mode.shape[0]
nmode = pol_q_mode.shape[1]
proj_array = np.zeros((nqpt,nmode))
with open("phonon-{}.proj".format(ion), "w") as phon_proj:
for iqpt in range(nqpt):
for imode in range(nmode):
eig=pol_q_mode[iqpt,imode]
if eig.shape[0] > 0:
eigval=eig[indices]
proj = round(np.real(np.sum(np.conj(eigval) * eigval)),4)
else:
proj = 0.
proj_array[iqpt,imode] = proj
phon_proj.write("{} {} {}".format(iqpt, imode, proj) + "\n")
proj_array = proj_array.reshape(nqpt,nmode)
return proj_array
[docs]
def print_to_file():
"""
Function to save mode resolved projection in phonon-compound.prog.gp format.
This function reads the input file 'phonproj.in' to extract necessary information about the phonon modes and ions.
It then calculates the mode-resolved projections for each ion and writes them to a file in the format
'phonon-compound.proj.gp'.
Returns:
--------
ions_list : list
List of ion names for which projections are calculated and saved.
"""
filename, nat, nqpoint, ions = reading_input("phonproj.in")
_,_,modes = parse_input(filename,nat,nqpoint)
with open("phonon-{}.proj.gp".format(filename.split(".")[0]), "w") as phon_proj:
for _,ion in enumerate(ions.keys()):
proj = projection(modes,ion,ions[ion])
phon_proj.write(str(proj).replace(' [', '').replace('[', '').replace(']', '').replace(' ]','')+"\n")
phon_proj.write("\n")
return list(ions.keys())
[docs]
def main():
"""
Main function to generate phonon projections and print ion-color associations.
This function serves as the main entry point for generating phonon projections and printing
ion-color associations based on the projections.
If 'phonproj.in' file is not found, it creates one.
It then generates phonon projections for each ion
and saves them to a file. Finally, it prints the association between ions and colors.
"""
if not os.path.isfile("phonproj.in"):
print("phonproj.in not found, creating one\n")
compound = sys.argv[1]
nkpoint = sys.argv[2]
write_phonproj_in(compound,nkpoint,'scf.in')
ions=print_to_file()
color_list = ['red','blue','green','cyan','k']
for i,_ in enumerate(ions):
print("ION: {}, PROJECTED COLOR: {}".format(ions[i],color_list[i]))
print("-------------------------------------------\n")
if __name__ == "__main__":
main()