def get_energy_from_energy_file(self): """Get the energy from the energy file. For VASP neb, this is the last E0 energy from each OSZICAR, and this function should be used if vasprun.xml is corrupted or not available. Args: mydir <str>: Directory in which to look. Returns: <str>: all last E0 energies from OSZICAR files. """ myct = 0 mystr = "" while myct <= self.keywords['program_keys']['mast_neb_settings'][ 'images'] + 1: fullpath = os.path.join(self.keywords['name'], str(myct).zfill(2), "OSZICAR") if not os.path.isfile(fullpath): raise MASTError(self.__class__.__name__, "No OSZICAR file at %s" % fullpath) myosz = MASTFile(fullpath) mye0 = myosz.get_segment_from_last_line_match("E0", "E0=", "d E =") mye0 = float(mye0) mystr = mystr + "%3.3f" % mye0 + ';' myct = myct + 1 mystr = mystr[0:-1] #remove last semicolon return mystr
def create_input_files(self, datasets_dict): """Create independently looped input files. Args: datasets_dict <dict of list> Returns: createdfiles <list of str>: list of newly-created file names Creates an input file for each entry. """ ifname = self.keywords['inputfile'] dirstem = os.path.dirname(ifname) basename = os.path.basename(ifname).split('.')[0] createdfiles = list() if dirstem == "": dirstem = os.getcwd() dkeys = datasets_dict.keys() dkeys.sort() dct = 1 for didx in dkeys: newfile = MASTFile() newfile.data = list(datasets_dict[didx]) newname = "%s/loop_%s_%s.inp" % (dirstem, basename, str(dct).zfill(2)) newfile.to_file(newname) #createdfiles.append(os.path.basename(newname)) createdfiles.append(newname) dct = dct + 1 return createdfiles
def save_ingredient_files(listfilename="",whichfilename=""): """Save off old files from ingredients in $MAST_SCRATCH into $MAST_CONTROL Args: listfilename <str>: File name for the list of files to be saved whichfilename <str>: Short file name to be saved (identical file for all recipes) """ if listfilename == "": raise NameError("No list file name given.") if whichfilename == "": raise NameError("No file name given for which files to save.") mastcontrol=dirutil.get_mast_control_path() if not os.path.exists(os.path.join(mastcontrol,listfilename)): os.mkdir(os.path.join(mastcontrol,listfilename)) mastscratch=dirutil.get_mast_scratch_path() recipedirs=dirutil.immediate_subdirs(mastscratch) for recipedir in recipedirs: recipefulldir=os.path.join(mastscratch,recipedir) ingreddirs=dirutil.immediate_subdirs(recipefulldir) for ingreddir in ingreddirs: statfilepath = os.path.join(recipefulldir, ingreddir, whichfilename) if os.path.exists(statfilepath): statusfile = MASTFile(statfilepath) trydir = os.path.join(mastcontrol,listfilename,recipedir,ingreddir) if not os.path.exists(trydir): os.mkdir(trydir) statusfile.to_file("%s/%s" % (trydir, whichfilename)) return True
def create_input_files(self, datasets_dict): """Create independently looped input files. Args: datasets_dict <dict of list> Returns: createdfiles <list of str>: list of newly-created file names Creates an input file for each entry. """ ifname = self.keywords['inputfile'] dirstem = os.path.dirname(ifname) basename = os.path.basename(ifname).split('.')[0] createdfiles=list() if dirstem == "": dirstem = os.getcwd() dkeys = datasets_dict.keys() dkeys.sort() dct=1 for didx in dkeys: newfile = MASTFile() newfile.data = list(datasets_dict[didx]) newname="%s/loop_%s_%s.inp" % (dirstem, basename, str(dct).zfill(2)) newfile.to_file(newname) #createdfiles.append(os.path.basename(newname)) createdfiles.append(newname) dct=dct+1 return createdfiles
def zip_only_changed(): """Zip only the changed recipes""" mastcontrol = dirutil.get_mast_control_path() mastscratch = dirutil.get_mast_scratch_path() rchangefile = MASTFile(os.path.join(mastcontrol, "recipechangefile.txt")) #import shutil #shutil.rmtree(os.path.join(mastcontrol,"statusfiles")) totarfile = MASTFile() for ritem in rchangefile.data: if ":send" in ritem: recipe = ritem.split(":")[0] ingred = ritem.split(":")[1] totarfile.data.append( os.path.join("MAST/SCRATCH", recipe, ingred) + "\n") totarfile.data.append( os.path.join("MAST/SCRATCH", recipe, "status.txt") + "\n") elif ":archived" in ritem: recipe = ritem.split(":")[0] totarfile.data.append(os.path.join("MAST/ARCHIVE", recipe) + "\n") elif ":changed" in ritem: recipe = ritem.split(":")[0] totarfile.data.append( os.path.join("MAST/SCRATCH", recipe, "MAST_ERROR") + "\n") totarfile.data.append("MAST/CONTROL\n") totarfile.to_file(os.getcwd() + "/tarthis") return
def compare_old_and_new_change_status_files(): """Compare old and new status files from $MAST_CONTROL and $MAST_SCRATCH. Write an indication whether the status files have changed, not changed. Use "archived" if a recipe has been archived or is missing from SCRATCH for some other reason. """ rdict = dict() mastcontrol = dirutil.get_mast_control_path() mastscratch = dirutil.get_mast_scratch_path() recipedirs = dirutil.immediate_subdirs( os.path.join(mastcontrol, "changestatusfiles")) for recipedir in recipedirs: mystatus = "unknown" rdict[recipedir] = dict() changelist = list() if not os.path.exists(os.path.join(mastscratch, recipedir)): mystatus = "archived" else: ingreddirs = dirutil.immediate_subdirs( os.path.join(mastcontrol, "changestatusfiles", recipedir)) for ingreddir in ingreddirs: scratchstatusfile = MASTFile( os.path.join(mastscratch, recipedir, ingreddir, "change_status.txt")) controlstatusfile = MASTFile( os.path.join(mastcontrol, "changestatusfiles", recipedir, ingreddir, "change_status.txt")) if scratchstatusfile.data == controlstatusfile.data: mystatus = "unchanged" else: mystatus = "changed" rdict[recipedir][ingreddir] = "send" rdict[recipedir]["MAIN"] = mystatus return rdict
def save_old_change_status_files(): """Save off old status files from $MAST_SCRATCH into $MAST_CONTROL """ mastcontrol = dirutil.get_mast_control_path() if not os.path.exists(os.path.join(mastcontrol, "changestatusfiles")): os.mkdir(os.path.join(mastcontrol, "changestatusfiles")) mastscratch = dirutil.get_mast_scratch_path() recipedirs = dirutil.immediate_subdirs(mastscratch) for recipedir in recipedirs: ingreddirs = dirutil.immediate_subdirs( os.path.join(mastscratch, recipedir)) for ingreddir in ingreddirs: csfile = os.path.join(mastscratch, recipedir, ingreddir, "change_status.txt") if os.path.isfile(csfile): changestatusfile = MASTFile(csfile) trydir = os.path.join(mastcontrol, "changestatusfiles", recipedir) if not os.path.exists(trydir): os.mkdir(trydir) trydir2 = os.path.join(trydir, ingreddir) if not os.path.exists(trydir2): os.mkdir(trydir2) changestatusfile.to_file("%s/change_status.txt" % trydir2) return True
def write_my_dynamical_matrix_file(self, dyndict, mydir="", fname="DYNMAT"): """Write a dynamical matrix file based on a dictionary. Args: dyndict <dict>: Dictionary of dynmat (see read_my_dynmat) mydir <str>: Directory in which to write; use ingredient directory if null fname <str>: filename (default DYNMAT) """ if mydir == "": mydir = self.keywords['name'] dynwrite=MASTFile() dynwrite.data=list() firstline=str(dyndict['numspec']) + " " + str(dyndict['numatoms']) + " " + str(dyndict['numdisp']) + "\n" dynwrite.data.append(firstline) dynwrite.data.append(dyndict['massline']) atomlist=dyndict['atoms'].keys() atomlist.sort() for atom in atomlist: displist = dyndict['atoms'][atom].keys() displist.sort() for disp in displist: thirdline = str(atom) + " " + str(disp) + " " + dyndict['atoms'][atom][disp]['displine'] + "\n" dynwrite.data.append(thirdline) for line in dyndict['atoms'][atom][disp]['dynmat']: dynwrite.data.append(line) dynwrite.to_file(os.path.join(mydir, fname))
def write_my_dynmat_without_disp_or_mass(self, dyndict, mydir="", fname="DYNMAT"): """Write a dynamical matrix file without the displacement indicators 1, 2, 3 and without the masses line, and with first line having only the total number of displacements, for PHON. Args: dyndict <dict>: Dictionary of dynmat (see read_my_dynmat) mydir <str>: Directory in which to write; use ingredient directory if null fname <str>: filename (default DYNMAT) """ if mydir == "": mydir = self.keywords['name'] dynwrite=MASTFile() dynwrite.data=list() firstline=str(dyndict['numdisp']) + "\n" dynwrite.data.append(firstline) atomlist=dyndict['atoms'].keys() atomlist.sort() for atom in atomlist: displist = dyndict['atoms'][atom].keys() displist.sort() for disp in displist: thirdline = str(atom) + " " + dyndict['atoms'][atom][disp]['displine'] + "\n" dynwrite.data.append(thirdline) for line in dyndict['atoms'][atom][disp]['dynmat']: dynwrite.data.append(line) dynwrite.to_file(os.path.join(mydir, fname))
def is_complete(self): """Check for the existence of mast_complete_file (default), or for a certain string, given by mast_complete_search""" if not "mast_complete_file" in self.keywords["program_keys"].keys(): raise MASTError( self.__class__.__name__, "No completion file indicated by mast_complete_file keyword for %s. Cannot determine whether run is complete." % self.keywords["name"], ) return False checkfile = os.path.join(self.keywords["name"], self.keywords["program_keys"]["mast_complete_file"]) searchstr = None if "mast_complete_search" in self.keywords["program_keys"].keys(): searchstr = self.keywords["program_keys"]["mast_complete_search"] if searchstr == "None": searchstr = None if os.path.isfile(checkfile): if searchstr == None: return True else: tempopen = MASTFile(checkfile) mymatch = tempopen.get_last_line_match(searchstr) if mymatch == None: return False else: return True else: return False
def write_my_displacement_file(self, xdatdict, mydir="", fname="XDATCAR"): """Write a displacement file. Args: xdatdict <dict>: Dictionary of XDATCAR (see read_my_xdatcar) mydir <str>: Directory in which to write. Use ingredient directory if null. fname <str>: filename (default DYNMAT) """ if mydir == "": mydir = self.keywords['name'] xdatwrite=MASTFile() xdatwrite.data=list() xdatwrite.data.append(xdatdict['descline']) if not (xdatdict['scale'] == ""): xdatwrite.data.append(xdatdict['scale']) xdatwrite.data.append(xdatdict['latta']) xdatwrite.data.append(xdatdict['lattb']) xdatwrite.data.append(xdatdict['lattc']) xdatwrite.data.append(xdatdict['specline']) xdatwrite.data.append(xdatdict['numline']) if not (xdatdict['type'] == ""): xdatwrite.data.append(xdatdict['type']) configlist = xdatdict['configs'].keys() configlist.sort() for cfg in configlist: xdatwrite.data.append("Konfig=%1i\n" % cfg) xdatwrite.data.extend(xdatdict['configs'][cfg]) xdatwrite.to_file(os.path.join(mydir, fname))
def compare_old_and_new_status_files(): """Compare old and new status files from $MAST_CONTROL and $MAST_SCRATCH. Write an indication whether the status files have changed, not changed. Use "archived" if a recipe has been archived or is missing from SCRATCH for some other reason. """ rdict=dict() mastcontrol=dirutil.get_mast_control_path() mastscratch=dirutil.get_mast_scratch_path() recipedirs=dirutil.immediate_subdirs(os.path.join(mastcontrol,"statusfiles")) for recipedir in recipedirs: mystatus="unknown" rdict[recipedir]=dict() changelist=list() if not os.path.exists(os.path.join(mastscratch,recipedir)): mystatus="archived" else: scratchstatusfile = MASTFile(os.path.join(mastscratch,recipedir,"status.txt")) controlstatusfile = MASTFile(os.path.join(mastcontrol,"statusfiles",recipedir,"status.txt")) if scratchstatusfile.data == controlstatusfile.data: mystatus="unchanged" else: mystatus="changed" myidx=0 while myidx < len(scratchstatusfile.data): oldline = controlstatusfile.data[myidx] newline = scratchstatusfile.data[myidx] if "#" in oldline: pass else: ingred = oldline.split(":")[0].strip() oldstatus = oldline.split(":")[1].strip() newstatus = newline.split(":")[1].strip() if (oldstatus == "P") and (newstatus == "P"): rdict[recipedir][ingred]="AVOID" elif (oldstatus == "C") and (newstatus == "C"): rdict[recipedir][ingred]="AVOID" else: rdict[recipedir][ingred]="send" myidx = myidx + 1 rdict[recipedir]["MAIN"]=mystatus recipedirs=dirutil.immedate_subdirs(os.path.join(mastcontrol,"changestatusfiles")) for recipedir in recipedirs: ingreddirs=dirutil.immediate_subdirs(os.path.join(mastcontrol,"changestatusfiles",recipedir)) for ingreddir in ingreddirs: oldchangestatus=MASTFile(os.path.join(mastcontrol,"changestatusfiles",recipedir,ingreddir,"change_status.txt")) newchangestatus=MASTFile(os.path.join(mastscratch,recipedir,ingreddir,"change_status.txt")) if not (oldchangestatus.data == newchangestatus.data): rdict[recipedir]["MAIN"]="changed" rdict[recipedir][ingreddir]="send" rchangefile = MASTFile() for rdir in rdict.keys(): for key, value in rdict[rdir].iteritems(): rchangefile.data.append("%s:%s:%s\n" % (rdir,key,value)) rchangefile.to_file(os.path.join(mastcontrol,"recipechangefile.txt")) return True
def _vasp_neb_incar_modify(self): """Modify the INCAR to add the IMAGES tag back in. """ name=self.keywords['name'] images = str(self.keywords['program_keys']['mast_neb_settings']['images']) my_incar = MASTFile(self.keywords['name'] + "/INCAR") my_incar.data.append("IMAGES=%s\n" % images) my_incar.to_file(self.keywords['name'] + "/INCAR") return my_incar
def _vasp_neb_incar_modify(self): """Modify the INCAR to add the IMAGES tag back in. """ name = self.keywords['name'] images = str( self.keywords['program_keys']['mast_neb_settings']['images']) my_incar = MASTFile(self.keywords['name'] + "/INCAR") my_incar.data.append("IMAGES=%s\n" % images) my_incar.to_file(self.keywords['name'] + "/INCAR") return my_incar
def clear_submission_list(): """Clear all entries from the submission list at $MAST_CONTROL/submitlist """ submitlist = os.path.join(mast_control, "submitlist") if not os.path.isfile(submitlist): print "No submission list at %s" % submitlist return submitfile = MASTFile(submitlist) submitfile.data = list() submitfile.data.append("\n") submitfile.to_file(submitlist)
def clear_submission_list(): """Clear all entries from the submission list at $MAST_CONTROL/submitlist """ submitlist=os.path.join(mast_control, "submitlist") if not os.path.isfile(submitlist): print "No submission list at %s" % submitlist return submitfile=MASTFile(submitlist) submitfile.data=list() submitfile.data.append("\n") submitfile.to_file(submitlist)
def _phon_inphon_setup(self): """Set up the INPHON file.""" name=self.keywords['name'] myd = dict() myd = self._phon_inphon_get_non_mast_keywords() my_inphon = MASTFile() for key, value in myd.iteritems(): my_inphon.data.append(str(key) + "=" + str(value).upper() + "\n") if not ("NTYPES" in myd.keys()) and not ("MASS" in myd.keys()): [nline,massline] = self._phon_inphon_get_masses() my_inphon.data.append(nline + "\n") my_inphon.data.append(massline + "\n") my_inphon.to_file(name + "/INPHON") return
def _phon_inphon_setup(self): """Set up the INPHON file.""" name = self.keywords['name'] myd = dict() myd = self._phon_inphon_get_non_mast_keywords() my_inphon = MASTFile() for key, value in myd.iteritems(): my_inphon.data.append(str(key) + "=" + str(value).upper() + "\n") if not ("NTYPES" in myd.keys()) and not ("MASS" in myd.keys()): [nline, massline] = self._phon_inphon_get_masses() my_inphon.data.append(nline + "\n") my_inphon.data.append(massline + "\n") my_inphon.to_file(name + "/INPHON") return
def write_to_jobids_file(subentry, status): """Write the job id to a jobids file in the ingredient directory. Args: subentry <str>: Folder from which the job was submitted (ingredient folder) status <str>: Job submission response from queueing system """ jobid = extract_submitted_jobid(status) jpath = "%s/jobids" % subentry if not os.path.isfile(jpath): jfile = MASTFile() else: jfile = MASTFile(jpath) jfile.data.insert(0,str(jobid) + '\n') jfile.to_file(jpath) return
def save_old_status_files(): """Save off old status files from $MAST_SCRATCH into $MAST_CONTROL """ mastcontrol=dirutil.get_mast_control_path() if not os.path.exists(os.path.join(mastcontrol,"statusfiles")): os.mkdir(os.path.join(mastcontrol,"statusfiles")) mastscratch=dirutil.get_mast_scratch_path() recipedirs=dirutil.immediate_subdirs(mastscratch) for recipedir in recipedirs: statusfile = MASTFile(os.path.join(mastscratch,recipedir,"status.txt")) trydir = os.path.join(mastcontrol,"statusfiles",recipedir) if not os.path.exists(trydir): os.mkdir(trydir) statusfile.to_file("%s/status.txt" % trydir) return True
def compare_old_and_new_status_files(): """Compare old and new status files from $MAST_CONTROL and $MAST_SCRATCH. Write an indication whether the status files have changed, not changed. Use "archived" if a recipe has been archived or is missing from SCRATCH for some other reason. """ rdict = dict() mastcontrol = dirutil.get_mast_control_path() mastscratch = dirutil.get_mast_scratch_path() recipedirs = dirutil.immediate_subdirs( os.path.join(mastcontrol, "statusfiles")) for recipedir in recipedirs: mystatus = "unknown" rdict[recipedir] = dict() changelist = list() if not os.path.exists(os.path.join(mastscratch, recipedir)): mystatus = "archived" else: scratchstatusfile = MASTFile( os.path.join(mastscratch, recipedir, "status.txt")) controlstatusfile = MASTFile( os.path.join(mastcontrol, "statusfiles", recipedir, "status.txt")) if scratchstatusfile.data == controlstatusfile.data: mystatus = "unchanged" else: mystatus = "changed" myidx = 0 while myidx < len(scratchstatusfile.data): oldline = controlstatusfile.data[myidx] newline = scratchstatusfile.data[myidx] if "#" in oldline: pass else: ingred = oldline.split(":")[0].strip() oldstatus = oldline.split(":")[1].strip() newstatus = newline.split(":")[1].strip() if (oldstatus == "P") and (newstatus == "P"): rdict[recipedir][ingred] = "AVOID" elif (oldstatus == "C") and (newstatus == "C"): rdict[recipedir][ingred] = "AVOID" else: rdict[recipedir][ingred] = "send" myidx = myidx + 1 rdict[recipedir]["MAIN"] = mystatus return rdict
def save_old_status_files(): """Save off old status files from $MAST_SCRATCH into $MAST_CONTROL """ mastcontrol = dirutil.get_mast_control_path() if not os.path.exists(os.path.join(mastcontrol, "statusfiles")): os.mkdir(os.path.join(mastcontrol, "statusfiles")) mastscratch = dirutil.get_mast_scratch_path() recipedirs = dirutil.immediate_subdirs(mastscratch) for recipedir in recipedirs: statusfile = MASTFile( os.path.join(mastscratch, recipedir, "status.txt")) trydir = os.path.join(mastcontrol, "statusfiles", recipedir) if not os.path.exists(trydir): os.mkdir(trydir) statusfile.to_file("%s/status.txt" % trydir) return True
def write_to_submit_list(mydir): """Write an entry to the submission list in $MAST_CONTROL/submitlist Args: mydir <str>: Directory which includes submission script submit.sh for a single calculation to be submitted to the queue """ mast_control = dirutil.get_mast_control_path() #setting here instead of globally allows tests to run in isolated test_control folder submitlist=os.path.join(mast_control, "submitlist") if os.path.isfile(submitlist): submitfile=MASTFile(submitlist) else: submitfile=MASTFile() submitfile.data.append(mydir + "\n") submitfile.to_file(submitlist) return
def __init__(self, **kwargs): MASTObj.__init__(self, ALLOWED_KEYS, **kwargs) self.indeploop = "indeploop" self.loop_delim = "," self.loop_start = "(" self.loop_end = ")" self.baseinput = MASTFile(self.keywords['inputfile']) self.pegloop1 = "pegloop1" self.pegloop2 = "pegloop2"
def write_script(self, dirname="", fname=""): """Write the python input script, created from the *.inp input file. Args: dirname <str>: directory in which to write fname <str>: filename to write Returns: filename <str>: Full file name of the input script created. """ mylines = self.print_input_options() if dirname == "": dirname = os.getcwd() if fname == "": fname = 'input.py' filename = os.path.join(dirname, fname) inputpy = MASTFile() inputpy.data = mylines inputpy.to_file(filename) return filename
def merge_dictionaries_and_write_recipechangefile(): rdict_status = compare_old_and_new_status_files() rdict_change_status = compare_old_and_new_change_status_files() rdict_new_change_status = get_new_change_status_files() rdict_error_files = get_new_mast_error_files() rchangefile = MASTFile() rdict = dict() for recdir in rdict_status.keys(): if not recdir in rdict.keys(): rdict[recdir] = dict() for ingreddir in rdict_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_status[recdir][ingreddir] for recdir in rdict_change_status.keys(): if not recdir in rdict.keys(): rdict[recdir] = dict() for ingreddir in rdict_change_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_change_status[recdir][ ingreddir] else: if rdict_change_status[recdir][ingreddir] == "send": rdict[recdir][ingreddir] = "send" #update to send for recdir in rdict_new_change_status.keys(): if not recdir in rdict.keys(): rdict[recdir] = dict() for ingreddir in rdict_new_change_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_new_change_status[recdir][ ingreddir] else: if rdict_new_change_status[recdir][ingreddir] == "send": rdict[recdir][ingreddir] = "send" #update to send for recdir in rdict_error_files.keys(): if not recdir in rdict.keys(): rdict[recdir] = dict() for ingreddir in rdict_error_files[recdir].keys(): rdict[recdir][ingreddir] = rdict_error_files[recdir][ingreddir] for rdir in rdict.keys(): for key, value in rdict[rdir].iteritems(): rchangefile.data.append("%s:%s:%s\n" % (rdir, key, value)) mastcontrol = dirutil.get_mast_control_path() rchangefile.to_file(os.path.join(mastcontrol, "recipechangefile.txt")) return True
def _input_setup(self): """Set up the input.txt file. Note that this is not the recipe input file, it is the ingredient input file. """ name=self.keywords['name'] myd = dict() myd = self._input_get_non_mast_keywords() my_input = MASTFile() if not 'mast_delimiter' in self.keywords['program_keys'].keys(): delim = " " else: delim = self.keywords['program_keys']['mast_delimiter'] if delim == "None": delim = " " for key, value in myd.iteritems(): my_input.data.append(str(key) + delim + str(value) + "\n") my_input.to_file(name + "/input.txt") self._copy_over_any_files() return
def _phon_inphon_get_allowed_keywords(self, allowedpath): """Get allowed PHON keywords. Args: allowedpath <str>: file path for allowed PHON keywords """ allowed = MASTFile(allowedpath) allowed_list = list() for entry in allowed.data: allowed_list.append(entry.strip()) return allowed_list
def _input_setup(self): """Set up the input.txt file. Note that this is not the recipe input file, it is the ingredient input file. """ name = self.keywords["name"] myd = dict() myd = self._input_get_non_mast_keywords() my_input = MASTFile() if not "mast_delimiter" in self.keywords["program_keys"].keys(): delim = " " else: delim = self.keywords["program_keys"]["mast_delimiter"] if delim == "None": delim = " " for key, value in myd.iteritems(): my_input.data.append(str(key) + delim + str(value) + "\n") my_input.to_file(name + "/input.txt") self._copy_over_any_files() return
def _lammps_input_get_allowed_keywords(self, allowedpath): """Get allowed vasp keywords. Args: allowedpath <str>: file path for allowed lammps keywords """ allowed = MASTFile(allowedpath) allowed_list = list() for entry in allowed.data: allowed_list.append(entry.strip()) return allowed_list
def merge_dictionaries_and_write_recipechangefile(): rdict_status = compare_old_and_new_status_files() rdict_change_status = compare_old_and_new_change_status_files() rdict_new_change_status = get_new_change_status_files() rdict_error_files = get_new_mast_error_files() rchangefile = MASTFile() rdict=dict() for recdir in rdict_status.keys(): if not recdir in rdict.keys(): rdict[recdir]=dict() for ingreddir in rdict_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_status[recdir][ingreddir] for recdir in rdict_change_status.keys(): if not recdir in rdict.keys(): rdict[recdir]=dict() for ingreddir in rdict_change_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_change_status[recdir][ingreddir] else: if rdict_change_status[recdir][ingreddir] == "send": rdict[recdir][ingreddir] = "send" #update to send for recdir in rdict_new_change_status.keys(): if not recdir in rdict.keys(): rdict[recdir]=dict() for ingreddir in rdict_new_change_status[recdir].keys(): if not ingreddir in rdict[recdir].keys(): rdict[recdir][ingreddir] = rdict_new_change_status[recdir][ingreddir] else: if rdict_new_change_status[recdir][ingreddir] == "send": rdict[recdir][ingreddir] = "send" #update to send for recdir in rdict_error_files.keys(): if not recdir in rdict.keys(): rdict[recdir]=dict() for ingreddir in rdict_error_files[recdir].keys(): rdict[recdir][ingreddir] = rdict_error_files[recdir][ingreddir] for rdir in rdict.keys(): for key, value in rdict[rdir].iteritems(): rchangefile.data.append("%s:%s:%s\n" % (rdir,key,value)) mastcontrol=dirutil.get_mast_control_path() rchangefile.to_file(os.path.join(mastcontrol,"recipechangefile.txt")) return True
def get_final_pressure(self): """Get the final pressure. For VASP, this is the last pressure line from the OUTCAR. Args: mydir <str>: Directory in which to look. Returns: <float>: last pressure from OUTCAR, in kB """ fullpath=os.path.join(self.keywords['name'], "OUTCAR") if not os.path.isfile(fullpath): raise MASTError(self.__class__.__name__, "No OUTCAR file at %s" % self.keywords['name']) myoutcar = MASTFile(fullpath) mypress = myoutcar.get_segment_from_last_line_match("pressure", "external pressure =","kB Pullay stress =") mypressfloat="" try: mypressfloat=float(mypress) except TypeError: self.logger.error("Failed to log pressure %s" % str(mypress)) return mypressfloat
def zip_only_changed(): """Zip only the changed recipes""" mastcontrol=dirutil.get_mast_control_path() mastscratch=dirutil.get_mast_scratch_path() rchangefile = MASTFile(os.path.join(mastcontrol,"recipechangefile.txt")) #import shutil #shutil.rmtree(os.path.join(mastcontrol,"statusfiles")) totarfile = MASTFile() for ritem in rchangefile.data: if ":send" in ritem: recipe=ritem.split(":")[0] ingred=ritem.split(":")[1] totarfile.data.append(os.path.join("MAST/SCRATCH",recipe,ingred)+"\n") totarfile.data.append(os.path.join("MAST/SCRATCH",recipe,"status.txt") + "\n") elif ":archived" in ritem: recipe=ritem.split(":")[0] totarfile.data.append(os.path.join("MAST/ARCHIVE",recipe)+"\n") totarfile.data.append("MAST/CONTROL\n") totarfile.to_file(os.getcwd() + "/tarthis") return
def get_energy_from_energy_file(self): """Get the energy from the energy file. For VASP, this is the last E0 energy from OSZICAR, and this function should be used if vasprun.xml is corrupted or not available. Args: mydir <str>: Directory in which to look. Returns: <float>: last E0 energy from OSZICAR """ fullpath=os.path.join(self.keywords['name'], "OSZICAR") if not os.path.isfile(fullpath): raise MASTError(self.__class__.__name__, "No OSZICAR file at %s" % self.keywords['name']) myosz = MASTFile(fullpath) mye0 = myosz.get_segment_from_last_line_match("E0", "E0=","d E =") mye0float="" try: mye0float=float(mye0) except TypeError: self.logger.error("Failed to log energy %s" % str(mye0)) return mye0float
def save_old_change_status_files(): """Save off old status files from $MAST_SCRATCH into $MAST_CONTROL """ mastcontrol=dirutil.get_mast_control_path() if not os.path.exists(os.path.join(mastcontrol,"changestatusfiles")): os.mkdir(os.path.join(mastcontrol,"changestatusfiles")) mastscratch=dirutil.get_mast_scratch_path() recipedirs=dirutil.immediate_subdirs(mastscratch) for recipedir in recipedirs: ingreddirs=dirutil.immediate_subdirs(os.path.join(mastscratch,recipedir)) for ingreddir in ingreddirs: csfile = os.path.join(mastscratch,recipedir,ingreddir,"change_status.txt") if os.path.isfile(csfile): changestatusfile = MASTFile(csfile) trydir = os.path.join(mastcontrol,"changestatusfiles",recipedir) if not os.path.exists(trydir): os.mkdir(trydir) trydir2 = os.path.join(trydir, ingreddir) if not os.path.exists(trydir2): os.mkdir(trydir2) changestatusfile.to_file("%s/change_status.txt" % trydir2) return True
def get_job_status_from_queue_snapshot(ingpath, jobid): """Match a jobid to the queue_snapshot Args: ingpath <str>: ingredient path jobid <int>: job id Returns: Queue status for the job, according to $MAST_CONTROL/queue_snapshot """ myqs = MASTFile("%s/queue_snapshot" % mast_control) jobstatus = "" for qsline in myqs.data: if str(jobid) in qsline: jobstatus = queue_status_from_text(jobid, qsline) return jobstatus
def is_complete(self): """Check for the existence of mast_complete_file (default), or for a certain string, given by mast_complete_search""" if not 'mast_complete_file' in self.keywords['program_keys'].keys(): raise MASTError(self.__class__.__name__, "No completion file indicated by mast_complete_file keyword for %s. Cannot determine whether run is complete." % self.keywords['name']) return False checkfile = os.path.join(self.keywords['name'],self.keywords['program_keys']['mast_complete_file']) searchstr = None if 'mast_complete_search' in self.keywords['program_keys'].keys(): searchstr = self.keywords['program_keys']['mast_complete_search'] if searchstr == "None": searchstr = None if os.path.isfile(checkfile): if searchstr == None: return True else: tempopen = MASTFile(checkfile) mymatch = tempopen.get_last_line_match(searchstr) if mymatch == None: return False else: return True else: return False
def get_energy_from_energy_file(self): """Get the energy from the energy file. For VASP neb, this is the last E0 energy from each OSZICAR, and this function should be used if vasprun.xml is corrupted or not available. Args: mydir <str>: Directory in which to look. Returns: <str>: all last E0 energies from OSZICAR files. """ myct=0 mystr="" while myct <= self.keywords['program_keys']['mast_neb_settings']['images']+1: fullpath = os.path.join(self.keywords['name'], str(myct).zfill(2), "OSZICAR") if not os.path.isfile(fullpath): raise MASTError(self.__class__.__name__, "No OSZICAR file at %s" % fullpath) myosz = MASTFile(fullpath) mye0 = myosz.get_segment_from_last_line_match("E0", "E0=","d E =") mye0 = float(mye0) mystr = mystr + "%3.3f" % mye0 + ';' myct = myct + 1 mystr=mystr[0:-1] #remove last semicolon return mystr
def print_submitted_dict(submitted): """Print a dictionary of all runs submitted into $MAST_CONTROL/submitted Args: submitted <dict>: Dictionary of submitted runs, with key as the directory name. """ subprint = os.path.join(mast_control, "submitted") subrecent = os.path.join(mast_control, "just_submitted") if os.path.isfile(subprint): subprintfile = MASTFile(subprint) else: subprintfile = MASTFile() subrecentfile = MASTFile() keylist = submitted.keys() keylist.sort() subprintfile.data.append(time.asctime() + "\n") subrecentfile.data.append(time.asctime() + "\n") for key in keylist: subprintfile.data.append("%s:%s\n" % (key, submitted[key])) subrecentfile.data.append("%s:%s\n" % (key, submitted[key])) subprintfile.to_file(subprint) subrecentfile.to_file(subrecent) return
def get_last_jobid(ingpath): """Get the last job id from the jobids file in the ingredient path Args: ingpath <str>: ingredient path Returns: jobid <int>: job id, if available None if no jobids file was found """ jpath = "%s/jobids" % ingpath if not os.path.isfile(jpath): return None jfile = MASTFile(jpath) if len(jfile.data) == 0: return None if 'None' in jfile.data[0]: return None return int(jfile.data[0].strip())
def submit_from_submission_list(): """Submit all entries from the submission list at $MAST_CONTROL/submitlist Adds a job number to the top of the "jobids" file in each ingredient directory. """ submitlist = os.path.join(mast_control, "submitlist") if not os.path.isfile(submitlist): print "No submission list at %s" % submitlist return submitfile = MASTFile(submitlist) subentries = list(submitfile.data) subentries.sort() submitted = dict() subcommand = queue_submission_command() for subentry in subentries: # each entry is a directory name if len(subentry) == 0: continue subentry = subentry.strip() if len(subentry) == 0: continue if not os.path.isdir(subentry): continue elif subentry in submitted.keys(): continue lastjobid = get_last_jobid(subentry) jstatus = "not yet determined" if not (lastjobid == None): jstatus = get_job_status_from_queue_snapshot(subentry, lastjobid) if jstatus.lower() in ['r', 'q', 'h', 'e']: #running, queued, held, error submitted[ subentry] = "Already on queue with status %s" % jstatus continue os.chdir(subentry) subme = subprocess.Popen(subcommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subme.wait() status = subme.communicate()[0] write_to_jobids_file(subentry, status) submitted[subentry] = status print_submitted_dict(submitted) os.chdir(mast_control)
def write_to_jobids_file(subentry, status): """Write the job id to a jobids file in the ingredient directory. Args: subentry <str>: Folder from which the job was submitted (ingredient folder) status <str>: Job submission response from queueing system """ jobid = extract_submitted_jobid(status) jpath = "%s/jobids" % subentry if not os.path.isfile(jpath): jfile = MASTFile() else: jfile = MASTFile(jpath) jfile.data.insert(0, str(jobid) + '\n') jfile.to_file(jpath) return
def write_to_submit_list(mydir): """Write an entry to the submission list in $MAST_CONTROL/submitlist Args: mydir <str>: Directory which includes submission script submit.sh for a single calculation to be submitted to the queue """ mast_control = dirutil.get_mast_control_path( ) #setting here instead of globally allows tests to run in isolated test_control folder submitlist = os.path.join(mast_control, "submitlist") if os.path.isfile(submitlist): submitfile = MASTFile(submitlist) else: submitfile = MASTFile() submitfile.data.append(mydir + "\n") submitfile.to_file(submitlist) return
def _phon_poscar_setup(self): """Set up a PHON POSCAR file. Strip out the "elements" line (that is, use VASP version 4 format. Also strip out anything beneath the atoms line. """ name = self.keywords['name'] pospath = os.path.join(name, "POSCAR") prepath = os.path.join(name, "POSCAR_prePHON") if os.path.isfile(pospath): #Already done. Return. return my_poscar = Poscar.from_file(prepath) my_poscar.selective_dynamics=None #unset SD if it is set my_poscar.velocities=None #unset velocities dirutil.lock_directory(name) my_poscar.write_file(pospath) dirutil.unlock_directory(name) #pick up a copy and strip out the elements line. mypfile = MASTFile(pospath) myline6=mypfile.get_line_number(6) if myline6.strip().split()[0].isalpha: mypfile.modify_file_by_line_number(6,"D") mypfile.to_file(pospath) return
def print_submitted_dict(submitted): """Print a dictionary of all runs submitted into $MAST_CONTROL/submitted Args: submitted <dict>: Dictionary of submitted runs, with key as the directory name. """ subprint=os.path.join(mast_control, "submitted") subrecent=os.path.join(mast_control, "just_submitted") if os.path.isfile(subprint): subprintfile=MASTFile(subprint) else: subprintfile=MASTFile() subrecentfile=MASTFile() keylist=submitted.keys() keylist.sort() subprintfile.data.append(time.asctime()+ "\n") subrecentfile.data.append(time.asctime()+ "\n") for key in keylist: subprintfile.data.append("%s:%s\n" % (key, submitted[key])) subrecentfile.data.append("%s:%s\n" % (key, submitted[key])) subprintfile.to_file(subprint) subrecentfile.to_file(subrecent) return
def _phon_poscar_setup(self): """Set up a PHON POSCAR file. Strip out the "elements" line (that is, use VASP version 4 format. Also strip out anything beneath the atoms line. """ name = self.keywords["name"] pospath = os.path.join(name, "POSCAR") prepath = os.path.join(name, "POSCAR_prePHON") if os.path.isfile(pospath): # Already done. Return. return my_poscar = Poscar.from_file(prepath) my_poscar.selective_dynamics = None # unset SD if it is set my_poscar.velocities = None # unset velocities dirutil.lock_directory(name) my_poscar.write_file(pospath) dirutil.unlock_directory(name) # pick up a copy and strip out the elements line. mypfile = MASTFile(pospath) myline6 = mypfile.get_line_number(6) if myline6.strip().split()[0].isalpha: mypfile.modify_file_by_line_number(6, "D") mypfile.to_file(pospath) return
def write_submit_script(keywords): """This script example is built on the following ingredient keywords, and may require significant customization. mast_processors mast_ppn mast_queue mast_nodes mast_exec mast_walltime mast_memory """ import os #set defaults name = os.path.basename(keywords['name']) try: mast_processors = str(keywords['program_keys']['mast_processors']) except KeyError: mast_processors = "8" try: mast_ppn = str(keywords['program_keys']['mast_ppn']) except KeyError: mast_ppn = "8" try: mast_queue = str(keywords['program_keys']['mast_queue']) except KeyError: mast_queue = 'default' try: mast_nodes = str(keywords['program_keys']['mast_nodes']) except KeyError: mast_nodes = "1" try: mast_exec = str(keywords['program_keys']['mast_exec']) except KeyError: mast_exec = "mpiexec vasp" try: mast_walltime = str( keywords['program_keys']['mast_walltime']) + ":00:00" except KeyError: mast_walltime = "24:00:00" try: mast_memory = str(keywords['program_keys']['mast_memory']) except KeyError: mast_memory = "1000" #create submission script newkey = dict() newkey['mast_memory'] = mast_memory newkey['mast_exec'] = mast_exec newkey['mast_walltime'] = mast_walltime newkey['mast_queue'] = mast_queue newkey['mast_ppn'] = mast_ppn newkey['mast_nodes'] = mast_nodes newkey['mast_processors'] = mast_processors newkey['mast_name'] = name my_template = MASTFile("%s/submit/platforms/%s/submit_template.sh" % (mast_install, mast_platform)) newdata = list() for myline in my_template.data: for mykey in newkey.keys(): querykey = "?" + mykey + "?" if querykey in myline: myline = myline.replace(querykey, newkey[mykey]) newdata.append(myline) filled_template = MASTFile() filled_template.data = list(newdata) filled_template.to_file("%s/submit.sh" % keywords['name']) #use full path here return
def write_submit_script(keywords): """This script example is built on the following ingredient keywords, and may require significant customization. mast_processors mast_ppn mast_queue mast_nodes mast_exec mast_walltime mast_memory """ import os #set defaults name = os.path.basename(keywords['name']) try: mast_processors = str(keywords['program_keys']['mast_processors']) except KeyError: mast_processors = "8" try: mast_ppn = str(keywords['program_keys']['mast_ppn']) except KeyError: mast_ppn = "8" try: mast_queue = str(keywords['program_keys']['mast_queue']) except KeyError: mast_queue = 'default' try: mast_nodes = str(keywords['program_keys']['mast_nodes']) except KeyError: mast_nodes = "1" try: mast_exec = str(keywords['program_keys']['mast_exec']) except KeyError: mast_exec = "mpiexec vasp" try: mast_walltime = str(keywords['program_keys']['mast_walltime']) + ":00:00" except KeyError: mast_walltime = "24:00:00" try: mast_memory = str(keywords['program_keys']['mast_memory']) except KeyError: mast_memory = "1000" #create submission script newkey = dict() newkey['mast_memory'] = mast_memory newkey['mast_exec'] = mast_exec newkey['mast_walltime'] = mast_walltime newkey['mast_queue'] = mast_queue newkey['mast_ppn'] = mast_ppn newkey['mast_nodes'] = mast_nodes newkey['mast_processors'] = mast_processors newkey['mast_name'] = name my_template = MASTFile("%s/submit/platforms/%s/submit_template.sh" % (mast_install, mast_platform)) newdata = list() for myline in my_template.data: for mykey in newkey.keys(): querykey = "?" + mykey + "?" if querykey in myline: myline = myline.replace(querykey, newkey[mykey]) newdata.append(myline) filled_template = MASTFile() filled_template.data = list(newdata) filled_template.to_file("%s/submit.sh" % keywords['name']) #use full path here return