Skip to content

Commit

Permalink
examples/LiFePO4-NEB.ipynb and document and link ref for PBE barrier …
Browse files Browse the repository at this point in the history
…heights
  • Loading branch information
janosh committed Dec 11, 2023
1 parent 03aaa07 commit f8e6edf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 67 deletions.
107 changes: 40 additions & 67 deletions examples/LiFePO4-NEB.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Prepare-NEB-end-structures\" data-toc-modified-id=\"Prepare-NEB-end-structures-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Prepare NEB end structures</a></span><ul class=\"toc-item\"><li><span><a href=\"#Download-from-Materials-Project-and-create-supercell\" data-toc-modified-id=\"Download-from-Materials-Project-and-create-supercell-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Download from Materials Project and create supercell</a></span></li><li><span><a href=\"#Relax-supercells-with-M3GNet-DIRECT,-M3GNet-MS,-and-CHGNet\" data-toc-modified-id=\"Relax-supercells-with-M3GNet-DIRECT,-M3GNet-MS,-and-CHGNet-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Relax supercells with M3GNet-DIRECT, M3GNet-MS, and CHGNet</a></span></li><li><span><a href=\"#Create-and-relax-NEB-end-structures----b-and-c-directions\" data-toc-modified-id=\"Create-and-relax-NEB-end-structures----b-and-c-directions-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>Create and relax NEB end structures -- b and c directions</a></span></li></ul></li><li><span><a href=\"#NEB-calculations-with-M3GNet-DIRECT,-M3GNet-MS,-and-CHGNet\" data-toc-modified-id=\"NEB-calculations-with-M3GNet-DIRECT,-M3GNet-MS,-and-CHGNet-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>NEB calculations with M3GNet-DIRECT, M3GNet-MS, and CHGNet</a></span><ul class=\"toc-item\"><li><span><a href=\"#generate-NEB-images-from-end-structures-and-conduct-NEB\" data-toc-modified-id=\"generate-NEB-images-from-end-structures-and-conduct-NEB-2.1\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>generate NEB images from end structures and conduct NEB</a></span></li><li><span><a href=\"#analyze-and-plot-NEB-results\" data-toc-modified-id=\"analyze-and-plot-NEB-results-2.2\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>analyze and plot NEB results</a></span></li><li><span><a href=\"#Store-NEB-images-in-one-cif-file-for-visualization\" data-toc-modified-id=\"Store-NEB-images-in-one-cif-file-for-visualization-2.3\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>Store NEB images in one cif file for visualization</a></span></li><li><span><a href=\"#Visualize-NEB-path-(snapshots-of-VESTA-visualization-of-path_final.cif)\" data-toc-modified-id=\"Visualize-NEB-path-(snapshots-of-VESTA-visualization-of-path_final.cif)-2.4\"><span class=\"toc-item-num\">2.4&nbsp;&nbsp;</span>Visualize NEB path (snapshots of VESTA visualization of path_final.cif)</a></span></li></ul></li></ul></div>\n"
]
},
{
"cell_type": "markdown",
"id": "a97a66b3",
"metadata": {},
"source": [
"DFT barrier heights: path b = 0.27 eV and path c = 2.5 eV. (see table 1 in https://doi.org/10.1039/C5TA05062F)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -24,7 +32,7 @@
"\n",
"from matcalc.util import get_universal_calculator\n",
"\n",
"mpr = MPRester(\"<MaterialsProject-api>\")"
"mpr = MPRester(\"mp-api-key\")"
]
},
{
Expand Down Expand Up @@ -75,50 +83,17 @@
"## Relax supercells with M3GNet-DIRECT, M3GNet-MS, and CHGNet\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99bf513b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CHGNet v0.3.0 initialized with 412,525 parameters\n",
"CHGNet will run on cpu\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/qiji/repos/matgl/src/matgl/apps/pes.py:59: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" self.element_refs = AtomRef(property_offset=torch.tensor(element_refs, dtype=matgl.float_th))\n",
"/Users/qiji/repos/matgl/src/matgl/apps/pes.py:65: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" self.register_buffer(\"data_mean\", torch.tensor(data_mean, dtype=matgl.float_th))\n",
"/Users/qiji/repos/matgl/src/matgl/apps/pes.py:66: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" self.register_buffer(\"data_std\", torch.tensor(data_std, dtype=matgl.float_th))\n"
]
}
],
"source": [
"calculator_M3GNet_DIRECT = get_universal_calculator(\"M3GNet-MP-2021.2.8-DIRECT-PES\")\n",
"calculator_M3GNet_MS = get_universal_calculator(\"M3GNet-MP-2021.2.8-PES\")\n",
"calculator_CHGNet = get_universal_calculator(\"CHGNet\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2368537",
"metadata": {},
"outputs": [],
"source": [
"UP_cals = {\n",
" \"M3GNet-DIRECT\": calculator_M3GNet_DIRECT,\n",
" \"M3GNet-MS\": calculator_M3GNet_MS,\n",
" \"CHGNet\": calculator_CHGNet,\n",
"models = {\n",
" \"M3GNet-DIRECT\": get_universal_calculator(\"M3GNet-MP-2021.2.8-DIRECT-PES\"),\n",
" \"M3GNet-MS\": get_universal_calculator(\"M3GNet-MP-2021.2.8-PES\"),\n",
" \"CHGNet\": get_universal_calculator(\"CHGNet\"),\n",
"}"
]
},
Expand Down Expand Up @@ -150,12 +125,10 @@
"source": [
"%%time\n",
"results = {}\n",
"for UP_label, UP_cal in UP_cals.items():\n",
" relaxer = RelaxCalc(UP_cal, optimizer=\"BFGS\", relax_cell=True, fmax=0.02)\n",
"for model_name, model in models.items():\n",
" relaxer = RelaxCalc(model, optimizer=\"BFGS\", relax_cell=True, fmax=0.02)\n",
" supercell_LFPO_relaxed = relaxer.calc(s_LFPO)['final_structure']\n",
" results[UP_label] = {\n",
" \"supercell_LFPO\": supercell_LFPO_relaxed\n",
" }"
" results[model_name] = {\"supercell_LFPO\": supercell_LFPO_relaxed}"
]
},
{
Expand Down Expand Up @@ -183,9 +156,9 @@
],
"source": [
"%%time\n",
"for UP_label, UP_cal in UP_cals.items():\n",
" relaxer = RelaxCalc(UP_cal, optimizer=\"BFGS\", relax_cell=False, fmax=0.02)\n",
" supercell_LFPO_relaxed = results[UP_label][\"supercell_LFPO\"]\n",
"for model_name, model in models.items():\n",
" relaxer = RelaxCalc(model, optimizer=\"BFGS\", relax_cell=False, fmax=0.02)\n",
" supercell_LFPO_relaxed = results[model_name][\"supercell_LFPO\"]\n",
"\n",
" # NEB path along b and c directions have the same starting image.\n",
" s_LFPO_end_b = supercell_LFPO_relaxed.copy()\n",
Expand All @@ -197,7 +170,7 @@
" s_LFPO_start_bc = supercell_LFPO_relaxed.copy()\n",
" s_LFPO_start_bc.remove_sites([5])\n",
" s_LFPO_start_bc_relaxed = relaxer.calc(s_LFPO_start_bc)['final_structure']\n",
" results[UP_label].update(\n",
" results[model_name].update(\n",
" {\n",
" \"supercell_LFPO_end_b\":s_LFPO_end_b_relaxed,\n",
" \"supercell_LFPO_end_c\":s_LFPO_end_c_relaxed,\n",
Expand Down Expand Up @@ -453,19 +426,19 @@
],
"source": [
"%%time\n",
"for NEB_path in ['b', 'c']:\n",
" for UP_label, UP_cal in UP_cals.items():\n",
"for neb_path in 'bc':\n",
" for model_name, model in models.items():\n",
" NEBcalc = NEBCalc.from_end_images(\n",
" start_struct= results[UP_label][\"supercell_LFPO_start_bc\"],\n",
" end_struct= results[UP_label][f\"supercell_LFPO_end_{NEB_path}\"],\n",
" calculator= UP_cal,\n",
" start_struct=results[model_name][\"supercell_LFPO_start_bc\"],\n",
" end_struct=results[model_name][f\"supercell_LFPO_end_{neb_path}\"],\n",
" calculator=model,\n",
" n_images=7,\n",
" climb=True,\n",
" traj_folder=f\"NEB_data/traj_{NEB_path}_{UP_label}/\"\n",
" traj_folder=f\"NEB_data/traj_{neb_path}_{model_name}/\"\n",
" )\n",
" barrier = NEBcalc.calc(fmax=0.05)[0]\n",
" results[UP_label][f\"NEB_{NEB_path}\"] = NEBcalc.neb\n",
" print(f\"Barrier along {NEB_path} by {UP_label}: {barrier} eV.\")"
" results[model_name][f\"NEB_{neb_path}\"] = NEBcalc.neb\n",
" print(f\"Barrier along {neb_path} by {model_name}: {barrier} eV.\")"
]
},
{
Expand Down Expand Up @@ -597,10 +570,10 @@
"%%time\n",
"import matplotlib.pyplot as plt\n",
"\n",
"for NEB_path in ['b', 'c']:\n",
" for UP_label, UP_cal in UP_cals.items():\n",
" NEB_tool = NEBTools(results[UP_label][f\"NEB_{NEB_path}\"].images)\n",
" print(f\"Path along {NEB_path}, {UP_label}: \")\n",
"for neb_path in 'bc':\n",
" for model_name, model in models.items():\n",
" NEB_tool = NEBTools(results[model_name][f\"NEB_{neb_path}\"].images)\n",
" print(f\"Path along {neb_path}, {model_name}: \")\n",
" fig = NEB_tool.plot_band()\n",
" plt.show()"
]
Expand Down Expand Up @@ -630,9 +603,9 @@
" \"\"\"Generate a cif file from a list of image atoms.\"\"\"\n",
" image_structs = list(map(AseAtomsAdaptor().get_structure, images))\n",
" sites = set()\n",
" lat = image_structs[0].lattice\n",
" lattice = image_structs[0].lattice\n",
" for site in chain(*(struct for struct in image_structs)):\n",
" sites.add(PeriodicSite(site.species, site.frac_coords, lat))\n",
" sites.add(PeriodicSite(site.species, site.frac_coords, lattice))\n",
" neb_path = Structure.from_sites(sorted(sites))\n",
" neb_path.to(filename, \"cif\")"
]
Expand All @@ -654,10 +627,10 @@
],
"source": [
"%%time\n",
"for NEB_path in ['b', 'c']:\n",
" for UP_label, UP_cal in UP_cals.items():\n",
" NEB_tool = NEBTools(results[UP_label][f\"NEB_{NEB_path}\"].images)\n",
" generate_path_cif_from_images(NEB_tool.images, f\"NEB_data/traj_{NEB_path}_{UP_label}/path_final.cif\")"
"for neb_path in 'bc':\n",
" for model_name, model in models.items():\n",
" NEB_tool = NEBTools(results[model_name][f\"NEB_{neb_path}\"].images)\n",
" generate_path_cif_from_images(NEB_tool.images, f\"NEB_data/traj_{neb_path}_{model_name}/path_final.cif\")"
]
},
{
Expand Down Expand Up @@ -705,7 +678,7 @@
],
"source": [
"print(\"Final path b by M3GNet-DIRECT:\")\n",
"Image(\"NEB_data/traj_b_M3GNet-DIRECT/path_b.png\")"
"Image(\"NEB_data/M3GNet-DIRECT-path-b.png\")"
]
},
{
Expand Down Expand Up @@ -735,7 +708,7 @@
],
"source": [
"print(\"Final path c by M3GNet-DIRECT:\")\n",
"Image(\"NEB_data/traj_c_M3GNet-DIRECT/path_c.png\")"
"Image(\"NEB_data/M3GNet-DIRECT-path-c.png\")"
]
}
],
Expand Down
File renamed without changes
File renamed without changes

0 comments on commit f8e6edf

Please sign in to comment.