Skip to content
Open
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
20 changes: 20 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ notebooks:
- ssl
- h5py
- lmdb
- name: mattersim
packages_pyodide:
# Packages with dependencies
- opt_einsum
- orjson
- pyyaml
- setuptools
# Packages without dependencies
- nodeps:opt_einsum_fx
- nodeps:e3nn>=0.5
- nodeps:ase
- nodeps:monty
- nodeps:deprecated
- wrapt
# MatterSim local wheel (pure Python, Cython replaced with NumPy)
- emfs:/drive/packages/mattersim-1.1.2-py3-none-any.whl
# Stubbed packages (patched by torch_pyodide with include_mattersim=True)
- ssl
- h5py
- lmdb
306 changes: 306 additions & 0 deletions other/experiments/jupyterlite/relax_structure_with_mattersim.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "cd6318a0",
"metadata": {},
"source": [
"# Relax Structure with MatterSim \u2014 M3GNet Interatomic Potential\n",
"\n",
"This notebook demonstrates structural relaxation using the **MatterSim** model,\n",
"a deep learning atomistic model based on the M3GNet architecture (Microsoft Research)."
]
},
{
"cell_type": "markdown",
"id": "ec82aac5",
"metadata": {},
"source": [
"## 1. Set Input Parameters\n",
"### 1.1. Structure and Relaxation"
]
},
{
"cell_type": "code",
"id": "00e95d72",
"metadata": {},
"source": [
"FOLDER = \"uploads\"\n",
"STRUCTURE_NAME = \"Interface\" # Name of the structure to load from local file\n",
"\n",
"RELAXATION_PARAMETERS = {\n",
" \"FMAX\": 0.05,\n",
"}"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"id": "5fb03403",
"metadata": {},
"source": [
"MATTERSIM_MODEL_PATH = \"/drive/packages/models/mattersim-v1.0.0-1M.pth\""
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "e2a90d7e",
"metadata": {},
"source": [
"## 2. Install Packages"
]
},
{
"cell_type": "code",
"id": "9634f33c",
"metadata": {},
"source": [
"from mat3ra.notebooks_utils.packages import install_packages\n",
"\n",
"await install_packages(\"made|api_examples|torch|mattersim\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"id": "46e728fa",
"metadata": {},
"source": [
"from mat3ra.notebooks_utils.pyodide.packages.torch import apply_all_patches\n",
"\n",
"apply_all_patches(include_mattersim=True)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "fdebcddf",
"metadata": {},
"source": [
"## 3. Load Materials"
]
},
{
"cell_type": "code",
"id": "bb76e05d",
"metadata": {},
"source": [
"from mat3ra.made.material import Material\n",
"from mat3ra.notebooks_utils.material import load_material_from_folder\n",
"from mat3ra.standata.materials import Materials\n",
"\n",
"structure = load_material_from_folder(FOLDER, STRUCTURE_NAME) or Material.create(\n",
" Materials.get_by_name_first_match(STRUCTURE_NAME))\n",
"\n",
"print(f\"INFO: Found: '{structure.name}'\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "9fe97f75",
"metadata": {},
"source": [
"### 3.1. Visualize Input Structure"
]
},
{
"cell_type": "code",
"id": "cbf71a5f",
"metadata": {},
"source": [
"from mat3ra.notebooks_utils.ipython.entity.material.visualize import ViewersEnum, visualize_materials as visualize\n",
"\n",
"visualize(structure, repetitions=[1, 1, 1], rotation=\"-90x\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "92dab479",
"metadata": {},
"source": [
"## 4. Apply Relaxation\n",
"### 4.1. Load MatterSim Model and Create Calculator"
]
},
{
"cell_type": "code",
"id": "7e4b8e07",
"metadata": {},
"source": [
"from mattersim.forcefield import MatterSimCalculator\n",
"\n",
"calculator = MatterSimCalculator.from_checkpoint(\n",
" load_path=MATTERSIM_MODEL_PATH,\n",
" device=\"cpu\",\n",
")\n",
"\n",
"print(f\"MatterSim model loaded from {MATTERSIM_MODEL_PATH}\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "4ed221b9",
"metadata": {},
"source": [
"### 4.2. Relax with MatterSim"
]
},
{
"cell_type": "code",
"id": "42fac903",
"metadata": {},
"source": [
"import plotly.graph_objs as go\n",
"from IPython.display import display\n",
"from plotly.subplots import make_subplots\n",
"\n",
"from mat3ra.made.tools.convert import to_ase\n",
"from ase.optimize import BFGS\n",
"\n",
"ase_structure = to_ase(structure)\n",
"ase_structure.calc = calculator\n",
"dyn = BFGS(ase_structure)\n",
"\n",
"steps = []\n",
"energies = []\n",
"\n",
"fig = make_subplots(rows=1, cols=1, specs=[[{\"type\": \"scatter\"}]])\n",
"scatter = go.Scatter(x=[], y=[], mode=\"lines+markers\", name=\"Energy\")\n",
"fig.add_trace(scatter)\n",
"fig.update_layout(title_text=\"Real-time Optimization Progress\", xaxis_title=\"Step\", yaxis_title=\"Energy (eV)\")\n",
"\n",
"try:\n",
" f = go.FigureWidget(fig)\n",
"except ImportError:\n",
" f = go.Figure(fig)\n",
"display(f)\n",
"\n",
"\n",
"def plotly_callback():\n",
" step = dyn.nsteps\n",
" energy = ase_structure.get_total_energy()\n",
" steps.append(step)\n",
" energies.append(energy)\n",
" print(f\"Step: {step}, Energy: {energy:.4f} eV\")\n",
" if hasattr(f, \"batch_update\"):\n",
" with f.batch_update():\n",
" f.data[0].x = steps\n",
" f.data[0].y = energies\n",
" else:\n",
" f.data[0].x = steps\n",
" f.data[0].y = energies\n",
"\n",
"\n",
"dyn.attach(plotly_callback, interval=1)\n",
"dyn.run(fmax=RELAXATION_PARAMETERS[\"FMAX\"])\n",
"\n",
"ase_original_structure = to_ase(structure)\n",
"ase_original_structure.calc = calculator\n",
"ase_final_structure = ase_structure\n",
"\n",
"original_energy = ase_original_structure.get_total_energy()\n",
"relaxed_energy = ase_structure.get_total_energy()\n",
"\n",
"print(f\"The final energy is {float(relaxed_energy):.3f} eV.\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "abf2e592",
"metadata": {},
"source": [
"## 5. Analyze Results\n",
"### 5.1. View Structure Before and After Relaxation"
]
},
{
"cell_type": "code",
"id": "fc047887",
"metadata": {},
"source": [
"from mat3ra.made.tools.convert import from_ase\n",
"\n",
"material_original = Material.create(from_ase(ase_original_structure))\n",
"material_relaxed = Material.create(from_ase(ase_final_structure))\n",
"material_original.name = structure.name\n",
"material_relaxed.name = structure.name + \" (MatterSim Relaxed)\"\n",
"\n",
"visualize([material_original, material_relaxed], viewer=ViewersEnum.wave)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "b540fa95",
"metadata": {},
"source": [
"### 5.2. Output interlayer distance before and after relaxation"
]
},
{
"cell_type": "code",
"id": "fbc98fe8",
"metadata": {},
"source": [
"from mat3ra.made.tools.analyze.other import get_average_interlayer_distance\n",
"\n",
"SUBSTRATE_TAG = 0\n",
"FILM_TAG = 1\n",
"\n",
"print(\n",
" f\"Interlayer distance before relaxation: {get_average_interlayer_distance(material_original, SUBSTRATE_TAG, FILM_TAG):.4f} \u00c5\")\n",
"print(\n",
" f\"Interlayer distance after relaxation: {get_average_interlayer_distance(material_relaxed, SUBSTRATE_TAG, FILM_TAG):.4f} \u00c5\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "0989b7a7",
"metadata": {},
"source": [
"## References\n",
"\n",
"[1] MatterSim: https://github.com/microsoft/mattersim\n",
"\n",
"[2] Han Yang et al., \"MatterSim: A Deep Learning Atomistic Model Across Elements, Temperatures and Pressures,\" arXiv:2405.04967 (2024)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (Pyodide)",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions packages/mattersim-1.1.2-py3-none-any.whl
Git LFS file not shown
Loading
Loading