Ejemplo n.º 1
0
def _fix_molden_from_buggy_codes(result, filename):
    '''Detect errors in the data loaded from a molden/mkl/... file and correct.

       **Argument:**

       result
            A dictionary with the data loaded in the ``load_molden`` function.

       This function can recognize erroneous files created by PSI4 and ORCA. The
       data in the obasis and signs fields will be updated accordingly.
    '''
    obasis = result['obasis']
    permutation = result.get('permutation', None)
    if _is_normalized_properly(result['lf'], obasis, permutation,
                               result['exp_alpha'], result.get('exp_beta')):
        # The file is good. No need to change data.
        return
    if log.do_medium:
        log('Detected incorrect normalization of orbitals loaded from a file.')
    # Try to fix it as if it was a file generated by ORCA.
    orca_signs = _get_orca_signs(obasis)
    orca_con_coeffs = _get_fixed_con_coeffs(obasis, 'orca')
    orca_obasis = GOBasis(obasis.centers, obasis.shell_map, obasis.nprims,
                          obasis.shell_types, obasis.alphas, orca_con_coeffs)
    if _is_normalized_properly(result['lf'], orca_obasis,
                               permutation, result['exp_alpha'],
                               result.get('exp_beta'), orca_signs):
        if log.do_medium:
            log('Detected typical ORCA errors in file. Fixing them...')
        result['obasis'] = orca_obasis
        result['signs'] = orca_signs
        return
    # Try to fix it as if it was a file generated by PSI4.
    psi4_con_coeffs = _get_fixed_con_coeffs(obasis, 'psi4')
    psi4_obasis = GOBasis(obasis.centers, obasis.shell_map, obasis.nprims,
                          obasis.shell_types, obasis.alphas, psi4_con_coeffs)
    if _is_normalized_properly(result['lf'], psi4_obasis, permutation,
                               result['exp_alpha'], result.get('exp_beta')):
        if log.do_medium:
            log('Detected typical PSI4 errors in file. Fixing them...')
        result['obasis'] = psi4_obasis
        return

    raise IOError(('Could not correct the data read from %s. The molden or '
                   'mkl file you are trying to load contains errors. Please '
                   'report this problem to [email protected], so he '
                   'can fix it.') % filename)
Ejemplo n.º 2
0
    def helper_obasis(f, coordinates):
        from horton.gbasis.io import str_to_shell_types
        from horton.gbasis.gobasis import GOBasis
        shell_types = []
        shell_map = []
        nprims = []
        alphas = []
        con_coeffs = []

        center_counter = 0
        in_shell = False
        nprim = None
        while True:
            line = f.readline()
            lstrip = line.strip()
            if len(line) == 0 or lstrip == '$END':
                break
            if len(lstrip) == 0:
                continue
            if lstrip == '$$':
                center_counter += 1
                in_shell = False
            else:
                words = line.split()
                if len(words) == 2:
                    assert in_shell
                    alpha = float(words[0])
                    alphas.append(alpha)
                    con_coeff = renorm_helper(float(words[1]), alpha,
                                              shell_type)
                    con_coeffs.append(con_coeff)
                    nprim += 1
                else:
                    if nprim is not None:
                        nprims.append(nprim)
                    shell_map.append(center_counter)
                    # always assume pure basis functions
                    shell_type = str_to_shell_types(words[1], pure=True)[0]
                    shell_types.append(shell_type)
                    in_shell = True
                    nprim = 0
        if nprim is not None:
            nprims.append(nprim)

        shell_map = np.array(shell_map)
        nprims = np.array(nprims)
        shell_types = np.array(shell_types)
        alphas = np.array(alphas)
        con_coeffs = np.array(con_coeffs)
        return GOBasis(coordinates, shell_map, nprims, shell_types, alphas,
                       con_coeffs)
