-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChemical-Reactor.py
42 lines (33 loc) · 1.01 KB
/
Chemical-Reactor.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
37
38
39
40
41
42
import matplotlib.pyplot as plt
def chemical_reactor(A, B, C, k1, k2, end_time, dt):
time = 0
substance_A = []
substance_B = []
substance_C = []
time_points = []
while time <= end_time:
substance_A.append(A)
substance_B.append(B)
substance_C.append(C)
time_points.append(time)
time += dt
val = k2 * C - k1 * A * B
A += val * dt
B += val * dt
C += (2 * k1 * A * B - 2 * k2 * C) * dt
plt.clf()
plt.title("Simulation of a Chemical Reactor")
plt.xlabel("Time (s)")
plt.ylabel("Quantity (gm)")
plt.xlim(0, end_time)
plt.plot(time_points, substance_A, label="Substance A")
plt.plot(time_points, substance_B, label="Substance B")
plt.plot(time_points, substance_C, label="Substance C")
plt.legend()
plt.grid()
plt.pause(0.001)
plt.show()
def main():
chemical_reactor(100, 50, 0, 0.008, 0.002, 10, 0.1)
if __name__ == "__main__":
main()