{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "# Configure Jupyter so figures appear in the notebook\n", "%matplotlib inline\n", "\n", "# Configure Jupyter to display the assigned value after an assignment\n", "%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'\n", "\n", "# import functions from the modsim.py module\n", "from modsim import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def make_system():\n", " \"\"\"Make a system object for the HIV model.\n", " \n", " \n", " \n", " returns: System object\n", " \"\"\"\n", " gamma=1.36\n", " tau=0.2\n", " mu=1.36e-3\n", " beta=0.00027\n", " ro=0.1\n", " alpha=3.6e-2\n", " delta=0.33\n", " pi=100\n", " sigma=2\n", " t_0 = 0\n", "\n", " t_end = 120\n", " dT=0.06\n", "\n", "\n", "\n", " return System(gamm=gamma, tau=tau, mu=mu,\n", " beta=beta, ro=ro, alpha=alpha,delta=delta, pi=pi, sigma=sigma,t_0=t_0,t_end=t_end,dT=dT)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def update(state, time, system):\n", " R, L, E, V = state\n", " gamma, tau, mu, beta, ro, alpha, delta, pi, sigma, t_0, t_end, dT = system\n", " \n", " dRdT = gamma * tau - mu * R - beta * R * V\n", " dLdT = ro * beta * R * V - mu * L - alpha * L\n", " dEdT = (1 - ro) * beta * R * V + alpha * L - delta * E\n", " dVdT = pi * E - sigma * V\n", " R += dRdT * dT\n", " L += dLdT * dT\n", " E += dEdT * dT\n", " V += dVdT * dT\n", "\n", " return (State(R=R, L=L, E=E, V=V))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def run_simulation(system, update_func):\n", " \"\"\"Runs a simulation of the system.\n", " \n", " Add a TimeFrame to the System: results\n", " \n", " system: System object\n", " update_func: function that updates state\n", " \"\"\"\n", " init = State(R=200, L=0, E=0, V=4e-7)\n", " gamma, tau, mu, beta, ro, alpha, delta, pi, sigma, t_0, t_end, dT = system\n", " \n", " frame = TimeFrame(columns=init.index)\n", " frame.row[t_0] = init\n", " ts = linrange(t_0, t_end, dT)\n", " \n", " for t in ts:\n", " frame.row[t+dT] = update_func(frame.row[t], t, system)\n", " \n", " return frame\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | R | \n", "L | \n", "E | \n", "V | \n", "
---|---|---|---|---|
0.00 | \n", "200.000000 | \n", "0.000000e+00 | \n", "0.000000e+00 | \n", "4.000000e-07 | \n", "
0.06 | \n", "200.000000 | \n", "1.296000e-10 | \n", "1.166400e-09 | \n", "3.520000e-07 | \n", "
0.12 | \n", "200.000000 | \n", "2.433575e-10 | \n", "2.170017e-09 | \n", "3.167584e-07 | \n", "
0.18 | \n", "200.000000 | \n", "3.454417e-10 | \n", "3.051244e-09 | \n", "2.917675e-07 | \n", "
0.24 | \n", "200.000000 | \n", "4.392000e-10 | \n", "3.842370e-09 | \n", "2.750629e-07 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
119.76 | \n", "17.993435 | \n", "6.626104e-01 | \n", "2.389403e-01 | \n", "1.201070e+01 | \n", "
119.82 | \n", "18.004785 | \n", "6.614752e-01 | \n", "2.387915e-01 | \n", "1.200306e+01 | \n", "
119.88 | \n", "18.016135 | \n", "6.603425e-01 | \n", "2.386431e-01 | \n", "1.199544e+01 | \n", "
119.94 | \n", "18.027484 | \n", "6.592124e-01 | \n", "2.384952e-01 | \n", "1.198784e+01 | \n", "
120.00 | \n", "18.038832 | \n", "6.580848e-01 | \n", "2.383478e-01 | \n", "1.198027e+01 | \n", "
2001 rows × 4 columns
\n", "