def __init__(self, mol, mints): """ Initialize the uhf :param mol: a psi4 molecule object :param mints: a molecular integrals object (from MintsHelper) """ self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = block_oei(mints.ao_kinetic()) self.S = block_oei(mints.ao_overlap()) self.V = block_oei(mints.ao_potential()) G = block_tei(mints.ao_eri()) # Convert to antisymmetrized integrals in physicist's notation self.g = G.transpose(0, 2, 1, 3) - G.transpose(0, 2, 3, 1) self.h = self.T + self.V # S^{-1/2} self.X = la.inv(la.sqrtm(self.S)).view(np.matrix) # Determine the number of electrons self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) self.nocc = self.nelec self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.norb = self.nbf*2
def __init__(self, mol, mints): """ Initialize the rhf :param mol: a psi4 molecule object :param mints: a molecular integrals object (from MintsHelper) """ self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = np.matrix(mints.ao_kinetic()) self.S = np.matrix(mints.ao_overlap()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()) # Determine the number of electrons and the number of doubly occupied orbitals self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) if mol.multiplicity() != 1 or self.nelec % 2: raise Exception("This code only allows closed-shell molecules") self.ndocc = self.nelec / 2 self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf()
def __init__(self, mol, mints): """ Initialize the uhf :param mol: a psi4 molecule object :param mints: a molecular integrals object (from MintsHelper) """ self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = block_oei(mints.ao_kinetic()) self.S = block_oei(mints.ao_overlap()) self.V = block_oei(mints.ao_potential()) G = block_tei(mints.ao_eri()) # Convert to antisymmetrized integrals in physicist's notation self.g = G.transpose(0, 2, 1, 3) - G.transpose(0, 2, 3, 1) self.h = self.T + self.V # S^{-1/2} self.X = la.inv(la.sqrtm(self.S)).view(np.matrix) # Determine the number of electrons self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) self.nocc = self.nelec self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.norb = self.nbf * 2
def __init__(self, mol, mints): """ Initialize the rhf :param mol: a Psi4 molecule object :param mints: a molecular integrals object (from MintsHelper) """ self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = np.matrix(mints.ao_kinetic()) self.S = np.matrix(mints.ao_overlap()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()).swapaxes(1, 2) # Determine the number of electrons and the number of doubly occupied orbitals self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) if mol.multiplicity() != 1 or self.nelec % 2: raise Exception("This code only allows closed-shell molecules") self.ndocc = self.nelec / 2 self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.vu = np.matrix(np.zeros((self.nbf, self.nbf)))
def __init__(self, mol, mints): """ Optimize a spin-orbital basis by Unrestricted Hartree-Fock theory. Inputs: 1) Molecule object 2) Integral package IMPORTANT MEMBER VARIABLES: Optimised energy................self.E Optimised density matrix........self.d n.b. Chemist's notation is used for in the evaluation of two electron integrals. """ self.mol, self.mints = mol, mints self.nbf = self.mints.basisset().nbf() self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.vnu = mol.nuclear_repulsion_energy() self.duration = 0 self.print_header() # Determine number of electrons self.nelec = -mol.molecular_charge() for i in range(mol.natom()): self.nelec += int(mol.Z(i)) # Import one and two electron integrals in spatial AO basis self.s_spat = np.array(mints.ao_overlap()) self.t_spat = np.array(mints.ao_kinetic()) self.v_spat = np.array(mints.ao_potential()) self.g_spat = np.array(mints.ao_eri()) # Transform 1- and 2-e integrals to spin AO basis self.s, self.t, self.v, self.g = self.spin_integrals() n = self.nbf # Form orthogonalizer, X = S^(-0.5) self.x = la.sqrtm(la.inv(self.s)) # Initial HF parameters: D=0, E=0 self.d = np.zeros((2 * self.nbf, 2 * self.nbf)) self.E = 0 self.diff = 0 self.count = 0 # Iterate to self-consistency self.iterate() # The following function compares output to Psi4 energy """
def __init__(self, mol, mints): """ Optimize a spin-orbital basis by Unrestricted Hartree-Fock theory. Inputs: 1) Molecule object 2) Integral package IMPORTANT MEMBER VARIABLES: Optimised energy................self.E Optimised density matrix........self.d n.b. Chemist's notation is used for in the evaluation of two electron integrals. """ self.mol, self.mints = mol, mints self.nbf = self.mints.basisset().nbf() self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.vnu = mol.nuclear_repulsion_energy() self.duration = 0 self.print_header() # Determine number of electrons self.nelec = -mol.molecular_charge() for i in range(mol.natom()): self.nelec += int(mol.Z(i)) # Import one and two electron integrals in spatial AO basis self.s_spat = np.array(mints.ao_overlap()) self.t_spat = np.array(mints.ao_kinetic()) self.v_spat = np.array(mints.ao_potential()) self.g_spat = np.array(mints.ao_eri()) # Transform 1- and 2-e integrals to spin AO basis self.s, self.t, self.v, self.g = self.spin_integrals() n = self.nbf # Form orthogonalizer, X = S^(-0.5) self.x = la.sqrtm(la.inv(self.s)) # Initial HF parameters: D=0, E=0 self.d = np.zeros((2*self.nbf,2*self.nbf)) self.E = 0 self.diff = 0 self.count = 0 # Iterate to self-consistency self.iterate() # The following function compares output to Psi4 energy """
def run_plugin_mp2(name, **kwargs): r"""Function encoding sequence of PSI module and plugin calls so that mollerplesset2 can be called via :py:func:`~driver.energy`. >>> energy('mollerplesset2') """ lowername = name.lower() kwargs = p4util.kwargs_lower(kwargs) # Your plugin's psi4 run sequence goes here psi4.set_local_option('MOLLERPLESSET2', 'PRINT', 1) scf_wfn = scf_helper(lowername) # Need to semicanonicalize the ROHF orbitals if psi4.get_global_option('REFERENCE') == 'ROHF': scf_wfn.semicanonicalize() # Ensure IWL files have been written when not using DF/CD proc_util.check_iwl_file_from_scf_type(psi4.get_option('SCF', 'SCF_TYPE'), scf_wfn) #psi4.set_legacy_wavefunction(scf_wfn) returnvalue = psi4.plugin('mollerplesset2.so', scf_wfn) return returnvalue
def format_options_for_input(): """Function to return a string of commands to replicate the current state of user-modified options. Used to capture C++ options information for distributed (sow/reap) input files. .. caution:: Some features are not yet implemented. Buy a developer a coffee. - Does not cover local (as opposed to global) options. - Does not work with array-type options. """ commands = '' commands += """\npsi4.set_memory(%s)\n\n""" % (psi4.get_memory()) for chgdopt in psi4.get_global_option_list(): if psi4.has_global_option_changed(chgdopt): chgdoptval = psi4.get_global_option(chgdopt) if isinstance(chgdoptval, basestring): commands += """psi4.set_global_option('%s', '%s')\n""" % ( chgdopt, chgdoptval) elif isinstance(chgdoptval, int) or isinstance(chgdoptval, float): commands += """psi4.set_global_option('%s', %s)\n""" % ( chgdopt, chgdoptval) else: raise ValidationError( 'Option \'%s\' is not of a type (string, int, float, bool) that can be processed.' % (chgdopt)) return commands
def run_gpu_dfcc(name, **kwargs): """Function encoding sequence of PSI module calls for a GPU-accelerated DF-CCSD(T) computation. >>> energy('df-ccsd(t)') """ lowername = name.lower() kwargs = kwargs_lower(kwargs) # stash user options optstash = OptionsState( ['GPU_DFCC','COMPUTE_TRIPLES'], ['GPU_DFCC','DFCC'], ['GPU_DFCC','NAT_ORBS'], ['SCF','DF_INTS_IO'], ['SCF','SCF_TYPE']) psi4.set_local_option('SCF','DF_INTS_IO', 'SAVE') psi4.set_local_option('GPU_DFCC','DFCC', True) # throw an exception for open-shells if (psi4.get_option('SCF','REFERENCE') != 'RHF' ): raise ValidationError("Error: %s requires \"reference rhf\"." % lowername) # override symmetry: molecule = psi4.get_active_molecule() molecule.update_geometry() molecule.reset_point_group('c1') molecule.fix_orientation(1) molecule.update_geometry() # triples? if (lowername == 'gpu-df-ccsd'): psi4.set_local_option('GPU_DFCC','COMPUTE_TRIPLES', False) if (lowername == 'gpu-df-ccsd(t)'): psi4.set_local_option('GPU_DFCC','COMPUTE_TRIPLES', True) #if (lowername == 'fno-df-ccsd'): # psi4.set_local_option('GPU_DFCC','COMPUTE_TRIPLES', False) # psi4.set_local_option('GPU_DFCC','NAT_ORBS', True) #if (lowername == 'fno-df-ccsd(t)'): # psi4.set_local_option('GPU_DFCC','COMPUTE_TRIPLES', True) # psi4.set_local_option('GPU_DFCC','NAT_ORBS', True) # set scf-type to df unless the user wants something else if psi4.has_option_changed('SCF','SCF_TYPE') == False: psi4.set_local_option('SCF','SCF_TYPE', 'DF') if psi4.get_option('GPU_DFCC','DF_BASIS_CC') == '': basis = psi4.get_global_option('BASIS') dfbasis = corresponding_rifit(basis) psi4.set_local_option('GPU_DFCC','DF_BASIS_CC',dfbasis) scf_helper(name,**kwargs) psi4.plugin('gpu_dfcc.so') # restore options optstash.restore() return psi4.get_variable("CURRENT ENERGY")
def __init__(self, mol, mints): """ Compute the energy of a system by the Restricted Hartree-Fock Self-Consistent Field Method. ============================================================= An initial guess of D = 0 is employed. """ self.mol = mol self.mints = mints self.nbf = self.mints.basisset().nbf() self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') #Import necessary integrals from psi4 self.Vnu = mol.nuclear_repulsion_energy() self.S = np.matrix(mints.ao_overlap()) self.T = np.matrix(mints.ao_kinetic()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()).transpose( (0, 2, 1, 3)) #Import eri integrals in physicist's not'n #Construct orthogonalizer, X = S^(-0.5) self.X = la.inv(la.sqrtm(self.S)) #Initial HF guess with D = 0 self.D = np.zeros((self.nbf, self.nbf)) # Determine the number of electrons and the number of doubly occupied orbitals self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) if mol.multiplicity() != 1 or self.nelec % 2: raise Exception("This code only allows closed-shell molecules.") self.ndocc = self.nelec / 2 self.E = 0 self.C = 0 self.orb_e = 0 self.count = 0 self.diff = 0 self.iterate()
def __init__(self, mol, mints): self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = np.matrix(mints.ao_kinetic()) self.S = np.matrix(mints.ao_overlap()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()).swapaxes(1, 2) self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) self.ndocc = int(self.nelec / 2) self.nsocc = self.nelec - 2 * self.ndocc self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.nsbf = 2 * self.nbf self.vu = np.matrix(np.zeros((self.nsbf, self.nsbf))) # Transform the integral matrix r = range(int(self.nsbf)) self.G = np.zeros((self.nsbf, self.nsbf, self.nsbf, self.nsbf)) for u in r: u_i = u % self.nbf for v in r: v_i = v % self.nbf for p in r: p_i = p % self.nbf for q in r: q_i = q % self.nbf self.G[u, p, v, q] += (self.delta(u, v) * self.delta(p, q) * self.g[u_i, p_i, v_i, q_i] - self.delta(u, q) * self.delta(p, v) * self.g[u_i, p_i, q_i, v_i])
def __init__(self, mol, mints): """ Initialize the rhf :param mol: a Psi4 molecule object :param mints: a molecular integrals object (from MintsHelper) """ self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = np.matrix(mints.ao_kinetic()) self.S = np.matrix(mints.ao_overlap()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()).transpose((0,2,1,3)) # Determine the number of electrons and the number of doubly occupied orbitals self.nelec = -mol.molecular_charge() #accounts for # of e- in ions for A in range(mol.natom()): self.nelec += int(mol.Z(A)) if mol.multiplicity() != 1 or self.nelec % 2: raise Exception("This code only allows closed-shell molecules") self.ndocc = self.nelec / 2 #number of doubly occupied orbitals self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.D = np.zeros((self.nbf,self.nbf)) #Form orthogonalizer X e_values, e_vectors = la.eig(self.S) v = np.diagflat(e_values) vsqrt = np.sqrt(v) vect_inv = la.inv(e_vectors) b = np.dot(e_vectors, vsqrt) Ssqrt = np.dot(b,vect_inv) X = la.inv(Ssqrt) self.X = X.real
def __init__(self, mol, mints): self.mol = mol self.mints = mints self.V_nuc = mol.nuclear_repulsion_energy() self.T = np.matrix(mints.ao_kinetic()) self.S = np.matrix(mints.ao_overlap()) self.V = np.matrix(mints.ao_potential()) self.g = np.array(mints.ao_eri()).swapaxes(1,2) self.nelec = -mol.molecular_charge() for A in range(mol.natom()): self.nelec += int(mol.Z(A)) self.ndocc = int(self.nelec / 2) self.nsocc = self.nelec - 2*self.ndocc self.maxiter = psi4.get_global_option('MAXITER') self.e_convergence = psi4.get_global_option('E_CONVERGENCE') self.nbf = mints.basisset().nbf() self.nsbf = 2*self.nbf self.vu = np.matrix(np.zeros((self.nsbf, self.nsbf))) # Transform the integral matrix r = range(int(self.nsbf)) self.G = np.zeros((self.nsbf, self.nsbf, self.nsbf, self.nsbf)) for u in r: u_i = u % self.nbf for v in r: v_i = v % self.nbf for p in r: p_i = p % self.nbf for q in r: q_i = q % self.nbf self.G[u, p, v, q] += (self.delta(u,v) * self.delta(p,q) * self.g[u_i, p_i, v_i, q_i] - self.delta(u,q) * self.delta(p,v) * self.g[u_i, p_i, q_i, v_i])
def __init__(self, option, module=None): self.option = option.upper() if module: self.module = module.upper() else: self.module = None self.value_global = psi4.get_global_option(option) self.haschanged_global = psi4.has_global_option_changed(option) if self.module: self.value_local = psi4.get_local_option(self.module, option) self.haschanged_local = psi4.has_local_option_changed(self.module, option) self.value_used = psi4.get_option(self.module, option) self.haschanged_used = psi4.has_option_changed(self.module, option) else: self.value_local = None self.haschanged_local = None self.value_used = None self.haschanged_used = None
def compute_energy(self): # copy over object attributes to avoid having to write "self." a lot h, g, D, X, Vnu, nocc = self.h, self.g, self.D, self.X, self.Vnu, self.nocc self.E = 0.0 for i in range(psi4.get_option('SCF', 'MAXITER')): v = np.einsum('mrns,sr', g, D) # e- field v_mu,nu = sum_rh,si <mu rh||mu si> D_si,rh f = h + v # build fock matrix tf = X*f*X # transform to orthogonalized AO basis e, tC = la.eigh(tf) # diagonalize C = X * tC # backtransform oC = C[:,:nocc] # get occupied MO coefficients D = oC * oC.T # build density matrix E = np.trace((h + v/2)*D) + Vnu # compute energy by tracing with density matrix dE = E - self.E # get change in energy self.E, self.C, self.e = E, C, e # save these for later print('UHF {:-3d}{:20.15f}{:20.15f}'.format(i, E, dE)) # print progress if(np.fabs(dE) < psi4.get_global_option('E_CONVERGENCE')): break # quit if converged return self.E
def format_options_for_input(): """Function to return a string of commands to replicate the current state of user-modified options. Used to capture C++ options information for distributed (sow/reap) input files. .. caution:: Some features are not yet implemented. Buy a developer a coffee. - Does not cover local (as opposed to global) options. - Does not work with array-type options. """ commands = '' commands += """\npsi4.set_memory(%s)\n\n""" % (psi4.get_memory()) for chgdopt in psi4.get_global_option_list(): if psi4.has_global_option_changed(chgdopt): chgdoptval = psi4.get_global_option(chgdopt) if isinstance(chgdoptval, basestring): commands += """psi4.set_global_option('%s', '%s')\n""" % (chgdopt, chgdoptval) elif isinstance(chgdoptval, int) or isinstance(chgdoptval, float): commands += """psi4.set_global_option('%s', %s)\n""" % (chgdopt, chgdoptval) else: raise ValidationError('Option \'%s\' is not of a type (string, int, float, bool) that can be processed.' % (chgdopt)) return commands
def run_gaussian_2(name, **kwargs): # throw an exception for open-shells if (psi4.get_option('SCF','REFERENCE') != 'RHF' ): raise ValidationError("""g2 computations require "reference rhf".""") # stash user options: optstash = p4util.OptionsState( ['FNOCC','COMPUTE_TRIPLES'], ['FNOCC','COMPUTE_MP4_TRIPLES'], ['FREEZE_CORE'], ['MP2_TYPE'], ['SCF','SCF_TYPE']) # override default scf_type psi4.set_local_option('SCF','SCF_TYPE','PK') # optimize geometry at scf level psi4.clean() psi4.set_global_option('BASIS',"6-31G(D)") driver.optimize('scf') psi4.clean() # scf frequencies for zpe # NOTE This line should not be needed, but without it there's a seg fault scf_e, ref = driver.frequency('scf', return_wfn=True) # thermodynamic properties du = psi4.get_variable('INTERNAL ENERGY CORRECTION') dh = psi4.get_variable('ENTHALPY CORRECTION') dg = psi4.get_variable('GIBBS FREE ENERGY CORRECTION') freqs = ref.frequencies() nfreq = freqs.dim(0) freqsum = 0.0 for i in range(0, nfreq): freqsum += freqs.get(i) zpe = freqsum / p4const.psi_hartree2wavenumbers * 0.8929 * 0.5 psi4.clean() # optimize geometry at mp2 (no frozen core) level # note: freeze_core isn't an option in MP2 psi4.set_global_option('FREEZE_CORE',"FALSE") psi4.set_global_option('MP2_TYPE', 'CONV') driver.optimize('mp2') psi4.clean() # qcisd(t) psi4.set_local_option('FNOCC','COMPUTE_MP4_TRIPLES',"TRUE") psi4.set_global_option('FREEZE_CORE',"TRUE") psi4.set_global_option('BASIS',"6-311G(D_P)") ref = driver.proc.run_fnocc('qcisd(t)', return_wfn=True, **kwargs) # HLC: high-level correction based on number of valence electrons nirrep = ref.nirrep() frzcpi = ref.frzcpi() nfzc = 0 for i in range (0,nirrep): nfzc += frzcpi[i] nalpha = ref.nalpha() - nfzc nbeta = ref.nbeta() - nfzc # hlc of gaussian-2 hlc = -0.00481 * nalpha -0.00019 * nbeta # hlc of gaussian-1 hlc1 = -0.00614 * nalpha eqci_6311gdp = psi4.get_variable("QCISD(T) TOTAL ENERGY") emp4_6311gd = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311gd = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # correction for diffuse functions psi4.set_global_option('BASIS',"6-311+G(D_P)") driver.energy('mp4') emp4_6311pg_dp = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311pg_dp = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # correction for polarization functions psi4.set_global_option('BASIS',"6-311G(2DF_P)") driver.energy('mp4') emp4_6311g2dfp = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311g2dfp = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # big basis mp2 psi4.set_global_option('BASIS',"6-311+G(3DF_2P)") #run_fnocc('_mp2',**kwargs) driver.energy('mp2') emp2_big = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() eqci = eqci_6311gdp e_delta_g2 = emp2_big + emp2_6311gd - emp2_6311g2dfp - emp2_6311pg_dp e_plus = emp4_6311pg_dp - emp4_6311gd e_2df = emp4_6311g2dfp - emp4_6311gd eg2 = eqci + e_delta_g2 + e_plus + e_2df eg2_mp2_0k = eqci + (emp2_big - emp2_6311gd) + hlc + zpe psi4.print_out('\n') psi4.print_out(' ==> G1/G2 Energy Components <==\n') psi4.print_out('\n') psi4.print_out(' QCISD(T): %20.12lf\n' % eqci) psi4.print_out(' E(Delta): %20.12lf\n' % e_delta_g2) psi4.print_out(' E(2DF): %20.12lf\n' % e_2df) psi4.print_out(' E(+): %20.12lf\n' % e_plus) psi4.print_out(' E(G1 HLC): %20.12lf\n' % hlc1) psi4.print_out(' E(G2 HLC): %20.12lf\n' % hlc) psi4.print_out(' E(ZPE): %20.12lf\n' % zpe) psi4.print_out('\n') psi4.print_out(' ==> 0 Kelvin Results <==\n') psi4.print_out('\n') eg2_0k = eg2 + zpe + hlc psi4.print_out(' G1: %20.12lf\n' % (eqci + e_plus + e_2df + hlc1 + zpe)) psi4.print_out(' G2(MP2): %20.12lf\n' % eg2_mp2_0k) psi4.print_out(' G2: %20.12lf\n' % eg2_0k) psi4.set_variable("G1 TOTAL ENERGY",eqci + e_plus + e_2df + hlc1 + zpe) psi4.set_variable("G2 TOTAL ENERGY",eg2_0k) psi4.set_variable("G2(MP2) TOTAL ENERGY",eg2_mp2_0k) psi4.print_out('\n') T = psi4.get_global_option('T') psi4.print_out(' ==> %3.0lf Kelvin Results <==\n'% T) psi4.print_out('\n') internal_energy = eg2_mp2_0k + du - zpe / 0.8929 enthalpy = eg2_mp2_0k + dh - zpe / 0.8929 gibbs = eg2_mp2_0k + dg - zpe / 0.8929 psi4.print_out(' G2(MP2) energy: %20.12lf\n' % internal_energy ) psi4.print_out(' G2(MP2) enthalpy: %20.12lf\n' % enthalpy) psi4.print_out(' G2(MP2) free energy: %20.12lf\n' % gibbs) psi4.print_out('\n') psi4.set_variable("G2(MP2) INTERNAL ENERGY",internal_energy) psi4.set_variable("G2(MP2) ENTHALPY",enthalpy) psi4.set_variable("G2(MP2) FREE ENERGY",gibbs) internal_energy = eg2_0k + du - zpe / 0.8929 enthalpy = eg2_0k + dh - zpe / 0.8929 gibbs = eg2_0k + dg - zpe / 0.8929 psi4.print_out(' G2 energy: %20.12lf\n' % internal_energy ) psi4.print_out(' G2 enthalpy: %20.12lf\n' % enthalpy) psi4.print_out(' G2 free energy: %20.12lf\n' % gibbs) psi4.set_variable("CURRENT ENERGY",eg2_0k) psi4.set_variable("G2 INTERNAL ENERGY",internal_energy) psi4.set_variable("G2 ENTHALPY",enthalpy) psi4.set_variable("G2 FREE ENERGY",gibbs) psi4.clean() optstash.restore() # return 0K g2 results return eg2_0k
def _nbody_gufunc(func, method_string, **kwargs): """ Computes the nbody interaction energy, gradient, or Hessian depending on input. Parameters ---------- func : python function Python function that accepts method_string and a molecule and returns a energy, gradient, or Hessian. method_string : str Lowername to be passed to function molecule : psi4.Molecule (default: Global Molecule) Molecule to use in all computations return_wfn : bool (default: False) Return a wavefunction or not bsse_type : str or list (default: None, this function is not called) Type of BSSE correction to compute: CP, NoCP, or VMFC. The first in this list is returned by this function. max_nbody : int Maximum n-body to compute, cannot exceede the number of fragments in the moleucle ptype : str Type of the procedure passed in return_total_data : bool (default: False) If True returns the total data (energy/gradient/etc) of the system otherwise returns interaction data Returns ------- data : return type of func The interaction data wfn : psi4.Wavefunction (optional) A wavefunction with energy/gradient/hessian set appropriotely. This wavefunction also contains Notes ----- This is a generalized univeral function for compute interaction quantities. Examples -------- """ ### ==> Parse some kwargs <== kwargs = p4util.kwargs_lower(kwargs) return_wfn = kwargs.pop('return_wfn', False) ptype = kwargs.pop('ptype', None) return_total_data = kwargs.pop('return_total_data', False) molecule = kwargs.pop('molecule', psi4.get_active_molecule()) molecule.update_geometry() psi4.clean_variables() if ptype not in ['energy', 'gradient', 'hessian']: raise ValidationError("""N-Body driver: The ptype '%s' is not regonized.""" % ptype) # Figure out BSSE types do_cp = False do_nocp = False do_vmfc = False return_method = False # Must be passed bsse_type bsse_type_list = kwargs.pop('bsse_type') if bsse_type_list is None: raise ValidationError("N-Body GUFunc: Must pass a bsse_type") if not isinstance(bsse_type_list, list): bsse_type_list = [bsse_type_list] for num, btype in enumerate(bsse_type_list): if btype.lower() == 'cp': do_cp = True if (num == 0): return_method = 'cp' elif btype.lower() == 'nocp': do_nocp = True if (num == 0): return_method = 'nocp' elif btype.lower() == 'vmfc': do_vmfc = True if (num == 0): return_method = 'vmfc' else: raise ValidationError("N-Body GUFunc: bsse_type '%s' is not recognized" % btype.lower()) max_nbody = kwargs.get('max_nbody', -1) max_frag = molecule.nfragments() if max_nbody == -1: max_nbody = molecule.nfragments() else: max_nbody = min(max_nbody, max_frag) # What levels do we need? nbody_range = range(1, max_nbody + 1) fragment_range = range(1, max_frag + 1) # If we are doing CP lets save them integrals if 'cp' in bsse_type_list and (len(bsse_type_list) == 1): # Set to save RI integrals for repeated full-basis computations ri_ints_io = psi4.get_global_option('DF_INTS_IO') # inquire if above at all applies to dfmp2 or just scf psi4.set_global_option('DF_INTS_IO', 'SAVE') psioh = psi4.IOManager.shared_object() psioh.set_specific_retention(97, True) bsse_str = bsse_type_list[0] if len(bsse_type_list) >1: bsse_str = str(bsse_type_list) psi4.print_out("\n\n") psi4.print_out(" ===> N-Body Interaction Abacus <===\n") psi4.print_out(" BSSE Treatment: %s\n" % bsse_str) cp_compute_list = {x:set() for x in nbody_range} nocp_compute_list = {x:set() for x in nbody_range} vmfc_compute_list = {x:set() for x in nbody_range} vmfc_level_list = {x:set() for x in nbody_range} # Need to sum something slightly different # Build up compute sets if do_cp: # Everything is in dimer basis basis_tuple = tuple(fragment_range) for nbody in nbody_range: for x in it.combinations(fragment_range, nbody): cp_compute_list[nbody].add( (x, basis_tuple) ) if do_nocp: # Everything in monomer basis for nbody in nbody_range: for x in it.combinations(fragment_range, nbody): nocp_compute_list[nbody].add( (x, x) ) if do_vmfc: # Like a CP for all combinations of pairs or greater for nbody in nbody_range: for cp_combos in it.combinations(fragment_range, nbody): basis_tuple = tuple(cp_combos) for interior_nbody in nbody_range: for x in it.combinations(cp_combos, interior_nbody): combo_tuple = (x, basis_tuple) vmfc_compute_list[interior_nbody].add( combo_tuple ) vmfc_level_list[len(basis_tuple)].add( combo_tuple ) # Build a comprehensive compute_range compute_list = {x:set() for x in nbody_range} for n in nbody_range: compute_list[n] |= cp_compute_list[n] compute_list[n] |= nocp_compute_list[n] compute_list[n] |= vmfc_compute_list[n] psi4.print_out(" Number of %d-body computations: %d\n" % (n, len(compute_list[n]))) # Build size and slices dictionaries fragment_size_dict = {frag: molecule.extract_subsets(frag).natom() for frag in range(1, max_frag+1)} start = 0 fragment_slice_dict = {} for k, v in fragment_size_dict.items(): fragment_slice_dict[k] = slice(start, start + v) start += v molecule_total_atoms = sum(fragment_size_dict.values()) # Now compute the energies energies_dict = {} ptype_dict = {} for n in compute_list.keys(): psi4.print_out("\n ==> N-Body: Now computing %d-body complexes <==\n\n" % n) print("\n ==> N-Body: Now computing %d-body complexes <==\n" % n) total = len(compute_list[n]) for num, pair in enumerate(compute_list[n]): psi4.print_out("\n N-Body: Computing complex (%d/%d) with fragments %s in the basis of fragments %s.\n\n" % (num + 1, total, str(pair[0]), str(pair[1]))) ghost = list(set(pair[1]) - set(pair[0])) current_mol = molecule.extract_subsets(list(pair[0]), ghost) ptype_dict[pair] = func(method_string, molecule=current_mol, **kwargs) energies_dict[pair] = psi4.get_variable("CURRENT ENERGY") psi4.print_out("\n N-Body: Complex Energy (fragments = %s, basis = %s: %20.14f)\n" % (str(pair[0]), str(pair[1]), energies_dict[pair])) if 'cp' in bsse_type_list and (len(bsse_type_list) == 1): psi4.set_global_option('DF_INTS_IO', 'LOAD') psi4.clean() # Final dictionaries cp_energy_by_level = {n: 0.0 for n in nbody_range} nocp_energy_by_level = {n: 0.0 for n in nbody_range} cp_energy_body_dict = {n: 0.0 for n in nbody_range} nocp_energy_body_dict = {n: 0.0 for n in nbody_range} vmfc_energy_body_dict = {n: 0.0 for n in nbody_range} # Build out ptype dictionaries if needed if ptype != 'energy': if ptype == 'gradient': arr_shape = (molecule_total_atoms, 3) elif ptype == 'hessian': arr_shape = (molecule_total_atoms * 3, molecule_total_atoms * 3) else: raise KeyError("N-Body: ptype '%s' not recognized" % ptype) cp_ptype_by_level = {n: np.zeros(arr_shape) for n in nbody_range} nocp_ptype_by_level = {n: np.zeros(arr_shape) for n in nbody_range} cp_ptype_body_dict = {n: np.zeros(arr_shape) for n in nbody_range} nocp_ptype_body_dict = {n: np.zeros(arr_shape) for n in nbody_range} vmfc_ptype_body_dict = {n: np.zeros(arr_shape) for n in nbody_range} else: cp_ptype_by_level, cp_ptype_body_dict = None, None nocp_ptype_by_level, nocp_ptype_body_dict = None, None vmfc_ptype_by_level= None # Sum up all of the levels for n in nbody_range: # Energy cp_energy_by_level[n] = sum(energies_dict[v] for v in cp_compute_list[n]) nocp_energy_by_level[n] = sum(energies_dict[v] for v in nocp_compute_list[n]) # Special vmfc case if n > 1: vmfc_energy_body_dict[n] = vmfc_energy_body_dict[n - 1] for tup in vmfc_level_list[n]: vmfc_energy_body_dict[n] += ((-1) ** (n - len(tup[0]))) * energies_dict[tup] # Do ptype if ptype != 'energy': _sum_cluster_ptype_data(ptype, ptype_dict, cp_compute_list[n], fragment_slice_dict, fragment_size_dict, cp_ptype_by_level[n]) _sum_cluster_ptype_data(ptype, ptype_dict, nocp_compute_list[n], fragment_slice_dict, fragment_size_dict, nocp_ptype_by_level[n]) _sum_cluster_ptype_data(ptype, ptype_dict, vmfc_level_list[n], fragment_slice_dict, fragment_size_dict, vmfc_ptype_by_level[n], vmfc=True) # Compute cp energy and ptype if do_cp: for n in nbody_range: if n == max_frag: cp_energy_body_dict[n] = cp_energy_by_level[n] if ptype != 'energy': cp_ptype_body_dict[n][:] = cp_ptype_by_level[n] continue for k in range(1, n + 1): take_nk = nCr(max_frag - k - 1, n - k) sign = ((-1) ** (n - k)) value = cp_energy_by_level[k] cp_energy_body_dict[n] += take_nk * sign * value if ptype != 'energy': value = cp_ptype_by_level[k] cp_ptype_body_dict[n] += take_nk * sign * value _print_nbody_energy(cp_energy_body_dict, "Counterpoise Corrected (CP)") cp_interaction_energy = cp_energy_body_dict[max_nbody] - cp_energy_body_dict[1] psi4.set_variable('Counterpoise Corrected Total Energy', cp_energy_body_dict[max_nbody]) psi4.set_variable('Counterpoise Corrected Interaction Energy', cp_interaction_energy) for n in nbody_range[1:]: var_key = 'CP-CORRECTED %d-BODY INTERACTION ENERGY' % n psi4.set_variable(var_key, cp_energy_body_dict[n] - cp_energy_body_dict[1]) # Compute nocp energy and ptype if do_nocp: for n in nbody_range: if n == max_frag: nocp_energy_body_dict[n] = nocp_energy_by_level[n] if ptype != 'energy': nocp_ptype_body_dict[n][:] = nocp_ptype_by_level[n] continue for k in range(1, n + 1): take_nk = nCr(max_frag - k - 1, n - k) sign = ((-1) ** (n - k)) value = nocp_energy_by_level[k] nocp_energy_body_dict[n] += take_nk * sign * value if ptype != 'energy': value = nocp_ptype_by_level[k] nocp_ptype_body_dict[n] += take_nk * sign * value _print_nbody_energy(nocp_energy_body_dict, "Non-Counterpoise Corrected (NoCP)") nocp_interaction_energy = nocp_energy_body_dict[max_nbody] - nocp_energy_body_dict[1] psi4.set_variable('Non-Counterpoise Corrected Total Energy', nocp_energy_body_dict[max_nbody]) psi4.set_variable('Non-Counterpoise Corrected Interaction Energy', nocp_interaction_energy) for n in nbody_range[1:]: var_key = 'NOCP-CORRECTED %d-BODY INTERACTION ENERGY' % n psi4.set_variable(var_key, nocp_energy_body_dict[n] - nocp_energy_body_dict[1]) # Compute vmfc energy and ptype if do_vmfc: _print_nbody_energy(vmfc_energy_body_dict, "Valiron-Mayer Function Couterpoise (VMFC)") vmfc_interaction_energy = vmfc_energy_body_dict[max_nbody] - vmfc_energy_body_dict[1] psi4.set_variable('Valiron-Mayer Function Couterpoise Total Energy', vmfc_energy_body_dict[max_nbody]) psi4.set_variable('Valiron-Mayer Function Couterpoise Interaction Energy', vmfc_interaction_energy) for n in nbody_range[1:]: var_key = 'VMFC-CORRECTED %d-BODY INTERACTION ENERGY' % n psi4.set_variable(var_key, vmfc_energy_body_dict[n] - vmfc_energy_body_dict[1]) if return_method == 'cp': ptype_body_dict = cp_ptype_body_dict energy_body_dict = cp_energy_body_dict elif return_method == 'nocp': ptype_body_dict = nocp_ptype_body_dict energy_body_dict = nocp_energy_body_dict elif return_method == 'vmfc': ptype_body_dict = vmfc_ptype_body_dict energy_body_dict = vmfc_energy_body_dict else: raise ValidationError("N-Body Wrapper: Invalid return type. Should never be here, please post this error on github.") # Figure out and build return types if return_total_data: ret_energy = energy_body_dict[max_nbody] else: ret_energy = energy_body_dict[max_nbody] ret_energy -= energy_body_dict[1] if ptype != 'energy': if return_total_data: np_final_ptype = ptype_body_dict[max_nbody].copy() else: np_final_ptype = ptype_body_dict[max_nbody].copy() np_final_ptype -= ptype_body_dict[1] ret_ptype = psi4.Matrix(*np_cp_final_ptype.shape) ret_ptype_view = np.asarray(final_ptype) ret_ptype_view[:] = np_final_ptype else: ret_ptype = ret_energy # Build and set a wavefunction wfn = psi4.new_wavefunction(molecule, 'sto-3g') wfn.nbody_energy = energies_dict wfn.nbody_ptype = ptype_dict wfn.nbody_body_energy = energy_body_dict wfn.nbody_body_ptype = ptype_body_dict if ptype == 'gradient': wfn.set_gradient(ret_ptype) elif ptype == 'hessian': wfn.set_hessian(ret_ptype) psi4.set_variable("CURRENT ENERGY", ret_energy) if return_wfn: return (ret_ptype, wfn) else: return ret_ptype
def ip_fitting(molecule, omega_l, omega_r, **kwargs): kwargs = p4util.kwargs_lower(kwargs) # By default, zero the omega to 3 digits omega_tol = kwargs.get('omega_tolerance', 1.0E-3) # By default, do up to twenty iterations maxiter = kwargs.get('maxiter', 20) # By default, do not read previous 180 orbitals file read = False read180 = '' if 'read' in kwargs: read = True read180 = kwargs['read'] # The molecule is required, and should be the neutral species molecule.update_geometry() charge0 = molecule.molecular_charge() mult0 = molecule.multiplicity() # How many electrons are there? N = 0 for A in range(molecule.natom()): N += molecule.Z(A) N -= charge0 N = int(N) Nb = int((N - mult0 + 1) / 2) Na = int(N - Nb) # Work in the ot namespace for this procedure psi4.IO.set_default_namespace("ot") # Burn in to determine orbital eigenvalues if read: psi4.set_global_option("GUESS", "READ") copy_file_to_scratch(read180, 'psi', 'ot', 180) old_guess = psi4.get_global_option("GUESS") psi4.set_global_option("DF_INTS_IO", "SAVE") psi4.print_out("""\n\t==> IP Fitting SCF: Burn-in <==\n""") E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) psi4.set_global_option("DF_INTS_IO", "LOAD") # Determine H**O, to determine mult1 eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() if Na == Nb: H**O = -Nb elif Nb == 0: H**O = Na else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: H**O = Na else: H**O = -Nb Na1 = Na Nb1 = Nb if H**O > 0: Na1 = Na1 - 1 else: Nb1 = Nb1 - 1 charge1 = charge0 + 1 mult1 = Na1 - Nb1 + 1 omegas = [] E0s = [] E1s = [] kIPs = [] IPs = [] types = [] # Right endpoint psi4.set_global_option('DFT_OMEGA', omega_r) # Neutral if read: psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out("""\n\t==> IP Fitting SCF: Neutral, Right Endpoint <==\n""") E0r, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0 if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b E_HOMOr = E_HOMO psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation if read: psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out("""\n\t==> IP Fitting SCF: Cation, Right Endpoint <==\n""") E1r = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IPr = E1r - E0r kIPr = -E_HOMOr delta_r = IPr - kIPr if IPr > kIPr: message = ( """\n***IP Fitting Error: Right Omega limit should have kIP > IP""" ) raise ValidationError(message) omegas.append(omega_r) types.append('Right Limit') E0s.append(E0r) E1s.append(E1r) IPs.append(IPr) kIPs.append(kIPr) # Use previous orbitals from here out psi4.set_global_option("GUESS", "READ") # Left endpoint psi4.set_global_option('DFT_OMEGA', omega_l) # Neutral psi4.IO.change_file_namespace(180, "neutral", "ot") molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out("""\n\t==> IP Fitting SCF: Neutral, Left Endpoint <==\n""") E0l, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0 if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b E_HOMOl = E_HOMO psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation psi4.IO.change_file_namespace(180, "cation", "ot") molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out("""\n\t==> IP Fitting SCF: Cation, Left Endpoint <==\n""") E1l = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IPl = E1l - E0l kIPl = -E_HOMOl delta_l = IPl - kIPl if IPl < kIPl: message = ( """\n***IP Fitting Error: Left Omega limit should have kIP < IP""") raise ValidationError(message) omegas.append(omega_l) types.append('Left Limit') E0s.append(E0l) E1s.append(E1l) IPs.append(IPl) kIPs.append(kIPl) converged = False repeat_l = 0 repeat_r = 0 step = 0 while True: step = step + 1 # Regula Falsi (modified) if repeat_l > 1: delta_l = delta_l / 2.0 if repeat_r > 1: delta_r = delta_r / 2.0 omega = -(omega_r - omega_l) / (delta_r - delta_l) * delta_l + omega_l psi4.set_global_option('DFT_OMEGA', omega) # Neutral psi4.IO.change_file_namespace(180, "neutral", "ot") molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out( """\n\t==> IP Fitting SCF: Neutral, Omega = %11.3E <==\n""" % omega) E0, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0 if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation psi4.IO.change_file_namespace(180, "cation", "ot") molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out( """\n\t==> IP Fitting SCF: Cation, Omega = %11.3E <==\n""" % omega) E1 = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IP = E1 - E0 kIP = -E_HOMO delta = IP - kIP if kIP > IP: omega_r = omega E0r = E0 E1r = E1 IPr = IP kIPr = kIP delta_r = delta repeat_r = 0 repeat_l = repeat_l + 1 else: omega_l = omega E0l = E0 E1l = E1 IPl = IP kIPl = kIP delta_l = delta repeat_l = 0 repeat_r = repeat_r + 1 omegas.append(omega) types.append('Regula-Falsi') E0s.append(E0) E1s.append(E1) IPs.append(IP) kIPs.append(kIP) # Termination if (abs(omega_l - omega_r) < omega_tol or step > maxiter): converged = True break # Properly, should clone molecule but since not returned and easy to unblemish, molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.IO.set_default_namespace("") psi4.print_out("""\n\t==> IP Fitting Results <==\n\n""") psi4.print_out("""\t => Occupation Determination <= \n\n""") psi4.print_out("""\t %6s %6s %6s %6s %6s %6s\n""" % ('N', 'Na', 'Nb', 'Charge', 'Mult', 'H**O')) psi4.print_out("""\t Neutral: %6d %6d %6d %6d %6d %6d\n""" % (N, Na, Nb, charge0, mult0, H**O)) psi4.print_out("""\t Cation: %6d %6d %6d %6d %6d\n\n""" % (N - 1, Na1, Nb1, charge1, mult1)) psi4.print_out("""\t => Regula Falsi Iterations <=\n\n""") psi4.print_out("""\t%3s %11s %14s %14s %14s %s\n""" % ('N', 'Omega', 'IP', 'kIP', 'Delta', 'Type')) for k in range(len(omegas)): psi4.print_out( """\t%3d %11.3E %14.6E %14.6E %14.6E %s\n""" % (k + 1, omegas[k], IPs[k], kIPs[k], IPs[k] - kIPs[k], types[k])) if converged: psi4.print_out("""\n\tIP Fitting Converged\n""") psi4.print_out("""\tFinal omega = %14.6E\n""" % ((omega_l + omega_r) / 2)) psi4.print_out( """\n\t"M,I. does the dying. Fleet just does the flying."\n""") psi4.print_out("""\t\t\t-Starship Troopers\n""") else: psi4.print_out("""\n\tIP Fitting did not converge!\n""") psi4.set_global_option("DF_INTS_IO", "NONE") psi4.set_global_option("GUESS", old_guess)
def frac_traverse(molecule, **kwargs): kwargs = p4util.kwargs_lower(kwargs) # The molecule is required, and should be the neutral species molecule.update_geometry() charge0 = molecule.molecular_charge() mult0 = molecule.multiplicity() chargep = charge0 + 1 chargem = charge0 - 1 # By default, the multiplicity of the cation/anion are mult0 + 1 # These are overridden with the cation_mult and anion_mult kwargs multp = kwargs.get('cation_mult', mult0 + 1) multm = kwargs.get('anion_mult', mult0 + 1) # By default, we start the frac procedure on the 25th iteration # when not reading a previous guess frac_start = kwargs.get('frac_start', 25) # By default, we occupy by tenths of electrons HOMO_occs = kwargs.get( 'HOMO_occs', [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]) LUMO_occs = kwargs.get( 'LUMO_occs', [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]) # By default, H**O and LUMO are both in alpha Z = 0 for A in range(molecule.natom()): Z += molecule.Z(A) Z -= charge0 H**O = kwargs.get('H**O', (Z / 2 + 1 if (Z % 2) else Z / 2)) LUMO = kwargs.get('LUMO', H**O + 1) # By default, DIIS in FRAC (1.0 occupation is always DIIS'd) frac_diis = kwargs.get('frac_diis', True) # By default, use the neutral orbitals as a guess for the anion neutral_guess = kwargs.get('neutral_guess', True) # By default, burn-in with UHF first, if UKS hf_guess = False if psi4.get_global_option('REFERENCE') == 'UKS': hf_guess = kwargs.get('hf_guess', True) # By default, re-guess at each N continuous_guess = kwargs.get('continuous_guess', False) # By default, drop the files to the molecule's name root = kwargs.get('filename', molecule.name()) traverse_filename = root + '.traverse.dat' # => Traverse <= # occs = [] energies = [] potentials = [] convs = [] # => Run the neutral for its orbitals, if requested <= # old_df_ints_io = psi4.get_global_option("DF_INTS_IO") psi4.set_global_option("DF_INTS_IO", "SAVE") old_guess = psi4.get_global_option("GUESS") if (neutral_guess): if (hf_guess): psi4.set_global_option("REFERENCE", "UHF") energy('scf') psi4.set_global_option("GUESS", "READ") psi4.set_global_option("DF_INTS_IO", "LOAD") # => Run the anion first <= # molecule.set_molecular_charge(chargem) molecule.set_multiplicity(multm) # => Burn the anion in with hf, if requested <= # if hf_guess: psi4.set_global_option("REFERENCE", "UHF") energy('scf', molecule=molecule, **kwargs) psi4.set_global_option("REFERENCE", "UKS") psi4.set_global_option("GUESS", "READ") psi4.set_global_option("DF_INTS_IO", "SAVE") psi4.set_global_option("FRAC_START", frac_start) psi4.set_global_option("FRAC_RENORMALIZE", True) psi4.set_global_option("FRAC_LOAD", False) for occ in LUMO_occs: psi4.set_global_option("FRAC_OCC", [LUMO]) psi4.set_global_option("FRAC_VAL", [occ]) E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) C = 1 if E == 0.0: E = psi4.get_variable('SCF ITERATION ENERGY') C = 0 if LUMO > 0: eps = wfn.epsilon_a() potentials.append(eps[int(LUMO) - 1]) else: eps = wfn.epsilon_b() potentials.append(eps[-int(LUMO) - 1]) occs.append(occ) energies.append(E) convs.append(C) psi4.set_global_option("FRAC_START", 2) psi4.set_global_option("FRAC_LOAD", True) psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_DIIS", frac_diis) psi4.set_global_option("DF_INTS_IO", "LOAD") # => Run the neutral next <= # molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) # Burn the neutral in with hf, if requested <= # if not continuous_guess: psi4.set_global_option("GUESS", old_guess) if hf_guess: psi4.set_global_option("FRAC_START", 0) psi4.set_global_option("REFERENCE", "UHF") energy('scf', molecule=molecule, **kwargs) psi4.set_global_option("REFERENCE", "UKS") psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_LOAD", False) psi4.set_global_option("FRAC_START", frac_start) psi4.set_global_option("FRAC_RENORMALIZE", True) for occ in HOMO_occs: psi4.set_global_option("FRAC_OCC", [H**O]) psi4.set_global_option("FRAC_VAL", [occ]) E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) C = 1 if E == 0.0: E = psi4.get_variable('SCF ITERATION ENERGY') C = 0 if LUMO > 0: eps = wfn.epsilon_a() potentials.append(eps[int(H**O) - 1]) else: eps = wfn.epsilon_b() potentials.append(eps[-int(H**O) - 1]) occs.append(occ - 1.0) energies.append(E) convs.append(C) psi4.set_global_option("FRAC_START", 2) psi4.set_global_option("FRAC_LOAD", True) psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_DIIS", frac_diis) psi4.set_global_option("DF_INTS_IO", "LOAD") psi4.set_global_option("DF_INTS_IO", old_df_ints_io) # => Print the results out <= # E = {} psi4.print_out( """\n ==> Fractional Occupation Traverse Results <==\n\n""") psi4.print_out("""\t%-11s %-24s %-24s %11s\n""" % ('N', 'Energy', 'H**O Energy', 'Converged')) for k in range(len(occs)): psi4.print_out("""\t%11.3E %24.16E %24.16E %11d\n""" % (occs[k], energies[k], potentials[k], convs[k])) E[occs[k]] = energies[k] psi4.print_out('\n\t"You trying to be a hero Watkins?"\n') psi4.print_out('\t"Just trying to kill some bugs sir!"\n') psi4.print_out('\t\t\t-Starship Troopers\n') # Drop the files out fh = open(traverse_filename, 'w') fh.write("""\t%-11s %-24s %-24s %11s\n""" % ('N', 'Energy', 'H**O Energy', 'Converged')) for k in range(len(occs)): fh.write("""\t%11.3E %24.16E %24.16E %11d\n""" % (occs[k], energies[k], potentials[k], convs[k])) fh.close() # Properly, should clone molecule but since not returned and easy to unblemish, molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) return E
def ip_fitting(molecule, omega_l, omega_r, **kwargs): kwargs = p4util.kwargs_lower(kwargs) # By default, zero the omega to 3 digits omega_tol = kwargs.get('omega_tolerance', 1.0E-3) # By default, do up to twenty iterations maxiter = kwargs.get('maxiter', 20) # By default, do not read previous 180 orbitals file read = False read180 = '' if 'read' in kwargs: read = True read180 = kwargs['read'] # The molecule is required, and should be the neutral species molecule.update_geometry() charge0 = molecule.molecular_charge() mult0 = molecule.multiplicity() # How many electrons are there? N = 0 for A in range(molecule.natom()): N += molecule.Z(A) N -= charge0 N = int(N) Nb = int((N - mult0 + 1) / 2) Na = int(N - Nb) # Work in the ot namespace for this procedure psi4.IO.set_default_namespace("ot") # Burn in to determine orbital eigenvalues if read: psi4.set_global_option("GUESS", "READ") copy_file_to_scratch(read180, 'psi', 'ot', 180) old_guess = psi4.get_global_option("GUESS") psi4.set_global_option("DF_INTS_IO", "SAVE") psi4.print_out("""\n\t==> IP Fitting SCF: Burn-in <==\n""") E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) psi4.set_global_option("DF_INTS_IO", "LOAD") # Determine H**O, to determine mult1 eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() if Na == Nb: H**O = -Nb elif Nb == 0: H**O = Na else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: H**O = Na else: H**O = -Nb Na1 = Na; Nb1 = Nb; if H**O > 0: Na1 = Na1 - 1; else: Nb1 = Nb1 - 1; charge1 = charge0 + 1; mult1 = Na1 - Nb1 + 1 omegas = [] E0s = [] E1s = [] kIPs = [] IPs = [] types = [] # Right endpoint psi4.set_global_option('DFT_OMEGA', omega_r) # Neutral if read: psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out("""\n\t==> IP Fitting SCF: Neutral, Right Endpoint <==\n""") E0r, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0; if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b E_HOMOr = E_HOMO psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation if read: psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out("""\n\t==> IP Fitting SCF: Cation, Right Endpoint <==\n""") E1r = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IPr = E1r - E0r; kIPr = -E_HOMOr; delta_r = IPr - kIPr; if IPr > kIPr: message = ("""\n***IP Fitting Error: Right Omega limit should have kIP > IP""") raise ValidationError(message) omegas.append(omega_r) types.append('Right Limit') E0s.append(E0r) E1s.append(E1r) IPs.append(IPr) kIPs.append(kIPr) # Use previous orbitals from here out psi4.set_global_option("GUESS", "READ") # Left endpoint psi4.set_global_option('DFT_OMEGA', omega_l) # Neutral psi4.IO.change_file_namespace(180, "neutral", "ot") molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out("""\n\t==> IP Fitting SCF: Neutral, Left Endpoint <==\n""") E0l, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0 if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b E_HOMOl = E_HOMO psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation psi4.IO.change_file_namespace(180, "cation", "ot") molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out("""\n\t==> IP Fitting SCF: Cation, Left Endpoint <==\n""") E1l = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IPl = E1l - E0l kIPl = -E_HOMOl delta_l = IPl - kIPl if IPl < kIPl: message = ("""\n***IP Fitting Error: Left Omega limit should have kIP < IP""") raise ValidationError(message) omegas.append(omega_l) types.append('Left Limit') E0s.append(E0l) E1s.append(E1l) IPs.append(IPl) kIPs.append(kIPl) converged = False repeat_l = 0 repeat_r = 0 step = 0 while True: step = step + 1 # Regula Falsi (modified) if repeat_l > 1: delta_l = delta_l / 2.0 if repeat_r > 1: delta_r = delta_r / 2.0 omega = - (omega_r - omega_l) / (delta_r - delta_l) * delta_l + omega_l psi4.set_global_option('DFT_OMEGA', omega) # Neutral psi4.IO.change_file_namespace(180, "neutral", "ot") molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.print_out("""\n\t==> IP Fitting SCF: Neutral, Omega = %11.3E <==\n""" % omega) E0, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) eps_a = wfn.epsilon_a() eps_b = wfn.epsilon_b() E_HOMO = 0.0 if Nb == 0: E_HOMO = eps_a[int(Na - 1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if E_a >= E_b: E_HOMO = E_a else: E_HOMO = E_b psi4.IO.change_file_namespace(180, "ot", "neutral") # Cation psi4.IO.change_file_namespace(180, "cation", "ot") molecule.set_molecular_charge(charge1) molecule.set_multiplicity(mult1) psi4.print_out("""\n\t==> IP Fitting SCF: Cation, Omega = %11.3E <==\n""" % omega) E1 = energy('scf', molecule=molecule, **kwargs) psi4.IO.change_file_namespace(180, "ot", "cation") IP = E1 - E0 kIP = -E_HOMO delta = IP - kIP if kIP > IP: omega_r = omega E0r = E0 E1r = E1 IPr = IP kIPr = kIP delta_r = delta repeat_r = 0 repeat_l = repeat_l + 1 else: omega_l = omega E0l = E0 E1l = E1 IPl = IP kIPl = kIP delta_l = delta repeat_l = 0; repeat_r = repeat_r + 1 omegas.append(omega) types.append('Regula-Falsi') E0s.append(E0) E1s.append(E1) IPs.append(IP) kIPs.append(kIP) # Termination if (abs(omega_l - omega_r) < omega_tol or step > maxiter): converged = True break # Properly, should clone molecule but since not returned and easy to unblemish, molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) psi4.IO.set_default_namespace("") psi4.print_out("""\n\t==> IP Fitting Results <==\n\n""") psi4.print_out("""\t => Occupation Determination <= \n\n""") psi4.print_out("""\t %6s %6s %6s %6s %6s %6s\n""" % ('N', 'Na', 'Nb', 'Charge', 'Mult', 'H**O')) psi4.print_out("""\t Neutral: %6d %6d %6d %6d %6d %6d\n""" % (N, Na, Nb, charge0, mult0, H**O)) psi4.print_out("""\t Cation: %6d %6d %6d %6d %6d\n\n""" % (N - 1, Na1, Nb1, charge1, mult1)) psi4.print_out("""\t => Regula Falsi Iterations <=\n\n""") psi4.print_out("""\t%3s %11s %14s %14s %14s %s\n""" % ('N','Omega','IP','kIP','Delta','Type')) for k in range(len(omegas)): psi4.print_out("""\t%3d %11.3E %14.6E %14.6E %14.6E %s\n""" % (k + 1, omegas[k], IPs[k], kIPs[k], IPs[k] - kIPs[k], types[k])) if converged: psi4.print_out("""\n\tIP Fitting Converged\n""") psi4.print_out("""\tFinal omega = %14.6E\n""" % ((omega_l + omega_r) / 2)) psi4.print_out("""\n\t"M,I. does the dying. Fleet just does the flying."\n""") psi4.print_out("""\t\t\t-Starship Troopers\n""") else: psi4.print_out("""\n\tIP Fitting did not converge!\n""") psi4.set_global_option("DF_INTS_IO", "NONE") psi4.set_global_option("GUESS", old_guess)
def frac_traverse(molecule, **kwargs): kwargs = p4util.kwargs_lower(kwargs) # The molecule is required, and should be the neutral species molecule.update_geometry() charge0 = molecule.molecular_charge() mult0 = molecule.multiplicity() chargep = charge0 + 1 chargem = charge0 - 1 # By default, the multiplicity of the cation/anion are mult0 + 1 # These are overridden with the cation_mult and anion_mult kwargs multp = kwargs.get('cation_mult', mult0 + 1) multm = kwargs.get('anion_mult', mult0 + 1) # By default, we start the frac procedure on the 25th iteration # when not reading a previous guess frac_start = kwargs.get('frac_start', 25) # By default, we occupy by tenths of electrons HOMO_occs = kwargs.get('HOMO_occs', [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]) LUMO_occs = kwargs.get('LUMO_occs', [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]) # By default, H**O and LUMO are both in alpha Z = 0; for A in range(molecule.natom()): Z += molecule.Z(A) Z -= charge0 H**O = kwargs.get('H**O', (Z / 2 + 1 if (Z % 2) else Z / 2)) LUMO = kwargs.get('LUMO', H**O + 1) # By default, DIIS in FRAC (1.0 occupation is always DIIS'd) frac_diis = kwargs.get('frac_diis', True) # By default, use the neutral orbitals as a guess for the anion neutral_guess = kwargs.get('neutral_guess', True) # By default, burn-in with UHF first, if UKS hf_guess = False if psi4.get_global_option('REFERENCE') == 'UKS': hf_guess = kwargs.get('hf_guess', True) # By default, re-guess at each N continuous_guess = kwargs.get('continuous_guess', False) # By default, drop the files to the molecule's name root = kwargs.get('filename', molecule.name()) traverse_filename = root + '.traverse.dat' # => Traverse <= # occs = [] energies = [] potentials = [] convs = [] # => Run the neutral for its orbitals, if requested <= # old_df_ints_io = psi4.get_global_option("DF_INTS_IO") psi4.set_global_option("DF_INTS_IO", "SAVE") old_guess = psi4.get_global_option("GUESS") if (neutral_guess): if (hf_guess): psi4.set_global_option("REFERENCE","UHF") energy('scf') psi4.set_global_option("GUESS", "READ") psi4.set_global_option("DF_INTS_IO", "LOAD") # => Run the anion first <= # molecule.set_molecular_charge(chargem) molecule.set_multiplicity(multm) # => Burn the anion in with hf, if requested <= # if hf_guess: psi4.set_global_option("REFERENCE","UHF") energy('scf', molecule=molecule, **kwargs) psi4.set_global_option("REFERENCE","UKS") psi4.set_global_option("GUESS", "READ") psi4.set_global_option("DF_INTS_IO", "SAVE") psi4.set_global_option("FRAC_START", frac_start) psi4.set_global_option("FRAC_RENORMALIZE", True) psi4.set_global_option("FRAC_LOAD", False) for occ in LUMO_occs: psi4.set_global_option("FRAC_OCC", [LUMO]) psi4.set_global_option("FRAC_VAL", [occ]) E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) C = 1 if E == 0.0: E = psi4.get_variable('SCF ITERATION ENERGY') C = 0 if LUMO > 0: eps = wfn.epsilon_a() potentials.append(eps[int(LUMO) - 1]) else: eps = wfn.epsilon_b() potentials.append(eps[-int(LUMO) - 1]) occs.append(occ) energies.append(E) convs.append(C) psi4.set_global_option("FRAC_START", 2) psi4.set_global_option("FRAC_LOAD", True) psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_DIIS", frac_diis) psi4.set_global_option("DF_INTS_IO", "LOAD") # => Run the neutral next <= # molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) # Burn the neutral in with hf, if requested <= # if not continuous_guess: psi4.set_global_option("GUESS", old_guess) if hf_guess: psi4.set_global_option("FRAC_START", 0) psi4.set_global_option("REFERENCE", "UHF") energy('scf', molecule=molecule, **kwargs) psi4.set_global_option("REFERENCE", "UKS") psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_LOAD", False) psi4.set_global_option("FRAC_START", frac_start) psi4.set_global_option("FRAC_RENORMALIZE", True) for occ in HOMO_occs: psi4.set_global_option("FRAC_OCC", [H**O]) psi4.set_global_option("FRAC_VAL", [occ]) E, wfn = energy('scf', return_wfn=True, molecule=molecule, **kwargs) C = 1 if E == 0.0: E = psi4.get_variable('SCF ITERATION ENERGY') C = 0 if LUMO > 0: eps = wfn.epsilon_a() potentials.append(eps[int(H**O) - 1]) else: eps = wfn.epsilon_b() potentials.append(eps[-int(H**O) - 1]) occs.append(occ - 1.0) energies.append(E) convs.append(C) psi4.set_global_option("FRAC_START", 2) psi4.set_global_option("FRAC_LOAD", True) psi4.set_global_option("GUESS", "READ") psi4.set_global_option("FRAC_DIIS", frac_diis) psi4.set_global_option("DF_INTS_IO", "LOAD") psi4.set_global_option("DF_INTS_IO", old_df_ints_io) # => Print the results out <= # E = {} psi4.print_out("""\n ==> Fractional Occupation Traverse Results <==\n\n""") psi4.print_out("""\t%-11s %-24s %-24s %11s\n""" % ('N', 'Energy', 'H**O Energy', 'Converged')) for k in range(len(occs)): psi4.print_out("""\t%11.3E %24.16E %24.16E %11d\n""" % (occs[k], energies[k], potentials[k], convs[k])) E[occs[k]] = energies[k] psi4.print_out('\n\t"You trying to be a hero Watkins?"\n') psi4.print_out('\t"Just trying to kill some bugs sir!"\n') psi4.print_out('\t\t\t-Starship Troopers\n') # Drop the files out fh = open(traverse_filename, 'w') fh.write("""\t%-11s %-24s %-24s %11s\n""" % ('N', 'Energy', 'H**O Energy', 'Converged')) for k in range(len(occs)): fh.write("""\t%11.3E %24.16E %24.16E %11d\n""" % (occs[k], energies[k], potentials[k], convs[k])) fh.close() # Properly, should clone molecule but since not returned and easy to unblemish, molecule.set_molecular_charge(charge0) molecule.set_multiplicity(mult0) return E
X = -indx.einsum('ai', (w, "ia"), (Ep1, "ia")) U = la.expm(X - X.T) spinorb.rotate_orbitals(U) h = spinorb.build_mo_H() g = spinorb.build_mo_antisymmetrized_G() f = spinorb.build_mo_F() Ep2 = spinorb.build_Ep2() np.fill_diagonal(f, 0.0) t = Ep2 * indx.meinsums('ijab', [1., I, (g, "ijab")], [1., P("ab"), (f, "ac"), (t, "ijcb")], [-1., P("ij"), (f, "ki"), (t, "kjab")]) E = indx.meinsums('', [1., I, (h, "pq"), (G1, "pq")], [1. / 4, I, (g, "pqrs"), (G2, "pqrs")]) + Vnu dE = E - self.E self.E = E psi4.print_out('\n@OMP2{:-3d}{:20.15f}{:20.15f}'.format(i, E, dE)) if (abs(dE) < e_conv): break return self.E # keyword values maxiter = psi4.get_global_option('MAXITER') e_conv = psi4.get_global_option('E_CONVERGENCE')
def run_gaussian_2(name, **kwargs): # throw an exception for open-shells if (psi4.get_option('SCF', 'REFERENCE') != 'RHF'): raise ValidationError("""g2 computations require "reference rhf".""") # stash user options: optstash = p4util.OptionsState(['FNOCC', 'COMPUTE_TRIPLES'], ['FNOCC', 'COMPUTE_MP4_TRIPLES'], ['FREEZE_CORE'], ['MP2_TYPE'], ['SCF', 'SCF_TYPE']) # override default scf_type psi4.set_local_option('SCF', 'SCF_TYPE', 'PK') # optimize geometry at scf level psi4.clean() psi4.set_global_option('BASIS', "6-31G(D)") driver.optimize('scf') psi4.clean() # scf frequencies for zpe # NOTE This line should not be needed, but without it there's a seg fault scf_e, ref = driver.frequency('scf', return_wfn=True) # thermodynamic properties du = psi4.get_variable('INTERNAL ENERGY CORRECTION') dh = psi4.get_variable('ENTHALPY CORRECTION') dg = psi4.get_variable('GIBBS FREE ENERGY CORRECTION') freqs = ref.frequencies() nfreq = freqs.dim(0) freqsum = 0.0 for i in range(0, nfreq): freqsum += freqs.get(i) zpe = freqsum / p4const.psi_hartree2wavenumbers * 0.8929 * 0.5 psi4.clean() # optimize geometry at mp2 (no frozen core) level # note: freeze_core isn't an option in MP2 psi4.set_global_option('FREEZE_CORE', "FALSE") psi4.set_global_option('MP2_TYPE', 'CONV') driver.optimize('mp2') psi4.clean() # qcisd(t) psi4.set_local_option('FNOCC', 'COMPUTE_MP4_TRIPLES', "TRUE") psi4.set_global_option('FREEZE_CORE', "TRUE") psi4.set_global_option('BASIS', "6-311G(D_P)") ref = driver.proc.run_fnocc('qcisd(t)', return_wfn=True, **kwargs) # HLC: high-level correction based on number of valence electrons nirrep = ref.nirrep() frzcpi = ref.frzcpi() nfzc = 0 for i in range(0, nirrep): nfzc += frzcpi[i] nalpha = ref.nalpha() - nfzc nbeta = ref.nbeta() - nfzc # hlc of gaussian-2 hlc = -0.00481 * nalpha - 0.00019 * nbeta # hlc of gaussian-1 hlc1 = -0.00614 * nalpha eqci_6311gdp = psi4.get_variable("QCISD(T) TOTAL ENERGY") emp4_6311gd = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311gd = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # correction for diffuse functions psi4.set_global_option('BASIS', "6-311+G(D_P)") driver.energy('mp4') emp4_6311pg_dp = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311pg_dp = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # correction for polarization functions psi4.set_global_option('BASIS', "6-311G(2DF_P)") driver.energy('mp4') emp4_6311g2dfp = psi4.get_variable("MP4 TOTAL ENERGY") emp2_6311g2dfp = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() # big basis mp2 psi4.set_global_option('BASIS', "6-311+G(3DF_2P)") #run_fnocc('_mp2',**kwargs) driver.energy('mp2') emp2_big = psi4.get_variable("MP2 TOTAL ENERGY") psi4.clean() eqci = eqci_6311gdp e_delta_g2 = emp2_big + emp2_6311gd - emp2_6311g2dfp - emp2_6311pg_dp e_plus = emp4_6311pg_dp - emp4_6311gd e_2df = emp4_6311g2dfp - emp4_6311gd eg2 = eqci + e_delta_g2 + e_plus + e_2df eg2_mp2_0k = eqci + (emp2_big - emp2_6311gd) + hlc + zpe psi4.print_out('\n') psi4.print_out(' ==> G1/G2 Energy Components <==\n') psi4.print_out('\n') psi4.print_out(' QCISD(T): %20.12lf\n' % eqci) psi4.print_out(' E(Delta): %20.12lf\n' % e_delta_g2) psi4.print_out(' E(2DF): %20.12lf\n' % e_2df) psi4.print_out(' E(+): %20.12lf\n' % e_plus) psi4.print_out(' E(G1 HLC): %20.12lf\n' % hlc1) psi4.print_out(' E(G2 HLC): %20.12lf\n' % hlc) psi4.print_out(' E(ZPE): %20.12lf\n' % zpe) psi4.print_out('\n') psi4.print_out(' ==> 0 Kelvin Results <==\n') psi4.print_out('\n') eg2_0k = eg2 + zpe + hlc psi4.print_out(' G1: %20.12lf\n' % (eqci + e_plus + e_2df + hlc1 + zpe)) psi4.print_out(' G2(MP2): %20.12lf\n' % eg2_mp2_0k) psi4.print_out(' G2: %20.12lf\n' % eg2_0k) psi4.set_variable("G1 TOTAL ENERGY", eqci + e_plus + e_2df + hlc1 + zpe) psi4.set_variable("G2 TOTAL ENERGY", eg2_0k) psi4.set_variable("G2(MP2) TOTAL ENERGY", eg2_mp2_0k) psi4.print_out('\n') T = psi4.get_global_option('T') psi4.print_out(' ==> %3.0lf Kelvin Results <==\n' % T) psi4.print_out('\n') internal_energy = eg2_mp2_0k + du - zpe / 0.8929 enthalpy = eg2_mp2_0k + dh - zpe / 0.8929 gibbs = eg2_mp2_0k + dg - zpe / 0.8929 psi4.print_out(' G2(MP2) energy: %20.12lf\n' % internal_energy) psi4.print_out(' G2(MP2) enthalpy: %20.12lf\n' % enthalpy) psi4.print_out(' G2(MP2) free energy: %20.12lf\n' % gibbs) psi4.print_out('\n') psi4.set_variable("G2(MP2) INTERNAL ENERGY", internal_energy) psi4.set_variable("G2(MP2) ENTHALPY", enthalpy) psi4.set_variable("G2(MP2) FREE ENERGY", gibbs) internal_energy = eg2_0k + du - zpe / 0.8929 enthalpy = eg2_0k + dh - zpe / 0.8929 gibbs = eg2_0k + dg - zpe / 0.8929 psi4.print_out(' G2 energy: %20.12lf\n' % internal_energy) psi4.print_out(' G2 enthalpy: %20.12lf\n' % enthalpy) psi4.print_out(' G2 free energy: %20.12lf\n' % gibbs) psi4.set_variable("CURRENT ENERGY", eg2_0k) psi4.set_variable("G2 INTERNAL ENERGY", internal_energy) psi4.set_variable("G2 ENTHALPY", enthalpy) psi4.set_variable("G2 FREE ENERGY", gibbs) psi4.clean() optstash.restore() # return 0K g2 results return eg2_0k
def get_conv(): # get convergence return psi4.get_global_option('E_CONVERGENCE')
[ 1./4, P("ij") , (g,"klcd"), (t1,"ic" ), (t1,"jd" ), (t2,"klab") ], [ 1./4, P("ab") , (g,"klcd"), (t1,"ka" ), (t1,"lb" ), (t2,"ijcd") ], [ 1. , P("ij|ab"), (g,"klcd"), (t1,"ic" ), (t1,"lb" ), (t2,"kjad") ], [ 1./4, P("ij|ab"), (g,"klcd"), (t1,"ic" ), (t1,"ka" ), (t1,"jd" ), (t1,"lb" )]) t1 = t1 + Ep1 * R1 t2 = t2 + Ep2 * R2 if do_diis: diis.add_vec((t1, R1), (t2, R2)) E = indx.meinsums('', [1./4, I, (g,"ijab"), (t2,"ijab") ], [1./2, I, (g,"ijab"), (t1,"ia" ), (t1,"jb" )]) dE = E - self.E self.E = E psi4.print_out('\n@CCSD{:-3d}{:20.15f}{:20.15f}'.format(i, E, dE)) if(abs(dE) < econv): break self.t1 = t1 self.t2 = t2 return self.E # keyword values maxiter = psi4.get_global_option('MAXITER') econv = psi4.get_global_option('E_CONVERGENCE') do_diis = psi4.get_global_option('DIIS') diis_start = psi4.get_global_option('DIIS_START')
def writeCSX(name, **kwargs): """function to write the CSX file """ if not psi4.get_global_option('WRITE_CSX'): return # import csx_api for csx writing import os import math import inspect #import openbabel import qcdb import qcdb.periodictable import csx2_api as api lowername = name.lower() # Make sure the molecule the user provided is the active one if ('molecule' in kwargs): activate(kwargs['molecule']) del kwargs['molecule'] molecule = psi4.get_active_molecule() molecule.update_geometry() # Determine the derivative type calledby = inspect.stack()[1][3] derdict = { 'energy': 0, 'property': 0, 'gradient': 1, 'optimize': 1, 'frequency': 2, 'frequencies': 2, 'hessian': 2, } dertype = derdict[calledby] hasFreq = False # Start to write the CSX file # First grab molecular information and energies from psi4 geom = molecule.save_string_xyz() # OB atomLine = geom.split('\n') # OB # general molecular information atomNum = molecule.natom() molSym = molecule.schoenflies_symbol() molCharge = molecule.molecular_charge() molMulti = molecule.multiplicity() # energy information molBasis = psi4.get_global_option('BASIS') molSpin = psi4.get_global_option('REFERENCE') molMethod = psi4.get_global_option('WFN') mol1E = psi4.get_variable('ONE-ELECTRON ENERGY') mol2E = psi4.get_variable('TWO-ELECTRON ENERGY') molNE = psi4.get_variable('NUCLEAR REPULSION ENERGY') molPE = mol1E + mol2E molEE = psi4.get_variable('CURRENT ENERGY') # wavefunction information try: wfn = kwargs['wfn'] except AttributeError: pass if wfn: molOrbE = wfn.epsilon_a() molOrbEb = wfn.epsilon_b() orbNmopi = wfn.nmopi() orbNsopi = wfn.nsopi() orbNum = wfn.nmo() if molOrbE else 0 orbSNum = wfn.nso() molOrb = wfn.Ca() orbNirrep = wfn.nirrep() orbAotoso = wfn.aotoso() orbDoccpi = wfn.doccpi() orbSoccpi = wfn.soccpi() basisNbf = wfn.basisset().nbf() basisDim = psi4.Dimension(1, 'basisDim') basisDim.__setitem__(0, basisNbf) wfnRestricted = True orbE = [] hlist = [] orblist = [] orbOcc = [] molOrbmo = psi4.Matrix('molOrbmo', basisDim, orbNmopi) molOrbmo.gemm(False, False, 1.0, orbAotoso, molOrb, 0.0) if molSpin == 'UHF': wfnRestricted = False orbEb = [] hlistCb = [] orblistCb = [] orbOccCb = [] molOrbCb = wfn.Cb() molOrbmoCb = psi4.Matrix('molOrbmoCb', basisDim, orbNmopi) molOrbmoCb.gemm(False, False, 1.0, orbAotoso, molOrbCb, 0.0) count = 0 eleExtra = 1 if wfnRestricted else 0 for ih in range(orbNirrep): for iorb in range(orbNmopi.__getitem__(ih)): hlist.append(ih) orblist.append(iorb) if molOrbE: orbE.append(molOrbE.get(count)) eleNum = 1 if iorb < (orbDoccpi.__getitem__(ih) + orbSoccpi.__getitem__(ih)) else 0 eleNum += eleExtra if iorb < orbDoccpi.__getitem__(ih) else 0 orbOcc.append(eleNum) count += 1 orbMos = sorted(zip(orbE, zip(hlist, orblist))) orbOccString = ' '.join(str(x) for x in sorted(orbOcc, reverse=True)) orbCaString = [] for imos in range(orbNum): (h, s) = orbMos[imos][1] orbCa = [] for iso in range(orbSNum): orbEle = molOrbmo.get(h, iso, s) orbCa.append(orbEle) orbCaString.append(' '.join(str(x) for x in orbCa)) orbEString = ' '.join(str(x) for x in sorted(orbE)) # now for beta spin if not wfnRestricted: count = 0 for ih in range(orbNirrep): for iorb in range(orbNmopi.__getitem__(ih)): hlistCb.append(ih) orblist.append(iorb) if molOrbEb: orbEb.append(molOrbEb.get(count)) eleNum = 1 if iorb < (orbDoccpi.__getitem__(ih) + orbSoccpi.__getitem__(ih)) else 0 if iorb < orbDoccpi.__getitem__(ih): eleNum += eleExtra orbOccCb.append(eleNum) count += 1 orbMosCb = sorted(zip(orbEb, zip(hlist, orblist))) orbOccCbString = ' '.join(str(x) for x in sorted(orbOccCb, reverse=True)) orbCbString = [] for imos in range(orbNum): (h, s) = orbMosCb[imos][1] orbCb = [] for iso in range(orbSNum): orbEle = molOrbmoCb.get(h, iso, s) orbCb.append(orbEle) orbCbString.append(' '.join(str(x) for x in orbCb)) orbEbString = ' '.join(str(x) for x in sorted(orbEb)) # orbColString = ' '.join(str(x) for x in orbCol) if wfnRestricted: wfn1 = api.waveFunctionType( orbitalCount=orbNum, orbitalOccupancies=orbOccString) orbe1 = api.stringArrayType(unit='gc:hartree') orbe1.set_valueOf_(orbEString) orbs1 = api.orbitalsType() for iorb in range(orbNum): orb1 = api.stringArrayType(id=iorb+1) orb1.set_valueOf_(orbCaString[iorb]) orbs1.add_orbital(orb1) wfn1.set_orbitals(orbs1) wfn1.set_orbitalEnergies(orbe1) else: wfn1 = api.waveFunctionType(orbitalCount=orbNum) # alpha electron: 1.5 orbe1 = api.stringArrayType(unit='gc:hartree') orbe1.set_valueOf_(orbEString) wfn1.set_alphaOrbitalEnergies(orbe1) wfn1.set_alphaOrbitalOccupancies(orbOccString) aorbs1 = api.orbitalsType() for iorb in range(orbNum): orb1 = api.stringArrayType(id=iorb+1) orb1.set_valueOf_(orbCaString[iorb]) aorbs1.add_orbital(orb1) wfn1.set_alphaOrbitals(aorbs1) # beta electron: 1.5 orbeb1 = api.stringArrayType(unit='gc:hartree') orbeb1.set_valueOf_(orbEbString) wfn1.set_betaOrbitalEnergies(orbeb1) wfn1.set_betaOrbitalOccupancies(orbOccCbString) borbs1 = api.orbitalsType() for iorb in range(orbNum): orb1 = api.stringArrayType(id=iorb+1) orb1.set_valueOf_(orbCbString[iorb]) borbs1.add_orbital(orb1) wfn1.set_betaOrbitals(borbs1) # frequency information if dertype == 2: hasFreq = True molFreq = psi4.get_frequencies() molFreqNum = molFreq.dim(0) frq = [] irInt = [] for ifrq in range(molFreqNum): frq.append(molFreq.get(ifrq)) irInt.append(0.0) frqString = ' '.join(str(x) for x in frq) intString = ' '.join(str(x) for x in irInt) normMod = psi4.get_normalmodes() normMdString = [] count = 0 for ifrq in range(molFreqNum): normM = [] for iatm in range(atomNum): for ixyz in range(3): normM.append(normMod.get(count)) count += 1 normMdString.append(' '.join(str(x) for x in normM)) vib1 = api.vibAnalysisType(vibrationCount=molFreqNum) freq1 = api.stringArrayType(unit="gc:cm-1") freq1.set_valueOf_(frqString) vib1.set_frequencies(freq1) irint1 = api.stringArrayType() irint1.set_valueOf_(intString) vib1.set_irIntensities(irint1) norms1 = api.normalModesType() for ifrq in range(molFreqNum): norm1 = api.normalModeType(id=ifrq+1) norm1.set_valueOf_(normMdString[ifrq]) norms1.add_normalMode(norm1) vib1.set_normalModes(norms1) # dipole moment information molDipoleX = psi4.get_variable('CURRENT DIPOLE X') molDipoleY = psi4.get_variable('CURRENT DIPOLE Y') molDipoleZ = psi4.get_variable('CURRENT DIPOLE Z') molDipoleTot = math.sqrt( molDipoleX * molDipoleX + molDipoleY * molDipoleY + molDipoleZ * molDipoleZ) prop1 = api.propertiesType() sprop1 = api.propertyType( name='dipoleMomentX', unit='gc:debye') sprop1.set_valueOf_(molDipoleX) sprop2 = api.propertyType( name='dipoleMomentY', unit='gc:debye') sprop2.set_valueOf_(molDipoleY) sprop3 = api.propertyType( name='dipoleMomentZ', unit='gc:debye') sprop3.set_valueOf_(molDipoleZ) sprop4 = api.propertyType( name='dipoleMomentAverage', unit='gc:debye') sprop4.set_valueOf_(molDipoleTot) prop1.add_systemProperty(sprop1) prop1.add_systemProperty(sprop2) prop1.add_systemProperty(sprop3) prop1.add_systemProperty(sprop4) # get the basename for the CSX file psio = psi4.IO.shared_object() namespace = psio.get_default_namespace() #csxfilename = '.'.join([namespace, str(os.getpid()), 'csx']) csxfilename = os.path.splitext(psi4.outfile_name())[0] + '.csx' csxfile = open(csxfilename, 'w') csxVer = psi4.get_global_option('CSX_VERSION') # Both CSX versions 0 and 1 depended on the procedures table, which in # turn required the writeCSX function to be in the driver.py file # itself. Starting with 1.5 (1, to run), this dependence is broken and # CSX has been shifted into a plugin. # Start to generate CSX elements # CSX version 1.5 if csxVer == 2.0: # import csx1_api as api cs1 = api.csType(version='2.0') #5') # molPublication section: 1.5 mp1 = api.mpubType( title=psi4.get_global_option('PUBLICATIONTITLE'), abstract=psi4.get_global_option('PUBLICATIONABSTRACT'), publisher=psi4.get_global_option('PUBLICATIONPUBLISHER'), status=['PRELIMINARY', 'DRAFT', 'FINAL'].index(psi4.get_global_option('PUBLICATIONSTATUS')), category=psi4.get_global_option('PUBLICATIONCATEGORY'), visibility=['PRIVATE', 'PROTECTED', 'PUBLIC'].index(psi4.get_global_option('PUBLICATIONVISIBILITY')), tags=psi4.get_global_option('PUBLICATIONTAGS'), key=psi4.get_global_option('PUBLICATIONKEY')) email = psi4.get_global_option('EMAIL').replace('__', '@') mp1.add_author(api.authorType( creator=psi4.get_global_option('CORRESPONDINGAUTHOR'), type_='gc:CorrespondingAuthor', organization=psi4.get_global_option('ORGANIZATION'), email=None if email == '' else email)) #mp1 = api.mpType( # title='', abstract='', publisher='', status=0, category=2, visibility=0, tags='', key='') mp1.set_sourcePackage(api.sourcePackageType(name='Psi4', version=psi4.version())) #mp1.add_author(api.authorType(creator='', type_='cs:corresponding', organization='', email='')) cs1.set_molecularPublication(mp1) # molSystem section: 1.5 ms1 = api.msysType( systemCharge=molCharge, systemMultiplicity=molMulti, id='s1') temp1 = api.dataWithUnitsType(unit='gc:kelvin') temp1.set_valueOf_(0.0) # LAB dispute ms1.set_systemTemperature(temp1) mol1 = api.moleculeType(id='m1', atomCount=molecule.natom()) #OBmol1 = api.moleculeType(id='m1', atomCount=atomNum) #OBobmol1 = openbabel.OBMol() #OBfor iatm in range(atomNum): #OB atomField = atomLine[iatm + 1].split() #OB atmSymbol = atomField[0] #OB xCoord = float(atomField[1]) #OB yCoord = float(atomField[2]) #OB zCoord = float(atomField[3]) #OB obatm = obmol1.NewAtom() #OB obatm.SetAtomicNum(qcdb.periodictable.el2z[atmSymbol.upper()]) #OB obatm.SetVector(xCoord, yCoord, zCoord) #OBobmol1.ConnectTheDots() #OBobmol1.PerceiveBondOrders() #OBobmol1.SetTotalSpinMultiplicity(molMulti) #OBobmol1.SetTotalCharge(molCharge) #OBconv1 = openbabel.OBConversion() #OBconv1.SetInAndOutFormats('mol', 'inchi') #OBconv1.SetOptions('K', conv1.OUTOPTIONS) #OBinchikey = conv1.WriteString(obmol1) #OBmol1.set_inchiKey(inchikey.rstrip()) #OBiatm = 0 for at in range(molecule.natom()): #xCoord1 = api.dataWithUnitsType(unit='cs:angstrom') #yCoord1 = api.dataWithUnitsType(unit='cs:angstrom') #zCoord1 = api.dataWithUnitsType(unit='cs:angstrom') #xCoord1.set_valueOf_(molecule.x(at) * p4const.psi_bohr2angstroms) #yCoord1.set_valueOf_(molecule.y(at) * p4const.psi_bohr2angstroms) #zCoord1.set_valueOf_(molecule.z(at) * p4const.psi_bohr2angstroms) xCoord1 = api.dataWithUnitsType(unit='gc:bohr') yCoord1 = api.dataWithUnitsType(unit='gc:bohr') zCoord1 = api.dataWithUnitsType(unit='gc:bohr') xCoord1.set_valueOf_(molecule.x(at)) yCoord1.set_valueOf_(molecule.y(at)) zCoord1.set_valueOf_(molecule.z(at)) # LAB 8jun2015: not getting masses from OB anymore so now dependent on qc programs # current proposition is changing API so masses only go into CSX if relevant (e.g., vib) # same situation as temperature atm = api.atomType( id='a' + str(at + 1), elementSymbol=molecule.symbol(at), atomMass=molecule.mass(at), # psi4 uses mass of most common isotope; OB uses natural distribution mass xCoord3D=xCoord1, yCoord3D=yCoord1, zCoord3D=zCoord1, basisSet='bse:' + molBasis, calculatedAtomCharge=0, formalAtomCharge=0) #OBiatm += 1 #OBcoord1 = api.coordinationType() #OBibond = 0 #OBfor nb_atom in openbabel.OBAtomAtomIter(obatom): #OB bond = obatom.GetBond(nb_atom) #OB bond1 = api.bondType( #OB id1='a' + str(obatom.GetId() + 1), #OB id2='a' + str(nb_atom.GetId() + 1)) #OB if bond.GetBondOrder() == 1: #OB bond1.set_valueOf_('single') #OB elif bond.GetBondOrder() == 2: #OB bond1.set_valueOf_('double') #OB elif bond.GetBondOrder() == 3: #OB bond1.set_valueOf_('triple') #OB elif bond.GetBondOrder() == 5: #OB bond1.set_valueOf_('aromatic') #OB else: #OB print('wrong bond order') #OB coord1.add_bond(bond1) #OB ibond += 1 #OBcoord1.set_bondCount(ibond) #OBatm.set_coordination(coord1) mol1.add_atom(atm) ms1.add_molecule(mol1) cs1.set_molecularSystem(ms1) # molCalculation section: 1.5 mc1 = api.mcalType(id='c1') qm1 = api.qmCalcType() srs1 = api.srsMethodType() psivars = psi4.get_variables() def form_ene(mandatoryPsivars, optionalPsivars={}, excessPsivars={}): """ """ ene = api.energiesType(unit='gc:hartree') for pv, csx in mandatoryPsivars.iteritems(): term = api.energyType(type_=csx) term.set_valueOf_(psivars.pop(pv)) ene.add_energy(term) for pv, csx in optionalPsivars.iteritems(): if pv in psivars: term = api.energyType(type_=csx) term.set_valueOf_(psivars.pop(pv)) ene.add_energy(term) for pv in excessPsivars: if pv in psivars: psivars.pop(pv) return ene # Reference stage- every calc has one if 'CCSD TOTAL ENERGY' in psivars or 'CCSD(T) TOTAL ENERGY' in psivars \ or 'CISD TOTAL ENERGY' in psivars or 'FCI TOTAL ENERGY' in psivars \ or 'QCISD TOTAL ENERGY' in psivars or 'QCISD(T) TOTAL ENERGY' in psivars: mdm1 = api.srsmdMethodType() # CCSD(T): 1.5 if 'CCSD(T) TOTAL ENERGY' in psivars: mandatoryPsivars = { 'CCSD(T) CORRELATION ENERGY': 'gc:correlation', 'CCSD(T) TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed CCSD(T) computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if hasFreq: block.set_vibrationalAnalysis(vib1) mdm1.set_ccsd_t(block) # CCSD: 1.5 elif 'CCSD TOTAL ENERGY' in psivars: mandatoryPsivars = { 'CCSD CORRELATION ENERGY': 'gc:correlation', 'CCSD TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed CCSD computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if hasFreq: block.set_vibrationalAnalysis(vib1) mdm1.set_ccsd(block) # CISD: 1.5 elif 'CISD TOTAL ENERGY' in psivars: mandatoryPsivars = { 'CISD CORRELATION ENERGY': 'gc:correlation', 'CISD TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed CISD computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if hasFreq: block.set_vibrationalAnalysis(vib1) mdm1.set_cisd(block) # FCI: 1.5 elif 'FCI TOTAL ENERGY' in psivars: mandatoryPsivars = { 'FCI CORRELATION ENERGY': 'gc:correlation', 'FCI TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed FCI computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) mdm1.set_fci(block) # QCISD(T): 1.5 elif 'QCISD(T) TOTAL ENERGY' in psivars: mandatoryPsivars = { 'QCISD(T) CORRELATION ENERGY': 'gc:correlation', 'QCISD(T) TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed QCISD(T) computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if hasFreq: block.set_vibrationalAnalysis(vib1) mdm1.set_qcisd_t(block) # QCISD: 1.5 elif 'QCISD TOTAL ENERGY' in psivars: mandatoryPsivars = { 'QCISD CORRELATION ENERGY': 'gc:correlation', 'QCISD TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed QCISD computation""") block = api.resultType( # TODO should be pointing to HF for correlation, maybe to MP2 for guess methodology='gc:normal', # TODO handle dfcc spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if hasFreq: block.set_vibrationalAnalysis(vib1) mdm1.set_qcisd(block) srs1.set_multipleDeterminant(mdm1) elif 'DFT TOTAL ENERGY' in psivars or 'HF TOTAL ENERGY' in psivars \ or 'MP2 TOTAL ENERGY' in psivars or 'MP3 TOTAL ENERGY' in psivars \ or 'MP4 TOTAL ENERGY' in psivars: sdm1 = api.srssdMethodType() # DFT 1.5 if 'DFT TOTAL ENERGY' in psivars: # TODO robust enough to avoid MP2C, etc.? mandatoryPsivars = { 'NUCLEAR REPULSION ENERGY': 'gc:nuclearRepulsion', 'DFT FUNCTIONAL TOTAL ENERGY': 'gc:dftFunctional', 'DFT TOTAL ENERGY': 'gc:electronic'} optionalPsivars = { 'DOUBLE-HYBRID CORRECTION ENERGY': 'gc:doubleHybrid correction', 'DISPERSION CORRECTION ENERGY': 'gc:dispersion correction'} excessPsivars = [ 'MP2 TOTAL ENERGY', 'MP2 CORRELATION ENERGY', 'MP2 SAME-SPIN CORRELATION ENERGY'] if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed DFT computation""") block = api.resultType( methodology='gc:normal', # TODO handle dfhf, dfmp spinType='gc:' + molSpin, basisSet='bse:' + molBasis, dftFunctional=name) # TODO this'll need to be exported block.set_energies(form_ene(mandatoryPsivars, optionalPsivars, excessPsivars)) if wfn: block.set_waveFunction(wfn1) if hasFreq: block.set_vibrationalAnalysis(vib1) block.set_properties(prop1) sdm1.set_dft(block) # post-reference block # MP4: 1.5 elif 'MP4 TOTAL ENERGY' in psivars: mandatoryPsivars = { 'MP4 CORRELATION ENERGY': 'gc:correlation', 'MP4 TOTAL ENERGY': 'gc:electronic'} optionalPsivars = { 'MP4 SAME-SPIN CORRELATION ENERGY': 'gc:sameSpin correlation'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed MP4 computation""") block = api.resultType( # TODO should be pointing to HF for correlation methodology='gc:normal', # TODO handle dfmp spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars, optionalPsivars)) if wfn: block.set_waveFunction(wfn1) if hasFreq: block.set_vibrationalAnalysis(vib1) block.set_properties(prop1) sdm1.set_mp4(block) # MP3: 1.5 elif 'MP3 TOTAL ENERGY' in psivars: mandatoryPsivars = { 'MP3 CORRELATION ENERGY': 'gc:correlation', 'MP3 TOTAL ENERGY': 'gc:electronic'} optionalPsivars = { 'MP3 SAME-SPIN CORRELATION ENERGY': 'gc:sameSpin correlation'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed MP3 computation""") block = api.resultType( # TODO should be pointing to HF for correlation methodology='gc:normal', # TODO handle dfmp spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars, optionalPsivars)) if wfn: block.set_waveFunction(wfn1) if hasFreq: block.set_vibrationalAnalysis(vib1) block.set_properties(prop1) sdm1.set_mp3(block) # MP2: 1.5 elif 'MP2 TOTAL ENERGY' in psivars: mandatoryPsivars = { 'MP2 CORRELATION ENERGY': 'gc:correlation', 'MP2 TOTAL ENERGY': 'gc:electronic'} optionalPsivars = { 'MP2 SAME-SPIN CORRELATION ENERGY': 'gc:sameSpin correlation'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed MP2 computation""") block = api.resultType( # TODO should be pointing to HF for correlation methodology='gc:normal', # TODO handle dfmp spinType='gc:' + molSpin, # TODO could have a closed-shell corl mtd atop open-shell scf? basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars, optionalPsivars)) if wfn: block.set_waveFunction(wfn1) if hasFreq: block.set_vibrationalAnalysis(vib1) block.set_properties(prop1) sdm1.set_mp2(block) # SCF: 1.5 elif 'HF TOTAL ENERGY' in psivars: mandatoryPsivars = { 'NUCLEAR REPULSION ENERGY': 'gc:nuclearRepulsion', 'HF TOTAL ENERGY': 'gc:electronic'} if not all([pv in psivars for pv in mandatoryPsivars.keys()]): raise CSXError("""Malformed HF computation""") block = api.resultType( methodology='gc:normal', # TODO handle dfhf, dfmp spinType='gc:' + molSpin, basisSet='bse:' + molBasis) block.set_energies(form_ene(mandatoryPsivars)) if wfn: block.set_waveFunction(wfn1) if hasFreq: block.set_vibrationalAnalysis(vib1) block.set_properties(prop1) sdm1.set_abinitioScf(block) else: psi4.print_out("""\nCSX version {0} does not support """ """method {1} for {2}\n""".format( csxVer, lowername, 'energies')) srs1.set_singleDeterminant(sdm1) #print('CSX not harvesting: ', ', '.join(psivars)) qm1.set_singleReferenceState(srs1) mc1.set_quantumMechanics(qm1) cs1.set_molecularCalculation(mc1) else: print('The future CSX file is here') csxfile.write('<?xml version="1.0" encoding="UTF-8"?>\n') cs1.export(csxfile, 0) csxfile.close()
def ip_fitting(mol, omega_l, omega_r, **kwargs): kwargs = p4util.kwargs_lower(kwargs) # By default, zero the omega to 3 digits omega_tol = 1.0E-3; if (kwargs.has_key('omega_tolerance')): omega_tol = kwargs['omega_tolerance'] # By default, do up to twenty iterations maxiter = 20; if (kwargs.has_key('maxiter')): maxiter = kwargs['maxiter'] # By default, do not read previous 180 orbitals file read = False; read180 = '' if (kwargs.has_key('read')): read = True; read180 = kwargs['read'] # The molecule is required, and should be the neutral species mol.update_geometry() activate(mol) charge0 = mol.molecular_charge() mult0 = mol.multiplicity() # How many electrons are there? N = 0; for A in range(mol.natom()): N += mol.Z(A) N -= charge0 N = int(N) Nb = int((N - mult0 + 1)/2) Na = int(N - Nb) # Work in the ot namespace for this procedure psi4.IO.set_default_namespace("ot") # Burn in to determine orbital eigenvalues if (read): psi4.set_global_option("GUESS", "READ") copy_file_to_scratch(read180, 'psi', 'ot', 180) old_guess = psi4.get_global_option("GUESS") psi4.set_global_option("DF_INTS_IO", "SAVE") psi4.print_out('\n\t==> IP Fitting SCF: Burn-in <==\n') energy('scf') psi4.set_global_option("DF_INTS_IO", "LOAD") # Determine H**O, to determine mult1 ref = psi4.wavefunction() eps_a = ref.epsilon_a() eps_b = ref.epsilon_b() if (Na == Nb): H**O = -Nb elif (Nb == 0): H**O = Na else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if (E_a >= E_b): H**O = Na else: H**O = -Nb Na1 = Na; Nb1 = Nb; if (H**O > 0): Na1 = Na1-1; else: Nb1 = Nb1-1; charge1 = charge0 + 1; mult1 = Na1 - Nb1 + 1 omegas = []; E0s = []; E1s = []; kIPs = []; IPs = []; types = []; # Right endpoint psi4.set_global_option('DFT_OMEGA',omega_r) # Neutral if (read): psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) mol.set_molecular_charge(charge0) mol.set_multiplicity(mult0) psi4.print_out('\n\t==> IP Fitting SCF: Neutral, Right Endpoint <==\n') E0r = energy('scf') ref = psi4.wavefunction() eps_a = ref.epsilon_a() eps_b = ref.epsilon_b() E_HOMO = 0.0; if (Nb == 0): E_HOMO = eps_a[int(Na-1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if (E_a >= E_b): E_HOMO = E_a; else: E_HOMO = E_b; E_HOMOr = E_HOMO; psi4.IO.change_file_namespace(180,"ot","neutral") # Cation if (read): psi4.set_global_option("GUESS", "READ") p4util.copy_file_to_scratch(read180, 'psi', 'ot', 180) mol.set_molecular_charge(charge1) mol.set_multiplicity(mult1) psi4.print_out('\n\t==> IP Fitting SCF: Cation, Right Endpoint <==\n') E1r = energy('scf') psi4.IO.change_file_namespace(180,"ot","cation") IPr = E1r - E0r; kIPr = -E_HOMOr; delta_r = IPr - kIPr; if (IPr > kIPr): psi4.print_out('\n***IP Fitting Error: Right Omega limit should have kIP > IP') sys.exit(1) omegas.append(omega_r) types.append('Right Limit') E0s.append(E0r) E1s.append(E1r) IPs.append(IPr) kIPs.append(kIPr) # Use previous orbitals from here out psi4.set_global_option("GUESS","READ") # Left endpoint psi4.set_global_option('DFT_OMEGA',omega_l) # Neutral psi4.IO.change_file_namespace(180,"neutral","ot") mol.set_molecular_charge(charge0) mol.set_multiplicity(mult0) psi4.print_out('\n\t==> IP Fitting SCF: Neutral, Left Endpoint <==\n') E0l = energy('scf') ref = psi4.wavefunction() eps_a = ref.epsilon_a() eps_b = ref.epsilon_b() E_HOMO = 0.0; if (Nb == 0): E_HOMO = eps_a[int(Na-1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if (E_a >= E_b): E_HOMO = E_a; else: E_HOMO = E_b; E_HOMOl = E_HOMO; psi4.IO.change_file_namespace(180,"ot","neutral") # Cation psi4.IO.change_file_namespace(180,"cation","ot") mol.set_molecular_charge(charge1) mol.set_multiplicity(mult1) psi4.print_out('\n\t==> IP Fitting SCF: Cation, Left Endpoint <==\n') E1l = energy('scf') psi4.IO.change_file_namespace(180,"ot","cation") IPl = E1l - E0l; kIPl = -E_HOMOl; delta_l = IPl - kIPl; if (IPl < kIPl): psi4.print_out('\n***IP Fitting Error: Left Omega limit should have kIP < IP') sys.exit(1) omegas.append(omega_l) types.append('Left Limit') E0s.append(E0l) E1s.append(E1l) IPs.append(IPl) kIPs.append(kIPl) converged = False repeat_l = 0; repeat_r = 0; step = 0; while True: step = step + 1; # Regula Falsi (modified) if (repeat_l > 1): delta_l = delta_l / 2.0; if (repeat_r > 1): delta_r = delta_r / 2.0; omega = - (omega_r - omega_l) / (delta_r - delta_l) * delta_l + omega_l; psi4.set_global_option('DFT_OMEGA',omega) # Neutral psi4.IO.change_file_namespace(180,"neutral","ot") mol.set_molecular_charge(charge0) mol.set_multiplicity(mult0) psi4.print_out('\n\t==> IP Fitting SCF: Neutral, Omega = %11.3E <==\n' % omega) E0 = energy('scf') ref = psi4.wavefunction() eps_a = ref.epsilon_a() eps_b = ref.epsilon_b() E_HOMO = 0.0; if (Nb == 0): E_HOMO = eps_a[int(Na-1)] else: E_a = eps_a[int(Na - 1)] E_b = eps_b[int(Nb - 1)] if (E_a >= E_b): E_HOMO = E_a; else: E_HOMO = E_b; psi4.IO.change_file_namespace(180,"ot","neutral") # Cation psi4.IO.change_file_namespace(180,"cation","ot") mol.set_molecular_charge(charge1) mol.set_multiplicity(mult1) psi4.print_out('\n\t==> IP Fitting SCF: Cation, Omega = %11.3E <==\n' % omega) E1 = energy('scf') psi4.IO.change_file_namespace(180,"ot","cation") IP = E1 - E0; kIP = -E_HOMO; delta = IP - kIP; if (kIP > IP): omega_r = omega E0r = E0 E1r = E1 IPr = IP kIPr = kIP delta_r = delta repeat_r = 0; repeat_l = repeat_l + 1; else: omega_l = omega E0l = E0 E1l = E1 IPl = IP kIPl = kIP delta_l = delta repeat_l = 0; repeat_r = repeat_r + 1; omegas.append(omega) types.append('Regula-Falsi') E0s.append(E0) E1s.append(E1) IPs.append(IP) kIPs.append(kIP) # Termination if (abs(omega_l - omega_r) < omega_tol or step > maxiter): converged = True; break psi4.IO.set_default_namespace("") psi4.print_out('\n\t==> IP Fitting Results <==\n\n') psi4.print_out('\t => Occupation Determination <= \n\n') psi4.print_out('\t %6s %6s %6s %6s %6s %6s\n' %('N', 'Na', 'Nb', 'Charge', 'Mult', 'H**O')) psi4.print_out('\t Neutral: %6d %6d %6d %6d %6d %6d\n' %(N, Na, Nb, charge0, mult0, H**O)) psi4.print_out('\t Cation: %6d %6d %6d %6d %6d\n\n' %(N-1, Na1, Nb1, charge1, mult1)) psi4.print_out('\t => Regula Falsi Iterations <=\n\n') psi4.print_out('\t%3s %11s %14s %14s %14s %s\n' % ('N','Omega','IP','kIP','Delta','Type')) for k in range(len(omegas)): psi4.print_out('\t%3d %11.3E %14.6E %14.6E %14.6E %s\n' % (k+1,omegas[k],IPs[k],kIPs[k],IPs[k] - kIPs[k], types[k])) if (converged): psi4.print_out('\n\tIP Fitting Converged\n') psi4.print_out('\tFinal omega = %14.6E\n' % ((omega_l + omega_r) / 2)) psi4.print_out('\n\t"M,I. does the dying. Fleet just does the flying."\n') psi4.print_out('\t\t\t-Starship Troopers\n') else: psi4.print_out('\n\tIP Fitting did not converge!\n') psi4.set_global_option("DF_INTS_IO", "NONE") psi4.set_global_option("GUESS", old_guess)
def option(keyword): """ Returns the requested option from the psi4 input file. """ return get_global_option(keyword.upper())
def _set_convergence_criterion(ptype, method_name, scf_Ec, pscf_Ec, scf_Dc, pscf_Dc, gen_Ec, verbose=1): r""" This function will set local SCF and global energy convergence criterion to the defaults listed at: http://www.psicode.org/psi4manual/master/scf.html#convergence-and- algorithm-defaults. SCF will be converged more tightly if a post-SCF method is select (pscf_Ec, and pscf_Dc) else the looser (scf_Ec, and scf_Dc convergence criterion will be used). ptype - Procedure type (energy, gradient, etc). Nearly always test on procedures['energy'] since that's guaranteed to exist for a method. method_name - Name of the method scf_Ec - E convergence criterion for scf target method pscf_Ec - E convergence criterion for scf of post-scf target method scf_Dc - D convergence criterion for scf target method pscf_Dc - D convergence criterion for scf of post-scf target method gen_Ec - E convergence criterion for post-scf target method """ optstash = p4util.OptionsState( ['SCF', 'E_CONVERGENCE'], ['SCF', 'D_CONVERGENCE'], ['E_CONVERGENCE']) # Kind of want to move this out of here _method_exists(ptype, method_name) if verbose >= 2: print(' Setting convergence', end=' ') # Set method-dependent scf convergence criteria, check against energy routines if not psi4.has_option_changed('SCF', 'E_CONVERGENCE'): if procedures['energy'][method_name] in [proc.run_scf, proc.run_dft]: psi4.set_local_option('SCF', 'E_CONVERGENCE', scf_Ec) if verbose >= 2: print(scf_Ec, end=' ') else: psi4.set_local_option('SCF', 'E_CONVERGENCE', pscf_Ec) if verbose >= 2: print(pscf_Ec, end=' ') else: if verbose >= 2: print('CUSTOM', psi4.get_option('SCF', 'E_CONVERGENCE'), end=' ') if not psi4.has_option_changed('SCF', 'D_CONVERGENCE'): if procedures['energy'][method_name] in [proc.run_scf, proc.run_dft]: psi4.set_local_option('SCF', 'D_CONVERGENCE', scf_Dc) if verbose >= 2: print(scf_Dc, end=' ') else: psi4.set_local_option('SCF', 'D_CONVERGENCE', pscf_Dc) if verbose >= 2: print(pscf_Dc, end=' ') else: if verbose >= 2: print('CUSTOM', psi4.get_option('SCF', 'D_CONVERGENCE'), end=' ') # Set post-scf convergence criteria (global will cover all correlated modules) if not psi4.has_global_option_changed('E_CONVERGENCE'): if procedures['energy'][method_name] not in [proc.run_scf, proc.run_dft]: psi4.set_global_option('E_CONVERGENCE', gen_Ec) if verbose >= 2: print(gen_Ec, end=' ') else: if procedures['energy'][method_name] not in [proc.run_scf, proc.run_dft]: if verbose >= 2: print('CUSTOM', psi4.get_global_option('E_CONVERGENCE'), end=' ') if verbose >= 2: print('') return optstash