Ejemplo n.º 1
0
    def get_version(self) -> str:
        self.found(raise_error=True)

        # Get the node configuration
        config = get_config()

        # Run NWChem
        which_prog = which("nwchem")
        if config.use_mpiexec:
            command = create_mpi_invocation(which_prog, config)
        else:
            command = [which_prog]
        command.append("v.nw")

        if which_prog not in self.version_cache:
            success, output = execute(command, {"v.nw": ""}, scratch_directory=config.scratch_directory)

            if success:
                for line in output["stdout"].splitlines():
                    if "nwchem branch" in line:
                        branch = line.strip().split()[-1]
                    if "nwchem revision" in line:
                        revision = line.strip().split()[-1]
                self.version_cache[which_prog] = safe_version(branch + "+" + revision)
            else:
                raise UnknownError(output["stderr"])

        return self.version_cache[which_prog]
Ejemplo n.º 2
0
 def check_convergence(logfile: str) -> None:
     """
     Check the gaussian log file to make sure we have normal termination.
     """
     if "Normal termination of Gaussian" not in logfile:
         # raise an error with the log file as output
         raise UnknownError(message=logfile)
Ejemplo n.º 3
0
    def _compute(self, input_model: AtomicInput,
                 config: "TaskConfig") -> AtomicResult:
        """
        Runs NWChem in executable mode
        """
        self.found(raise_error=True)

        job_inputs = self.build_input(input_model, config)
        success, dexe = self.execute(job_inputs)

        stdin = job_inputs["infiles"]["nwchem.nw"]
        if "There is an error in the input file" in dexe["stdout"]:
            raise InputError(error_stamp(stdin, dexe["stdout"],
                                         dexe["stderr"]))
        if "not compiled" in dexe["stdout"]:
            # recoverable with a different compilation with optional modules
            raise InputError(error_stamp(stdin, dexe["stdout"],
                                         dexe["stderr"]))

        if success:
            dexe["outfiles"]["stdout"] = dexe["stdout"]
            dexe["outfiles"]["stderr"] = dexe["stderr"]
            dexe["outfiles"]["input"] = stdin
            return self.parse_output(dexe["outfiles"], input_model)
        else:
            # Check if any of the errors are known
            for error in all_errors:
                error.detect_error(dexe)
            raise UnknownError(
                error_stamp(stdin, dexe["stdout"], dexe["stderr"]))
Ejemplo n.º 4
0
    def get_version(self) -> str:
        self.found(raise_error=True)

        # Get the node configuration
        config = get_config()

        # Run MADNESS
        which_prog = which("moldft")

        if config.use_mpiexec:
            command = create_mpi_invocation(which_prog, config)
        else:
            command = [which_prog]
        command.append("v.moldft")

        if which_prog not in self.version_cache:
            success, output = execute(
                command,
                {
                    "v.moldft":
                    "dft\nxc lda\nend\ngeometry\nBe  0.0    0.0 0.0\n\nend\n"
                },
                scratch_directory=config.scratch_directory,
            )

            if success:
                for line in output["stdout"].splitlines():
                    if "multiresolution suite" in line:
                        version = line.strip().split()[1]
                self.version_cache[which_prog] = safe_version(version)
            else:
                raise UnknownError(output["stderr"])

        return self.version_cache[which_prog]
Ejemplo n.º 5
0
    def compute(self, input_model: "AtomicInput",
                config: "TaskConfig") -> "AtomicResult":
        """
        Runs madness in executable mode
        """
        self.found(raise_error=True)

        job_inputs = self.build_input(input_model, config)
        success, dexe = self.execute(job_inputs)
        if "There is an error in the input file" in dexe["moldft"]["stdout"]:
            raise InputError(dexe["moldft"]["stdout"])
        if "not compiled" in dexe["moldft"]["stdout"]:
            # recoverable with a different compilation with optional modules
            raise InputError(dexe["moldft"]["stdout"])
        if success:
            num_commands = len(dexe)
            if num_commands == 2:
                dexe["moldft"]["outfiles"]["stdout"] = dexe["moldft"]["stdout"]
                dexe["moldft"]["outfiles"]["stderr"] = dexe["moldft"]["stderr"]
                dexe["molresponse"]["outfiles"]["stdout"] = dexe[
                    "molresponse"]["stdout"]
                dexe["molresponse"]["outfiles"]["stderr"] = dexe[
                    "molresponse"]["stderr"]
            else:
                dexe["moldft"]["outfiles"]["stdout"] = dexe["moldft"]["stdout"]
                dexe["moldft"]["outfiles"]["stderr"] = dexe["moldft"]["stderr"]
            return self.parse_output(dexe, input_model)
        else:
            print(dexe["stdout"])

            raise UnknownError(dexe["stderr"])
