def test_to_dict_from_dict(self): k = Kpoints.monkhorst_automatic([2, 2, 2], [0, 0, 0]) d = k.to_dict k2 = Kpoints.from_dict(d) self.assertEqual(k.kpts, k2.kpts) self.assertEqual(k.style, k2.style) self.assertEqual(k.kpts_shift, k2.kpts_shift)
def test_static_constructors(self): kpoints = Kpoints.gamma_automatic([3, 3, 3], [0, 0, 0]) self.assertEqual(kpoints.style, "Gamma") self.assertEqual(kpoints.kpts, [[3, 3, 3]]) kpoints = Kpoints.monkhorst_automatic([2, 2, 2], [0, 0, 0]) self.assertEqual(kpoints.style, "Monkhorst") self.assertEqual(kpoints.kpts, [[2, 2, 2]]) kpoints = Kpoints.automatic(100) self.assertEqual(kpoints.style, "Automatic") self.assertEqual(kpoints.kpts, [[100]]) filepath = os.path.join(test_dir, 'POSCAR') poscar = Poscar.from_file(filepath) kpoints = Kpoints.automatic_density(poscar.struct, 500) self.assertEqual(kpoints.kpts, [[2, 4, 4]])
def _vasp_kpoints_setup(self): """Parse mast_kpoints string, which should take the format: number, number, number designation examples: "3x3x3 M", "1x1x1 G". If no designation is given, Monkhorst-Pack is assumed. """ name = self.keywords['name'] tryname = os.path.join(name, "KPOINTS") if os.path.isfile(tryname): #KPOINTS already exists. Do not overwrite. my_kpoints = Kpoints.from_file(tryname) return my_kpoints if not (self.metafile.read_data('kpoints')==None): kpoints = self.metafile.read_data('kpoints').split() kmesh = (int(kpoints[0].split('x')[0]),int(kpoints[0].split('x')[1]),int(kpoints[0].split('x')[2])) try: desig = kpoints[1].upper() except IndexError: desig = 'M' try: kshift = (float(kpoints[2]),float(kpoints[3]),float(kpoints[4])) except IndexError: kshift = (0.0,0.0,0.0) else: if 'mast_kpoints' in self.keywords['program_keys'].keys(): kpoints = self.keywords['program_keys']['mast_kpoints'] else: raise MASTError(self.__class__.__name__,"k-point instructions need to be set either in ingredients keyword mast_kpoints or scaling section in structure ingredient: No k-point settings for the ingredient %s"% name) if len(kpoints) == 3: desig = "M" elif 'line' in str(kpoints[1]): desig = 'L' else: desig = kpoints[3].upper() if not desig == 'L': kmesh = (int(kpoints[0]),int(kpoints[1]),int(kpoints[2])) kshift = (0,0,0) if desig == "M": my_kpoints = Kpoints.monkhorst_automatic(kpts=kmesh,shift=kshift) elif desig == "G": my_kpoints = Kpoints.gamma_automatic(kpts=kmesh,shift=kshift) elif desig == 'L': my_kpoints='Line Mode\n'+'\n'.join(' '.join(kpoints).split(','))+'\n' fp=open(name+'/KPOINTS','w') fp.write(my_kpoints) else: raise MASTError(self.__class__.__name__,"kpoint designation " + desig + " not recognized.") dirutil.lock_directory(name) if not desig=='L': my_kpoints.write_file(name + "/KPOINTS") dirutil.unlock_directory(name) return my_kpoints
def band_path(line_density=40): """ for band calculation """ poscar = Poscar.from_file('POSCAR', check_for_POTCAR=False) highsymmkp = MyHighSymmKpath(poscar.structure) kpts = highsymmkp.get_kpoints(line_density) args = {'comment': "Kpoints for band calc", 'kpts': kpts[0], 'num_kpts': len(kpts[0]), 'labels': kpts[1], 'style': 'Reciprocal', 'kpts_weights': [1]*len(kpts[0])} kpoints = Kpoints(**args) kpoints.write_file('KPOINTS_band')
def make_hex_lattice_input(path, atomic_sep, kpointdivs, comment, forbid_symmetry=True): pb = hexagonal_unitcell('C', atomic_sep, layersep=25.) incar = Incar() incar['SYSTEM'] = comment incar['ALGO'] = 'Fast' incar['NSW'] = 1000 incar['IBRION'] = 2 incar['EDIFF'] = 1E-8 if forbid_symmetry: incar['ISYM'] = 0 kpoints = Kpoints.gamma_automatic(kpointdivs, [0, 0, 0]) metadata = {'atomic_sep': atomic_sep, 'kpointdivs': kpointdivs} ws = VaspInput( poscar=pb.poscar(comment=comment), potcar=pb.potcar(functional='PBE'), incar=incar, kpoints=kpoints, # additional files metadata=json.dumps(metadata), ) ws.write_input(path)
def make_hex_lattice_input(path, atomic_sep, kpointdivs, comment, forbid_symmetry=True): pb = hexagonal_unitcell('C', atomic_sep, layersep=25.) incar = Incar() incar['SYSTEM'] = comment incar['ALGO'] = 'Fast' incar['NSW'] = 1000 incar['IBRION'] = 2 incar['EDIFF'] = 1E-8 if forbid_symmetry: incar['ISYM'] = 0 kpoints = Kpoints.gamma_automatic(kpointdivs, [0,0,0]) metadata = {'atomic_sep': atomic_sep, 'kpointdivs': kpointdivs} ws = VaspInput( poscar = pb.poscar(comment=comment), potcar = pb.potcar(functional='PBE'), incar = incar, kpoints = kpoints, # additional files metadata = json.dumps(metadata), ) ws.write_input(path)
def get_kpoints(self, structure): ''' Writes out a KPOINTS file using the fully automated grid method. Uses Gamma centered meshes for hexagonal cells and Monkhorst-Pack grids otherwise. Algorithm: Uses a simple approach scaling the number of divisions along each reciprocal lattice vector proportional to its length. ''' return Kpoints.automatic_density(structure, int(self.kpoints_settings['grid_density']))
def make_vasp_input(path, *, kpointdivs, comment, forbid_symmetry, functional, perturb_dist, **kwargs): assert hasattr(kpointdivs, '__iter__') assert isinstance(comment, str) assert isinstance(forbid_symmetry, bool) assert isinstance(perturb_dist, float) pb = carbon_chain_poxcar(**kwargs) incar = Incar() incar['SYSTEM'] = comment incar['ALGO'] = 'Fast' incar['NSW'] = 1000 incar['NPAR'] = 4 incar['IBRION'] = 2 incar['EDIFF'] = 1E-8 incar['EDIFFG'] = -0.001 if forbid_symmetry: incar['ISYM'] = 0 kpoints = Kpoints.gamma_automatic(kpointdivs, [0,0,0]) metadata = { 'kpointdivs':kpointdivs, 'functional':functional, 'perturb_dist':perturb_dist, 'forbid_symmetry':forbid_symmetry, } # FIXME FIXME FIXME # Here, we insert all structure parameters into the metadata. The purpose is because # oftentimes those parameters are the ones we're most interested in (such as scale). # # However, this is all kinds of bad: # * Not all structure arguments are necessarily desirable to have in the metadata. # * What if we want a structure argument to be of a non JSON-encodable type? # * It creates a dependency between names of function arguments in code, and the output file. # In other words, madness. metadata = dict_union(metadata, kwargs) ws = VaspInput( poscar = pb.poscar( perturb_dist=perturb_dist, comment=comment, ), potcar = pb.potcar( functional=functional, ), incar = incar, kpoints = kpoints, # additional files metadata = json.dumps(metadata), ) ws.write_input(path)
def make_vasp_input(path, *, kpointdivs, comment, forbid_symmetry, functional, perturb_dist, **kwargs): assert hasattr(kpointdivs, '__iter__') assert isinstance(comment, str) assert isinstance(forbid_symmetry, bool) assert isinstance(perturb_dist, float) pb = carbon_chain_poxcar(**kwargs) incar = Incar() incar['SYSTEM'] = comment incar['ALGO'] = 'Fast' incar['NSW'] = 1000 incar['NPAR'] = 4 incar['IBRION'] = 2 incar['EDIFF'] = 1E-8 incar['EDIFFG'] = -0.001 if forbid_symmetry: incar['ISYM'] = 0 kpoints = Kpoints.gamma_automatic(kpointdivs, [0, 0, 0]) metadata = { 'kpointdivs': kpointdivs, 'functional': functional, 'perturb_dist': perturb_dist, 'forbid_symmetry': forbid_symmetry, } # FIXME FIXME FIXME # Here, we insert all structure parameters into the metadata. The purpose is because # oftentimes those parameters are the ones we're most interested in (such as scale). # # However, this is all kinds of bad: # * Not all structure arguments are necessarily desirable to have in the metadata. # * What if we want a structure argument to be of a non JSON-encodable type? # * It creates a dependency between names of function arguments in code, and the output file. # In other words, madness. metadata = dict_union(metadata, kwargs) ws = VaspInput( poscar=pb.poscar( perturb_dist=perturb_dist, comment=comment, ), potcar=pb.potcar(functional=functional, ), incar=incar, kpoints=kpoints, # additional files metadata=json.dumps(metadata), ) ws.write_input(path)
def test_init(self): filepath = os.path.join(test_dir, 'KPOINTS.auto') kpoints = Kpoints.from_file(filepath) self.assertEqual(kpoints.kpts, [[10]], "Wrong kpoint lattice read") filepath = os.path.join(test_dir, 'KPOINTS.cartesian') kpoints = Kpoints.from_file(filepath) self.assertEqual(kpoints.kpts, [[0.25, 0, 0], [0, 0.25, 0], [0, 0, 0.25]], "Wrong kpoint lattice read") self.assertEqual(kpoints.kpts_shift, [0.5, 0.5, 0.5], "Wrong kpoint shift read") filepath = os.path.join(test_dir, 'KPOINTS') kpoints = Kpoints.from_file(filepath) self.assertEqual(kpoints.kpts, [[2, 4, 6]], "Wrong kpoint lattice read") filepath = os.path.join(test_dir, 'KPOINTS.band') kpoints = Kpoints.from_file(filepath) self.assertIsNotNone(kpoints.labels) self.assertEqual(kpoints.style, "Line_mode") filepath = os.path.join(test_dir, 'KPOINTS.explicit') kpoints = Kpoints.from_file(filepath) self.assertIsNotNone(kpoints.kpts_weights) filepath = os.path.join(test_dir, 'KPOINTS.explicit_tet') kpoints = Kpoints.from_file(filepath) self.assertEqual(kpoints.tet_connections, [(6, [1, 2, 3, 4])])
def scale_mesh(self, newstr, density): """Scale the kpoint mesh. Args: newstr <structure>: new structure density <float>: kpoint density Returns: newkmesh <pymatgen Kpoints>: new kpoint mesh sets program_keys 'mast_kpoints' to new mesh """ newkmesh = Kpoints.automatic_density(newstr, density) klist = list() klist = newkmesh.to_dict['kpoints'][0] klist.append(newkmesh.to_dict['generation_style'][0]) self.keywords['program_keys']['mast_kpoints'] = klist return newkmesh
def process_killed_run(cls, dir_name): """ Process a killed vasp run. """ fullpath = os.path.abspath(dir_name) logger.info("Processing Killed run " + fullpath) d = {"dir_name": fullpath, "state": "killed", "oszicar": {}} for f in os.listdir(dir_name): filename = os.path.join(dir_name, f) if fnmatch(f, "INCAR*"): try: incar = Incar.from_file(filename) d["incar"] = incar.to_dict d["is_hubbard"] = incar.get("LDAU", False) if d["is_hubbard"]: us = incar.get("LDAUU", []) js = incar.get("LDAUJ", []) if sum(us) == 0 and sum(js) == 0: d["is_hubbard"] = False d["hubbards"] = {} else: d["hubbards"] = {} if d["is_hubbard"]: d["run_type"] = "GGA+U" elif incar.get("LHFCALC", False): d["run_type"] = "HF" else: d["run_type"] = "GGA" except Exception as ex: print str(ex) logger.error("Unable to parse INCAR for killed run {}.".format(dir_name)) elif fnmatch(f, "KPOINTS*"): try: kpoints = Kpoints.from_file(filename) d["kpoints"] = kpoints.to_dict except: logger.error("Unable to parse KPOINTS for killed run {}.".format(dir_name)) elif fnmatch(f, "POSCAR*"): try: s = Poscar.from_file(filename).structure comp = s.composition el_amt = s.composition.get_el_amt_dict() d.update( { "unit_cell_formula": comp.to_dict, "reduced_cell_formula": comp.to_reduced_dict, "elements": list(el_amt.keys()), "nelements": len(el_amt), "pretty_formula": comp.reduced_formula, "anonymous_formula": comp.anonymized_formula, "nsites": comp.num_atoms, "chemsys": "-".join(sorted(el_amt.keys())), } ) d["poscar"] = s.to_dict except: logger.error("Unable to parse POSCAR for killed run {}.".format(dir_name)) elif fnmatch(f, "POTCAR*"): try: potcar = Potcar.from_file(filename) d["pseudo_potential"] = {"functional": "pbe", "pot_type": "paw", "labels": potcar.symbols} except: logger.error("Unable to parse POTCAR for killed run in {}.".format(dir_name)) elif fnmatch(f, "OSZICAR"): try: d["oszicar"]["root"] = Oszicar(os.path.join(dir_name, f)).to_dict except: logger.error("Unable to parse OSZICAR for killed run in {}.".format(dir_name)) elif re.match("relax\d", f): if os.path.exists(os.path.join(dir_name, f, "OSZICAR")): try: d["oszicar"][f] = Oszicar(os.path.join(dir_name, f, "OSZICAR")).to_dict except: logger.error("Unable to parse OSZICAR for killed " "run in {}.".format(dir_name)) return d
def process_killed_run(cls, dir_name): """ Process a killed vasp run. """ fullpath = os.path.abspath(dir_name) logger.info("Processing Killed run " + fullpath) d = {"dir_name": fullpath, "state": "killed", "oszicar": {}} for f in os.listdir(dir_name): filename = os.path.join(dir_name, f) if fnmatch(f, "INCAR*"): try: incar = Incar.from_file(filename) d["incar"] = incar.to_dict d["is_hubbard"] = incar.get("LDAU", False) if d["is_hubbard"]: us = incar.get("LDAUU", []) js = incar.get("LDAUJ", []) if sum(us) == 0 and sum(js) == 0: d["is_hubbard"] = False d["hubbards"] = {} else: d["hubbards"] = {} if d["is_hubbard"]: d["run_type"] = "GGA+U" elif incar.get("LHFCALC", False): d["run_type"] = "HF" else: d["run_type"] = "GGA" except Exception as ex: print str(ex) logger.error( "Unable to parse INCAR for killed run {}.".format( dir_name)) elif fnmatch(f, "KPOINTS*"): try: kpoints = Kpoints.from_file(filename) d["kpoints"] = kpoints.to_dict except: logger.error( "Unable to parse KPOINTS for killed run {}.".format( dir_name)) elif fnmatch(f, "POSCAR*"): try: s = Poscar.from_file(filename).structure comp = s.composition el_amt = s.composition.get_el_amt_dict() d.update({ "unit_cell_formula": comp.to_dict, "reduced_cell_formula": comp.to_reduced_dict, "elements": list(el_amt.keys()), "nelements": len(el_amt), "pretty_formula": comp.reduced_formula, "anonymous_formula": comp.anonymized_formula, "nsites": comp.num_atoms, "chemsys": "-".join(sorted(el_amt.keys())) }) d["poscar"] = s.to_dict except: logger.error( "Unable to parse POSCAR for killed run {}.".format( dir_name)) elif fnmatch(f, "POTCAR*"): try: potcar = Potcar.from_file(filename) d["pseudo_potential"] = { "functional": "pbe", "pot_type": "paw", "labels": potcar.symbols } except: logger.error( "Unable to parse POTCAR for killed run in {}.".format( dir_name)) elif fnmatch(f, "OSZICAR"): try: d["oszicar"]["root"] = \ Oszicar(os.path.join(dir_name, f)).to_dict except: logger.error( "Unable to parse OSZICAR for killed run in {}.".format( dir_name)) elif re.match("relax\d", f): if os.path.exists(os.path.join(dir_name, f, "OSZICAR")): try: d["oszicar"][f] = Oszicar( os.path.join(dir_name, f, "OSZICAR")).to_dict except: logger.error("Unable to parse OSZICAR for killed " "run in {}.".format(dir_name)) return d
def test_kpt_bands_to_dict_from_dict(self): file_name = os.path.join(test_dir, 'KPOINTS.band') k = Kpoints.from_file(file_name) d = k.to_dict