"""Parsers provided by aiida_mlip."""frompathlibimportPathfromaiida.commonimportexceptionsfromaiida.engineimportExitCodefromaiida.ormimportDict,SinglefileDatafromaiida.orm.nodes.process.processimportProcessNodefromaiida.pluginsimportCalculationFactoryfromase.ioimportreadfromaiida_mlip.helpers.convertersimportconvert_numpyfromaiida_mlip.parsers.base_parserimportBaseParserSinglepointCalc=CalculationFactory("mlip.sp")
[docs]classSPParser(BaseParser):""" Parser class for parsing output of calculation. Parameters ---------- node : aiida.orm.nodes.process.process.ProcessNode ProcessNode of calculation. Methods ------- __init__(node: aiida.orm.nodes.process.process.ProcessNode) Initialize the SPParser instance. parse(**kwargs: Any) -> int: Parse outputs, store results in the database. Returns ------- int An exit code. Raises ------ exceptions.ParsingError If the ProcessNode being passed was not produced by a SinglepointCalc. """
[docs]def__init__(self,node:ProcessNode):""" Check that the ProcessNode being passed was produced by a `Singlepoint`. Parameters ---------- node : aiida.orm.nodes.process.process.ProcessNode ProcessNode of calculation. """super().__init__(node)ifnotissubclass(node.process_class,SinglepointCalc):raiseexceptions.ParsingError("Can only parse `Singlepoint` calculations")
[docs]defparse(self,**kwargs)->int:""" Parse outputs, store results in the database. Parameters ---------- **kwargs : Any Any keyword arguments. Returns ------- int An exit code. """exit_code=super().parse(**kwargs)ifexit_code!=ExitCode(0):returnexit_codexyz_output=(self.node.inputs.out).value# Check that folder content is as expectedfiles_retrieved=self.retrieved.list_object_names()files_expected={xyz_output}ifnotfiles_expected.issubset(files_retrieved):self.logger.error(f"Found files '{files_retrieved}', expected to find '{files_expected}'")returnself.exit_codes.ERROR_MISSING_OUTPUT_FILES# Add output file to the outputsself.logger.info(f"Parsing '{xyz_output}'")withself.retrieved.open(xyz_output,"rb")ashandle:self.out("xyz_output",SinglefileData(file=handle,filename=xyz_output))content=read(Path(self.node.get_remote_workdir(),xyz_output),format="extxyz")results=convert_numpy(content.todict())results_node=Dict(results)self.out("results_dict",results_node)returnExitCode(0)