def test_as_pyscf_method(self): mol = gto.M(atom=''' O 0. 0. 0. H 0. -0.757 0.587 H 0. 0.757 0.587 ''', verbose=0) mol1 = geometric_solver.optimize(scf.RHF(mol)) self.assertAlmostEqual(lib.finger(mol1.atom_coords()), 2.19951175503979, 4)
def test_optimize(self): mol = gto.M(atom=''' O 0. 0. 0. H 0. -0.757 0.587 H 0. 0.757 0.587 ''', symmetry=True, verbose=0) mol1 = geometric_solver.optimize(scf.RHF(mol)) self.assertAlmostEqual(lib.finger(mol1.atom_coords()), 3.038506469458414, 4) self.assertEqual(mol1.symmetry, 'C2v')
def Q3OptimizerDriver(pre_energy, mol , HF, R, conv_params): if pre_energy ==True: opt_pre = optimize(pre_energy, **conv_params) opt_geom = opt_pre.tofile(working_dir+'/opt-geom.xyz') else: if theory==KS: if shell == O: SPE =scf.UKS(mol).run() else shell == R: SPE =scf.RKS(mol).run() else theory == HF: if shell == O: SPE =scf.UHF(mol).run() else shell == R: SPE =scf.RHF(mol).run()
def f(mol): e, g = grad_scan(mol) r = mol.atom_coords() penalty = np.linalg.norm(r[0] - r[1])**2 * 0.1 e += penalty g[0] += (r[0] - r[1]) * 2 * 0.1 g[1] -= (r[0] - r[1]) * 2 * 0.1 print('Customized |g|', np.linalg.norm(g)) return e, g # # Function as_pyscf_method is a wrapper that convert the "energy-gradients" # function to berny_solver. The "energy-gradients" function takes the Mole # object as geometry input, and returns the energy and gradients of that # geometry. # fake_method = as_pyscf_method(mol, f) new_mol = berny_solver.optimize(fake_method) print('Old geometry (Bohr)') print(mol.atom_coords()) print('New geometry (Bohr)') print(new_mol.atom_coords()) # # Geometry can be also optimized with geomeTRIC library # new_mol = geometric_solver.optimize(fake_method)
''' import numpy from pyscf import gto, scf, cc, qmmm from pyscf.geomopt import berny_solver mol = gto.M(atom=''' C 1.1879 -0.3829 0.0000 C 0.0000 0.5526 0.0000 O -1.1867 -0.2472 0.0000 H -1.9237 0.3850 0.0000 H 2.0985 0.2306 0.0000 H 1.1184 -1.0093 0.8869 H 1.1184 -1.0093 -0.8869 H -0.0227 1.1812 0.8852 H -0.0227 1.1812 -0.8852 ''', basis='3-21g') numpy.random.seed(1) coords = numpy.random.random((5, 3)) * 10 charges = (numpy.arange(5) + 1.) * -.001 mf = qmmm.mm_charge(scf.RHF(mol), coords, charges) #mf.verbose=4 #mf.kernel() mol1 = berny_solver.optimize(mf) from pyscf.geomopt import geometric_solver mycc = cc.CCSD(mf) mol1 = geometric_solver.optimize(mycc)
''' Use geomeTRIC library to optimize the molecular geometry. ''' from pyscf import gto, scf from pyscf.geomopt.geometric_solver import optimize mol = gto.M(atom='N 0 0 0; N 0 0 1.2', basis='ccpvdz') mf = scf.RHF(mol) # # geometry optimization for HF. There are two entries to invoke the geomeTRIC # optimization # # method 1: import the optimize function from pyscf.geomopt.geometric_solver mol_eq = optimize(mf) print(mol_eq.atom_coords()) # method 2: create the optimizer from Gradients class mol_eq = mf.Gradients().optimizer(solver='geomeTRIC').kernel() # # geometry optimization for CASSCF # from pyscf import mcscf mf = scf.RHF(mol) mc = mcscf.CASSCF(mf, 4, 4) conv_params = { 'convergence_energy': 1e-4, # Eh 'convergence_grms': 3e-3, # Eh/Bohr 'convergence_gmax': 4.5e-3, # Eh/Bohr
print("Initial energy: {:.8e}".format(mc.e_tot[0])) mc.nuc_grad_iroot = 0 def my_call(env): carts = env['mol'].atom_coords() * BOHR h2co_geom_analysis(carts) conv_params = { 'convergence_energy': 1e-6, # Eh 'convergence_grms': 3e-5, # Eh/Bohr 'convergence_gmax': 4.5e-5, # Eh/Bohr 'convergence_drms': 1.2e-5, # Angstrom 'convergence_dmax': 1.8e-5, # Angstrom } conv, mol_eq = optimize(mc, callback=my_call, **conv_params) molcas_geom = np.asarray([[0.550219, -0.000000, -0.000000], [-0.690238, -0.000000, -0.000000], [1.139489, -0.000000, 0.937479], [1.139489, -0.000000, -0.937479]]) print( "SA(2) tPBE(6,6)/6-31g optimized geometry of first root of formaldehdye:") h2co_geom_analysis(mol_eq.atom_coords() * BOHR) print( "OpenMolcas's opinion using analytical gradient implementation (note OpenMolcas and PySCF have different quadrature grids):" ) h2co_geom_analysis(molcas_geom)
#!/usr/bin/env python # # Author: Qiming Sun <*****@*****.**> # ''' Geometry optimization with solvent model ''' from pyscf import gto, scf, dft from pyscf import solvent from pyscf.geomopt import geometric_solver mol = gto.M(atom=''' C 0.000000 0.000000 -0.542500 O 0.000000 0.000000 0.677500 H 0.000000 0.9353074360871938 -1.082500 H 0.000000 -0.9353074360871938 -1.082500 ''', verbose=4) mf = solvent.ddCOSMO(scf.RHF(mol)) new_mol = geometric_solver.optimize(mf)
Raises: Q3_Error: Invalid Input """ conv_params = { # These are the default settings 'convergence_energy': 1e-6, # Eh 'convergence_grms': 3e-4, # Eh/Bohr 'convergence_gmax': 4.5e-4, # Eh/Bohr 'convergence_drms': 1.2e-3, # Angstrom 'convergence_dmax': 1.8e-3, # Angstrom } def Q3OptimizerDriver(pre_energy, mol , HF, R, conv_params): if pre_energy ==True: opt_pre = optimize(pre_energy, **conv_params) opt_geom = opt_pre.tofile(working_dir+'/opt-geom.xyz') else: if theory==KS: if shell == O: SPE =scf.UKS(mol).run() else shell == R: SPE =scf.RKS(mol).run() else theory == HF: if shell == O: SPE =scf.UHF(mol).run() else shell == R: SPE =scf.RHF(mol).run() SPE_opt = optimize(SPE, **conv_params) opt_geom = mol_eq.tofile(working_dir+'opt-geom.xyz')
mol = gto.M(atom=''' C 0.000000 0.000000 -0.542500 O 0.000000 0.000000 0.677500 H 0.000000 0.9353074360871938 -1.082500 H 0.000000 -0.9353074360871938 -1.082500 ''', basis='3-21g') mf = scf.RHF(mol) # Run analyze function in callback def cb(envs): mf = envs['g_scanner'].base mf.analyze(verbose=4) # # Method 1: Pass callback to optimize function # geometric_solver.optimize(mf, callback=cb) berny_solver.optimize(mf, callback=cb) # # Method 2: Add callback to geometry optimizer # opt = mf.nuc_grad_method().as_scanner().optimizer() opt.callback = cb opt.kernel()
''' import numpy from pyscf import gto, scf, cc, qmmm from pyscf.geomopt import berny_solver mol = gto.M(atom=''' C 1.1879 -0.3829 0.0000 C 0.0000 0.5526 0.0000 O -1.1867 -0.2472 0.0000 H -1.9237 0.3850 0.0000 H 2.0985 0.2306 0.0000 H 1.1184 -1.0093 0.8869 H 1.1184 -1.0093 -0.8869 H -0.0227 1.1812 0.8852 H -0.0227 1.1812 -0.8852 ''', basis='3-21g') numpy.random.seed(1) coords = numpy.random.random((5,3)) * 10 charges = (numpy.arange(5) + 1.) * -.001 mf = qmmm.mm_charge(scf.RHF(mol), coords, charges) #mf.verbose=4 #mf.kernel() mol1 = berny_solver.optimize(mf) from pyscf.geomopt import geometric_solver mycc = cc.CCSD(mf) mol1 = geometric_solver.optimize(mycc)
#!/usr/bin/env python ''' Use geomeTRIC library to optimize the molecular geometry. ''' from pyscf import gto, scf from pyscf.geomopt.geometric_solver import optimize mol = gto.M(atom='N 0 0 0; N 0 0 1.2', basis='ccpvdz') mf = scf.RHF(mol) # # geometry optimization for HF # mol_eq = optimize(mf) print(mol_eq.atom_coords()) # # geometry optimization for CASSCF # from pyscf import mcscf mf = scf.RHF(mol) mc = mcscf.CASSCF(mf, 4, 4) conv_params = { 'convergence_energy': 1e-4, # Eh 'convergence_grms': 3e-3, # Eh/Bohr 'convergence_gmax': 4.5e-3, # Eh/Bohr 'convergence_drms': 1.2e-2, # Angstrom 'convergence_dmax': 1.8e-2, # Angstrom }