-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
155 lines (119 loc) · 3.65 KB
/
simulation.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
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Simulation of the movement of a pneumatic balloon in a gif-format.
"""
from dataclasses import dataclass
from typing import NamedTuple
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patches, pyplot
from celluloid import Camera
class PhysicProperties(NamedTuple):
pressure: float
gravity: float
mass: float
tau: float
epsilon: float
@dataclass
class Timer:
start: float
end: float
step: float
@dataclass
class Variables:
length: float
start_x: float
funcs: int
Ax: float
Bx: float
Ay: float
By: float
Vy: float
C: float
def functions(arguments: np.array, consts: Variables) -> np.array:
x1, x2, y, f1, f2 = arguments
return np.array(
[
x1 + y * np.cos(3 * np.pi / 2 - f1) - consts.Ax,
x2 + y * np.cos(3 * np.pi / 2 + f2) - consts.Bx,
y + y * np.sin(3 * np.pi / 2 - f1) - consts.Ay,
(f1 + f2) * y + (x2 - x1) - consts.C,
y + y * np.sin(3 * np.pi / 2 + f2) - consts.By,
]
)
def make_patch(x: float, angle1: float, angle2: float) -> patches.Arc:
"""Creates one part of a balloon."""
return patches.Arc(
(x, X_values[2]),
X_values[2] * 2,
X_values[2] * 2,
angle=0,
theta1=angle1,
theta2=angle2,
linewidth=2,
)
_data = PhysicProperties(pressure=2000, gravity=9.8, mass=100, tau=0.005, epsilon=0.001)
_time = Timer(start=0, end=2.65, step=0.01)
_vars = Variables(
length=0,
start_x=0.5,
funcs=5,
Ax=-0.353,
Bx=0.353,
Ay=0.3,
By=0.3,
C=3 * np.pi / 8,
Vy=0.5,
)
X_values = np.array([_vars.start_x for _ in range(_vars.funcs)])
figure, axis = pyplot.subplots()
camera = Camera(figure)
while _time.start < _time.end:
# Makes all arithmetic
_vars.Ay = _vars.Ay + _vars.Vy * _time.step
_vars.By = _vars.Ay
while ...:
F_values = functions(X_values, _vars)
X_values = X_values - F_values * _data.tau
if all([abs(el) < _data.epsilon for el in F_values]):
break
_vars.length = X_values[1] - X_values[0]
F = _data.pressure * _vars.length - _data.mass * _data.gravity
_vars.Vy = _vars.Vy + (1 / _data.mass) * F * _time.step
_time.start += _time.step
# Output results in a frame
e1 = make_patch(X_values[0], 270 - X_values[3] * 180 / np.pi, 270)
e2 = make_patch(X_values[1], 270, 270 + X_values[4] * 180 / np.pi)
axis.add_patch(e1)
axis.add_patch(e2)
figure.suptitle(
"Simulation of the movement of a pneumatic balloon",
fontsize=14,
fontweight="bold",
)
axis.set_title(
f"pressure={_data.pressure} Pa, mass={_data.mass} kg, gravity={_data.gravity} m/s",
style="italic",
)
pyplot.text(-0.55, 0.365, "time = %.2f s" % _time.start, size="large")
pyplot.text(X_values[0] + 0.025, X_values[2] + 0.025, "x1")
pyplot.text(X_values[1] + 0.025, X_values[2] + 0.025, "x2")
pyplot.plot(X_values[0], X_values[2], "bo")
pyplot.plot(X_values[1], X_values[2], "bo")
direction = 1 if F > 0 else -1
pyplot.annotate(
"F",
xy=(
(_vars.Ax + _vars.Bx) / 2,
_vars.Ay + abs(F) * 0.000075 * direction + 0.05 * direction,
),
xytext=((_vars.Ax + _vars.Bx) / 2 - 0.01, _vars.Ay),
arrowprops=dict(facecolor="orange", shrink=0.0005, width=3, headwidth=9),
)
pyplot.xlim([-0.6, 0.6])
pyplot.ylim([0, 0.4])
camera.snap()
print(
f"[LOG]: {int(_time.start // _time.step)}/{int(_time.end // _time.step)} frame was created."
)
animation = camera.animate()
animation.save("animation.gif", fps=10)