diff --git a/getexcited/getexcited_package/newsim.py b/getexcited/getexcited_package/newsim.py index a942801..ad8e17c 100644 --- a/getexcited/getexcited_package/newsim.py +++ b/getexcited/getexcited_package/newsim.py @@ -1,6 +1,6 @@ -#/usr/bin/python +# /usr/bin/python -''' +""" This function prepares input files for an adiabatic simulation, where geometries are taken from a separate NEXMD simulation located in @@ -22,225 +22,271 @@ NOTE: All NEXMD folders and rseedslists, inside directory B, will be deleted if this function is completely executed! -''' +""" -import numpy as np -import random -import os -import sys +import fileinput import glob +import os +import random import shutil -import fileinput +import sys + +import numpy as np cwd = os.getcwd() -def newsim(header): - print('Preparing input files for a new simulation with geometries taken from another simulation.') +def newsim(header): + print( + "Preparing input files for a new simulation with geometries taken from another" + " simulation." + ) ## Directory names ## - gsdir = input('Ground-state dynamics directory: ') + gsdir = input("Ground-state dynamics directory: ") if not os.path.exists(gsdir): - print('Path %s does not exist.' % (gsdir)) + print("Path %s does not exist." % (gsdir)) sys.exit() - olddir = input('NEXMD directory where geometries should be taken from: ') + olddir = input("NEXMD directory where geometries should be taken from: ") if not os.path.exists(olddir): - print('Path %s does not exist.' % (olddir)) + print("Path %s does not exist." % (olddir)) sys.exit() - newdir = input('NEXMD directory for new simulation: ') + newdir = input("NEXMD directory for new simulation: ") if not os.path.exists(newdir): - print('Path %s does not exist.' % (newdir)) + print("Path %s does not exist." % (newdir)) sys.exit() ## Delete previous NEXMD folders and rseedslist ## - NEXMDs = glob.glob('%s/NEXMD*/' % (newdir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (newdir)) NEXMDs.sort() if len(NEXMDs) != 0: - contq = eval(input('** WARNING ** All NEXMD folders and rseedslist inside %s will be deleted!\nContinue? Answer yes [1] or no [0]: ' % (newdir))) - if contq not in [1,0]: - print('Answer must be 1 or 0.') + contq = eval( + input( + "** WARNING ** All NEXMD folders and rseedslist inside %s will be" + " deleted!\nContinue? Answer yes [1] or no [0]: " % (newdir) + ) + ) + if contq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if contq == 0: sys.exit() for NEXMD in NEXMDs: - print('Deleting %s.' % (NEXMD)) + print("Deleting %s." % (NEXMD)) shutil.rmtree(NEXMD) - rseedslist = glob.glob('%s/rseedslist*' % (newdir)) + rseedslist = glob.glob("%s/rseedslist*" % (newdir)) for rseeds in rseedslist: os.remove(rseeds) - print('Deleting %s.' % (rseeds)) + print("Deleting %s." % (rseeds)) ## Information from old header (completed NEXMD geometries) ## - if not os.path.exists('%s/header' % (olddir)): - print('Path %s/header does not exist.' % (olddir)) + if not os.path.exists("%s/header" % (olddir)): + print("Path %s/header does not exist." % (olddir)) sys.exit() - old_header = header('%s/header' % (olddir)) + old_header = header("%s/header" % (olddir)) ## Check output data ## if old_header.out_data_steps == 0: - print('No data have been printed to files because out_data_steps = 0 in %s/header.' % (olddir)) + print( + "No data have been printed to files because out_data_steps = 0 in" + " %s/header." % (olddir) + ) sys.exit() if old_header.out_coords_steps == 0: - print('No coordinates have been printed to coords.xyz because out_coords_steps = 0 in %/header.' % (olddir)) + print( + "No coordinates have been printed to coords.xyz because out_coords_steps =" + " 0 in %/header." % (olddir) + ) sys.exit() ## Tell user available information from header regarding coordinates ## - cprint = old_header.time_step*old_header.out_data_steps*old_header.out_coords_steps - print('The coordinates in %s began at %.2f fs and were printed to coords.xyz every %.2f fs.' % (olddir,old_header.time_init,cprint)) + cprint = ( + old_header.time_step * old_header.out_data_steps * old_header.out_coords_steps + ) + print( + "The coordinates in %s began at %.2f fs and were printed to coords.xyz every" + " %.2f fs." % (olddir, old_header.time_init, cprint) + ) ## Information from new header (desired NEXMD simulations) ## - if not os.path.exists('%s/header' % (newdir)): - print('Path %s/header does not exist.' % (newdir)) + if not os.path.exists("%s/header" % (newdir)): + print("Path %s/header does not exist." % (newdir)) sys.exit() - new_header = header('%s/header' % (newdir)) + new_header = header("%s/header" % (newdir)) ## Check running dynamics ## if new_header.n_class_steps <= 0: - print('Must change n_class_steps in %s/header to greater than 0 to run dynamics.' % (newdir)) + print( + "Must change n_class_steps in %s/header to greater than 0 to run dynamics." + % (newdir) + ) sys.exit() ## Extract atomic numbers ## - if not os.path.exists('%s/restart.out' % (gsdir)): - print('Path %s/restart.out does not exist.' % (gsdir)) + if not os.path.exists("%s/restart.out" % (gsdir)): + print("Path %s/restart.out does not exist." % (gsdir)) sys.exit() - anum = open('%s/restart.out' % (gsdir),'r') + anum = open("%s/restart.out" % (gsdir), "r") anum = anum.readlines() top = None bottom = None index = 0 for line in anum: - if '$COORD' in line: + if "&coord" in line: top = index - if '$ENDCOORD' in line: + if "&endcoord" in line: bottom = index break index += 1 if isinstance(top, int) == True and isinstance(bottom, int) == True: - anum = [ line.split()[0] for line in anum[top+1:bottom:1] ] + anum = [line.split()[0] for line in anum[top + 1 : bottom : 1]] else: - print('There is a problem with %s/restart.out.' % (gsdir)) + print("There is a problem with %s/restart.out." % (gsdir)) sys.exit() ## Choose random seeds ## - NEXMDs = glob.glob('%s/NEXMD*/' % (olddir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (olddir)) NEXMDs.sort() if len(NEXMDs) == 0: - print('There are no NEXMD folders in %s.' % (olddir)) + print("There are no NEXMD folders in %s." % (olddir)) sys.exit() - with open('%s/totdirlist' % (newdir),'w') as data: + with open("%s/totdirlist" % (newdir), "w") as data: for NEXMD in NEXMDs: - if not os.path.exists('%s/dirlist1' % (NEXMD)): - print('Path %sdirlist1 does not exist.' % (NEXMD)) + if not os.path.exists("%s/dirlist1" % (NEXMD)): + print("Path %sdirlist1 does not exist." % (NEXMD)) sys.exit() - inputdata = fileinput.input('%s/dirlist1' % (NEXMD)) + inputdata = fileinput.input("%s/dirlist1" % (NEXMD)) data.writelines(inputdata) - dirlist1 = np.int_(np.genfromtxt('%s/totdirlist' % (newdir))) - if isinstance(dirlist1,int) == True: + dirlist1 = np.int_(np.genfromtxt("%s/totdirlist" % (newdir))) + if isinstance(dirlist1, int) == True: dirlist1 = np.array([dirlist1]) - os.remove('%s/totdirlist' % (newdir)) + os.remove("%s/totdirlist" % (newdir)) ntraj = len(dirlist1) - randq = eval(input('New random seeds? Answer yes [1] or no [0]: ')) - if randq not in [1,0]: - print('Answer must be 1 or 0.') + randq = eval(input("New random seeds? Answer yes [1] or no [0]: ")) + if randq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if randq == 1: - rseeds = random.sample(list(np.arange(1,1000001)), ntraj) + rseeds = random.sample(list(np.arange(1, 1000001)), ntraj) else: - rseeds = input('Path to random-seeds list: ') + rseeds = input("Path to random-seeds list: ") if not os.path.exists(rseeds): - print('Path %s does not exist.' % (rseeds)) + print("Path %s does not exist." % (rseeds)) sys.exit() - rseeds = np.int_(np.genfromtxt('%s' % (rseeds))) - if isinstance(rseeds,int) == True: + rseeds = np.int_(np.genfromtxt("%s" % (rseeds))) + if isinstance(rseeds, int) == True: rseeds = np.array([rseeds]) lenrseeds = len(rseeds) if lenrseeds < ntraj: - print('Length of random-seeds list must be equal to or greater than the number of trajectories.\nUser inputted a random-seeds list of length %d, while the number of trajectories requested is %d.' % (lenrseeds,ntraj)) + print( + "Length of random-seeds list must be equal to or greater than the" + " number of trajectories.\nUser inputted a random-seeds list of length" + " %d, while the number of trajectories requested is %d." + % (lenrseeds, ntraj) + ) sys.exit() for rseed in rseeds: if rseed < 0: - print('A negative random seed was detected, %d.\nWithin the getexcited_package, a negative seed is assigned to a trajectory that could not be prepared due to some problem.' % (rseed)) + print( + "A negative random seed was detected, %d.\nWithin the" + " getexcited_package, a negative seed is assigned to a trajectory that" + " could not be prepared due to some problem." % (rseed) + ) sys.exit() rseeds = np.int_(rseeds) ## Choose time at which geometries should be taken from the old simulation ## - startq = eval(input('At what time, in femtoseconds, should geometries be taken from %s? Plese enter a time greater than 0: ' % (olddir))) + startq = eval( + input( + "At what time, in femtoseconds, should geometries be taken from %s? Plese" + " enter a time greater than 0: " % (olddir) + ) + ) if isinstance(startq, int) == False and isinstance(startq, float) == False: - print('Time must be integer or float') + print("Time must be integer or float") sys.exit() if startq <= 0: - print('Time must be integer or float greater than zero.') + print("Time must be integer or float greater than zero.") sys.exit() startq = np.float(startq) nsteps = 0 - while round(nsteps*cprint + old_header.time_init,3) <= round(startq,3): + while round(nsteps * cprint + old_header.time_init, 3) <= round(startq, 3): nsteps += 1 - if round((nsteps - 1)*cprint + old_header.time_init,2) != round(startq,2): - print('The time, %.2f, minus the initial time, %.2f, is not divisible by %.2f.' % (startq,old_header.time_init,cprint)) + if round((nsteps - 1) * cprint + old_header.time_init, 2) != round(startq, 2): + print( + "The time, %.2f, minus the initial time, %.2f, is not divisible by %.2f." + % (startq, old_header.time_init, cprint) + ) sys.exit() ## Find geometries ## - print('Finding coordinates and velocities from %s. please wait ...' % (olddir)) + print("Finding coordinates and velocities from %s. please wait ..." % (olddir)) tarrayc = np.array([]) tarrayv = np.array([]) - error = open('%s/newsim.err' % (cwd),'w') + error = open("%s/newsim.err" % (cwd), "w") errflag = 0 terrflag = 0 traj = 0 - NEXMDs = glob.glob('%s/NEXMD*/' % (olddir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (olddir)) NEXMDs.sort() if len(NEXMDs) == 0: - print('There are no NEXMD folders in %s.' % (olddir)) + print("There are no NEXMD folders in %s." % (olddir)) sys.exit() for NEXMD in NEXMDs: - dirlist1 = np.int_(np.genfromtxt('%s/dirlist1' % (NEXMD))) - print('%s/dirlist1'% (NEXMD)) - if isinstance(dirlist1,int) == True: + dirlist1 = np.int_(np.genfromtxt("%s/dirlist1" % (NEXMD))) + print("%s/dirlist1" % (NEXMD)) + if isinstance(dirlist1, int) == True: dirlist1 = np.array([dirlist1]) for dir in dirlist1: - if not os.path.exists('%s/%04d/coords.xyz' % (NEXMD,dir)): - print('%s/%04d/coords.xyz' % (NEXMD,dir), 'does not exist', file=error) + if not os.path.exists("%s/%04d/coords.xyz" % (NEXMD, dir)): + print("%s/%04d/coords.xyz" % (NEXMD, dir), "does not exist", file=error) errflag = 1 terrflag = 1 - if not os.path.exists('%s/%04d/velocity.out' % (NEXMD,dir)): - print('%s/%04d/velocity.out' % (NEXMD,dir), 'does not exist', file=error) + if not os.path.exists("%s/%04d/velocity.out" % (NEXMD, dir)): + print( + "%s/%04d/velocity.out" % (NEXMD, dir), "does not exist", file=error + ) errflag = 1 terrflag = 1 if errflag == 1: - tarrayc = np.append(tarrayc, np.array([-9999,-9999])) - tarrayv = np.append(tarrayv, np.array([-9999,-9999])) + tarrayc = np.append(tarrayc, np.array([-9999, -9999])) + tarrayv = np.append(tarrayv, np.array([-9999, -9999])) errflag = 0 traj += 1 continue - datac = open('%s/%04d/coords.xyz' % (NEXMD,dir),'r') + datac = open("%s/%04d/coords.xyz" % (NEXMD, dir), "r") datac = datac.readlines() lenc = len(datac) - datav = open('%s/%04d/velocity.out' % (NEXMD,dir),'r') + datav = open("%s/%04d/velocity.out" % (NEXMD, dir), "r") datav = datav.readlines() lenv = len(datav) ncoords = 0 index = 0 arrayc = np.array([]) for line in datac: - if 'time' in line: + if "time" in line: if ncoords == 0: - dtinit = np.around(np.float(line.split()[-1]), decimals = 3) + dtinit = np.around(np.float(line.split()[-1]), decimals=3) if dtinit != old_header.time_init: - print('Initial time in %s%04d/coords.xyz does not match initial time in %s/header.' %(NEXMD,dir,olddir), file=error) + print( + "Initial time in %s%04d/coords.xyz does not match" + " initial time in %s/header." % (NEXMD, dir, olddir), + file=error, + ) errflag = 1 terrflag = 1 break else: - time = np.around(np.float(line.split()[-1]), decimals = 3) + time = np.around(np.float(line.split()[-1]), decimals=3) if time == startq: tindex = index ncoords += 1 arrayc = np.append(arrayc, index) index += 1 if errflag == 1: - tarrayc = np.append(tarrayc, np.array([-9999,-9999])) - tarrayv = np.append(tarrayv, np.array([-9999,-9999])) + tarrayc = np.append(tarrayc, np.array([-9999, -9999])) + tarrayv = np.append(tarrayv, np.array([-9999, -9999])) errflag = 0 traj += 1 continue @@ -248,13 +294,20 @@ def newsim(header): try: cindex = np.where(arrayc == tindex) except: - print(' There is problem with the directory: %s%04d' %(NEXMD,dir), file=error) - traj +=1 + print( + " There is problem with the directory: %s%04d" % (NEXMD, dir), + file=error, + ) + traj += 1 continue if len(cindex[0]) == 0: - print('Coordinates at %.2f fs in %s%04d/coords.xyz were not found.' % (startq,NEXMD,dir), file=error) - tarrayc = np.append(tarrayc, np.array([-9999,-9999])) - tarrayv = np.append(tarrayv, np.array([-9999,-9999])) + print( + "Coordinates at %.2f fs in %s%04d/coords.xyz were not found." + % (startq, NEXMD, dir), + file=error, + ) + tarrayc = np.append(tarrayc, np.array([-9999, -9999])) + tarrayv = np.append(tarrayv, np.array([-9999, -9999])) terrflag = 1 traj += 1 continue @@ -262,8 +315,8 @@ def newsim(header): index = 0 arrayv = np.array([]) for line in datav: - if 'time' in line: - time = np.around(np.float(line.split()[-1]), decimals = 3) + if "time" in line: + time = np.around(np.float(line.split()[-1]), decimals=3) if time == startq: tindex = index arrayv = np.append(arrayv, index) @@ -272,9 +325,13 @@ def newsim(header): arrayv = np.append(arrayv, lenv) vindex = np.where(arrayv == tindex) if len(vindex[0]) == 0: - print('Velocities at %.2f fs in %s%04d/velocity.out were not found.' % (startq,NEXMD,dir), file=error) - tarrayc = np.append(tarrayc, np.array([-9999,-9999])) - tarrayv = np.append(tarrayv, np.array([-9999,-9999])) + print( + "Velocities at %.2f fs in %s%04d/velocity.out were not found." + % (startq, NEXMD, dir), + file=error, + ) + tarrayc = np.append(tarrayc, np.array([-9999, -9999])) + tarrayv = np.append(tarrayv, np.array([-9999, -9999])) terrflag = 1 traj += 1 continue @@ -283,10 +340,15 @@ def newsim(header): tarrayv = np.append(tarrayv, arrayv) traj += 1 if terrflag == 1: - print('One or more trajectories of the previous simulation cannot be used for in the current simulation, check newsim.err.') - contq = eval(input('Continue preparing input files? Answer yes [1] or no [0]: ')) - if contq not in [1,0]: - print('Answer must be 1 or 0.') + print( + "One or more trajectories of the previous simulation cannot be used for in" + " the current simulation, check newsim.err." + ) + contq = eval( + input("Continue preparing input files? Answer yes [1] or no [0]: ") + ) + if contq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if contq == 0: sys.exit() @@ -297,52 +359,66 @@ def newsim(header): errflag = 0 traj = 0 for NEXMD in np.arange(1, len(NEXMDs) + 1): - os.makedirs('%s/NEXMD%d' % (newdir,NEXMD)) - dirlist1 = np.int_(np.genfromtxt('%s/NEXMD%d/dirlist1' % (olddir,NEXMD))) - if isinstance(dirlist1,int) == True: + os.makedirs("%s/NEXMD%d" % (newdir, NEXMD)) + dirlist1 = np.int_(np.genfromtxt("%s/NEXMD%d/dirlist1" % (olddir, NEXMD))) + if isinstance(dirlist1, int) == True: dirlist1 = np.array([dirlist1]) - dirlist = open('%s/NEXMD%d/dirlist' % (newdir,NEXMD),'w') + dirlist = open("%s/NEXMD%d/dirlist" % (newdir, NEXMD), "w") for dir in dirlist1: - os.makedirs('%s/NEXMD%d/%04d' % (newdir,NEXMD,dir)) + os.makedirs("%s/NEXMD%d/%04d" % (newdir, NEXMD, dir)) if -9999 in tarrayc[traj]: - print('The input file for %s/NEXMD%d/%04d cannot be generated.' % (olddir,NEXMD,dir), file=error) + print( + "The input file for %s/NEXMD%d/%04d cannot be generated." + % (olddir, NEXMD, dir), + file=error, + ) errflag = 1 traj += 1 continue - datac = open('%s/NEXMD%d/%04d/coords.xyz' % (olddir,NEXMD,dir),'r') + datac = open("%s/NEXMD%d/%04d/coords.xyz" % (olddir, NEXMD, dir), "r") datac = datac.readlines() - datav = open('%s/NEXMD%d/%04d/velocity.out' % (olddir,NEXMD,dir),'r') + datav = open("%s/NEXMD%d/%04d/velocity.out" % (olddir, NEXMD, dir), "r") datav = datav.readlines() datac = np.asarray(datac) - coords = datac[tarrayc[traj][0] + 1:tarrayc[traj][1] - 1:1] - velocs = datav[tarrayv[traj][0] + 2:tarrayv[traj][1] - 1:1] - inputfile = open('%s/NEXMD%d/%04d/input.ceon' % (newdir,NEXMD,dir),'w') + coords = datac[tarrayc[traj][0] + 1 : tarrayc[traj][1] - 1 : 1] + velocs = datav[tarrayv[traj][0] + 2 : tarrayv[traj][1] - 1 : 1] + inputfile = open("%s/NEXMD%d/%04d/input.ceon" % (newdir, NEXMD, dir), "w") for line in new_header.file: - if 'rnd_seed_flag' in line: - inputfile.write(' rnd_seed=%d, ! seed for the random number generator\n' % (rseeds[traj])) + if "rnd_seed_flag" in line: + inputfile.write( + " rnd_seed=%d, ! seed for the random number generator\n" + % (rseeds[traj]) + ) else: - if 'nucl_coord_veloc_flag' in line: - inputfile.write('&coord\n') + if "nucl_coord_veloc_flag" in line: + inputfile.write("&coord\n") aindex = 0 for line in coords: val = line.split() - inputfile.write('{:>6} {:>12} {:>12} {:>12}'.format(anum[aindex],val[1],val[2],val[3])) - inputfile.write('\n') + inputfile.write( + "{:>6} {:>12} {:>12} {:>12}".format( + anum[aindex], val[1], val[2], val[3] + ) + ) + inputfile.write("\n") aindex += 1 - inputfile.write('&endcoord\n\n&veloc\n') + inputfile.write("&endcoord\n\n&veloc\n") for line in velocs: inputfile.write(line) - inputfile.write('&endveloc') + inputfile.write("&endveloc") else: inputfile.write(line) - print('%04d' % (dir), file=dirlist) - print('%s/NEXMD%d/%04d' % (newdir,NEXMD,dir)) + print("%04d" % (dir), file=dirlist) + print("%s/NEXMD%d/%04d" % (newdir, NEXMD, dir)) traj += 1 dirlist.close() - shutil.copyfile('%s/NEXMD%d/dirlist' % (newdir,NEXMD), '%s/NEXMD%d/dirlist1' % (newdir,NEXMD)) - np.savetxt('%s/rseedslist' % (newdir), np.transpose(rseeds[0:traj:1])) + shutil.copyfile( + "%s/NEXMD%d/dirlist" % (newdir, NEXMD), + "%s/NEXMD%d/dirlist1" % (newdir, NEXMD), + ) + np.savetxt("%s/rseedslist" % (newdir), np.transpose(rseeds[0:traj:1])) if errflag == 1: - print('One or more trajectories cannot be prepared, check newsim.err.') + print("One or more trajectories cannot be prepared, check newsim.err.") sys.exit() else: - os.remove('%s/newsim.err' % (cwd)) + os.remove("%s/newsim.err" % (cwd)) diff --git a/getexcited/getexcited_package/nexmd.py b/getexcited/getexcited_package/nexmd.py index 04a91fe..63f0f9e 100644 --- a/getexcited/getexcited_package/nexmd.py +++ b/getexcited/getexcited_package/nexmd.py @@ -1,6 +1,6 @@ -#/usr/bin/python +# /usr/bin/python -''' +""" This function prepares input files for non-adiabatic excited-state molecular dynamics (NEXMD). @@ -22,64 +22,74 @@ directory, will be deleted if this function is completely executed! -''' +""" -import numpy as np -import random +import fileinput +import glob import os -import sys +import random import shutil -import glob -import fileinput +import sys + +import numpy as np cwd = os.getcwd() + def nexmd(header): - - print('Preparing input files for NEXMD.') - + print("Preparing input files for NEXMD.") + ## Directory names ## - gsdir = input('Ground-state dynamics directory: ') + gsdir = input("Ground-state dynamics directory: ") if not os.path.exists(gsdir): - print('Path %s does not exist.' % (gsdir)) + print("Path %s does not exist." % (gsdir)) sys.exit() - outdir = input('Output directory [e.g. NEXMD]: ') + outdir = input("Output directory [e.g. NEXMD]: ") if not os.path.exists(outdir): - print('Path %s does not exist.' % (outdir)) + print("Path %s does not exist." % (outdir)) sys.exit() ## Delete previous NEXMD folders and rseedslists ## - NEXMDs = glob.glob('%s/NEXMD*/' % (outdir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (outdir)) NEXMDs.sort() if len(NEXMDs) != 0: - contq = eval(input('** WARNING ** All NEXMD folders and rseedslists inside %s will be deleted!\ndo you want to continue? Answer yes [1] or no [0]: ' % (outdir))) - if contq not in [1,0]: - print('answer must be 1 or 0.') + contq = eval( + input( + "** WARNING ** All NEXMD folders and rseedslists inside %s will be" + " deleted!\ndo you want to continue? Answer yes [1] or no [0]: " + % (outdir) + ) + ) + if contq not in [1, 0]: + print("answer must be 1 or 0.") sys.exit() if contq == 0: sys.exit() for NEXMD in NEXMDs: - print('Deleting', '%s' % (NEXMD)) + print("Deleting", "%s" % (NEXMD)) shutil.rmtree(NEXMD) - rseedslist = glob.glob('%s/rseedslist*' % (outdir)) + rseedslist = glob.glob("%s/rseedslist*" % (outdir)) for rseeds in rseedslist: os.remove(rseeds) - print('Deleting', '%s' % (rseeds)) + print("Deleting", "%s" % (rseeds)) ## Information from header ## - if not os.path.exists('%s/header' % (outdir)): - print('Path %s/header does not exist.' % (outdir)) + if not os.path.exists("%s/header" % (outdir)): + print("Path %s/header does not exist." % (outdir)) sys.exit() - header = header('%s/header' % (outdir)) + header = header("%s/header" % (outdir)) ## Check running dynamics ## if header.n_class_steps <= 0: - print('Must change n_class_steps in %s/header to greater than 0 for dynamics.' % (outdir)) + print( + "Must change n_class_steps in %s/header to greater than 0 for dynamics." + % (outdir) + ) sys.exit() ## Check type of initial excitation ## - #waves = 1 # only for old code - #wavec = 1 # only for old code + # waves = 1 # only for old code + # wavec = 1 # only for old code ## possible combinations ## ## state set, coefficients set (state_set = 1, coeff_set = 1) - single state ## state set, coefficients not set (state_set = 1, coeff_set = 0) - single state @@ -96,268 +106,374 @@ def nexmd(header): except AttributeError: coeff_set = 1 if state_set == 1 and coeff_set == 1 or state_set == 1 and coeff_set == 0: - print('All trajectories will begin on state %d.' % (header.exc_state_init)) + print("All trajectories will begin on state %d." % (header.exc_state_init)) if state_set == 0 and coeff_set == 1: - print('There is an inconsistency in header.\nInput exc_state_init is not set, while coefficients are set.') + print( + "There is an inconsistency in header.\nInput exc_state_init is not set," + " while coefficients are set." + ) sys.exit() if state_set == 0 and coeff_set == 0: - print('Initial excited states will model a photoexcited wavepacket according to the optical spectrum.') - spdir = input('Single-point calculations directory: ') + print( + "Initial excited states will model a photoexcited wavepacket according to" + " the optical spectrum." + ) + spdir = input("Single-point calculations directory: ") if not os.path.exists(spdir): - print('Path %s does not exist.' % (spdir)) + print("Path %s does not exist." % (spdir)) sys.exit() - + ## Find geometries ## - if not os.path.exists('%s/coords.xyz' % (gsdir)): - print('Path %s/coords.xyz does not exist.' % (gsdir)) + if not os.path.exists("%s/coords.xyz" % (gsdir)): + print("Path %s/coords.xyz does not exist." % (gsdir)) sys.exit() - datac = open('%s/coords.xyz' % (gsdir),'r') + datac = open("%s/coords.xyz" % (gsdir), "r") datac = datac.readlines() lenc = len(datac) - if not os.path.exists('%s/velocity.out' % (gsdir)): - print('path %s/velocity.out does not exist.' % (gsdir)) + if not os.path.exists("%s/velocity.out" % (gsdir)): + print("path %s/velocity.out does not exist." % (gsdir)) sys.exit() - datav = open('%s/velocity.out' % (gsdir),'r') + datav = open("%s/velocity.out" % (gsdir), "r") datav = datav.readlines() lenv = len(datav) ncoords = 0 index = 0 arrayc = np.array([]) for line in datac: - if 'time' in line: + if "time" in line: if ncoords == 0: tinit = np.float(line.split()[-1]) else: time = np.float(line.split()[-1]) ncoords += 1 - arrayc = np.append(arrayc,index) + arrayc = np.append(arrayc, index) index += 1 - arrayc = np.append(arrayc,lenc + 1) + arrayc = np.append(arrayc, lenc + 1) arrayc = np.int_(arrayc) if ncoords == 0: - print('No coordinates were found.') + print("No coordinates were found.") sys.exit() if ncoords == 1: - print('Only initial coordinates, at %.2f fs, were found.' % (tinit)) + print("Only initial coordinates, at %.2f fs, were found." % (tinit)) sys.exit() if ncoords > 1: index = 0 arrayv = np.array([]) for line in datav: - if 'time' in line: - arrayv = np.append(arrayv,index) + if "time" in line: + arrayv = np.append(arrayv, index) index += 1 - arrayv = np.append(arrayv,lenv) + arrayv = np.append(arrayv, lenv) arrayv = np.int_(arrayv) - tinc = time/(ncoords - 1) + tinc = time / (ncoords - 1) if state_set == 1 and coeff_set == 1 or state_set == 1 and coeff_set == 0: - print('A total of %d coordinate files, ranging from %.2f to %.2f fs in increments of %.2f fs, were found.' % (ncoords,tinit,time,tinc)) + print( + "A total of %d coordinate files, ranging from %.2f to %.2f fs in increments" + " of %.2f fs, were found." % (ncoords, tinit, time, tinc) + ) if state_set == 0 and coeff_set == 0: - print('A total of %d coordinate files, ranging from %.2f to %.2f fs in increments of %.2f fs, were found.\nNote: only coordinate files used for single-point calculations can be used for NEXMD.' % (ncoords,tinit,time,tinc)) + print( + "A total of %d coordinate files, ranging from %.2f to %.2f fs in increments" + " of %.2f fs, were found.\nNote: only coordinate files used for" + " single-point calculations can be used for NEXMD." + % (ncoords, tinit, time, tinc) + ) ## Choose geometries for a photoexcited wavepacket ## if state_set == 0 and coeff_set == 0: - NEXMDs = glob.glob('%s/NEXMD*/' % (spdir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (spdir)) NEXMDs.sort() if len(NEXMDs) == 0: - print('There are no NEXMD folders in %s.' % (spdir)) + print("There are no NEXMD folders in %s." % (spdir)) sys.exit() - with open('%s/totdirlist' % (outdir),'w') as data: + with open("%s/totdirlist" % (outdir), "w") as data: for NEXMD in NEXMDs: - if not os.path.exists('%s/dirlist1' % (NEXMD)): - print('Path %sdirlist1 does not exist.' % (NEXMD)) + if not os.path.exists("%s/dirlist1" % (NEXMD)): + print("Path %sdirlist1 does not exist." % (NEXMD)) sys.exit() - inputdata = fileinput.input('%s/dirlist1' % (NEXMD)) + inputdata = fileinput.input("%s/dirlist1" % (NEXMD)) data.writelines(inputdata) - dirlist1 = np.int_(np.genfromtxt('%s/totdirlist' % (outdir))) - if isinstance(dirlist1,int) == True: + dirlist1 = np.int_(np.genfromtxt("%s/totdirlist" % (outdir))) + if isinstance(dirlist1, int) == True: dirlist1 = np.array([dirlist1]) - os.remove('%s/totdirlist' % (outdir)) + os.remove("%s/totdirlist" % (outdir)) ntraj = len(dirlist1) - ntrajq = eval(input('How many trajectories for NEXMD? Enter a number no greater than %d: ' % (ntraj))) + ntrajq = eval( + input( + "How many trajectories for NEXMD? Enter a number no greater than %d: " + % (ntraj) + ) + ) if isinstance(ntrajq, int) == False: - print('Number of trajectories must be integer.') + print("Number of trajectories must be integer.") sys.exit() if ntrajq == 0: - print('Number of trajectories must be positive integer.') + print("Number of trajectories must be positive integer.") sys.exit() if np.abs(ntrajq) > ntraj: - print('Number of trajectories must be less than or equal to %d.' % (ntraj)) + print("Number of trajectories must be less than or equal to %d." % (ntraj)) sys.exit() if ntrajq < 0: ntraj = np.abs(ntrajq) - coordsq = eval(input('You have requested %d randomly-selected coordinate files in the range %d to %d.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,dirlist[0],dirlist[-1]))) + coordsq = eval( + input( + "You have requested %d randomly-selected coordinate files in the" + " range %d to %d.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, dirlist[0], dirlist[-1]) + ) + ) else: - interval = np.int(np.ceil(ntraj/np.float(ntrajq))) - if interval*ntrajq > ntraj: + interval = np.int(np.ceil(ntraj / np.float(ntrajq))) + if interval * ntrajq > ntraj: interval = interval - 1 ntraj = len(dirlist1[0::interval]) else: ntraj = ntrajq - coordsq = eval(input('You have requested %d evenly-spaced coordinate files in the range %d to %d for NEXMD.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,dirlist1[0],dirlist1[0::interval][-1]))) - if coordsq not in [1,0]: - print('Answer must be 1 or 0.') + coordsq = eval( + input( + "You have requested %d evenly-spaced coordinate files in the range" + " %d to %d for NEXMD.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, dirlist1[0], dirlist1[0::interval][-1]) + ) + ) + if coordsq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if coordsq == 0: sys.exit() ## Choose geometries for a single excited state ## if state_set == 1 and coeff_set == 1 or state_set == 1 and coeff_set == 0: - coords = eval(input('Enter requested range of the ground-state sampling by coordinate files and the number of trajectories.\nInput an array of the form [start, end, number]: ')) - if not isinstance(coords,list): - print('Input must be an array of the form [start, end, number].\nFor example, [1, 1000, 500] requests 500 coordinate files sampled from 1 to 1000.') + coords = eval( + input( + "Enter requested range of the ground-state sampling by coordinate files" + " and the number of trajectories.\nInput an array of the form [start," + " end, number]: " + ) + ) + if not isinstance(coords, list): + print( + "Input must be an array of the form [start, end, number].\nFor example," + " [1, 1000, 500] requests 500 coordinate files sampled from 1 to 1000." + ) sys.exit() if len(coords) != 3: - print('Input must be an array with three elements of the form [start, end, number].\nFor example, [1, 1000, 500] requests 500 coordinate files sampled from 1 to 1000.') + print( + "Input must be an array with three elements of the form [start, end," + " number].\nFor example, [1, 1000, 500] requests 500 coordinate files" + " sampled from 1 to 1000." + ) sys.exit() index = 0 for i in coords: if isinstance(i, int) == False: - print('Element number %d of input array must be integer.\nUser inputted [%s, %s, %s], which is not allowed.' % (index + 1,coords[0],coords[1],coords[2])) + print( + "Element number %d of input array must be integer.\nUser inputted" + " [%s, %s, %s], which is not allowed." + % (index + 1, coords[0], coords[1], coords[2]) + ) sys.exit() - if index in [0,1]: + if index in [0, 1]: if i not in np.arange(ncoords): - print('Element number %d of input array must be integer between 0 and %d.\nUser inputted [%d, %d, %d], which is not allowed.' % (index + 1,ncoords - 1,coords[0],coords[1],coords[2])) + print( + "Element number %d of input array must be integer between 0 and" + " %d.\nUser inputted [%d, %d, %d], which is not allowed." + % (index + 1, ncoords - 1, coords[0], coords[1], coords[2]) + ) sys.exit() else: - if i not in np.arange(1,ncoords + 1) and i not in -np.arange(1,ncoords + 1): - print('Element number %d of input array must be integer between 1 and %d.\nUser inputted [%d, %d, %d], which is not allowed.' % (index + 1,ncoords,coords[0],coords[1],coords[2])) + if i not in np.arange(1, ncoords + 1) and i not in -np.arange( + 1, ncoords + 1 + ): + print( + "Element number %d of input array must be integer between 1 and" + " %d.\nUser inputted [%d, %d, %d], which is not allowed." + % (index + 1, ncoords, coords[0], coords[1], coords[2]) + ) sys.exit() index += 1 if coords[0] > coords[1]: - print('Second element of input array must be greater than first element.\nUser inputted [%d, %d, %d], which is not allowed.' % (coords[0],coords[1],coords[2])) + print( + "Second element of input array must be greater than first" + " element.\nUser inputted [%d, %d, %d], which is not allowed." + % (coords[0], coords[1], coords[2]) + ) sys.exit() if (coords[1] - coords[0]) + 1 < np.abs(coords[2]): - print('Number of coordinate files requested must be less than or equal to the number of coordinate files in the sample.\nUser inputted [%d, %d, %d], which is not allowed.' % (coords[0],coords[1],coords[2])) + print( + "Number of coordinate files requested must be less than or equal to the" + " number of coordinate files in the sample.\nUser inputted [%d, %d," + " %d], which is not allowed." % (coords[0], coords[1], coords[2]) + ) sys.exit() if coords[2] < 0: ntraj = np.abs(coords[2]) - coordsq = eval(input('You have requested %d randomly-selected coordinate files in the range %d to %d.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,coords[0],coords[1]))) + coordsq = eval( + input( + "You have requested %d randomly-selected coordinate files in the" + " range %d to %d.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, coords[0], coords[1]) + ) + ) else: - interval = np.int(np.ceil((coords[1] - coords[0] + 1)/np.float(coords[2]))) - if interval*coords[2] > (coords[1] - coords[0] + 1): + interval = np.int( + np.ceil((coords[1] - coords[0] + 1) / np.float(coords[2])) + ) + if interval * coords[2] > (coords[1] - coords[0] + 1): interval = interval - 1 - ntraj = len(np.arange(coords[0],coords[1] + 1,interval)) + ntraj = len(np.arange(coords[0], coords[1] + 1, interval)) else: ntraj = coords[2] - coordsq = eval(input('You have requested %d evenly-spaced coordinate files in the range %d to %d.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,coords[0],coords[1]))) - if coordsq not in [1,0]: - print('Answer must be 1 or 0.') + coordsq = eval( + input( + "You have requested %d evenly-spaced coordinate files in the range" + " %d to %d.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, coords[0], coords[1]) + ) + ) + if coordsq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if coordsq == 0: sys.exit() ## Split geometries ## - split = eval(input('Number of trajectories per NEXMD folder: ')) + split = eval(input("Number of trajectories per NEXMD folder: ")) if isinstance(split, int) == False: - print('Number of trajectories per NEXMD folder must be integer.') + print("Number of trajectories per NEXMD folder must be integer.") sys.exit() if split <= 0: - print('Number of trajectories per NEXMD folder must be integer greater than zero.') + print( + "Number of trajectories per NEXMD folder must be integer greater than zero." + ) sys.exit() - dirsplit = split*np.arange(1,np.ceil(np.float(ntraj)/split)+1) + dirsplit = split * np.arange(1, np.ceil(np.float(ntraj) / split) + 1) if state_set == 1 and coeff_set == 1 or state_set == 1 and coeff_set == 0: dirsplit[-1] = coords[1] + 1 if coords[2] < 0: - dirsplit = dirsplit.astype(np.int64) - dirsplit = np.split(np.sort(random.sample(np.arange(coords[0],coords[1]+1),ntraj)),dirsplit) + dirsplit = dirsplit.astype(np.int64) + dirsplit = np.split( + np.sort(random.sample(np.arange(coords[0], coords[1] + 1), ntraj)), + dirsplit, + ) else: - dirsplit = dirsplit.astype(np.int64) - dirsplit = np.split(np.arange(coords[0],coords[1]+1,interval),dirsplit) + dirsplit = dirsplit.astype(np.int64) + dirsplit = np.split(np.arange(coords[0], coords[1] + 1, interval), dirsplit) if state_set == 0 and coeff_set == 0: dirsplit[-1] = dirlist1[-1] dirsplit = dirsplit.astype(np.int64) if ntrajq < 0: - dirsplit = np.split(np.sort(random.sample(dirlist1,ntraj)),dirsplit) + dirsplit = np.split(np.sort(random.sample(dirlist1, ntraj)), dirsplit) else: - dirsplit = np.split(dirlist1[0::interval],dirsplit) + dirsplit = np.split(dirlist1[0::interval], dirsplit) ## Extract atomic numbers ## - if not os.path.exists('%s/restart.out' % (gsdir)): - print('Path %s/restart.out does not exist.' % (gsdir)) + if not os.path.exists("%s/restart.out" % (gsdir)): + print("Path %s/restart.out does not exist." % (gsdir)) sys.exit() - anum = open('%s/restart.out' % (gsdir),'r') + anum = open("%s/restart.out" % (gsdir), "r") anum = anum.readlines() top = None bottom = None index = 0 for line in anum: - if '$COORD' in line: + if "&coord" in line: top = index - if '$ENDCOORD' in line: + if "&endcoord" in line: bottom = index break index += 1 if isinstance(top, int) == True and isinstance(bottom, int) == True: - anum = [ line.split()[0] for line in anum[top+1:bottom:1] ] + anum = [line.split()[0] for line in anum[top + 1 : bottom : 1]] else: - print('There is a problem with %s/restart.out.' % (gsdir)) + print("There is a problem with %s/restart.out." % (gsdir)) sys.exit() ## Choose random seeds ## - randq = eval(input('New random seeds? answer yes [1] or no [0]: ')) - if randq not in [1,0]: - print('Answer must be 1 or 0.') + randq = eval(input("New random seeds? answer yes [1] or no [0]: ")) + if randq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if randq == 1: - rseeds = random.sample(list(np.arange(1,1000001)), ntraj) + rseeds = random.sample(list(np.arange(1, 1000001)), ntraj) else: - rseeds = input('path to random-seeds list: ') + rseeds = input("path to random-seeds list: ") if not os.path.exists(rseeds): - print('Path %s does not exist.' % (rseeds)) + print("Path %s does not exist." % (rseeds)) sys.exit() - rseeds = np.int_(np.genfromtxt('%s' % (rseeds))) - if isinstance(rseeds,int) == True: + rseeds = np.int_(np.genfromtxt("%s" % (rseeds))) + if isinstance(rseeds, int) == True: rseeds = np.array([rseeds]) lenrseeds = len(rseeds) if lenrseeds < ntraj: - print('Length of random-seeds list must be equal to or greater than the number of trajectories.\nUser inputted a random-seeds list of length %d, while the number of trajectories requested is %d.' % (lenrseeds,ntraj)) + print( + "Length of random-seeds list must be equal to or greater than the" + " number of trajectories.\nUser inputted a random-seeds list of length" + " %d, while the number of trajectories requested is %d." + % (lenrseeds, ntraj) + ) sys.exit() for rseed in rseeds: if rseed < 0: - print('A negative random seed was detected, %d.\nWithin the getexcited_package, a negative seed is assigned to a trajectory that could not be prepared due to some problem.' % (rseed)) + print( + "A negative random seed was detected, %d.\nWithin the" + " getexcited_package, a negative seed is assigned to a trajectory that" + " could not be prepared due to some problem." % (rseed) + ) sys.exit() rseeds = np.int_(rseeds) ## Prepare NEXMD input files with a photoexcited wavepacket ## if state_set == 0 and coeff_set == 0: - spNEXMDs = glob.glob('%s/NEXMD*/' % (spdir)) + spNEXMDs = glob.glob("%s/NEXMD*/" % (spdir)) spNEXMDs.sort() - error = open('%s/ceo.err' % (cwd),'w') - stype = eval(input('Spectral lineshape? Answer Gaussian [0] or Lorentzian [1]: ')) - if stype not in [0,1]: - print('Answer must be 0 or 1.') + error = open("%s/ceo.err" % (cwd), "w") + stype = eval( + input("Spectral lineshape? Answer Gaussian [0] or Lorentzian [1]: ") + ) + if stype not in [0, 1]: + print("Answer must be 0 or 1.") sys.exit() - excen = eval(input('Laser excitation energy in eV: ')) + excen = eval(input("Laser excitation energy in eV: ")) if isinstance(excen, int) == False and isinstance(excen, float) == False: - print('Excitation energy must be integer or float.') + print("Excitation energy must be integer or float.") sys.exit() if stype == 0: - FWHM = eval(input('Please enter the full width at half maximum (FWHM) for the Gaussian lineshape in eV [e.g. 0.36]: ')) - specb = FWHM/(2*np.sqrt(2*np.log(2))) + FWHM = eval( + input( + "Please enter the full width at half maximum (FWHM) for the" + " Gaussian lineshape in eV [e.g. 0.36]: " + ) + ) + specb = FWHM / (2 * np.sqrt(2 * np.log(2))) else: - FWHM = eval(input('Please enter the full width at half maximum (FWHM) for the Lorentzian lineshape in eV [e.g. 0.36]: ')) + FWHM = eval( + input( + "Please enter the full width at half maximum (FWHM) for the" + " Lorentzian lineshape in eV [e.g. 0.36]: " + ) + ) specb = FWHM if isinstance(specb, int) == False and isinstance(specb, float) == False: - print('Spectral broadening must be integer or float.') + print("Spectral broadening must be integer or float.") sys.exit() traj = 0 index = 0 ceoflag = 0 - for NEXMD in np.arange(1,np.ceil(np.float(ntraj)/split)+1): - os.makedirs('%s/NEXMD%d' % (outdir,NEXMD)) - dirlist = open('%s/NEXMD%d/dirlist' % (outdir,NEXMD),'w') + for NEXMD in np.arange(1, np.ceil(np.float(ntraj) / split) + 1): + os.makedirs("%s/NEXMD%d" % (outdir, NEXMD)) + dirlist = open("%s/NEXMD%d/dirlist" % (outdir, NEXMD), "w") for dir in dirsplit[index]: - os.makedirs('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) + os.makedirs("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) for spNEXMD in spNEXMDs: - if os.path.exists('%s/%04d/ceo.out' % (spNEXMD,dir)): - data = np.genfromtxt('%s/%04d/ceo.out' % (spNEXMD,dir)) + if os.path.exists("%s/%04d/ceo.out" % (spNEXMD, dir)): + data = np.genfromtxt("%s/%04d/ceo.out" % (spNEXMD, dir)) iceoflag = 0 break else: iceoflag = 1 if iceoflag == 1: - print('%s%04d/ceo.out does not exist' % (spNEXMD,dir), file=error) + print("%s%04d/ceo.out does not exist" % (spNEXMD, dir), file=error) rseeds[traj] = -123456789 ceoflag = 1 iceoflag = 0 @@ -368,58 +484,83 @@ def nexmd(header): oindex = 0 if stype == 0: for state, energy, osx, osy, osz, oscstren in data: - qpop[oindex] = oscstren*np.exp(-(energy - excen)**(2.0)/(2.0*specb**(2.0)))/np.sqrt(2.0*np.pi*specb**(2.0)) + qpop[oindex] = ( + oscstren + * np.exp( + -((energy - excen) ** (2.0)) / (2.0 * specb ** (2.0)) + ) + / np.sqrt(2.0 * np.pi * specb ** (2.0)) + ) oindex += 1 else: for state, energy, osx, osy, osz, oscstren in data: - qpop[oindex] = oscstren/(1.0 + ((energy - excen)**2.0)/(specb/2.0)**(2.0))/(specb*np.pi/2.0) + qpop[oindex] = ( + oscstren + / (1.0 + ((energy - excen) ** 2.0) / (specb / 2.0) ** (2.0)) + / (specb * np.pi / 2.0) + ) oindex += 1 - qpop = qpop/np.sum(qpop) - state = np.searchsorted(np.cumsum(qpop),np.random.uniform()) + 1 + qpop = qpop / np.sum(qpop) + state = np.searchsorted(np.cumsum(qpop), np.random.uniform()) + 1 qpop = np.zeros(lendata) - qpop[state-1] = 1.0 - coords = datac[arrayc[dir]+1:arrayc[dir+1]-1:1] - velocs = datav[arrayv[dir]+2:arrayv[dir+1]-1:1] - inputfile = open('%s/NEXMD%d/%04d/input.ceon' % (outdir,NEXMD,dir),'w') + qpop[state - 1] = 1.0 + coords = datac[arrayc[dir] + 1 : arrayc[dir + 1] - 1 : 1] + velocs = datav[arrayv[dir] + 2 : arrayv[dir + 1] - 1 : 1] + inputfile = open( + "%s/NEXMD%d/%04d/input.ceon" % (outdir, NEXMD, dir), "w" + ) for line in header.file: - if 'rnd_seed_flag' in line: - inputfile.write(' rnd_seed=%d, ! seed for the random number generator\n' % (rseeds[traj])) + if "rnd_seed_flag" in line: + inputfile.write( + " rnd_seed=%d, ! seed for the random number generator\n" + % (rseeds[traj]) + ) else: - if 'exc_state_init_flag' in line: - inputfile.write(' exc_state_init=%d, ! initial excited state (0 - ground state) [0]\n' % (state)) + if "exc_state_init_flag" in line: + inputfile.write( + " exc_state_init=%d, ! initial excited state (0 -" + " ground state) [0]\n" % (state) + ) else: - if 'nucl_coord_veloc_flag' in line: - inputfile.write('&coord\n') + if "nucl_coord_veloc_flag" in line: + inputfile.write("&coord\n") aindex = 0 for line in coords: val = line.split() - inputfile.write('{:>6} {:>12} {:>12} {:>12}'.format(anum[aindex],val[1],val[2],val[3])) - inputfile.write('\n') + inputfile.write( + "{:>6} {:>12} {:>12} {:>12}".format( + anum[aindex], val[1], val[2], val[3] + ) + ) + inputfile.write("\n") aindex += 1 - inputfile.write('&endcoord\n\n&veloc\n') + inputfile.write("&endcoord\n\n&veloc\n") for line in velocs: inputfile.write(line) - inputfile.write('&endveloc\n') + inputfile.write("&endveloc\n") else: - if 'quant_amp_phase_flag' in line: - inputfile.write('&coeff\n') + if "quant_amp_phase_flag" in line: + inputfile.write("&coeff\n") for line in qpop: - inputfile.write(' %.3f %.3f\n' % (line,0.0)) - inputfile.write('&endcoeff\n') + inputfile.write(" %.3f %.3f\n" % (line, 0.0)) + inputfile.write("&endcoeff\n") else: inputfile.write(line) - print('%04d' % (dir), file=dirlist) - print('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) + print("%04d" % (dir), file=dirlist) + print("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) traj += 1 dirlist.close() - shutil.copyfile('%s/NEXMD%d/dirlist' % (outdir,NEXMD), '%s/NEXMD%d/dirlist1' % (outdir,NEXMD)) + shutil.copyfile( + "%s/NEXMD%d/dirlist" % (outdir, NEXMD), + "%s/NEXMD%d/dirlist1" % (outdir, NEXMD), + ) index += 1 - np.savetxt('%s/rseedslist' % (outdir), np.transpose(rseeds[0:traj:1])) + np.savetxt("%s/rseedslist" % (outdir), np.transpose(rseeds[0:traj:1])) if ceoflag == 1: - print('One or more NEXMD trajectories cannot be prepared, check ceo.err.') + print("One or more NEXMD trajectories cannot be prepared, check ceo.err.") sys.exit() else: - os.remove('%s/ceo.err' % (cwd)) + os.remove("%s/ceo.err" % (cwd)) ## Prepare NEXMD input files with a single excited state ## if state_set == 1 and coeff_set == 1 or state_set == 1 and coeff_set == 0: @@ -427,46 +568,60 @@ def nexmd(header): qpop[header.exc_state_init - 1] = 1.0 traj = 0 index = 0 - for NEXMD in np.arange(1,np.ceil(np.float(ntraj)/split)+1): - os.makedirs('%s/NEXMD%d' % (outdir,NEXMD)) - dirlist = open('%s/NEXMD%d/dirlist' % (outdir,NEXMD),'w') + for NEXMD in np.arange(1, np.ceil(np.float(ntraj) / split) + 1): + os.makedirs("%s/NEXMD%d" % (outdir, NEXMD)) + dirlist = open("%s/NEXMD%d/dirlist" % (outdir, NEXMD), "w") for dir in dirsplit[index]: - os.makedirs('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) - coords = datac[arrayc[dir]+1:arrayc[dir+1]-1:1] - velocs = datav[arrayv[dir]+2:arrayv[dir+1]-1:1] - inputfile = open('%s/NEXMD%d/%04d/input.ceon' % (outdir,NEXMD,dir),'w') + os.makedirs("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) + coords = datac[arrayc[dir] + 1 : arrayc[dir + 1] - 1 : 1] + velocs = datav[arrayv[dir] + 2 : arrayv[dir + 1] - 1 : 1] + inputfile = open( + "%s/NEXMD%d/%04d/input.ceon" % (outdir, NEXMD, dir), "w" + ) for line in header.file: - if 'rnd_seed_flag' in line: - inputfile.write(' rnd_seed=%d, ! seed for the random number generator\n' % (rseeds[traj])) ## for new code (NEXMD) - #inputfile.write('%d ! seed for the random number generator\n' % (rseeds[traj])) ## for old code (NAESMD) + if "rnd_seed_flag" in line: + inputfile.write( + " rnd_seed=%d, ! seed for the random number generator\n" + % (rseeds[traj]) + ) ## for new code (NEXMD) + # inputfile.write('%d ! seed for the random number generator\n' % (rseeds[traj])) ## for old code (NAESMD) else: - if 'nucl_coord_veloc_flag' in line: - inputfile.write('&coord\n') ## for new code (NEXMD) - #inputfile.write('$COORD\n') ## for old code (NAESMD) + if "nucl_coord_veloc_flag" in line: + inputfile.write("&coord\n") ## for new code (NEXMD) + # inputfile.write('&coord\n') ## for old code (NAESMD) aindex = 0 for line in coords: val = line.split() - inputfile.write('{:>6} {:>12} {:>12} {:>12}'.format(anum[aindex],val[1],val[2],val[3])) - inputfile.write('\n') + inputfile.write( + "{:>6} {:>12} {:>12} {:>12}".format( + anum[aindex], val[1], val[2], val[3] + ) + ) + inputfile.write("\n") aindex += 1 - inputfile.write('&endcoord\n\n&veloc\n') ## for new code (NEXMD) - #inputfile.write('$ENDCOORD\n\n$VELOC\n') ## for old code (NAESMD) + inputfile.write( + "&endcoord\n\n&veloc\n" + ) ## for new code (NEXMD) + # inputfile.write('&endcoord\n\n$VELOC\n') ## for old code (NAESMD) for line in velocs: inputfile.write(line) - inputfile.write('&endveloc\n') - #inputfile.write('$ENDVELOC\n') ## for old code (NAESMD) + inputfile.write("&endveloc\n") + # inputfile.write('$ENDVELOC\n') ## for old code (NAESMD) else: - if 'quant_amp_phase_flag' in line: - inputfile.write('&coeff\n') + if "quant_amp_phase_flag" in line: + inputfile.write("&coeff\n") for line in qpop: - inputfile.write(' %.3f %.3f\n' % (line,0.0)) - inputfile.write('&endcoeff\n') + inputfile.write(" %.3f %.3f\n" % (line, 0.0)) + inputfile.write("&endcoeff\n") else: inputfile.write(line) - print('%04d' % (dir), file=dirlist) - print('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) + print("%04d" % (dir), file=dirlist) + print("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) traj += 1 dirlist.close() - shutil.copyfile('%s/NEXMD%d/dirlist' % (outdir,NEXMD), '%s/NEXMD%d/dirlist1' % (outdir,NEXMD)) + shutil.copyfile( + "%s/NEXMD%d/dirlist" % (outdir, NEXMD), + "%s/NEXMD%d/dirlist1" % (outdir, NEXMD), + ) index += 1 - np.savetxt('%s/rseedslist' % (outdir), np.transpose(rseeds[0:traj:1])) + np.savetxt("%s/rseedslist" % (outdir), np.transpose(rseeds[0:traj:1])) diff --git a/getexcited/getexcited_package/restart.py b/getexcited/getexcited_package/restart.py index d348571..96d948c 100644 --- a/getexcited/getexcited_package/restart.py +++ b/getexcited/getexcited_package/restart.py @@ -1,6 +1,6 @@ -#/usr/bin/python +# /usr/bin/python -''' +""" This function prepares restart input files for NEXMD. @@ -26,532 +26,805 @@ repeated information. An error file called 'delextra.out' is generated if one or more output files do not exist. -''' +""" -import numpy as np +import filecmp +import glob import os -import sys import re -import glob -import filecmp -import subprocess import shlex +import subprocess +import sys + +import numpy as np cwd = os.getcwd() + def extract(file): - num = re.findall('\d+$', file) + num = re.findall("\d+$", file) return (np.int(num[0]) if num else 0, file) -def restart(pathtopack,header): - print('Preparing restart input files for NEXMD.') +def restart(pathtopack, header): + print("Preparing restart input files for NEXMD.") ## Type of calculation and directory check ## - dynq = eval(input('Restart a single trajectory or an ensemble of trajectories?\nAnswer one [1] or ensemble [0]: ')) - if dynq not in [1,0]: - print('Answer must be 1 or 0.') + dynq = eval( + input( + "Restart a single trajectory or an ensemble of trajectories?\nAnswer one" + " [1] or ensemble [0]: " + ) + ) + if dynq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() - numRestarts = eval(input('Input the number of times you have restarted the trajectories? e.g 1 if this is the first time you have run a restart:')) - if numRestarts < 0 or not(isinstance(numRestarts,int)) : - print('Answer must be greater than 1 and an integer.') + numRestarts = eval( + input( + "Input the number of times you have restarted the trajectories? e.g 1 if" + " this is the first time you have run a restart:" + ) + ) + if numRestarts < 0 or not (isinstance(numRestarts, int)): + print("Answer must be greater than 1 and an integer.") sys.exit() - if dynq == 0: ## ensemble - NEXMDir = input('Ensemble directory [e.g. NEXMD]: ') + if dynq == 0: ## ensemble + NEXMDir = input("Ensemble directory [e.g. NEXMD]: ") if not os.path.exists(NEXMDir): - print('Path %s does not exist.' % (NEXMDir)) + print("Path %s does not exist." % (NEXMDir)) sys.exit() ## Check if NEXMD folders exist ## - NEXMDs = glob.glob('%s/NEXMD*/' % (NEXMDir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (NEXMDir)) NEXMDs.sort() if len(NEXMDs) == 0: - print('There are no NEXMD folders in %s.' % (NEXMDir)) + print("There are no NEXMD folders in %s." % (NEXMDir)) sys.exit() - if dynq == 1: ## single trajectory - NEXMDir = input('Single trajectory directory: ') + if dynq == 1: ## single trajectory + NEXMDir = input("Single trajectory directory: ") if not os.path.exists(NEXMDir): - print('Path %s does not exist.' % (NEXMDir)) + print("Path %s does not exist." % (NEXMDir)) sys.exit() ## Information from header ## - if not os.path.exists('%s/header' % (NEXMDir)): - print('Path %s/header does not exist.' % (NEXMDir)) + if not os.path.exists("%s/header" % (NEXMDir)): + print("Path %s/header does not exist." % (NEXMDir)) sys.exit() - header = header('%s/header' % (NEXMDir)) + header = header("%s/header" % (NEXMDir)) ## Check output data ## if header.out_data_steps == 0: - print('No data have been printed to files because out_data_steps = 0 in %s/header.' % (NEXMDir)) + print( + "No data have been printed to files because out_data_steps = 0 in" + " %s/header." % (NEXMDir) + ) sys.exit() ## Redefine number of quantum steps per classical step if set to 0 in header ## if header.n_quant_steps == 0: header.n_quant_steps = 1 -## Figure out what this part is about - print('Currently, trajectories are set to run for %d classical steps with a time-step of %.2f fs.\nThis is a total of %.2f fs.' % (header.n_class_steps,header.time_step,header.n_class_steps*header.time_step)) - tsmaxq = eval(input('Keep this trajectory length? Answer yes [1] or no [0]: ')) - if tsmaxq not in [1,0]: - print('Answer must be 1 or 0.') + ## Figure out what this part is about + print( + "Currently, trajectories are set to run for %d classical steps with a time-step" + " of %.2f fs.\nThis is a total of %.2f fs." + % ( + header.n_class_steps, + header.time_step, + header.n_class_steps * header.time_step, + ) + ) + tsmaxq = eval(input("Keep this trajectory length? Answer yes [1] or no [0]: ")) + if tsmaxq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if tsmaxq == 0: - ntsmax = eval(input('Enter new number of classical time-steps: ')) + ntsmax = eval(input("Enter new number of classical time-steps: ")) if isinstance(ntsmax, int) == False: - print('Answer must be integer.') + print("Answer must be integer.") sys.exit() if ntsmax <= header.n_class_steps: - print('Answer must be greater than or equal to the previous number of classical steps used, which was %d.\nTo reduce number of classical steps past %d, simply change n_class_steps in header.' % (header.n_class_steps,header.n_class_steps)) + print( + "Answer must be greater than or equal to the previous number of" + " classical steps used, which was %d.\nTo reduce number of classical" + " steps past %d, simply change n_class_steps in header." + % (header.n_class_steps, header.n_class_steps) + ) sys.exit() - nheader = open('%s/nheader' % (NEXMDir),'w') - headerFile = open('%s/header' % (NEXMDir)) #Changed - for line in headerFile: #Changed - if 'n_class_steps' in line: - nheader.write(' n_class_steps=%d, ! number of classical steps [1]\n' % (ntsmax)) + nheader = open("%s/nheader" % (NEXMDir), "w") + headerFile = open("%s/header" % (NEXMDir)) # Changed + for line in headerFile: # Changed + if "n_class_steps" in line: + nheader.write( + " n_class_steps=%d, ! number of classical steps [1]\n" % (ntsmax) + ) else: nheader.write(line) - headerFile.close() # Changed + headerFile.close() # Changed nheader.close() header.n_class_steps = ntsmax - os.rename('%s/nheader' % (NEXMDir), '%s/header' % (NEXMDir)) - + os.rename("%s/nheader" % (NEXMDir), "%s/header" % (NEXMDir)) + ## Choose random seeds ## - if dynq == 0: ## ensemble + if dynq == 0: ## ensemble ntraj = 0 for NEXMD in NEXMDs: - if not os.path.exists('%s/dirlist1' % (NEXMD)): - print('Path %sdirlist1 does not exist.' % (NEXMD)) + if not os.path.exists("%s/dirlist1" % (NEXMD)): + print("Path %sdirlist1 does not exist." % (NEXMD)) sys.exit() - data = np.int_(np.genfromtxt('%s/dirlist1' % (NEXMD))) - if isinstance(data,int) == True: + data = np.int_(np.genfromtxt("%s/dirlist1" % (NEXMD))) + if isinstance(data, int) == True: data = np.array([data]) ntraj += len(data) - if dynq == 1: ## single trajectory + if dynq == 1: ## single trajectory ntraj = 1 - randq = eval(input('New random seeds? Answer yes [1] or no [0]: ')) - if randq not in [1,0]: - print('Answer must be 1 or 0.') + randq = eval(input("New random seeds? Answer yes [1] or no [0]: ")) + if randq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() - data = glob.glob('%s/rseedslist*' % (NEXMDir)) + data = glob.glob("%s/rseedslist*" % (NEXMDir)) if len(data) == 0: - print('There are no rseedslists in %s.' % (NEXMDir)) + print("There are no rseedslists in %s." % (NEXMDir)) sys.exit() - maxdir = np.int(extract(max(data,key = extract))[0]) + maxdir = np.int(extract(max(data, key=extract))[0]) if randq == 0: - rseeds = input('Path to random-seeds list (** must be different from past random seeds **): ') + rseeds = input( + "Path to random-seeds list (** must be different from past random seeds" + " **): " + ) if not os.path.exists(rseeds): - print('Path %s does not exist.' % (rseeds)) + print("Path %s does not exist." % (rseeds)) sys.exit() - rseeds = np.int_(np.genfromtxt('%s' % (rseeds))) - if isinstance(rseeds,int) == True: + rseeds = np.int_(np.genfromtxt("%s" % (rseeds))) + if isinstance(rseeds, int) == True: rseeds = np.array([rseeds]) lenrseeds = len(rseeds) if lenrseeds < ntraj: - print('Length of random-seeds list must be equal to or greater than the number of trajectories.\nUser inputted a random-seeds list of length %d, while the number of trajectories is %d.' % (lenrseeds,ntraj)) + print( + "Length of random-seeds list must be equal to or greater than the" + " number of trajectories.\nUser inputted a random-seeds list of length" + " %d, while the number of trajectories is %d." % (lenrseeds, ntraj) + ) sys.exit() for rseed in rseeds: if rseed < 0: - print('A negative random seed was detected, %d.\nWithin the getexcited_package, a negative seed is assigned to a trajectory that could not be prepared due to some problem.' % (rseed)) + print( + "A negative random seed was detected, %d.\nWithin the" + " getexcited_package, a negative seed is assigned to a trajectory" + " that could not be prepared due to some problem." % (rseed) + ) sys.exit() ## Prepare NEXMD restart input files ## if dynq == 0: - print('Searching for incomplete trajectories and preparing input files.') - #header = open('%s/header' % (NEXMDir),'r') - #header = header.readlines() - rseedslist = open('%s/rseedslist%d' % (NEXMDir,maxdir + 1),'w') - error = open('%s/restart.err' % (cwd),'w') + print("Searching for incomplete trajectories and preparing input files.") + # header = open('%s/header' % (NEXMDir),'r') + # header = header.readlines() + rseedslist = open("%s/rseedslist%d" % (NEXMDir, maxdir + 1), "w") + error = open("%s/restart.err" % (cwd), "w") rtimes = np.array([]) rstflag = 0 traj = 0 for NEXMD in NEXMDs: - dirlist1 = np.int_(np.genfromtxt('%s/dirlist1' % (NEXMD))) - if isinstance(dirlist1,int) == True: + dirlist1 = np.int_(np.genfromtxt("%s/dirlist1" % (NEXMD))) + if isinstance(dirlist1, int) == True: dirlist1 = np.array([dirlist1]) - dirlist = open('%s/dirlist' % (NEXMD),'w') + dirlist = open("%s/dirlist" % (NEXMD), "w") for dir in dirlist1: - if not os.path.exists('%s/%04d/energy-ev.out' % (NEXMD,dir)): - print('Path %s%04d/energy-ev.out does not exist.' % (NEXMD,dir), file=error) - print('%d' % (-123456789), file=rseedslist) + if not os.path.exists("%s/%04d/energy-ev.out" % (NEXMD, dir)): + print( + "Path %s%04d/energy-ev.out does not exist." % (NEXMD, dir), + file=error, + ) + print("%d" % (-123456789), file=rseedslist) rstflag = 1 traj += 1 continue - data = open('%s/%04d/energy-ev.out' % (NEXMD,dir),'r') + data = open("%s/%04d/energy-ev.out" % (NEXMD, dir), "r") data = data.readlines() tsteps = len(data) - 2 if tsteps != header.n_class_steps: - if not os.path.exists('%s/%04d/restart.out' % (NEXMD,dir)): - print('Path %s/%04d/restart.out does not exist.' % (NEXMD,dir), file=error) - print('%d' % (-123456789), file=rseedslist) + if not os.path.exists("%s/%04d/restart.out" % (NEXMD, dir)): + print( + "Path %s/%04d/restart.out does not exist." % (NEXMD, dir), + file=error, + ) + print("%d" % (-123456789), file=rseedslist) rstflag = 1 traj += 1 continue - data = open('%s/%04d/restart.out' % (NEXMD,dir),'r') + data = open("%s/%04d/restart.out" % (NEXMD, dir), "r") data = data.readlines() index = 0 array = np.array([]) for line in data: - if 'time' in line: - time = np.around(np.float(line.split()[-1]), decimals = 3) - if 'State' in line: + if "time" in line: + time = np.around(np.float(line.split()[-1]), decimals=3) + if "State" in line: state = np.int(line.split()[-1]) - if 'Seed' in line: + if "Seed" in line: rseed = np.int(line.split()[-1]) - if '$COORD' in line: - array = np.append(array,index) - if '$ENDCOORD' in line: - array = np.append(array,index) - if '$VELOC' in line: - array = np.append(array,index) - if '$ENDVELOC' in line: - array = np.append(array,index) - if '$COEFF' in line: - array = np.append(array,index) - if '$ENDCOEFF' in line: - array = np.append(array,index) + if "&endcoord" in line: + array = np.append(array, index) + if "$ENDCOORD" in line: + array = np.append(array, index) + if "$VELOC" in line: + array = np.append(array, index) + if "$ENDVELOC" in line: + array = np.append(array, index) + if "$COEFF" in line: + array = np.append(array, index) + if "$ENDCOEFF" in line: + array = np.append(array, index) index += 1 array = np.int_(array) if len(array) != 6: - print('Path %s%04d/restart.out is incomplete.' % (NEXMD,dir), file=error) - print('%d' % (-123456789), file=rseedslist) + print( + "Path %s%04d/restart.out is incomplete." % (NEXMD, dir), + file=error, + ) + print("%d" % (-123456789), file=rseedslist) rstflag = 1 traj += 1 continue - coords = data[array[0]:array[1]+1:1] - velocs = data[array[2]:array[3]+1:1] - coeffs = data[array[4]:array[5]+1:1] + coords = data[array[0] : array[1] + 1 : 1] + velocs = data[array[2] : array[3] + 1 : 1] + coeffs = data[array[4] : array[5] + 1 : 1] ## Start renormalize coefficients ## ncoeffs = np.zeros(((len(coeffs) - 2), 2)) index = 0 - for line in coeffs[1:len(coeffs) - 1:1]: + for line in coeffs[1 : len(coeffs) - 1 : 1]: ncoeffs[index] = np.float_(line.split()) index += 1 - if np.sum(ncoeffs[:,0]) != 0: - ncoeffs[:,0] = ncoeffs[:,0]/np.sum(ncoeffs[:,0]) + if np.sum(ncoeffs[:, 0]) != 0: + ncoeffs[:, 0] = ncoeffs[:, 0] / np.sum(ncoeffs[:, 0]) ## Find the maximum view file ## - data = glob.glob('%s/%04d/view*' % (NEXMD,dir)) - data = [ x[:-10] for x in data ] + data = glob.glob("%s/%04d/view*" % (NEXMD, dir)) + data = [x[:-10] for x in data] if len(data) != 0: - maxview = np.int(extract(max(data,key = extract))[0]) + maxview = np.int(extract(max(data, key=extract))[0]) else: maxview = 0 ## Make new input file ## - os.rename('%s/%04d/input.ceon' % (NEXMD,dir), '%s/%04d/input%d.ceon' % (NEXMD,dir,numRestarts)) - inputfile = open('%s/%04d/input.ceon' % (NEXMD,dir),'w') - headerFile = open('%s/header' % (NEXMDir)) #Changed - for line in headerFile: #Changed - if 'rnd_seed_flag' in line: - inputfile.write(' rnd_seed=%d, ! seed for the random number generator\n' % (rseed if randq == 1 else rseeds[traj])) + os.rename( + "%s/%04d/input.ceon" % (NEXMD, dir), + "%s/%04d/input%d.ceon" % (NEXMD, dir, numRestarts), + ) + inputfile = open("%s/%04d/input.ceon" % (NEXMD, dir), "w") + headerFile = open("%s/header" % (NEXMDir)) # Changed + for line in headerFile: # Changed + if "rnd_seed_flag" in line: + inputfile.write( + " rnd_seed=%d, ! seed for the random number" + " generator\n" % (rseed if randq == 1 else rseeds[traj]) + ) else: - if 'exc_state_init_flag' in line: - inputfile.write(' exc_state_init=%d, ! initial excited state (0 - ground state) [0]\n' % (state)) + if "exc_state_init_flag" in line: + inputfile.write( + " exc_state_init=%d, ! initial excited state (0 -" + " ground state) [0]\n" % (state) + ) else: - if 'time_init' in line: - inputfile.write(' time_init=%.1f, ! initial time, fs [0.00]\n' % (time)) + if "time_init" in line: + inputfile.write( + " time_init=%.1f, ! initial time, fs [0.00]\n" + % (time) + ) else: - if 'n_class_steps' in line: - inputfile.write(' n_class_steps=%d, ! number of classical steps [1]\n' % (np.int(header.n_class_steps-time/header.time_step))) + if "n_class_steps" in line: + inputfile.write( + " n_class_steps=%d, ! number of classical" + " steps [1]\n" + % ( + np.int( + header.n_class_steps + - time / header.time_step + ) + ) + ) ####inputfile.write(' n_class_steps=%d, ! number of classical steps [1]\n' % (np.int(header.n_class_steps-time/header.time_step))) else: - if 'out_count_init' in line: - inputfile.write(' out_count_init=%d, ! initial count for output files [0]\n' % (maxview)) + if "out_count_init" in line: + inputfile.write( + " out_count_init=%d, ! initial count" + " for output files [0]\n" % (maxview) + ) else: - if 'nucl_coord_veloc_flag' in line: + if "nucl_coord_veloc_flag" in line: for line in coords: inputfile.write(line) - inputfile.write('\n') + inputfile.write("\n") for line in velocs: inputfile.write(line) else: - if 'quant_amp_phase_flag' in line: - inputfile.write('$coeff\n') + if "quant_amp_phase_flag" in line: + inputfile.write("$coeff\n") for line in ncoeffs: - inputfile.write(' %.10f %.10f\n' % (line[0],line[1])) - inputfile.write('$endcoeff\n') + inputfile.write( + " %.10f %.10f\n" + % (line[0], line[1]) + ) + inputfile.write("$endcoeff\n") else: inputfile.write(line) - headerFile.close() # Changed - rtimes = np.append(rtimes,time) - print('%04d' % (dir), file=dirlist) - print('%s%04d' % (NEXMD,dir)) - print('%d' % (rseed if randq == 1 else rseeds[traj]), file=rseedslist) + headerFile.close() # Changed + rtimes = np.append(rtimes, time) + print("%04d" % (dir), file=dirlist) + print("%s%04d" % (NEXMD, dir)) + print( + "%d" % (rseed if randq == 1 else rseeds[traj]), file=rseedslist + ) else: - print('%d' % (-123456789), file=rseedslist) + print("%d" % (-123456789), file=rseedslist) traj += 1 dirlist.close() if rstflag == 1: - print('One or more trajectories cannot be restarted, check restart.err.') + print("One or more trajectories cannot be restarted, check restart.err.") else: - os.remove('%s/restart.err' % (cwd)) + os.remove("%s/restart.err" % (cwd)) - if dynq == 1: ## single trajectory - print('Searching for incomplete trajectory and preparing input file.') - #header = open('%s/header' % (NEXMDir),'r') - #header = header.readlines() - rseedslist = open('%s/rseedslist%d' % (NEXMDir,maxdir + 1),'w') + if dynq == 1: ## single trajectory + print("Searching for incomplete trajectory and preparing input file.") + # header = open('%s/header' % (NEXMDir),'r') + # header = header.readlines() + rseedslist = open("%s/rseedslist%d" % (NEXMDir, maxdir + 1), "w") rtimes = np.array([]) rstflag = 0 traj = 0 - if not os.path.exists('%s/energy-ev.out' % (NEXMDir)): - print('Path %s/energy-ev.out does not exist.' % (NEXMDir)) - os.remove('%s/rseedslist%d' % (NEXMDir, maxdir + 1)) + if not os.path.exists("%s/energy-ev.out" % (NEXMDir)): + print("Path %s/energy-ev.out does not exist." % (NEXMDir)) + os.remove("%s/rseedslist%d" % (NEXMDir, maxdir + 1)) sys.exit() - data = open('%s/energy-ev.out' % (NEXMDir),'r') + data = open("%s/energy-ev.out" % (NEXMDir), "r") data = data.readlines() tsteps = len(data) - 2 if tsteps != header.n_class_steps: - if not os.path.exists('%s/restart.out' % (NEXMDir)): - print('Path %s/restart.out does not exist.' % (NEXMDir), file=error) - os.remove('%s/rseedslist%d' % (NEXMDir, maxdir + 1)) + if not os.path.exists("%s/restart.out" % (NEXMDir)): + print("Path %s/restart.out does not exist." % (NEXMDir), file=error) + os.remove("%s/rseedslist%d" % (NEXMDir, maxdir + 1)) sys.exit() - data = open('%s/restart.out' % (NEXMDir),'r') + data = open("%s/restart.out" % (NEXMDir), "r") data = data.readlines() index = 0 array = np.array([]) for line in data: - if 'time' in line: - time = np.around(np.float(line.split()[-1]), decimals = 3) - if 'State' in line: + if "time" in line: + time = np.around(np.float(line.split()[-1]), decimals=3) + if "State" in line: state = np.int(line.split()[-1]) - if 'Seed' in line: + if "Seed" in line: rseed = np.int(line.split()[-1]) - if '$COORD' in line: - array = np.append(array,index) - if '$ENDCOORD' in line: - array = np.append(array,index) - if '$VELOC' in line: - array = np.append(array,index) - if '$ENDVELOC' in line: - array = np.append(array,index) - if '$COEFF' in line: - array = np.append(array,index) - if '$ENDCOEFF' in line: - array = np.append(array,index) + if "&endcoord" in line: + array = np.append(array, index) + if "$ENDCOORD" in line: + array = np.append(array, index) + if "$VELOC" in line: + array = np.append(array, index) + if "$ENDVELOC" in line: + array = np.append(array, index) + if "$COEFF" in line: + array = np.append(array, index) + if "$ENDCOEFF" in line: + array = np.append(array, index) index += 1 array = np.int_(array) if len(array) != 6: - print('Path %s/restart.out is incomplete.' % (NEXMDir)) - os.remove('%s/rseedslist%d' % (NEXMDir, maxdir + 1)) - coords = data[array[0]:array[1]+1:1] - velocs = data[array[2]:array[3]+1:1] - coeffs = data[array[4]:array[5]+1:1] + print("Path %s/restart.out is incomplete." % (NEXMDir)) + os.remove("%s/rseedslist%d" % (NEXMDir, maxdir + 1)) + coords = data[array[0] : array[1] + 1 : 1] + velocs = data[array[2] : array[3] + 1 : 1] + coeffs = data[array[4] : array[5] + 1 : 1] ## Start renormalize coefficients ## ncoeffs = np.zeros(((len(coeffs) - 2), 2)) index = 0 - for line in coeffs[1:len(coeffs) - 1:1]: + for line in coeffs[1 : len(coeffs) - 1 : 1]: ncoeffs[index] = np.float_(line.split()) index += 1 - if np.sum(ncoeffs[:,0]) != 0: - ncoeffs[:,0] = ncoeffs[:,0]/np.sum(ncoeffs[:,0]) + if np.sum(ncoeffs[:, 0]) != 0: + ncoeffs[:, 0] = ncoeffs[:, 0] / np.sum(ncoeffs[:, 0]) ## Find the maximum view file ## - data = glob.glob('%s/view*' % (NEXMDir)) - data = [ x[:-10] for x in data ] + data = glob.glob("%s/view*" % (NEXMDir)) + data = [x[:-10] for x in data] if len(data) != 0: - maxview = np.int(extract(max(data,key = extract))[0]) + maxview = np.int(extract(max(data, key=extract))[0]) else: maxview = 0 ## Make new input file ## - os.rename('%s/input.ceon' % (NEXMDir), '%s/input%d.ceon' % (NEXMDir,numRestarts)) - inputfile = open('%s/input.ceon' % (NEXMDir),'w') - headerFile = open('%s/header' % (NEXMDir)) #Changed - for line in headerFile: #Changed - if 'rnd_seed_flag' in line: - inputfile.write(' rnd_seed=%d, ! seed for the random number generator\n' % (rseed if randq == 1 else rseeds[traj])) + os.rename( + "%s/input.ceon" % (NEXMDir), "%s/input%d.ceon" % (NEXMDir, numRestarts) + ) + inputfile = open("%s/input.ceon" % (NEXMDir), "w") + headerFile = open("%s/header" % (NEXMDir)) # Changed + for line in headerFile: # Changed + if "rnd_seed_flag" in line: + inputfile.write( + " rnd_seed=%d, ! seed for the random number generator\n" + % (rseed if randq == 1 else rseeds[traj]) + ) else: - if 'exc_state_init_flag' in line: - inputfile.write(' exc_state_init=%d, ! initial excited state (0 - ground state) [0]\n' % (state)) + if "exc_state_init_flag" in line: + inputfile.write( + " exc_state_init=%d, ! initial excited state (0 - ground" + " state) [0]\n" % (state) + ) else: - if 'time_init' in line: - inputfile.write(' time_init=%.1f, ! initial time, fs [0.00]\n' % (time)) + if "time_init" in line: + inputfile.write( + " time_init=%.1f, ! initial time, fs [0.00]\n" + % (time) + ) else: - if 'n_class_steps' in line: - inputfile.write(' n_class_steps=%d, ! number of classical steps [1]\n' % (np.int(header.n_class_steps-time/header.time_step))) + if "n_class_steps" in line: + inputfile.write( + " n_class_steps=%d, ! number of classical steps" + " [1]\n" + % ( + np.int( + header.n_class_steps + - time / header.time_step + ) + ) + ) else: - if 'out_count_init' in line: - inputfile.write(' out_count_init=%d, ! initial count for output files [0]\n' % (maxview)) + if "out_count_init" in line: + inputfile.write( + " out_count_init=%d, ! initial count for" + " output files [0]\n" % (maxview) + ) else: - if 'nucl_coord_veloc_flag' in line: + if "nucl_coord_veloc_flag" in line: for line in coords: inputfile.write(line) - inputfile.write('\n') + inputfile.write("\n") for line in velocs: inputfile.write(line) else: - if 'quant_amp_phase_flag' in line: - inputfile.write('&coeff\n') + if "quant_amp_phase_flag" in line: + inputfile.write("&coeff\n") for line in ncoeffs: - inputfile.write(' %.10f %.10f\n' % (line[0],line[1])) - inputfile.write('&endcoeff\n') + inputfile.write( + " %.10f %.10f\n" + % (line[0], line[1]) + ) + inputfile.write("&endcoeff\n") else: inputfile.write(line) - rtimes = np.append(rtimes,time) - headerFile.close() #Changed - print('%s' % (NEXMDir)) - print('%d' % (rseed if randq == 1 else rseeds[traj]), file=rseedslist) + rtimes = np.append(rtimes, time) + headerFile.close() # Changed + print("%s" % (NEXMDir)) + print("%d" % (rseed if randq == 1 else rseeds[traj]), file=rseedslist) else: - print('Trajectory has completed.') + print("Trajectory has completed.") sys.exit() traj += 1 - if filecmp.cmp('%s/rseedslist%d' % (NEXMDir,maxdir), '%s/rseedslist%d' % (NEXMDir,maxdir + 1)): - os.remove('%s/rseedslist%d' % (NEXMDir,maxdir + 1)) + if filecmp.cmp( + "%s/rseedslist%d" % (NEXMDir, maxdir), + "%s/rseedslist%d" % (NEXMDir, maxdir + 1), + ): + os.remove("%s/rseedslist%d" % (NEXMDir, maxdir + 1)) ## Determine whether or not to delete extraneous data in output files ## - contq = eval(input('Continue to delete extraneous data in output files? Answer yes [1] or no [0]: ')) - if contq not in [1,0]: - print('Answer must be 1 or 0.') + contq = eval( + input( + "Continue to delete extraneous data in output files? Answer yes [1] or no" + " [0]: " + ) + ) + if contq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if contq == 0: sys.exit() - print('Deleting extraneous data in output files. please wait ...') + print("Deleting extraneous data in output files. please wait ...") if header.bo_dynamics_flag == 0: - files = np.array(['coeff-n.out', 'energy-ev.out', 'nact.out', 'order.out', 'pes.out', 'temperature.out', 'transition-densities.out']) - ofiles = np.array(['hops.out', 'hops-trial.out']) + files = np.array( + [ + "coeff-n.out", + "energy-ev.out", + "nact.out", + "order.out", + "pes.out", + "temperature.out", + "transition-densities.out", + ] + ) + ofiles = np.array(["hops.out", "hops-trial.out"]) else: - files = np.array(['energy-ev.out', 'temperature.out', 'pes.out','transition-densities.out']) + files = np.array( + ["energy-ev.out", "temperature.out", "pes.out", "transition-densities.out"] + ) ofiles = np.array([]) ## Delete extraneous data ## - if dynq == 0: ## ensemble - error = open('%s/delextra.err' % (cwd),'w') + if dynq == 0: ## ensemble + error = open("%s/delextra.err" % (cwd), "w") rstflag = 0 traj = 0 for NEXMD in NEXMDs: - if os.stat('%s/%s/dirlist' % (cwd,NEXMD)).st_size == 0: + if os.stat("%s/%s/dirlist" % (cwd, NEXMD)).st_size == 0: continue else: - dirlist = np.int_(np.genfromtxt('%s/%s/dirlist' % (cwd,NEXMD))) - if(np.isscalar(dirlist)): - dirlist = np.array([dirlist],dtype=np.int64) + dirlist = np.int_(np.genfromtxt("%s/%s/dirlist" % (cwd, NEXMD))) + if np.isscalar(dirlist): + dirlist = np.array([dirlist], dtype=np.int64) else: - dirlist = np.array(dirlist,dtype=np.int64) + dirlist = np.array(dirlist, dtype=np.int64) for dir in dirlist: - if not os.path.exists('%s/%s/%04d' % (cwd,NEXMD,dir)): - print('Path %s/%s%04d does not exist.' % (cwd,NEXMD,dir), file=error) + if not os.path.exists("%s/%s/%04d" % (cwd, NEXMD, dir)): + print( + "Path %s/%s%04d does not exist." % (cwd, NEXMD, dir), file=error + ) rstflag = 1 traj += 1 continue - os.chdir('%s/%s/%04d' % (cwd,NEXMD,dir)) + os.chdir("%s/%s/%04d" % (cwd, NEXMD, dir)) for index in np.arange(len(files)): - if not os.path.exists('%s/%s/%04d/%s' % (cwd,NEXMD,dir,files[index])): - print('Path %s/%s%04d/%s does not exist.' % (cwd,NEXMD,dir,files[index]), file=error) + if not os.path.exists( + "%s/%s/%04d/%s" % (cwd, NEXMD, dir, files[index]) + ): + print( + "Path %s/%s%04d/%s does not exist." + % (cwd, NEXMD, dir, files[index]), + file=error, + ) rstflag = 1 continue ## Derivation of the following algorithm is provided at the end of this script ## - data = subprocess.check_output(['tail','-1','%s' % (files[index])]) - ltime = np.around(np.float(np.fromstring(data,dtype=float,sep=' ')[1 if index == 0 and header.bo_dynamics_flag == 0 else 0]), decimals = 3) - if rtimes[traj] > ltime + header.out_data_steps*header.time_step: - print('last time-step in', '%s/%s%04d/%s' % (cwd,NEXMD,dir,'restart.out'), 'exceeds last time-step in', '%s/%s%04d/%s' % (cwd,NEXMD,dir,files[index]), file=error) + data = subprocess.check_output( + ["tail", "-1", "%s" % (files[index])] + ) + ltime = np.around( + np.float( + np.fromstring(data, dtype=float, sep=" ")[ + 1 if index == 0 and header.bo_dynamics_flag == 0 else 0 + ] + ), + decimals=3, + ) + if rtimes[traj] > ltime + header.out_data_steps * header.time_step: + print( + "last time-step in", + "%s/%s%04d/%s" % (cwd, NEXMD, dir, "restart.out"), + "exceeds last time-step in", + "%s/%s%04d/%s" % (cwd, NEXMD, dir, files[index]), + file=error, + ) rstflag = 1 continue ncsteps = 0 - while rtimes[traj] + ncsteps*(header.out_data_steps*header.time_step) <= ltime: + while ( + rtimes[traj] + + ncsteps * (header.out_data_steps * header.time_step) + <= ltime + ): ncsteps += 1 - if header.moldyn_verbosity == 3 and index in [2,4]: - lctime = rtimes[traj] + (ncsteps - 1)*(header.out_data_steps*header.time_step) + if header.moldyn_verbosity == 3 and index in [2, 4]: + lctime = rtimes[traj] + (ncsteps - 1) * ( + header.out_data_steps * header.time_step + ) nqsteps = 0 - while lctime*header.n_quant_steps + nqsteps*header.time_step <= ltime*header.n_quant_steps: + while ( + lctime * header.n_quant_steps + nqsteps * header.time_step + <= ltime * header.n_quant_steps + ): nqsteps += 1 if ltime == lctime: - nlines = (ncsteps - 1)*(header.out_data_steps*(header.n_quant_steps - 1) + 1) + (nqsteps - 1) + 1 + nlines = ( + (ncsteps - 1) + * ( + header.out_data_steps * (header.n_quant_steps - 1) + + 1 + ) + + (nqsteps - 1) + + 1 + ) else: ncsteps = 0 - while rtimes[traj] + ncsteps*header.time_step <= ltime: + while rtimes[traj] + ncsteps * header.time_step <= ltime: ncsteps += 1 - nlines = (ncsteps - 1)*(header.out_data_steps*(header.n_quant_steps - 1) + 1) + (nqsteps - 1) - (ncsteps - 1) + 1 + nlines = ( + (ncsteps - 1) + * ( + header.out_data_steps * (header.n_quant_steps - 1) + + 1 + ) + + (nqsteps - 1) + - (ncsteps - 1) + + 1 + ) else: nlines = (ncsteps - 1) + 1 if header.bo_dynamics_flag == 0: - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack,(nlines - 1 if index in [2,3,5] else nlines),files[index]))) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % ( + pathtopack, + (nlines - 1 if index in [2, 3, 5] else nlines), + files[index], + ) + ) + ) else: - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack,(nlines - 1 if index == 1 else nlines),files[index]))) - os.rename('%s.restart' % (files[index]), '%s' % (files[index])) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % ( + pathtopack, + (nlines - 1 if index == 1 else nlines), + files[index], + ) + ) + ) + os.rename("%s.restart" % (files[index]), "%s" % (files[index])) for index in np.arange(len(ofiles)): - if not os.path.exists('%s/%s/%04d/%s' % (cwd,NEXMD,dir,ofiles[index])): - print('Path %s/%s%04d/%s does not exist.' % (cwd,NEXMD,dir,ofiles[index]), file=error) + if not os.path.exists( + "%s/%s/%04d/%s" % (cwd, NEXMD, dir, ofiles[index]) + ): + print( + "Path %s/%s%04d/%s does not exist." + % (cwd, NEXMD, dir, ofiles[index]), + file=error, + ) rstflag = 1 continue ltime = 1000000 nlines = 0 while ltime >= rtimes[traj]: - if(os.stat(ofiles[index]).st_size == 0): + if os.stat(ofiles[index]).st_size == 0: break - data = subprocess.check_output(['tail','%d' % (-(nlines + 1)),'%s' % (ofiles[index])]) - ltime = np.float(np.fromstring(data,dtype=float,sep=' ')[0]) + data = subprocess.check_output( + ["tail", "%d" % (-(nlines + 1)), "%s" % (ofiles[index])] + ) + ltime = np.float(np.fromstring(data, dtype=float, sep=" ")[0]) nlines += 1 - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack, nlines - 1, ofiles[index]))) - os.rename('%s.restart' % (ofiles[index]), '%s' % (ofiles[index])) - print('%s%04d' % (NEXMD,dir)) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % (pathtopack, nlines - 1, ofiles[index]) + ) + ) + os.rename("%s.restart" % (ofiles[index]), "%s" % (ofiles[index])) + print("%s%04d" % (NEXMD, dir)) traj += 1 if rstflag == 1: - print('One or more trajectories cannot be restarted properly, check delextra.err.') + print( + "One or more trajectories cannot be restarted properly, check" + " delextra.err." + ) else: - os.remove('%s/delextra.err' % (cwd)) - print('A total of %d trajectories have been prepared for restart.' % (traj)) + os.remove("%s/delextra.err" % (cwd)) + print("A total of %d trajectories have been prepared for restart." % (traj)) ## Delete extraneous data ## - if dynq == 1: ## single trajectory - error = open('%s/delextra.err' % (cwd),'w') + if dynq == 1: ## single trajectory + error = open("%s/delextra.err" % (cwd), "w") rstflag = 0 traj = 0 - os.chdir('%s/%s' % (cwd,NEXMDir)) + os.chdir("%s/%s" % (cwd, NEXMDir)) for index in np.arange(len(files)): - if not os.path.exists('%s/%s/%s' % (cwd,NEXMDir,files[index])): - print('%s/%s/%s does not exist.' % (cwd,NEXMDir,files[index]), file=error) + if not os.path.exists("%s/%s/%s" % (cwd, NEXMDir, files[index])): + print( + "%s/%s/%s does not exist." % (cwd, NEXMDir, files[index]), + file=error, + ) rstflag = 1 continue ## Derivation of the following algorithm is provided at the end of this script ## - data = subprocess.check_output(['tail','-1','%s' % (files[index])]) - ltime = np.around(np.float(np.fromstring(data,dtype=float,sep=' ')[1 if index == 0 and header.bo_dynamics_flag == 0 else 0]), decimals = 3) - if rtimes[traj] > ltime + header.out_data_steps*header.time_step: - print('last time-step in', '%s/%s/%s' % (cwd,NEXMDir,'restart.out'), 'exceeds last time-step in', '%s/%s/%s' % (cwd,NEXMDir,files[index]), file=error) + data = subprocess.check_output(["tail", "-1", "%s" % (files[index])]) + ltime = np.around( + np.float( + np.fromstring(data, dtype=float, sep=" ")[ + 1 if index == 0 and header.bo_dynamics_flag == 0 else 0 + ] + ), + decimals=3, + ) + if rtimes[traj] > ltime + header.out_data_steps * header.time_step: + print( + "last time-step in", + "%s/%s/%s" % (cwd, NEXMDir, "restart.out"), + "exceeds last time-step in", + "%s/%s/%s" % (cwd, NEXMDir, files[index]), + file=error, + ) rstflag = 1 continue ncsteps = 0 - while rtimes[traj] + ncsteps*(header.out_data_steps*header.time_step) <= ltime: + while ( + rtimes[traj] + ncsteps * (header.out_data_steps * header.time_step) + <= ltime + ): ncsteps += 1 - if header.moldyn_verbosity == 3 and index in [2,4]: - lctime = rtimes[traj] + (ncsteps - 1)*(header.out_data_steps*header.time_step) + if header.moldyn_verbosity == 3 and index in [2, 4]: + lctime = rtimes[traj] + (ncsteps - 1) * ( + header.out_data_steps * header.time_step + ) nqsteps = 0 - while lctime*header.n_quant_steps + nqsteps*header.time_step <= ltime*header.n_quant_steps: + while ( + lctime * header.n_quant_steps + nqsteps * header.time_step + <= ltime * header.n_quant_steps + ): nqsteps += 1 if ltime == lctime: - nlines = (ncsteps - 1)*(header.out_data_steps*(header.n_quant_steps - 1) + 1) + (nqsteps - 1) + 1 + nlines = ( + (ncsteps - 1) + * (header.out_data_steps * (header.n_quant_steps - 1) + 1) + + (nqsteps - 1) + + 1 + ) else: ncsteps = 0 - while rtimes[traj] + ncsteps*header.time_step <= ltime: + while rtimes[traj] + ncsteps * header.time_step <= ltime: ncsteps += 1 - nlines = (ncsteps - 1)*(header.out_data_steps*(header.n_quant_steps - 1) + 1) + (nqsteps - 1) - (ncsteps - 1) + 1 + nlines = ( + (ncsteps - 1) + * (header.out_data_steps * (header.n_quant_steps - 1) + 1) + + (nqsteps - 1) + - (ncsteps - 1) + + 1 + ) else: nlines = (ncsteps - 1) + 1 if header.bo_dynamics_flag == 0: - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack,(nlines - 1 if index in [2,3,5] else nlines),files[index]))) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % ( + pathtopack, + (nlines - 1 if index in [2, 3, 5] else nlines), + files[index], + ) + ) + ) else: - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack,(nlines - 1 if index == 1 else nlines),files[index]))) - os.rename('%s.restart' % (files[index]), '%s' % (files[index])) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % ( + pathtopack, + (nlines - 1 if index == 1 else nlines), + files[index], + ) + ) + ) + os.rename("%s.restart" % (files[index]), "%s" % (files[index])) for index in np.arange(len(ofiles)): - if not os.path.exists('%s/%s/%s' % (cwd,NEXMDir,ofiles[index])): - print('%s/%s/%s' % (cwd,NEXMDir,ofiles[index]), 'does not exist', file=error) + if not os.path.exists("%s/%s/%s" % (cwd, NEXMDir, ofiles[index])): + print( + "%s/%s/%s" % (cwd, NEXMDir, ofiles[index]), + "does not exist", + file=error, + ) rstflag = 1 continue ltime = 1000000 nlines = 0 while ltime >= rtimes[traj]: - if(os.stat(ofiles[index]).st_size == 0): - break - data = subprocess.check_output(['tail','%d' % (-(nlines + 1)),'%s' % (ofiles[index])]) - ltime = np.float(np.fromstring(data,dtype=float,sep=' ')[0]) + if os.stat(ofiles[index]).st_size == 0: + break + data = subprocess.check_output( + ["tail", "%d" % (-(nlines + 1)), "%s" % (ofiles[index])] + ) + ltime = np.float(np.fromstring(data, dtype=float, sep=" ")[0]) nlines += 1 - subprocess.call(shlex.split('sh %s/getexcited_package/cutdata.sh %d %s' % (pathtopack, nlines - 1, ofiles[index]))) - os.rename('%s.restart' % (ofiles[index]), '%s' % (ofiles[index])) - print('%s' % (NEXMDir)) + subprocess.call( + shlex.split( + "sh %s/getexcited_package/cutdata.sh %d %s" + % (pathtopack, nlines - 1, ofiles[index]) + ) + ) + os.rename("%s.restart" % (ofiles[index]), "%s" % (ofiles[index])) + print("%s" % (NEXMDir)) traj += 1 if rstflag == 1: - print('Trajectory cannot be restarted properly, check delextra.err.') + print("Trajectory cannot be restarted properly, check delextra.err.") else: - os.remove('%s/delextra.err' % (cwd)) - print('A total of %d trajectory has been prepared for restart.' % (traj)) + os.remove("%s/delextra.err" % (cwd)) + print("A total of %d trajectory has been prepared for restart." % (traj)) + -''' +""" header.time_step = time-step header.moldyn_verbosity = verbosity of molecular dynamics data @@ -587,4 +860,4 @@ def restart(pathtopack,header): total # of lines to delete = (ncsteps - 1)*(header.out_data_steps*(header.n_quant_steps- 1) + 1) + (nqsteps - 1) - (ncsteps - 1) + 1 *** see in code how to calculate ncsteps and nqsteps *** -''' +""" diff --git a/getexcited/getexcited_package/spcalc.py b/getexcited/getexcited_package/spcalc.py index 20762d6..6db4295 100644 --- a/getexcited/getexcited_package/spcalc.py +++ b/getexcited/getexcited_package/spcalc.py @@ -1,6 +1,6 @@ -#/usr/bin/python +# /usr/bin/python -''' +""" This function prepares input files for single-point calculations. @@ -17,212 +17,285 @@ NOTE: All NEXMD folders, inside the single-point directory, will be deleted if this function is completely executed! -''' +""" -import numpy as np -import random +import glob import os -import sys +import random import shutil -import glob +import sys -def spcalc(header): +import numpy as np - print('Preparing input files for single-point calculations.') + +def spcalc(header): + print("Preparing input files for single-point calculations.") ## Directory names ## - gsdir = input('Ground-state dynamics directory: ') + gsdir = input("Ground-state dynamics directory: ") if not os.path.exists(gsdir): - print('Path %s does not exist.' % (gsdir)) + print("Path %s does not exist." % (gsdir)) sys.exit() - outdir = input('Output directory [e.g. singlepoint]: ') + outdir = input("Output directory [e.g. singlepoint]: ") if not os.path.exists(outdir): - print('Path %s does not exist.' % (outdir)) + print("Path %s does not exist." % (outdir)) sys.exit() ## Delete previous NEXMD folders ## - NEXMDs = glob.glob('%s/NEXMD*/' % (outdir)) + NEXMDs = glob.glob("%s/NEXMD*/" % (outdir)) NEXMDs.sort() if len(NEXMDs) != 0: - contq = eval(input('** WARNING ** All NEXMD folders inside %s will be deleted!\nContinue? Answer yes [1] or no [0]: ' % (outdir))) - if contq not in [1,0]: - print('Answer must be 1 or 0.') + contq = eval( + input( + "** WARNING ** All NEXMD folders inside %s will be deleted!\nContinue?" + " Answer yes [1] or no [0]: " % (outdir) + ) + ) + if contq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if contq == 0: sys.exit() for NEXMD in NEXMDs: - print('Deleting', '%s' % (NEXMD)) + print("Deleting", "%s" % (NEXMD)) shutil.rmtree(NEXMD) ## Information from header ## - if not os.path.exists('%s/header' % (outdir)): - print('Path %s/header does not exist.' % (outdir)) + if not os.path.exists("%s/header" % (outdir)): + print("Path %s/header does not exist." % (outdir)) sys.exit() - header = header('%s/header' % (outdir)) + header = header("%s/header" % (outdir)) ## Check single-point calculation ## if header.n_class_steps != 0: - print('User must change n_class_steps in %s/header to 0 for single-point calculations.' % (outdir)) + print( + "User must change n_class_steps in %s/header to 0 for single-point" + " calculations." % (outdir) + ) sys.exit() ## Find geometries ## - if not os.path.exists('%s/coords.xyz' % (gsdir)): - print('path %s/coords.xyz does not exist.' % (gsdir)) + if not os.path.exists("%s/coords.xyz" % (gsdir)): + print("path %s/coords.xyz does not exist." % (gsdir)) sys.exit() - datac = open('%s/coords.xyz' % (gsdir),'r') + datac = open("%s/coords.xyz" % (gsdir), "r") datac = datac.readlines() lenc = len(datac) - if not os.path.exists('%s/velocity.out' % (gsdir)): - print('Path %s/velocity.out does not exist.' % (gsdir)) + if not os.path.exists("%s/velocity.out" % (gsdir)): + print("Path %s/velocity.out does not exist." % (gsdir)) sys.exit() - datav = open('%s/velocity.out' % (gsdir),'r') + datav = open("%s/velocity.out" % (gsdir), "r") datav = datav.readlines() lenv = len(datav) ncoords = 0 index = 0 arrayc = np.array([]) for line in datac: - if 'time' in line: + if "time" in line: if ncoords == 0: tinit = np.float(line.split()[-1]) else: time = np.float(line.split()[-1]) ncoords += 1 - arrayc = np.append(arrayc,index) + arrayc = np.append(arrayc, index) index += 1 - arrayc = np.append(arrayc,lenc + 1) + arrayc = np.append(arrayc, lenc + 1) arrayc = np.int_(arrayc) if ncoords == 0: - print('No coordinates were found.') + print("No coordinates were found.") sys.exit() if ncoords == 1: - print('Only initial coordinates, at %.2f fs, were found.' % (tinit)) + print("Only initial coordinates, at %.2f fs, were found." % (tinit)) sys.exit() if ncoords > 1: index = 0 arrayv = np.array([]) for line in datav: - if 'time' in line: - arrayv = np.append(arrayv,index) + if "time" in line: + arrayv = np.append(arrayv, index) index += 1 - arrayv = np.append(arrayv,lenv) + arrayv = np.append(arrayv, lenv) arrayv = np.int_(arrayv) - tinc = time/(ncoords - 1) - print('A total of %d coordinates files, ranging from %.2f to %.2f fs in increments of %.2f fs, were found.' % (ncoords,tinit,time,tinc)) + tinc = time / (ncoords - 1) + print( + "A total of %d coordinates files, ranging from %.2f to %.2f fs in increments of" + " %.2f fs, were found." % (ncoords, tinit, time, tinc) + ) ## Choose geometries ## - coords = eval(input('Enter requested range of the ground-state sampling by coordinate files and the number of single-point calculations.\nInput an array of the form [start, end, number]: ')) - if not isinstance(coords,list): - print('Input must be an array of the form [start, end, number].\nFor example, [1, 1000, 500] requests 500 coordinate files sampled from 1 to 1000.') + coords = eval( + input( + "Enter requested range of the ground-state sampling by coordinate files and" + " the number of single-point calculations.\nInput an array of the form" + " [start, end, number]: " + ) + ) + if not isinstance(coords, list): + print( + "Input must be an array of the form [start, end, number].\nFor example, [1," + " 1000, 500] requests 500 coordinate files sampled from 1 to 1000." + ) sys.exit() if len(coords) != 3: - print('Input must be an array with three elements of the form [start, end, number].\nFor example, [1, 1000, 500] requests 500 coordinate files sampled from 1 to 1000.') + print( + "Input must be an array with three elements of the form [start, end," + " number].\nFor example, [1, 1000, 500] requests 500 coordinate files" + " sampled from 1 to 1000." + ) sys.exit() index = 0 for i in coords: if isinstance(i, int) == False: - print('Element number %d of input array must be integer.\nUser inputted [%s, %s, %s], which is not allowed.' % (index + 1,coords[0],coords[1],coords[2])) + print( + "Element number %d of input array must be integer.\nUser inputted [%s," + " %s, %s], which is not allowed." + % (index + 1, coords[0], coords[1], coords[2]) + ) sys.exit() - if index in [0,1]: + if index in [0, 1]: if i not in np.arange(ncoords): - print('Element number %d of input array must be integer between 0 and %d.\nUser inputted [%d, %d, %d], which is not allowed.' % (index + 1,ncoords - 1,coords[0],coords[1],coords[2])) + print( + "Element number %d of input array must be integer between 0 and" + " %d.\nUser inputted [%d, %d, %d], which is not allowed." + % (index + 1, ncoords - 1, coords[0], coords[1], coords[2]) + ) sys.exit() else: - if i not in np.arange(1,ncoords + 1) and i not in -np.arange(1,ncoords + 1): - print('Element number %d of input array must be integer between 1 and %d.\nUser inputted [%d, %d, %d], which is not allowed.' % (index + 1,ncoords,coords[0],coords[1],coords[2])) + if i not in np.arange(1, ncoords + 1) and i not in -np.arange( + 1, ncoords + 1 + ): + print( + "Element number %d of input array must be integer between 1 and" + " %d.\nUser inputted [%d, %d, %d], which is not allowed." + % (index + 1, ncoords, coords[0], coords[1], coords[2]) + ) sys.exit() index += 1 if coords[0] > coords[1]: - print('second element of input array must be greater than first element.\nUser inputted [%d, %d, %d], which is not allowed.' % (coords[0],coords[1],coords[2])) + print( + "second element of input array must be greater than first element.\nUser" + " inputted [%d, %d, %d], which is not allowed." + % (coords[0], coords[1], coords[2]) + ) sys.exit() if (coords[1] - coords[0]) + 1 < np.abs(coords[2]): - print('Number of coordinate files requested must be less than or equal to the number of coordinate files in the sample.\nUser inputted [%d, %d, %d], which is not allowed.' % (coords[0],coords[1],coords[2])) + print( + "Number of coordinate files requested must be less than or equal to the" + " number of coordinate files in the sample.\nUser inputted [%d, %d, %d]," + " which is not allowed." % (coords[0], coords[1], coords[2]) + ) sys.exit() if coords[2] < 0: ntraj = np.abs(coords[2]) - coordsq = eval(input('You have requested %d randomly-selected coordinate files in the range %d to %d.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,coords[0],coords[1]))) + coordsq = eval( + input( + "You have requested %d randomly-selected coordinate files in the range" + " %d to %d.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, coords[0], coords[1]) + ) + ) else: - interval = np.int(np.ceil((coords[1] - coords[0] + 1)/np.float(coords[2]))) - if interval*coords[2] > (coords[1] - coords[0] + 1): + interval = np.int(np.ceil((coords[1] - coords[0] + 1) / np.float(coords[2]))) + if interval * coords[2] > (coords[1] - coords[0] + 1): interval = interval - 1 - ntraj = len(np.arange(coords[0],coords[1] + 1,interval)) + ntraj = len(np.arange(coords[0], coords[1] + 1, interval)) else: ntraj = coords[2] - coordsq = eval(input('You have requested %d evenly-spaced coordinate files in the range %d to %d.\nContinue? Answer yes [1] or no [0]: ' % (ntraj,coords[0],coords[1]))) - if coordsq not in [1,0]: - print('Answer must be 1 or 0.') + coordsq = eval( + input( + "You have requested %d evenly-spaced coordinate files in the range %d" + " to %d.\nContinue? Answer yes [1] or no [0]: " + % (ntraj, coords[0], coords[1]) + ) + ) + if coordsq not in [1, 0]: + print("Answer must be 1 or 0.") sys.exit() if coordsq == 0: sys.exit() ## Split geometries ## - split = eval(input('Number of single-point calculations per NEXMD folder [e.g. 100]: ')) + split = eval( + input("Number of single-point calculations per NEXMD folder [e.g. 100]: ") + ) if isinstance(split, int) == False: - print('Number of single-point calculations per NEXMD folder must be integer.') + print("Number of single-point calculations per NEXMD folder must be integer.") sys.exit() if split < 0: - print('Number of single-point calculations per NEXMD folder must be integer greater than zero.') + print( + "Number of single-point calculations per NEXMD folder must be integer" + " greater than zero." + ) sys.exit() - dirsplit = split*np.arange(1,np.ceil(np.float(ntraj)/split) + 1) + dirsplit = split * np.arange(1, np.ceil(np.float(ntraj) / split) + 1) dirsplit[-1] = coords[1] + 1 dirsplit = dirsplit.astype(np.int64) if coords[2] < 0: - dirsplit = np.split(np.sort(random.sample(np.arange(coords[0],coords[1]+1),ntraj)),dirsplit) + dirsplit = np.split( + np.sort(random.sample(np.arange(coords[0], coords[1] + 1), ntraj)), dirsplit + ) else: - dirsplit = np.split(np.arange(coords[0],coords[1]+1,interval),dirsplit) + dirsplit = np.split(np.arange(coords[0], coords[1] + 1, interval), dirsplit) ## Extract atomic numbers ## - if not os.path.exists('%s/restart.out' % (gsdir)): - print('Path %s/restart.out does not exist.' % (gsdir)) + if not os.path.exists("%s/restart.out" % (gsdir)): + print("Path %s/restart.out does not exist." % (gsdir)) sys.exit() - anum = open('%s/restart.out' % (gsdir),'r') + anum = open("%s/restart.out" % (gsdir), "r") anum = anum.readlines() top = None bottom = None index = 0 for line in anum: - if '$COORD' in line: + if "&coord" in line: top = index - if '$ENDCOORD' in line: + if "&endcoord" in line: bottom = index break index += 1 if top == None or bottom == None: - print('There is a problem with %s/restart.out.' % (gsdir)) + print("There is a problem with %s/restart.out." % (gsdir)) sys.exit() if isinstance(top, int) == True and isinstance(bottom, int) == True: - anum = [ line.split()[0] for line in anum[top+1:bottom:1] ] + anum = [line.split()[0] for line in anum[top + 1 : bottom : 1]] else: - print('There is a problem with %s/restart.out.' % (gsdir)) + print("There is a problem with %s/restart.out." % (gsdir)) sys.exit() ## Prepare single-point input files ## index = 0 - for NEXMD in np.arange(1,np.ceil(np.float(ntraj)/split)+1): - os.makedirs('%s/NEXMD%d' % (outdir,NEXMD)) - dirlist = open('%s/NEXMD%d/dirlist' % (outdir,NEXMD),'w') + for NEXMD in np.arange(1, np.ceil(np.float(ntraj) / split) + 1): + os.makedirs("%s/NEXMD%d" % (outdir, NEXMD)) + dirlist = open("%s/NEXMD%d/dirlist" % (outdir, NEXMD), "w") for dir in dirsplit[index]: - coords = datac[arrayc[dir]+1:arrayc[dir+1]-1:1] - velocs = datav[arrayv[dir]+2:arrayv[dir+1]-1:1] - os.makedirs('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) - inputfile = open('%s/NEXMD%d/%04d/input.ceon' % (outdir,NEXMD,dir),'w') + coords = datac[arrayc[dir] + 1 : arrayc[dir + 1] - 1 : 1] + velocs = datav[arrayv[dir] + 2 : arrayv[dir + 1] - 1 : 1] + os.makedirs("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) + inputfile = open("%s/NEXMD%d/%04d/input.ceon" % (outdir, NEXMD, dir), "w") for line in header.file: - if 'nucl_coord_veloc_flag' in line: - inputfile.write('&coord\n') + if "nucl_coord_veloc_flag" in line: + inputfile.write("&coord\n") aindex = 0 for line in coords: val = line.split() - inputfile.write('{:>6} {:>12} {:>12} {:>12}'.format(anum[aindex],val[1],val[2],val[3])) - inputfile.write('\n') + inputfile.write( + "{:>6} {:>12} {:>12} {:>12}".format( + anum[aindex], val[1], val[2], val[3] + ) + ) + inputfile.write("\n") aindex += 1 - inputfile.write('&endcoord\n\n&veloc\n') + inputfile.write("&endcoord\n\n&veloc\n") for line in velocs: inputfile.write(line) - inputfile.write('&endveloc\n') + inputfile.write("&endveloc\n") else: inputfile.write(line) - print('%04d' % (dir), file=dirlist) - print('%s/NEXMD%d/%04d' % (outdir,NEXMD,dir)) + print("%04d" % (dir), file=dirlist) + print("%s/NEXMD%d/%04d" % (outdir, NEXMD, dir)) dirlist.close() - shutil.copyfile('%s/NEXMD%d/dirlist' % (outdir,NEXMD), '%s/NEXMD%d/dirlist1' % (outdir,NEXMD)) + shutil.copyfile( + "%s/NEXMD%d/dirlist" % (outdir, NEXMD), + "%s/NEXMD%d/dirlist1" % (outdir, NEXMD), + ) index += 1