-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsimple_diffusion_demo.py
36 lines (26 loc) · 1.13 KB
/
simple_diffusion_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import netomaton as ntm
if __name__ == "__main__":
"""
Simulates the 1D Diffusion Equation (also known as the heat equation):
∂u/∂t = α ∂²u/∂x²
Each of the 120 nodes represents a body that can contain some amount of heat. Reproduces the plot at the top of
Wolfram's NKS, page 163.
See: https://www.wolframscience.com/nks/p163--partial-differential-equations/
See: http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html
"""
space = np.linspace(25, -25, 120)
initial_conditions = [np.exp(-x ** 2) for x in space]
network = ntm.topology.cellular_automaton(120)
a = 0.25
dt = .5
dx = .5
F = a * dt / dx ** 2
def activity_rule(ctx):
current = ctx.current_activity
left = ctx.neighbourhood_activities[0]
right = ctx.neighbourhood_activities[2]
return current + F * (right - 2 * current + left)
trajectory = ntm.evolve(initial_conditions=initial_conditions, network=network,
activity_rule=activity_rule, timesteps=75)
ntm.plot_activities(trajectory)