Skip to content
Merged
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
2 changes: 2 additions & 0 deletions other/materials_designer/Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
"\n",
"This notebook demonstrates a workflow for converting materials data from the [JARVIS](https://jarvis.nist.gov/) database into ESSE format for use with the Mat3ra.com platform.\n",
"\n",
"#### [6.1.4. Optimize film position on interface](optimize_film_position.ipynb).\n",
"\n",
"## 7. Read more\n",
"\n",
"### 7.1. Under the hood.\n",
Expand Down
288 changes: 288 additions & 0 deletions other/materials_designer/optimize_film_position.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Optimize Interface Film Position\n",
"\n",
"Find most optimal position of the film on the substrate interface.\n",
"\n",
"<h2 style=\"color:green\">Usage</h2>\n",
"\n",
"1. Make sure to select Input Material from the list of available materials. Must be an interface.\n",
"1. Set notebook parameters in cell 1.2. below (or use the default values).\n",
"1. Click \"Run\" > \"Run All\" to run all cells.\n",
"1. Wait for the run to complete.\n",
"1. Scroll down to view results.\n",
"\n",
"## Notes\n",
"\n",
"- The optimization is performed on a 2D grid of x,y translations.\n",
"- Interface material must have atoms labeled \"0\" for the substrate and \"1\" for the film.\n",
"\n",
"\n",
"## 1. Prepare the Environment\n",
"### 1.1. Install Packages\n"
],
"metadata": {
"collapsed": false
},
"id": "4dc7b2ed495d66e0"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import sys\n",
"\n",
"if sys.platform == \"emscripten\":\n",
" import micropip\n",
"\n",
" await micropip.install('mat3ra-api-examples', deps=False)\n",
" from utils.jupyterlite import install_packages\n",
"\n",
" await install_packages(\"\")\n"
],
"metadata": {
"collapsed": false
},
"id": "dd86bee2985f1b50",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 1.2. Set optimization parameters\n"
],
"metadata": {
"collapsed": false
},
"id": "cca70ab27ef1d01d"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"MATERIAL_INDEX = 0 # Index of the material to optimize\n",
"# Grid parameters\n",
"GRID_SIZE = (20, 20) # Resolution of the x-y grid\n",
"GRID_RANGE_X = (-0.5, 0.5) # Range to search in x direction\n",
"GRID_RANGE_Y = (-0.5, 0.5) # Range to search in y direction\n",
"USE_CARTESIAN = False # Whether to use Cartesian coordinates\n",
"\n",
"# Visualization parameters\n",
"SHOW_3D_LANDSCAPE = False # Whether to show 3D energy landscape\n",
"STRUCTURE_REPETITIONS = [3, 3, 1] # Repetitions for structure visualization\n"
],
"metadata": {
"collapsed": false
},
"id": "12878fd61f5a6b13",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## 3. Load Material\n",
"### 3.1. Make sure that loaded material is an interface material (atoms must have labels \"0\" for the substrate and \"1\" for the film)"
],
"metadata": {
"collapsed": false
},
"id": "463af646361cd982"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.jupyterlite import get_materials\n",
"\n",
"materials = get_materials(globals())\n",
"interface_material = materials[MATERIAL_INDEX]\n"
],
"metadata": {
"collapsed": false
},
"id": "3d982a1ca641f0d8"
},
{
"cell_type": "markdown",
"source": [
"### 3.2. Visualize the Material\n"
],
"metadata": {
"collapsed": false
},
"id": "e920a6dd4906d8e8"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.visualize import visualize_materials\n",
"\n",
"visualize_materials([interface_material], repetitions=STRUCTURE_REPETITIONS)\n",
"visualize_materials([interface_material], repetitions=STRUCTURE_REPETITIONS, rotation='-90x')"
],
"metadata": {
"collapsed": false
},
"id": "5f4afdb7ac0c865b"
},
{
"cell_type": "markdown",
"source": [
"### 3.3. Optimize Film Position"
],
"metadata": {
"collapsed": false
},
"id": "90255d774f62d1da"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from mat3ra.made.tools.build.interface import get_optimal_film_displacement\n",
"from mat3ra.made.tools.modify import interface_displace_part\n",
"from mat3ra.made.tools.calculate.calculators import InterfaceMaterialCalculator\n",
"from mat3ra.made.tools.optimize import evaluate_calculator_on_xy_grid\n",
"calculator = InterfaceMaterialCalculator()\n",
"\n",
"# Calculate energy landscape\n",
"xy_matrix, energy_matrix = evaluate_calculator_on_xy_grid(\n",
" material=interface_material,\n",
" calculator_function=calculator.get_energy,\n",
" modifier=interface_displace_part,\n",
" grid_size_xy=GRID_SIZE,\n",
" grid_range_x=GRID_RANGE_X,\n",
" grid_range_y=GRID_RANGE_Y,\n",
" use_cartesian_coordinates=USE_CARTESIAN\n",
")\n",
"\n",
"# Find optimal position\n",
"optimal_displacement = get_optimal_film_displacement(\n",
" material=interface_material,\n",
" calculator=calculator,\n",
" grid_size_xy=GRID_SIZE,\n",
" grid_range_x=GRID_RANGE_X,\n",
" grid_range_y=GRID_RANGE_Y,\n",
" use_cartesian_coordinates=USE_CARTESIAN\n",
")\n",
"\n",
"print(f\"\\nOptimal displacement vector: {optimal_displacement}\")\n"
],
"metadata": {
"collapsed": false
},
"id": "eb0b6e59c24dda4"
},
{
"cell_type": "markdown",
"source": [
"## 4. Visualize Results\n",
"### 4.1. Plot Energy Landscape"
],
"metadata": {
"collapsed": false
},
"id": "2945179d3729935d"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.plot import plot_energy_heatmap, plot_energy_landscape\n",
"# Plot energy landscape\n",
"plot_energy_heatmap(xy_matrix, energy_matrix, optimal_position=optimal_displacement[:2])\n",
"\n",
"if SHOW_3D_LANDSCAPE:\n",
" plot_energy_landscape(xy_matrix, energy_matrix, optimal_position=optimal_displacement[:2])\n",
"\n",
"# Create optimized material\n",
"optimized_material = interface_displace_part(\n",
" interface_material,\n",
" displacement=optimal_displacement,\n",
" use_cartesian_coordinates=USE_CARTESIAN\n",
")\n"
],
"metadata": {
"collapsed": false
},
"id": "41ac6b383001db6b",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 4.1. Visualize Original and Optimized Materials"
],
"metadata": {
"collapsed": false
},
"id": "82a1af573c6ca0e9"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"print(\"\\nVisualization of original and optimized materials:\")\n",
"visualize_materials([interface_material, optimized_material],\n",
" repetitions=STRUCTURE_REPETITIONS)\n",
"visualize_materials([interface_material, optimized_material],\n",
" repetitions=STRUCTURE_REPETITIONS,\n",
" rotation='-90x')\n"
],
"metadata": {
"collapsed": false
},
"id": "e7972543ae747b68",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## 5. Save Results\n"
],
"metadata": {
"collapsed": false
},
"id": "b4f6308e795e4f3c"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.jupyterlite import set_materials\n",
"\n",
"set_materials(optimized_material)"
],
"metadata": {
"collapsed": false
},
"id": "c81ec652fbb64316",
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading