def _read_cp2k_contracted_obasis(f): """Read a contracted basis set from an open CP2K ATOM output file. Parameters ---------- f : file An open readable file object. Returns ------- obasis : GOBasis The orbital basis read from the file. """ # Load the relevant data from the file basis_desc = [] for line in f: if line.startswith(' *******************'): break elif line[3:12] == 'Functions': shell_type = str_to_shell_types(line[1:2], pure=True)[0] a = [] # exponents (alpha) c = [] # contraction coefficients basis_desc.append((shell_type, a, c)) else: values = [float(w) for w in line.split()] a.append(values[0]) # one exponent per line c.append(values[1:]) # many contraction coefficients per line # Convert the basis into HORTON format shell_map = [] shell_types = [] nprims = [] alphas = [] con_coeffs = [] for shell_type, a, c in basis_desc: # get correction to contraction coefficients. CP2K uses different normalization # conventions. corrections = _get_cp2k_norm_corrections(abs(shell_type), np.array(a)) c = np.array(c) / corrections.reshape(-1, 1) # fill in arrays for col in c.T: shell_map.append(0) shell_types.append(shell_type) nprims.append(len(col)) alphas.extend(a) con_coeffs.extend(col) # Create the basis object coordinates = np.zeros((1, 3)) 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) obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs) return obasis
def _read_cp2k_contracted_obasis(f): """Read a contracted basis set from an open CP2K ATOM output file. Parameters ---------- f : file An open readable file object. Returns ------- obasis : GOBasis The orbital basis read from the file. """ # Load the relevant data from the file basis_desc = [] for line in f: if line.startswith(' *******************'): break elif line[3:12] == 'Functions': shell_type = str_to_shell_types(line[1:2], pure=True)[0] a = [] # exponents (alpha) c = [] # contraction coefficients basis_desc.append((shell_type, a, c)) else: values = [float(w) for w in line.split()] a.append(values[0]) # one exponent per line c.append(values[1:]) # many contraction coefficients per line # Convert the basis into HORTON format shell_map = [] shell_types = [] nprims = [] alphas = [] con_coeffs = [] for shell_type, a, c in basis_desc: # get correction to contraction coefficients. CP2K uses different normalization # conventions. corrections = _get_cp2k_norm_corrections(abs(shell_type), np.array(a)) c = np.array(c)/corrections.reshape(-1, 1) # fill in arrays for col in c.T: shell_map.append(0) shell_types.append(shell_type) nprims.append(len(col)) alphas.extend(a) con_coeffs.extend(col) # Create the basis object coordinates = np.zeros((1, 3)) 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) obasis = GOBasis(coordinates, shell_map, nprims, shell_types, alphas, con_coeffs) return obasis
def _read_cp2k_uncontracted_obasis(f): """Read an uncontracted basis set from an open CP2K ATOM output file. Parameters ---------- f : file An open readable file object. Returns ------- obasis : GOBasis The orbital basis read from the file. """ # Load the relevant data from the file basis_desc = [] shell_type = None for line in f: if line.startswith(' *******************'): break elif line[3:13] == 'Exponents:': shell_type = str_to_shell_types(line[1:2], pure=True)[0] words = line.split() if len(words) >= 2: # read the exponent alpha = float(words[-1]) basis_desc.append((shell_type, alpha)) # Convert the basis into HORTON format shell_map = [] shell_types = [] nprims = [] alphas = [] con_coeffs = [] # fill in arrays for shell_type, alpha in basis_desc: correction = _get_cp2k_norm_corrections(abs(shell_type), alpha) shell_map.append(0) shell_types.append(shell_type) nprims.append(1) alphas.append(alpha) con_coeffs.append(1.0 / correction) # Create the basis object centers = np.zeros((1, 3)) 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) obasis = GOBasis(centers, shell_map, nprims, shell_types, alphas, con_coeffs) return obasis
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)
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)
def helper_obasis(f, coordinates): 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_coeffs.append(float(words[1])) 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)
def load_atom_cp2k(filename, lf): '''Load data from a CP2K ATOM computation **Arguments:** filename The name of the cp2k out file **Returns** a dictionary with ``obasis``, ``exp_alpha``, ``coordinates``, ``numbers``, ``energy``, ``pseudo_numbers``. The dictionary may also contain: ``exp_beta``. ''' with open(filename) as f: # Find the element number for line in f: if line.startswith(' Atomic Energy Calculation'): number = int(line[-5:-1]) break # Go to the pseudo basis set for line in f: if line.startswith(' Pseudopotential Basis'): break f.next() # empty line line = f.next() # Check for GTO assert line == ' ********************** Contracted Gaussian Type Orbitals **********************\n' # Load the basis used for the PP wavefn basis_desc = [] for line in f: if line.startswith(' *******************'): break elif line[3:12] == 'Functions': shell_type = str_to_shell_types(line[1:2], pure=True)[0] a = [] c = [] basis_desc.append((shell_type, a, c)) else: values = [float(w) for w in line.split()] a.append(values[0]) c.append(values[1:]) # Convert the basis into HORTON format shell_map = [] shell_types = [] nprims = [] alphas = [] con_coeffs = [] for shell_type, a, c in basis_desc: # get correction to contraction coefficients. corrections = _get_cp2k_norm_corrections(abs(shell_type), a) c = np.array(c)/corrections.reshape(-1,1) # fill in arrays for col in c.T: shell_map.append(0) shell_types.append(shell_type) nprims.append(len(col)) alphas.extend(a) con_coeffs.extend(col) # Create the basis object coordinates = np.zeros((1, 3)) 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) 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 cp2k.out file.') lf.default_nbasis = obasis.nbasis # Search for (un)restricted restricted = None for line in f: if line.startswith(' METHOD |'): if 'U' in line: restricted = False break elif 'R' in line: restricted = True break # Search for the core charge (pseudo number) for line in f: if line.startswith(' Core Charge'): pseudo_number = float(line[70:]) assert pseudo_number == int(pseudo_number) pseudo_number = int(pseudo_number) break # Search for energy for line in f: if line.startswith(' Energy components [Hartree] Total Energy ::'): energy = float(line[60:]) break # Read orbital energies and occupations for line in f: if line.startswith(' Orbital energies'): break f.next() oe_alpha = [] oe_beta = [] empty = 0 while empty < 2: line = f.next() words = line.split() if len(words) == 0: empty += 1 continue empty = 0 s = int(words[0]) l = int(words[2-restricted]) occ = float(words[3-restricted]) ener = float(words[4-restricted]) if restricted or words[1] == 'alpha': oe_alpha.append((l, s, occ, ener)) else: oe_beta.append((l, s, occ, ener)) # Read orbital expansion coefficients line = f.next() assert (line == " Atomic orbital expansion coefficients [Alpha]\n") or \ (line == " Atomic orbital expansion coefficients []\n") coeffs_alpha = _read_coeffs_helper(f, oe_alpha) if not restricted: line = f.next() assert (line == " Atomic orbital expansion coefficients [Beta]\n") coeffs_beta = _read_coeffs_helper(f, oe_beta) # Turn orbital data into a HORTON orbital expansions if restricted: norb, nel = _helper_norb(oe_alpha) assert nel%2 == 0 exp_alpha = lf.create_expansion(obasis.nbasis, norb) exp_beta = None _helper_exp(exp_alpha, oe_alpha, coeffs_alpha, shell_types, restricted) else: norb_alpha, nalpha = _helper_norb(oe_alpha) norb_beta, nbeta = _helper_norb(oe_beta) assert norb_alpha == norb_beta exp_alpha = lf.create_expansion(obasis.nbasis, norb_alpha) exp_beta = lf.create_expansion(obasis.nbasis, norb_beta) _helper_exp(exp_alpha, oe_alpha, coeffs_alpha, shell_types, restricted) _helper_exp(exp_beta, oe_beta, coeffs_beta, shell_types, restricted) result = { 'obasis': obasis, 'lf': lf, 'exp_alpha': exp_alpha, 'coordinates': coordinates, 'numbers': np.array([number]), 'energy': energy, 'pseudo_numbers': np.array([pseudo_number]), } if exp_beta is not None: result['exp_beta'] = exp_beta return result