{"id":13335170,"url":"https://github.com/Michael-MD/Elastic-Multislice-Electron-Microscopy","last_synced_at":"2025-03-11T04:31:04.278Z","repository":{"id":149902646,"uuid":"609737264","full_name":"Michael-MD/Elastic-Multislice-Electron-Microscopy","owner":"Michael-MD","description":"Simulation of elastic scattering in electron microscopy using the multislice algorithm. Simulations include various STEM and TEM techniques such as ADFSTEM, BFSTEM, ABFSTEM and CoM.","archived":false,"fork":false,"pushed_at":"2023-04-18T05:01:08.000Z","size":7635,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-23T16:05:22.437Z","etag":null,"topics":["abfstem","adfstem","algorithm","annular","bfstem","com","ctem","elastic","electron","imaging","kirkland","microscopy","multislice","physics","physics-simulation","scanning","scatter","scattering","stem","transmission"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Michael-MD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-05T04:25:33.000Z","updated_at":"2023-04-18T02:30:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"740e841d-f132-4933-9677-ce649b07b0e1","html_url":"https://github.com/Michael-MD/Elastic-Multislice-Electron-Microscopy","commit_stats":null,"previous_names":["michael-md/elastic-multislice-electron-microscopy","michael-md/electron-microscopy-in-c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michael-MD%2FElastic-Multislice-Electron-Microscopy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michael-MD%2FElastic-Multislice-Electron-Microscopy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michael-MD%2FElastic-Multislice-Electron-Microscopy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michael-MD%2FElastic-Multislice-Electron-Microscopy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Michael-MD","download_url":"https://codeload.github.com/Michael-MD/Elastic-Multislice-Electron-Microscopy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242972125,"owners_count":20215138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abfstem","adfstem","algorithm","annular","bfstem","com","ctem","elastic","electron","imaging","kirkland","microscopy","multislice","physics","physics-simulation","scanning","scatter","scattering","stem","transmission"],"created_at":"2024-07-29T19:04:37.581Z","updated_at":"2025-03-11T04:31:04.269Z","avatar_url":"https://github.com/Michael-MD.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elastic-Multislice-Electron-Microscopy\nSimulation of elastic scattering in electron microscopy using the multislice algorithm. Simulations include various STEM and TEM techniques such as ADFSTEM, BFSTEM, ABFSTEM and CoM.\n\nThe code is written with readability and ease of understanding rather than speed. I've written the code such that it can be easily expanded upon. For example, to begin incorporating inelastic scattering one can calculate the transition potentials and reuse the multislice class. Further, addition elastic scattering techniques can be included by simply specifying the detector function.\n\n## 1. Setup\nThis code has been tested with C++17 on Ubuntu, ensure that you place your project in a location which can access the header file of the technique you would like to use. You can clone the repo using the command\n```\ngit clone https://github.com/Michael-MD/Electron-Microscopy-in-C.git\n```\nNext run\n```\nmake\n```\nThis will prepare executable `a.out`.\n\n## 2. Theory\nThe theory introduced here summarizes the key results of elastic scattering in electron microscopy. Further details can be found in \nAdvanced computing in electron microscopy by Earl J. Kirkland which has become the go-to for an introduction to the field.\n\nIn electron microscopy, information about the strucutre and constituants of a specimen is inferred by observing the scattering and diffraction of\nelectrons. Although the incident electrons have energies on the order of 10keV-100keV, the Schrödinger equation is employed with a relativistically \ncorrected mass, wavelength and energy.\nGiven a specimen with potential V(r), the Schrodinger equation reads\n\n$$  -\\frac{\\hbar^2}{2m} \\nabla^2 \\Psi - e V(r) \\Psi = i\\hbar \\frac{\\partial \\Psi}{\\partial t} $$\n\nwhere $m=\\gamma m_0$. If we consider energy eigentates with a time-independent hamiltonian, then we obtain the Helmholtz equation\n\n$$ [\\nabla^2 + 4 \\pi k^2 + 4 \\pi U ] \\Psi = 0 $$\n\nwhere $k^2 = \\frac{2m e}{h^2}E$ and $U = \\frac{2m e}{h^2}V$. Assuming a solution of the form $\\Psi = \\exp(i 2\\pi k z) \\psi$ and applying the paraxial approximation this simplifies to\n\n$$ \\frac{d\\psi}{dz} = \\frac{i}{4\\pi k} [\\nabla_\\perp^2 + 4\\pi^2 U] \\psi  $$\n\nwith formal operator solution given by\n\n$$ \\psi(r_\\perp, \\Delta z) = e^{\\mathcal{Q} z} \\psi(r_\\perp, 0)  $$ \n\nwhere $\\mathcal{Q} = \\frac{i}{4\\pi k} [\\nabla_\\perp^2 + 4\\pi^2 U_c]$ if $\\Delta z$ is small. Here \n\n$$  U_c(r_\\perp) = \\int_0^{\\Delta z} dz \\\\, U(r_\\perp,z) $$\n\nand is refered to as the projected potential.\nSince $\\Delta z$ is taken to be small, \n\n$$ e^{\\mathcal Q \\Delta z} = e^{(\\mathcal T + \\mathcal U) \\Delta z} = e^{\\mathcal T \\Delta z} e^{\\mathcal U \\Delta z} + \\mathcal{O}(\\Delta z ^2) $$\n\nNow if $\\mathcal U = 0$, then the angular spectrum formulation applies to our problem and we may identify the $e^{\\mathcal T \\Delta z}$ as  the free-space propagotor. So we may propagate the wave function through the specimen through repeted applicaiton of these operators until the wave function is at the exit of the specimen. Mathematically, this may be written as \n\n$$ \\psi(r_\\perp, z) = \\prod \\mathcal F^{-1} \\\\{ P(k_\\perp, \\Delta z) \\mathcal F\\\\{ e^{\\mathcal U \\Delta z} \\psi(r_\\perp, 0) \\\\} \\\\} $$\n\nwhere $P(k_\\perp, \\Delta z)$ is the free-space propagator in reciprical space. The approach is referred to as the multislice approach and is the method used here. An interesting note is that the transmission function makes this system not LSI (Linear Shift-Invariant) since shifting the input wave doesn't result in a shifted output. For instance, a probe shifted far from the crystal will barely undergo diffraction if any but it will certainly propagate through free-space in the same way.\n\nThe techniques of electron microscopy can be catigoriezed into two primary modes. HR-TEM (High-Resolution Transmission Electron Microscopy) which uses incident place waves and STEM (Scanning Transmission Electron Mircsocopy) which uses\na converged probe which scans across the specimenn surface.\n\nIn STEM, for each probe position, a diffraction pattern is obtained, the detector which measures the intensity pattern \nconsitiues the various STEM techniques. The recorded signal at a given probe position is given by\n\n$$ g(k) = \\int dk \\\\, |I(k)|^2 D(k) $$\n\nwhere $D(k)$ is the detector geometry. For instance, ADFSTEM (Annular-Dark-Field STEM) collects electrons which scatter at high\nangles.\n\n## 3. Usage\n### 3.1 Defining structure of crystal\nTo image a particular material, a .xyz file containing the crystal atomic species and locations for a single unit cell is to be placed in the structure directory.\nThis can then be loaded into the program by specifying the file name in the constructor of the technique of interest. \nA few examples are provided in the repo. The file specifies general information about the unit cell at the beginning such as dimensions etc. and then specifies the atoms and locations on the subsequent lines. The template is as follows: \u003cbr\u003e\n\n[name] \u003cbr\u003e\n[unit cell length x] \u0026ensp; [unit cell length y] \u0026ensp; [unit cell length z] \u0026ensp; [basis angle alpha] \u0026ensp; [basis angle beta] \u0026ensp; [basis angle gamma] \u003cbr\u003e\n[atomic number] \u0026ensp; [fractional x-coord] \u0026ensp; [fractional y-coord] \u0026ensp; [fractional z-coord] \u0026ensp; [occupancy] \u0026ensp; [mean-square displacement] \u003cbr\u003e\n$\\vdots$ \u003cbr\u003e\n-1 \u003cbr\u003e\n\nThe code currently doesn't make use of the basis angles, occupancy or msd.\n\n### 3.2 Available Techniques\n\nThe available techniques have the following headers:\n```\nADFSTEM(float E, int px, int py, int px_p, int py_p, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\tfloat Cs, float deltaf, float alpha_max, float alpha_min_D, float alpha_max_D)\n    \nABFSTEM(float E, int px, int py, int px_p, int py_p, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\tfloat Cs, float deltaf, float alpha_max, float alpha_min_D, float alpha_max_D)\n    \nBFSTEM(float E, int px, int py, int px_p, int py_p, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\tfloat Cs, float deltaf, float alpha_max, float alpha_max_D)\n    \nCSTEM(float E, int px, int py, int px_p, int py_p, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\tfloat Cs, float deltaf, float alpha_max)\n    \nHRTEM(float E, int px, int py, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\t\tfloat Cs, float deltaf, float alpha_max)\n      \nCoM(float E, int px_p, int py_p, int px, int py, int tx, int ty, int tz, string filename, vector\u003cfloat\u003e \u0026s,\n\t\tfloat Cs, float deltaf, float alpha_max, bool CoM_dir_x)\n```\nThe inputs are as follows:\u003cbr\u003e\n\u003cb\u003e E \u003c/b\u003e: \u003cbr\u003e\u0026emsp;Energy of input beam in eV \u003cbr\u003e\n\u003cb\u003e(px, py)\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Dimensions of grid on which calculation is to occur in pixels i.e. the tranmission function, propagator etc. would be calculated on a grid of dimensions (px, py) pixels. \u003cbr\u003e\n\u003cb\u003e(px_p, py_p)\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Resolution of STEM scan positions i.e. the number of scan positions in each direction. \u003cbr\u003e\n\u003cb\u003e(tx, ty, tz)\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Tiling of unit cell.\u003cbr\u003e\n\u003cb\u003efilename\u003c/b\u003e:\u003cbr\u003e\u0026emsp; File name of unit cell. Assumes files is located in directory strcutures/. Currently only .xyz files are supported.\u003cbr\u003e\n\u003cb\u003es\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Vector of slice locations in fractions of unit cell thickness. For instance, if a unit cell is 3.905 Ang. then s = {.2,.8} will result in slices at .2 $\\times$ 3.905 = 0.781, .8 $\\times$ 3.905 = 3.124.\u003cbr\u003e\n\u003cb\u003eCs\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Sperhical abberation of lens in [mm].\u003cbr\u003e\n\u003cb\u003edeltaf\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Lens defocus in Ang.\u003cbr\u003e\n\u003cb\u003ealpha_max\u003c/b\u003e:\u003cbr\u003e\u0026emsp; Aperture opening angle in mrad.\u003cbr\u003e\n\u003cb\u003ealpha_min_D\u003c/b\u003e:\u003cbr\u003e\u0026emsp; minimum detector angle in mrad. \u003cbr\u003e\n\u003cb\u003ealpha_max_D\u003c/b\u003e:\u003cbr\u003e\u0026emsp; maximum detector angle in mrad.\u003cbr\u003e\n\nNew techniqes can be made by inheritying the multislice class and specifying the function which determines the detector. You may look at the wiki for details or simply look at one of the already existing techniqes as a template.\n\nYou may access any of the internal attributes such as the wave function or intensity and write these to a file by including `\"general_purpose_utils.h\"` which includes a function `writeToFile` which takes in the file name and 2D vector to be written to a file. For example,\n```\n#include \"general_purpose_utils.h\"\nvector\u003cfloat\u003e s = {1};\nCoM l(2e5, 64, 64, 128, 128, 1,1, 1, \"fig511.xyz\", s, 1.3, 500, 8.9, true);\nwriteToFile(\"I.txt\", l.I);\n```\n\n### 3.3 Class attributes\nAll classes have an attribute `layers` which stores the transmission function and propagators for the given layer. For example, continuing from the above example `l.layers[0].t_re` is the real component of the transmission function of the first layer. The propagator can be accessed using `P_re` or `P_im`.\n\nAll classes have attribtues `psi_re` and `psi_im` while only STEM techniques have `I` which stores the intensity.\n\n## 4. Examples\nHere are a few examples to get you up and running. The code to generate the file and the resulting image produced using a python script which I've inlcuded in the repo named visualize.py. STEM techniques will overwrite the wave function for each probe position so you will need to modify the code slightly to save the vector or otherwise. Secondly, HRTEM doesn't have access to a function which calcaultes the intensity but you may do so using the wave function.\n### 4.1 HRTEM\n```\n#include \"general_purpose_utils.h\"\n#include \"HRTEM.h\"\n#include \u003cvector\u003e\nusing namespace std;\n\nvector\u003cfloat\u003e s = {1};\nHRTEM l(2e5, 512, 512, 8,8,1, \"fig511.xyz\", s, 1.3, 700, 10.37);\nwriteToFile(\"psi_re.txt\", l.psi_re);\nwriteToFile(\"psi_im.txt\", l.psi_im);\n```\n![alt text](figures/Fig_5_12.png)\n\n### 4.2 ADFSTEM\n```\n#include \"general_purpose_utils.h\"\n#include \"ADFSTEM.h\"\n#include \u003cvector\u003e\nusing namespace std;\n\nvector\u003cfloat\u003e s = {1};\nADFSTEM l(2e5, 1024, 1024, 128, 128, 1,1, 1, \"fig511.xyz\", s, 1.3, 500, 8.9, 35, 200);\nwriteToFile(\"I.txt\", l.I);\n```\n![alt text](figures/5_21.jpg)\n\nThis is a linescan midway through the image obtained in python.\n\n### 4.3 CoM\n```\n#include \"general_purpose_utils.h\"\n#include \"CoM.h\"\n#include \u003cvector\u003e\nusing namespace std;\n\nvector\u003cfloat\u003e s = {1};\nCoM l(2e5, 64, 64, 128, 128, 1,1, 1, \"fig511.xyz\", s, 1.3, 500, 8.9, true);\nwriteToFile(\"I.txt\", l.I);\n```\n![alt text](figures/CoM.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMichael-MD%2FElastic-Multislice-Electron-Microscopy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMichael-MD%2FElastic-Multislice-Electron-Microscopy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMichael-MD%2FElastic-Multislice-Electron-Microscopy/lists"}