Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy matgenb repo into new pymatgen examples/ folder #3811

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
920 changes: 920 additions & 0 deletions examples/2013-01-01-Bandstructure of NiO.ipynb

Large diffs are not rendered by default.

489 changes: 489 additions & 0 deletions examples/2013-01-01-Basic functionality.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook demonstrates how you can calculate reaction energies using the Materials API and pymatgen."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the introductory text clearly explains the purpose and scope of the notebook.

-    "This notebook demonstrates how you can calculate reaction energies using the Materials API and pymatgen."
+    "This notebook demonstrates how to calculate reaction energies using the Materials API and pymatgen, providing a practical example for users."

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"This notebook demonstrates how you can calculate reaction energies using the Materials API and pymatgen."
"This notebook demonstrates how to calculate reaction energies using the Materials API and pymatgen, providing a practical example for users."

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n",
"# !pip install pymatgen==2022.7.19"
]
Comment on lines +16 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the necessity of the commented installation command for dependencies.

-    "# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n",
-    "# !pip install pymatgen==2022.7.19"
+    "# If running in Google Colab, uncomment the following line to install the required pymatgen version.\n",
+    "# !pip install pymatgen==2022.7.19"

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n",
"# !pip install pymatgen==2022.7.19"
]
"# If running in Google Colab, uncomment the following line to install the required pymatgen version.\n",
"# !pip install pymatgen==2022.7.19"
]

},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Caculated\n",
"CaO + CO2 -> CaCO3\n",
"Reaction energy = -146.67299148102813 kJ mol^-1\n",
"Experimental\n",
"CaO + CO2 -> CaCO3\n",
"Reaction energy = -178.30000000000064 kJ mol^-1\n"
]
}
],
"source": [
"from pymatgen.analysis.reaction_calculator import ComputedReaction\n",
"from pymatgen.core import Composition\n",
"from pymatgen.core.units import FloatWithUnit\n",
"from pymatgen.entries.computed_entries import ComputedEntry\n",
"from pymatgen.ext.matproj import MPRester\n",
"\n",
"# This initializes the REST adaptor. Put your own API key in if necessary.\n",
"mpr = MPRester()\n",
"\n",
"# This gets all entries belonging to the Ca-C-O system.\n",
"all_entries = mpr.get_entries_in_chemsys([\"Ca\", \"C\", \"O\"])\n",
"\n",
"\n",
"# This method simply gets the lowest energy entry for all entry with the same composition.\n",
"def get_most_stable_entry(formula):\n",
" relevant_entries = [\n",
" entry\n",
" for entry in all_entries\n",
" if entry.composition.reduced_formula == Composition(formula).reduced_formula\n",
" ]\n",
" relevant_entries = sorted(relevant_entries, key=lambda e: e.energy_per_atom)\n",
" return relevant_entries[0]\n",
"\n",
"\n",
"CaO = get_most_stable_entry(\"CaO\")\n",
"CO2 = get_most_stable_entry(\"CO2\")\n",
"CaCO3 = get_most_stable_entry(\"CaCO3\")\n",
"\n",
"reaction = ComputedReaction([CaO, CO2], [CaCO3])\n",
"energy = FloatWithUnit(reaction.calculated_reaction_energy, \"eV atom^-1\")\n",
"\n",
"print(\"Caculated\")\n",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the typo in the output text for better clarity.

-    "print(\"Caculated\")\n",
+    "print(\"Calculated\")\n",

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"print(\"Caculated\")\n",
"print(\"Calculated\")\n",

"print(reaction)\n",
"print(\"Reaction energy = {}\".format(energy.to(\"kJ mol^-1\")))\n",
"print\n",
"\n",
"# The following portions demonstrate how to get the experimental values as well.\n",
"exp_CaO = mpr.get_exp_entry(\"CaO\")\n",
"exp_CaCO3 = mpr.get_exp_entry(\"CaCO3\")\n",
"\n",
"# Unfortunately, the Materials Project database does not have gas phase experimental entries. This is the value from NIST. We manually create the entry.\n",
"# Exp entries should be in kJ/mol.\n",
"exp_CO2 = ComputedEntry(\"CO2\", -393.51)\n",
"\n",
"exp_reaction = ComputedReaction([exp_CaO, exp_CO2], [exp_CaCO3])\n",
"\n",
"print(\"Experimental\")\n",
"print(exp_reaction)\n",
"print(f\"Reaction energy = {exp_reaction.calculated_reaction_energy} kJ mol^-1\")"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3.10.5 64-bit",
"language": "python",
"name": "python3"
},
"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.10.5"
},
"vscode": {
"interpreter": {
"hash": "8022b3e932e045c760cb4633b91dd1cb8bc60d104ca9808334cbd1645adbe837"
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
195 changes: 195 additions & 0 deletions examples/2013-01-01-Calculating XRD patterns.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction\n",
"\n",
"Pymatgen supports reading of most common file formats, including the Crystallographic Information File and various input and output files of computational codes like VASP. However, it is often easier and quicker to directly query for structures from online sources. Though private databases such as the Inorganic Crystal Structure Database are not open, there are open sources such as the Materials Project and the Crystallographic Open Database (COD) where one can obtain crystal structures."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n",
"# !pip install pymatgen==2022.7.19"
]
Comment on lines +18 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a conditional check to ensure that the dependencies are only installed if not already present. This can prevent unnecessary reinstallation and save time during execution.

- # Uncomment the subsequent lines in this cell to install dependencies for Google Colab.
- # !pip install pymatgen==2022.7.19
+ # Uncomment the subsequent lines in this cell to install dependencies for Google Colab if they are not already installed.
+ # !pip install pymatgen==2022.7.19 --upgrade --quiet

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n",
"# !pip install pymatgen==2022.7.19"
]
"# Uncomment the subsequent lines in this cell to install dependencies for Google Colab if they are not already installed.\n",
"# !pip install pymatgen==2022.7.19 --upgrade --quiet"

},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Materials Project\n",
"\n",
"Pymatgen contains a high-level interface to the Materials Project, which can be used to query for structures very easily."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymatgen.ext.matproj import MPRester\n",
"\n",
"# Note that you will need to add your Materials API key in your .pmgrc.yaml file as \"PMG_MAPI_KEY\".\n",
"# Alternatively, you will need to supply the API key as an arg to MPRester.\n",
"mpr = MPRester()"
Comment on lines +39 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the API key is not hardcoded in the notebook for security reasons. It's good practice to load sensitive information like API keys from environment variables or configuration files.

