Skip to content

Commit

Permalink
remove %%time ...
Browse files Browse the repository at this point in the history
  • Loading branch information
ajz34 committed Mar 1, 2019
1 parent 960818d commit c983096
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions source/dft_nuc_hess.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"由于 DFT 格点积分的计算中,会使用内存消耗上巨大的格点;在小体系下,格点的数量会远大于基组与原子数量,因此处理计算时,不谨慎的算法与数据存储很可能导致巨大的计算消耗,或者巨大的内存占用.事实上,在我自己编写与调试这份文档时,已经出现过内存占用过大而宕机的情况.因此,不少代码块将会显示执行时间,以对代码效率有直观的认识.不过,`%%time` 只能给出定性效率;定量的效率仍然需要通过 `%%timeit` 给出."
"由于 DFT 格点积分的计算中,会使用内存消耗上巨大的格点;在小体系下,格点的数量会远大于基组与原子数量,因此处理计算时,不谨慎的算法与数据存储很可能导致巨大的计算消耗,或者巨大的内存占用.事实上,在我自己编写与调试这份文档时,已经出现过内存占用过大而宕机的情况.因此,不少代码块将会显示执行时间,以对代码效率有直观的认识.不过,`%%time` 只能给出定性效率;定量的效率仍然需要通过 `%%timeit` 给出.\n",
"\n",
"**临时更新**:似乎加入了 `%%time` 的代码在 ReadTheDocs 上不太能运行.这里暂时统一注释之."
]
},
{
Expand Down Expand Up @@ -78,7 +80,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"scf_eng = dft.RKS(mol)\n",
"scf_eng.xc = \"b3lypg\" # compare that to gaussian\n",
"scf_eng.grids.atom_grid = (99, 590)\n",
Expand All @@ -93,7 +95,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"scf_grad = grad.rks.Gradients(scf_eng)\n",
"grad_RKS = scf_grad.kernel()"
]
Expand All @@ -104,7 +106,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"scf_hess = hessian.rks.Hessian(scf_eng)\n",
"hess_RKS = scf_hess.kernel()"
]
Expand Down Expand Up @@ -301,7 +303,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_ao, grid_mask, grid_weight, grid_coord = next(scf_eng._numint.block_loop(mol, scf_eng.grids, nao, 3, 2000))\n",
"ngrid = grid_ao.shape[1]"
]
Expand Down Expand Up @@ -331,7 +333,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_ao_0 = grid_ao[0]\n",
"grid_ao_1 = grid_ao[1:4]\n",
"grid_ao_2T = grid_ao[4:10]"
Expand All @@ -350,7 +352,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"XX, XY, XZ, YY, YZ, ZZ = range(4, 10)\n",
"XXX, XXY, XXZ, XYY, XYZ, XZZ, YYY, YYZ, YZZ, ZZZ = range(10, 20)\n",
"\n",
Expand Down Expand Up @@ -379,7 +381,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"XX, XY, XZ, YY, YZ, ZZ = range(4, 10)\n",
"XXX, XXY, XXZ, XYY, XYZ, XZZ, YYY, YYZ, YZZ, ZZZ = range(10, 20)\n",
"\n",
Expand Down Expand Up @@ -463,7 +465,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_rho = np.einsum(\"uv, tgu, gv -> tg\", D, grid_ao[0:4], grid_ao_0, optimize=True)\n",
"grid_rho[1:] *= 2\n",
"grid_rho_0 = grid_rho[0]\n",
Expand All @@ -487,7 +489,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_vxc, grid_fxc = scf_eng._numint.eval_xc(scf_eng.xc, grid_rho, deriv=2)[1:3]\n",
"grid_fr, grid_fg = grid_vxc[0:2]\n",
"grid_frr, grid_frg, grid_fgg = grid_fxc[0:3]\n",
Expand Down Expand Up @@ -519,7 +521,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_A_rho_1 = np.zeros((natm, 3, ngrid))\n",
"grid_A_rho_2 = np.zeros((natm, 3, 3, ngrid))\n",
"for A in range(natm):\n",
Expand Down Expand Up @@ -698,7 +700,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grad_gga = - np.einsum(\"g, Atg -> At\", grid_fr, grid_A_rho_1)\n",
"grad_gga += - 2 * np.einsum(\"g, Atrg, rg -> At\", grid_fg, grid_A_rho_2, grid_rho_1)\n",
"print(np.allclose(grad_hf + grad_gga, scf_grad.grad_elec()))"
Expand Down Expand Up @@ -806,7 +808,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_noU_gga_1 = 1 * np.einsum(\"g, Atg , Bsg -> ABts\", grid_frr, grid_A_rho_1, grid_A_rho_1, optimize=True)\n",
"hess_noU_gga_1 += 2 * np.einsum(\"g, Atg , Bswg, wg -> ABts\", grid_frg, grid_A_rho_1, grid_A_rho_2, grid_rho_1, optimize=True)\n",
"hess_noU_gga_1 += 2 * np.einsum(\"g, Atrg, Bsg , rg -> ABts\", grid_frg, grid_A_rho_2, grid_A_rho_1, grid_rho_1, optimize=True)\n",
Expand Down Expand Up @@ -838,7 +840,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_noU_gga_2 = np.zeros((natm, natm, 3, 3))\n",
"hess_noU_gga_2_ao = 4 * np.einsum(\"g, rg, trgu, sgv -> tsuv\", grid_fg, grid_rho_1, grid_ao_2, grid_ao_1, optimize=True)\n",
"hess_noU_gga_2_ao += hess_noU_gga_2_ao.transpose(1, 0, 3, 2)\n",
Expand All @@ -864,7 +866,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"vxc_deriv2_pyscf = hessian.rks._get_vxc_deriv2(scf_hess, C, mo_occ, 2000)\n",
"hess_deriv2_pyscf = np.zeros((natm, natm, 3, 3))\n",
"for A in range(natm):\n",
Expand Down Expand Up @@ -911,7 +913,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_noU_gga_3_ao = 2 * np.einsum(\"g, Tgu, gv -> Tuv\", grid_fr, grid_ao_2T, grid_ao_0, optimize=True)\n",
"hess_noU_gga_3_ao += 4 * np.einsum(\"g, rg, Tgu, rgv -> Tuv\", grid_fg, grid_rho_1, grid_ao_2T, grid_ao_1, optimize=True)\n",
"hess_noU_gga_3_ao += 4 * np.einsum(\"g, rg, rTgu, gv -> Tuv\", grid_fg, grid_rho_1, grid_ao_3T, grid_ao_0, optimize=True)"
Expand All @@ -923,7 +925,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"XX, XY, XZ, YY, YZ, ZZ = range(6)\n",
"hess_noU_gga_3 = np.zeros((natm, natm, 3, 3))\n",
"for A in range(natm):\n",
Expand All @@ -944,7 +946,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"vxc_diag_pyscf = hessian.rks._get_vxc_diag(scf_hess, C, mo_occ, 2000)\n",
"print(np.allclose(vxc_diag_pyscf, hess_noU_gga_3_ao[[XX, XY, XZ, XY, YY, YZ, XZ, YZ, ZZ]].reshape((3, 3, nao, nao)) / 2))"
]
Expand All @@ -962,7 +964,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"def get_hess_ao_noU_hcore(A, B):\n",
" ao_matrix = np.zeros((3 * 3, nao, nao))\n",
" zA, zB = mol.atom_charge(A), mol.atom_charge(B)\n",
Expand Down Expand Up @@ -1025,7 +1027,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"partial_hess_pyscf = scf_hess.partial_hess_elec()"
]
},
Expand Down Expand Up @@ -1162,7 +1164,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_ao_hgga = -0.5 * np.einsum(\"g, Atg , gu, gv -> Atuv\", grid_frr, grid_A_rho_1, grid_ao_0, grid_ao_0, optimize=EINOPT)\n",
"hess_ao_hgga += - 2 * np.einsum(\"g, Atg , wg, wgu, gv -> Atuv\", grid_frg, grid_A_rho_1, grid_rho_1, grid_ao_1, grid_ao_0, optimize=EINOPT)\n",
"hess_ao_hgga += - np.einsum(\"g, rg, Atrg, gu, gv -> Atuv\", grid_frg, grid_rho_1, grid_A_rho_2, grid_ao_0, grid_ao_0, optimize=EINOPT)\n",
Expand All @@ -1183,7 +1185,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_ao_hgga_mat1 = - np.einsum(\"g, tgu, gv -> tuv\", grid_fr, grid_ao_1, grid_ao_0, optimize=EINOPT)\n",
"hess_ao_hgga_mat1 -= 2 * np.einsum(\"g, rg, tgu, rgv -> tuv\", grid_fg, grid_rho_1, grid_ao_1, grid_ao_1, optimize=EINOPT)\n",
"hess_ao_hgga_mat1 -= 2 * np.einsum(\"g, rg, trgu, gv -> tuv\", grid_fg, grid_rho_1, grid_ao_2, grid_ao_0, optimize=EINOPT)\n",
Expand All @@ -1206,7 +1208,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_ao_hgga += hess_ao_hgga.swapaxes(2, 3)"
]
},
Expand All @@ -1223,7 +1225,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"vxc_deriv1_pyscf = hessian.rks._get_vxc_deriv1(scf_hess, C, mo_occ, 2000)\n",
"print(np.allclose(vxc_deriv1_pyscf, hess_ao_hgga))"
]
Expand Down Expand Up @@ -1349,7 +1351,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"rand_pi_pyscf = hessian.rhf.gen_vind(scf_eng, C, mo_occ)(rand_pi)"
]
},
Expand All @@ -1359,7 +1361,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"rand_pi_my = Ax(rand_pi)"
]
},
Expand Down Expand Up @@ -1387,7 +1389,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"grid_rho1phi1 = np.einsum(\"tg, tgu -> gu\", grid_rho_1, grid_ao_1, optimize=True)\n",
"rdm2_inAx = 2 * np.einsum(\"g, gk, gu -> gku\", grid_frg, grid_ao_0, grid_rho1phi1, optimize=EINOPT)\n",
"rdm2_inAx += rdm2_inAx.swapaxes(1, 2)\n",
Expand Down Expand Up @@ -1429,7 +1431,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"rand_pi_my = Ax(rand_pi)"
]
},
Expand Down Expand Up @@ -1462,7 +1464,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"def get_hess_ao_h1(A):\n",
" ao_matrix = np.zeros((3, nao, nao))\n",
" sA = mol_slice(A)\n",
Expand Down Expand Up @@ -1494,7 +1496,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_U, hess_M = scf.cphf.solve(Ax, e, mo_occ, hess_pi_h1.reshape(-1, nmo, nocc), hess_pi_s1.reshape(-1, nmo, nocc))\n",
"hess_U.shape = (natm, 3, nmo, nocc); hess_M.shape = (natm, 3, nocc, nocc)"
]
Expand All @@ -1505,7 +1507,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_withU = 4 * np.einsum(\"Bspi, Atpi -> ABts\", hess_U, hess_pi_h1)\n",
"hess_withU -= 4 * np.einsum(\"Bspi, Atpi, i -> ABts\", hess_U, hess_pi_s1, eo)\n",
"hess_withU -= 2 * np.einsum(\"Atki, Bski -> ABts\", hess_pi_s1[:, :, :nocc], hess_M)"
Expand All @@ -1524,7 +1526,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_my_GGA_elec = hess_withU + hess_noU_noGGA + hess_noU_gga_1 + hess_noU_gga_2 + hess_noU_gga_3\n",
"hess_pyscf_GGA_elec = scf_hess.hess_elec()\n",
"print(np.allclose(hess_my_GGA_elec, hess_pyscf_GGA_elec))"
Expand All @@ -1550,7 +1552,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"\n",
"EINOPT = [\"greedy\", 1024**3 * 2 / 8]\n",
"\n",
Expand Down Expand Up @@ -1718,7 +1720,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# %%time\n",
"hess_pyscf_GGA_elec = scf_hess.hess_elec()"
]
},
Expand Down Expand Up @@ -1748,7 +1750,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.6.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c983096

Please sign in to comment.