|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Modeling and Simulation in Python\n", |
| 8 | + "\n", |
| 9 | + "Chapter 15\n", |
| 10 | + "\n", |
| 11 | + "Copyright 2017 Allen Downey\n", |
| 12 | + "\n", |
| 13 | + "License: [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0)\n" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 1, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "# Configure Jupyter so figures appear in the notebook\n", |
| 23 | + "%matplotlib inline\n", |
| 24 | + "\n", |
| 25 | + "# Configure Jupyter to display the assigned value after an assignment\n", |
| 26 | + "%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'\n", |
| 27 | + "\n", |
| 28 | + "# import functions from the modsim.py module\n", |
| 29 | + "from modsim import *" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "### The coffee cooling problem\n", |
| 37 | + "\n", |
| 38 | + "I'll use a `State` object to store the initial temperature.\n" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 2, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "init = State(T=90)" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "And a `System` object to contain the system parameters." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 3, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "coffee = System(init=init,\n", |
| 64 | + " volume=300,\n", |
| 65 | + " r=0.01,\n", |
| 66 | + " T_env=22,\n", |
| 67 | + " t_end=30,\n", |
| 68 | + " dt=1)" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "The update function implements Newton's law of cooling." |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 4, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "def update_func(state, t, system):\n", |
| 85 | + " \"\"\"Update the thermal transfer model.\n", |
| 86 | + " \n", |
| 87 | + " state: State (temp)\n", |
| 88 | + " t: time\n", |
| 89 | + " system: System object\n", |
| 90 | + " \n", |
| 91 | + " returns: State (temp)\n", |
| 92 | + " \"\"\"\n", |
| 93 | + " unpack(system)\n", |
| 94 | + " \n", |
| 95 | + " T = state.T\n", |
| 96 | + " T += -r * (T - T_env) * dt\n", |
| 97 | + " \n", |
| 98 | + " return State(T=T)" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "Here's how it works." |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 5, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "update_func(init, 0, coffee)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "Here's a version of `run_simulation` that uses `linrange` to make an array of time steps." |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 6, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "def run_simulation(system, update_func):\n", |
| 131 | + " \"\"\"Runs a simulation of the system.\n", |
| 132 | + " \n", |
| 133 | + " Add a TimeFrame to the System: results\n", |
| 134 | + " \n", |
| 135 | + " system: System object\n", |
| 136 | + " update_func: function that updates state\n", |
| 137 | + " \"\"\"\n", |
| 138 | + " unpack(system)\n", |
| 139 | + " \n", |
| 140 | + " frame = TimeFrame(columns=init.index)\n", |
| 141 | + " frame.row[0] = init\n", |
| 142 | + " ts = linrange(0, t_end, dt)\n", |
| 143 | + " \n", |
| 144 | + " for t in ts:\n", |
| 145 | + " frame.row[t+dt] = update_func(frame.row[t], t, system)\n", |
| 146 | + " \n", |
| 147 | + " # store the final temperature in T_final\n", |
| 148 | + " system.T_final = get_last_value(frame.T)\n", |
| 149 | + " \n", |
| 150 | + " return frame" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "metadata": {}, |
| 156 | + "source": [ |
| 157 | + "And here's how it works." |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": 7, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "results = run_simulation(coffee, update_func)" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "markdown", |
| 171 | + "metadata": {}, |
| 172 | + "source": [ |
| 173 | + "Here's what the results look like." |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 8, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "plot(results.T, label='coffee')\n", |
| 183 | + "decorate(xlabel='Time (minutes)',\n", |
| 184 | + " ylabel='Temperature (C)')" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "And here's the final temperature:" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": 9, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [], |
| 199 | + "source": [ |
| 200 | + "coffee.T_final" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "markdown", |
| 205 | + "metadata": {}, |
| 206 | + "source": [ |
| 207 | + "## Encapsulation\n", |
| 208 | + "\n", |
| 209 | + "Before we go on, let's define a function to initialize `System` objects with relevant parameters:" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "execution_count": 10, |
| 215 | + "metadata": {}, |
| 216 | + "outputs": [], |
| 217 | + "source": [ |
| 218 | + "def make_system(T_init, r, volume, t_end):\n", |
| 219 | + " \"\"\"Makes a System object with the given parameters.\n", |
| 220 | + "\n", |
| 221 | + " T_init: initial temperature in degC\n", |
| 222 | + " r: heat transfer rate, in 1/min\n", |
| 223 | + " volume: volume of liquid in mL\n", |
| 224 | + " t_end: end time of simulation\n", |
| 225 | + " \n", |
| 226 | + " returns: System object\n", |
| 227 | + " \"\"\"\n", |
| 228 | + " init = State(T=T_init)\n", |
| 229 | + " \n", |
| 230 | + " # T_final is used to store the final temperature.\n", |
| 231 | + " # Before the simulation runs, T_final = T_init\n", |
| 232 | + " T_final = T_init\n", |
| 233 | + "\n", |
| 234 | + " T_env = 22 \n", |
| 235 | + " dt = 1\n", |
| 236 | + " \n", |
| 237 | + " return System(locals())" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "markdown", |
| 242 | + "metadata": {}, |
| 243 | + "source": [ |
| 244 | + "Here's how we use it:" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 11, |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [], |
| 252 | + "source": [ |
| 253 | + "coffee = make_system(T_init=90, r=0.01, volume=300, t_end=30)\n", |
| 254 | + "results = run_simulation(coffee, update_func)\n", |
| 255 | + "coffee.T_final" |
| 256 | + ] |
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "markdown", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "## Exercises\n", |
| 263 | + "\n", |
| 264 | + "**Exercise:** Simulate the temperature of 50 mL of milk with a starting temperature of 5 degC, in a vessel with the same insulation, for 15 minutes, and plot the results.\n", |
| 265 | + "\n", |
| 266 | + "By trial and error, find a values for `r` that makes the final temperature close to 20 C." |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": 12, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "# Solution goes here" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "code", |
| 280 | + "execution_count": 13, |
| 281 | + "metadata": {}, |
| 282 | + "outputs": [], |
| 283 | + "source": [ |
| 284 | + "plot(results.T, label='milk')\n", |
| 285 | + "decorate(xlabel='Time (minutes)',\n", |
| 286 | + " ylabel='Temperature (C)')" |
| 287 | + ] |
| 288 | + }, |
| 289 | + { |
| 290 | + "cell_type": "code", |
| 291 | + "execution_count": null, |
| 292 | + "metadata": {}, |
| 293 | + "outputs": [], |
| 294 | + "source": [] |
| 295 | + } |
| 296 | + ], |
| 297 | + "metadata": { |
| 298 | + "kernelspec": { |
| 299 | + "display_name": "Python 3", |
| 300 | + "language": "python", |
| 301 | + "name": "python3" |
| 302 | + }, |
| 303 | + "language_info": { |
| 304 | + "codemirror_mode": { |
| 305 | + "name": "ipython", |
| 306 | + "version": 3 |
| 307 | + }, |
| 308 | + "file_extension": ".py", |
| 309 | + "mimetype": "text/x-python", |
| 310 | + "name": "python", |
| 311 | + "nbconvert_exporter": "python", |
| 312 | + "pygments_lexer": "ipython3", |
| 313 | + "version": "3.6.6" |
| 314 | + } |
| 315 | + }, |
| 316 | + "nbformat": 4, |
| 317 | + "nbformat_minor": 2 |
| 318 | +} |
0 commit comments