{ "metadata": { "name": "", "signature": "sha256:44f398033fe758ff3b3d4068d094cd621a075236e20a9a4f350a432a1ad48552" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy import integrate\n", "import time" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.plotly as py\n", "import plotly.tools as tls\n", "from plotly.graph_objs import *\n", "\n", "stream_ids = tls.get_credentials_file()['stream_ids']\n", "stream_ids" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[u'p17pxe0idb', u'ilzladc8ro', u'o72o1p08y4', u'81dygs4lct']" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "def lorentz_deriv((x, y, z), t0, sigma=10., beta=8./3, rho=28.0):\n", " \"\"\"Compute the time-derivative of a Lorentz system.\"\"\"\n", " return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# Number individual trajectories to integrate\n", "N_trajectories = 1\n", "\n", "# Choose random starting points, uniformly distributed from -15 to 15\n", "np.random.seed(1)\n", "x0 = -15 + 30 * np.random.random((N_trajectories, 3))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "stream1 = dict(\n", " token=stream_ids[0],\n", " maxpoints=3000 \n", ")\n", "\n", "stream2 = dict(\n", " token=stream_ids[1]\n", ")\n", "\n", "trace1 = dict(\n", " type='scatter3d',\n", " x=[0],\n", " y=[0],\n", " z=[0],\n", " mode='lines',\n", " stream=stream1\n", ")\n", "\n", "trace2 = dict(\n", " type='scatter3d',\n", " x=[0],\n", " y=[0],\n", " z=[0],\n", " mode='markers',\n", " marker=dict(\n", " color=\"#1f77b4\",\n", " size=12,\n", " symbol='circle'\n", " ),\n", " stream=stream2\n", ")\n", "\n", "data = [trace1, trace2]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "layout = dict(\n", " title='Lorenz Attractor',\n", " scene=dict(\n", " xaxis=dict(\n", " autorange=False,\n", " range=[-25,25]\n", " ),\n", " yaxis=dict(\n", " autorange=False,\n", " range=[-35,35]\n", " ),\n", " zaxis=dict(\n", " autorange=False,\n", " range=[0,55]\n", " )\n", " ),\n", " margin=dict(\n", " l=0,\n", " r=0,\n", " t=80,\n", " b=0\n", " )\n", ")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "fig = dict(data=data, layout=layout)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "py.plot(fig, validate=False, filename='lorenz-eqs-stream')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "u'https://plot.ly/~etpinard/470'" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "s1 = py.Stream(stream_ids[0])\n", "s1.open()\n", "\n", "s2 = py.Stream(stream_ids[1])\n", "s2.open()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "np.random.seed(1)\n", "x0 = -15 + 30 * np.random.random((1, 3))[0]\n", "\n", "while True:\n", " \n", " t = np.linspace(0, 4, 1000)\n", " X_t = integrate.odeint(lorentz_deriv, x0, t)\n", " \n", " for x_t in X_t:\n", " \n", " s_data1 = dict(\n", " type='scatter3d',\n", " x=x_t[0],\n", " y=x_t[1],\n", " z=x_t[2]\n", " )\n", "\n", " s_data2 = dict(\n", " type='scatter3d',\n", " x=[x_t[0]],\n", " y=[x_t[1]],\n", " z=[x_t[2]]\n", " )\n", "\n", " s1.write(s_data1, validate=False)\n", " s2.write(s_data2, validate=False)\n", "\n", " time.sleep(0.05)\n", " \n", " x0 = X_t[-1,:]\n", "\n", "s.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[0ms2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms_data2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalidate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.05\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 29\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 30\u001b[0m \u001b[0mx0\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX_t\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: " ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }