Skip to content

Commit cacc946

Browse files
committed
add example jupyter notebook of permitation algorithms
1 parent 5685500 commit cacc946

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed

examples/basic_algorithms.ipynb

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b42d0808",
6+
"metadata": {
7+
"id": "b42d0808"
8+
},
9+
"source": [
10+
"Usage of algorithms for symmetry analysis\n",
11+
"=========================================\n",
12+
"\n",
13+
"In order to determine the geometric symmetry, posym implements diferent algorithms to compute the permutation of atoms that minimize the CSM"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"id": "7d4340db",
19+
"metadata": {
20+
"id": "7d4340db"
21+
},
22+
"source": [
23+
"Setup environment\n",
24+
"----------------------"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"id": "65af4b5c",
30+
"metadata": {
31+
"id": "65af4b5c"
32+
},
33+
"source": [
34+
"Install required packages"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"id": "6368cc0b",
41+
"metadata": {
42+
"colab": {
43+
"base_uri": "https://localhost:8080/"
44+
},
45+
"id": "6368cc0b",
46+
"outputId": "d8389a23-86de-44e5-b89a-f1039e74e74a"
47+
},
48+
"outputs": [],
49+
"source": [
50+
"try:\n",
51+
" import posym\n",
52+
"except ImportError:\n",
53+
" ! pip install git+https://github.com/abelcarreras/posym.git@development\n",
54+
"try:\n",
55+
" import pyscf\n",
56+
"except ImportError:\n",
57+
" ! pip install pyscf\n",
58+
"try:\n",
59+
" import matplotlib\n",
60+
"except ImportError:\n",
61+
" ! pip install matplotlib"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "42420111",
67+
"metadata": {
68+
"id": "42420111"
69+
},
70+
"source": [
71+
"Import required modules"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"id": "5b788bc8",
78+
"metadata": {
79+
"id": "5b788bc8"
80+
},
81+
"outputs": [],
82+
"source": [
83+
"from posym import SymmetryMolecule\n",
84+
"from posym.tools import get_basis_set_pyscf, build_density, build_orbital\n",
85+
"from posym.algebra import norm\n",
86+
"from posym.config import Configuration\n",
87+
"import numpy as np\n",
88+
"import time\n",
89+
"import matplotlib.pyplot as plt"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"id": "ECd689yd1Fzg",
95+
"metadata": {
96+
"id": "ECd689yd1Fzg"
97+
},
98+
"source": [
99+
"helper function for timing"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"id": "LeCfugrjiN20",
106+
"metadata": {
107+
"id": "LeCfugrjiN20"
108+
},
109+
"outputs": [],
110+
"source": [
111+
"class TimeIt:\n",
112+
" def __init__(self):\n",
113+
" self._time_ini = 0\n",
114+
" self._time_fin = 0\n",
115+
"\n",
116+
" def __enter__(self):\n",
117+
" # Record the start time\n",
118+
" self._time_ini = time.time()\n",
119+
" return self # So that `as f` receives this object\n",
120+
"\n",
121+
" def __exit__(self, exc_type, exc_value, traceback):\n",
122+
" # Record the end time\n",
123+
" self._time_fin = time.time()\n",
124+
"\n",
125+
" @property\n",
126+
" def elapsed(self):\n",
127+
" # Return the elapsed time\n",
128+
" return self._time_fin - self._time_ini"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "CIQ8B_XraLqk",
134+
"metadata": {
135+
"id": "CIQ8B_XraLqk"
136+
},
137+
"source": [
138+
"Algorithms to compute CSM\n",
139+
"-------------------------"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"id": "Xt1jmD6Agi2w",
145+
"metadata": {
146+
"id": "Xt1jmD6Agi2w"
147+
},
148+
"source": [
149+
"Define molecule (distorted ethane)"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"id": "JA918EMggsu6",
156+
"metadata": {
157+
"id": "JA918EMggsu6"
158+
},
159+
"outputs": [],
160+
"source": [
161+
"coordinates = [[-0.7560, 0.0000, 0.0000],\n",
162+
" [ 0.7560, 0.0000, 0.0000],\n",
163+
" [-4.1404, 0.6586, 0.7845],\n",
164+
" [-2.1404, 0.3501, -0.9626],\n",
165+
" [-1.1405, -1.0087, 0.1781],\n",
166+
" [ 1.1404, -0.3501, 0.9626],\n",
167+
" [ 2.1405, 1.0087, -0.1781],\n",
168+
" [ 4.1404, -0.6586, -0.7845]]\n",
169+
"\n",
170+
"symbols = ['C','C','H','H','H','H','H','H']"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"id": "Fr36QEM8g90G",
176+
"metadata": {
177+
"id": "Fr36QEM8g90G"
178+
},
179+
"source": [
180+
"**Hungarian algorithm** : very fast, coherence only garanteed if close to exact symmetry (lower bound for the real CSM)"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"id": "VP3xB3G9g0jq",
187+
"metadata": {
188+
"colab": {
189+
"base_uri": "https://localhost:8080/"
190+
},
191+
"id": "VP3xB3G9g0jq",
192+
"outputId": "40720b40-7af9-4966-acce-073a56877854"
193+
},
194+
"outputs": [],
195+
"source": [
196+
"Configuration().algorithm = 'hungarian'\n",
197+
"Configuration().label_tolerance = 1.0\n",
198+
"\n",
199+
"with TimeIt() as f:\n",
200+
" mb = SymmetryMolecule('C2h', coordinates, symbols, orientation_angles=[-90, 0.0, 109.987640])\n",
201+
" print('CSM: ', mb.measure)\n",
202+
" print('Coor measure: ', mb, '(', norm(mb), ')')\n",
203+
" print(mb._symbols_restricted)\n",
204+
"print('time:', f.elapsed)\n"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"id": "HmrmyMgIhDEG",
210+
"metadata": {
211+
"id": "HmrmyMgIhDEG"
212+
},
213+
"source": [
214+
"**Exact algorithm** : very slow but exact for any geometry and any group"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": null,
220+
"id": "Dh6WJ3BfZgJd",
221+
"metadata": {
222+
"colab": {
223+
"base_uri": "https://localhost:8080/"
224+
},
225+
"id": "Dh6WJ3BfZgJd",
226+
"outputId": "13e7906c-3afd-409a-ce09-62b9e082ce8a"
227+
},
228+
"outputs": [],
229+
"source": [
230+
"\n",
231+
"Configuration().algorithm = 'exact'\n",
232+
"Configuration().label_tolerance = 1.0\n",
233+
"\n",
234+
"with TimeIt() as f:\n",
235+
" mb = SymmetryMolecule('C2h', coordinates, symbols, orientation_angles=[-90, 0.0, 109.987640])\n",
236+
" print('CSM: ', mb.measure)\n",
237+
" print('Coor measure: ', mb, '(', norm(mb), ')')\n",
238+
" print(mb._symbols_restricted)\n",
239+
"print('time:', f.elapsed)\n"
240+
]
241+
},
242+
{
243+
"cell_type": "markdown",
244+
"id": "WeQA4lOVhGNc",
245+
"metadata": {
246+
"id": "WeQA4lOVhGNc"
247+
},
248+
"source": [
249+
"**Exact algorithm with tolerance label**: faster as tolerance get close to zero, will yield overstimated CSM if tolerance label is too low (upper bound for the CSM)"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": null,
255+
"id": "vtVZFvlXhGk2",
256+
"metadata": {
257+
"colab": {
258+
"base_uri": "https://localhost:8080/"
259+
},
260+
"id": "vtVZFvlXhGk2",
261+
"outputId": "d6eb415f-82a0-4608-c450-366fb85c7b7c"
262+
},
263+
"outputs": [],
264+
"source": [
265+
"Configuration().algorithm = 'exact'\n",
266+
"Configuration().label_tolerance = 0.2\n",
267+
"\n",
268+
"with TimeIt() as f:\n",
269+
" mb = SymmetryMolecule('C2h', coordinates, symbols, orientation_angles=[-90, 0.0, 109.987640])\n",
270+
" print('CSM: ', mb.measure)\n",
271+
" print('Coor measure: ', mb, '(', norm(mb), ')')\n",
272+
" print(mb._symbols_restricted)\n",
273+
"print('time:', f.elapsed)"
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": null,
279+
"id": "8HJZpn-5tmei",
280+
"metadata": {
281+
"colab": {
282+
"base_uri": "https://localhost:8080/"
283+
},
284+
"id": "8HJZpn-5tmei",
285+
"outputId": "bcd03258-0493-4bc4-db22-9ab0da056563"
286+
},
287+
"outputs": [],
288+
"source": [
289+
"Configuration().algorithm = 'exact'\n",
290+
"Configuration().label_tolerance = 0.1\n",
291+
"\n",
292+
"with TimeIt() as f:\n",
293+
" mb = SymmetryMolecule('C2h', coordinates, symbols, orientation_angles=[-90, 0.0, 109.987640])\n",
294+
" print('CSM: ', mb.measure)\n",
295+
" print('Coor measure: ', mb, '(', norm(mb), ')')\n",
296+
" print(mb._symbols_restricted)\n",
297+
"print('time:', f.elapsed)"
298+
]
299+
}
300+
],
301+
"metadata": {
302+
"colab": {
303+
"provenance": []
304+
},
305+
"kernelspec": {
306+
"display_name": "Python 3 (ipykernel)",
307+
"language": "python",
308+
"name": "python3"
309+
},
310+
"language_info": {
311+
"codemirror_mode": {
312+
"name": "ipython",
313+
"version": 3
314+
},
315+
"file_extension": ".py",
316+
"mimetype": "text/x-python",
317+
"name": "python",
318+
"nbconvert_exporter": "python",
319+
"pygments_lexer": "ipython3",
320+
"version": "3.11.9"
321+
}
322+
},
323+
"nbformat": 4,
324+
"nbformat_minor": 5
325+
}

0 commit comments

Comments
 (0)