def set_up_recipe_plan(self, fulldir, verbose=0): """Set up a recipe plan from the recipe directory. Args: fulldir <str>: full path of recipe directory verbose <int>: verbosity """ self.logger.info("--------------------------------") self.logger.info("Setting up recipe plan for %s" % fulldir) self.logger.info("--------------------------------") os.chdir(fulldir) #need to change directories in order to submit jobs? myipparser = InputParser(inputfile=os.path.join(fulldir, 'input.inp')) myinputoptions = myipparser.parse() input_options_keys = myinputoptions.get_sections() key = 'personal_recipe' if key in input_options_keys: self.logger.debug("Key - personal recipe was found") personal_recipe_contents = myinputoptions.get_item('personal_recipe', 'personal_recipe_list') rsetup = RecipeSetup(recipeFile=personal_recipe_contents, inputOptions=myinputoptions, structure=myinputoptions.get_item('structure','structure'), workingDirectory=fulldir) recipe_plan_obj = rsetup.start() recipe_plan_obj.get_statuses_from_file() self.logger.debug("Returning recipe plan object to calling method.") return recipe_plan_obj
def set_up_recipe_plan(self, fulldir, verbose=0): """Set up a recipe plan from the recipe directory. Args: fulldir <str>: full path of recipe directory verbose <int>: verbosity """ self.logger.info("--------------------------------") self.logger.info("Setting up recipe plan for %s" % fulldir) self.logger.info("--------------------------------") os.chdir(fulldir) #need to change directories in order to submit jobs? myipparser = InputParser(inputfile=os.path.join(fulldir, 'input.inp')) myinputoptions = myipparser.parse() input_options_keys = myinputoptions.get_sections() key = 'personal_recipe' if key in input_options_keys: self.logger.debug("Key - personal recipe was found") personal_recipe_contents = myinputoptions.get_item( 'personal_recipe', 'personal_recipe_list') rsetup = RecipeSetup(recipeFile=personal_recipe_contents, inputOptions=myinputoptions, structure=myinputoptions.get_item( 'structure', 'structure'), workingDirectory=fulldir) recipe_plan_obj = rsetup.start() recipe_plan_obj.get_statuses_from_file() self.logger.debug("Returning recipe plan object to calling method.") return recipe_plan_obj
def check_recipe_dir(self, fulldir, verbose): """Check a recipe directory. Args: fulldir <str>: full path of recipe directory verbose <int>: verbosity """ shortdir = os.path.basename(fulldir) #only the recipe directory name if not os.path.exists(fulldir): raise MASTError(self.__class__.__name__, "No recipe directory at %s" % fulldir) if os.path.exists(os.path.join(fulldir, "MAST_SKIP")): self.logger.warning("Skipping recipe %s due to the presence of a MAST_SKIP file in the recipe directory." % shortdir) return if os.path.exists(os.path.join(fulldir, "MAST_ERROR")): self.logger.error("ATTENTION!: Skipping recipe %s due to the presence of a MAST_ERROR file in the recipe directory." % shortdir) return self.logger.info("--------------------------------") self.logger.info("Processing recipe %s" % shortdir) self.logger.info("--------------------------------") os.chdir(fulldir) #need to change directories in order to submit jobs? myipparser = InputParser(inputfile=os.path.join(fulldir, 'input.inp')) myinputoptions = myipparser.parse() input_options_keys = myinputoptions.get_sections() key = 'personal_recipe' if key in input_options_keys: self.logger.debug("Key - personal recipe was found") personal_recipe_contents = myinputoptions.get_item('personal_recipe', 'personal_recipe_list') rsetup = RecipeSetup(recipeFile=personal_recipe_contents, inputOptions=myinputoptions, structure=myinputoptions.get_item('structure','structure'), workingDirectory=fulldir) recipe_plan_obj = rsetup.start() recipe_plan_obj.get_statuses_from_file() try: recipe_plan_obj.check_recipe_status(verbose) except Exception: import sys,traceback #ex_type, ex, trbck = sys.exc_info() errortext = traceback.format_exc() #del trbck errorfile = open(os.path.join(fulldir, "MAST_ERROR"), "ab") errorfile.write("ERROR LOGGED %s\n" % time.asctime()) errorfile.write("%s\n" % errortext) errorfile.close() self.logger.warning("ERROR in recipe %s. Check MAST_ERROR file in the %s directory." % (shortdir, fulldir)) #raise MASTError(self.__class__.__name__,"Error in recipe %s as follows: %s %s %s" % (shortdir, ex_type, ex, errortext)) os.chdir(self.scratch) if recipe_plan_obj.status == "C": shutil.move(fulldir, self._ARCHIVE) summarypath = "%s/%s/SUMMARY.txt" % (self._ARCHIVE, shortdir) if os.path.isfile(summarypath): self.logger.info("Recipe %s completed." % shortdir) self.logger.info("SUMMARY.txt below:") summarytext = MASTFile(summarypath) for myline in summarytext.data: self.logger.info(myline.strip()) self.logger.info("-----------------------------") self.logger.info("Recipe %s processed." % shortdir) self.logger.info("-----------------------------")
def create_recipe_plan(self): """Create the recipe plan object, and print its status. """ personal_recipe_contents = self.input_options.get_item('personal_recipe', 'personal_recipe_list') setup_obj = RecipeSetup(recipeFile=personal_recipe_contents, inputOptions=self.input_options, structure=self.input_options.get_item('structure','structure'), workingDirectory=self.recdir ) self.recipe_plan = setup_obj.start() self.recipe_plan.print_status() return
def create_recipe_plan(self): """Create the recipe plan object, and print its status. """ personal_recipe_contents = self.input_options.get_item( 'personal_recipe', 'personal_recipe_list') setup_obj = RecipeSetup(recipeFile=personal_recipe_contents, inputOptions=self.input_options, structure=self.input_options.get_item( 'structure', 'structure'), workingDirectory=self.recdir) self.recipe_plan = setup_obj.start() self.recipe_plan.print_status() return
def test_start(self): #rfile=os.path.join(testdir,'personalized') wdir=os.path.join(testdir,'workdir') struc=None iopt=MAST.parsers.inputparser.InputParser(inputfile='input.inp').parse() rfile = iopt.get_item('personal_recipe', 'personal_recipe_list') myrs=RecipeSetup(recipeFile=rfile, workingDirectory=wdir, inputOptions=iopt, structure=struc) myrs.start() compare_metadata=MASTFile('compare_metadata.txt') myrs_metadata=MASTFile(os.path.join(wdir,'metadata.txt')) self.assertEqual(myrs_metadata.data, compare_metadata.data) dirlist=MAST.utility.dirutil.walkdirs(wdir) print dirlist compare_dirlist=list() compare_dirlist.append('defect_1nn_q=p0_opt1') compare_dirlist.append('defect_1nn_q=p0_opt2') compare_dirlist.append('defect_1nn_q=p0_stat') compare_dirlist.append('defect_purefin_q=p0_opt1') compare_dirlist.append('defect_purefin_q=p0_opt2') compare_dirlist.append('defect_purefin_q=p0_stat') compare_dirlist.append('defect_pureinit_q=p0_opt1') compare_dirlist.append('defect_pureinit_q=p0_opt2') compare_dirlist.append('defect_pureinit_q=p0_stat') compare_dirlist.append('defect_solute_q=p0_opt1') compare_dirlist.append('defect_solute_q=p0_opt2') compare_dirlist.append('defect_solute_q=p0_stat') compare_dirlist.append('diffcoeff_utility') compare_dirlist.append('inducedefect_1nn') compare_dirlist.append('inducedefect_purefin') compare_dirlist.append('inducedefect_pureinit') compare_dirlist.append('inducedefect_solute') compare_dirlist.append('neb_1nn-solute_q=p0_opt1') compare_dirlist.append('neb_1nn-solute_q=p0_opt2') compare_dirlist.append('neb_1nn-solute_q=p0_stat') compare_dirlist.append('neb_pureinit-purefin_q=p0_opt1') compare_dirlist.append('neb_pureinit-purefin_q=p0_opt2') compare_dirlist.append('neb_pureinit-purefin_q=p0_stat') compare_dirlist.append('perfect_opt1') compare_dirlist.append('perfect_opt2') compare_dirlist.append('perfect_stat') compare_dirlist.append('phonon_1nn-solute_q=p0_movingsolute') compare_dirlist.append('phonon_1nn_q=p0_solute') compare_dirlist.append('phonon_pureinit-purefin_q=p0_movingsolvent') compare_dirlist.append('phonon_pureinit_q=p0_solvent') compare_dirlist_withpath=list() for diritem in compare_dirlist: compare_dirlist_withpath.append(os.path.join(wdir,diritem)) mdiff=self.maxDiff self.maxDiff=None self.assertItemsEqual(dirlist,compare_dirlist_withpath) self.maxDiff=mdiff
def __init__(self, directory=None, plot_threshold=0.01): self.directory = directory self.plot_threshold = plot_threshold ipparser = InputParser( inputfile=os.path.join(self.directory, 'input.inp')) self.input_options = ipparser.parse() #pm = PickleManager(self.directory + '/input_options.pickle') #self.input_options = pm.load_variable() self.recipe_setup = RecipeSetup(inputOptions=self.input_options, recipeFile=self.input_options.get_item( "personal_recipe", "personal_recipe_list"), workingDirectory=self.directory) self.recipe_plan = self.recipe_setup.start() self.e_defects = dict()
def __init__(self, directory=None, plot_threshold=0.01): self.directory = directory self.plot_threshold = plot_threshold ipparser = InputParser(inputfile=os.path.join(self.directory,'input.inp')) self.input_options = ipparser.parse() #pm = PickleManager(self.directory + '/input_options.pickle') #self.input_options = pm.load_variable() self.recipe_setup = RecipeSetup(inputOptions=self.input_options,recipeFile=self.input_options.get_item("personal_recipe","personal_recipe_list"),workingDirectory=self.directory) self.recipe_plan = self.recipe_setup.start() self.e_defects = dict()
def test_get_my_ingredient_options(self): raise SkipTest #rfile=os.path.join(testdir,'personalized') wdir=os.path.join(testdir,'workdir') struc=None iopt=MAST.parsers.inputparser.InputParser(inputfile='input.inp').parse() rfile = iopt.get_item('personal_recipe', 'personal_recipe_list') myrs=RecipeSetup(recipeFile=rfile, workingDirectory=wdir, inputOptions=iopt, structure=struc) print iopt self.assertEqual(type(myrs.metafile),MAST.utility.metadata.Metadata)
class DefectFormationEnergy: """Class for calculating the defect formation energy for a completed MAST run. """ def __init__(self, directory=None, plot_threshold=0.01): self.directory = directory self.plot_threshold = plot_threshold ipparser = InputParser(inputfile=os.path.join(self.directory,'input.inp')) self.input_options = ipparser.parse() #pm = PickleManager(self.directory + '/input_options.pickle') #self.input_options = pm.load_variable() self.recipe_setup = RecipeSetup(inputOptions=self.input_options,recipeFile=self.input_options.get_item("personal_recipe","personal_recipe_list"),workingDirectory=self.directory) self.recipe_plan = self.recipe_setup.start() self.e_defects = dict() def _calculate_defect_formation_energies(self,scsize): perf_dirs = [] ingredients = self.recipe_plan.ingredients.keys() for i in range(len(ingredients)): if 'perfect' in ingredients[i]: perf_dirs.append(ingredients[i]) if len(perf_dirs)==0: raise MASTError(self.__class__.__name__, "A perfect final directory (has no children and has the word 'perfect' in its name) could not be found. Check recipe %s for a perfect directory." % self.directory) def_dir = ru.read_recipe(self.input_options.get_item("personal_recipe","personal_recipe_list"))[1]['madelung_utility'] defects = self.input_options.get_item('defects', 'defects') chempot = self.input_options.get_item('chemical_potentials') for i in range(len(perf_dirs)): perf_meta = Metadata(metafile='%s/%s/metadata.txt' % (self.directory, perf_dirs[i])) if scsize == perf_meta.read_data('scaling_size'): perf_dir = perf_dirs[i] perfpath = os.path.join(self.directory, perf_dir) if os.path.isfile(os.path.join(perfpath,"vasprun.xml")): e_perf = pmg.io.vaspio.vasp_output.Vasprun(os.path.join(perfpath, "vasprun.xml")).final_energy else: e_perf = float(open(os.path.join(perfpath, "OSZICAR")).readlines()[-1].split('E0=')[1].split()[0]) efermi = self.get_fermi_energy(perf_dir) struct_perf = self.get_structure(perf_dir) perf_species = dict() for site in struct_perf: if (str(site.specie) not in perf_species): perf_species[str(site.specie)] = 1 else: perf_species[str(site.specie)] += 1 #print 'Perfect composition: ', perf_species #First Loop through the conditions for the defects for conditions, potentials in chempot.items(): self.e_defects[conditions] = dict() # Loop through each defect for ddir in sorted(def_dir): def_meta = Metadata(metafile='%s/%s/metadata.txt' % (self.directory, ddir)) if scsize == def_meta.read_data('scaling_size'): label = def_meta.read_data('defect_label')+'_q='+def_meta.read_data('charge') charge = int(def_meta.read_data('charge')) if os.path.isfile(os.path.join(self.directory, ddir, "vasprun.xml")): energy = pmg.io.vaspio.vasp_output.Vasprun(self.directory+'/'+ddir+'/vasprun.xml').final_energy else: energy = float(open(self.directory+'/'+ddir+'/OSZICAR').readlines()[-1].split('E0=')[1].split()[0]) structure = self.get_structure(ddir) #if (label not in self.e_defects[conditions]): # self.e_defects[conditions][label] = list() # Find out how many atoms of each type are in the defects def_species = dict() for site in structure.sites: if (site.specie not in def_species): def_species[site.specie] = 1 else: def_species[site.specie] += 1 # Find the differences in the number of each atom type # between the perfect and the defect struct_diff = dict() for specie, number in def_species.items(): try: nperf = perf_species[str(specie)] except KeyError: nperf = 0 struct_diff[str(specie)] = number - nperf # Get the potential alignment correction alignment = self.get_potential_alignment(perf_dir, ddir) # Calculate the base DFE energy e_def = energy - e_perf # E_defect - E_perf for specie, number in struct_diff.items(): mu = potentials[str(specie)] e_def -= (number * mu) e_def += charge * (efermi + alignment) # Add in the shift here! #print '%-15s%-5i%12.5f%12.5f%12.5f%12.5f' % (label.split('_')[1], charge, energy, e_perf, efermi, alignment) #print 'DFE = %f' % e_def self.e_defects[conditions][label] = e_def def get_total_energy(self, directory): """Returns the total energy from a directory""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): # Modified from the PyMatGen Vasprun.final_energy() function to return E_0 return Vasprun('%s/vasprun.xml' % abspath).ionic_steps[-1]["electronic_steps"][-1]["e_0_energy"] def get_fermi_energy(self, directory): """Returns the Fermi energy from a directory""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): return Vasprun('%s/vasprun.xml' % abspath).efermi elif ('OUTCAR' in os.listdir(abspath)): return Outcar('%s/OUTCAR' % abspath).efermi def get_structure(self, directory): """Returns the final structure from an optimization""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): return Vasprun('%s/vasprun.xml' % abspath).final_structure elif ('CONTCAR' in os.listdir(abspath)): return pmg.read_structure('%s/CONTCAR' % abspath) def get_potential_alignment(self, perf_dir, def_dir): """Returns the potential alignment correction used in charge defects""" #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ abs_path_perf = '%s/%s/' % (self.directory, perf_dir) abs_path_def = '%s/%s/' % (self.directory, def_dir) #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ pa = PotentialAlignment() #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ if ('OUTCAR' in os.listdir(abs_path_perf)): perfect_info = pa.read_outcar('%s/%s' % (abs_path_perf, 'OUTCAR')) defect_info = pa.read_outcar('%s/%s' % (abs_path_def, 'OUTCAR')) #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ return pa.get_potential_alignment(perfect_info, defect_info) def get_defect_formation_energies(self,scsize): if not self.e_defects: self._calculate_defect_formation_energies(scsize) return self.e_defects @property def defect_formation_energies(self): return self.get_defect_formation_energies(scsize) @property def dfe(self): return self.get_defect_formation_energies(scsize) def print_table(self,scsize): if not self.e_defects: self._calculate_defect_formation_energies(scsize) myfile = MASTFile() for conditions, defects in self.e_defects.items(): myfile.data.append('\n\nDefect formation energies for %s conditions.\n' % conditions.upper()) myfile.data.append('%-20s | %10s\n' % ('Defect', 'DFE')) myfile.data.append('---------------------------------\n') for defect, energies in defects.items(): for energy in energies: myfile.data.append('%-14s(q=%2i) | %8.4f\n' % (defect, energy[0], energy[1])) myfile.data.append(str()) # Add a blank line here myfile.data.append('---------------------------------\n') myfile.to_file(os.path.join(os.getcwd(),"dfe.txt")) for line in myfile.data: print line.strip()
class DefectFormationEnergy: """Class for calculating the defect formation energy for a completed MAST run. """ def __init__(self, directory=None, plot_threshold=0.01): self.directory = directory self.plot_threshold = plot_threshold ipparser = InputParser( inputfile=os.path.join(self.directory, 'input.inp')) self.input_options = ipparser.parse() #pm = PickleManager(self.directory + '/input_options.pickle') #self.input_options = pm.load_variable() self.recipe_setup = RecipeSetup(inputOptions=self.input_options, recipeFile=self.input_options.get_item( "personal_recipe", "personal_recipe_list"), workingDirectory=self.directory) self.recipe_plan = self.recipe_setup.start() self.e_defects = dict() def _calculate_defect_formation_energies(self, scsize): perf_dirs = [] ingredients = self.recipe_plan.ingredients.keys() for i in range(len(ingredients)): if 'perfect' in ingredients[i]: perf_dirs.append(ingredients[i]) if len(perf_dirs) == 0: raise MASTError( self.__class__.__name__, "A perfect final directory (has no children and has the word 'perfect' in its name) could not be found. Check recipe %s for a perfect directory." % self.directory) def_dir = ru.read_recipe( self.input_options.get_item( "personal_recipe", "personal_recipe_list"))[1]['madelung_utility'] defects = self.input_options.get_item('defects', 'defects') chempot = self.input_options.get_item('chemical_potentials') for i in range(len(perf_dirs)): perf_meta = Metadata(metafile='%s/%s/metadata.txt' % (self.directory, perf_dirs[i])) if scsize == perf_meta.read_data('scaling_size'): perf_dir = perf_dirs[i] perfpath = os.path.join(self.directory, perf_dir) if os.path.isfile(os.path.join(perfpath, "vasprun.xml")): e_perf = pmg.io.vaspio.vasp_output.Vasprun( os.path.join(perfpath, "vasprun.xml")).final_energy else: e_perf = float( open(os.path.join( perfpath, "OSZICAR")).readlines()[-1].split('E0=')[1].split()[0]) efermi = self.get_fermi_energy(perf_dir) struct_perf = self.get_structure(perf_dir) perf_species = dict() for site in struct_perf: if (str(site.specie) not in perf_species): perf_species[str(site.specie)] = 1 else: perf_species[str(site.specie)] += 1 #print 'Perfect composition: ', perf_species #First Loop through the conditions for the defects for conditions, potentials in chempot.items(): self.e_defects[conditions] = dict() # Loop through each defect for ddir in sorted(def_dir): def_meta = Metadata(metafile='%s/%s/metadata.txt' % (self.directory, ddir)) if scsize == def_meta.read_data('scaling_size'): label = def_meta.read_data( 'defect_label') + '_q=' + def_meta.read_data('charge') charge = int(def_meta.read_data('charge')) if os.path.isfile( os.path.join(self.directory, ddir, "vasprun.xml")): energy = pmg.io.vaspio.vasp_output.Vasprun( self.directory + '/' + ddir + '/vasprun.xml').final_energy else: energy = float( open(self.directory + '/' + ddir + '/OSZICAR').readlines()[-1].split('E0=') [1].split()[0]) structure = self.get_structure(ddir) #if (label not in self.e_defects[conditions]): # self.e_defects[conditions][label] = list() # Find out how many atoms of each type are in the defects def_species = dict() for site in structure.sites: if (site.specie not in def_species): def_species[site.specie] = 1 else: def_species[site.specie] += 1 # Find the differences in the number of each atom type # between the perfect and the defect struct_diff = dict() for specie, number in def_species.items(): try: nperf = perf_species[str(specie)] except KeyError: nperf = 0 struct_diff[str(specie)] = number - nperf # Get the potential alignment correction alignment = self.get_potential_alignment(perf_dir, ddir) # Calculate the base DFE energy e_def = energy - e_perf # E_defect - E_perf for specie, number in struct_diff.items(): mu = potentials[str(specie)] e_def -= (number * mu) e_def += charge * (efermi + alignment ) # Add in the shift here! #print '%-15s%-5i%12.5f%12.5f%12.5f%12.5f' % (label.split('_')[1], charge, energy, e_perf, efermi, alignment) #print 'DFE = %f' % e_def self.e_defects[conditions][label] = e_def def get_total_energy(self, directory): """Returns the total energy from a directory""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): # Modified from the PyMatGen Vasprun.final_energy() function to return E_0 return Vasprun( '%s/vasprun.xml' % abspath).ionic_steps[-1]["electronic_steps"][-1]["e_0_energy"] def get_fermi_energy(self, directory): """Returns the Fermi energy from a directory""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): return Vasprun('%s/vasprun.xml' % abspath).efermi elif ('OUTCAR' in os.listdir(abspath)): return Outcar('%s/OUTCAR' % abspath).efermi def get_structure(self, directory): """Returns the final structure from an optimization""" abspath = '%s/%s/' % (self.directory, directory) if ('vasprun.xml' in os.listdir(abspath)): return Vasprun('%s/vasprun.xml' % abspath).final_structure elif ('CONTCAR' in os.listdir(abspath)): return pmg.read_structure('%s/CONTCAR' % abspath) def get_potential_alignment(self, perf_dir, def_dir): """Returns the potential alignment correction used in charge defects""" #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ abs_path_perf = '%s/%s/' % (self.directory, perf_dir) abs_path_def = '%s/%s/' % (self.directory, def_dir) #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ pa = PotentialAlignment() #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ if ('OUTCAR' in os.listdir(abs_path_perf)): perfect_info = pa.read_outcar('%s/%s' % (abs_path_perf, 'OUTCAR')) defect_info = pa.read_outcar('%s/%s' % (abs_path_def, 'OUTCAR')) #ifndef VERSION1 #else /* VERSION1 */ #endif /* VERSION1 */ return pa.get_potential_alignment(perfect_info, defect_info) def get_defect_formation_energies(self, scsize): if not self.e_defects: self._calculate_defect_formation_energies(scsize) return self.e_defects @property def defect_formation_energies(self): return self.get_defect_formation_energies(scsize) @property def dfe(self): return self.get_defect_formation_energies(scsize) def print_table(self, scsize): if not self.e_defects: self._calculate_defect_formation_energies(scsize) myfile = MASTFile() for conditions, defects in self.e_defects.items(): myfile.data.append( '\n\nDefect formation energies for %s conditions.\n' % conditions.upper()) myfile.data.append('%-20s | %10s\n' % ('Defect', 'DFE')) myfile.data.append('---------------------------------\n') for defect, energies in defects.items(): for energy in energies: myfile.data.append('%-14s(q=%2i) | %8.4f\n' % (defect, energy[0], energy[1])) myfile.data.append(str()) # Add a blank line here myfile.data.append('---------------------------------\n') myfile.to_file(os.path.join(os.getcwd(), "dfe.txt")) for line in myfile.data: print line.strip()