classAddTwoIntsWorkChain(WorkChain): """WorkChain to add two ints, for testing and demonstration purposes.""" @classmethod defdefine(cls, spec): """Specify inputs and outputs.""" super().define(spec) spec.input('x', valid_type=Int) spec.input('y', valid_type=Int) spec.input('pwd', valid_type=Str) spec.input('code', valid_type=AbstractCode) spec.outline( cls.init_dir, cls.add, cls.validate_result, cls.result, cls.write_result, ) spec.output('result', valid_type=Int) spec.exit_code(400, 'ERROR_NEGATIVE_NUMBER', message='The result is a negative number.')
definit_dir(self): self.report('---init: Now at dir---:{}'.format(os.getcwd())) os.chdir(str(self.inputs.pwd.value)) #### change work path self.report('---init: Now at dir---:{}'.format(os.getcwd())) defadd(self): """Add two numbers using the `ArithmeticAddCalculation` calculation job plugin.""" inputs = {'x': self.inputs.x, 'y': self.inputs.y, 'code': self.inputs.code} future = self.submit(ArithmeticAddCalculation, **inputs) self.to_context(addition=future)
defvalidate_result(self): """Make sure the result is not negative.""" result = self.ctx.addition.outputs.sum if result.value < 0: return self.exit_codes.ERROR_NEGATIVE_NUMBER
defresult(self): """Add the result to the outputs.""" self.out('result', self.ctx.addition.outputs.sum)
defwrite_result(self): withopen('sum_rlt.txt', 'w') as f: f.write('result: {}\n'.format(int(self.ctx.addition.outputs.sum)))
from testworkflow import AddTwoIntsWorkChain from aiida.orm import Int, load_code, Str from aiida import load_profile from aiida.common.extendeddicts import AttributeDict from aiida.engine import submit import os