Ejemplo n.º 3
0
    def helper_obasis(f, coordinates, pure):
        '''Load the orbital basis'''
        shell_types = []
        shell_map = []
        nprims = []
        alphas = []
        con_coeffs = []

        icenter = 0
        in_atom = False
        in_shell = False
        while True:
            last_pos = f.tell()
            line = f.readline()
            if len(line) == 0:
                break
            words = line.split()
            if len(words) == 0:
                in_atom = False
                in_shell = False
            elif len(words) == 2 and not in_atom:
                icenter = int(words[0]) - 1
                in_atom = True
                in_shell = False
            elif len(words) == 3:
                in_shell = True
                shell_map.append(icenter)
                shell_label = words[0].lower()
                shell_type = str_to_shell_types(shell_label,
                                                pure.get(shell_label,
                                                         False))[0]
                shell_types.append(shell_type)
                nprims.append(int(words[1]))
            elif len(words) == 2 and in_atom:
                assert in_shell
                alpha = float(words[0].replace('D', 'E'))
                alphas.append(alpha)
                con_coeff = float(words[1].replace('D', 'E'))
                con_coeffs.append(con_coeff)
            else:
                # done, go back one line
                f.seek(last_pos)
                break

        shell_map = np.array(shell_map)
        nprims = np.array(nprims)
        shell_types = np.array(shell_types)
        alphas = np.array(alphas)
        con_coeffs = np.array(con_coeffs)
        return GOBasis(coordinates, shell_map, nprims, shell_types, alphas,
                       con_coeffs)
Ejemplo n.º 4
0
    def helper_obasis(f, coordinates):
        '''Load the orbital basis'''
        shell_types = []
        shell_map = []
        nprims = []
        alphas = []
        con_coeffs = []

        icenter = 0
        in_atom = False
        in_shell = False
        while True:
            last_pos = f.tell()
            line = f.readline()
            if len(line) == 0:
                break
            words = line.split()
            if len(words) == 0:
                in_atom = False
                in_shell = False
            elif len(words) == 2 and not in_atom:
                icenter = int(words[0])-1
                in_atom = True
                in_shell = False
            elif len(words) == 3:
                in_shell = True
                shell_map.append(icenter)
                # always assume pure basis functions
                shell_type = str_to_shell_types(words[0], True)[0]
                shell_types.append(shell_type)
                nprims.append(int(words[1]))
            elif len(words) == 2 and in_atom:
                assert in_shell
                alpha = float(words[0])
                alphas.append(alpha)
                con_coeff = renorm_helper(float(words[1]), alpha, shell_type)
                con_coeffs.append(con_coeff)
            else:
                # done, go back one line
                f.seek(last_pos)
                break

        shell_map = np.array(shell_map)
        nprims = np.array(nprims)
        shell_types = np.array(shell_types)
        alphas = np.array(alphas)
        con_coeffs = np.array(con_coeffs)
        return GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs)
Ejemplo n.º 5
0
def load_wfn(filename, lf):
    """Load data from a WFN file.

       **Arguments:**

       filename
            The filename of the wfn file.

       lf
            An instance of LinalgFactory, used to initialize the wavefunction
            expansions.

       **Returns:** a dictionary with ``title``, ``coordinates``, ``numbers``,
       ``energy``, ``obasis`` and ``exp_alpha``. May contain ``exp_beta``.
    """
    title, numbers, coordinates, centers, type_assignment, exponents, \
        mo_count, mo_occ, mo_energy, coefficients, energy = load_wfn_low(filename)
    permutation = get_permutation_basis(type_assignment)
    # permute arrays containing wfn data
    type_assignment = type_assignment[permutation]
    mask = get_mask(type_assignment)
    reduced_size = np.array(mask, int).sum()
    num_mo = coefficients.shape[1]
    alphas = np.empty(reduced_size)
    alphas[:] = exponents[permutation][mask]
    assert (centers == centers[permutation]).all()
    shell_map = centers[mask]
    # cartesian basis: {S:0, P:1, D:2, F:3, G:4, H:5}
    shell = {1: 0, 2: 1, 5: 2, 11: 3, 21: 4, 36: 5}
    shell_types = type_assignment[mask]
    shell_types = np.array([shell[i] for i in shell_types])
    assert shell_map.size == shell_types.size == reduced_size
    nprims = np.ones(reduced_size, int)
    con_coeffs = np.ones(reduced_size)
    # build basis set
    obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas,
                     con_coeffs)
    if lf.default_nbasis is not None and lf.default_nbasis != obasis.nbasis:
        raise TypeError(
            'The value of lf.default_nbasis does not match nbasis reported in the wfn file.'
        )
    lf.default_nbasis = obasis.nbasis
    coefficients = coefficients[permutation]
    coefficients /= obasis.get_scales().reshape(-1, 1)
    # make the wavefunction
    if mo_occ.max() > 1.0:
        # close shell system
        exp_alpha = lf.create_expansion(obasis.nbasis, coefficients.shape[1])
        exp_alpha.coeffs[:] = coefficients
        exp_alpha.energies[:] = mo_energy
        exp_alpha.occupations[:] = mo_occ / 2
        exp_beta = None
    else:
        # open shell system
        # counting the number of alpha and beta orbitals
        index = 1
        while index < num_mo and mo_energy[index] >= mo_energy[
                index - 1] and mo_count[index] == mo_count[index - 1] + 1:
            index += 1
        exp_alpha = lf.create_expansion(obasis.nbasis, index)
        exp_alpha.coeffs[:] = coefficients[:, :index]
        exp_alpha.energies[:] = mo_energy[:index]
        exp_alpha.occupations[:] = mo_occ[:index]
        exp_beta = lf.create_expansion(obasis.nbasis, num_mo - index)
        exp_beta.coeffs[:] = coefficients[:, index:]
        exp_beta.energies[:] = mo_energy[index:]
        exp_beta.occupations[:] = mo_occ[index:]

    result = {
        'title': title,
        'coordinates': coordinates,
        'exp_alpha': exp_alpha,
        'lf': lf,
        'numbers': numbers,
        'obasis': obasis,
        'energy': energy,
    }
    if exp_beta is not None:
        result['exp_beta'] = exp_beta
    return result