Ejemplo n.º 6
0
    def parse_output(
        self, outfiles: Dict[str, str], input_model: "AtomicInput"
    ) -> AtomicResult:  # lgtm: [py/similar-function]

        # Get the stdout from the calculation (required)
        stdout = outfiles.pop("stdout")
        stderr = outfiles.pop("stderr")

        # Read the NWChem stdout file and, if needed, the hess or grad files
        try:
            qcvars, nwhess, nwgrad, nwmol, version, errorTMP = harvest(input_model.molecule, stdout, **outfiles)
        except Exception as e:
            raise UnknownError(stdout)

        if nwgrad is not None:
            qcvars[f"{input_model.model.method.upper()[4:]} TOTAL GRADIENT"] = nwgrad
            qcvars["CURRENT GRADIENT"] = nwgrad
        if nwhess is not None:
            qcvars["CURRENT HESSIAN"] = nwhess

        # Normalize the output as a float or list of floats
        if input_model.driver.upper() == "PROPERTIES":
            retres = qcvars[f"CURRENT ENERGY"]
        else:
            retres = qcvars[f"CURRENT {input_model.driver.upper()}"]
        if isinstance(retres, Decimal):
            retres = float(retres)
        elif isinstance(retres, np.ndarray):
            retres = retres.tolist()

        # Get the formatted properties
        build_out(qcvars)
        atprop = build_atomicproperties(qcvars)

        # Format them inout an output
        output_data = {
            "schema_version": 1,
            "extras": {"outfiles": outfiles, **input_model.extras},
            "properties": atprop,
            "provenance": Provenance(creator="NWChem", version=self.get_version(), routine="nwchem"),
            "return_result": retres,
            "stderr": stderr,
            "stdout": stdout,
            "success": True,
        }

        # got to even out who needs plump/flat/Decimal/float/ndarray/list
        # Decimal --> str preserves precision
        # * formerly unnp(qcvars, flat=True).items()
        output_data["extras"]["qcvars"] = {
            k.upper(): str(v) if isinstance(v, Decimal) else v for k, v in qcvars.items()
        }

        return AtomicResult(**{**input_model.dict(), **output_data})
Ejemplo n.º 7
0
    def compute(self, input_data: OptimizationInput,
                config: TaskConfig) -> "BaseModel":
        nwc_harness = NWChemHarness()
        self.found(raise_error=True)

        # Unify the keywords from the OptimizationInput and QCInputSpecification
        #  Optimization input will override, but don't tell users this as it seems unnecessary
        keywords = input_data.keywords.copy()
        keywords.update(input_data.input_specification.keywords)
        if keywords.get("program", "nwchem").lower() != "nwchem":
            raise InputError("NWChemDriver procedure only works with NWChem")

        # Make an atomic input
        atomic_input = AtomicInput(
            molecule=input_data.initial_molecule,
            driver="energy",
            keywords=keywords,
            **input_data.input_specification.dict(
                exclude={"driver", "keywords"}),
        )

        # Build the inputs for the job
        job_inputs = nwc_harness.build_input(atomic_input, config)

        # Replace the last line with a "task {} optimize"
        input_file: str = job_inputs["infiles"]["nwchem.nw"].strip()
        beginning, last_line = input_file.rsplit("\n", 1)
        assert last_line.startswith("task")
        last_line = f"task {last_line.split(' ')[1]} optimize"
        job_inputs["infiles"]["nwchem.nw"] = f"{beginning}\n{last_line}"

        # Run it!
        success, dexe = nwc_harness.execute(job_inputs)

        # Check for common errors
        if "There is an error in the input file" in dexe["stdout"]:
            raise InputError(dexe["stdout"])
        if "not compiled" in dexe["stdout"]:
            # recoverable with a different compilation with optional modules
            raise InputError(dexe["stdout"])

        # Parse it
        if success:
            dexe["outfiles"]["stdout"] = dexe["stdout"]
            dexe["outfiles"]["stderr"] = dexe["stderr"]
            return self.parse_output(dexe["outfiles"], input_data)
        else:
            raise UnknownError(dexe["stdout"])
Ejemplo n.º 8
0
 def compute(self, input_data: AtomicInput,
             config: "TaskConfig") -> AtomicResult:
     """
     Run the compute job via the gaussian CLI.
     """
     # we always use an internal template
     job_inputs = self.build_input(input_model=input_data, config=config)
     # check for extra output files
     if "gaussian.wfx" in input_data.keywords.get("add_input", []):
         extra_outfiles = ["gaussian.wfx"]
     else:
         extra_outfiles = None
     exe_success, proc = self.execute(job_inputs,
                                      extra_outfiles=extra_outfiles)
     if exe_success:
         result = self.parse_output(proc["outfiles"], input_data)
         return result
     else:
         raise UnknownError(proc["stderr"])
