From 62381cb6535ba12b1ca964bccc920f6afa36bc37 Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:27:28 -0800 Subject: [PATCH] feat: add fraft implementation --- .../text_on_nanoribbon.ipynb | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 other/materials_designer/text_on_nanoribbon.ipynb diff --git a/other/materials_designer/text_on_nanoribbon.ipynb b/other/materials_designer/text_on_nanoribbon.ipynb new file mode 100644 index 00000000..0041a50b --- /dev/null +++ b/other/materials_designer/text_on_nanoribbon.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "code", + "outputs": [], + "source": [ + "\n", + "from mat3ra.made.material import Material\n", + "from mat3ra.standata.materials import Materials\n", + "from mat3ra.made.tools.build.nanoribbon import create_nanoribbon, NanoribbonConfiguration\n", + "combined_length = 80\n", + "material = Material(Material.default_config)\n", + "graphene = Material(Materials.get_by_name_first_match(\"Graphene\"))\n", + "config = NanoribbonConfiguration(material=graphene, width = 4 * 10, length = combined_length , edge_type = \"zigzag\")\n", + "nanoribbon = create_nanoribbon(config)" + ], + "metadata": { + "collapsed": false + }, + "id": "624bdfcebc989400", + "execution_count": null + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "from mat3ra.made.tools.modify import rotate\n", + "from IPython.display import display\n", + "\n", + "import numpy as np\n", + "from matplotlib import font_manager\n", + "def create_text_grid(digit, grid_size=10):\n", + " \"\"\"\n", + " Create a grid representation of a single digit.\n", + " Parameters:\n", + " digit (str): A single digit as a string (e.g., '2', '0', etc.).\n", + " grid_size (int): The size of the grid for each digit (default: 10).\n", + " Returns:\n", + " np.ndarray: A binary grid with 1s representing the digit.\n", + " \"\"\"\n", + " from PIL import Image, ImageDraw, ImageFont\n", + "\n", + " # Create a blank image with white background\n", + " image = Image.new(\"1\", (grid_size, grid_size), color=1) # \"1\" mode for binary image\n", + " draw = ImageDraw.Draw(image)\n", + " \n", + " #Find the path for a specific fon\n", + " \n", + " # Use a bitmap font to disable antialiasing\n", + " try:\n", + " font = ImageFont.load_default()\n", + " except IOError:\n", + " raise RuntimeError(\"Failed to load bitmap font. Make sure PIL's default font is available.\")\n", + "\n", + " # Load a basic font and draw the digit\n", + " draw.text((0, 0), digit, fill=0, font=font)\n", + "\n", + " # Convert to a binary grid\n", + " grid = np.array(image)\n", + " \n", + " scale_factor = 10\n", + " # Scale the image for better visualization\n", + " scaled_size = (grid_size * scale_factor, grid_size * scale_factor)\n", + " scaled_image = image.resize(scaled_size, resample=Image.NEAREST)\n", + "\n", + " # Display the scaled image in the notebook\n", + " print(f\"Digit: {digit}\")\n", + " display(scaled_image)\n", + "\n", + " return grid\n", + "\n", + "def create_text_structure(digits, spacing=10, grid_size=10, lattice_param=2.0, x_shift=0):\n", + " \"\"\"\n", + " Create the atomic coordinates to represent a series of digits.\n", + " Parameters:\n", + " digits (str): The digits to represent (e.g., '2025').\n", + " spacing (int): Spacing between the digits.\n", + " grid_size (int): The size of the grid for each digit.\n", + " lattice_param (float): Lattice parameter for the atomic structure.\n", + " Returns:\n", + " np.ndarray: Array of atomic coordinates for the digits.\n", + " \"\"\"\n", + " all_coords = []\n", + " x_offset = 0\n", + "\n", + " for digit in digits:\n", + " # Get the grid for the current digit\n", + " grid = create_text_grid(digit, grid_size)\n", + " \n", + " # Convert grid positions to coordinates\n", + " x_offset += grid_size * lattice_param + spacing * lattice_param + x_shift\n", + " for y in range(grid.shape[0]):\n", + " for x in range(grid.shape[1]):\n", + " if not grid[y, x]:\n", + " # Map grid points to 3D coordinates\n", + " all_coords.append([\n", + " x * lattice_param + x_offset,\n", + " y * lattice_param,\n", + " 0.0\n", + " ])\n", + " # Add spacing between digits\n", + "\n", + " return np.array(all_coords)\n", + "\n", + "\n", + "\n", + "# Parameters\n", + "digits = \"2025\"\n", + "lattice_param = 2.46 # Distance between atoms\n", + "grid_size = 10 # Resolution of the digit\n", + "spacing = 0 # Spacing between digits\n", + "x_shift = -5\n", + "\n", + "# Generate the coordinates for the digits\n", + "coords = create_text_structure(digits, spacing=spacing, grid_size=grid_size, lattice_param=lattice_param, x_shift=x_shift)\n", + "# print(coords)\n", + "\n", + "# material = Material(Material.default_config)\n", + "# graphene = Material(Materials.get_by_name_first_match(\"Graphene\"))\n", + "# config = NanoribbonConfiguration(material=graphene, width = len(digits) * grid_size, length = len(digits) * grid_size * 2 , edge_type = \"zigzag\")\n", + "# material = create_nanoribbon(config)\n", + "material = nanoribbon.clone()\n", + "# size = lattice_param * grid_size * len(digits) + spacing * lattice_param * (len(digits) - 1)\n", + "# material.set_new_lattice_vectors([size, 0, 0], [0, size, 0], [0, 0, 20])\n", + "\n", + "\n", + "for coord in coords:\n", + " coord_copy = coord\n", + " coord[0] = coord[0] - lattice_param*6\n", + " coord[1] = coord[1] + lattice_param*3\n", + " coord[2] = coord[2] - lattice_param\n", + " material.add_atom(\"Au\", coord, use_cartesian_coordinates=True)\n", + " mirror_coord = coord\n", + " mirror_coord[0] = -coord[0]\n", + " mirror_coord[2] = coord_copy[2] + 2.5*lattice_param\n", + " material.add_atom(\"Cs\", mirror_coord, use_cartesian_coordinates=True)\n", + " \n", + "\n" + ], + "metadata": { + "collapsed": false + }, + "id": "77ef96120b42e981", + "execution_count": null + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "from utils.jupyterlite import set_materials\n", + "from utils.visualize import visualize_materials\n", + "from mat3ra.made.tools.modify import add_vacuum, translate_to_z_level\n", + "\n", + "material_copy = material.clone()\n", + "\n", + "material_copy = add_vacuum(material_copy, 20, to_bottom=True)\n", + "material_copy = translate_to_z_level(material_copy, \"center\")\n", + "material_copy = rotate(material_copy, [1,0,0], -90, rotate_cell=False)\n", + "\n", + "visualize_materials([material_copy], rotation=\"-180x\")\n", + "visualize_materials([material_copy], rotation=\"-90x\")\n", + "\n", + "material_copy.name = \"2025\"\n", + "set_materials([material_copy])\n", + "\n" + ], + "metadata": { + "collapsed": false + }, + "id": "9d490352a66e5f20", + "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 +}