Ejemplo n.º 6
0
def load_wfn(filename, lf):
    """Load data from a WFN file.

       **Arguments:**

       filename
            The filename of the wfn file.

       lf
            An instance of LinalgFactory, used to initialize the wavefunction
            expansions.

       **Returns:** a dictionary with ``title``, ``coordinates``, ``numbers``,
       ``energy``, ``obasis`` and ``exp_alpha``. May contain ``exp_beta``.
    """
    title, numbers, coordinates, centers, type_assignment, exponents, \
        mo_count, mo_occ, mo_energy, coefficients, energy = load_wfn_low(filename)
    permutation = get_permutation_basis(type_assignment)
    # permute arrays containing wfn data
    type_assignment = type_assignment[permutation]
    mask = get_mask(type_assignment)
    reduced_size = np.array(mask, int).sum()
    num_mo = coefficients.shape[1]
    alphas = np.empty(reduced_size)
    alphas[:] = exponents[permutation][mask]
    assert (centers == centers[permutation]).all()
    shell_map = centers[mask]
    # cartesian basis: {S:0, P:1, D:2, F:3, G:4, H:5}
    shell = {1: 0, 2: 1, 5: 2, 11: 3, 21: 4, 36: 5}
    shell_types = type_assignment[mask]
    shell_types = np.array([shell[i] for i in shell_types])
    assert shell_map.size == shell_types.size == reduced_size
    nprims = np.ones(reduced_size, int)
    con_coeffs = np.ones(reduced_size)
    # build basis set
    obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs)
    if lf.default_nbasis is not None and lf.default_nbasis != obasis.nbasis:
        raise TypeError(
            'The value of lf.default_nbasis does not match nbasis reported in the wfn file.')
    lf.default_nbasis = obasis.nbasis
    coefficients = coefficients[permutation]
    coefficients /= obasis.get_scales().reshape(-1, 1)
    # make the wavefunction
    if mo_occ.max() > 1.0:
        # close shell system
        exp_alpha = lf.create_expansion(obasis.nbasis, coefficients.shape[1])
        exp_alpha.coeffs[:] = coefficients
        exp_alpha.energies[:] = mo_energy
        exp_alpha.occupations[:] = mo_occ / 2
        exp_beta = None
    else:
        # open shell system
        # counting the number of alpha and beta orbitals
        index = 1
        while index < num_mo and mo_energy[index] >= mo_energy[index - 1] and mo_count[index] == mo_count[index - 1] + 1:
            index += 1
        exp_alpha = lf.create_expansion(obasis.nbasis, index)
        exp_alpha.coeffs[:] = coefficients[:, :index]
        exp_alpha.energies[:] = mo_energy[:index]
        exp_alpha.occupations[:] = mo_occ[:index]
        exp_beta = lf.create_expansion(obasis.nbasis, num_mo - index)
        exp_beta.coeffs[:] = coefficients[:, index:]
        exp_beta.energies[:] = mo_energy[index:]
        exp_beta.occupations[:] = mo_occ[index:]

    result = {
        'title': title,
        'coordinates': coordinates,
        'exp_alpha': exp_alpha,
        'lf': lf,
        'numbers': numbers,
        'obasis': obasis,
        'energy': energy,
    }
    if exp_beta is not None:
        result['exp_beta'] = exp_beta
    return result
