def correct(self): backup() vi = VaspInput.from_directory(".") potim = float(vi["INCAR"].get("POTIM", 0.5)) * 0.5 actions = [{"dict": "INCAR", "action": {"_set": {"POTIM": potim}}}] m = Modder() modified = [] for a in actions: modified.append(a["dict"]) vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) for f in modified: vi[f].write_file(f) return {"errors": ["POTIM"], "actions": actions}
def setup(self): files = os.listdir(".") num_structures = 0 if not set(files).issuperset(VASP_INPUT_FILES): for f in files: try: struct = read_structure(f) num_structures += 1 except: pass if num_structures != 1: raise RuntimeError("{} structures found. Unable to continue." .format(num_structures)) else: self.default_vis.write_input(struct, ".") if self.backup: for f in VASP_INPUT_FILES: shutil.copy(f, "{}.orig".format(f)) if self.auto_npar: try: vi = VaspInput.from_directory(".") incar = vi["INCAR"] #Only optimized NPAR for non-HF and non-RPA calculations. if (not incar.get("LHFCALC")) and (not incar.get("LRPA")): import multiprocessing ncores = multiprocessing.cpu_count() for npar in range(int(round(math.sqrt(ncores))), ncores): if ncores % npar == 0: incar["NPAR"] = npar break incar.write_file("INCAR") except: pass if self.settings_override is not None: vi = VaspInput.from_directory(".") m = Modder([FileActions, DictActions]) modified = [] for a in self.settings_override: if "dict" in a: modified.append(a["dict"]) vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) elif "filename" in a: m.modify(a["action"], a["filename"]) for f in modified: vi[f].write_file(f)
def correct(self): backup(self.output_filename) p = Poscar.from_file("POSCAR") s = p.structure trans = PerturbStructureTransformation(0.05) new_s = trans.apply_transformation(s) actions = [{"dict": "POSCAR", "action": {"_set": {"structure": new_s.to_dict}}, "transformation": trans.to_dict}] m = Modder() vi = VaspInput.from_directory(".") for a in actions: vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) vi["POSCAR"].write_file("POSCAR") return {"errors": ["Frozen job"], "actions": actions}
def correct(self): backup(self.output_filename) vi = VaspInput.from_directory(".") m = reduce(operator.mul, vi["KPOINTS"].kpts[0]) m = max(int(round(m ** (1 / 3))), 1) if vi["KPOINTS"].style.lower().startswith("m"): m += m % 2 actions = [{"dict": "KPOINTS", "action": {"_set": {"kpoints": [[m] * 3]}}}] m = Modder() modified = [] for a in actions: modified.append(a["dict"]) vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) for f in modified: vi[f].write_file(f) return {"errors": ["mesh_symmetry"], "actions": actions}
def test_strict_mode(self): modder = Modder(actions=[FileActions]) d = {"Hello": "World"} mod = {'_set': {'Hello': 'Universe', 'Bye': 'World'}} self.assertRaises(ValueError, modder.modify, mod, d) #In non-strict mode, unknown actions are ignored. d = {"Hello": "World"} modder = Modder(actions=[FileActions], strict=False) modder.modify(mod, d) self.assertEqual(d, {"Hello": "World"}) #File actions not supported modder = Modder() self.assertRaises(ValueError, modder.modify, {'_file_create': {'content': 'Test data'}}, 'test_file')
def correct(self): backup() actions = [{"file": "CONTCAR", "action": {"_file_copy": {"dest": "POSCAR"}}}, {"dict": "INCAR", "action": {"_set": {"ISTART": 1, "ALGO": "Normal", "NELMDL": 6, "BMIX": 0.001, "AMIX_MAG": 0.8, "BMIX_MAG": 0.001}}}] vi = VaspInput.from_directory(".") m = Modder(actions=[DictActions, FileActions]) for a in actions: if "dict" in a: vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) elif "file" in a: m.modify(a["action"], a["file"]) vi["INCAR"].write_file("INCAR") return {"errors": ["Unconverged"], "actions": actions}
def correct(self): backup(self.output_filename) actions = [] vi = VaspInput.from_directory(".") if "tet" in self.errors or "dentet" in self.errors: actions.append({"dict": "INCAR", "action": {"_set": {"ISMEAR": 0}}}) if "inv_rot_mat" in self.errors: actions.append({"dict": "INCAR", "action": {"_set": {"SYMPREC": 1e-8}}}) if "brmix" in self.errors or "zpotrf" in self.errors: actions.append({"dict": "INCAR", "action": {"_set": {"ISYM": 0}}}) if "subspacematrix" in self.errors or "rspher" in self.errors or \ "real_optlay" in self.errors: actions.append({"dict": "INCAR", "action": {"_set": {"LREAL": False}}}) if "tetirr" in self.errors or "incorrect_shift" in self.errors: actions.append({"dict": "KPOINTS", "action": {"_set": {"generation_style": "Gamma"}}}) if "amin" in self.errors: actions.append({"dict": "INCAR", "action": {"_set": {"AMIN": "0.01"}}}) if "too_few_bands" in self.errors: if "NBANDS" in vi["INCAR"]: nbands = int(vi["INCAR"]["NBANDS"]) else: with open("OUTCAR") as f: for line in f: if "NBANDS" in line: try: d = line.split("=") nbands = int(d[-1].strip()) break except (IndexError, ValueError): pass actions.append({"dict": "INCAR", "action": {"_set": {"NBANDS": int(1.1 * nbands)}}}) if "triple_product" in self.errors: s = vi["POSCAR"].structure trans = SupercellTransformation(((1, 0, 0), (0, 0, 1), (0, 1, 0))) new_s = trans.apply_transformation(s) actions.append({"dict": "POSCAR", "action": {"_set": {"structure": new_s.to_dict}}, "transformation": trans.to_dict}) if "rot_matrix" in self.errors or "pricel" in self.errors: s = vi["POSCAR"].structure trans = PerturbStructureTransformation(0.05) new_s = trans.apply_transformation(s) actions.append({"dict": "POSCAR", "action": {"_set": {"structure": new_s.to_dict}}, "transformation": trans.to_dict}) actions.append({"dict": "INCAR", "action": {"_set": {"SYMPREC": 1e-8}}}) if "brions" in self.errors: potim = float(vi["INCAR"].get("POTIM", 0.5)) + 0.1 actions.append({"dict": "INCAR", "action": {"_set": {"POTIM": potim}}}) m = Modder() modified = [] for a in actions: modified.append(a["dict"]) vi[a["dict"]] = m.modify_object(a["action"], vi[a["dict"]]) for f in modified: vi[f].write_file(f) return {"errors": list(self.errors), "actions": actions}
def test_dict_modify(self): modder = Modder() d = {"Hello": "World"} mod = {'_set': {'Hello': 'Universe', 'Bye': 'World'}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'Hello': 'Universe'}) mod = {'_unset': {'Hello': 1}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World'}) mod = {'_push': {'List': 1}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1]}) mod = {'_push': {'List': 2}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2]}) mod = {'_inc': {'num': 5}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2], 'num': 5}) mod = {'_inc': {'num': 5}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2], 'num': 10}) mod = {'_rename': {'num': 'number'}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2], 'number': 10}) mod = {'_add_to_set': {'List': 2}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2], 'number': 10}) mod = {'_add_to_set': {'List': 3}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2, 3], 'number': 10}) mod = {'_add_to_set': {'number': 3}} self.assertRaises(ValueError, modder.modify, mod, d) mod = {'_pull': {'List': 1}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [2, 3], 'number': 10}) mod = {'_pull_all': {'List': [2, 3]}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [], 'number': 10}) mod = {'_push_all': {'List': xrange(10)}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'number': 10}) mod = {'_pop': {'List': 1}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [0, 1, 2, 3, 4, 5, 6, 7, 8], 'number': 10}) mod = {'_pop': {'List':-1}} modder.modify(mod, d) self.assertEqual(d, {'Bye': 'World', 'List': [1, 2, 3, 4, 5, 6, 7, 8], 'number': 10}) d = {} mod = {'_set': {'a->b->c': 100}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100}}}) mod = {'_set': {'a->b->d': 200}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100, 'd': 200}}}) mod = {'_set': {'a->b->d': 300}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100, 'd': 300}}}) mod = {'_unset': {'a->b->d': 300}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100}}}) mod = {'_push': {'a->e->f': 300}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100}, 'e': {'f': [300]}}}) mod = {'_push_all': {'a->e->f': [100, 200]}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 100}, 'e': {'f': [300, 100, 200]}}}) mod = {'_inc': {'a->b->c': 2}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': [300, 100, 200]}}}) mod = {'_pull': {'a->e->f': 300}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': [100, 200]}}}) mod = {'_pull_all': {'a->e->f': [100, 200]}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': []}}}) mod = {'_push_all': {'a->e->f': [101, 201, 301, 401]}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': [101, 201, 301, 401]}}}) mod = {'_pop': {'a->e->f': 1}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': [101, 201, 301]}}}) mod = {'_pop': {'a->e->f':-1}} modder.modify(mod, d) self.assertEqual(d, {'a': {'b': {'c': 102}, 'e': {'f': [201, 301]}}})
def test_modify_object(self): modder = Modder() o = MyObject(1) self.assertEqual(o.b["a"], 1) mod_o = modder.modify_object({'_set': {'b->a': 20}}, o) self.assertEqual(mod_o.b["a"], 20)
def test_file_modify(self): modder = Modder(actions=[FileActions]) modder.modify({'_file_create': {'content': 'Test data'}}, 'test_file') modder.modify({'_file_copy': {'dest': 'test_file_copy'}}, 'test_file') modder.modify({'_file_copy': {'dest1': 'test_file_copy1', 'dest2': 'test_file_copy2'}}, 'test_file') modder.modify({'_file_move': {'dest': 'renamed_test_file'}}, 'test_file') modder.modify({'_file_delete': {'mode': "actual"}}, 'renamed_test_file') modder.modify({'_file_delete': {'mode': "actual"}}, 'test_file_copy') modder.modify({'_file_delete': {'mode': "actual"}}, 'test_file_copy1') modder.modify({'_file_delete': {'mode': "actual"}}, 'test_file_copy2')