Skip to content

Commit

Permalink
Merge pull request #139 from Exabyte-io/feature/SOF-7398
Browse files Browse the repository at this point in the history
feature/SOF 7398
  • Loading branch information
VsevolodX authored Aug 17, 2024
2 parents ee1eacb + 20ebb48 commit fe60c46
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 2 deletions.
4 changes: 4 additions & 0 deletions other/materials_designer/Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"\n",
"This notebook lets user create a slab and place an adatom at specified location. [Click here to open the notebook](create_adatom_defect.ipynb).\n",
"\n",
"#### [1.6. Slab Perturbation](create_perturbation.ipynb)\n",
"\n",
"This notebook lets user add a perturbation to a slab by moving atoms according to a specified function. [Click here to open the notebook](create_perturbation.ipynb).\n",
"\n",
"### 2. Data Import\n",
"\n",
"#### [2.1. Materials import from files in ASE-supported formats](import_materials_from_files.ipynb)\n",
Expand Down
356 changes: 356 additions & 0 deletions other/materials_designer/create_perturbation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,356 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Create a Perturbation in a Material\n",
"\n",
"Create a perturbation in a material with a specified smooth function or a custom function described with [SymPy](https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html) expressions. \n",
"\n",
"<h2 style=\"color:green\">Usage</h2>\n",
"\n",
"1. Make sure to select Input Materials (in the outer runtime) before running the notebook.\n",
"1. Set notebook parameters in cell 1.1. below (or use the default values).\n",
"1. Set perturbation parameters in cell 2.1. (or use default).\n",
"1. Click “Run” > “Run All” to run all cells. \n",
"1. Wait for the run to complete (depending on the parameters can take a few min). \n",
"1. Scroll down to view results. \n",
"\n",
"## Summary\n",
"1. Prepare the Environment: Set up the notebook and install packages, preview the input materials\n",
"1. Create the Perturbation: Add a smooth perturbation to the material\n",
"2. Visualize the Perturbed Material\n",
"\n",
"## Notes\n",
"\n",
"1. For more information, see [Introduction](Introduction.ipynb)\n"
],
"metadata": {
"collapsed": false
},
"id": "367a698b29e22bd7"
},
{
"cell_type": "markdown",
"source": [
"## 1. Prepare the Environment\n",
"### 1.1. Set up supercell parameters "
],
"metadata": {
"collapsed": false
},
"id": "193a4e6a78fd5bd7"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"SUPERCELL_MATRIX = [[5, 0, 0], [0, 5, 0], [0, 0, 1]] "
],
"metadata": {
"collapsed": false
},
"id": "a40d7b697c413113",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 1.2. Install Packages\n",
"The step executes only in Pyodide environment. For other environments, the packages should be installed via `pip install` (see [README](../../README.ipynb))."
],
"metadata": {
"collapsed": false
},
"id": "49c9f5022d50517e"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import sys\n",
"\n",
"if sys.platform == \"emscripten\":\n",
" import micropip\n",
" await micropip.install('mat3ra-api-examples', deps=False)\n",
" from utils.jupyterlite import install_packages\n",
" await install_packages(\"\", \"../../config.yml\")"
],
"metadata": {
"collapsed": false
},
"id": "c6d9f8e57bef2f91",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 1.3. Get input materials\n",
"Materials are loaded with `get_data()`. The first material is assigned as substrate and the second as film."
],
"metadata": {
"collapsed": false
},
"id": "edf02101e27a2742"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from mat3ra.made.material import Material\n",
"from utils.jupyterlite import get_data\n",
"\n",
"# Get the list of input materials and load them into `materials_in` variable\n",
"get_data(\"materials_in\", globals())\n",
"materials = list(map(Material, globals()[\"materials_in\"]))"
],
"metadata": {
"collapsed": false
},
"id": "e0c53233ce728cc1",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 1.4. Create and preview Supercell"
],
"metadata": {
"collapsed": false
},
"id": "cf29b7f6fe114d8f"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.visualize import visualize_materials as visualize\n",
"from mat3ra.made.tools.build.supercell import create_supercell\n",
"\n",
"unit_cell = materials[0]\n",
"supercell = create_supercell(unit_cell, supercell_matrix=SUPERCELL_MATRIX)\n",
"visualize(supercell, repetitions=[1, 1, 1], rotation=\"0x\")"
],
"metadata": {
"collapsed": false
},
"id": "897ba7aa4e402d24",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## 2. Create the SineWave Perturbation\n",
"### 2.1. Set sine wave perturbation parameters\n",
"Use perturbation function imported from `mat3ra.made.tools.utils.perturbation` folder."
],
"metadata": {
"collapsed": false
},
"id": "39a93c4635762a83"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from mat3ra.made.tools.build.perturbation import PerturbationConfiguration, SlabPerturbationBuilder\n",
"from mat3ra.made.tools.utils.perturbation import SineWavePerturbationFunctionHolder\n",
"\n",
"amplitude = 0.05\n",
"wavelength = 1\n",
"phase = 0\n",
"axis = \"y\"\n",
"perturbation_function = SineWavePerturbationFunctionHolder(amplitude=amplitude, \n",
" wavelength=wavelength, \n",
" phase=phase,\n",
" axis=axis)\n",
"\n",
"configuration = PerturbationConfiguration(material=supercell, \n",
" perturbation_function_holder=perturbation_function,\n",
" use_cartesian_coordinates=False)\n",
"\n",
"builder = SlabPerturbationBuilder()"
],
"metadata": {
"collapsed": false
},
"id": "1991efeeebe39db4",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 2.2. Apply perturbation to the material"
],
"metadata": {
"collapsed": false
},
"id": "d296d7287618dc0b"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from mat3ra.made.tools.build.perturbation import create_perturbation\n",
"\n",
"material_with_perturbation = create_perturbation(configuration, builder)"
],
"metadata": {
"collapsed": false
},
"id": "d77821f7d0b8c330",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### 2.3. Visualize the Material"
],
"metadata": {
"collapsed": false
},
"id": "eecd561cd92fb18a"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.visualize import visualize_materials as visualize\n",
"\n",
"visualize([{\"material\": supercell, \"title\": \"Original material\"},\n",
" {\"material\": material_with_perturbation, \"title\": f\"Material with perturbation\"},\n",
" {\"material\": material_with_perturbation, \"title\": f\"Material with perturbation\", \"rotation\": \"-90x\"},\n",
"])"
],
"metadata": {
"collapsed": false
},
"id": "1ee393a7f2ec3bc8",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## 3. Create a Custom Perturbation\n",
"### 3.1. Set custom perturbation parameters\n",
"Provide a SymPy expression for the perturbation function. The expression should be a function of `x`, `y` and `z` variables."
],
"metadata": {
"collapsed": false
},
"id": "6d4adf0d580e0340"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import sympy as sp\n",
"from mat3ra.made.tools.build.perturbation import CellMatchingDistancePreservingSlabPerturbationBuilder\n",
"from mat3ra.made.tools.utils.perturbation import PerturbationFunctionHolder\n",
"\n",
"x,y = sp.symbols('x y')\n",
"function = amplitude * sp.sin(2 * sp.pi * x / wavelength + phase) * sp.sin(2 * sp.pi * y / wavelength)\n",
"\n",
"custom_perturbation_function = PerturbationFunctionHolder(function=function, variables=[\"x\", \"y\"])\n",
"configuration_custom = PerturbationConfiguration(material=supercell,\n",
" perturbation_function_holder=custom_perturbation_function,\n",
" use_cartesian_coordinates=False)\n",
"distance_preserving_builder = CellMatchingDistancePreservingSlabPerturbationBuilder()"
],
"metadata": {
"collapsed": false
},
"id": "8d90932312c418ee"
},
{
"cell_type": "markdown",
"source": [
"### 3.2. Apply perturbation to the material"
],
"metadata": {
"collapsed": false
},
"id": "7695d5d1df6be2e3"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"material_with_custom_perturbation = create_perturbation(configuration_custom, distance_preserving_builder)"
],
"metadata": {
"collapsed": false
},
"id": "69ccc90b8c5c1191"
},
{
"cell_type": "markdown",
"source": [
"### 3.3. Visualize the Material"
],
"metadata": {
"collapsed": false
},
"id": "10e7ca8950839991"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"visualize([\n",
" {\"material\": material_with_custom_perturbation, \"title\": f\"Material with custom perturbation\"},\n",
" {\"material\": material_with_custom_perturbation, \"title\": f\"Material with custom perturbation\",\"rotation\": \"-90x\"}\n",
"])"
],
"metadata": {
"collapsed": false
},
"id": "cbfe0878a16f6c83"
},
{
"cell_type": "markdown",
"source": [
"## 4. Pass data to the outside runtime"
],
"metadata": {
"collapsed": false
},
"id": "9e0b241366592109"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from utils.jupyterlite import set_data\n",
"\n",
"set_data(\"materials\", [material_with_perturbation.to_json(), material_with_custom_perturbation.to_json()])"
],
"metadata": {
"collapsed": false
},
"id": "29dfa0a329cca2fa",
"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
}
2 changes: 1 addition & 1 deletion other/materials_designer/create_point_defect.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
{
"cell_type": "markdown",
"source": [
"### 1.3. Get input material and \n",
"### 1.3. Get input materials\n",
"Materials are loaded with `get_data()`. The first material is assigned as substrate and the second as film."
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion utils/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_material_image(material: Material, title: str, rotation="0x,0y,0z", repe
# Create supercell for visualization
supercell_matrix = [[repetitions[0], 0, 0], [0, repetitions[1], 0], [0, 0, repetitions[2]]]
material_repeat = make_supercell(ase_atoms, supercell_matrix)
text = f"{ase_atoms.symbols} - {title} - rotation: {rotation}"
text = f"{ase_atoms.get_chemical_formula()} - {title} - rotation: {rotation}"

# Write image to a buffer to display in HTML
buf = io.BytesIO()
Expand Down

0 comments on commit fe60c46

Please sign in to comment.