|
50 | 50 | "source": [ |
51 | 51 | "# Generate some particles\n", |
52 | 52 | "N = 10**7\n", |
53 | | - "box = 123.\n", |
| 53 | + "box = 123.0\n", |
54 | 54 | "nthread = 32\n", |
55 | 55 | "\n", |
56 | 56 | "rng = np.random.default_rng(123)\n", |
57 | | - "pos = rng.random((N,3), dtype=np.float32) * box" |
| 57 | + "pos = rng.random((N, 3), dtype=np.float32) * box" |
58 | 58 | ] |
59 | 59 | }, |
60 | 60 | { |
|
98 | 98 | "metadata": {}, |
99 | 99 | "outputs": [], |
100 | 100 | "source": [ |
101 | | - "dens = np.zeros((ngrid,ngrid,ngrid), dtype=np.float32)\n", |
| 101 | + "dens = np.zeros((ngrid, ngrid, ngrid), dtype=np.float32)\n", |
102 | 102 | "dens = tsc_parallel(pos, dens, box, nthread=nthread)" |
103 | 103 | ] |
104 | 104 | }, |
|
202 | 202 | "source": [ |
203 | 203 | "fig, ax = plt.subplots(dpi=144)\n", |
204 | 204 | "ax.set_aspect('equal')\n", |
205 | | - "ax.scatter(hpos[:,0], hpos[:,1])\n", |
| 205 | + "ax.scatter(hpos[:, 0], hpos[:, 1])\n", |
206 | 206 | "ax.set_xlabel('$x$ [Mpc/$h$]')\n", |
207 | 207 | "ax.set_ylabel('$y$ [Mpc/$h$]')\n", |
208 | 208 | "ax.set_xlim(0, L)\n", |
|
247 | 247 | "dens = tsc_parallel(hpos, 64, L, nthread=nthread)\n", |
248 | 248 | "\n", |
249 | 249 | "fig, ax = plt.subplots(dpi=144)\n", |
250 | | - "ax.imshow(dens.sum(axis=2).T,\n", |
| 250 | + "ax.imshow(\n", |
| 251 | + " dens.sum(axis=2).T,\n", |
251 | 252 | " origin='lower',\n", |
252 | 253 | " interpolation='none',\n", |
253 | | - " extent=(0,L,0,L),\n", |
254 | | - " )\n", |
| 254 | + " extent=(0, L, 0, L),\n", |
| 255 | + ")\n", |
255 | 256 | "ax.set_xlabel('$x$ [Mpc/$h$]')\n", |
256 | 257 | "ax.set_ylabel('$y$ [Mpc/$h$]')" |
257 | 258 | ] |
|
323 | 324 | } |
324 | 325 | ], |
325 | 326 | "source": [ |
326 | | - "dens = np.zeros((ngrid,ngrid,ngrid), dtype=np.float32)\n", |
| 327 | + "dens = np.zeros((ngrid, ngrid, ngrid), dtype=np.float32)\n", |
327 | 328 | "%timeit tsc_parallel(pos, dens, box, nthread=nthread)\n", |
328 | 329 | "\n", |
329 | | - "dens64 = np.zeros((ngrid,ngrid,ngrid), dtype=np.float64)\n", |
| 330 | + "dens64 = np.zeros((ngrid, ngrid, ngrid), dtype=np.float64)\n", |
330 | 331 | "pos64 = pos.astype(np.float64)\n", |
331 | 332 | "%timeit tsc_parallel(pos64, dens64, box, nthread=nthread)" |
332 | 333 | ] |
|
389 | 390 | ], |
390 | 391 | "source": [ |
391 | 392 | "def test_precision(N, ngrid):\n", |
392 | | - " pos = rng.random((N,3), dtype=np.float32) * box\n", |
393 | | - " dens = np.zeros((ngrid,ngrid,ngrid), dtype=np.float32)\n", |
| 393 | + " pos = rng.random((N, 3), dtype=np.float32) * box\n", |
| 394 | + " dens = np.zeros((ngrid, ngrid, ngrid), dtype=np.float32)\n", |
394 | 395 | " tsc_parallel(pos, dens, box, nthread=nthread)\n", |
395 | 396 | " print(f'Total dens, 32-bit computation, 32-bit summation type: {dens.sum()}')\n", |
396 | | - " print(f'Total dens, 32-bit computation, 64-bit summation type: {dens.sum(dtype=np.float64)}')\n", |
| 397 | + " print(\n", |
| 398 | + " f'Total dens, 32-bit computation, 64-bit summation type: {dens.sum(dtype=np.float64)}'\n", |
| 399 | + " )\n", |
397 | 400 | "\n", |
398 | | - " dens = np.zeros((ngrid,ngrid,ngrid), dtype=np.float64)\n", |
| 401 | + " dens = np.zeros((ngrid, ngrid, ngrid), dtype=np.float64)\n", |
399 | 402 | " tsc_parallel(pos, dens, box, nthread=nthread)\n", |
400 | | - " print(f'Total dens, 64-bit computation, 64-bit summation type: {dens.sum(dtype=np.float64)}')\n", |
| 403 | + " print(\n", |
| 404 | + " f'Total dens, 64-bit computation, 64-bit summation type: {dens.sum(dtype=np.float64)}'\n", |
| 405 | + " )\n", |
| 406 | + "\n", |
401 | 407 | "\n", |
402 | 408 | "print('Case 1:')\n", |
403 | 409 | "test_precision(10**7, 256)\n", |
404 | 410 | "print('Case 2:')\n", |
405 | | - "test_precision(5*10**9, 2048)" |
| 411 | + "test_precision(5 * 10**9, 2048)" |
406 | 412 | ] |
407 | 413 | }, |
408 | 414 | { |
|
429 | 435 | } |
430 | 436 | ], |
431 | 437 | "source": [ |
432 | | - "pos = rng.random((5 * 10**9,3), dtype=np.float32) * box\n", |
| 438 | + "pos = rng.random((5 * 10**9, 3), dtype=np.float32) * box\n", |
433 | 439 | "dens = tsc_parallel(pos, 2048, box, nthread=nthread)\n", |
434 | 440 | "\n", |
435 | 441 | "delta = dens / dens.mean(dtype=np.float64) - 1\n", |
|
0 commit comments