{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"rm: trajectory.xyz: No such file or directory\n"
]
}
],
"source": [
"!rm trajectory.xyz\n",
"!rm -rf phonons\n",
"!pip install graph-pes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ASE\n",
"\n",
"> **FYI**, you can open this documentation as a [Google Colab notebook](https://colab.research.google.com/github/jla-gardner/graph-pes/blob/main/docs/source/quickstart/quickstart.ipynb) to follow along interactively\n",
"\n",
"Any [GraphPESModel](https://jla-gardner.github.io/graph-pes/models/index.html) can be used as an [ase.calculators.calculator.Calculator](https://wiki.fysik.dtu.dk/ase/ase/calculators/calculator.html) by wrapping it in a [GraphPESCalculator](https://jla-gardner.github.io/graph-pes/tools/ase.html#graph_pes.utils.calculator.GraphPESCalculator). This allows the model to be used in any ASE workflow, such as geometry optimization, molecular dynamics, or phonon calculations (see below)."
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
".. autoclass:: graph_pes.utils.calculator.GraphPESCalculator\n",
" :members: calculate, calculate_all, cache_hit_rate, reset_cache_stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Single point calculations\n",
"\n",
"First we instantiate a calculator from a simple [LennardJones](https://jla-gardner.github.io/graph-pes/models/pairwise.html#graph_pes.models.LennardJones) model, but this can be replaced with any model that inherits from [GraphPESModel](https://jla-gardner.github.io/graph-pes/models/root.html):\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LennardJones(epsilon=0.14, sigma=2.27, cutoff=5.0)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from graph_pes.models import LennardJones\n",
"\n",
"# some-what arbitrary parameters for this model\n",
"model = LennardJones(epsilon=0.14, sigma=2.27)\n",
"model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from graph_pes.utils.calculator import GraphPESCalculator\n",
"\n",
"calculator = GraphPESCalculator(model)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Atoms(symbols='CH4', pbc=False),\n",
" Atoms(symbols='OH2', pbc=False),\n",
" Atoms(symbols='C2H6', pbc=False)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ase.build import molecule\n",
"\n",
"molecules = [molecule(s) for s in \"CH4 H2O C2H6\".split()]\n",
"molecules"
]
},
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"`GraphPESCalculator`\\ s behave like normal `ase.calculators.calculator.Calculator` objects:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[14828.7490234375, 30633.576171875, 21518.35546875]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[calculator.get_potential_energy(m) for m in molecules]"
]
},
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"For accelerated predictions via batching, feel free to also use `GraphPESCalculator.calculate_all`:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'energy': 14828.7490234375},\n",
" {'energy': 30633.576171875},\n",
" {'energy': 21518.35546875}]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculator.calculate_all(molecules, properties=[\"energy\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Merging the results of these predictions is provided by `merge_predictions`:"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"\n",
"\n",
".. autofunction:: graph_pes.utils.calculator.merge_predictions\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'energy': array([14828.74902344, 30633.57617188, 21518.35546875]),\n",
" 'forces': array([[-0.0000000e+00, -0.0000000e+00, -0.0000000e+00],\n",
" [ 2.3727711e+04, 2.3727711e+04, 2.3727711e+04],\n",
" [-2.3727711e+04, -2.3727711e+04, 2.3727711e+04],\n",
" [ 2.3727711e+04, -2.3727711e+04, -2.3727711e+04],\n",
" [-2.3727711e+04, 2.3727711e+04, -2.3727711e+04],\n",
" [-0.0000000e+00, -0.0000000e+00, 2.3391900e+05],\n",
" [-0.0000000e+00, 1.5019208e+05, -1.1695950e+05],\n",
" [-0.0000000e+00, -1.5019208e+05, -1.1695950e+05],\n",
" [-0.0000000e+00, -1.9921422e-01, -4.2235555e+04],\n",
" [-0.0000000e+00, 1.9988680e-01, 4.2235555e+04],\n",
" [-0.0000000e+00, 3.6874703e+04, 1.4250874e+04],\n",
" [-3.1934238e+04, -1.8437250e+04, 1.4250783e+04],\n",
" [ 3.1934238e+04, -1.8437250e+04, 1.4250783e+04],\n",
" [-0.0000000e+00, -3.6874699e+04, -1.4250874e+04],\n",
" [-3.1934238e+04, 1.8437250e+04, -1.4250783e+04],\n",
" [ 3.1934238e+04, 1.8437250e+04, -1.4250783e+04]], dtype=float32)}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from graph_pes.utils.calculator import merge_predictions\n",
"\n",
"single_predictions = calculator.calculate_all(molecules)\n",
"merge_predictions(single_predictions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Phonon calculations\n",
"\n",
"See [the phonons section](https://wiki.fysik.dtu.dk/ase/ase/phonons.html) of the ASE documentation for more details."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Atoms(symbols='Cu4', pbc=True, cell=[3.61, 3.61, 3.61])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ase.build import bulk\n",
"\n",
"structure = bulk('Cu', cubic=True)\n",
"structure"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"