def test_evaluate_basis_pyscf(): """Test gbasis.evals.eval.evaluate_basis against pyscf results.""" pytest.importorskip("pyscf") from pyscf import gto from gbasis.wrappers import from_pyscf mol = gto.Mole() mol.build(atom="H 0 0 0; He 0.8 0 0", basis="ano-rcc", spin=1) basis = from_pyscf(mol) grid_1d = np.linspace(-2, 2, num=5) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T pyscf_eval_sph = gto.eval_gto(mol, "GTOval_sph", grid_3d) pyscf_eval_cart = gto.eval_gto(mol, "GTOval_cart", grid_3d) # s orbitals assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[:6], pyscf_eval_cart.T[:6]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[46:53], pyscf_eval_cart.T[46:53]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[:6], pyscf_eval_sph.T[:6]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[40:47], pyscf_eval_sph.T[40:47]) # p orbitals assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[6:18], pyscf_eval_cart.T[6:18]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[53:65], pyscf_eval_cart.T[53:65]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[6:18], pyscf_eval_sph.T[6:18]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[47:59], pyscf_eval_sph.T[47:59]) # d orbitals are off by some constant for the cartesian case assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[18:33], pyscf_eval_sph.T[18:33]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[59:74], pyscf_eval_sph.T[59:74]) # f orbitals are off by some constant for the cartesian case assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[33:40], pyscf_eval_sph.T[33:40]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="spherical")[74:88], pyscf_eval_sph.T[74:88])
def MolDensity(self,samps,m,p=[0.0,0.0,0.0],tag=None): Ps = np.zeros(len(samps)) mol = gto.Mole() pyscfatomstring='' if (tag==None): for j in range(len(m.atoms)): pyscfatomstring=pyscfatomstring+"H@"+str(m.atoms[j])+" "+str(m.coords[j,0])+" "+str(m.coords[j,1])+" "+str(m.coords[j,2])+(";" if j!= len(m.atoms)-1 else "") else: for j in range(len(m.atoms)): if (j == tag): print("Tagging atom", j) pyscfatomstring=pyscfatomstring+"N@0"+" "+str(m.coords[j,0])+" "+str(m.coords[j,1])+" "+str(m.coords[j,2])+(";" if j!= len(m.atoms)-1 else "") else: pyscfatomstring=pyscfatomstring+"H@"+str(m.atoms[j])+" "+str(m.coords[j,0])+" "+str(m.coords[j,1])+" "+str(m.coords[j,2])+(";" if j!= len(m.atoms)-1 else "") mol.atom = pyscfatomstring mol.basis = TOTAL_SENSORY_BASIS mol.verbose = 0 if (len(m.atoms)%2 == 0): mol.spin = 0 else: mol.spin = 1 try: mol.build() except Exception as Ex: print(mol.atom, mol.basis, m.atoms, m.coords) raise Ex return np.sum(gto.eval_gto('GTOval_sph',mol._atm,mol._bas,mol._env,samps*1.889725989),axis=1)
def AtomEmbedAtomCentered(self, samps, m, p, i=-1): mol = gto.Mole() MaxEmbedded = 30 SensedAtoms = [a for a in m.AtomsWithin(30.0, p) if a != i] if (len(SensedAtoms) > MaxEmbedded): SensedAtoms = SensedAtoms[:MaxEmbedded] if (len(SensedAtoms) == 0): raise Exception("NoAtomsInSensoryRadius") mol.atom = "C " + str(p[0]) + " " + str(p[1]) + " " + str(p[2]) + ";" na = 0 for j in SensedAtoms: mol.atom = mol.atom + "H@" + str(m.atoms[j]) + " " + str( m.coords[j, 0]) + " " + str(m.coords[j, 1]) + " " + str( m.coords[j, 2]) + (";" if j != SensedAtoms[-1] else "") na = na + 1 #print mol.atom #print self.atoms #print self.coords #print "Basis Atom",[mol.bas_atom(i) for i in range(mol.nbas)] if (na % 2 == 0): mol.spin = 0 else: mol.spin = 1 if (TOTAL_SENSORY_BASIS == None): raise ("missing sensory basis") mol.basis = TOTAL_SENSORY_BASIS nsaos = 0 try: mol.build() except Exception as Ex: print mol.atom, mol.basis, m.atoms, m.coords, SensedAtoms, p raise Ex nsaos = gto.nao_nr_range(mol, 0, mol.atom_nshells(0))[1] nbas = gto.nao_nr(mol) print "nAtoms: ", m.NAtoms(), " nsaos: ", nsaos, " nbas ", nbas S = mol.intor('cint1e_ovlp_sph', shls_slice=(0, mol.atom_nshells(0), 0, mol.atom_nshells(0))) Sinv = MatrixPower(S, -1.0) SBFs = gto.eval_gto('GTOval_sph', mol._atm, mol._bas, mol._env, samps * 1.889725989, comp=1, shls_slice=(0, mol.atom_nshells(0))) print "SBFs.shape", SBFs.shape Cs = mol.intor('cint1e_ovlp_sph', shls_slice=(0, mol.atom_nshells(0), mol.atom_nshells(0), mol.nbas)) print "Cs.shape", Cs.shape # for i in range(len(Cs[0])): # tmp = np.dot(SBFs,np.dot(Sinv,Cs[:,i])) # GridstoRaw(tmp*tmp,150,"Atoms"+str(i)) # exit(0) Sd = np.sum(np.dot(SBFs, np.dot(Sinv, Cs)), axis=1) print "Sd.shape", Sd.shape return Sd
def Populate(self): print("Populating Grids... ") # # Populate output Bases # self.GauGrid = MakeUniform([0,0,0],self.GridRange*.8,self.NGau) self.Grid = MakeUniform([0,0,0],self.GridRange,self.NPts) self.dx=(np.max(self.Grid[:,0])-np.min(self.Grid[:,0]))/self.NPts*1.889725989 self.dy=(np.max(self.Grid[:,1])-np.min(self.Grid[:,1]))/self.NPts*1.889725989 self.dz=(np.max(self.Grid[:,2])-np.min(self.Grid[:,2]))/self.NPts*1.889725989 return mol = gto.Mole() mol.atom = ''.join(["H@0 "+str(self.GauGrid[iii,0])+" "+str(self.GauGrid[iii,1])+" "+str(self.GauGrid[iii,2])+";" for iii in range(len(self.GauGrid))])[:-1] if (self.NGau3%2==0): mol.spin = 0 else: mol.spin = 1 if (ATOM_BASIS == None): raise("missing ATOM_BASIS") mol.basis = ATOM_BASIS try: mol.build() except Exception as Ex: print(mol.atom) raise Ex # All this shit could be Pre-Computed... # Really any grid could be used. orbs=gto.eval_gto('GTOval_sph',mol._atm,mol._bas,mol._env,self.Grid*1.889725989) nbas=orbs.shape[1] if (nbas!=self.NGau3): raise Exception("insanity") S=mol.intor('cint1e_ovlp_sph') #S = np.zeros(shape=(nbas,nbas)) #for i in range(nbas): # for j in range(nbas): # S[i,j] += np.sum(orbs[:,i]*orbs[:,j]) C = MatrixPower(S,-0.5) if (0): for i in range(nbas): CM = np.dot(self.Grid.T,orbs[:,i]) print("Centers of Mass, i", np.dot(self.Grid.T,orbs[:,i]*orbs[:,i])*self.dx*self.dy*self.dz, self.GauGrid[i]) Rsq = np.array(map(np.linalg.norm,self.Grid-CM)) print("Rsq of Mass, i", np.sqrt(np.dot(Rsq,orbs[:,i]*orbs[:,i]))*self.dx*self.dy*self.dz) for j in range(nbas): print("Normalization of grid i.", np.sum(orbs[:,i]*orbs[:,j])*self.dx*self.dy*self.dz) self.OBFs = np.zeros(shape=(self.NGau3,self.NPts3)) for i in range(nbas): for j in range(nbas): self.OBFs[i,:] += (C.T[i,j]*orbs[:,j]).T # Populate Sensory bases. self.PopulateSense() if (not self.Spherical): self.BuildIsometries() print("Using ", len(self.Isometries), " isometries.") print("Grid storage cost: ",self.OBFs.size*64/1024/1024, "Mb")
def TestGridGauEmbedding(self,samps,m,p,i): mol = gto.Mole() MaxEmbedded = 15 SensedAtoms = [a for a in m.AtomsWithin(10.0,p) if a != i] if (len(SensedAtoms)>MaxEmbedded): SensedAtoms=SensedAtoms[:MaxEmbedded] if (len(SensedAtoms)==0): raise Exception("NoAtomsInSensoryRadius") GauGrid = MakeUniform([0,0,0],3.5,self.NGau) #pyscfatomstring="C "+str(p[0])+" "+str(p[1])+" "+str(p[2])+";" mol.atom = ''.join(["H@0 "+str(GauGrid[iii,0])+" "+str(GauGrid[iii,1])+" "+str(GauGrid[iii,2])+";" for iii in range(len(GauGrid))]) na = len(GauGrid) #na=0 for j in SensedAtoms: mol.atom=mol.atom+"H@"+str(m.atoms[j])+" "+str(m.coords[j,0])+" "+str(m.coords[j,1])+" "+str(m.coords[j,2])+(";" if j!=SensedAtoms[-1] else "") na=na+1 #print mol.atom #print self.atoms #print self.coords #print "Basis Atom",[mol.bas_atom(i) for i in range(mol.nbas)] if (na%2 == 0): mol.spin = 0 else: mol.spin = 1 if (TOTAL_SENSORY_BASIS == None): raise("missing sensory basis") mol.basis = TOTAL_SENSORY_BASIS nsaos = 0 try: mol.build() except Exception as Ex: print(mol.atom, mol.basis, m.atoms, m.coords, SensedAtoms, p) raise Ex nsaos = len(GauGrid) nbas = gto.nao_nr(mol) print("nAtoms: ",m.NAtoms()," nsaos: ", nsaos, " nbas ", nbas) S = mol.intor('cint1e_ovlp_sph',shls_slice=(0,nsaos,0,nsaos)) Sinv = MatrixPower(S,-1.0) SBFs = gto.eval_gto('GTOval_sph',mol._atm,mol._bas,mol._env,samps*1.889725989,comp=1,shls_slice=(0,nsaos)) print("SBFs.shape", SBFs.shape) Cs = mol.intor('cint1e_ovlp_sph',shls_slice=(0,nsaos,nsaos,mol.nbas)) print("Cs.shape", Cs.shape) Sd = np.sum(np.dot(SBFs,np.dot(Sinv,Cs)),axis=1) print("Sd.shape", Sd.shape) return Sd
def SenseOnGrid(self,p,grd_): mol = gto.Mole() mol.atom ='' if (not self.Spherical): tmpgrid = self.SenseGrid + p mol.atom = ''.join(["H@0 "+str(tmpgrid[iii,0])+" "+str(tmpgrid[iii,1])+" "+str(tmpgrid[iii,2])+";" for iii in range(len(tmpgrid))]) else: mol.atom = ''.join("C "+str(p[0])+" "+str(p[1])+" "+str(p[2])+";") mol.spin = 0 if (TOTAL_SENSORY_BASIS == None): raise("missing sensory basis") mol.basis = TOTAL_SENSORY_BASIS mol.build() return gto.eval_gto('GTOval_sph',mol._atm,mol._bas,mol._env,grd_*1.889725989)
def test_evaluate_basis_pyscf_cart_norm(): """Test gbasis.evals.eval.evaluate_basis against pyscf results. These cases fail because pyscf seems to have a different normalization constant for the d and f orbitals. """ pytest.importorskip("pyscf") from pyscf import gto from gbasis.wrappers import from_pyscf mol = gto.Mole() mol.build(atom="H 0 0 0; He 0.8 0 0", basis="ano-rcc", spin=1) basis = from_pyscf(mol) grid_1d = np.linspace(-2, 2, num=5) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T pyscf_eval_cart = gto.eval_gto(mol, "GTOval_cart", grid_3d) # d orbitals are all off by some scalar factor assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[18:36], pyscf_eval_cart.T[18:36]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[65:83], pyscf_eval_cart.T[65:83]) # f orbitals are all off by some scalar factor assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[36:46], pyscf_eval_cart.T[36:46]) assert np.allclose( evaluate_basis(basis, grid_3d, coord_type="cartesian")[83:103], pyscf_eval_cart.T[83:103])