Exemplo n.º 1
0
def run_qcschema(input_data, clean=True):

    outfile = os.path.join(core.IOManager.shared_object().get_default_path(),
                           str(uuid.uuid4()) + ".qcschema_tmpout")
    core.set_output_file(outfile, False)
    print_header()
    start_time = datetime.datetime.now()

    try:
        input_model = qcng.util.model_wrapper(input_data,
                                              qcel.models.AtomicInput)

        keep_wfn = input_model.protocols.wavefunction != 'none'

        # qcschema should be copied
        ret_data = run_json_qcschema(input_model.dict(),
                                     clean,
                                     False,
                                     keep_wfn=keep_wfn)
        ret_data["provenance"] = {
            "creator": "Psi4",
            "version": __version__,
            "routine": "psi4.schema_runner.run_qcschema"
        }

        exit_printing(start_time=start_time, success=True)

        ret = qcel.models.AtomicResult(**ret_data,
                                       stdout=_read_output(outfile))

    except Exception as exc:

        if not isinstance(input_data, dict):
            input_data = input_data.dict()

        input_data = input_data.copy()
        input_data["stdout"] = _read_output(outfile)
        ret = qcel.models.FailedOperation(
            input_data=input_data,
            success=False,
            error={
                'error_type':
                type(exc).__name__,
                'error_message':
                ''.join(traceback.format_exception(*sys.exc_info())),
            })

    atexit.register(_quiet_remove, outfile)

    return ret
Exemplo n.º 2
0
def run_qcschema(
    input_data: Union[Dict[str, Any], qcel.models.AtomicInput],
    clean: bool = True,
    postclean: bool = True
) -> Union[qcel.models.AtomicResult, qcel.models.FailedOperation]:
    """Run a quantum chemistry job specified by :py:class:`qcelemental.models.AtomicInput` **input_data** in |PSIfour|.

    Parameters
    ----------
    input_data
        Quantum chemistry job in either AtomicInput class or dictionary form.
    clean
        Reset global QCVariables, options, and scratch files to default state.
    postclean
        When ``False``, *remove* the output file since absorbed into AtomicResult.
        When ``True``, simply *close* the output file. True is useful when calling
        from a Psi4 session to avoid removing the parent Psi4's output file.

    Returns
    -------
    qcelemental.models.AtomicResult
        Full record of quantum chemistry calculation, including output text. Returned upon job success.
    qcelemental.models.FailedOperation
        Record to diagnose calculation failure, including output text and input specification. Returned upon job failure.

    """
    outfile = os.path.join(core.IOManager.shared_object().get_default_path(),
                           str(uuid.uuid4()) + ".qcschema_tmpout")
    core.set_output_file(outfile, False)
    print_header()

    start_time = datetime.datetime.now()

    try:
        input_model = qcng.util.model_wrapper(input_data,
                                              qcel.models.AtomicInput)

        # Echo the infile on the outfile
        core.print_out("\n  ==> Input QCSchema <==\n")
        core.print_out(
            "\n--------------------------------------------------------------------------\n"
        )
        core.print_out(pp.pformat(json.loads(input_model.json())))
        core.print_out(
            "\n--------------------------------------------------------------------------\n"
        )

        keep_wfn = input_model.protocols.wavefunction != 'none'

        # qcschema should be copied
        ret_data = run_json_qcschema(input_model.dict(),
                                     clean,
                                     False,
                                     keep_wfn=keep_wfn)
        ret_data["provenance"].update({
            "creator":
            "Psi4",
            "version":
            __version__,
            "routine":
            "psi4.schema_runner.run_qcschema"
        })
        ret_data["native_files"]["input"] = json.dumps(json.loads(
            input_model.json()),
                                                       indent=1)

        exit_printing(start_time=start_time, success=True)

        ret = qcel.models.AtomicResult(**ret_data,
                                       stdout=_read_output(outfile))

    except Exception as exc:

        if not isinstance(input_data, dict):
            input_data = input_data.dict()

        input_data = input_data.copy()
        input_data["stdout"] = _read_output(outfile)
        ret = qcel.models.FailedOperation(
            input_data=input_data,
            success=False,
            error={
                'error_type':
                type(exc).__name__,
                'error_message':
                ''.join(traceback.format_exception(*sys.exc_info())),
            })

    _clean_psi_output(postclean, outfile)

    return ret