示例#1
0
文件: fchk.py 项目: yychuang/psi4
def compare_fchkfiles(expected, computed, digits, label):
    # """Function to compare two FCHK files.

    # an older format description can be found here
    # http://wild.life.nctu.edu.tw/~jsyu/compchem/g09/g09ur/f_formchk.htm
    # It lists more fields (logical, character) that are not included in this
    # test function. They should be covered by the string comparison.
    # This function is only meant to work with PSI4's FCHK files.
    #
    # :param expected: reference FCHK file name
    # :param computed: computed FCHK file name
    # :param digits: tolerance for high accuracy fields -- 1.e-8 or 1.e-9 suitable
    # :param label: string labelling the test
    # """

    fchk_ref = fchkfile_to_string(expected).splitlines()
    fchk_calc = fchkfile_to_string(computed).splitlines()

    high_accuracy = digits
    low_accuracy = 3

    # Those listed below need super high scf convergence (d_conv 1e-12) and might
    # show machine dependence. They will be tested with low_accuracy.
    sensitive = ['Current cartesian coordinates', 'MO coefficients']

    if len(fchk_ref) != len(fchk_calc):
        raise ValidationError('The two FCHK files to compare have a different file length! \n')

    index = 0
    max_length = len(fchk_calc)
    tests = []
    for start in range(max_length):
        if index >= max_length:
            break
        line = fchk_calc[index]
        if "N=" in line:
            offset, calc = _consume_fchk_section(fchk_calc, index)
            _, ref = _consume_fchk_section(fchk_ref, index)
            if any(x in line for x in sensitive):
                test = compare_arrays(ref, calc, low_accuracy, f" matrix section: {line}")
            else:
                test = compare_arrays(ref, calc, high_accuracy, f" matrix section: {line}")
            index += offset
        elif " R " in line and not "N=" in line:
            calc = line.split()[-1]
            ref = fchk_ref[index].split()[-1]
            test = compare_values(ref, calc, high_accuracy, f" float value: {line}")
            index += 1
        elif " I " in line and not "N=" in line:
            calc = line.split()[-1]
            ref = fchk_ref[index].split()[-1]
            test = compare_integers(ref, calc, f" int value: {line}")
            index += 1
        else:
            test = compare_strings(line, fchk_ref[index], f"FCK text line {index+1}.")
            index += 1
        tests.append(test)

    return compare_integers(True, all(tests), label)
示例#2
0
文件: fchk.py 项目: philipmnel/psi4
def compare_fchkfiles(expected, computed, atol_exponent, label):
    """Comparison function for output data in FCHK (formatted checkpoint) file format.
    Compares many fields including number of electrons, highest angular momentum, basis
    set exponents, densities, final gradient.

    Note only Psi4-style signature (``(expected, computed, atol_exponent, label)``) available.

    An older format description can be found here
    http://wild.life.nctu.edu.tw/~jsyu/compchem/g09/g09ur/f_formchk.htm
    It lists more fields (logical, character) that are not included in this
    test function. They should be covered by the string comparison.
    This function is only meant to work with PSI4's FCHK files.

    Parameters
    ----------
    expected : file
        Reference FCHK file against which `computed` is compared.
    computed : file
        Input FCHK file to compare against `expected`.
    atol_exponent : int or float
        Absolute tolerance for high accuracy fields -- 1.e-8 or 1.e-9 is suitable.
        Values less than one are taken literally; one or greater taken as decimal digits for comparison.
        So `1` means `atol=0.1` and `2` means `atol=0.01` but `0.04` means `atol=0.04`
        Note that the largest expressable processed atol will be `~0.99`.
    label : str
        Label for passed and error messages.

    """
    fchk_ref = fchkfile_to_string(expected).splitlines()
    fchk_calc = fchkfile_to_string(computed).splitlines()

    high_accuracy = atol_exponent
    low_accuracy = 3

    # Those listed below need super high scf convergence (d_conv 1e-12) and might
    # show machine dependence. They will be tested with low_accuracy.
    sensitive = ['Current cartesian coordinates', 'MO coefficients']

    if len(fchk_ref) != len(fchk_calc):
        raise ValidationError(
            'The two FCHK files to compare have a different file length! \n')

    index = 0
    max_length = len(fchk_calc)
    tests = []
    for start in range(max_length):
        if index >= max_length:
            break
        line = fchk_calc[index]
        if "N=" in line:
            offset, calc = _consume_fchk_section(fchk_calc, index)
            _, ref = _consume_fchk_section(fchk_ref, index)
            if any(x in line for x in sensitive):
                test = compare_arrays(ref, calc, low_accuracy,
                                      f" matrix section: {line}")
            else:
                test = compare_arrays(ref, calc, high_accuracy,
                                      f" matrix section: {line}")
            index += offset
        elif " R " in line and "N=" not in line:
            calc = line.split()[-1]
            ref = fchk_ref[index].split()[-1]
            test = compare_values(ref, calc, high_accuracy,
                                  f" float value: {line}")
            index += 1
        elif " I " in line and "N=" not in line:
            calc = line.split()[-1]
            ref = fchk_ref[index].split()[-1]
            test = compare_integers(ref, calc, f" int value: {line}")
            index += 1
        else:
            test = compare_strings(line, fchk_ref[index],
                                   f"FCK text line {index+1}.")
            index += 1
        tests.append(test)

    return compare_integers(True, all(tests), label)