Ejemplo n.º 7
0
def load_wfn(filename, lf):
    '''Load data from a WFN file

       **Arguments:**

       filename
            The filename of the wfn file.

       lf
            An instance of LinalgFactory, used to initialize the wavefunction
            expansions.
    '''
    numbers, coordinates, centers, type_assignment, exponents, mo_count, mo_occ, mo_energy, coefficients = load_wfn_low(
        filename)
    permutation = setup_permutation2(type_assignment)
    #Permuting the arrays containing the wfn data
    type_assignment = type_assignment[permutation]
    mask = setup_mask(type_assignment)
    reduced_size = np.array(mask, int).sum()
    num_mo = coefficients.shape[1]
    alphas = np.empty(reduced_size)
    alphas[:] = exponents[permutation][mask]
    assert (centers == centers[permutation]).all()
    shell_map = centers[mask]
    shell = {
        1: 0,
        2: 1,
        5: 2,
        11: 3,
        21: 4,
        36: 5
    }  #Cartesian: {S:0, P:1, D:2, F:3, G:4, H:5}
    shell_types = type_assignment[mask]
    shell_types = np.array([shell[i] for i in shell_types])
    assert shell_map.size == shell_types.size == reduced_size
    nprims = np.ones(reduced_size, int)
    con_coeffs = np.ones(reduced_size)
    #Building the basis set
    from horton.gbasis.gobasis import GOBasis
    obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas,
                     con_coeffs)
    coefficients = coefficients[permutation]
    coefficients /= obasis.get_scales().reshape(-1, 1)
    #Making the wavefunction:
    if mo_occ.max() > 1.0:
        #close shell system
        nelec = int(round(mo_occ.sum()))
        wfn = RestrictedWFN(lf, obasis.nbasis, norb=coefficients.shape[1])
        exp_alpha = wfn.init_exp('alpha')
        exp_alpha.coeffs[:] = coefficients
        exp_alpha.energies[:] = mo_energy
        exp_alpha.occupations[:] = mo_occ / 2
    else:
        #open shell system
        #counting the number of alpha and beta orbitals
        index = 1
        while index < num_mo and mo_energy[index] >= mo_energy[
                index - 1] and mo_count[index] == mo_count[index - 1] + 1:
            index += 1
        wfn = UnrestrictedWFN(lf, obasis.nbasis)
        exp_alpha = wfn.init_exp('alpha', norb=index)
        exp_alpha.coeffs[:] = coefficients[:, :index]
        exp_alpha.energies[:] = mo_energy[:index]
        exp_alpha.occupations[:] = mo_occ[:index]
        exp_beta = wfn.init_exp('beta', norb=num_mo - index)
        exp_beta.coeffs[:] = coefficients[:, index:]
        exp_beta.energies[:] = mo_energy[index:]
        exp_beta.occupations[:] = mo_occ[index:]

    return {
        'coordinates': coordinates,
        'numbers': numbers,
        'obasis': obasis,
        'wfn': wfn,
    }