- # Note that you will need to add your Materials API key in your .pmgrc.yaml file as "PMG_MAPI_KEY".
- # Alternatively, you will need to supply the API key as an arg to MPRester.
- mpr = MPRester()
+ # Ensure your Materials API key is set in the environment variables or a configuration file for security.
+ import os
+ mpr = MPRester(os.getenv('PMG_MAPI_KEY'))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"# Note that you will need to add your Materials API key in your .pmgrc.yaml file as \"PMG_MAPI_KEY\".\n",
"# Alternatively, you will need to supply the API key as an arg to MPRester.\n",
"mpr = MPRester()"
"# Ensure your Materials API key is set in the environment variables or a configuration file for security.\n",
"import os\n",
"mpr = MPRester(os.getenv('PMG_MAPI_KEY'))"

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Structure Summary\n",
"Lattice\n",
" abc : 3.2910717923597561 3.2910718996250861 3.2910720568557887\n",
" angles : 60.129710432884849 60.129709521376753 60.129703130390972\n",
" volume : 25.279668381289056\n",
" A : 2.91738857 0.097894369999999994 1.5200046599999999\n",
" B : 0.96463405999999996 2.7550356100000002 1.5200046599999999\n",
" C : 0.13320635 0.097894430000000004 3.28691771\n",
"PeriodicSite: O (0.0000, 0.0000, 0.0000) [0.0000, 0.0000, 0.0000]\n",
"PeriodicSite: Li (3.0121, 2.2136, 4.7463) [0.7502, 0.7502, 0.7502]\n",
"PeriodicSite: Li (1.0031, 0.7372, 1.5806) [0.2498, 0.2498, 0.2498], Structure Summary\n",
"Lattice\n",
" abc : 5.1517948200000001 3.1404278300000001 5.9334081599999999\n",
" angles : 90.0 90.0 90.0\n",
" volume : 95.995660249910003\n",
" A : 5.1517948200000001 0.0 0.0\n",
" B : 0.0 3.1404278300000001 0.0\n",
" C : 0.0 0.0 5.9334081599999999\n",
"PeriodicSite: Li (0.0631, 0.7851, 0.9438) [0.0122, 0.2500, 0.1591]\n",
"PeriodicSite: Li (0.7268, 0.7851, 3.4367) [0.1411, 0.2500, 0.5792]\n",
"PeriodicSite: Li (1.8490, 2.3553, 0.4700) [0.3589, 0.7500, 0.0792]\n",
"PeriodicSite: Li (2.5128, 2.3553, 3.9105) [0.4878, 0.7500, 0.6591]\n",
"PeriodicSite: Li (2.6390, 0.7851, 2.0229) [0.5122, 0.2500, 0.3409]\n",
"PeriodicSite: Li (3.3027, 0.7851, 5.4634) [0.6411, 0.2500, 0.9208]\n",
"PeriodicSite: Li (4.4249, 2.3553, 2.4967) [0.8589, 0.7500, 0.4208]\n",
"PeriodicSite: Li (5.0887, 2.3553, 4.9896) [0.9878, 0.7500, 0.8409]\n",
"PeriodicSite: O (1.2677, 2.3553, 2.3664) [0.2461, 0.7500, 0.3988]\n",
"PeriodicSite: O (1.3082, 0.7851, 5.3331) [0.2539, 0.2500, 0.8988]\n",
"PeriodicSite: O (3.8436, 2.3553, 0.6003) [0.7461, 0.7500, 0.1012]\n",
"PeriodicSite: O (3.8841, 0.7851, 3.5670) [0.7539, 0.2500, 0.6012]]\n"
]
}
],
"source": [
"# Querying by formula only.\n",
"structures = mpr.get_structures(\"Li2O\")\n",
"print(structures)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Li2 O4\n",
"Li2 O1\n",
"Li8 O4\n",
"Li4 O4\n",
"Li1 O3\n",
"Li16 O16\n"
]
}
],
"source": [
"# Querying by chemical system only.\n",
"structures = mpr.get_structures(\"Li-O\")\n",
"for s in structures:\n",
" print(s.formula)\n",
"# A number of Li-O structures are returned with different Li:O ratios."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## COD\n",
"\n",
"To obtain structures from COD by the COD id, you just need to know the id. However, most sophisticated searches require that you have installed mysql given that the COD does not support a proper REST API at this time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymatgen.ext.cod import COD\n",
"\n",
"cod = COD()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Full Formula (Li8 O4)\n",
"Reduced Formula: Li2O\n",
"abc : 4.610000 4.610000 4.610000\n",
"angles: 90.000000 90.000000 90.000000\n",
"Sites (12)\n",
" # SP a b c\n",
"--- ---- ---- ---- ----\n",
" 0 Li+ 0.25 0.25 0.25\n",
" 1 Li+ 0.25 0.75 0.75\n",
" 2 Li+ 0.75 0.25 0.75\n",
" 3 Li+ 0.75 0.75 0.25\n",
" 4 Li+ 0.75 0.75 0.75\n",
" 5 Li+ 0.75 0.25 0.25\n",
" 6 Li+ 0.25 0.75 0.25\n",
" 7 Li+ 0.25 0.25 0.75\n",
" 8 O2- 0 0 0\n",
" 9 O2- 0 0.5 0.5\n",
" 10 O2- 0.5 0 0.5\n",
" 11 O2- 0.5 0.5 0\n"
]
}
],
"source": [
"structure = cod.get_structure_by_id(1010064)\n",
"print(structure)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/shyue/repos/pymatgen/pymatgen/io/cif.py:801: UserWarning: LI parsed as L\n",
" warnings.warn(\"{} parsed as {}\".format(sym, v))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"COD ID: 1010064, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1011372, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 9009059, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 4311895, Formula: L8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514086, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514087, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514088, Formula: Li6 O3, Spacegroup: R -3 m :H\n",
"COD ID: 4121514, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 4121515, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514092, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514093, Formula: Li7.84 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514094, Formula: Li7.9984 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514095, Formula: Li8.0008 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514096, Formula: Li8 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514097, Formula: Li8.0008 O4, Spacegroup: F m -3 m\n",
"COD ID: 1514098, Formula: Li8 O4, Spacegroup: F m -3 m\n"
]
}
],
"source": [
"structures = cod.get_structure_by_formula(\"Li2O\")\n",
"for d in structures:\n",
" print(\n",
" \"COD ID: %d, Formula: %s, Spacegroup: %s\"\n",
" % (d[\"cod_id\"], d[\"structure\"].formula, d[\"sg\"])\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Full Formula (Li8 O4)\n",
"Reduced Formula: Li2O\n",
"abc : 4.610000 4.610000 4.610000\n",
"angles: 90.000000 90.000000 90.000000\n",
"Sites (12)\n",
" # SP a b c\n",
"--- ---- ---- ---- ----\n",
" 0 Li+ 0.25 0.25 0.25\n",
" 1 Li+ 0.25 0.75 0.75\n",
" 2 Li+ 0.75 0.25 0.75\n",
" 3 Li+ 0.75 0.75 0.25\n",
" 4 Li+ 0.75 0.75 0.75\n",
" 5 Li+ 0.75 0.25 0.25\n",
" 6 Li+ 0.25 0.75 0.25\n",
" 7 Li+ 0.25 0.25 0.75\n",
" 8 O2- 0 0 0\n",
" 9 O2- 0 0.5 0.5\n",
" 10 O2- 0.5 0 0.5\n",
" 11 O2- 0.5 0.5 0\n"
]
}
],
"source": [
"print(structures[0][\"structure\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading