{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "from pylj import mc, sample, util\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def mc_simulation(number_of_particles, temperature, box_length, number_of_steps, sample_frequency):\n", " # Creates the visualisation environment\n", " %matplotlib widget\n", " # Initialise the system placing the particles on a square lattice\n", " system = mc.initialise(number_of_particles, temperature, box_length, 'square')\n", " # This sets the sampling class as Energy, which shows the energy of the system\n", " sample_system = sample.Energy(system)\n", " # Compute the energy of the system\n", " system.compute_energy()\n", " system.old_energy = system.energies.sum()\n", " # Add this energy to the energy sample array\n", " system.mc_sample()\n", " # Begin the monte carlo loop\n", " for i in range(0, number_of_steps):\n", " system.step += 1\n", " # Select a random particle to remove\n", " system.select_random_particle()\n", " # Select a random position to replace that particle\n", " system.new_random_position()\n", " # Compute the new energy of the system\n", " system.compute_energy()\n", " system.new_energy = system.energies.sum()\n", " # Assess the Metropolis condition\n", " if mc.metropolis(temperature, system.old_energy, system.new_energy):\n", " system.accept()\n", " else:\n", " system.reject()\n", " # Add this energy to the energy sample array\n", " system.mc_sample()\n", " # At a given frequency sample the positions and plot\n", " if system.step % sample_frequency == 0:\n", " sample_system.update(system)\n", " return system" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `mc_simulation` function takes five variables:\n", "- The number of particles\n", "- The simulation temperature (for the Metropolis condition)\n", "- The simulation cell vector\n", "- The number of steps\n", "- The sampling frequency (how often the image is updated)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "system = mc_simulation(100, 273.15, 45, 5000, 25)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }