Skip to content

Instantly share code, notes, and snippets.

@jxy
Created February 21, 2019 16:24
Show Gist options
  • Save jxy/cdbfe75baee7275773282f8b687347fa to your computer and use it in GitHub Desktop.
Save jxy/cdbfe75baee7275773282f8b687347fa to your computer and use it in GitHub Desktop.
2DU1.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "2DU1.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "TPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jxy/cdbfe75baee7275773282f8b687347fa/2du1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "Wd4yXWk9rICc",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"MIT License\n",
"\n",
"Copyright (c) 2019 Xiao-Yong Jin\n",
"\n",
"Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n",
"\n",
"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n",
"\n",
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
]
},
{
"metadata": {
"id": "atHZ8W7jCYK-",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"import math\n",
"import timeit"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "9PQOoyHd-GeG",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"beta = 4.0\n",
"nx = 8\n",
"nt = 8\n",
"nd = 2\n",
"tau = 1 # 0.3\n",
"nstep = 10 # 3\n",
"ntraj = 2**16 # 2**10 # 2**15"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "6MlCMisDrsnf",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"dt = tau/nstep"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "mBoVn4ZcCdyf",
"colab_type": "code",
"outputId": "83d46a1e-a1bb-4dba-c0ec-3de964c5df97",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 108
}
},
"cell_type": "code",
"source": [
"field = tf.get_variable(\"field\", [nd,nx,nt], dtype=tf.float64, initializer=tf.zeros_initializer) # mu, x, t"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Colocations handled automatically by placer.\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "W9JttzTPC0Re",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"plaqphase = lambda f: f[0,:] - f[1,:] - tf.roll(f[0,:], shift=-1, axis=1) + tf.roll(f[1,:], shift=-1, axis=0)\n",
"action = lambda f: (-beta)*tf.reduce_sum(tf.cos(plaqphase(f)))\n",
"def action_and_force(f):\n",
" with tf.GradientTape() as t:\n",
" t.watch(f)\n",
" s = action(f)\n",
" return (s, t.gradient(s, f))\n",
"force = lambda f: action_and_force(f)[1]\n",
"def regularize(f):\n",
" p2 = 2*math.pi\n",
" f_ = f - math.pi\n",
" return p2*(f_/p2 - tf.floordiv(f_, p2) - 0.5)\n",
"topocharge = lambda f: tf.reduce_sum(regularize(plaqphase(f))) / (2*math.pi)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "jfeQHeAdX3o0",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"def leapfrog(x, p):\n",
" x_ = x + 0.5*dt*p\n",
" p_ = p + (-dt)*force(x_)\n",
" def body(i, xx, pp):\n",
" xx_ = xx + dt*pp\n",
" pp_ = pp + (-dt)*force(xx_)\n",
" return (i+1, xx_, pp_)\n",
" (_, x__, pp) = tf.while_loop(\n",
" lambda i, xx, pp: i < nstep-1,\n",
" body,\n",
" (0, x_, p_))\n",
" xx = x__ + 0.5*dt*pp\n",
" return (xx, pp)\n",
"def hmc(x):\n",
" p = tf.random.normal(tf.shape(x), dtype=tf.float64)\n",
" act0 = action(x) + 0.5*tf.reduce_sum(p*p)\n",
" (x_, p_) = leapfrog(x, p)\n",
" xr = regularize(x_)\n",
" act = action(xr) + 0.5*tf.reduce_sum(p_*p_)\n",
" prob = tf.random.uniform([], dtype=tf.float64)\n",
" dH = act-act0\n",
" exp_mdH = tf.exp(-dH)\n",
" acc = tf.less(prob, exp_mdH)\n",
" newx = tf.cond(acc, lambda: xr, lambda: x)\n",
" return (dH, exp_mdH, acc, newx)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "x1DPImkddDHI",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"(dH, exp_mdH, acc, field_) = hmc(field)\n",
"plaq = action(field_) / (-beta*nx*nt)\n",
"topo = topocharge(field_)\n",
"update = field.assign(field_)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "sAH71vu5fFY7",
"colab_type": "code",
"outputId": "5b2cd378-4551-4175-f11b-030a2a94b737",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1513
}
},
"cell_type": "code",
"source": [
"def run():\n",
" with open(f\"tf_out_l{nx}_t{nt}_b{beta}_n{ntraj}_t{tau}_s{nstep}\", \"w\") as O:\n",
" params = f\"\"\"latsize = @[{nx}, {nt}]\n",
"volume = {nx*nt}\n",
"beta = {beta}\n",
"trajs = {ntraj}\n",
"tau = {tau}\n",
"steps = {nstep}\n",
"\"\"\"\n",
" O.write(params)\n",
" print(params)\n",
" with tf.Session() as sess:\n",
" sess.run(tf.global_variables_initializer())\n",
" for i in range(ntraj):\n",
" (_, dH_, exp_mdH_, acc_, plaq_, topo_) = sess.run((update, dH, exp_mdH, acc, plaq, topo))\n",
" ifacc = \"ACCEPT\" if acc_ else \"REJECT\"\n",
" status = f\"\"\"Begin traj: {i+1}\n",
"{ifacc}: dH: {dH_} exp(-dH): {exp_mdH_}\n",
"plaq: {plaq_}\n",
"topo: {topo_}\n",
"\"\"\"\n",
" O.write(status)\n",
" if (i+1) % (ntraj//16) == 0:\n",
" print(status)\n",
"print(\"Run time: \", timeit.timeit('run()', \"from __main__ import run\", number=1), \" seconds\")"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"latsize = @[8, 8]\n",
"volume = 64\n",
"beta = 4.0\n",
"trajs = 65536\n",
"tau = 1\n",
"steps = 10\n",
"\n",
"Begin traj: 4096\n",
"ACCEPT: dH: -0.09544170206339686 exp(-dH): 1.1001446839993538\n",
"plaq: 0.8209504717845939\n",
"topo: -1.0000000000000007\n",
"\n",
"Begin traj: 8192\n",
"ACCEPT: dH: -0.0414533763025986 exp(-dH): 1.0423245636935177\n",
"plaq: 0.8611138213736146\n",
"topo: -1.0000000000000002\n",
"\n",
"Begin traj: 12288\n",
"ACCEPT: dH: -0.083858338244994 exp(-dH): 1.0874748293037158\n",
"plaq: 0.8668186191917382\n",
"topo: -0.9999999999999996\n",
"\n",
"Begin traj: 16384\n",
"ACCEPT: dH: -0.25725252013353384 exp(-dH): 1.2933716879173265\n",
"plaq: 0.8618708683916562\n",
"topo: -1.5549378442710527e-15\n",
"\n",
"Begin traj: 20480\n",
"ACCEPT: dH: 0.1859811991979825 exp(-dH): 0.8302892049381517\n",
"plaq: 0.9010665884972758\n",
"topo: 0.9999999999999996\n",
"\n",
"Begin traj: 24576\n",
"ACCEPT: dH: 0.0689194110157132 exp(-dH): 0.9334018989582231\n",
"plaq: 0.8523641614632844\n",
"topo: 2.473764752249402e-16\n",
"\n",
"Begin traj: 28672\n",
"ACCEPT: dH: -0.04408617890234723 exp(-dH): 1.0450724142013108\n",
"plaq: 0.8468308622083323\n",
"topo: -1.0601848938211723e-15\n",
"\n",
"Begin traj: 32768\n",
"ACCEPT: dH: 0.24267838627991978 exp(-dH): 0.7845237868157151\n",
"plaq: 0.8854576320866961\n",
"topo: -0.9999999999999998\n",
"\n",
"Begin traj: 36864\n",
"ACCEPT: dH: -0.14572420690507215 exp(-dH): 1.15687708533784\n",
"plaq: 0.8501986687946108\n",
"topo: 7.067899292141149e-17\n",
"\n",
"Begin traj: 40960\n",
"ACCEPT: dH: 0.32352624606116365 exp(-dH): 0.7235929662079058\n",
"plaq: 0.8620958845589899\n",
"topo: 0.0\n",
"\n",
"Begin traj: 45056\n",
"ACCEPT: dH: 0.11343120389039996 exp(-dH): 0.8927656130995122\n",
"plaq: 0.8906176038847988\n",
"topo: 1.0000000000000007\n",
"\n",
"Begin traj: 49152\n",
"ACCEPT: dH: -0.06628563797269749 exp(-dH): 1.0685318868618177\n",
"plaq: 0.8437304022224479\n",
"topo: -7.067899292141149e-16\n",
"\n",
"Begin traj: 53248\n",
"ACCEPT: dH: 0.20247096542905751 exp(-dH): 0.8167101950839669\n",
"plaq: 0.8605370557813808\n",
"topo: -1.0000000000000013\n",
"\n",
"Begin traj: 57344\n",
"ACCEPT: dH: 0.19662536173339618 exp(-dH): 0.8214983403838964\n",
"plaq: 0.8993017252217254\n",
"topo: 1.0248453973604666e-15\n",
"\n",
"Begin traj: 61440\n",
"ACCEPT: dH: -0.09269852302836057 exp(-dH): 1.0971309256958828\n",
"plaq: 0.862437347281531\n",
"topo: -1.0601848938211723e-15\n",
"\n",
"Begin traj: 65536\n",
"ACCEPT: dH: 0.05288867256069807 exp(-dH): 0.9484855990778066\n",
"plaq: 0.9122317971544541\n",
"topo: -1.519598347810347e-15\n",
"\n",
"Run time: 180.46811224900011 seconds\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "hHs_jrh6yFy5",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment