-
Notifications
You must be signed in to change notification settings - Fork 8
Feat: add stacking fault task #397
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
Merged
anyangml
merged 5 commits into
deepmodeling:main
from
anyangml2nd:feat/support-stacking-fault
Jan 28, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6062685
feat: add stacking fault task
anyangml 9c901ea
Update lambench/metrics/results/metadata.json
anyangml 28d8e0c
Update lambench/metrics/results/metadata.json
anyangml b625f36
Update lambench/tasks/calculator/stacking_fault/utils.py
anyangml 90740ed
chore: add dependency
anyangml 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
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
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
72 changes: 72 additions & 0 deletions
72
lambench/tasks/calculator/stacking_fault/stacking_fault.py
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,72 @@ | ||
| from ase.io import read | ||
| from pathlib import Path | ||
| from tqdm import tqdm | ||
| import numpy as np | ||
| import pandas as pd | ||
| from sklearn.metrics import mean_absolute_error | ||
| from lambench.models.ase_models import ASEModel | ||
| from lambench.tasks.calculator.stacking_fault.utils import fit_pchip | ||
|
|
||
|
|
||
| EV_A2_TO_MJ_M2 = 16021.766208 | ||
| NUM_POINTS = 200 | ||
|
|
||
|
|
||
| def calc_one_traj(traj, label, calc): | ||
| atoms_list = read(traj, ":") | ||
| df = pd.read_csv(label, header=0) | ||
|
|
||
| a_vector = atoms_list[0].cell[0] | ||
| b_vector = atoms_list[0].cell[1] | ||
| area = np.linalg.norm(np.cross(a_vector, b_vector)) | ||
|
|
||
| d = df["Displacement"] | ||
| preds = [] | ||
| for atoms in atoms_list: | ||
| atoms.calc = calc | ||
| preds.append(atoms.get_potential_energy()) | ||
| res = pd.DataFrame({"Displacement": d.to_list(), "Energy": preds}) | ||
| res["Energy"] = (res["Energy"] - res["Energy"].min()) * EV_A2_TO_MJ_M2 / area | ||
|
|
||
| _, y_smooth_label = fit_pchip( | ||
| df, x_col="Displacement", y_col="Energy", num_points=NUM_POINTS | ||
| ) | ||
| _, y_smooth_pred = fit_pchip( | ||
| res, x_col="Displacement", y_col="Energy", num_points=NUM_POINTS | ||
| ) | ||
|
|
||
| derivative_label = ( | ||
| (y_smooth_label[1:] - y_smooth_label[:-1]) | ||
| * (NUM_POINTS - 1) | ||
| / max(y_smooth_label) | ||
| ) | ||
| derivative_pred = ( | ||
| (y_smooth_pred[1:] - y_smooth_pred[:-1]) * (NUM_POINTS - 1) / max(y_smooth_pred) | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| return np.round(mean_absolute_error(y_smooth_label, y_smooth_pred), 4), np.round( | ||
| mean_absolute_error(derivative_label, derivative_pred), 4 | ||
| ) | ||
|
|
||
|
|
||
| def run_inference(model: ASEModel, test_data: Path) -> dict: | ||
| calc = model.calc | ||
|
|
||
| traj_files = sorted(list(test_data.glob("*.traj"))) | ||
| label_files = sorted(list(test_data.glob("*.csv"))) | ||
|
|
||
| energy_maes = [] | ||
| derivative_maes = [] | ||
| for traj_file, label_file in tqdm( | ||
| zip(traj_files, label_files), | ||
| total=len(traj_files), | ||
| desc="Calculating Stacking Fault Energies", | ||
| ): | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| energy_mae, derivative_mae = calc_one_traj(traj_file, label_file, calc) | ||
| energy_maes.append(energy_mae) | ||
| derivative_maes.append(derivative_mae) | ||
|
|
||
| return { | ||
| "MAE_E": np.round(np.mean(energy_maes), 4), # mJ/m² | ||
| "MAE_dE": np.round(np.mean(derivative_maes), 4), # mJ/m²/unit displacement | ||
| } | ||
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,44 @@ | ||
| from scipy.interpolate import PchipInterpolator | ||
| import pandas as pd | ||
| import numpy as np | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def fit_pchip( | ||
| df: pd.DataFrame, x_col: str, y_col: str, num_points: int | ||
| ) -> tuple[np.ndarray, np.ndarray]: | ||
| """ | ||
| Fit a PCHIP (Piecewise Cubic Hermite Interpolating Polynomial) to a dataframe. | ||
| PCHIP preserves monotonicity and is shape-preserving. | ||
|
|
||
| Parameters: | ||
| ----------- | ||
| df : pandas.DataFrame | ||
| Dataframe with x and y columns | ||
| x_col : str | ||
| Name of the x column | ||
| y_col : str | ||
| Name of the y column | ||
| num_points : int | ||
| Number of points for smooth interpolation | ||
|
|
||
| Returns: | ||
| -------- | ||
| x_smooth : numpy.ndarray | ||
| Smooth x values | ||
| y_smooth : numpy.ndarray | ||
| Smooth y values from PCHIP interpolation | ||
| """ | ||
| # Extract x and y values | ||
| x = df[x_col].values | ||
| y = df[y_col].values | ||
|
|
||
| # Create PCHIP interpolator | ||
| pchip = PchipInterpolator(x, y) | ||
|
|
||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Generate smooth x values | ||
| x_smooth = np.linspace(x.min(), x.max(), num_points) | ||
|
|
||
| # Evaluate PCHIP at smooth x values | ||
| y_smooth = pchip(x_smooth) | ||
|
|
||
| return x_smooth, y_smooth | ||
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
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.