def tearDown(self): #return dnames = dirutil.walkdirs("childdir",1,1) for dname in dnames: shutil.rmtree(dname) fnames = dirutil.walkfiles("childdir",1,1) for fname in fnames: os.remove(fname)
def test_dfe_tool(self): import subprocess os.chdir("archive/GaAs_defects_AsGa_recipe_defects_20131125T220427/dfe_ingredient") mydfetest=subprocess.Popen(["mast_defect_formation_energy dfe_input.txt"],shell=True) mydfetest.wait() os.chdir(testdir) compare_walk = dirutil.walkfiles("compare_results") compare_walk.remove("compare_results/__init__.py") compare_walk.remove("compare_results/__init__.pyc") res_walk = dirutil.walkfiles("archive/GaAs_defects_AsGa_recipe_defects_20131125T220427/dfe_ingredient/dfe_results") compare_walk.sort() res_walk.sort() self.assertEqual(len(compare_walk), len(res_walk)) for idx in range(0,len(compare_walk)): compfile = MASTFile(compare_walk[idx]) myfile = MASTFile(res_walk[idx]) self.assertEqual(compfile.data, myfile.data)
def test_softlink_wavefunction_file(self): import subprocess myvc = VaspChecker(name="files") myvc.softlink_wavefunction_file("childdir") myfiles = dirutil.walkfiles("childdir") print myfiles # self.assertTrue("childdir/CHGCAR" in myfiles) listme = subprocess.Popen("ls -l childdir", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres = listme.communicate()[0] listme.wait() print listres self.assertTrue("files/WAVECAR" in listres)
def test_softlink_a_file_emptyparentfile(self): allowed=dict() allowed['name']=(str,"","Directory name") mybc=BaseChecker(allowed,name='files') mybc.softlink_a_file('childdir','emptyfile') myfiles=dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme=subprocess.Popen("ls -l childdir",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres=listme.communicate()[0] listme.wait() print listres self.assertTrue("files/emptyfile" in listres)
def test_softlink_a_file_child_file_exists(self): allowed=dict() allowed['name']=(str,"","Directory name") shutil.copy('files/another_alphatest','childdir/alphatest') mybc=BaseChecker(allowed,name='files') mybc.softlink_a_file('childdir','alphatest') myfiles=dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme=subprocess.Popen("ls -l childdir",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres=listme.communicate()[0] listme.wait() print listres self.assertFalse("files/alphatest" in listres) self.assertTrue("childdir/alphatest" in myfiles)
def test_softlink_wavefunction_file(self): import subprocess myvc = VaspChecker(name="files") myvc.softlink_wavefunction_file("childdir") myfiles = dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme = subprocess.Popen("ls -l childdir", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres = listme.communicate()[0] listme.wait() print listres self.assertTrue("files/WAVECAR" in listres)
def test_softlink_a_file_emptyparentfile(self): allowed = dict() allowed['name'] = (str, "", "Directory name") mybc = BaseChecker(allowed, name='files') mybc.softlink_a_file('childdir', 'emptyfile') myfiles = dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme = subprocess.Popen("ls -l childdir", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres = listme.communicate()[0] listme.wait() print listres self.assertTrue("files/emptyfile" in listres)
def test_softlink_a_file_child_file_exists(self): allowed = dict() allowed['name'] = (str, "", "Directory name") shutil.copy('files/another_alphatest', 'childdir/alphatest') mybc = BaseChecker(allowed, name='files') mybc.softlink_a_file('childdir', 'alphatest') myfiles = dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme = subprocess.Popen("ls -l childdir", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres = listme.communicate()[0] listme.wait() print listres self.assertFalse("files/alphatest" in listres) self.assertTrue("childdir/alphatest" in myfiles)
def test_softlink_a_file_child_softlink_exists(self): print "CURDIR: ", os.getcwd() allowed=dict() allowed['name']=(str,"","Directory name") os.chdir("childdir") linkme=subprocess.Popen("ln -s ../files/another_alphatest alphatest",shell=True) linkme.wait() os.chdir(testdir) mybc=BaseChecker(allowed,name='files') mybc.softlink_a_file('childdir','alphatest') myfiles=dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme=subprocess.Popen("ls -l childdir",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres=listme.communicate()[0] listme.wait() print listres self.assertTrue("files/another_alphatest" in listres) self.assertTrue("childdir/alphatest" in myfiles)
def combine_dynamical_matrix_files(self, mydir=""): """Combine dynamical matrix files into one file. The subfiles should be in subfolders under the top directory. Args: mydir <str>: top directory for DYNMAT files. Use ingredient directory if null. Returns: Creates a single DYNMAT file """ if mydir == "": mydir = self.keywords['name'] dynmatlist = dirutil.walkfiles(mydir, 2, 5, "*DYNMAT*") #start one level below if len(dynmatlist) == 0: raise MASTError("pmgextend combine_dynmats", "No DYNMATs found under " + mydir) totnumdisp=0 largedyn=dict() largedyn['atoms'] = dict() for onedynmat in dynmatlist: dyndir = os.path.dirname(onedynmat) onedyn = self.read_my_dynamical_matrix_file(dyndir) totnumdisp = totnumdisp + onedyn['numdisp'] for atom in onedyn['atoms'].keys(): if not atom in largedyn['atoms'].keys(): largedyn['atoms'][atom]=dict() mydisp=1 #start at 1 for disp in onedyn['atoms'][atom].keys(): if disp in largedyn['atoms'][atom].keys(): mydisp=mydisp + 1 #increment largedyn['atoms'][atom][mydisp]=dict() largedyn['atoms'][atom][mydisp]['displine'] = str(onedyn['atoms'][atom][disp]['displine']) largedyn['atoms'][atom][mydisp]['dynmat']=list(onedyn['atoms'][atom][disp]['dynmat']) largedyn['numspec'] = onedyn['numspec'] #should all be the same largedyn['numatoms'] = onedyn['numatoms'] largedyn['massline'] = onedyn['massline'] largedyn['numdisp'] = totnumdisp self.write_my_dynamical_matrix_file(largedyn, mydir, "DYNMAT_combined") self.write_my_dynamical_matrix_file(largedyn, mydir, "DYNMAT")
def test_softlink_a_file_child_softlink_exists(self): print "CURDIR: ", os.getcwd() allowed = dict() allowed['name'] = (str, "", "Directory name") os.chdir("childdir") linkme = subprocess.Popen("ln -s ../files/another_alphatest alphatest", shell=True) linkme.wait() os.chdir(testdir) mybc = BaseChecker(allowed, name='files') mybc.softlink_a_file('childdir', 'alphatest') myfiles = dirutil.walkfiles("childdir") print myfiles #self.assertTrue("childdir/CHGCAR" in myfiles) listme = subprocess.Popen("ls -l childdir", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) listres = listme.communicate()[0] listme.wait() print listres self.assertTrue("files/another_alphatest" in listres) self.assertTrue("childdir/alphatest" in myfiles)
def combine_displacement_files(self, mydir=""): """Combine displacement files (here XDATCARs) into one file. Args: mydir <str>: top directory for DYNMAT files. Use ingredient directory if null. """ if mydir == "": mydir = self.keywords['name'] largexdat=dict() xdatlist = dirutil.walkfiles(mydir, 2, 5, "*XDATCAR*") #start one level below if len(xdatlist) == 0: raise MASTError("pmgextend combine_displacements", "No XDATCARs found under " + mydir) kfgct=1 # skip config 1 until the end largexdat['configs']=dict() xdatlist.sort() #get them in order for onexdatmat in xdatlist: xdatdir = os.path.dirname(onexdatmat) onexdat = self.read_my_displacement_file(xdatdir) for kfg in onexdat['configs'].keys(): if kfg == 1: #skip config 1 until the end pass else: kfgct = kfgct + 1 #start at 2 largexdat['configs'][kfgct] = onexdat['configs'][kfg] largexdat['configs'][1] = onexdat['configs'][1] #get one of the first configs (the first should be the same for all the XDATCARs) largexdat['descline'] = onexdat['descline'] #should all be the same largexdat['specline'] = onexdat['specline'] largexdat['scale'] = onexdat['scale'] largexdat['latta'] = onexdat['latta'] largexdat['lattb'] = onexdat['lattb'] largexdat['lattc'] = onexdat['lattc'] largexdat['numline'] = onexdat['numline'] largexdat['numatoms'] = onexdat['numatoms'] largexdat['type'] = onexdat['type'] self.write_my_displacement_file(largexdat, mydir, "XDATCAR_combined") self.write_my_displacement_file(largexdat, mydir, "XDATCAR")
def parse_ingredients_section(self, section_name, section_content, options): """Parses the ingredients section and populates the options. Section takes the form of: $ingredients begin ingredients_global mast_kpoints 3x3x3 mast_xc pbe end begin singlepoint encut 400 end begin optimize encut 300 ibrion 2 end $end mast_kpoints are parsed out as a 3 index list of integers. Everything else is parsed out as a string. Anything in ingredients_global is then appended onto each individual ingredient. """ global_dict = dict() ingredients_dict = dict() for line in section_content: if (line.startswith('begin')): # Each ingredient section starts with "begin". # Check for this line, and initialize the individual # ingredient dictionary ingredient_name = line.split()[1] ingredient_dict = dict() elif (not (line == 'end')): opt = line.split() # print opt if (opt[0] == 'mast_kpoints'): try: kpts = map(int, opt[1].split('x')) if len(opt) > 2: kpts.append(opt[2]) except ValueError: kpts = opt[1:] # Second option after mast_kpoints tells where to # center the k-point mesh. # If it's there, we append it onto the k-point grid list. ingredient_dict[opt[0]] = kpts elif (opt[0] == 'mast_pp_setup'): psp_dict = dict() for key in opt[1:]: key = key.split('=') ref = key[0].title() val = str().join(key[1][0].upper() + key[1][1:]) psp_dict[ref] = val ingredient_dict[opt[0]] = psp_dict #elif (opt[0] == 'mast_exec'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'mast_strain'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'ptemp'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'rwigs'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'mast_setmagmom'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line elif (opt[0] == 'mast_coordinates'): shortsplit = opt[1].split(",") filesplit = list() origindir = os.getcwd() metatry = os.path.join(os.getcwd(), 'metadata.txt') if os.path.isfile(metatry): myrecipemeta = Metadata(metafile=metatry) origindir = myrecipemeta.search_data('origin_dir')[1] myfiles = dirutil.walkfiles(origindir) for shortname in shortsplit: for fullfile in myfiles: if shortname.strip() in os.path.basename(fullfile): filesplit.append(fullfile) if not (len(filesplit) == len(shortsplit)): raise MASTError( self.__class__.__name__, "Not all files given by %s were found in %s." % (shortsplit, origindir)) ingredient_dict[opt[0]] = filesplit else: ingredient_dict[opt[0]] = ' '.join( opt[1:]) #preserve whole line #ingredient_dict[opt[0]] = opt[1] #old behavior took only the first part if (opt[0] == 'mast_program') and (opt[1] == 'vasp' or opt[1] == 'vasp_neb'): if os.getenv('VASP_PSP_DIR') == None: raise MASTError( self.__class__.__name__, "Input file specifies program vasp, but no POTCAR directory is set in environment variable VASP_PSP_DIR" ) elif ('end' in line): # Each ingredient section ends with "end", if present finish # that current section and assign # the neccessary element in the ingredients dictionary and # create the global dictionary if (ingredient_name == 'ingredients_global'): global_dict = ingredient_dict else: ingredients_dict[ingredient_name] = ingredient_dict # Each value in ingredients_dict is a dictionary containing the relevant # ingredient and option(s). for ing_key, ing_value in ingredients_dict.items(): options.set_item(section_name, ing_key, ing_value) options.set_item(section_name, 'global', global_dict)
def parse_structure_section(self, section_name, section_content, options): """Parses the structure section and populate the options. Does not create the structure. Format is along the lines of: coord_type fractional begin coordinates Ga 0.000000 0.000000 0.000000 Ga 0.500000 0.500000 0.000000 Ga 0.000000 0.500000 0.500000 Ga 0.500000 0.000000 0.500000 As 0.250000 0.250000 0.250000 As 0.750000 0.750000 0.250000 As 0.250000 0.750000 0.750000 As 0.750000 0.250000 0.750000 end begin lattice 2.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 2.0 end Note that coord_type will default to "cartesian" if not specified. """ # Initialize with default values structure_dict = STRUCTURE_KEYWORDS.copy() subsection_dict = dict() for myline in section_content: line = myline.split() if (line[0] in structure_dict): structure_dict[line[0]] = line[1] elif ('begin' in line[0]): subsection = line[1] subsection_list = list() elif ('end' not in line): lsplit = myline.split() lineval = list() for lval in lsplit: lval.strip() if len(lval) > 0: lineval.append(lval) subsection_list.append(lineval) elif ('end' in line): subsection_dict[subsection] = subsection_list # GRJ: Since we lowercase the whole input file, and filenames may not # conform to this, we examine the current directory for any files that # may match and use that. If there are multiple matches, we'll throw # an error. if (structure_dict['posfile'] is not None): # Do we have a geometry file? # TTM for issue #423 posfiletry = os.path.join(os.getcwd(), structure_dict['posfile']) if os.path.isfile(posfiletry): self.logger.info("Using posfile %s in directory %s" % (structure_dict['posfile'], os.getcwd())) else: # First build a list of likely files origindir = os.path.dirname(self.keywords['inputfile']) if origindir == "": origindir = os.getcwd() metatry = os.path.join(os.getcwd(), 'metadata.txt') if os.path.isfile(metatry): myrecipemeta = Metadata(metafile=metatry) origindir = myrecipemeta.search_data('origin_dir')[1] myfiles = dirutil.walkfiles(origindir) file_list = list() for myfile in myfiles: if structure_dict['posfile'] in myfile: file_list.append(myfile) if (len(file_list) > 1): # If we have multiple files with the same name, but different capitalization, throw an error here self.logger.warning( 'Found multiple files with the name %s' % structure_dict['posfile']) self.logger.warning('Found the files:') for file in file_list: self.logger.warning(file) error = 'Found ambiguous file names' raise MASTError(self.__class__.__name__, error) elif len(file_list) == 0: raise MASTError( self.__class__.__name__, "No structure file %s found in %s" % (structure_dict['posfile'], origindir)) else: structure_dict['posfile'] = file_list[0] # print 'in InputParser.parse_structure_section:', subsection_dict # TM element_map = dict() atom_list = list() for key, value in subsection_dict.items(): if (key == 'coordinates'): value = np.array(value) # Here we use .title() to re-capitalize the first letter of all # the atomic symbols to comply with what # pymatgen needs atom_list = [val.title() for val in value[:, 0]] structure_dict['atom_list'] = atom_list coordinates = np.array(value[:, 1:], dtype='float') structure_dict['coordinates'] = coordinates if (key == 'lattice'): #print "VALUE: ", value lattice = np.array(value, dtype='float') structure_dict['lattice'] = lattice if (key == 'elementmap'): for elline in value: elkey = elline[0].strip().upper() #all caps elname = elline[1].strip().title() #Title case element_map[elkey] = elname structure_dict['element_map'] = element_map if len(element_map) > 0 and len(atom_list) > 0: new_atom_list = list() for atomval in atom_list: if atomval.upper() in element_map.keys(): new_atom_list.append(element_map[atomval]) else: new_atom_list.append(atomval) structure_dict['atom_list'] = new_atom_list for key, value in structure_dict.items(): options.set_item(section_name, key, value)
def parse_ingredients_section(self, section_name, section_content, options): """Parses the ingredients section and populates the options. Section takes the form of: $ingredients begin ingredients_global mast_kpoints 3x3x3 mast_xc pbe end begin singlepoint encut 400 end begin optimize encut 300 ibrion 2 end $end mast_kpoints are parsed out as a 3 index list of integers. Everything else is parsed out as a string. Anything in ingredients_global is then appended onto each individual ingredient. """ global_dict = dict() ingredients_dict = dict() for line in section_content: if (line.startswith('begin')): # Each ingredient section starts with "begin". # Check for this line, and initialize the individual # ingredient dictionary ingredient_name = line.split()[1] ingredient_dict = dict() elif (not (line == 'end')): opt = line.split() # print opt if (opt[0] == 'mast_kpoints'): try: kpts = map(int, opt[1].split('x')) if len(opt) > 2: kpts.append(opt[2]) except ValueError: kpts = opt[1:] # Second option after mast_kpoints tells where to # center the k-point mesh. # If it's there, we append it onto the k-point grid list. ingredient_dict[opt[0]] = kpts elif (opt[0] == 'mast_pp_setup'): psp_dict = dict() for key in opt[1:]: key = key.split('=') ref = key[0].title() val = str().join(key[1][0].upper() + key[1][1:]) psp_dict[ref] = val ingredient_dict[opt[0]] = psp_dict #elif (opt[0] == 'mast_exec'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'mast_strain'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'ptemp'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'rwigs'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #elif (opt[0] == 'mast_setmagmom'): # ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line elif (opt[0] == 'mast_coordinates'): shortsplit = opt[1].split(",") filesplit=list() origindir = os.getcwd() metatry = os.path.join(os.getcwd(), 'metadata.txt') if os.path.isfile(metatry): myrecipemeta = Metadata(metafile=metatry) origindir = myrecipemeta.search_data('origin_dir')[1] myfiles = dirutil.walkfiles(origindir) for shortname in shortsplit: for fullfile in myfiles: if shortname.strip() in os.path.basename(fullfile): filesplit.append(fullfile) if not (len(filesplit) == len(shortsplit)): raise MASTError(self.__class__.__name__, "Not all files given by %s were found in %s." % (shortsplit, origindir)) ingredient_dict[opt[0]] = filesplit else: ingredient_dict[opt[0]] = ' '.join(opt[1:]) #preserve whole line #ingredient_dict[opt[0]] = opt[1] #old behavior took only the first part if (opt[0] == 'mast_program') and (opt[1] == 'vasp' or opt[1] == 'vasp_neb'): if os.getenv('VASP_PSP_DIR') == None: raise MASTError(self.__class__.__name__, "Input file specifies program vasp, but no POTCAR directory is set in environment variable VASP_PSP_DIR") elif ('end' in line): # Each ingredient section ends with "end", if present finish # that current section and assign # the neccessary element in the ingredients dictionary and # create the global dictionary if (ingredient_name == 'ingredients_global'): global_dict = ingredient_dict else: ingredients_dict[ingredient_name] = ingredient_dict # Each value in ingredients_dict is a dictionary containing the relevant # ingredient and option(s). for ing_key, ing_value in ingredients_dict.items(): options.set_item(section_name, ing_key, ing_value) options.set_item(section_name, 'global', global_dict)
def parse_structure_section(self, section_name, section_content, options): """Parses the structure section and populate the options. Does not create the structure. Format is along the lines of: coord_type fractional begin coordinates Ga 0.000000 0.000000 0.000000 Ga 0.500000 0.500000 0.000000 Ga 0.000000 0.500000 0.500000 Ga 0.500000 0.000000 0.500000 As 0.250000 0.250000 0.250000 As 0.750000 0.750000 0.250000 As 0.250000 0.750000 0.750000 As 0.750000 0.250000 0.750000 end begin lattice 2.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 2.0 end Note that coord_type will default to "cartesian" if not specified. """ # Initialize with default values structure_dict = STRUCTURE_KEYWORDS.copy() subsection_dict = dict() for myline in section_content: line = myline.split() if (line[0] in structure_dict): structure_dict[line[0]] = line[1] elif ('begin' in line[0]): subsection = line[1] subsection_list = list() elif ('end' not in line): lsplit = myline.split() lineval = list() for lval in lsplit: lval.strip() if len(lval) > 0: lineval.append(lval) subsection_list.append(lineval) elif ('end' in line): subsection_dict[subsection] = subsection_list # GRJ: Since we lowercase the whole input file, and filenames may not # conform to this, we examine the current directory for any files that # may match and use that. If there are multiple matches, we'll throw # an error. if (structure_dict['posfile'] is not None): # Do we have a geometry file? # TTM for issue #423 posfiletry = os.path.join(os.getcwd(), structure_dict['posfile']) if os.path.isfile(posfiletry): self.logger.info("Using posfile %s in directory %s" % (structure_dict['posfile'], os.getcwd())) else: # First build a list of likely files origindir = os.path.dirname(self.keywords['inputfile']) if origindir == "": origindir = os.getcwd() metatry = os.path.join(os.getcwd(), 'metadata.txt') if os.path.isfile(metatry): myrecipemeta = Metadata(metafile=metatry) origindir = myrecipemeta.search_data('origin_dir')[1] myfiles = dirutil.walkfiles(origindir) file_list=list() for myfile in myfiles: if structure_dict['posfile'] in myfile: file_list.append(myfile) if (len(file_list) > 1): # If we have multiple files with the same name, but different capitalization, throw an error here self.logger.warning('Found multiple files with the name %s' % structure_dict['posfile']) self.logger.warning('Found the files:') for file in file_list: self.logger.warning(file) error='Found ambiguous file names' raise MASTError(self.__class__.__name__, error) elif len(file_list) == 0: raise MASTError(self.__class__.__name__, "No structure file %s found in %s" % (structure_dict['posfile'], origindir)) else: structure_dict['posfile'] = file_list[0] # print 'in InputParser.parse_structure_section:', subsection_dict # TM element_map = dict() atom_list = list() for key, value in subsection_dict.items(): if (key == 'coordinates'): value = np.array(value) # Here we use .title() to re-capitalize the first letter of all # the atomic symbols to comply with what # pymatgen needs atom_list = [val.title() for val in value[:, 0]] structure_dict['atom_list'] = atom_list coordinates = np.array(value[:, 1:], dtype='float') structure_dict['coordinates'] = coordinates if (key == 'lattice'): #print "VALUE: ", value lattice = np.array(value, dtype='float') structure_dict['lattice'] = lattice if (key == 'elementmap'): for elline in value: elkey = elline[0].strip().upper() #all caps elname = elline[1].strip().title() #Title case element_map[elkey]=elname structure_dict['element_map'] = element_map if len(element_map) > 0 and len(atom_list) > 0: new_atom_list = list() for atomval in atom_list: if atomval.upper() in element_map.keys(): new_atom_list.append(element_map[atomval]) else: new_atom_list.append(atomval) structure_dict['atom_list'] = new_atom_list for key, value in structure_dict.items(): options.set_item(section_name, key, value)