pyodide/benchmark/benchmarks/grayscott.py

42 lines
1.1 KiB
Python
Raw Normal View History

2018-10-03 18:59:01 +00:00
# http://stackoverflow.com/questions/26823312/numba-or-cython-acceleration-in-reaction-diffusion-algorithm
# setup: pass
# run: grayscott(40, 0.16, 0.08, 0.04, 0.06)
2018-04-05 22:07:33 +00:00
2018-10-03 18:59:01 +00:00
# pythran export grayscott(int, float, float, float, float)
2018-04-05 22:07:33 +00:00
import numpy as np
2018-10-03 12:38:48 +00:00
2018-10-03 18:59:01 +00:00
2018-04-05 22:07:33 +00:00
def grayscott(counts, Du, Dv, F, k):
2018-10-03 12:38:48 +00:00
n = 100
2018-10-03 18:59:01 +00:00
U = np.zeros((n + 2, n + 2), dtype=np.float32)
V = np.zeros((n + 2, n + 2), dtype=np.float32)
u, v = U[1:-1, 1:-1], V[1:-1, 1:-1]
2018-04-05 22:07:33 +00:00
r = 20
u[:] = 1.0
U[n // 2 - r : n // 2 + r, n // 2 - r : n // 2 + r] = 0.50
V[n // 2 - r : n // 2 + r, n // 2 - r : n // 2 + r] = 0.25
2018-10-03 18:59:01 +00:00
u += 0.15 * np.random.random((n, n))
v += 0.15 * np.random.random((n, n))
2018-04-05 22:07:33 +00:00
for i in range(counts):
Lu = (
U[0:-2, 1:-1]
+ U[1:-1, 0:-2]
- 4 * U[1:-1, 1:-1]
+ U[1:-1, 2:]
+ U[2:, 1:-1]
)
Lv = (
V[0:-2, 1:-1]
+ V[1:-1, 0:-2]
- 4 * V[1:-1, 1:-1]
+ V[1:-1, 2:]
+ V[2:, 1:-1]
)
2018-10-03 18:59:01 +00:00
uvv = u * v * v
u += Du * Lu - uvv + F * (1 - u)
v += Dv * Lv + uvv - (F + k) * v
2018-04-05 22:07:33 +00:00
return V