def test_scf_grad(self): mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol0)).run() # solvent only de_cosmo = ddcosmo_grad.kernel(mf.with_solvent, mf.make_rdm1()) self.assertAlmostEqual(lib.fp(de_cosmo), 0.000770107393352652, 6) # solvent + solute de = mf.nuc_grad_method().kernel() self.assertAlmostEqual(lib.fp(de), -0.1920179073822721, 6) dm1 = mf.make_rdm1() mf1 = ddcosmo.ddcosmo_for_scf(scf.RHF(mol1)).run() e1 = mf1.e_tot e1_cosmo = mf1.with_solvent.energy(dm1) mf2 = ddcosmo.ddcosmo_for_scf(scf.RHF(mol2)).run() e2 = mf2.e_tot e2_cosmo = mf2.with_solvent.energy(dm1) self.assertAlmostEqual(abs((e2 - e1) / dx - de[0, 2]).max(), 0, 7) self.assertAlmostEqual( abs((e2_cosmo - e1_cosmo) / dx - de_cosmo[0, 2]).max(), 0, 7) sc = mf.nuc_grad_method().as_scanner() e, g = sc('H 0 1 0; H 0 1 1.2; H 1. 0 0; H .5 .5 0') self.assertAlmostEqual(e, -0.8317337703056022, 8) self.assertAlmostEqual(lib.fp(g), 0.06804297145388238, 6) mol3 = gto.M(atom='H 0 1 0; H 0 1 1.2; H 1. 0 0; H .5 .5 0', unit='B') mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol3)).run() de = mf.nuc_grad_method().kernel() self.assertAlmostEqual(lib.fp(de), 0.06804297145388238, 6)
def test_e_cosmo_grad(self): pcmobj = ddcosmo.DDCOSMO(mol0) de = ddcosmo_grad.kernel(pcmobj, dm) pcmobj = ddcosmo.DDCOSMO(mol1) e1 = pcmobj.energy(dm) pcmobj = ddcosmo.DDCOSMO(mol2) e2 = pcmobj.energy(dm) self.assertAlmostEqual(abs((e2 - e1) / dx - de[0, 2]).max(), 0, 7)
def test_e_cosmo_grad(self): pcmobj = ddcosmo.DDCOSMO(mol0) de = ddcosmo_grad.kernel(pcmobj, dm) pcmobj = ddcosmo.DDCOSMO(mol1) e1 = pcmobj.energy(dm) pcmobj = ddcosmo.DDCOSMO(mol2) e2 = pcmobj.energy(dm) self.assertAlmostEqual(abs((e2-e1)/dx - de[0,2]).max(), 0, 7)
def test_scf_grad(self): mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol0)).run() de_cosmo = ddcosmo_grad.kernel(mf._solvent, mf.make_rdm1()) de = mf.nuc_grad_method().kernel() dm1 = mf.make_rdm1() mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol1)).run() e1 = mf.e_tot e1_cosmo = mf._solvent.energy(dm1) mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol2)).run() e2 = mf.e_tot e2_cosmo = mf._solvent.energy(dm1) self.assertAlmostEqual(abs((e2-e1)/dx - de[0,2]).max(), 0, 7) self.assertAlmostEqual(abs((e2_cosmo-e1_cosmo)/dx - de_cosmo[0,2]).max(), 0, 7)
def test_scf_grad(self): mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol0)).run() de_cosmo = ddcosmo_grad.kernel(mf.with_solvent, mf.make_rdm1()) de = mf.nuc_grad_method().kernel() dm1 = mf.make_rdm1() mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol1)).run() e1 = mf.e_tot e1_cosmo = mf.with_solvent.energy(dm1) mf = ddcosmo.ddcosmo_for_scf(scf.RHF(mol2)).run() e2 = mf.e_tot e2_cosmo = mf.with_solvent.energy(dm1) self.assertAlmostEqual(abs((e2-e1)/dx - de[0,2]).max(), 0, 7) self.assertAlmostEqual(abs((e2_cosmo-e1_cosmo)/dx - de_cosmo[0,2]).max(), 0, 7)
def test_B1(self): def getB(mol): pcm = ddcosmo.DDCOSMO(mol) pcm.lmax = 2 pcm.eps = 0 natm = mol.natm nao = mol.nao nlm = (pcm.lmax + 1)**2 r_vdw = ddcosmo.get_atomic_radii(pcm) fi = ddcosmo.make_fi(pcm, r_vdw) ui = 1 - fi ui[ui < 0] = 0 pcm.grids = grids = dft.gen_grid.Grids(mol).run(level=0) coords_1sph, weights_1sph = ddcosmo.make_grids_one_sphere( pcm.lebedev_order) ylm_1sph = numpy.vstack( sph.real_sph_vec(coords_1sph, pcm.lmax, True)) cached_pol = ddcosmo.cache_fake_multipoles(grids, r_vdw, pcm.lmax) L = ddcosmo.make_L(pcm, r_vdw, ylm_1sph, fi) return make_B(pcm, r_vdw, ui, ylm_1sph, cached_pol, L) pcm = ddcosmo.DDCOSMO(mol0) pcm.lmax = 2 pcm.eps = 0 natm = mol0.natm nao = mol0.nao nlm = (pcm.lmax + 1)**2 r_vdw = ddcosmo.get_atomic_radii(pcm) fi = ddcosmo.make_fi(pcm, r_vdw) ui = 1 - fi ui[ui < 0] = 0 pcm.grids = grids = dft.gen_grid.Grids(mol0).run(level=0) coords_1sph, weights_1sph = ddcosmo.make_grids_one_sphere( pcm.lebedev_order) ylm_1sph = numpy.vstack(sph.real_sph_vec(coords_1sph, pcm.lmax, True)) cached_pol = ddcosmo.cache_fake_multipoles(grids, r_vdw, pcm.lmax) L = ddcosmo.make_L(pcm, r_vdw, ylm_1sph, fi) dB = make_B1(pcm, r_vdw, ui, ylm_1sph, cached_pol, L) B1 = getB(mol1) B2 = getB(mol2) self.assertAlmostEqual(abs((B2 - B1) / dx - dB[0, 2]).max(), 0, 8) nao = mol0.nao numpy.random.seed(1) dm1 = numpy.random.random((2, nao, nao)) dm2 = numpy.random.random((2, nao, nao)) dm = dm1[0] ref = numpy.einsum('azpqrs,npq->nazrs', dB, dm1) v = B1_dot_x(pcm, dm, r_vdw, ui, ylm_1sph, cached_pol, L) self.assertAlmostEqual(abs(v - ref[0]).max(), 0, 12) de = _ddcosmo_tdscf_grad._grad_ee(pcm, dm1, dm2, r_vdw, ui, ylm_1sph, cached_pol, L) ref = numpy.einsum('nazij,nij->naz', ref, dm2) self.assertAlmostEqual(abs(de - ref).max(), 0, 12) numpy.random.seed(1) dm = numpy.random.random((nao, nao)) dm = dm + dm.T ref = ddcosmo_grad.kernel(pcm, dm) dielectric = pcm.eps if dielectric > 0: f_epsilon = (dielectric - 1.) / dielectric else: f_epsilon = 1 de = _ddcosmo_tdscf_grad._grad_nn(pcm, r_vdw, ui, ylm_1sph, cached_pol, L) de += _ddcosmo_tdscf_grad._grad_ne(pcm, dm, r_vdw, ui, ylm_1sph, cached_pol, L) de += .5 * _ddcosmo_tdscf_grad._grad_ee(pcm, dm, dm, r_vdw, ui, ylm_1sph, cached_pol, L) de *= .5 * f_epsilon self.assertAlmostEqual(abs(de - ref).max(), 0, 12)