-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
32 lines (24 loc) · 691 Bytes
/
solver.py
File metadata and controls
32 lines (24 loc) · 691 Bytes
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
"""A solver for the 1D diffusion equation."""
import numpy as np
np.set_printoptions(formatter={"float": "{: 6.1f}".format})
def solve1d(concentration, spacing, time_step, diffusivity):
flux = -diffusivity * np.diff(concentration) / spacing
concentration[1:-1] -= time_step * np.diff(flux) / spacing
return concentration
def _example():
# list of actual parameters
D = 100
Lx = 10
dx = 0.5
C1 = 500
C2 = 0
C = np.empty(Lx)
C[: int(Lx/2)] = C1
C[int(Lx/2):] = C2
dt = dx * dx / D / 5
print(C)
for _ in range(1,20):
C = solve1d(C,dx,dt,D)
print(C)
if __name__ == "__main__":
_example()