|
25 | 25 | },
|
26 | 26 | {
|
27 | 27 | "cell_type": "code",
|
28 |
| - "execution_count": 1, |
| 28 | + "execution_count": 3, |
29 | 29 | "id": "46e47a0b",
|
30 | 30 | "metadata": {},
|
31 |
| - "outputs": [], |
| 31 | + "outputs": [ |
| 32 | + { |
| 33 | + "name": "stdout", |
| 34 | + "output_type": "stream", |
| 35 | + "text": [ |
| 36 | + "The autoreload extension is already loaded. To reload it, use:\n", |
| 37 | + " %reload_ext autoreload\n" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
32 | 41 | "source": [
|
33 | 42 | "%load_ext autoreload\n",
|
34 | 43 | "%autoreload 2"
|
35 | 44 | ]
|
36 | 45 | },
|
37 | 46 | {
|
38 | 47 | "cell_type": "code",
|
39 |
| - "execution_count": 2, |
| 48 | + "execution_count": 4, |
40 | 49 | "id": "c91b4ebc",
|
41 | 50 | "metadata": {},
|
42 | 51 | "outputs": [],
|
43 | 52 | "source": [
|
44 | 53 | "import logging\n",
|
| 54 | + "import time\n", |
45 | 55 | "\n",
|
46 | 56 | "import numpy as np\n",
|
47 | 57 | "import pde as py_pde\n",
|
48 | 58 | "from IPython.display import display\n",
|
49 |
| - "import time\n", |
50 |
| - "\n", |
51 | 59 | "from PyMPDATA_examples.comparisons_against_pypde.diffusion_equation_with_spatial_dependence import (\n",
|
52 | 60 | " solutions,\n",
|
53 | 61 | ")\n",
|
|
71 | 79 | "id": "76f14b27",
|
72 | 80 | "metadata": {},
|
73 | 81 | "source": [
|
74 |
| - "## Original example from the *py-pde* library" |
| 82 | + "## Original example from the *py-pde* library\n", |
| 83 | + "\n", |
| 84 | + "We use the almost identical code as in the original example from the *py-pde* library, which is available [here](https://py-pde.readthedocs.io/en/latest/examples_gallery/simple_pdes/pde_heterogeneous_diffusion.html)." |
75 | 85 | ]
|
76 | 86 | },
|
77 | 87 | {
|
78 | 88 | "cell_type": "code",
|
79 |
| - "execution_count": 3, |
| 89 | + "execution_count": null, |
80 | 90 | "id": "209d79d2",
|
81 | 91 | "metadata": {},
|
82 | 92 | "outputs": [
|
|
85 | 95 | "output_type": "stream",
|
86 | 96 | "text": [
|
87 | 97 | "INFO:pde.tools.numba:Compile `dot_compiled` with parallel=True\n",
|
88 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
89 |
| - "INFO:pde.tools.numba:Compile `laplace` with parallel=True\n", |
| 98 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n", |
| 99 | + "INFO:pde.tools.numba:Compile `gradient` with parallel=True\n", |
90 | 100 | "INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
|
91 | 101 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
92 | 102 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
93 | 103 | "INFO:pde.tools.numba:Compile `set_valid_bcs` with parallel=True\n",
|
94 | 104 | "INFO:pde.tools.numba:Compile `apply_op_compiled` with parallel=True\n",
|
95 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n", |
96 |
| - "INFO:pde.tools.numba:Compile `gradient` with parallel=True\n", |
| 105 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
| 106 | + "INFO:pde.tools.numba:Compile `laplace` with parallel=True\n", |
97 | 107 | "INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
|
98 | 108 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
99 | 109 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
|
105 | 115 | "INFO:pde.tools.expressions.ScalarExpression:Parse sympy expression `(tanh(x) + 1.01)*laplace(c, none, bc_args) + dot(gradient(tanh(x) + 1.01, none, bc_args), gradient(c, none, bc_args))`\n",
|
106 | 116 | "INFO:pde.tools.numba:Compile `_lambdifygenerated` with parallel=True\n",
|
107 | 117 | "INFO:pde.tools.numba:Compile `rhs_func` with parallel=True\n",
|
108 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
109 | 118 | "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n",
|
| 119 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
110 | 120 | "INFO:pde.pdes.PDE:RHS for `c` has signature ('c', 't', 'none', 'bc_args', 'x')\n",
|
111 | 121 | "INFO:pde.tools.expressions.ScalarExpression:Parse sympy expression `(tanh(x) + 1.01)*laplace(c, none, bc_args) + dot(gradient(tanh(x) + 1.01, none, bc_args), gradient(c, none, bc_args))`\n",
|
112 | 122 | "INFO:pde.solvers.ExplicitSolver:Init explicit Euler stepper with dt=0.001\n",
|
|
126 | 136 | {
|
127 | 137 | "data": {
|
128 | 138 | "text/plain": [
|
129 |
| - "<pde.tools.plotting.PlotReference at 0x29102b96c40>" |
| 139 | + "<pde.tools.plotting.PlotReference at 0x12a29a66d00>" |
130 | 140 | ]
|
131 | 141 | },
|
132 |
| - "execution_count": 3, |
| 142 | + "execution_count": 5, |
133 | 143 | "metadata": {},
|
134 | 144 | "output_type": "execute_result"
|
135 | 145 | }
|
|
140 | 150 | "term_2 = f\"dot(gradient({diffusivity}), gradient(c))\"\n",
|
141 | 151 | "eq = py_pde.PDE({\"c\": f\"{term_1} + {term_2}\"}, bc={\"value\": 0})\n",
|
142 | 152 | "\n",
|
143 |
| - "\n", |
144 | 153 | "grid = py_pde.CartesianGrid([[-5, 5]], 64)\n",
|
145 | 154 | "field = py_pde.ScalarField(grid, 1)\n",
|
146 | 155 | "\n",
|
|
160 | 169 | },
|
161 | 170 | {
|
162 | 171 | "cell_type": "code",
|
163 |
| - "execution_count": 4, |
| 172 | + "execution_count": 8, |
| 173 | + "id": "1c5bf44b", |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "class Timer:\n", |
| 178 | + " \"\"\"A simple timer class to measure elapsed time.\"\"\"\n", |
| 179 | + "\n", |
| 180 | + " def __init__(self):\n", |
| 181 | + " self._start = None\n", |
| 182 | + " self._time = None\n", |
| 183 | + "\n", |
| 184 | + " @property\n", |
| 185 | + " def time(self):\n", |
| 186 | + " return self._time\n", |
| 187 | + "\n", |
| 188 | + " def __enter__(self):\n", |
| 189 | + " self._start = time.perf_counter()\n", |
| 190 | + " return self\n", |
| 191 | + "\n", |
| 192 | + " def __exit__(self, *args):\n", |
| 193 | + " self._time = time.perf_counter() - self._start" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": null, |
164 | 199 | "id": "ba25763a",
|
165 | 200 | "metadata": {},
|
166 | 201 | "outputs": [
|
|
169 | 204 | "output_type": "stream",
|
170 | 205 | "text": [
|
171 | 206 | "INFO:pde.tools.numba:Compile `dot_compiled` with parallel=True\n",
|
172 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
173 |
| - "INFO:pde.tools.numba:Compile `laplace` with parallel=True\n", |
| 207 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n", |
| 208 | + "INFO:pde.tools.numba:Compile `gradient` with parallel=True\n", |
174 | 209 | "INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
|
175 | 210 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
176 | 211 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
177 | 212 | "INFO:pde.tools.numba:Compile `set_valid_bcs` with parallel=True\n",
|
178 | 213 | "INFO:pde.tools.numba:Compile `apply_op_compiled` with parallel=True\n",
|
179 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n", |
180 |
| - "INFO:pde.tools.numba:Compile `gradient` with parallel=True\n", |
| 214 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
| 215 | + "INFO:pde.tools.numba:Compile `laplace` with parallel=True\n", |
181 | 216 | "INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
|
182 | 217 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
183 | 218 | "INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
|
|
189 | 224 | "INFO:pde.tools.expressions.ScalarExpression:Parse sympy expression `(tanh(x) + 1.01)*laplace(c, none, bc_args) + dot(gradient(tanh(x) + 1.01, none, bc_args), gradient(c, none, bc_args))`\n",
|
190 | 225 | "INFO:pde.tools.numba:Compile `_lambdifygenerated` with parallel=True\n",
|
191 | 226 | "INFO:pde.tools.numba:Compile `rhs_func` with parallel=True\n",
|
192 |
| - "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
193 | 227 | "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `gradient` in PDE for `c`\n",
|
| 228 | + "INFO:pde.pdes.PDE:Using boundary condition `{'value': 0}` for operator `laplace` in PDE for `c`\n", |
194 | 229 | "INFO:pde.pdes.PDE:RHS for `c` has signature ('c', 't', 'none', 'bc_args', 'x')\n",
|
195 | 230 | "INFO:pde.tools.expressions.ScalarExpression:Parse sympy expression `(tanh(x) + 1.01)*laplace(c, none, bc_args) + dot(gradient(tanh(x) + 1.01, none, bc_args), gradient(c, none, bc_args))`\n",
|
196 | 231 | "INFO:pde.solvers.ExplicitSolver:Init explicit Euler stepper with dt=0.001\n",
|
|
199 | 234 | }
|
200 | 235 | ],
|
201 | 236 | "source": [
|
202 |
| - "start_time = time.perf_counter()\n", |
203 |
| - "py_pde_result = solutions.py_pde_solution(simulation_args)\n", |
204 |
| - "py_pde_time = time.perf_counter() - start_time" |
| 237 | + "with Timer() as timer:\n", |
| 238 | + " py_pde_result = solutions.py_pde_solution(simulation_args)\n", |
| 239 | + "py_pde_time = timer.time" |
205 | 240 | ]
|
206 | 241 | },
|
207 | 242 | {
|
|
214 | 249 | },
|
215 | 250 | {
|
216 | 251 | "cell_type": "code",
|
217 |
| - "execution_count": 5, |
| 252 | + "execution_count": 7, |
218 | 253 | "id": "42c6278f",
|
219 | 254 | "metadata": {},
|
220 | 255 | "outputs": [],
|
|
269 | 304 | },
|
270 | 305 | {
|
271 | 306 | "cell_type": "code",
|
272 |
| - "execution_count": 7, |
| 307 | + "execution_count": null, |
273 | 308 | "id": "51c60e1d",
|
274 | 309 | "metadata": {},
|
275 | 310 | "outputs": [
|
|
281 | 316 | "INFO:root:Using native PyMPDATA implementation (should be ~3x faster than Strang splitting)\n",
|
282 | 317 | "INFO:root:Diffusivity range: 0.010 to 2.010\n",
|
283 | 318 | "INFO:root:Using balanced mu coefficient: 0.050000\n",
|
284 |
| - "INFO:root:At step 10000/100000\n", |
285 |
| - "INFO:root:At step 20000/100000\n", |
286 |
| - "INFO:root:At step 30000/100000\n", |
287 |
| - "INFO:root:At step 40000/100000\n", |
288 |
| - "INFO:root:At step 50000/100000\n", |
289 |
| - "INFO:root:At step 60000/100000\n", |
290 |
| - "INFO:root:At step 70000/100000\n", |
291 |
| - "INFO:root:At step 80000/100000\n", |
292 |
| - "INFO:root:At step 90000/100000\n", |
293 |
| - "INFO:root:At step 100000/100000\n", |
294 |
| - "INFO:root:Simulation completed!\n", |
295 |
| - "INFO:root:Mass conservation: initial=64.000000, final=4.440033\n", |
296 |
| - "INFO:root:Relative mass change: 9.31e+01%\n" |
| 319 | + "INFO:root:At step 10000/100000\n" |
297 | 320 | ]
|
298 | 321 | }
|
299 | 322 | ],
|
300 | 323 | "source": [
|
301 |
| - "start_time = time.perf_counter()\n", |
302 |
| - "pympdata_result = solutions.pympdata_solution(simulation_args)\n", |
303 |
| - "pympdata_time = time.perf_counter() - start_time" |
| 324 | + "with Timer() as timer:\n", |
| 325 | + " pympdata_result = solutions.pympdata_solution(simulation_args)\n", |
| 326 | + " \n", |
| 327 | + "pympdata_time = timer.time" |
304 | 328 | ]
|
305 | 329 | },
|
306 | 330 | {
|
|
390 | 414 | },
|
391 | 415 | {
|
392 | 416 | "cell_type": "code",
|
393 |
| - "execution_count": 12, |
| 417 | + "execution_count": null, |
394 | 418 | "id": "f2ac8a37",
|
395 | 419 | "metadata": {},
|
396 | 420 | "outputs": [],
|
397 | 421 | "source": [
|
398 | 422 | "assert np.allclose(\n",
|
399 |
| - " pympdata_result.kymograph_result, py_pde_result.kymograph_result, atol=5*1e-1\n", |
| 423 | + " pympdata_result.kymograph_result, py_pde_result.kymograph_result, atol=0.2\n", |
400 | 424 | "), \"Kymograph results do not match.\""
|
401 | 425 | ]
|
402 | 426 | },
|
|
0 commit comments