Ejemplo n.º 9
0
    def compute(self, input_model: AtomicInput,
                config: "TaskConfig") -> AtomicResult:
        """
        Runs NWChem in executable mode
        """
        self.found(raise_error=True)

        job_inputs = self.build_input(input_model, config)
        success, dexe = self.execute(job_inputs)

        if "There is an error in the input file" in dexe["stdout"]:
            raise InputError(dexe["stdout"])
        if "not compiled" in dexe["stdout"]:
            # recoverable with a different compilation with optional modules
            raise InputError(dexe["stdout"])

        if success:
            dexe["outfiles"]["stdout"] = dexe["stdout"]
            dexe["outfiles"]["stderr"] = dexe["stderr"]
            return self.parse_output(dexe["outfiles"], input_model)
        else:
            raise UnknownError(dexe["stdout"])
Ejemplo n.º 10
0
    def parse_output(
        self, outfiles: Dict[str, str], input_model: "AtomicInput"
    ) -> AtomicResult:  # lgtm: [py/similar-function]

        # Get the stdout from the calculation (required)
        stdout = outfiles.pop("stdout")
        stderr = outfiles.pop("stderr")

        method = input_model.model.method.lower()
        method = method[4:] if method.startswith("nwc-") else method

        # Read the NWChem stdout file and, if needed, the hess or grad files
        # July 2021: nwmol & vector returns now atin/outfile orientation depending on fix_com,orientation=T/F. previously always atin orientation
        try:
            qcvars, nwhess, nwgrad, nwmol, version, module, errorTMP = harvest(
                input_model.molecule, method, stdout, **outfiles)
        except Exception:
            raise UnknownError(error_stamp(outfiles["input"], stdout, stderr))

        try:
            if nwgrad is not None:
                qcvars[f"{method.upper()} TOTAL GRADIENT"] = nwgrad
                qcvars["CURRENT GRADIENT"] = nwgrad

            if nwhess is not None:
                qcvars[f"{method.upper()} TOTAL HESSIAN"] = nwhess
                qcvars["CURRENT HESSIAN"] = nwhess

            # Normalize the output as a float or list of floats
            if input_model.driver.upper() == "PROPERTIES":
                retres = qcvars[f"CURRENT ENERGY"]
            else:
                retres = qcvars[f"CURRENT {input_model.driver.upper()}"]
        except KeyError:
            raise UnknownError(error_stamp(outfiles["input"], stdout, stderr))

        if isinstance(retres, Decimal):
            retres = float(retres)
        elif isinstance(retres, np.ndarray):
            retres = retres.tolist()

        # Get the formatted properties
        build_out(qcvars)
        atprop = build_atomicproperties(qcvars)

        provenance = Provenance(creator="NWChem",
                                version=self.get_version(),
                                routine="nwchem").dict()
        if module is not None:
            provenance["module"] = module

        # Format them inout an output
        output_data = {
            "schema_version": 1,
            "molecule":
            nwmol,  # overwrites with outfile Cartesians in case fix_*=F
            "extras": {
                **input_model.extras
            },
            "native_files":
            {k: v
             for k, v in outfiles.items() if v is not None},
            "properties": atprop,
            "provenance": provenance,
            "return_result": retres,
            "stderr": stderr,
            "stdout": stdout,
            "success": True,
        }

        # got to even out who needs plump/flat/Decimal/float/ndarray/list
        # Decimal --> str preserves precision
        # * formerly unnp(qcvars, flat=True).items()
        output_data["extras"]["qcvars"] = {
            k.upper(): str(v) if isinstance(v, Decimal) else v
            for k, v in qcvars.items()
        }

        return AtomicResult(**{**input_model.dict(), **output_data})
