-
Notifications
You must be signed in to change notification settings - Fork 74
Add CHGNet model support #288
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
brunocucco
wants to merge
13
commits into
TorchSim:main
Choose a base branch
from
brunocucco:feature/chgnetmodel
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.
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3ee36e6
Add CHGNet model support:
brunocucco 05691fd
Apply ruff formatting to CHGNet files
brunocucco 5abdcfe
Merge branch 'main' into feature/chgnetmodel
abhijeetgangan 60bbcf4
Merge branch 'main' into feature/chgnetmodel
abhijeetgangan 6527c47
Merge branch 'main' into feature/chgnetmodel
brunocucco b60a0c0
Apply ruff formatting to CHGNet files
brunocucco 95da328
Addressed comments and suggestions given by the reviewers
brunocucco 2ae60e8
Merge branch 'main' into feature/chgnetmodel
abhijeetgangan 99c9d8c
Merge branch 'main' into feature/chgnetmodel
orionarcher 388ef35
Merge branch 'main' into feature/chgnetmodel
orionarcher 35d1b3d
Fixes for chgnet:
brunocucco a2d230b
Merge branch 'main' into feature/chgnetmodel
abhijeetgangan 4c09f8c
Merge branch 'main' into feature/chgnetmodel
abhijeetgangan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| """CHGNet model example for TorchSim.""" | ||
|
|
||
| # /// script | ||
| # dependencies = ["chgnet>=0.4.2"] | ||
| # /// | ||
|
|
||
| import os | ||
| import warnings | ||
|
|
||
| import torch | ||
| from ase import Atoms | ||
| from ase.build import bulk | ||
|
|
||
| import torch_sim as ts | ||
| from torch_sim.models.chgnet import CHGNetModel | ||
|
|
||
|
|
||
| # Silence warnings | ||
| warnings.filterwarnings("ignore") | ||
| os.environ["TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD"] = "1" | ||
|
|
||
| # Set device | ||
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
| dtype = torch.float32 | ||
|
|
||
| print("CHGNet Example for TorchSim") | ||
| print("=" * 40) | ||
|
|
||
| # Create CHGNet model | ||
| model = CHGNetModel( | ||
| device=device, | ||
| dtype=dtype, | ||
| compute_forces=True, | ||
| compute_stress=True, | ||
| ) | ||
|
|
||
| # Create test systems | ||
| al_atoms = bulk("Al", "fcc", a=4.05, cubic=True) | ||
| c_atoms = bulk("C", "diamond", a=3.57, cubic=True) | ||
| mg_atoms = bulk("Mg", "hcp", a=3.21, c=5.21) | ||
| a_perovskite = 3.84 | ||
| ca_tio3_atoms = Atoms( | ||
| ["Ca", "Ti", "O", "O", "O"], | ||
| positions=[ | ||
| [0, 0, 0], | ||
| [a_perovskite / 2, a_perovskite / 2, a_perovskite / 2], | ||
| [a_perovskite / 2, 0, 0], | ||
| [0, a_perovskite / 2, 0], | ||
| [0, 0, a_perovskite / 2], | ||
| ], | ||
| cell=[a_perovskite, a_perovskite, a_perovskite], | ||
| pbc=True, | ||
| ) | ||
|
|
||
| # Convert to TorchSim state | ||
| state = ts.io.atoms_to_state([al_atoms, c_atoms, mg_atoms], device, dtype) | ||
|
|
||
| # Load MACE model for comparison | ||
| try: | ||
| from mace.calculators.foundations_models import mace_mp | ||
|
|
||
| from torch_sim.models.mace import MaceModel, MaceUrls | ||
|
|
||
| raw_mace_mp = mace_mp(model=MaceUrls.mace_mp_small, return_raw_model=True) | ||
| mace_model = MaceModel( | ||
| model=raw_mace_mp, | ||
| device=device, | ||
| dtype=dtype, | ||
| compute_forces=True, | ||
| compute_stress=True, | ||
| ) | ||
| mace_available = True | ||
| except ImportError: | ||
| mace_available = False | ||
| print("MACE not available for comparison") | ||
|
|
||
| # In this table we compare CHGNet and MACE on the equilibrium structures | ||
| print("\nTABLE 1: Equilibrium Structures") | ||
| print("=" * 80) | ||
| print( | ||
| f"{'System':<10} {'CHGNet E (eV)':<15} {'CHGNet F (eV/Å)':<15} " | ||
| f"{'MACE E (eV)':<15} {'MACE F (eV/Å)':<15}" | ||
| ) | ||
| print("-" * 80) | ||
|
|
||
| for i, system_name in enumerate(["Al", "C", "Mg"]): | ||
| # Get states | ||
| single_state = ts.io.atoms_to_state([[al_atoms, c_atoms, mg_atoms][i]], device, dtype) | ||
|
|
||
| # CHGNet results | ||
| chgnet_result = model.forward(single_state) | ||
| chgnet_energy = chgnet_result["energy"].item() | ||
| chgnet_force = torch.norm(chgnet_result["forces"], dim=1).max().item() | ||
|
|
||
| # MACE results | ||
| if mace_available: | ||
| mace_result = mace_model.forward(single_state) | ||
| mace_energy = mace_result["energy"].item() | ||
| mace_force = torch.norm(mace_result["forces"], dim=1).max().item() | ||
| print( | ||
| f"{system_name:<10} {chgnet_energy:<15.6f} {chgnet_force:<15.6f} " | ||
| f"{mace_energy:<15.6f} {mace_force:<15.6f}" | ||
| ) | ||
| else: | ||
| print( | ||
| f"{system_name:<10} {chgnet_energy:<15.6f} {chgnet_force:<15.6f} " | ||
| f"{'N/A':<15} {'N/A':<15}" | ||
| ) | ||
|
|
||
| # In this table we compare CHGNet and MACE on the displaced and optimized structures | ||
| print("\nTABLE 2: Displaced and Optimized Structures") | ||
| print("=" * 100) | ||
| print( | ||
| f"{'System':<10} {'CHGNet Init E':<15} {'CHGNet Fin E':<15} " | ||
| f"{'CHGNet Fin F':<15} {'MACE Init E':<15} {'MACE Fin E':<15} " | ||
| f"{'MACE Fin F':<15}" | ||
| ) | ||
| print("-" * 120) | ||
|
|
||
| for atoms, system_name in zip( | ||
| [al_atoms, c_atoms, ca_tio3_atoms], ["Al", "C", "CaTiO3"], strict=False | ||
| ): | ||
| # Create displaced state | ||
| single_state = ts.io.atoms_to_state([atoms], device, dtype) | ||
| displacement = torch.randn_like(single_state.positions) * 0.1 | ||
| displaced_state = single_state.clone() | ||
| displaced_state.positions = single_state.positions + displacement | ||
|
|
||
| # CHGNet optimization | ||
| chgnet_initial = model.forward(displaced_state) | ||
| chgnet_initial_energy = chgnet_initial["energy"].item() | ||
|
|
||
| chgnet_optimized = ts.optimize( | ||
| displaced_state, | ||
| model, | ||
| optimizer=ts.optimizers.Optimizer.fire, | ||
| max_steps=100, | ||
| ) | ||
|
|
||
| chgnet_final = model.forward(chgnet_optimized) | ||
| chgnet_final_energy = chgnet_final["energy"].item() | ||
| chgnet_final_force = torch.norm(chgnet_final["forces"], dim=1).max().item() | ||
|
|
||
| # MACE optimization | ||
| if mace_available: | ||
| mace_initial = mace_model.forward(displaced_state) | ||
| mace_initial_energy = mace_initial["energy"].item() | ||
|
|
||
| mace_optimized = ts.optimize( | ||
| displaced_state, | ||
| mace_model, | ||
| optimizer=ts.optimizers.Optimizer.fire, | ||
| max_steps=100, | ||
| ) | ||
|
|
||
| mace_final = mace_model.forward(mace_optimized) | ||
| mace_final_energy = mace_final["energy"].item() | ||
| mace_final_force = torch.norm(mace_final["forces"], dim=1).max().item() | ||
|
|
||
| print( | ||
| f"{system_name:<10} {chgnet_initial_energy:<15.6f} " | ||
| f"{chgnet_final_energy:<15.6f} {chgnet_final_force:<15.6f} " | ||
| f"{mace_initial_energy:<15.6f} {mace_final_energy:<15.6f} " | ||
| f"{mace_final_force:<15.6f}" | ||
| ) | ||
| else: | ||
| print( | ||
| f"{system_name:<10} {chgnet_initial_energy:<15.6f} " | ||
| f"{chgnet_final_energy:<15.6f} {chgnet_final_force:<15.6f} " | ||
| f"{'N/A':<15} {'N/A':<15} {'N/A':<15}" | ||
| ) | ||
|
|
||
| print("\n" + "=" * 100) | ||
brunocucco marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| print("CHGNet example completed successfully!") | ||
| print("=" * 100) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.