{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7daba201",
   "metadata": {},
   "source": [
    "# Simulating a magnet using a Monte Carlo algorithm\n",
    "\n",
    "- by Börge Göbel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "48985065",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96417124",
   "metadata": {},
   "source": [
    "## 1. Generate starting configuration"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26ee5641",
   "metadata": {},
   "source": [
    "We start with a randomly generated array of magnetic moments \\\\( \\{ \\vec{m}_1, \\vec{m}_2, \\dots, \\vec{m}_n \\} \\\\). The length of each moment is fixed \\\\( |\\vec{m}_i| = 1 \\\\) but the orientation is chosen randomly. We can construct this by generating a random polar angle \\\\( \\varphi_i \\\\) and azimutal angle \\\\( \\theta_i \\\\) and using spherical coordinates:\n",
    "\n",
    "\\\\( \\vec{m}_i = \\begin{pmatrix} \\cos\\varphi_i\\sin\\theta_i \\\\ \\sin\\varphi_i\\sin\\theta_i \\\\ \\cos\\theta_i \\end{pmatrix} \\\\)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04f251fc",
   "metadata": {},
   "source": [
    "### Magnetic moments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d2099a3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "54ba25e4",
   "metadata": {},
   "source": [
    "### Positions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ac159f3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a26aba55",
   "metadata": {},
   "source": [
    "### Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fccc1095",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change standard size of all figures in this notebook\n",
    "plt.rcParams['figure.figsize'] = [40, 15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f83398d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0c257341",
   "metadata": {},
   "source": [
    "## 2. Calculating the energy "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8b6de7f",
   "metadata": {},
   "source": [
    "We are searching for the configuration with the lowest energy. For a ferromagnet the energy can be written as:\n",
    "\n",
    "\\\\( H = -\\frac{1}{2}J\\sum_{<i,j>}\\vec{m}_i\\cdot\\vec{m}_j  \\\\) \n",
    "\n",
    "Every moment is interacting with its direct neighbor (\\\\( <i,j> \\\\)) and prefers a parallel orientation as long as \\\\( J > 0\\\\). Of course, there exist additional energy terms that have to be considered for a realistic ferromagnet but we will neglect them for now. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74dae63e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b3d044c7",
   "metadata": {},
   "source": [
    "__Update 2 (skip for the beginning):__ We can apply an external magnetic field \\\\( \\vec{B} \\\\). This leads to the energy:\n",
    "\n",
    "\\\\( H = -\\frac{1}{2}J\\sum_{<i,j>}\\vec{m}_i\\cdot\\vec{m}_j - \\mu \\sum_i \\vec{B}\\cdot \\vec{m}_i \\\\) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82f48a7e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f04ce610",
   "metadata": {},
   "source": [
    "__Update 3 (skip for the beginning):__ We include the Dzyaloshinskii–Moriya interaction (asymmetric exchange).\n",
    "\n",
    "\\\\( H = -\\frac{1}{2}J\\sum_{<i,j>}\\vec{m}_i\\cdot\\vec{m}_j - \\mu B \\sum_i m_i^{(z)}  + \\frac{1}{2}\\sum_{<i,j>}\\vec{D}_{ij}\\cdot\\left(\\vec{m}_i\\times\\vec{m}_j\\right)\\\\) \n",
    "\n",
    "The vectors \\\\( \\vec{D}_{ij} \\\\) are determined by the symmetry of the sample. In our example, they shall point along the direction \\\\( i \\rightarrow j\\\\). \n",
    "\n",
    "As an example, let us consider a bond along the \\\\( x \\\\) direction:\n",
    "\n",
    "\\\\( \\vec{D}_{ij}\\cdot\\left(\\vec{m}_i\\times\\vec{m}_j\\right) = D \\vec{e}_x \\cdot \\left(\\vec{m}_i\\times\\vec{m}_j\\right) = D \\left( m_i^{(y)}m_j^{(z)} - m_i^{(z)}m_j^{(y)} \\right)\\\\) \n",
    "\n",
    "For a bond along the \\\\( y \\\\) direction:\n",
    "\n",
    "\\\\( \\vec{D}_{ij}\\cdot\\left(\\vec{m}_i\\times\\vec{m}_j\\right) = D \\vec{e}_y \\cdot \\left(\\vec{m}_i\\times\\vec{m}_j\\right) = D \\left( m_i^{(z)}m_j^{(x)} - m_i^{(x)}m_j^{(z)} \\right)\\\\) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c27dd52",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fd4100c1",
   "metadata": {},
   "source": [
    "## 3. Metropolis step"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67348885",
   "metadata": {},
   "source": [
    "Now to the actual Monte Carlo algorithm. We will repeditly do so-called Metropolis steps: \n",
    "\n",
    "1. A random magnetic moment is selected.\n",
    "2. It is reoriented along a random direction.\n",
    "3. The energy is calculated \\\\( E_\\mathrm{new} \\\\).\n",
    "4. The energy is compared to the old energy \\\\( E_\\mathrm{old} \\\\): \n",
    "    - If the energy is decreased, the random change of the magnetic moment is accepted. \n",
    "    - If the energy is increased, the old magnetic moment is restored."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5031084e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "91526551",
   "metadata": {},
   "source": [
    "__Update 1 (skip for the beginning):__ For finite temperatures \\\\( T \\\\), we accept the change with a chance \\\\( \\exp\\left(-\\frac{E_\\mathrm{new}-E_\\mathrm{old}}{k_BT}\\right) \\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1eb7111",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "894a06a9",
   "metadata": {},
   "source": [
    "__Update 2 (skip for the beginning):__ We include the energy corresponding to the interaction with the magnetic field:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "760f9996",
   "metadata": {},
   "source": [
    "__Update 3 (skip for the beginning):__ We include the energy corresponding to the Dzyaloshinskii–Moriya interaction (asymmetric exchange):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b3f3b3d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c932f35d",
   "metadata": {},
   "source": [
    "## 4. Run the Monte Carlo algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5245a95a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
