def correct(self): content = "LSTOP = .TRUE." if not self.electronic_step_stop else \ "LABORT = .TRUE." #Write STOPCAR actions = [{ "file": "STOPCAR", "action": { "_file_create": { 'content': content } } }] if self.auto_continue: actions.append({ "file": "STOPCAR", "action": { "_file_modify": { 'mode': 0o444 } } }) m = Modder(actions=[FileActions]) for a in actions: m.modify(a["action"], a["file"]) # Actions is being returned as None so that custodian will stop after # STOPCAR is written. We do not want subsequent jobs to proceed. return {"errors": ["Walltime reached"], "actions": None}
def correct(self): content = "LSTOP = .TRUE." chkpt_content = "Index: %d\nTime: \"%s\"" % (self.chk_counter, datetime.datetime.now()) self.chk_counter += 1 #Write STOPCAR actions = [{ "file": "STOPCAR", "action": { "_file_create": { 'content': content } } }, { "file": "chkpt.yaml", "action": { "_file_create": { 'content': chkpt_content } } }] m = Modder(actions=[FileActions]) for a in actions: m.modify(a["action"], a["file"]) # Reset the clock. self.start_time = datetime.datetime.now() return {"errors": ["Checkpoint reached"], "actions": actions}
def apply_corrections_to_spec(corrections, spec): modder = Modder() for correction in corrections: actions = correction['actions'] for action in actions: if action['action_type'] == 'modify_object': if action['object']['source'] == 'fw_spec': myobject = spec[action['object']['key']] else: raise NotImplementedError( 'Object source "{}" not implemented in ' 'CheckTask'.format(action['object']['source'])) newobj = modder.modify_object(action['action'], myobject) spec[action['object']['key']] = newobj elif action['action_type'] == 'modify_dict': if action['dict']['source'] == 'fw_spec': mydict = spec[action['dict']['key']] else: raise NotImplementedError( 'Dict source "{}" not implemented in ' 'CheckTask'.format(action['dict']['source'])) modder.modify(action['action'], mydict) else: raise NotImplementedError( 'Action type "{}" not implemented in ' 'CheckTask'.format(action['action_type']))
def correct(self): backup("*.nw*") actions = [] nwi = NwInput.from_file(self.input_file) for e in self.errors: if e == "autoz error": action = { "_set": { "geometry_options": ["units", "angstroms", "noautoz"] } } actions.append(action) elif e == "Bad convergence": t = nwi.tasks[self.ntasks - 1] if "cgmin" in t.theory_directives: nwi.tasks.pop(self.ntasks - 1) else: t.theory_directives["cgmin"] = "" for t in nwi.tasks: if t.operation.startswith("freq"): # You cannot calculate hessian with cgmin. t.theory_directives["nocgmin"] = "" action = {"_set": {"tasks": [t.as_dict() for t in nwi.tasks]}} actions.append(action) else: # For unimplemented errors, this should just cause the job to # die. return {"errors": self.errors, "actions": None} m = Modder() for action in actions: nwi = m.modify_object(action, nwi) nwi.write_file(self.input_file) return {"errors": self.errors, "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 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): content = "LSTOP = .TRUE." if not self.electronic_step_stop else \ "LABORT = .TRUE." # Write STOPCAR actions = [{"file": "STOPCAR", "action": {"_file_create": {'content': content}}}] m = Modder(actions=[FileActions]) for a in actions: m.modify(a["action"], a["file"]) return {"errors": ["Walltime reached"], "actions": None}
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_modify": {"mode": 0o666}}, "test_file_copy") 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")
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')
def correct(self): # Write ``stop`` file actions = [{ "file": "stop", "action": { "_file_create": { 'content': '' } } }] m = Modder(actions=[FileActions]) for a in actions: m.modify(a["action"], a["file"]) return {"errors": ["Walltime reached"], "actions": None}
def correct(self): d = loadfn("chkpt.yaml") i = d["Index"] name = shutil.make_archive( os.path.join(os.getcwd(), "vasp.chk.%d" % i), "gztar") actions = [{"file": "CONTCAR", "action": {"_file_copy": {"dest": "POSCAR"}}}] m = Modder(actions=[FileActions]) for a in actions: m.modify(a["action"], a["file"]) actions.append({"Checkpoint": name}) return {"errors": ["Stopped run."], "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': list(range(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_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": list(range(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 apply_corrections(self, fw_to_correct, corrections): # Apply the corrections spec = fw_to_correct.spec modder = Modder() for correction in corrections: actions = correction['actions'] for action in actions: if action['action_type'] == 'modify_object': if action['object']['source'] == 'fw_spec': myobject = spec[action['object']['key']] else: raise NotImplementedError( 'Object source "{}" not implemented in ' 'CheckTask'.format(action['object']['source'])) newobj = modder.modify_object(action['action'], myobject) spec[action['object']['key']] = newobj elif action['action_type'] == 'modify_dict': if action['dict']['source'] == 'fw_spec': mydict = spec[action['dict']['key']] else: raise NotImplementedError( 'Dict source "{}" not implemented in ' 'CheckTask'.format(action['dict']['source'])) modder.modify(action['action'], mydict) else: raise NotImplementedError( 'Action type "{}" not implemented in ' 'CheckTask'.format(action['action_type'])) # Keep track of the corrections that have been applied spec['SRC_check_corrections'] = corrections # Update the task index fws_task_index = int( fw_to_correct.spec['wf_task_index'].split('_')[-1]) new_index = fws_task_index + 1 # Update the Fireworks _queueadapter key #TODO: in the future, see whether the FW queueadapter might be replaced by the qtk_queueadapter ? # ... to be discussed with Anubhav, when the qtk queueadapter is in a qtk toolkit and not anymore # in pymatgen/io/abinit spec['_queueadapter'] = spec['qtk_queueadapter'].get_subs_dict() queue_adapter_update = get_queue_adapter_update( qtk_queueadapter=spec['qtk_queueadapter'], corrections=corrections) # Get and update the task_input if needed # TODO: make this more general ... right now, it is based on AbinitInput and thus is strongly tight # to abinit due to abiinput, deps, ... mytask = fw_to_correct.tasks[0] task_class = mytask.__class__ decoder = MontyDecoder() task_input = decoder.process_decoded( fw_to_correct.spec['_tasks'][0]['abiinput']) initialization_info = fw_to_correct.spec['initialization_info'] deps = mytask.deps # Create the new Setup/Run/Check fireworks SRC_fws = createSRCFireworksOld( task_class=task_class, task_input=task_input, SRC_spec=spec, initialization_info=initialization_info, wf_task_index_prefix=spec['wf_task_index_prefix'], current_task_index=new_index, handlers=self.handlers, validators=self.validators, deps=deps, task_type=mytask.task_type, queue_adapter_update=queue_adapter_update) wf = Workflow(fireworks=SRC_fws['fws'], links_dict=SRC_fws['links_dict']) return FWAction(detours=[wf])