Skip to content

Commit 9daeb12

Browse files
committed
Adjust the tolerance in the comparison between methods
1 parent ca8516a commit 9daeb12

File tree

2 files changed

+69
-45
lines changed

2 files changed

+69
-45
lines changed

examples/PyMPDATA_examples/comparisons_against_pypde/diffusion_equation_with_spatial_dependence/diffusion_equation_with_spatial_dependence.ipynb

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,37 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": 1,
28+
"execution_count": 3,
2929
"id": "46e47a0b",
3030
"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+
],
3241
"source": [
3342
"%load_ext autoreload\n",
3443
"%autoreload 2"
3544
]
3645
},
3746
{
3847
"cell_type": "code",
39-
"execution_count": 2,
48+
"execution_count": 4,
4049
"id": "c91b4ebc",
4150
"metadata": {},
4251
"outputs": [],
4352
"source": [
4453
"import logging\n",
54+
"import time\n",
4555
"\n",
4656
"import numpy as np\n",
4757
"import pde as py_pde\n",
4858
"from IPython.display import display\n",
49-
"import time\n",
50-
"\n",
5159
"from PyMPDATA_examples.comparisons_against_pypde.diffusion_equation_with_spatial_dependence import (\n",
5260
" solutions,\n",
5361
")\n",
@@ -71,12 +79,14 @@
7179
"id": "76f14b27",
7280
"metadata": {},
7381
"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)."
7585
]
7686
},
7787
{
7888
"cell_type": "code",
79-
"execution_count": 3,
89+
"execution_count": null,
8090
"id": "209d79d2",
8191
"metadata": {},
8292
"outputs": [
@@ -85,15 +95,15 @@
8595
"output_type": "stream",
8696
"text": [
8797
"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",
90100
"INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
91101
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
92102
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
93103
"INFO:pde.tools.numba:Compile `set_valid_bcs` with parallel=True\n",
94104
"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",
97107
"INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
98108
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
99109
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
@@ -105,8 +115,8 @@
105115
"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",
106116
"INFO:pde.tools.numba:Compile `_lambdifygenerated` with parallel=True\n",
107117
"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",
109118
"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",
110120
"INFO:pde.pdes.PDE:RHS for `c` has signature ('c', 't', 'none', 'bc_args', 'x')\n",
111121
"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",
112122
"INFO:pde.solvers.ExplicitSolver:Init explicit Euler stepper with dt=0.001\n",
@@ -126,10 +136,10 @@
126136
{
127137
"data": {
128138
"text/plain": [
129-
"<pde.tools.plotting.PlotReference at 0x29102b96c40>"
139+
"<pde.tools.plotting.PlotReference at 0x12a29a66d00>"
130140
]
131141
},
132-
"execution_count": 3,
142+
"execution_count": 5,
133143
"metadata": {},
134144
"output_type": "execute_result"
135145
}
@@ -140,7 +150,6 @@
140150
"term_2 = f\"dot(gradient({diffusivity}), gradient(c))\"\n",
141151
"eq = py_pde.PDE({\"c\": f\"{term_1} + {term_2}\"}, bc={\"value\": 0})\n",
142152
"\n",
143-
"\n",
144153
"grid = py_pde.CartesianGrid([[-5, 5]], 64)\n",
145154
"field = py_pde.ScalarField(grid, 1)\n",
146155
"\n",
@@ -160,7 +169,33 @@
160169
},
161170
{
162171
"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,
164199
"id": "ba25763a",
165200
"metadata": {},
166201
"outputs": [
@@ -169,15 +204,15 @@
169204
"output_type": "stream",
170205
"text": [
171206
"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",
174209
"INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
175210
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
176211
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
177212
"INFO:pde.tools.numba:Compile `set_valid_bcs` with parallel=True\n",
178213
"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",
181216
"INFO:pde.tools.numba:Compile `set_valid` with parallel=True\n",
182217
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
183218
"INFO:pde.tools.numba:Compile `virtual_point` with parallel=True\n",
@@ -189,8 +224,8 @@
189224
"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",
190225
"INFO:pde.tools.numba:Compile `_lambdifygenerated` with parallel=True\n",
191226
"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",
193227
"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",
194229
"INFO:pde.pdes.PDE:RHS for `c` has signature ('c', 't', 'none', 'bc_args', 'x')\n",
195230
"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",
196231
"INFO:pde.solvers.ExplicitSolver:Init explicit Euler stepper with dt=0.001\n",
@@ -199,9 +234,9 @@
199234
}
200235
],
201236
"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"
205240
]
206241
},
207242
{
@@ -214,7 +249,7 @@
214249
},
215250
{
216251
"cell_type": "code",
217-
"execution_count": 5,
252+
"execution_count": 7,
218253
"id": "42c6278f",
219254
"metadata": {},
220255
"outputs": [],
@@ -269,7 +304,7 @@
269304
},
270305
{
271306
"cell_type": "code",
272-
"execution_count": 7,
307+
"execution_count": null,
273308
"id": "51c60e1d",
274309
"metadata": {},
275310
"outputs": [
@@ -281,26 +316,15 @@
281316
"INFO:root:Using native PyMPDATA implementation (should be ~3x faster than Strang splitting)\n",
282317
"INFO:root:Diffusivity range: 0.010 to 2.010\n",
283318
"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"
297320
]
298321
}
299322
],
300323
"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"
304328
]
305329
},
306330
{
@@ -390,13 +414,13 @@
390414
},
391415
{
392416
"cell_type": "code",
393-
"execution_count": 12,
417+
"execution_count": null,
394418
"id": "f2ac8a37",
395419
"metadata": {},
396420
"outputs": [],
397421
"source": [
398422
"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",
400424
"), \"Kymograph results do not match.\""
401425
]
402426
},

tests/smoke_tests/comparison_against_pypde/diffusion_equation_with_spatial_dependence/test_similarity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ def test_similarity():
3838
pympdata_result.kymograph_result.shape == py_pde_result.kymograph_result.shape
3939
), "Kymograph results from both implementations should have the same shape."
4040
assert np.allclose(
41-
pympdata_result.kymograph_result, py_pde_result.kymograph_result, atol=5 * 1e-1
41+
pympdata_result.kymograph_result, py_pde_result.kymograph_result, atol=0.2
4242
), "Kymograph results from both implementations should be similar within the tolerance."

0 commit comments

Comments
 (0)