Skip to content

Commit 69edd56

Browse files
committed
week 5 updates
1 parent 910d69e commit 69edd56

1 file changed

Lines changed: 104 additions & 14 deletions

File tree

CH40208/geometry_optimisation/Lennard_Jones_optimisation.ipynb

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,124 @@
55
"id": "455b7ed0-22a2-4d49-852c-db66b191c027",
66
"metadata": {},
77
"source": [
8-
"### Exercise: Geometry Optimisation of a Lennard-Jones Potential\n",
8+
"## Synoptic Exercise: Geometry Optimisation of a Lennard-Jones Potential\n",
99
"\n",
10-
"The potential energy surface for a pair of “Lennard–Jones” is given by\n",
10+
"The Lennard-Jones potential describes the interaction between a pair of non-bonded atoms:\n",
1111
"\n",
12-
"$$U_\\mathrm{LJ} = \\frac{A}{r^{12}} - \\frac{B}{r^6}$$\n",
12+
"$$U_\\mathrm{LJ}(r) = \\frac{A}{r^{12}} - \\frac{B}{r^6}$$\n",
1313
"\n",
14-
"#### 1.\n",
14+
"where $r$ is the interatomic separation. The repulsive term ($r^{-12}$) represents electron cloud overlap at short distances, whilst the attractive term ($r^{-6}$) represents van der Waals interactions.\n",
1515
"\n",
16-
"Plot this function for $A$ = 1 × 10<sup>5</sup> eV Å<sup>12</sup> and $B$ = 40 eV Å<sup>6</sup> for the range $3.6 \\leq r \\leq 8.0$.\n",
16+
"In this exercise, you will use both gradient descent and Newton-Raphson methods to find the equilibrium separation for a Lennard-Jones pair, compare their performance, and explore how the choice of starting position affects convergence.\n",
1717
"\n",
18+
"We will use the parameters $A$ = 1 × 10<sup>5</sup> eV Å<sup>12</sup> and $B$ = 40 eV Å<sup>6</sup>.\n",
1819
"\n",
19-
"#### 2.\n",
20+
"The derivatives of the Lennard-Jones potential are:\n",
2021
"\n",
21-
"The first derivative of this potential energy function is given by\n",
22+
"**First derivative:**\n",
23+
"$$U'_\\mathrm{LJ}(r) = -12 \\frac{A}{r^{13}} + 6\\frac{B}{r^7}$$\n",
2224
"\n",
23-
"$$U^\\prime_\\mathrm{LJ} = -12 \\frac{A}{r^{13}} + 6\\frac{B}{r^7}$$\n",
25+
"**Second derivative:**\n",
26+
"$$U''_\\mathrm{LJ}(r) = 156\\frac{A}{r^{14}} - 42\\frac{B}{r^8}$$\n",
2427
"\n",
25-
"Write code to find the lowest-energy interatomic separation for a pair of Lennard–Jones atoms (with the potential parameters given above) using gradient descent. Start with $r = 5.0$ and $\\alpha=100$. How does your optimisation code perform with different values of $\\alpha$ and different starting values of $r$ (try $r$ = 3.2 Å, $r$ = 4.4 Å, and $r$ = 6.0 Å)?\n",
28+
"### Part A: Visualising the Potential Energy Surface\n",
2629
"\n",
27-
"#### 3.\n",
30+
"Before attempting optimisation, it is important to understand the shape of the potential energy surface.\n",
2831
"\n",
29-
"Write code to perform geometry optimisation of your Lennard-Jones potential using the Newton-Raphson method.\n",
32+
"1. Write a function `lennard_jones(r, A, B)` that calculates $U_\\mathrm{LJ}(r)$ for a given separation $r$ and parameters $A$ and $B$.\n",
3033
"\n",
31-
"The second derivative of $U_\\mathrm{LJ}$ is\n",
34+
"2. Create an array of $r$ values from 3.6 Å to 8.0 Å using `np.linspace()` with at least 200 points.\n",
3235
"\n",
33-
"$$U^{\\prime\\prime} = 156\\frac{A}{r^{14}} - 42\\frac{B}{r^8}$$\n",
36+
"3. Plot the potential energy as a function of $r$ for the given parameters. Label your axes appropriately and include a title.\n",
3437
"\n",
35-
"Investigate how the Newton-Raphson method performs with starting points of $r$ = 3.2 Å, $r$ = 4.4 Å, and $r$ = 6.0 Å."
38+
"4. By examining your plot:\n",
39+
" - Estimate the location of the minimum (the equilibrium bond length)\n",
40+
" - Identify the approximate energy at the minimum\n",
41+
" - Note where the potential energy crosses zero (where attractive and repulsive forces balance to give $U = 0$)\n",
42+
"\n",
43+
"### Part B: Gradient Descent Optimisation\n",
44+
"\n",
45+
"Now we will use gradient descent with an adaptive step size to find the minimum.\n",
46+
"\n",
47+
"5. Write a function `lennard_jones_gradient(r, A, B)` that calculates the first derivative $U'_\\mathrm{LJ}(r)$.\n",
48+
"\n",
49+
"6. Implement a gradient descent loop that:\n",
50+
" - Starts from $r = 5.0$ Å\n",
51+
" - Uses the update rule: $r_{i+1} = r_i - \\alpha U'(r_i)$\n",
52+
" - Uses $\\alpha = 100$ as the learning rate\n",
53+
" - Runs for a maximum of 50 iterations\n",
54+
" - Stops early (using `break`) when $|U'(r)| < 0.001$\n",
55+
" - Stores the position and gradient at each iteration in lists (for later analysis)\n",
56+
"\n",
57+
"7. After your loop completes, print:\n",
58+
" - The final predicted equilibrium separation\n",
59+
" - The number of iterations required\n",
60+
" - The final gradient value\n",
61+
"\n",
62+
"8. Create a plot showing how the position $r$ changes with iteration number. This visualises the convergence path.\n",
63+
"\n",
64+
"**Questions to consider:**\n",
65+
"- How many iterations were required to converge?\n",
66+
"- Does the convergence appear smooth or does the algorithm oscillate?\n",
67+
"- Look at your convergence plot: does the step size decrease as you approach the minimum? (Remember: the step size is $\\alpha \\times |U'(r)|$, and $U'(r)$ should decrease as you approach the minimum.)\n",
68+
"\n",
69+
"### Part C: Exploring Different Starting Positions with Gradient Descent\n",
70+
"\n",
71+
"The choice of starting position can significantly affect optimisation performance.\n",
72+
"\n",
73+
"9. Test your gradient descent code with three different starting positions:\n",
74+
" - $r = 3.2$ Å (close to the minimum, approaching from below)\n",
75+
" - $r = 4.4$ Å (at the minimum, approximately)\n",
76+
" - $r = 6.0$ Å (far from the minimum, approaching from above)\n",
77+
"\n",
78+
" For each starting position, record:\n",
79+
" - The final optimised position\n",
80+
" - The number of iterations required\n",
81+
" - Whether the algorithm converged successfully (i.e., did it find $|U'(r)| < 0.001$ within 50 iterations?)\n",
82+
"\n",
83+
"10. Summarise your results, showing the three starting positions and their outcomes.\n",
84+
"\n",
85+
"**Questions to consider:**\n",
86+
"- Do all starting positions converge to the same minimum?\n",
87+
"- Which starting position converges most quickly? Can you explain why based on your plot from Part A?\n",
88+
"- Did any starting positions fail to converge within 50 iterations?\n",
89+
"- How does the learning rate $\\alpha = 100$ perform for the different starting positions? Based on the gradient magnitudes at different $r$ values, does this $\\alpha$ seem appropriate everywhere, or only in certain regions?\n",
90+
"\n",
91+
"### Part D: Newton-Raphson Optimisation\n",
92+
"\n",
93+
"The Newton-Raphson method uses both first- and second-derivative information to converge more rapidly.\n",
94+
"\n",
95+
"11. Write a function `lennard_jones_second_derivative(r, A, B)` that calculates $U''_\\mathrm{LJ}(r)$.\n",
96+
"\n",
97+
"12. Implement a Newton-Raphson loop that:\n",
98+
" - Starts from $r = 5.0$ Å\n",
99+
" - Uses the update rule: $r_{i+1} = r_i - \\frac{U'(r_i)}{U''(r_i)}$\n",
100+
" - Runs for a maximum of 50 iterations\n",
101+
" - Stops early when $|U'(r)| < 0.001$\n",
102+
" - Stores the position and gradient at each iteration\n",
103+
"\n",
104+
"13. After your loop completes, print the final equilibrium separation and number of iterations required.\n",
105+
"\n",
106+
"14. Create a plot comparing the convergence paths of gradient descent (from Part B) and Newton-Raphson, both starting from $r = 5.0$ Å. Plot both curves on the same axes showing position vs. iteration number.\n",
107+
"\n",
108+
"**Questions to consider:**\n",
109+
"- How does the convergence speed of Newton-Raphson compare to gradient descent?\n",
110+
"- Why doesn't Newton-Raphson converge in a single step for the Lennard-Jones potential, unlike the harmonic potential?\n",
111+
"- Which method required fewer function evaluations? (Remember: gradient descent needs one derivative per step, whilst Newton-Raphson needs two.)\n",
112+
"\n",
113+
"### Part E: Comparing Methods Across Different Starting Positions\n",
114+
"\n",
115+
"15. Test Newton-Raphson with the same three starting positions you used for gradient descent in Part C:\n",
116+
" - $r = 3.2$ Å\n",
117+
" - $r = 4.4$ Å\n",
118+
" - $r = 6.0$ Å\n",
119+
"\n",
120+
" For each starting position, record the final position and the number of iterations required.\n",
121+
"\n",
122+
"**Questions to consider:**\n",
123+
"- Which method is more robust to different starting positions?\n",
124+
"- For which starting position(s) does Newton-Raphson show the greatest advantage?\n",
125+
"- Based on your results, which method would you recommend for optimising a Lennard-Jones potential? What factors influenced your decision?"
36126
]
37127
},
38128
{

0 commit comments

Comments
 (0)