Skip to content

Instantly share code, notes, and snippets.

@Asif-Iqbal-Bhatti
Last active January 19, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asif-Iqbal-Bhatti/c0bead508582bbddb3665278bd09179b to your computer and use it in GitHub Desktop.
Save Asif-Iqbal-Bhatti/c0bead508582bbddb3665278bd09179b to your computer and use it in GitHub Desktop.
This script creates a number of directories based on POSCARS-* generated from phonopy code
#!/usr/bin/env python3.6
#_____________________________________________________________________________
'''
USAGE: python3 script to create directories for POSCAR files generated from
PHONOPY code.
Credit: Asif Iqbal Bhatti
DATED: 19-01-2020
'''
#_____________________________________________________________________________
import os, sys, scipy
import pathlib, subprocess, shutil
from os import listdir
from os.path import isfile, join
import os.path
import multiprocessing as mp
import fnmatch
import time
#_____________________________________________________________________________
#os.system("phonopy -d --dim="2 2 2") ## It will call a phonopy code
os.system('rm -r disp-*')
#_____________________________________________________________________________
def copy_fntn(k):
for i in range(1, int(k)+1):
shutil.rmtree('disp-'+str(i).zfill(3), ignore_errors=True) #overwrite a directory
os.mkdir('disp-'+str(i).zfill(3))
#subprocess.check_call(['mkdir', 'disp-'+str(i).zfill(3)])
#subprocess.check_call('cd disp-'+str(i).zfill(3), shell=True)
# print(str(i).zfill(3))
#
# The zfill()/rjust() is a function associated with the string object.
# 3 is the expected length of the string after padding.
#
os.system('cp INCAR disp-'+str(i).zfill(3) )
os.system('cp POTCAR disp-'+str(i).zfill(3) )
os.system('cp KPOINTS disp-'+str(i).zfill(3) )
os.system('cp job.sh disp-'+str(i).zfill(3) )
subprocess.call(['cp','-r','POSCAR-'+str(i).zfill(3),'disp-'+str(i).zfill(3)], shell = False)
os.chdir('disp-'+str(i).zfill(3))
shutil.copyfile("POSCAR-"+str(i).zfill(3), "POSCAR-"+str(i).zfill(3)+"-disp")
#os.system('ls')
os.chdir('../')
if __name__ == '__main__':
print("Number of processors Detected: ", mp.cpu_count())
pool = mp.Pool(4)
print("*"*80)
if not os.path.exists('POSCAR'):
print (' ERROR: POSCAR does not exist here.')
sys.exit(0)
else:
counter=0
mypath = os.getcwd()
for entry in os.listdir(mypath):
if os.path.isfile(os.path.join(mypath, entry)):
if fnmatch.fnmatchcase(entry,'POSCAR-*'):
counter+=1
print ("# of POSCAR-* files generated with Phonopy code: ----> {}".format(counter) )
print("*"*80)
start_time = time.time()
#copy_fntn(counter)
#p = mp.Process(target=copy_fntn, args=('9',))
#p.start()
#p.join()
with pool as p:
p.map(copy_fntn, [counter])
end_time = time.time()
print("total time taken this loop: ", end_time - start_time)
@Asif-Iqbal-Bhatti
Copy link
Author

Please check for your self. I have tried to implement the multiprocessing on the most demanding for loop part but I guess the speed is same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment