Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active July 5, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/ccab53f9f3e2b1fb672566abe3f9c996 to your computer and use it in GitHub Desktop.
Save zeffii/ccab53f9f3e2b1fb672566abe3f9c996 to your computer and use it in GitHub Desktop.
snlite reaction diffusion test
"""
in steps s d=40 n=2
out nparray s
out nparray2 s
"""
import numpy as np
from numpy import zeros, random
# pylint: disable=c0103
n = 100 # size of grid n*n
Dh = 1. / n # spatial res, assuming space is [0, 1] * [0, 1]
Dt = 0.02 # temporal res
a, b, c, d, h, k = 1., -1, 2., -1.5, 1., 1. # param values
Du = 0.0001 # diffusion constant of U
Dv = 0.0006 # dif V
def initialize():
global u, v, nextu, nextv
u = zeros([n, n])
v = zeros([n, n])
for x in range(n):
for y in range(n):
u[x, y] = 1. + random.uniform(-0.03, 0.03) # small noise is added
v[x, y] = 1. + random.uniform(-0.03, 0.03) # small noise is added
nextu = zeros([n, n])
nextv = zeros([n, n])
def update():
global u, v, nextu, nextv
for x in range(n):
for y in range(n):
# state-transition function
uC, uR, uL, uU, uD = u[x, y], u[(x+1) % n, y], u[(x-1)%n, y], u[x, (y+1) % n], u[x, (y-1) % n]
vC, vR, vL, vU, vD = v[x, y], v[(x+1) % n, y], v[(x-1)%n, y], v[x, (y+1) % n], v[x, (y-1) % n]
uLap = (uR + uL + uU + uD - 4 * uC) / (Dh**2)
vLap = (vR + vL + vU + vD - 4 * vC) / (Dh**2)
nextu[x, y] = uC + (a*(uC-h) + b*(vC-k) + Du * uLap) * Dt
nextv[x, y] = vC + (c*(uC-h) + d*(vC-k) + Dv * vLap) * Dt
u, nextu = nextu, u
v, nextv = nextv, v
initialize()
for iteration in range(steps):
update()
nparray.append(u.ravel())
nparray2.append(v.ravel())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment