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 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 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_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')