|
51 | 51 | }, |
52 | 52 | { |
53 | 53 | "cell_type": "code", |
54 | | - "execution_count": null, |
| 54 | + "execution_count": 1, |
55 | 55 | "metadata": {}, |
56 | | - "outputs": [], |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "output_type": "stream", |
| 59 | + "name": "stdout", |
| 60 | + "text": [ |
| 61 | + "D3 dispersion energy: -0.0163961834 Hartree\n" |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
57 | 65 | "source": [ |
58 | 66 | "import numpy as np\n", |
59 | 67 | "from dftd3.interface import DispersionModel, RationalDampingParam\n", |
|
122 | 130 | }, |
123 | 131 | { |
124 | 132 | "cell_type": "code", |
125 | | - "execution_count": null, |
| 133 | + "execution_count": 2, |
126 | 134 | "metadata": {}, |
127 | | - "outputs": [], |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "output_type": "stream", |
| 138 | + "name": "stdout", |
| 139 | + "text": [ |
| 140 | + "gCP energy: -0.0901468369 Hartree\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
128 | 144 | "source": [ |
129 | 145 | "from dftd3.interface import GeometricCounterpoise\n", |
130 | 146 | "\n", |
|
151 | 167 | }, |
152 | 168 | { |
153 | 169 | "cell_type": "code", |
154 | | - "execution_count": null, |
| 170 | + "execution_count": 3, |
155 | 171 | "metadata": {}, |
156 | | - "outputs": [], |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "output_type": "stream", |
| 175 | + "name": "stdout", |
| 176 | + "text": [ |
| 177 | + "gCP energy: 0.0461285860 Hartree\n" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
157 | 181 | "source": [ |
158 | 182 | "gcp = GeometricCounterpoise(\n", |
159 | 183 | " numbers,\n", |
|
188 | 212 | }, |
189 | 213 | { |
190 | 214 | "cell_type": "code", |
191 | | - "execution_count": null, |
| 215 | + "execution_count": 4, |
192 | 216 | "metadata": {}, |
193 | | - "outputs": [], |
| 217 | + "outputs": [ |
| 218 | + { |
| 219 | + "output_type": "stream", |
| 220 | + "name": "stdout", |
| 221 | + "text": [ |
| 222 | + "D3 dispersion energy: -0.0632960598 Hartree\n", |
| 223 | + "gCP correction energy: -0.0901468369 Hartree\n", |
| 224 | + "Total correction: -0.1534428968 Hartree\n" |
| 225 | + ] |
| 226 | + } |
| 227 | + ], |
194 | 228 | "source": [ |
195 | | - "import numpy as np\n", |
196 | | - "from dftd3.interface import (\n", |
197 | | - " DispersionModel,\n", |
198 | | - " RationalDampingParam,\n", |
199 | | - " GeometricCounterpoise,\n", |
200 | | - ")\n", |
201 | | - "\n", |
202 | | - "numbers = np.array(\n", |
203 | | - " [6, 7, 6, 7, 6, 6, 6, 8, 7, 6, 8, 7, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", |
204 | | - ")\n", |
205 | | - "positions = np.array([\n", |
206 | | - " [+2.02799738646442, +0.09231312124713, -0.14310895950963],\n", |
207 | | - " [+4.75011007621000, +0.02373496014051, -0.14324124033844],\n", |
208 | | - " [+6.33434307654413, +2.07098865582721, -0.14235306905930],\n", |
209 | | - " [+8.72860718071825, +1.38002919517619, -0.14265542523943],\n", |
210 | | - " [+8.65318821103610, -1.19324866489847, -0.14231527453678],\n", |
211 | | - " [+6.23857175648671, -2.08353643730276, -0.14218299370797],\n", |
212 | | - " [+5.63266886875962, -4.69950321056008, -0.13940509630299],\n", |
213 | | - " [+3.44931709749015, -5.48092386085491, -0.14318454855466],\n", |
214 | | - " [+7.77508917214943, -5.24064112783473, -0.13206210149840],\n", |
215 | | - " [+9.68504443463980, -3.43480556543577, -0.13376503914194],\n", |
216 | | - " [+11.7766597740572, -2.85589272667498, -0.13347836327959],\n", |
217 | | - " [+0.71292821912498, -1.81184541295565, -0.14404507712755],\n", |
218 | | - " [-1.07804988915028, -0.36933811262178, -0.14399838668498],\n", |
219 | | - " [+9.84554065797340, -6.86700842661498, -0.13277505395063],\n", |
220 | | - " [-2.48328028863736, -1.73067674389689, -0.14259442617502],\n", |
221 | | - " [-1.34385752710948, +0.57786478045678, +1.58564153988000],\n", |
222 | | - " [-1.34425478091498, +0.57834894571524, -1.87640258713638],\n", |
223 | | - " [+8.96081504094682, -8.37942090821983, +1.00589803206426],\n", |
224 | | - " [+11.5017680845878, -6.28787412600376, -0.13181456387625],\n", |
225 | | - " [+8.96116647088646, -8.37865627213056, -1.27402975539366],\n", |
226 | | - " [+1.52389863680920, +4.42491864930852, +1.59083729873498],\n", |
227 | | - " [+1.52355529826621, +4.42437909527069, -1.87861372682396],\n", |
228 | | - " [-0.43240687424107, +5.48666820368743, -0.14223727178700],\n", |
229 | | - " [+3.59762505724975, +5.19469189498972, -0.14192321862387],\n", |
230 | | - "])\n", |
231 | | - "\n", |
232 | | - "# 1. Compute D3 dispersion correction\n", |
233 | | - "model = DispersionModel(numbers, positions)\n", |
| 229 | + "# Compute D3 dispersion correction for B97-3c (using the model created above)\n", |
234 | 230 | "d3_res = model.get_dispersion(RationalDampingParam(method=\"b973c\"), grad=False)\n", |
235 | 231 | "d3_energy = d3_res[\"energy\"]\n", |
236 | 232 | "\n", |
|
271 | 267 | }, |
272 | 268 | { |
273 | 269 | "cell_type": "code", |
274 | | - "execution_count": null, |
| 270 | + "execution_count": 5, |
275 | 271 | "metadata": {}, |
276 | 272 | "outputs": [], |
277 | 273 | "source": [ |
|
320 | 316 | { |
321 | 317 | "cell_type": "code", |
322 | 318 | "id": "cell-pyscf-mol", |
323 | | - "execution_count": null, |
| 319 | + "execution_count": 6, |
324 | 320 | "metadata": {}, |
325 | | - "outputs": [], |
| 321 | + "outputs": [ |
| 322 | + { |
| 323 | + "output_type": "stream", |
| 324 | + "name": "stdout", |
| 325 | + "text": [ |
| 326 | + "Number of atoms: 6\n", |
| 327 | + "Basis set: def2-mTZVP\n" |
| 328 | + ] |
| 329 | + } |
| 330 | + ], |
326 | 331 | "source": [ |
327 | 332 | "from pyscf import gto, scf\n", |
328 | 333 | "\n", |
|
355 | 360 | { |
356 | 361 | "cell_type": "code", |
357 | 362 | "id": "cell-pyscf-d3", |
358 | | - "execution_count": null, |
| 363 | + "execution_count": 7, |
359 | 364 | "metadata": {}, |
360 | | - "outputs": [], |
| 365 | + "outputs": [ |
| 366 | + { |
| 367 | + "output_type": "stream", |
| 368 | + "name": "stdout", |
| 369 | + "text": [ |
| 370 | + "D3 dispersion energy: -0.0029542429 Hartree\n" |
| 371 | + ] |
| 372 | + } |
| 373 | + ], |
361 | 374 | "source": [ |
362 | 375 | "import dftd3.pyscf as disp\n", |
363 | 376 | "\n", |
|
380 | 393 | { |
381 | 394 | "cell_type": "code", |
382 | 395 | "id": "cell-pyscf-gcp", |
383 | | - "execution_count": null, |
| 396 | + "execution_count": 8, |
384 | 397 | "metadata": {}, |
385 | | - "outputs": [], |
| 398 | + "outputs": [ |
| 399 | + { |
| 400 | + "output_type": "stream", |
| 401 | + "name": "stdout", |
| 402 | + "text": [ |
| 403 | + "gCP correction energy: -0.0112038519 Hartree\n" |
| 404 | + ] |
| 405 | + } |
| 406 | + ], |
386 | 407 | "source": [ |
387 | 408 | "gcp = disp.CounterpoiseCorrection(mol, method=\"b973c\")\n", |
388 | 409 | "gcp_energy, gcp_gradient = gcp.kernel()\n", |
|
396 | 417 | "source": [ |
397 | 418 | "### Combining into the B97-3c composite energy\n", |
398 | 419 | "\n", |
399 | | - "The `dftd3.pyscf` module provides `energy` and `gcp_energy` functions that patch into PySCF's SCF cycle.\n", |
400 | | - "We can apply both the D3 correction and the gCP correction to obtain the full B97-3c result." |
| 420 | + "With the D3 and gCP corrections computed, we can now run the DFT calculation and\n", |
| 421 | + "add both corrections to obtain the full B97-3c result." |
401 | 422 | ] |
402 | 423 | }, |
403 | 424 | { |
404 | 425 | "cell_type": "code", |
405 | 426 | "id": "cell-pyscf-scf", |
406 | | - "execution_count": null, |
| 427 | + "execution_count": 9, |
407 | 428 | "metadata": {}, |
408 | | - "outputs": [], |
| 429 | + "outputs": [ |
| 430 | + { |
| 431 | + "output_type": "stream", |
| 432 | + "name": "stdout", |
| 433 | + "text": [ |
| 434 | + "DFT energy: -152.8634165688 Hartree\n", |
| 435 | + "D3 dispersion: -0.0029542429 Hartree\n", |
| 436 | + "gCP correction: -0.0112038519 Hartree\n", |
| 437 | + "B97-3c total energy: -152.8775746636 Hartree\n" |
| 438 | + ] |
| 439 | + } |
| 440 | + ], |
409 | 441 | "source": [ |
410 | | - "# Run DFT with D3 and gCP corrections integrated into SCF\n", |
| 442 | + "# Run DFT calculation\n", |
411 | 443 | "mf = scf.RKS(mol, xc=\"b97\")\n", |
412 | | - "mf = disp.energy(mf, xc=\"b973c\", version=\"d3bj\")\n", |
413 | | - "mf = disp.gcp_energy(mf, method=\"b973c\")\n", |
414 | | - "e_b973c = mf.kernel()\n", |
| 444 | + "mf.verbose = 0\n", |
| 445 | + "e_scf = mf.kernel()\n", |
| 446 | + "\n", |
| 447 | + "# Add D3 and gCP corrections for the full B97-3c energy\n", |
| 448 | + "e_b973c = e_scf + float(d3_energy) + float(gcp_energy)\n", |
415 | 449 | "\n", |
| 450 | + "print(f\"DFT energy: {e_scf:16.10f} Hartree\")\n", |
| 451 | + "print(f\"D3 dispersion: {float(d3_energy):16.10f} Hartree\")\n", |
| 452 | + "print(f\"gCP correction: {float(gcp_energy):16.10f} Hartree\")\n", |
416 | 453 | "print(f\"B97-3c total energy: {e_b973c:16.10f} Hartree\")" |
417 | 454 | ] |
418 | 455 | }, |
|
422 | 459 | "metadata": {}, |
423 | 460 | "source": [ |
424 | 461 | "```{note}\n", |
425 | | - "The `disp.energy` function patches the D3 dispersion into PySCF's SCF cycle, and `disp.gcp_energy` does the same for the geometric counter-poise correction.\n", |
426 | | - "Both corrections are included during the self-consistent field optimization via the `energy_nuc()` hook.\n", |
| 462 | + "The D3 dispersion and gCP corrections are geometry-dependent but independent of the\n", |
| 463 | + "electronic density, so they can be computed separately and added to the converged SCF energy.\n", |
| 464 | + "For geometry optimizations, the gradients from all three contributions (SCF, D3, gCP) must be combined.\n", |
427 | 465 | "```" |
428 | 466 | ] |
429 | 467 | }, |
|
0 commit comments