-
Notifications
You must be signed in to change notification settings - Fork 402
Formation energy calculator #1538
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
Open
lbluque
wants to merge
17
commits into
main
Choose a base branch
from
formation-energy-calculator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−123
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5a823db
generalize hf reference download
lbluque d5a76ef
initial implementation of formation energy calc
lbluque 17e0871
use runtime re-binding instead of a new class
lbluque b747b90
add corrections for omat
lbluque adbbf55
add formation element reference loading
lbluque 4ca5f66
fix HFCheckpoint attrs
lbluque 61a0b93
set_predict_formation_energy function
lbluque 097cc72
merge upstream
lbluque 612a62f
tests
lbluque 2687a14
predict formation energies in inorganic bulk tutorial
lbluque 542dad2
remove test
lbluque b6783d3
fix correct total energy
lbluque 73a7e8c
add more tests
lbluque 2b0a525
fix tests
lbluque 63aafbf
reset calculator cash
lbluque c47a9e9
reset cache after calculation
lbluque 460acdc
add MP value to tutorial
lbluque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
docs/inorganic_materials/examples_tutorials/bulk_stability.md
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
docs/inorganic_materials/examples_tutorials/formation_energy.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| --- | ||
| jupytext: | ||
| text_representation: | ||
| extension: .md | ||
| format_name: myst | ||
| format_version: 0.13 | ||
| jupytext_version: 1.17.1 | ||
| kernelspec: | ||
| display_name: Python 3 (ipykernel) | ||
| language: python | ||
| name: python3 | ||
| --- | ||
|
|
||
| Formation energies | ||
| ------------------ | ||
|
|
||
| We're going to start simple here - let's run a local relaxation (optimize the unit cell and positions) using a pre-trained UMA model to compute formation energies for inorganic materials. | ||
|
|
||
| Note predicting formation energy using models that models trained solely on OMat24 must use OMat24 compatible references and corrections for mixing PBE and PBE+U calculations. We use MP2020-style corrections fitted to OMat24 DFT calculations. For more information see the [documentation](https://docs.materialsproject.org/methodology/materials-methodology/thermodynamic-stability/thermodynamic-stability/anion-and-gga-gga+u-mixing) at the Materials Project. The necessary references can be found using the `fairchem.data.omat` package! | ||
|
|
||
| ````{admonition} Need to install fairchem-core or get UMA access or getting permissions/401 errors? | ||
| :class: dropdown | ||
|
|
||
|
|
||
| 1. Install the necessary packages using pip, uv etc | ||
| ```{code-cell} ipython3 | ||
| :tags: [skip-execution] | ||
|
|
||
| ! pip install fairchem-core fairchem-data-omat | ||
| ``` | ||
|
|
||
| 2. Get access to any necessary huggingface gated models | ||
| * Get and login to your Huggingface account | ||
| * Request access to https://huggingface.co/facebook/UMA | ||
| * Create a Huggingface token at https://huggingface.co/settings/tokens/ with the permission "Permissions: Read access to contents of all public gated repos you can access" | ||
| * Add the token as an environment variable using `huggingface-cli login` or by setting the HF_TOKEN environment variable. | ||
|
|
||
| ```{code-cell} ipython3 | ||
| :tags: [skip-execution] | ||
|
|
||
| # Login using the huggingface-cli utility | ||
| ! huggingface-cli login | ||
|
|
||
| # alternatively, | ||
| import os | ||
| os.environ['HF_TOKEN'] = 'MY_TOKEN' | ||
| ``` | ||
| ```` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| from __future__ import annotations | ||
|
|
||
| import pprint | ||
|
|
||
| from ase.build import bulk | ||
| from ase.optimize import LBFGS | ||
| from quacc.recipes.mlp.core import relax_job | ||
|
|
||
| from fairchem.core.calculate.ase_calculator import FAIRChemCalculator, set_predict_formation_energy | ||
|
|
||
| # Make an Atoms object of a bulk MgO structure | ||
| atoms = bulk("MgO", "rocksalt", a=4.213) | ||
|
|
||
| # Run a structure relaxation | ||
| result = relax_job( | ||
| atoms, | ||
| method="fairchem", | ||
| name_or_path="uma-s-1p1", | ||
| task_name="omat", | ||
| relax_cell=True, | ||
| opt_params={"fmax": 1e-3, "optimizer": FIRE}, | ||
| ) | ||
|
|
||
| # Get the realxed atoms! | ||
| atoms = result["atoms"] | ||
|
|
||
| # Create an calculator using uma-s-1p1 | ||
| calculator = FAIRChemCalculator.from_model_checkpoint("uma-s-1p1", task_name="omat") | ||
| atoms.calc = calculator | ||
|
|
||
| # Adapt the calculation to automatically return MP-style corrected formation energies | ||
| # For the omat task, this defaults to apply MP2020 style corrections with OMat24 compatibility | ||
| with set_predict_formation_energy(atoms.calc, apply_corrections=True): | ||
| form_energy = atoms.get_potential_energy() | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| pprint.pprint(f"Total energy: {result["results"]["energy"] eV\n Formation energy {form_energy} eV}) | ||
| ``` | ||
|
|
||
| Compare the results to the value of [-3.038 eV/atom reported](https://next-gen.materialsproject.org/materials/mp-1265?chemsys=Mg-O#thermodynamic_stability) in the the Materials Project! | ||
| *Note that we expect differences due to the different DFT settings used to calculate the OMat24 training data.* | ||
|
|
||
| Congratulations; you ran your first relaxation and predicted the formation energy of MgO using UMA and `quacc`! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a numerical comparison to the MP value here? Just mention what MP says and include a link to the MP structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea!