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42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### OCP Data Preprocessing Tutorial\n",
    "\n",
    "\n",
    "This notebook provides an overview of converting ASE Atoms objects to PyTorch Geometric Data objects. To better understand the raw data contained within OC20, check out the following tutorial first: https://github.com/Open-Catalyst-Project/ocp/blob/master/docs/source/tutorials/data_visualization.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ocpmodels.preprocessing import AtomsToGraphs\n",
    "import ase.io\n",
    "from ase.build import bulk\n",
    "from ase.build import fcc100, add_adsorbate, molecule\n",
    "from ase.constraints import FixAtoms\n",
    "from ase.calculators.emt import EMT\n",
    "from ase.optimize import BFGS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate toy dataset: Relaxation of CO on Cu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adslab = fcc100(\"Cu\", size=(2, 2, 3))\n",
    "ads = molecule(\"CO\")\n",
    "add_adsorbate(adslab, ads, 3, offset=(1, 1))\n",
    "cons = FixAtoms(indices=[atom.index for atom in adslab if (atom.tag == 3)])\n",
    "adslab.set_constraint(cons)\n",
    "adslab.center(vacuum=13.0, axis=2)\n",
    "adslab.set_pbc(True)\n",
    "adslab.set_calculator(EMT())\n",
    "dyn = BFGS(adslab, trajectory=\"CuCO_adslab.traj\", logfile=None)\n",
    "dyn.run(fmax=0, steps=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1001\n"
     ]
    }
   ],
   "source": [
    "raw_data = ase.io.read(\"CuCO_adslab.traj\", \":\")\n",
    "print(len(raw_data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert Atoms object to Data object\n",
    "\n",
    "The AtomsToGraphs class takes in several arguments to control how Data objects created:\n",
    "\n",
    "- max_neigh (int):   Maximum number of neighbors a given atom is allowed to have, discarding the furthest\n",
    "- radius (float):      Cutoff radius to compute nearest neighbors around\n",
    "- r_energy (bool):    Write energy to Data object\n",
    "- r_forces (bool):    Write forces to Data object\n",
    "- r_distances (bool): Write distances between neighbors to Data object\n",
    "- r_edges (bool):     Write neigbhor edge indices to Data object\n",
    "- r_fixed (bool):     Write indices of fixed atoms to Data object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a2g = AtomsToGraphs(\n",
    "    max_neigh=50,\n",
    "    radius=6,\n",
    "    r_energy=True,\n",
    "    r_forces=True,\n",
    "    r_distances=False,\n",
    "    r_edges=True,\n",
    "    r_fixed=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_objects = a2g.convert_all(raw_data, disable_tqdm=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Data(atomic_numbers=[14], cell=[1, 3, 3], cell_offsets=[636, 3], edge_index=[2, 636], fixed=[14], force=[14, 3], natoms=14, pos=[14, 3], y=3.989314410668539)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data_objects[0]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([29., 29., 29., 29., 29., 29., 29., 29., 29., 29., 29., 29.,  8.,  6.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.atomic_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 5.1053,  0.0000,  0.0000],\n",
       "         [ 0.0000,  5.1053,  0.0000],\n",
       "         [ 0.0000,  0.0000, 32.6100]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1,  2,  2,  ...,  4,  6,  3],\n",
       "        [ 0,  0,  0,  ..., 13, 13, 13]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.edge_index #neighbor idx, source idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([45., 45., 45., 46., 49., 49., 49., 49., 50., 49., 49., 50., 26., 35.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from torch_geometric.utils import degree\n",
    "# Degree corresponds to the number of neighbors a given node has. Note there is no more than max_neigh neighbors for\n",
    "# any given node.\n",
    "\n",
    "degree(data.edge_index[1]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.fixed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 9.9356e-16,  4.5465e-15,  1.1354e-01],\n",
       "        [ 2.6749e-15,  3.7696e-15,  1.1344e-01],\n",
       "        [ 8.4481e-16,  2.7062e-16,  1.1344e-01],\n",
       "        [-6.6623e-18,  6.6196e-17,  1.1294e-01],\n",
       "        [-8.5221e-03, -8.5221e-03, -1.1496e-02],\n",
       "        [ 8.5221e-03, -8.5221e-03, -1.1496e-02],\n",
       "        [-8.5221e-03,  8.5221e-03, -1.1496e-02],\n",
       "        [ 8.5221e-03,  8.5221e-03, -1.1496e-02],\n",
       "        [ 1.9082e-17,  9.6277e-16, -1.0431e-01],\n",
       "        [-2.0583e-15, -4.3021e-16, -6.6610e-02],\n",
       "        [-5.5511e-17, -2.3592e-15, -6.6610e-02],\n",
       "        [-2.9409e-17, -4.3038e-15, -3.3250e-01],\n",
       "        [ 3.3204e-19,  6.7763e-21, -3.4247e-01],\n",
       "        [-4.5103e-17, -5.2042e-17,  5.0512e-01]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.force"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.0000,  0.0000, 13.0000],\n",
       "        [ 2.5527,  0.0000, 13.0000],\n",
       "        [ 0.0000,  2.5527, 13.0000],\n",
       "        [ 2.5527,  2.5527, 13.0000],\n",
       "        [ 1.2763,  1.2763, 14.8050],\n",
       "        [ 3.8290,  1.2763, 14.8050],\n",
       "        [ 1.2763,  3.8290, 14.8050],\n",
       "        [ 3.8290,  3.8290, 14.8050],\n",
       "        [ 0.0000,  0.0000, 16.6100],\n",
       "        [ 2.5527,  0.0000, 16.6100],\n",
       "        [ 0.0000,  2.5527, 16.6100],\n",
       "        [ 2.5527,  2.5527, 16.6100],\n",
       "        [ 2.5527,  2.5527, 19.6100],\n",
       "        [ 2.5527,  2.5527, 18.4597]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.989314410668539"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding additional info to your Data objects\n",
    "\n",
    "In addition to the above information, the OCP repo requires several other pieces of information for your data to work\n",
    "with the provided trainers:\n",
    "\n",
    "- sid (int): A unique identifier for a particular system. Does not affect your model performance, used for prediction saving \n",
    "- fid (int) (S2EF only): If training for the S2EF task, your data must also contain a unique frame identifier for atoms objects coming from the same system.\n",
    "- tags (tensor): Tag information - 0 for adsorbate, 1 for surface, 2 for subsurface. Optional, can be used for training.\n",
    "\n",
    "\n",
    "Other information may be added her as well if you choose to incorporate other information in your models/frameworks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_objects = []\n",
    "for idx, system in enumerate(raw_data):\n",
    "    data = a2g.convert(system)\n",
    "    data.fid = idx\n",
    "    data.sid = 0 # All data points come from the same system, arbitrarly define this as 0\n",
    "    data_objects.append(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Data(atomic_numbers=[14], cell=[1, 3, 3], cell_offsets=[636, 3], edge_index=[2, 636], fid=100, fixed=[14], force=[14, 3], natoms=14, pos=[14, 3], sid=0, y=3.968355893395719)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data_objects[100]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.sid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.fid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Resources:\n",
    "\n",
    "- https://github.com/Open-Catalyst-Project/ocp/blob/6604e7130ea41fabff93c229af2486433093e3b4/ocpmodels/preprocessing/atoms_to_graphs.py\n",
    "- https://github.com/Open-Catalyst-Project/ocp/blob/master/scripts/preprocess_ef.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ocp-models",
   "language": "python",
   "name": "ocp-models"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}