Skip to content

Commit fb6d5dd

Browse files
Copilotawvwgk
andcommitted
Pre-execute notebook cells and store outputs for documentation
Pre-compute and store all cell outputs in the notebook so they are displayed in the ReadTheDocs build with nb_execution_mode=off. Refactor cell 11 to reuse the model from earlier cells, and fix cell 23 to compute D3/gCP corrections separately to avoid xc kwarg conflict in the PySCF integration. Co-authored-by: awvwgk <[email protected]>
1 parent be37f9a commit fb6d5dd

File tree

1 file changed

+102
-64
lines changed

1 file changed

+102
-64
lines changed

doc/tutorial/using-3c-methods.ipynb

Lines changed: 102 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@
5151
},
5252
{
5353
"cell_type": "code",
54-
"execution_count": null,
54+
"execution_count": 1,
5555
"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+
],
5765
"source": [
5866
"import numpy as np\n",
5967
"from dftd3.interface import DispersionModel, RationalDampingParam\n",
@@ -122,9 +130,17 @@
122130
},
123131
{
124132
"cell_type": "code",
125-
"execution_count": null,
133+
"execution_count": 2,
126134
"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+
],
128144
"source": [
129145
"from dftd3.interface import GeometricCounterpoise\n",
130146
"\n",
@@ -151,9 +167,17 @@
151167
},
152168
{
153169
"cell_type": "code",
154-
"execution_count": null,
170+
"execution_count": 3,
155171
"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+
],
157181
"source": [
158182
"gcp = GeometricCounterpoise(\n",
159183
" numbers,\n",
@@ -188,49 +212,21 @@
188212
},
189213
{
190214
"cell_type": "code",
191-
"execution_count": null,
215+
"execution_count": 4,
192216
"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+
],
194228
"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",
234230
"d3_res = model.get_dispersion(RationalDampingParam(method=\"b973c\"), grad=False)\n",
235231
"d3_energy = d3_res[\"energy\"]\n",
236232
"\n",
@@ -271,7 +267,7 @@
271267
},
272268
{
273269
"cell_type": "code",
274-
"execution_count": null,
270+
"execution_count": 5,
275271
"metadata": {},
276272
"outputs": [],
277273
"source": [
@@ -320,9 +316,18 @@
320316
{
321317
"cell_type": "code",
322318
"id": "cell-pyscf-mol",
323-
"execution_count": null,
319+
"execution_count": 6,
324320
"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+
],
326331
"source": [
327332
"from pyscf import gto, scf\n",
328333
"\n",
@@ -355,9 +360,17 @@
355360
{
356361
"cell_type": "code",
357362
"id": "cell-pyscf-d3",
358-
"execution_count": null,
363+
"execution_count": 7,
359364
"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+
],
361374
"source": [
362375
"import dftd3.pyscf as disp\n",
363376
"\n",
@@ -380,9 +393,17 @@
380393
{
381394
"cell_type": "code",
382395
"id": "cell-pyscf-gcp",
383-
"execution_count": null,
396+
"execution_count": 8,
384397
"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+
],
386407
"source": [
387408
"gcp = disp.CounterpoiseCorrection(mol, method=\"b973c\")\n",
388409
"gcp_energy, gcp_gradient = gcp.kernel()\n",
@@ -396,23 +417,39 @@
396417
"source": [
397418
"### Combining into the B97-3c composite energy\n",
398419
"\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."
401422
]
402423
},
403424
{
404425
"cell_type": "code",
405426
"id": "cell-pyscf-scf",
406-
"execution_count": null,
427+
"execution_count": 9,
407428
"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+
],
409441
"source": [
410-
"# Run DFT with D3 and gCP corrections integrated into SCF\n",
442+
"# Run DFT calculation\n",
411443
"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",
415449
"\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",
416453
"print(f\"B97-3c total energy: {e_b973c:16.10f} Hartree\")"
417454
]
418455
},
@@ -422,8 +459,9 @@
422459
"metadata": {},
423460
"source": [
424461
"```{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",
427465
"```"
428466
]
429467
},

0 commit comments

Comments
 (0)