File size: 10,670 Bytes
f421c2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | {
"cells": [
{
"cell_type": "markdown",
"id": "b8674439",
"metadata": {},
"source": [
"# Steady State Transport\n",
"\n",
"Simulating transport such as diffusion or flow is a key output of PNM. This notebook will explain the underlying process for these simulations in OpenPNM. \n",
"\n",
"The transport of a species between pore `i` and `j` is dictated by the conductance `g` of the conduit between these pores. `g` depends on the geometry as well as the fluid properties. Consider the case of diffusion which is described by Fick's law:\n",
"\n",
"$$ n_A = D_{AB}\\frac{A}{L} \\Delta C_A = g^D \\Delta C_A $$\n",
"\n",
"$g^D$ is the diffusive conductance and is a function of pore diameter, shape, length, as well as the physical properties of the fluid such as composition, temperature, pressure, and so on. Determination of $g^D$ is the subject of a different notebook, so for the present purposes random values between 0 and 1.0 will be used. Consider at 2x2 network:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d604aeca",
"metadata": {},
"outputs": [],
"source": [
"import openpnm as op\n",
"import numpy as np\n",
"op.visualization.set_mpl_style()\n",
"\n",
"pn = op.network.Cubic(shape=[2, 2, 1])\n",
"air = op.phase.Phase(network=pn)\n",
"air['throat.diffusive_conductance'] = np.random.rand(pn.Nt)"
]
},
{
"cell_type": "markdown",
"id": "16346729",
"metadata": {},
"source": [
"## Creating the Coefficient Matrix\n",
"\n",
"The rate equation given above applies to each throat in the network. In a pore network we want to solve for the quantity of interest in each pore. Each pore in the network has many throats. Depending on the concentrations in the neighboring pores the rate of diffusion through each throat may either be into or away from a given pore. At steady state and in the absence of reaction, the net rate into pore `i` will be zero. Thus we can write the mass balance equation for pore `i` using summation notation as:\n",
"\n",
"$$ \\Sigma g_{i, j} (x_j - x_i) = 0 $$\n",
"\n",
"We can write the above equation for each pore in the network and obtain a system of coupled linear equations such that:\n",
"\n",
"$$ x = A^{-1} b $$\n",
"\n",
"where `b` is the right hand side of the balance equation. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "78f456d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-1.13706265 0.39776399 0.73929866 0. ]\n",
" [ 0.39776399 -1.36417473 0. 0.96641074]\n",
" [ 0.73929866 0. -1.43682166 0.697523 ]\n",
" [ 0. 0.96641074 0.697523 -1.66393375]]\n"
]
}
],
"source": [
"import scipy.sparse.csgraph as csg\n",
"am = pn.create_adjacency_matrix(weights=-air['throat.diffusive_conductance'], fmt='csr', triu=False)\n",
"A = csg.laplacian(am).todense()\n",
"print(A)"
]
},
{
"cell_type": "markdown",
"id": "117261ce",
"metadata": {},
"source": [
"## Applying Value and Rate Boundary Conditions\n",
"\n",
"If `b=0` for all pores then `x=0` throughout the network. We must apply boundary conditions to obtain a non-trivial result. One option is to apply \"rate\" boundary conditions, such that the net movement of the species in or out of a pore is not zero, such that: \n",
"\n",
"$$ \\Sigma g_{i, j} (x_j - x_i) = r_i $$\n",
"\n",
"Another option is to specify the value of the species of interest in a pore. In this case the balance equation can be replaced with:\n",
"\n",
"$$ x_i = b_i $$\n",
"\n",
"Let's set $x_0 = 2.0$ and $r_3 = -0.5$. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "9f506711",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The A matrix is:\n",
" [[ 1. 0. 0. 0. ]\n",
" [ 0.39776399 -1.36417473 0. 0.96641074]\n",
" [ 0.73929866 0. -1.43682166 0.697523 ]\n",
" [ 0. 0.96641074 0.697523 -1.66393375]]\n",
"The b matrix is:\n",
" [2. 0. 0. 0.5]\n"
]
}
],
"source": [
"b = np.zeros([pn.Np, ], dtype=float)\n",
"b[0] = 2.0\n",
"A[0, :] = 0.0\n",
"A[0, 0] = 1.0\n",
"b[3] = 0.5\n",
"print(\"The A matrix is:\\n\", A)\n",
"print(\"The b matrix is:\\n\", b)"
]
},
{
"cell_type": "markdown",
"id": "1ba7e343",
"metadata": {},
"source": [
"## Solving the System of Equations\n",
"\n",
"One the coefficient matrix has been built from the system of equation, and boundary conditions are applied, we can solve the system $x = A^{-1}b$ to find $x_i$ in each pore. This can be done using any number of numerical solvers"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2055702a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2. 1.44713857 1.62113873 1.21958691]\n"
]
}
],
"source": [
"from scipy.linalg import solve\n",
"x = solve(A, b)\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"id": "1d2e562e",
"metadata": {},
"source": [
"## Sparse Matrices\n",
"\n",
"In the above example the `A` matrix was small so we could convert it to dense form. In a real network the coefficient matrix will be much larger so requires dense representation. The above process is the same, but some of the numerical steps are different to account for the sparse nature of the coefficient matrix. In particular the application of value BCs is more complicated:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3abc4552",
"metadata": {},
"outputs": [],
"source": [
"import scipy.sparse.csgraph as csg\n",
"am = pn.create_adjacency_matrix(weights=-air['throat.diffusive_conductance'], fmt='coo', triu=False)\n",
"A = csg.laplacian(am)\n",
"b = np.zeros([pn.Np, ], dtype=float)"
]
},
{
"cell_type": "markdown",
"id": "e0d0c3e1",
"metadata": {},
"source": [
"We can set the rate boundary condition in pore 3 in the same manner:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "07cbf18c",
"metadata": {},
"outputs": [],
"source": [
"b[3] = 0.5"
]
},
{
"cell_type": "markdown",
"id": "ab742d53",
"metadata": {},
"source": [
"Setting the value BC on pore 0 requires setting all elements in row 0 to 0, except the diagonal. However, in sparse form this cannot be done with direct indexing. Instead, we can inspect the `row` attribute of `A` to find entries on row 0:"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "455a5348",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 1 1 3 2 3 0 1 2 3]\n"
]
}
],
"source": [
"print(A.row)\n",
"hits = A.row == 0"
]
},
{
"cell_type": "markdown",
"id": "6a645561",
"metadata": {},
"source": [
"Now we set the `data` values to zero at these locations:"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "8582456d",
"metadata": {},
"outputs": [],
"source": [
"A.data[hits] = 0.0"
]
},
{
"cell_type": "markdown",
"id": "cd2067d7",
"metadata": {},
"source": [
"Then add a 1.0 in the diagonal:"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "3ec6eebf",
"metadata": {},
"outputs": [],
"source": [
"diag = A.diagonal()\n",
"diag[0] = 1.0\n",
"A.setdiag(diag)"
]
},
{
"cell_type": "markdown",
"id": "84788a3d",
"metadata": {},
"source": [
"And finally we can use `eliminate_zeros` to convert it into a proper sparse representation again:"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "0f5f86fd",
"metadata": {},
"outputs": [],
"source": [
"A.eliminate_zeros()"
]
},
{
"cell_type": "markdown",
"id": "5e8527ab",
"metadata": {},
"source": [
"And let's not forget to add the value BC to `b`:"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "1ec371a3",
"metadata": {},
"outputs": [],
"source": [
"b[0] = 2.0"
]
},
{
"cell_type": "markdown",
"id": "dc500c4a",
"metadata": {},
"source": [
"Let's inspect our new coefficient matrix to be sure:"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "79d6afb2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The A matrix is:\n",
" [[ 1. 0. 0. 0. ]\n",
" [ 0.39776399 -1.36417473 0. 0.96641074]\n",
" [ 0.73929866 0. -1.43682166 0.697523 ]\n",
" [ 0. 0.96641074 0.697523 -1.66393375]]\n",
"The b matrix is:\n",
" [2. 0. 0. 0.5]\n"
]
}
],
"source": [
"print(\"The A matrix is:\\n\", A.todense())\n",
"print(\"The b matrix is:\\n\", b)"
]
},
{
"cell_type": "markdown",
"id": "352b30b9",
"metadata": {},
"source": [
"Now we are ready to use the sparse solvers in `scipy`:"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "639d0dd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2. 1.44713857 1.62113873 1.21958691]\n"
]
}
],
"source": [
"from scipy.sparse.linalg import spsolve\n",
"x = spsolve(A.tocsr(), b)\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"id": "72df7a0a",
"metadata": {},
"source": [
"We can see the numerical results are idential. The additional effort required to deal with the sparse format is worth it since large network would become numerically intractable very quickly."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|