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

Feature/SOF-7438 feat: media creation nb #200

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
19 changes: 19 additions & 0 deletions other/media_generation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GIFs generation with Materials visualization

## Setup materials

Place materials JSON files in the `input` directory.

Name them with a short name that will appear at bottom left of GIF.

## Start wave.js

Run wave.js (from commit: https://github.com/Exabyte-io/wave.js/pull/154/commits/83fc6fd587345728cb35bae0a0ebb258ef1491b6) locally (default port 3002 -- used in notebooks).

Or keep URL of the Netlify deployment.

## Generate GIFs

Run `record_gifs_with_wave.ipynb` to generate GIFs.

Run `gif_processing_multiple.ipynb` to add overlays and generate final GIFs.
200 changes: 200 additions & 0 deletions other/media_generation/create_materials.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"source": [
"import json\n",
"import re\n",
"import os\n",
"from pymatgen.ext.matproj import MPRester\n",
"from mat3ra.made.tools.build.slab import SlabConfiguration, get_terminations, create_slab\n",
"from mat3ra.made.tools.build.interface import InterfaceConfiguration, ZSLStrainMatchingParameters, \\\n",
" ZSLStrainMatchingInterfaceBuilder, ZSLStrainMatchingInterfaceBuilderParameters\n",
"from utils.visualize import visualize_materials\n",
"from utils.jupyterlite import set_materials\n",
"\n",
"from mat3ra.standata.materials import Materials\n",
"from mat3ra.made.material import Material\n",
"from mat3ra.made.tools.modify import wrap_to_unit_cell\n",
"\n",
"materials = Materials.get_by_categories(\"3D\")\n",
"\n",
"# Load the symbols from the JSON file\n",
"with open('symbols_map.json', 'r') as file:\n",
" symbols = json.load(file)\n",
"\n",
"# Extract the \"OVER\" symbol\n",
"SLASH_SYMBOL = symbols[\"/\"]\n",
"\n",
"# Map lattice type to Miller Index\n",
"\n",
"lattice_to_miller = {\n",
" \"CUB\": (1, 1, 1),\n",
" \"FCC\": (1, 1, 1),\n",
" \"HEX\": (0, 0, 1),\n",
" \"TRI\": (1, 1, 1)\n",
"}\n",
"\n",
"\n",
"def generate_interface(substrate_json, film_json):\n",
" print(f\"Creating interface: {substrate_json['name']} and {film_json['name']}\")\n",
"\n",
" substrate = Material(substrate_json)\n",
" film = Material(film_json)\n",
"\n",
"\n",
" substrate_miller_indices = lattice_to_miller[substrate.lattice.type] if substrate.lattice.type in lattice_to_miller else (0, 0, 1)\n",
" film_miller_indices = lattice_to_miller[film.lattice.type] if film.lattice.type in lattice_to_miller else (0, 0, 1)\n",
"\n",
" # Get material names before the \",\" -- the formula\n",
" substrate_name = re.match(r'[^,]*', substrate.name).group(0) + str(substrate_miller_indices).replace(\", \", \"\")\n",
" film_name = re.match(r'[^,]*', film.name).group(0) + str(film_miller_indices).replace(\", \", \"\")\n",
"\n",
" interface_name = f\"{film_name}{SLASH_SYMBOL}{substrate_name}\"\n",
" print(f\"Interface name: {interface_name}\")\n",
"\n",
"\n",
" # Define slab and interface parameters\n",
" film_params = {\n",
" \"miller_indices\": film_miller_indices,\n",
" \"thickness\": 3,\n",
" \"vacuum\": 15.0,\n",
" \"xy_supercell_matrix\": [[1, 0], [0, 1]],\n",
" \"use_conventional_cell\": True,\n",
" \"use_orthogonal_z\": True\n",
" }\n",
"\n",
" substrate_params = {\n",
" \"miller_indices\": substrate_miller_indices,\n",
" \"thickness\": 3,\n",
" \"vacuum\": 15.0,\n",
" \"xy_supercell_matrix\": [[1, 0], [0, 1]],\n",
" \"use_conventional_cell\": True,\n",
" \"use_orthogonal_z\": True\n",
" }\n",
"\n",
"\n",
" interface_params = {\n",
" \"distance_z\": 3.0,\n",
" \"vacuum\": 1.0,\n",
" \"max_area\": 150,\n",
" \"max_area_tol\": 0.10,\n",
" \"max_angle_tol\": 0.04,\n",
" \"max_length_tol\": 0.04\n",
" }\n",
"\n",
"\n",
" # Create slab configurations\n",
" substrate_slab_config = SlabConfiguration(bulk=substrate, **substrate_params)\n",
" film_slab_config = SlabConfiguration(bulk=film, **film_params)\n",
" try:\n",
" # Get terminations\n",
" substrate_terminations = get_terminations(substrate_slab_config)\n",
" film_terminations = get_terminations(film_slab_config)\n",
"\n",
" # Create slabs\n",
" # substrate_slabs = [create_slab(substrate_slab_config, t) for t in substrate_terminations]\n",
" # film_slabs = [create_slab(film_slab_config, t) for t in film_terminations]\n",
"\n",
" # Select termination pair (example: first pair)\n",
" termination_pair = (film_terminations[0], substrate_terminations[0])\n",
"\n",
" # Create interface configuration\n",
" interface_config = InterfaceConfiguration(\n",
" film_configuration=film_slab_config,\n",
" substrate_configuration=substrate_slab_config,\n",
" film_termination=termination_pair[0],\n",
" substrate_termination=termination_pair[1],\n",
" distance_z=interface_params[\"distance_z\"],\n",
" vacuum=interface_params[\"vacuum\"]\n",
" )\n",
"\n",
" # Set strain matching parameters\n",
" zsl_params = ZSLStrainMatchingParameters(\n",
" max_area=interface_params[\"max_area\"],\n",
" max_area_tol=interface_params[\"max_area_tol\"],\n",
" max_angle_tol=interface_params[\"max_angle_tol\"],\n",
" max_length_tol=interface_params[\"max_length_tol\"]\n",
" )\n",
"\n",
" # Generate interfaces\n",
" builder = ZSLStrainMatchingInterfaceBuilder(\n",
" build_parameters=ZSLStrainMatchingInterfaceBuilderParameters(strain_matching_parameters=zsl_params)\n",
" )\n",
" interfaces = builder.get_materials(configuration=interface_config)\n",
"\n",
" # Visualize and save the interfaces\n",
" interface = interfaces[0]\n",
" interface = wrap_to_unit_cell(interface)\n",
" visualize_materials(interface, repetitions=[1, 1, 1])\n",
"\n",
" interface.name = interface_name\n",
" set_materials(interface)\n",
" except Exception as e:\n",
" print(f\"Error creating interface between {substrate.name} and {film.name}: {e}\")\n"
],
"id": "f0c47155aa2a5343",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Generate interfaces between all pairs of materials",
"id": "2d27d1cce04b6bc4"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"# Create interfaces\n",
"# for i, substrate_json in enumerate(materials):\n",
"# for j, film_json in enumerate(materials):\n",
"# if i != j:\n",
"# generate_interface(substrate_json, film_json)\n"
],
"id": "82f16d004d364af3",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Generate interfaces between random pairs of materials",
"id": "8c63d6310b33335c"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"import random\n",
"num_pairs = 10\n",
"\n",
"for _ in range(num_pairs):\n",
" substrate_json, film_json = random.sample(materials, 2)\n",
" generate_interface(substrate_json, film_json)"
],
"id": "3572ccacbb4fd25f",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"id": "36307e16f113f5e8",
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"language": "python",
"display_name": "Python 3 (ipykernel)"
}
},
"nbformat": 5,
"nbformat_minor": 9
}
Loading
Loading