-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (111 loc) · 2.93 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import argparse
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import molecules as mol
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def move_cursor(y, x):
print("\033[%d;%dH" % (y, x))
parser = argparse.ArgumentParser(
description=(
"plot the evolution of a 2-dimensional system of"
"particles using cell linked-lists method"
),
epilog=(
"the letters in the plot legend stand for: "
"U, the potential energy of the system; "
"K, the kinetic energy of the system; "
"U + K, the total energy of the system; "
"T, the temperature, measure as mean value of K"
),
)
parser.add_argument(
"particles",
type=int,
nargs="?",
help=(
"the number of particles in the system. The larger this number, "
"the longer will be the computation time of each step."
),
)
parser.add_argument(
"-s",
"--size",
type=float,
nargs=2,
default=[100, 100],
metavar=("WIDTH", "HEIGHT"),
help="desired width and height of the box. Default is 100 and 100.",
)
parser.add_argument(
"-c",
"--cell_size",
type=float,
nargs="?",
default=3.0,
const=3.0,
help="size of the cells in which the bow will be divided. Default is 3.0.",
)
parser.add_argument(
"-t",
"--temperature",
type=float,
nargs="?",
default=100.0,
const=100.0,
help="initial temperature of the system in energy units. Default is 100.",
)
args = parser.parse_args()
size = args.size
cell_size = args.cell_size
particles = args.particles
init_temp = args.temperature
if not particles:
particles = 2000
grid = mol.Grid(
size=size, cell_size=cell_size, particles=particles, init_temp=init_temp
)
time = []
potential = []
kinetic = []
energy = []
temperature = []
fig, ax = plt.subplots()
def animate(frame):
potential_now = grid.potential_energy()
kinetic_now = grid.kinetic_energy()
energy_now = grid.total_energy()
temperature_now = grid.temperature()
time.append(grid.time)
potential.append(potential_now)
kinetic.append(kinetic_now)
energy.append(energy_now)
temperature.append(temperature_now)
move_cursor(0, 0)
print(
f"Time = {grid.time:.3e} seconds",
f"U = {potential_now:.3e} Joules",
f"K = {kinetic_now:.3e} Joules",
f"U + K = {energy_now:.3e} Joules",
f"T = {temperature_now:.3e} Joules",
sep="\n",
)
ax.clear()
ax.set(xlabel="Time, in seconds")
ax.plot(time, potential, label="U")
ax.plot(time, kinetic, label="K")
ax.plot(time, energy, label="U + K")
ax.plot(time, temperature, label="T")
ax.legend(
loc="upper center",
ncol=4,
bbox_to_anchor=(0.5, 1.10),
fancybox=True,
shadow=True,
)
ax.grid(axis="y")
grid.update()
clear_screen()
ani = FuncAnimation(fig, animate)
plt.show()