Ejemplo n.º 11
0
    def compute(
        self, input_data: "OptimizationInput", config: "TaskConfig"
    ) -> Union["OptimizationResult", "FailedOperation"]:
        try:
            import berny
        except ModuleNotFoundError:
            raise ModuleNotFoundError("Could not find Berny in the Python path.")

        # Get berny version from the installed package, use setuptools'
        # pkg_resources for python < 3.8
        if sys.version_info >= (3, 8):
            from importlib.metadata import distribution
        else:
            from pkg_resources import get_distribution as distribution
        berny_version = distribution("pyberny").version

        # Berny uses the stdlib logging module and by default uses per-module
        # loggers. For QCEngine, we create one logger per BernyProcedure
        # instance, by using the instance's id(), and send all logging messages
        # to a string stream
        log_stream = StringIO()
        log = logging.getLogger(f"{__name__}.{id(self)}")
        log.addHandler(logging.StreamHandler(log_stream))
        log.setLevel("INFO")

        input_data = input_data.dict()
        geom_qcng = input_data["initial_molecule"]
        comput = {**input_data["input_specification"], "molecule": geom_qcng}
        program = input_data["keywords"].pop("program")
        trajectory = []
        output_data = input_data.copy()
        try:
            # Pyberny uses angstroms for the Cartesian geometry, but atomic
            # units for everything else, including the gradients (hartree/bohr).
            geom_berny = berny.Geometry(geom_qcng["symbols"], geom_qcng["geometry"] / berny.angstrom)
            opt = berny.Berny(geom_berny, logger=log, **input_data["keywords"])
            for geom_berny in opt:
                geom_qcng["geometry"] = np.stack(geom_berny.coords * berny.angstrom)
                ret = qcengine.compute(comput, program)
                if ret.success:
                    trajectory.append(ret.dict())
                    opt.send((ret.properties.return_energy, ret.return_result))
                else:
                    # qcengine.compute returned FailedOperation
                    raise UnknownError("Gradient computation failed")

        except UnknownError:
            error = ret.error.dict()  # ComputeError
        except Exception:
            error = {"error_type": "unknown", "error_message": f"Berny error:\n{traceback.format_exc()}"}
        else:
            output_data["success"] = True
            output_data.update(
                {
                    "schema_name": "qcschema_optimization_output",
                    "final_molecule": trajectory[-1]["molecule"],
                    "energies": [r["properties"]["return_energy"] for r in trajectory],
                    "trajectory": trajectory,
                    "provenance": {"creator": "Berny", "routine": "berny.Berny", "version": berny_version},
                    "stdout": log_stream.getvalue(),  # collect logged messages
                }
            )
            return OptimizationResult(**output_data)
        return FailedOperation(input_data=input_data, error=error)
Ejemplo n.º 12
0
    def parse_output(
        self, outfiles: Dict[str, str], input_model: "AtomicInput"
    ) -> AtomicResult:  # lgtm: [py/similar-function]

        # Get the stdout from the calculation (required)
        stdout = outfiles.pop("stdout")
        stderr = outfiles.pop("stderr")

        method = input_model.model.method.lower()
        method = method[4:] if method.startswith("nwc-") else method

        # Read the NWChem stdout file and, if needed, the hess or grad files
        # LW 7Jul21: I allow exceptions to be raised so that we can detect errors
        #   in the parsing of output files
        qcvars, nwhess, nwgrad, nwmol, version, module, errorTMP = harvest(
            input_model.molecule, method, stdout, **outfiles)

        try:
            if nwgrad is not None:
                qcvars[f"{method.upper()} TOTAL GRADIENT"] = nwgrad
                qcvars["CURRENT GRADIENT"] = nwgrad

            if nwhess is not None:
                qcvars[f"{method.upper()} TOTAL HESSIAN"] = nwhess
                qcvars["CURRENT HESSIAN"] = nwhess

            # Normalize the output as a float or list of floats
            if input_model.driver.upper() == "PROPERTIES":
                retres = qcvars[f"CURRENT ENERGY"]
            else:
                retres = qcvars[f"CURRENT {input_model.driver.upper()}"]
        except KeyError as e:
            raise UnknownError(
                "STDOUT:\n" + stdout + "\nSTDERR:\n" + stderr +
                "\nTRACEBACK:\n" +
                "".join(traceback.format_exception(*sys.exc_info())))

        if isinstance(retres, Decimal):
            retres = float(retres)
        elif isinstance(retres, np.ndarray):
            retres = retres.tolist()

        # Get the formatted properties
        build_out(qcvars)
        atprop = build_atomicproperties(qcvars)

        provenance = Provenance(creator="NWChem",
                                version=self.get_version(),
                                routine="nwchem").dict()
        if module is not None:
            provenance["module"] = module

        # Format them inout an output
        output_data = {
            "schema_version": 1,
            "extras": {
                "outfiles": outfiles,
                **input_model.extras
            },
            "properties": atprop,
            "provenance": provenance,
            "return_result": retres,
            "stderr": stderr,
            "stdout": stdout,
            "success": True,
        }

        # got to even out who needs plump/flat/Decimal/float/ndarray/list
        # Decimal --> str preserves precision
        # * formerly unnp(qcvars, flat=True).items()
        output_data["extras"]["qcvars"] = {
            k.upper(): str(v) if isinstance(v, Decimal) else v
            for k, v in qcvars.items()
        }

        return AtomicResult(**{**input_model.dict(), **output_data})