├── MD.ipynb └── README.md /MD.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "from itertools import product\n", 13 | "rho = .88 # density of Argon in natural units\n", 14 | "dt = 0.004 # time step size\n", 15 | "T_0 = 1 # temperature\n", 16 | "N_cell = 3 # number of unitcells in one direction \n", 17 | "N = 4 * N_cell ** 3 # the total number of particles in the system\n", 18 | "L_box = (N / rho) ** (1 / 3) # length of the whole box\n", 19 | "L_cell = L_box / N_cell # length of a unitcell\n", 20 | "F = np.zeros((N, N, 3)) # matrix that contains all forces\n", 21 | "ind = np.triu_indices(N, k=1) # indices of upper triangular matrix\n", 22 | "bins = 30" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "def IC_pos(N_cell, L_cell):\n", 34 | " pos = [[[x, y, z], [x, 0.5 + y, 0.5 + z], [0.5 + x, y, 0.5 + z], [0.5 + x, 0.5 + y, z]] \n", 35 | " for x, y, z in product(range(N_cell), range(N_cell), range(N_cell))]\n", 36 | " pos = np.array(pos).reshape((-1, 3))\n", 37 | " return pos * L_cell\n", 38 | "\n", 39 | "\n", 40 | "def IC_vel(N):\n", 41 | " vel = np.random.randn(N, 3)\n", 42 | " vel -= np.average(vel, axis=0)\n", 43 | " return vel \n", 44 | "\n", 45 | "\n", 46 | "def find_force(pos, L_box=L_box):\n", 47 | " r_vec = pos[ind[0]] - pos[ind[1]]\n", 48 | " r_vec = r_vec - np.rint(r_vec / L_box) * L_box\n", 49 | " r_sq = np.sum(r_vec**2, axis=1)\n", 50 | " F_vec = -(48 / r_sq ** 7 - 24 / r_sq ** 4)[:, None] * r_vec\n", 51 | " F[ind[0], ind[1]] = F_vec\n", 52 | " pot = np.sum(4 / r_sq ** 6 - 4 / r_sq ** 3)\n", 53 | " P = np.sum(F_vec * r_vec)\n", 54 | " hist = np.histogram(r_sq, bins=bins, range=(0, L_box / 2))[0]\n", 55 | " return np.sum(F, axis=0) - np.sum(F, axis=1), pot, P, hist\n", 56 | "\n", 57 | "\n", 58 | "def time_step(pos, vel, F):\n", 59 | " vel += 0.5 * F * dt\n", 60 | " pos = np.mod(pos + vel * dt, L_box)\n", 61 | " F, pot, P, hist = find_force(pos)\n", 62 | " \n", 63 | " vel += 0.5 * F * dt\n", 64 | " kin = 0.5 * np.sum(vel**2)\n", 65 | " return pos, vel, F, pot, kin, P, hist" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 7, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "def simulate():\n", 77 | " kins, pots, Ps, hists = [], [], [], []\n", 78 | " pos = IC_pos(N_cell, L_cell)\n", 79 | " vel = IC_vel(N)\n", 80 | " F = find_force(pos)[0]\n", 81 | " for t in range(10000):\n", 82 | " pos, vel, F, pot, kin, P, hist = time_step(pos, vel, F)\n", 83 | " if t > 1000:\n", 84 | " kins.append(kin)\n", 85 | " pots.append(pot)\n", 86 | " Ps.append(P)\n", 87 | " hists.append(hist)\n", 88 | " else:\n", 89 | " vel *= np.sqrt(N * 3 * T_0 / (2 * kin))\n", 90 | " return np.array(kins), np.array(pots), np.array(Ps), np.mean(hists, axis=0)\n", 91 | "\n", 92 | "kins, pots, Ps, hist = simulate()\n", 93 | "\n", 94 | "T = np.mean(kins * 2 / (3 * N))\n", 95 | "P = 1 - np.mean(Ps) / (3 * N * T) - 16 * np.pi * rho / (3 * T * L_box**3)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 8, 101 | "metadata": { 102 | "collapsed": false 103 | }, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/html": [ 108 | "\n", 109 | "\n", 110 | "\n", 564 | "\n", 565 | "\n", 566 | "\n", 567 | "\n", 631 | "\n", 632 | "\n", 633 | "