Ejemplo n.º 8
0
def load_wfn(filename):
    """Load data from a WFN file.

    Parameters
    ----------
    filename : str
        The filename of the wfn file.

    Returns
    -------
    results : dict
        Data loaded from file, with keys ``title``, ``coordinates``, ``numbers``,
        ``energy``, ``obasis`` and ``orb_alpha``. May contain ``orb_beta``.
    """
    title, numbers, coordinates, centers, type_assignment, exponents, \
        mo_count, mo_occ, mo_energy, coefficients, energy = load_wfn_low(filename)
    permutation = get_permutation_basis(type_assignment)
    # permute arrays containing wfn data
    type_assignment = type_assignment[permutation]
    mask = get_mask(type_assignment)
    reduced_size = np.array(mask, int).sum()
    num_mo = coefficients.shape[1]
    alphas = np.empty(reduced_size)
    alphas[:] = exponents[permutation][mask]
    assert (centers == centers[permutation]).all()
    shell_map = centers[mask]
    # cartesian basis: {S:0, P:1, D:2, F:3, G:4, H:5}
    shell = {1: 0, 2: 1, 5: 2, 11: 3, 21: 4, 36: 5}
    shell_types = type_assignment[mask]
    shell_types = np.array([shell[i] for i in shell_types])
    assert shell_map.size == shell_types.size == reduced_size
    nprims = np.ones(reduced_size, int)
    con_coeffs = np.ones(reduced_size)
    # build basis set
    obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs)
    coefficients = coefficients[permutation]
    coefficients /= obasis.get_scales().reshape(-1, 1)
    # make the wavefunction
    if mo_occ.max() > 1.0:
        # close shell system
        orb_alpha = Orbitals(obasis.nbasis, coefficients.shape[1])
        orb_alpha.coeffs[:] = coefficients
        orb_alpha.energies[:] = mo_energy
        orb_alpha.occupations[:] = mo_occ / 2
        orb_beta = None
    else:
        # open shell system
        # counting the number of alpha and beta orbitals
        index = 1
        while index < num_mo and mo_energy[index] >= mo_energy[index - 1] and mo_count[index] == mo_count[index - 1] + 1:
            index += 1
        orb_alpha = Orbitals(obasis.nbasis, index)
        orb_alpha.coeffs[:] = coefficients[:, :index]
        orb_alpha.energies[:] = mo_energy[:index]
        orb_alpha.occupations[:] = mo_occ[:index]
        orb_beta = Orbitals(obasis.nbasis, num_mo - index)
        orb_beta.coeffs[:] = coefficients[:, index:]
        orb_beta.energies[:] = mo_energy[index:]
        orb_beta.occupations[:] = mo_occ[index:]

    result = {
        'title': title,
        'coordinates': coordinates,
        'orb_alpha': orb_alpha,
        'numbers': numbers,
        'obasis': obasis,
        'energy': energy,
    }
    if orb_beta is not None:
        result['orb_beta'] = orb_beta
    return result
Ejemplo n.º 9
0
def load_wfn(filename, lf):
    '''Load data from a WFN file

       **Arguments:**

       filename
            The filename of the wfn file.

       lf
            An instance of LinalgFactory, used to initialize the wavefunction
            expansions.
    '''
    numbers, coordinates, centers, type_assignment, exponents, mo_count, mo_occ, mo_energy, coefficients = load_wfn_low(filename)
    permutation = setup_permutation2(type_assignment)
    #Permuting the arrays containing the wfn data
    type_assignment = type_assignment[permutation]
    mask = setup_mask(type_assignment)
    reduced_size = np.array(mask, int).sum()
    num_mo = coefficients.shape[1]
    alphas = np.empty(reduced_size)
    alphas[:] = exponents[permutation][mask]
    assert (centers == centers[permutation]).all()
    shell_map = centers[mask]
    shell = {1:0, 2:1, 5:2, 11:3, 21:4, 36:5} #Cartesian: {S:0, P:1, D:2, F:3, G:4, H:5}
    shell_types = type_assignment[mask]
    shell_types = np.array([shell[i] for i in shell_types])
    assert shell_map.size == shell_types.size == reduced_size
    nprims = np.ones(reduced_size, int)
    con_coeffs = np.ones(reduced_size)
    #Building the basis set
    from horton.gbasis.gobasis import GOBasis
    obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs)
    coefficients = coefficients[permutation]
    coefficients /= obasis.get_scales().reshape(-1,1)
    #Making the wavefunction:
    if mo_occ.max() > 1.0:
        #close shell system
        nelec = int(round(mo_occ.sum()))
        wfn = RestrictedWFN(lf, obasis.nbasis, norb=coefficients.shape[1])
        exp_alpha = wfn.init_exp('alpha')
        exp_alpha.coeffs[:] = coefficients
        exp_alpha.energies[:] = mo_energy
        exp_alpha.occupations[:] = mo_occ/2
    else:
        #open shell system
        #counting the number of alpha and beta orbitals
        index = 1
        while index < num_mo and mo_energy[index] >= mo_energy[index-1] and mo_count[index] == mo_count[index-1]+1:
            index += 1
        wfn = UnrestrictedWFN(lf, obasis.nbasis)
        exp_alpha = wfn.init_exp('alpha', norb=index)
        exp_alpha.coeffs[:] = coefficients[:,:index]
        exp_alpha.energies[:] = mo_energy[:index]
        exp_alpha.occupations[:] = mo_occ[:index]
        exp_beta = wfn.init_exp('beta', norb=num_mo-index)
        exp_beta.coeffs[:] = coefficients[:,index:]
        exp_beta.energies[:] = mo_energy[index:]
        exp_beta.occupations[:] = mo_occ[index:]

    return {
        'coordinates': coordinates,
        'numbers': numbers,
        'obasis': obasis,
        'wfn': wfn,
    }