|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "source": [ |
| 6 | + "# Optimize Interface Film Position\n", |
| 7 | + "\n", |
| 8 | + "Find most optimal position of the film on the substrate interface.\n", |
| 9 | + "\n", |
| 10 | + "<h2 style=\"color:green\">Usage</h2>\n", |
| 11 | + "\n", |
| 12 | + "1. Make sure to select Input Material from the list of available materials. Must be an interface.\n", |
| 13 | + "1. Set notebook parameters in cell 1.2. below (or use the default values).\n", |
| 14 | + "1. Click \"Run\" > \"Run All\" to run all cells.\n", |
| 15 | + "1. Wait for the run to complete.\n", |
| 16 | + "1. Scroll down to view results.\n", |
| 17 | + "\n", |
| 18 | + "## Notes\n", |
| 19 | + "\n", |
| 20 | + "- The optimization is performed on a 2D grid of x,y translations.\n", |
| 21 | + "- Interface material must have atoms labeled \"0\" for the substrate and \"1\" for the film.\n", |
| 22 | + "\n", |
| 23 | + "\n", |
| 24 | + "## 1. Prepare the Environment\n", |
| 25 | + "### 1.1. Install Packages\n" |
| 26 | + ], |
| 27 | + "metadata": { |
| 28 | + "collapsed": false |
| 29 | + }, |
| 30 | + "id": "4dc7b2ed495d66e0" |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "import sys\n", |
| 37 | + "\n", |
| 38 | + "if sys.platform == \"emscripten\":\n", |
| 39 | + " import micropip\n", |
| 40 | + "\n", |
| 41 | + " await micropip.install('mat3ra-api-examples', deps=False)\n", |
| 42 | + " from utils.jupyterlite import install_packages\n", |
| 43 | + "\n", |
| 44 | + " await install_packages(\"\")\n" |
| 45 | + ], |
| 46 | + "metadata": { |
| 47 | + "collapsed": false |
| 48 | + }, |
| 49 | + "id": "dd86bee2985f1b50", |
| 50 | + "execution_count": null |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "source": [ |
| 55 | + "### 1.2. Set optimization parameters\n" |
| 56 | + ], |
| 57 | + "metadata": { |
| 58 | + "collapsed": false |
| 59 | + }, |
| 60 | + "id": "cca70ab27ef1d01d" |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "MATERIAL_INDEX = 0 # Index of the material to optimize\n", |
| 67 | + "# Grid parameters\n", |
| 68 | + "GRID_SIZE = (20, 20) # Resolution of the x-y grid\n", |
| 69 | + "GRID_RANGE_X = (-0.5, 0.5) # Range to search in x direction\n", |
| 70 | + "GRID_RANGE_Y = (-0.5, 0.5) # Range to search in y direction\n", |
| 71 | + "USE_CARTESIAN = False # Whether to use Cartesian coordinates\n", |
| 72 | + "\n", |
| 73 | + "# Visualization parameters\n", |
| 74 | + "SHOW_3D_LANDSCAPE = False # Whether to show 3D energy landscape\n", |
| 75 | + "STRUCTURE_REPETITIONS = [3, 3, 1] # Repetitions for structure visualization\n" |
| 76 | + ], |
| 77 | + "metadata": { |
| 78 | + "collapsed": false |
| 79 | + }, |
| 80 | + "id": "12878fd61f5a6b13", |
| 81 | + "execution_count": null |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "source": [ |
| 86 | + "## 3. Load Material\n", |
| 87 | + "### 3.1. Make sure that loaded material is an interface material (atoms must have labels \"0\" for the substrate and \"1\" for the film)" |
| 88 | + ], |
| 89 | + "metadata": { |
| 90 | + "collapsed": false |
| 91 | + }, |
| 92 | + "id": "463af646361cd982" |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "from utils.jupyterlite import get_materials\n", |
| 99 | + "\n", |
| 100 | + "materials = get_materials(globals())\n", |
| 101 | + "interface_material = materials[MATERIAL_INDEX]\n" |
| 102 | + ], |
| 103 | + "metadata": { |
| 104 | + "collapsed": false |
| 105 | + }, |
| 106 | + "id": "3d982a1ca641f0d8" |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "source": [ |
| 111 | + "### 3.2. Visualize the Material\n" |
| 112 | + ], |
| 113 | + "metadata": { |
| 114 | + "collapsed": false |
| 115 | + }, |
| 116 | + "id": "e920a6dd4906d8e8" |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "outputs": [], |
| 121 | + "source": [ |
| 122 | + "from utils.visualize import visualize_materials\n", |
| 123 | + "\n", |
| 124 | + "visualize_materials([interface_material], repetitions=STRUCTURE_REPETITIONS)\n", |
| 125 | + "visualize_materials([interface_material], repetitions=STRUCTURE_REPETITIONS, rotation='-90x')" |
| 126 | + ], |
| 127 | + "metadata": { |
| 128 | + "collapsed": false |
| 129 | + }, |
| 130 | + "id": "5f4afdb7ac0c865b" |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "source": [ |
| 135 | + "### 3.3. Optimize Film Position" |
| 136 | + ], |
| 137 | + "metadata": { |
| 138 | + "collapsed": false |
| 139 | + }, |
| 140 | + "id": "90255d774f62d1da" |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "from mat3ra.made.tools.build.interface import get_optimal_film_displacement\n", |
| 147 | + "from mat3ra.made.tools.modify import interface_displace_part\n", |
| 148 | + "from mat3ra.made.tools.calculate.calculators import InterfaceMaterialCalculator\n", |
| 149 | + "from mat3ra.made.tools.optimize import evaluate_calculator_on_xy_grid\n", |
| 150 | + "calculator = InterfaceMaterialCalculator()\n", |
| 151 | + "\n", |
| 152 | + "# Calculate energy landscape\n", |
| 153 | + "xy_matrix, energy_matrix = evaluate_calculator_on_xy_grid(\n", |
| 154 | + " material=interface_material,\n", |
| 155 | + " calculator_function=calculator.get_energy,\n", |
| 156 | + " modifier=interface_displace_part,\n", |
| 157 | + " grid_size_xy=GRID_SIZE,\n", |
| 158 | + " grid_range_x=GRID_RANGE_X,\n", |
| 159 | + " grid_range_y=GRID_RANGE_Y,\n", |
| 160 | + " use_cartesian_coordinates=USE_CARTESIAN\n", |
| 161 | + ")\n", |
| 162 | + "\n", |
| 163 | + "# Find optimal position\n", |
| 164 | + "optimal_displacement = get_optimal_film_displacement(\n", |
| 165 | + " material=interface_material,\n", |
| 166 | + " calculator=calculator,\n", |
| 167 | + " grid_size_xy=GRID_SIZE,\n", |
| 168 | + " grid_range_x=GRID_RANGE_X,\n", |
| 169 | + " grid_range_y=GRID_RANGE_Y,\n", |
| 170 | + " use_cartesian_coordinates=USE_CARTESIAN\n", |
| 171 | + ")\n", |
| 172 | + "\n", |
| 173 | + "print(f\"\\nOptimal displacement vector: {optimal_displacement}\")\n" |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "collapsed": false |
| 177 | + }, |
| 178 | + "id": "eb0b6e59c24dda4" |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "source": [ |
| 183 | + "## 4. Visualize Results\n", |
| 184 | + "### 4.1. Plot Energy Landscape" |
| 185 | + ], |
| 186 | + "metadata": { |
| 187 | + "collapsed": false |
| 188 | + }, |
| 189 | + "id": "2945179d3729935d" |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "outputs": [], |
| 194 | + "source": [ |
| 195 | + "from utils.plot import plot_energy_heatmap, plot_energy_landscape\n", |
| 196 | + "# Plot energy landscape\n", |
| 197 | + "plot_energy_heatmap(xy_matrix, energy_matrix, optimal_position=optimal_displacement[:2])\n", |
| 198 | + "\n", |
| 199 | + "if SHOW_3D_LANDSCAPE:\n", |
| 200 | + " plot_energy_landscape(xy_matrix, energy_matrix, optimal_position=optimal_displacement[:2])\n", |
| 201 | + "\n", |
| 202 | + "# Create optimized material\n", |
| 203 | + "optimized_material = interface_displace_part(\n", |
| 204 | + " interface_material,\n", |
| 205 | + " displacement=optimal_displacement,\n", |
| 206 | + " use_cartesian_coordinates=USE_CARTESIAN\n", |
| 207 | + ")\n" |
| 208 | + ], |
| 209 | + "metadata": { |
| 210 | + "collapsed": false |
| 211 | + }, |
| 212 | + "id": "41ac6b383001db6b", |
| 213 | + "execution_count": null |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "markdown", |
| 217 | + "source": [ |
| 218 | + "### 4.1. Visualize Original and Optimized Materials" |
| 219 | + ], |
| 220 | + "metadata": { |
| 221 | + "collapsed": false |
| 222 | + }, |
| 223 | + "id": "82a1af573c6ca0e9" |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "outputs": [], |
| 228 | + "source": [ |
| 229 | + "print(\"\\nVisualization of original and optimized materials:\")\n", |
| 230 | + "visualize_materials([interface_material, optimized_material],\n", |
| 231 | + " repetitions=STRUCTURE_REPETITIONS)\n", |
| 232 | + "visualize_materials([interface_material, optimized_material],\n", |
| 233 | + " repetitions=STRUCTURE_REPETITIONS,\n", |
| 234 | + " rotation='-90x')\n" |
| 235 | + ], |
| 236 | + "metadata": { |
| 237 | + "collapsed": false |
| 238 | + }, |
| 239 | + "id": "e7972543ae747b68", |
| 240 | + "execution_count": null |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "markdown", |
| 244 | + "source": [ |
| 245 | + "## 5. Save Results\n" |
| 246 | + ], |
| 247 | + "metadata": { |
| 248 | + "collapsed": false |
| 249 | + }, |
| 250 | + "id": "b4f6308e795e4f3c" |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "code", |
| 254 | + "outputs": [], |
| 255 | + "source": [ |
| 256 | + "from utils.jupyterlite import set_materials\n", |
| 257 | + "\n", |
| 258 | + "set_materials(optimized_material)" |
| 259 | + ], |
| 260 | + "metadata": { |
| 261 | + "collapsed": false |
| 262 | + }, |
| 263 | + "id": "c81ec652fbb64316", |
| 264 | + "execution_count": null |
| 265 | + } |
| 266 | + ], |
| 267 | + "metadata": { |
| 268 | + "kernelspec": { |
| 269 | + "display_name": "Python 3", |
| 270 | + "language": "python", |
| 271 | + "name": "python3" |
| 272 | + }, |
| 273 | + "language_info": { |
| 274 | + "codemirror_mode": { |
| 275 | + "name": "ipython", |
| 276 | + "version": 2 |
| 277 | + }, |
| 278 | + "file_extension": ".py", |
| 279 | + "mimetype": "text/x-python", |
| 280 | + "name": "python", |
| 281 | + "nbconvert_exporter": "python", |
| 282 | + "pygments_lexer": "ipython2", |
| 283 | + "version": "2.7.6" |
| 284 | + } |
| 285 | + }, |
| 286 | + "nbformat": 4, |
| 287 | + "nbformat_minor": 5 |
| 288 | +} |
0 commit comments