def parse_ecp(string, symb=None): from pyscf.gto.mole import _std_symbol if symb is not None: symb = _std_symbol(symb) raw_data = string.splitlines() for i, dat in enumerate(raw_data): dat0 = dat.split(None, 1) if dat0 and dat0[0] == symb: break if i + 1 == len(raw_data): raise KeyError('ECP not found for %s' % symb) seg = [] for dat in raw_data[i:]: dat = dat.strip().upper() if ((dat[0].isalpha() and dat.split(None, 1)[0] != symb.upper())): break elif dat: # remove blank lines seg.append(dat) else: seg = string.splitlines() ecptxt = [] for dat in seg: x = dat.split('#')[0].strip().upper() if (x and not x.startswith('END') and not x.startswith('ECP')): ecptxt.append(x) return _parse_ecp(ecptxt)
def format_basis(basis_tab): '''Convert the input :attr:`Cell.basis` to the internal data format:: { atom: (l, kappa, ((-exp, c_1, c_2, ..), nprim, nctr, ptr-exps, ptr-contraction-coeff)), ... } Args: basis_tab : dict Similar to :attr:`Cell.basis`, it **cannot** be a str Returns: Formated :attr:`~Cell.basis` Examples: >>> pbc.format_basis({'H':'gth-szv'}) {'H': [[0, (8.3744350009, -0.0283380461), (1.8058681460, -0.1333810052), (0.4852528328, -0.3995676063), (0.1658236932, -0.5531027541)]]} ''' fmt_basis = {} for atom in basis_tab.keys(): atom_basis = basis_tab[atom] if isinstance(atom_basis, str) and 'gth' in atom_basis: fmt_basis[atom] = basis.load(atom_basis, _std_symbol(atom)) else: fmt_basis[atom] = atom_basis return mole.format_basis(fmt_basis)
def format_basis(basis_tab): '''Convert the input :attr:`Cell.basis` to the internal data format. ``{ atom: (l, kappa, ((-exp, c_1, c_2, ..), nprim, nctr, ptr-exps, ptr-contraction-coeff)), ... }`` Args: basis_tab : dict Similar to :attr:`Cell.basis`, it **cannot** be a str Returns: Formated :attr:`~Cell.basis` Examples: >>> pbc.format_basis({'H':'gth-szv'}) {'H': [[0, (8.3744350009, -0.0283380461), (1.8058681460, -0.1333810052), (0.4852528328, -0.3995676063), (0.1658236932, -0.5531027541)]]} ''' fmt_basis = {} for atom in basis_tab.keys(): atom_basis = basis_tab[atom] if isinstance(atom_basis, str) and 'gth' in atom_basis: fmt_basis[atom] = basis.load(atom_basis, _std_symbol(atom)) else: fmt_basis[atom] = atom_basis return mole.format_basis(fmt_basis)
def convert_basis_to_nwchem(symb, basis): '''Convert the internal basis format to NWChem format string''' from pyscf.gto.mole import _std_symbol res = [] symb = _std_symbol(symb) # pass 1: comment line ls = [b[0] for b in basis] nprims = [len(b[1:]) for b in basis] nctrs = [len(b[1]) - 1 for b in basis] prim_to_ctr = {} for i, l in enumerate(ls): if l in prim_to_ctr: prim_to_ctr[l][0] += nprims[i] prim_to_ctr[l][1] += nctrs[i] else: prim_to_ctr[l] = [nprims[i], nctrs[i]] nprims = [] nctrs = [] for l in set(ls): nprims.append(str(prim_to_ctr[l][0]) + SPDF[l].lower()) nctrs.append(str(prim_to_ctr[l][1]) + SPDF[l].lower()) res.append('#BASIS SET: (%s) -> [%s]' % (','.join(nprims), ','.join(nctrs))) # pass 2: basis data for bas in basis: res.append('%-2s %s' % (symb, SPDF[bas[0]])) for dat in bas[1:]: res.append(' '.join('%15.9f' % x for x in dat)) return '\n'.join(res)
def format_basis(basis_tab): """Convert the input :attr:`Cell.basis` to the internal data format. ``{ atom: (l, kappa, ((-exp, c_1, c_2, ..), nprim, nctr, ptr-exps, ptr-contraction-coeff)), ... }`` Args: basis_tab : list Similar to :attr:`Cell.basis`, it **cannot** be a str Returns: Formated :attr:`~Cell.basis` Examples: >>> pbc.format_basis({'H':'gth-szv'}) {'H': [[0, (8.3744350009, -0.0283380461), (1.8058681460, -0.1333810052), (0.4852528328, -0.3995676063), (0.1658236932, -0.5531027541)]]} """ fmt_basis = {} for atom in basis_tab.keys(): symb = _symbol(atom) rawsymb = _rm_digit(symb) stdsymb = _std_symbol(rawsymb) symb = symb.replace(rawsymb, stdsymb) if isinstance(basis_tab[atom], str): fmt_basis[symb] = basis.load(basis_tab[atom], stdsymb) else: fmt_basis[symb] = basis_tab[atom] return fmt_basis
def convert_basis_to_nwchem(symb, basis): '''Convert the internal basis format to NWChem format string''' from pyscf.gto.mole import _std_symbol res = [] symb = _std_symbol(symb) # pass 1: comment line ls = [b[0] for b in basis] nprims = [len(b[1:]) for b in basis] nctrs = [len(b[1])-1 for b in basis] prim_to_ctr = {} for i, l in enumerate(ls): if l in prim_to_ctr: prim_to_ctr[l][0] += nprims[i] prim_to_ctr[l][1] += nctrs[i] else: prim_to_ctr[l] = [nprims[i], nctrs[i]] nprims = [] nctrs = [] for l in set(ls): nprims.append(str(prim_to_ctr[l][0])+SPDF[l].lower()) nctrs.append(str(prim_to_ctr[l][1])+SPDF[l].lower()) res.append('#BASIS SET: (%s) -> [%s]' % (','.join(nprims), ','.join(nctrs))) # pass 2: basis data for bas in basis: res.append('%-2s %s' % (symb, SPDF[bas[0]])) for dat in bas[1:]: res.append(' '.join('%15.9f'%x for x in dat)) return '\n'.join(res)
def search_seg(basisfile, symb): from pyscf.gto.mole import _std_symbol symb = _std_symbol(symb) with open(basisfile, 'r') as fin: fdata = re.split(BASIS_SET_DELIMITER, fin.read()) dat = _search_seg(fdata, symb) if dat is None: return [] else: return [x.upper() for x in dat.splitlines() if x and 'END' not in x]
def format_basis(basis_tab): '''Convert the input :attr:`Cell.basis` to the internal data format:: { atom: (l, kappa, ((-exp, c_1, c_2, ..), nprim, nctr, ptr-exps, ptr-contraction-coeff)), ... } Args: basis_tab : dict Similar to :attr:`Cell.basis`, it **cannot** be a str Returns: Formated :attr:`~Cell.basis` Examples: >>> pbc.format_basis({'H':'gth-szv'}) {'H': [[0, (8.3744350009, -0.0283380461), (1.8058681460, -0.1333810052), (0.4852528328, -0.3995676063), (0.1658236932, -0.5531027541)]]} ''' def convert(basis_name, symb): if basis_name.lower().startswith('unc'): return uncontract(basis.load(basis_name[3:], symb)) else: return basis.load(basis_name, symb) fmt_basis = {} for atom in basis_tab.keys(): symb = _atom_symbol(atom) stdsymb = _std_symbol(symb) if stdsymb.startswith('GHOST-'): stdsymb = stdsymb[6:] atom_basis = basis_tab[atom] if isinstance(atom_basis, (str, unicode)): if 'gth' in atom_basis: bset = convert(str(atom_basis), symb) else: bset = atom_basis else: bset = [] for rawb in atom_basis: if isinstance(rawb, (str, unicode)) and 'gth' in rawb: bset.append(convert(str(rawb), stdsymb)) else: bset.append(rawb) fmt_basis[symb] = bset return mole.format_basis(fmt_basis)
def convert_ecp_to_nwchem(symb, ecp): '''Convert the internal ecp format to NWChem format string''' from pyscf.gto.mole import _std_symbol symb = _std_symbol(symb) res = ['%-2s nelec %d' % (symb, ecp[0])] for ecp_block in ecp[1]: l = ecp_block[0] if l == -1: res.append('%-2s ul' % symb) else: res.append('%-2s %s' % (symb, SPDF[l].lower())) for r_order, dat in enumerate(ecp_block[1]): for e, c in dat: res.append('%d %15.9f %15.9f' % (r_order, e, c)) return '\n'.join(res)
def convert_ecp_to_nwchem(symb, ecp): '''Convert the internal ecp format to NWChem format string''' from pyscf.gto.mole import _std_symbol symb = _std_symbol(symb) res = ['%-2s nelec %d' % (symb, ecp[0])] for ecp_block in ecp[1]: l = ecp_block[0] if l == -1: res.append('%-2s ul' % symb) else: res.append('%-2s %s' % (symb, SPDF[l].lower())) for r_order, dat in enumerate(ecp_block[1]): for e,c in dat: res.append('%d %15.9f %15.9f' % (r_order, e, c)) return '\n'.join(res)
def search_ecp(basisfile, symb): from pyscf.gto.mole import _std_symbol symb = _std_symbol(symb) with open(basisfile, 'r') as fin: fdata = re.split(ECP_DELIMITER, fin.read()) fdata = fdata[1].splitlines() for i, dat in enumerate(fdata): dat0 = dat.split(None, 1) if dat0 and dat0[0] == symb: break seg = [] for dat in fdata[i:]: dat = dat.strip().upper() if ((dat[0].isalpha() and dat.split(None, 1)[0] != symb.upper())): return seg elif dat: # remove blank lines seg.append(dat) return []
def parse(string, symb=None): '''Parse the basis text which is in NWChem format, return an internal basis format which can be assigned to :attr:`Mole.basis` Lines started with # are ignored. ''' from pyscf.gto.mole import _std_symbol if symb is not None: symb = _std_symbol(symb) string = _search_seg(re.split(BASIS_SET_DELIMITER, string), symb) if string is None: raise KeyError('Basis not found for %s' % symb) bastxt = [] for dat in string.splitlines(): x = dat.split('#')[0].strip().upper() # Use # to start comments if (x and not x.startswith('END') and not x.startswith('BASIS')): bastxt.append(x) return _parse(bastxt)
def format_pseudo(pseudo_tab): r'''Convert the input :attr:`Cell.pseudo` (dict) to the internal data format:: { atom: ( (nelec_s, nele_p, nelec_d, ...), rloc, nexp, (cexp_1, cexp_2, ..., cexp_nexp), nproj_types, (r1, nproj1, ( (hproj1[1,1], hproj1[1,2], ..., hproj1[1,nproj1]), (hproj1[2,1], hproj1[2,2], ..., hproj1[2,nproj1]), ... (hproj1[nproj1,1], hproj1[nproj1,2], ... ) )), (r2, nproj2, ( (hproj2[1,1], hproj2[1,2], ..., hproj2[1,nproj1]), ... ) ) ) ... } Args: pseudo_tab : dict Similar to :attr:`Cell.pseudo` (a dict), it **cannot** be a str Returns: Formatted :attr:`~Cell.pseudo` Examples: >>> pbc.format_pseudo({'H':'gth-blyp', 'He': 'gth-pade'}) {'H': [[1], 0.2, 2, [-4.19596147, 0.73049821], 0], 'He': [[2], 0.2, 2, [-9.1120234, 1.69836797], 0]} ''' fmt_pseudo = {} for atom in pseudo_tab: symb = _symbol(atom) rawsymb = _rm_digit(symb) stdsymb = _std_symbol(rawsymb) symb = symb.replace(rawsymb, stdsymb) if isinstance(pseudo_tab[atom], str): fmt_pseudo[symb] = pseudo.load(pseudo_tab[atom], stdsymb) else: fmt_pseudo[symb] = pseudo_tab[atom] return fmt_pseudo
def format_pseudo(pseudo_tab): '''Convert the input :attr:`Cell.pseudo` (dict) to the internal data format. ``{ atom: ( (nelec_s, nele_p, nelec_d, ...), rloc, nexp, (cexp_1, cexp_2, ..., cexp_nexp), nproj_types, (r1, nproj1, ( (hproj1[1,1], hproj1[1,2], ..., hproj1[1,nproj1]), (hproj1[2,1], hproj1[2,2], ..., hproj1[2,nproj1]), ... (hproj1[nproj1,1], hproj1[nproj1,2], ... ) )), (r2, nproj2, ( (hproj2[1,1], hproj2[1,2], ..., hproj2[1,nproj1]), ... ) ) ) ... }`` Args: pseudo_tab : dict Similar to :attr:`Cell.pseudo` (a dict), it **cannot** be a str Returns: Formatted :attr:`~Cell.pseudo` Examples: >>> pbc.format_pseudo({'H':'gth-blyp', 'He': 'gth-pade'}) {'H': [[1], 0.2, 2, [-4.19596147, 0.73049821], 0], 'He': [[2], 0.2, 2, [-9.1120234, 1.69836797], 0]} ''' fmt_pseudo = {} for atom in pseudo_tab.keys(): symb = _symbol(atom) rawsymb = _rm_digit(symb) stdsymb = _std_symbol(rawsymb) symb = symb.replace(rawsymb, stdsymb) if isinstance(pseudo_tab[atom], str): fmt_pseudo[symb] = pseudo.load(pseudo_tab[atom], stdsymb) else: fmt_pseudo[symb] = pseudo_tab[atom] return fmt_pseudo
def convert_basis_to_psi4(symb, basis): from pyscf.gto.mole import _std_symbol '''Convert pyscf internal basis format to Gaussian format string Psi4 uses Gaussian 94 format ''' res = [] symb = _std_symbol(symb) SPDF = ('S', 'P', 'D', 'F', 'G', 'H', 'I', 'J') MAXL = 8 MAPSPDF = {'S': 0, 'P': 1, 'D': 2, 'F': 3, 'G': 4, 'H': 5, 'I': 6, 'J': 7} # element name res.append('%-2s 0' % (symb)) # gaussian formatting for bas in basis: for i in range(len(bas[1]) - 1): res.append('%s %s 1.00' % (SPDF[bas[0]], len(bas[1:]))) for dat in bas[1:]: res.append('%15.9f %15.9f ' % (dat[0], dat[i + 1])) #if len(bas[1]) > 2: # for i in range(len(bas[1])-1): # res.append('%s %s 1.00' % (SPDF[bas[0]], len(bas[1:]))) # for dat in bas[1:]: # res.append('%15.9f %15.9f ' % (dat[0], dat[i+1])) #elif len(bas[1]) == 2: # res.append('%s %s 1.00' % (SPDF[bas[0]], len(bas[1:]))) # for dat in bas[1:]: # res.append('%15.9f %15.9f ' % (dat[0], dat[1])) # #else: # raise RuntimeError( # 'Warning! Please manually check basis set format!') # closing res.append('****') return '\n'.join(res)