{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37daac59",
   "metadata": {},
   "source": [
    "<table style=\"width: 100%; border-style: none\">\n",
    "<tr style=\"border-style: none; background-color: #82a8cf\">\n",
    "<td style=\"border-style: none; width: 1%; text-align: left; font-size: 18px; color: #ffffff\">Institut f&uuml;r Theoretische Physik<br /> <font color=\"#e6e6e6\">Universit&auml;t zu K&ouml;ln </font></td>\n",
    "<td style=\"border-style: none; width: 1%; font-size: 16px\">&nbsp;</td>\n",
    "<td style=\"border-style: none; width: 1%; text-align: right; font-size: 18px; color: #ffffff\">Prof. Dr. Simon Trebst<br /> <font color=\"#e6e6e6\"> Christoph Berke </font> </td>\n",
    "</tr>\n",
    "</table>\n",
    "<hr  style=\"height: 2px; border-color: #606060; background-color: #606060\"> \n",
    "<h1 style=\"font-weight:200; text-align: center; margin: 0px; font-size: 48px; padding:0px; color: #606060\">Statistische Physik </h1>\n",
    "<h1 style=\"font-weight:light; text-align: center; margin: 10px; padding:0px; color: #606060\">&Uuml;bungsblatt 1</h1>\n",
    "<hr  style=\"height: 2px; border-color: #606060; background-color: #606060\"> \n",
    "<h3 style=\"font-weight:400; text-align: center; margin: 0px; font-size: 20px; padding:0px; margin-bottom: 20px; color: #606060\">Wintersemester 23/24</h3>\n",
    "\n",
    "\n",
    "<font size=\"4\" color=\"#606060\">**Website:** <a href=\"https://www.thp.uni-koeln.de/trebst/Lectures/2023-StatPhys.shtml\" style=\"color:#82a8cf; text-decoration: underline;text-decoration-style: dotted;\">https://www.thp.uni-koeln.de/trebst/Lectures/2023-StatPhys.shtml</a></font>\n",
    "\n",
    "<font size=\"4\" color=\"#606060\">**Abgabe**: <span style=\"color:#82a8cf\"> 16.10.2023, 10:00 Uhr </span> <span style=\"float:right;\">**Besprechung**: 17.10.2023 </span></font>\n",
    "\n",
    "<font size=\"4\" color=\"#606060\">**Name**: <span style=\"color:#82a8cf\"> Bitte geben Sie Ihren Namen an.  </span> </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60ce47b9",
   "metadata": {},
   "source": [
    "<table style=\"border-style: none; width: 100%; background-color: #FFFFFF\"><tr style=\"border-style: none;\">\n",
    "<td style=\"border-style: none; width:2%; text-align: left; font-size: 25px; font-weight: 200;background-color: #FFFFFF\">Aufgabe 6: Thermalisierung eines Gittergases </td>\n",
    "<td style=\"border-style: none; width: 1%; text-align: right; font-size: 15px;background-color: #FFFFFF\">[6 + 4 = 10 Punkte]</td></tr></table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "492754f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "using GLMakie, Random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bee74cf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Diese Funktion erzeugt eine Anfangskonfiguration, in der auf dem Teilquadrat der \n",
    "# Größe Linit x Linit in der Ecke des größeren Gitters mit Lfinal x Lfinal Punkte\n",
    "# der Anteil filling der Gitterplätze zufällig besetzt sind (d.h. den Wert 1 haben).\n",
    "# Rückgabewert sind das Gitter, sowie die Indizes der besetzten Plätze.\n",
    "function initialize_lattice(Linit, Lfinal, filling)\n",
    "    \n",
    "    lattice = zeros(Bool, Lfinal, Lfinal) # Gitterpunkte, 0 wenn unbesetzt, 1 wenn besetzt\n",
    "    lattice[1:Linit, 1:Linit] = (rand(Linit, Linit) .< filling) # Erzeuge Zufallsmatrix mit gewünschter Füllung\n",
    "    \n",
    "    return lattice, findall(lattice) # Rueckgabe des Gitters, sowie der Indizes der Einträge mit Wert 1.\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b291156",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Funktion soll einen Update-Schritt ausführen: \n",
    "# (i)  zufälliges Teilchen auswählen \n",
    "# (ii) zufällige Richtung (oben,unten,links,rechts) auswählen\n",
    "# (iii) versuchen, das Teilchen in die Richtung zu bewegen (siehe Aufgabenblatt)\n",
    "# (iv) lattice und indices updaten, wenn Schritt (iii) erfolgreich ist.\n",
    "# Die Variable lattice speichert das Gitterarray, die Variable indices die Indizes der von Null\n",
    "# verschiedenen Einträge und die Variable L_current die Größe des zugänglichen Gitters \n",
    "# (für Aufgabebteil a) ist das einfach die Dimension von lattice in x- oder y-Richtung)\n",
    "function update!(lattice, indices, L_current)\n",
    "    \n",
    "    ### Schritt (i) ###\n",
    "    p = rand(1:size(indices,1)) # waehle eine zufaelliges Teilchen aus\n",
    "    x, y = Tuple(indices[p])    # aktuelle Koordinaten finden\n",
    "    \n",
    "    nx, ny = -1, -1 # potenziell neue Koordinaten des Teilchens\n",
    "    \n",
    "    #################################################\n",
    "    # Ergänzen Sie hier die Schritte (ii) und (iii) #\n",
    "    #################################################    \n",
    "    # Diesen Code hier sollen sie ersetzen  #\n",
    "    shuffle!(lattice)                       #\n",
    "    #########################################\n",
    "    \n",
    "    \n",
    "    ### Schritt (iv) ###\n",
    "    # Gegebenenfalls Update des Gitterarrays und des Arrays, in dem die Positionen gespeichert sind.\n",
    "    # lattice[x, y], lattice[nx, ny] = lattice[nx, ny], lattice[x, y]\n",
    "    # Update der Koordinaten\n",
    "    # indices[p] = CartesianIndex(nx,ny)\n",
    "    \n",
    "    return\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfc729a0",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "## a) Thermalisierung des Gittergases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e24a8ad3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variablen für Gesamtgittergröße, Anfangsgittergröße, Filling\n",
    "Ltot, Linit, fillfrac = 120, 30, 0.4\n",
    "\n",
    "# Erzeuge Gitter lat und Array pos, das die aktuellen Indizes der Gasteilchen speichert.\n",
    "lat, pos = initialize_lattice(Linit,Ltot,fillfrac);\n",
    "lat, pos = Observable(lat), Observable(pos)\n",
    "\n",
    "# Figure und Achsenumgebung erzeugen. Anfangskonfiguration darstellen.\n",
    "fig = Figure()\n",
    "ax = Axis(fig[1,1:2], aspect = DataAspect())\n",
    "hm = heatmap!(ax, lat, colormap = :binary)\n",
    "hidedecorations!(ax)\n",
    "\n",
    "# Start/Stop Button\n",
    "run = Button(fig[2,1]; label = \"Start / Stop\", tellwidth = false)\n",
    "isrunning = Observable(false)\n",
    "on(run.clicks) do clicks; isrunning[] = !isrunning[]; end\n",
    "\n",
    "# Restart Button\n",
    "restart = Button(fig[2,2]; label = \"Restart\", tellwidth = false)\n",
    "on(restart.clicks) do clicks; lat[], pos[] = initialize_lattice(Linit,Ltot,fillfrac) end\n",
    "    \n",
    "# Schleife die dauerhaft läuft, wenn isrunning[] == true\n",
    "on(run.clicks) do clicks\n",
    "    @async while isrunning[]\n",
    "        isopen(fig.scene) || break # Stoppen wenn Fenster geschlossen ist.\n",
    "        \n",
    "        # Update Gitter\n",
    "        for _ in 1:1_000 # Kann auf 10_000 erhöht werden für schnellere Thermalisierung.\n",
    "            update!(lat[],pos[],Ltot)\n",
    "        end\n",
    "        notify(lat)        \n",
    "        sleep(0.001)\n",
    "    end\n",
    "end\n",
    "\n",
    "# Anzeige der Figure.\n",
    "display(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0a5ab79",
   "metadata": {},
   "source": [
    "## b) Thermalisierung mit elastischen Wänden"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57019906",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.9.3",
   "language": "julia",
   "name": "julia-1.9"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.9.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
