|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Broadcasts\n", |
| 8 | + "\n", |
| 9 | + "This notebook explains the different types of broadcast available in PyBaMM.\n", |
| 10 | + "Understanding of the [expression_tree](./expression-tree.ipynb) and [discretisation](../spatial_methods/finite-volumes.ipynb) notebooks is assumed." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import pybamm\n", |
| 20 | + "import numpy as np" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "We also explicitly set up the discretisation that is used for this notebook. We use a small number of points in each domain, in order to easily visualise the results." |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 2, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "var = pybamm.standard_spatial_vars\n", |
| 37 | + "geometry = {\n", |
| 38 | + " \"negative electrode\": {\"primary\": {var.x_n: {\"min\": pybamm.Scalar(0), \"max\": pybamm.Scalar(1)}}},\n", |
| 39 | + " \"negative particle\": {\"primary\": {var.r_n: {\"min\": pybamm.Scalar(0), \"max\": pybamm.Scalar(1)}}},\n", |
| 40 | + "\n", |
| 41 | + "}\n", |
| 42 | + "\n", |
| 43 | + "submesh_types = {\n", |
| 44 | + " \"negative electrode\": pybamm.Uniform1DSubMesh,\n", |
| 45 | + " \"negative particle\": pybamm.Uniform1DSubMesh,\n", |
| 46 | + "}\n", |
| 47 | + "\n", |
| 48 | + "var_pts = {var.x_n: 5, var.r_n: 3}\n", |
| 49 | + "mesh = pybamm.Mesh(geometry, submesh_types, var_pts)\n", |
| 50 | + "\n", |
| 51 | + "spatial_methods = {\n", |
| 52 | + " \"negative electrode\": pybamm.FiniteVolume(),\n", |
| 53 | + " \"negative particle\": pybamm.FiniteVolume(),\n", |
| 54 | + "}\n", |
| 55 | + "disc = pybamm.Discretisation(mesh, spatial_methods)" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "## Primary broadcasts" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "Primary broadcasts are used to broadcast from a \"larger\" scale to a \"smaller\" scale, for example broadcasting temperature T(x) from the electrode to the particles, or broadcasting current collector current i(y, z) from the current collector to the electrodes.\n", |
| 70 | + "To demonstrate this, we first create a variable `T` on the negative electrode domain, discretise it, and evaluate it with a simple linear vector" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": 3, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "data": { |
| 80 | + "text/plain": [ |
| 81 | + "array([[0. ],\n", |
| 82 | + " [0.25],\n", |
| 83 | + " [0.5 ],\n", |
| 84 | + " [0.75],\n", |
| 85 | + " [1. ]])" |
| 86 | + ] |
| 87 | + }, |
| 88 | + "execution_count": 3, |
| 89 | + "metadata": {}, |
| 90 | + "output_type": "execute_result" |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "T = pybamm.Variable(\"T\", domain=\"negative electrode\")\n", |
| 95 | + "disc.set_variable_slices([T])\n", |
| 96 | + "disc_T = disc.process_symbol(T)\n", |
| 97 | + "disc_T.evaluate(y=np.linspace(0,1,5))" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "We then broadcast `T` onto the \"negative particle\" domain (using primary broadcast as we are going from the larger electrode scale to the smaller particle scale), and discretise and evaluate the resulting object." |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 4, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [ |
| 112 | + { |
| 113 | + "data": { |
| 114 | + "text/plain": [ |
| 115 | + "array([[0. ],\n", |
| 116 | + " [0. ],\n", |
| 117 | + " [0. ],\n", |
| 118 | + " [0.25],\n", |
| 119 | + " [0.25],\n", |
| 120 | + " [0.25],\n", |
| 121 | + " [0.5 ],\n", |
| 122 | + " [0.5 ],\n", |
| 123 | + " [0.5 ],\n", |
| 124 | + " [0.75],\n", |
| 125 | + " [0.75],\n", |
| 126 | + " [0.75],\n", |
| 127 | + " [1. ],\n", |
| 128 | + " [1. ],\n", |
| 129 | + " [1. ]])" |
| 130 | + ] |
| 131 | + }, |
| 132 | + "execution_count": 4, |
| 133 | + "metadata": {}, |
| 134 | + "output_type": "execute_result" |
| 135 | + } |
| 136 | + ], |
| 137 | + "source": [ |
| 138 | + "primary_broad_T = pybamm.PrimaryBroadcast(T, \"negative particle\")\n", |
| 139 | + "disc_T = disc.process_symbol(primary_broad_T)\n", |
| 140 | + "disc_T.evaluate(y=np.linspace(0,1,5))" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "The broadcasted object makes 3 (since the r-grid has 3 points) copies of each element of `T` and stacks them all up to give an object with size 3x5=15. In the resulting vector, the first 3 entries correspond to the 3 points in the r-domain at the first x-grid point (where T=0 uniformly in r), the next 3 entries correspond to the next 3 points in the r-domain at the second x-grid point (where T=0.25 uniformly in r), etc" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "## Secondary broadcasts" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "metadata": {}, |
| 160 | + "source": [ |
| 161 | + "Secondary broadcasts are used to broadcast from a \"smaller\" scale to a \"larger\" scale, for example broadcasting SPM particle concentrations c_s(r) from the particles to the electrodes. Note that this wouldn't be used to broadcast particle concentrations in the DFN, since these already depend on both x and r.\n", |
| 162 | + "To demonstrate this, we first create a variable `c_s` on the negative particle domain, discretise it, and evaluate it with a simple linear vector" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": 5, |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [ |
| 170 | + { |
| 171 | + "data": { |
| 172 | + "text/plain": [ |
| 173 | + "array([[0. ],\n", |
| 174 | + " [0.5],\n", |
| 175 | + " [1. ]])" |
| 176 | + ] |
| 177 | + }, |
| 178 | + "execution_count": 5, |
| 179 | + "metadata": {}, |
| 180 | + "output_type": "execute_result" |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "c_s = pybamm.Variable(\"c_s\", domain=\"negative particle\")\n", |
| 185 | + "disc.set_variable_slices([c_s])\n", |
| 186 | + "disc_c_s = disc.process_symbol(c_s)\n", |
| 187 | + "disc_c_s.evaluate(y=np.linspace(0,1,3))" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "We then broadcast `c_s` onto the \"negative electrode\" domain (using secondary broadcast as we are going from the smaller particle scale to the large electrode scale), and discretise and evaluate the resulting object." |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": 6, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [ |
| 202 | + { |
| 203 | + "data": { |
| 204 | + "text/plain": [ |
| 205 | + "array([[0. ],\n", |
| 206 | + " [0.5],\n", |
| 207 | + " [1. ],\n", |
| 208 | + " [0. ],\n", |
| 209 | + " [0.5],\n", |
| 210 | + " [1. ],\n", |
| 211 | + " [0. ],\n", |
| 212 | + " [0.5],\n", |
| 213 | + " [1. ],\n", |
| 214 | + " [0. ],\n", |
| 215 | + " [0.5],\n", |
| 216 | + " [1. ],\n", |
| 217 | + " [0. ],\n", |
| 218 | + " [0.5],\n", |
| 219 | + " [1. ]])" |
| 220 | + ] |
| 221 | + }, |
| 222 | + "execution_count": 6, |
| 223 | + "metadata": {}, |
| 224 | + "output_type": "execute_result" |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "secondary_broad_c_s = pybamm.SecondaryBroadcast(c_s, \"negative electrode\")\n", |
| 229 | + "disc_broad_c_s = disc.process_symbol(secondary_broad_c_s)\n", |
| 230 | + "disc_broad_c_s.evaluate(y=np.linspace(0,1,3))" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "markdown", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "The broadcasted object makes 5 (since the x-grid has 5 points) identical copies of the whole variable `c_s` to give an object with size 5x3=15. In the resulting vector, the first 3 entries correspond to the 3 points in the r-domain at the first x-grid point (where c_s varies in r), the next 3 entries correspond to the next 3 points in the r-domain at the second x-grid point (where c_s varies in r), etc" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": {}, |
| 244 | + "outputs": [], |
| 245 | + "source": [] |
| 246 | + } |
| 247 | + ], |
| 248 | + "metadata": { |
| 249 | + "kernelspec": { |
| 250 | + "display_name": "Python 3", |
| 251 | + "language": "python", |
| 252 | + "name": "python3" |
| 253 | + }, |
| 254 | + "language_info": { |
| 255 | + "codemirror_mode": { |
| 256 | + "name": "ipython", |
| 257 | + "version": 3 |
| 258 | + }, |
| 259 | + "file_extension": ".py", |
| 260 | + "mimetype": "text/x-python", |
| 261 | + "name": "python", |
| 262 | + "nbconvert_exporter": "python", |
| 263 | + "pygments_lexer": "ipython3", |
| 264 | + "version": "3.7.3" |
| 265 | + } |
| 266 | + }, |
| 267 | + "nbformat": 4, |
| 268 | + "nbformat_minor": 2 |
| 269 | +} |
0 commit comments